| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h" | 7 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h" |
| 8 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 8 #include "content/public/test/test_browser_thread.h" | 9 #include "content/public/test/test_browser_thread.h" |
| 9 #include "google_apis/gaia/google_service_auth_error.h" | 10 #include "google_apis/gaia/google_service_auth_error.h" |
| 10 #include "net/base/host_port_pair.h" | 11 #include "net/base/host_port_pair.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/http/http_request_headers.h" | 13 #include "net/http/http_request_headers.h" |
| 13 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 using testing::NiceMock; | 19 using testing::NiceMock; |
| 19 | 20 |
| 20 namespace local_discovery { | 21 namespace local_discovery { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const char kSampleConfirmResponse[] = "{" | 25 const char kSampleConfirmResponse[] = "{" |
| 25 " \"success\": true" | 26 " \"success\": true" |
| 26 "}"; | 27 "}"; |
| 27 | 28 |
| 28 const char kFailedConfirmResponse[] = "{" | 29 const char kFailedConfirmResponse[] = "{" |
| 29 " \"success\": false" | 30 " \"success\": false" |
| 30 "}"; | 31 "}"; |
| 31 | 32 |
| 32 const char kFailedConfirmResponseBadJson[] = "[" | 33 const char kFailedConfirmResponseBadJson[] = "[" |
| 33 " \"success\"" | 34 " \"success\"" |
| 34 "]"; | 35 "]"; |
| 35 | 36 |
| 37 const char kAccountId[] = "account_id"; |
| 38 |
| 36 class TestOAuth2TokenService : public OAuth2TokenService { | 39 class TestOAuth2TokenService : public OAuth2TokenService { |
| 37 public: | 40 public: |
| 38 explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context) | 41 explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context) |
| 39 : request_context_(request_context) { | 42 : request_context_(request_context) { |
| 40 } | 43 } |
| 41 protected: | 44 protected: |
| 42 virtual std::string GetRefreshToken() OVERRIDE { | 45 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE { |
| 43 return "SampleToken"; | 46 return "SampleToken"; |
| 44 } | 47 } |
| 45 | 48 |
| 46 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { | 49 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { |
| 47 return request_context_.get(); | 50 return request_context_.get(); |
| 48 } | 51 } |
| 49 | 52 |
| 50 private: | 53 private: |
| 51 scoped_refptr<net::URLRequestContextGetter> request_context_; | 54 scoped_refptr<net::URLRequestContextGetter> request_context_; |
| 52 }; | 55 }; |
| 53 | 56 |
| 54 class MockableConfirmCallback { | 57 class MockableConfirmCallback { |
| 55 public: | 58 public: |
| 56 MOCK_METHOD1(ConfirmCallback, void(PrivetConfirmApiCallFlow::Status)); | 59 MOCK_METHOD1(ConfirmCallback, void(PrivetConfirmApiCallFlow::Status)); |
| 57 | 60 |
| 58 PrivetConfirmApiCallFlow::ResponseCallback callback() { | 61 PrivetConfirmApiCallFlow::ResponseCallback callback() { |
| 59 return base::Bind(&MockableConfirmCallback::ConfirmCallback, | 62 return base::Bind(&MockableConfirmCallback::ConfirmCallback, |
| 60 base::Unretained(this)); | 63 base::Unretained(this)); |
| 61 } | 64 } |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 class PrivetConfirmApiFlowTest : public testing::Test { | 67 class PrivetConfirmApiFlowTest : public testing::Test { |
| 65 public: | 68 public: |
| 66 PrivetConfirmApiFlowTest() | 69 PrivetConfirmApiFlowTest() |
| 67 : ui_thread_(content::BrowserThread::UI, | 70 : ui_thread_(content::BrowserThread::UI, |
| 68 &loop_), | 71 &loop_), |
| 69 request_context_(new net::TestURLRequestContextGetter( | 72 request_context_(new net::TestURLRequestContextGetter( |
| 70 base::MessageLoopProxy::current())), | 73 base::MessageLoopProxy::current())), |
| 71 token_service_(request_context_.get()) { | 74 token_service_(request_context_.get()), |
| 75 account_id_(kAccountId) { |
| 72 ui_thread_.Stop(); // HACK: Fake being on the UI thread | 76 ui_thread_.Stop(); // HACK: Fake being on the UI thread |
| 73 } | 77 } |
| 74 | 78 |
| 75 virtual ~PrivetConfirmApiFlowTest() { | 79 virtual ~PrivetConfirmApiFlowTest() { |
| 76 } | 80 } |
| 77 | 81 |
| 78 protected: | 82 protected: |
| 79 base::MessageLoopForUI loop_; | 83 base::MessageLoopForUI loop_; |
| 80 content::TestBrowserThread ui_thread_; | 84 content::TestBrowserThread ui_thread_; |
| 81 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 85 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 82 net::TestURLFetcherFactory fetcher_factory_; | 86 net::TestURLFetcherFactory fetcher_factory_; |
| 83 TestOAuth2TokenService token_service_; | 87 TestOAuth2TokenService token_service_; |
| 84 MockableConfirmCallback callback_; | 88 MockableConfirmCallback callback_; |
| 89 std::string account_id_; |
| 85 }; | 90 }; |
| 86 | 91 |
| 87 TEST_F(PrivetConfirmApiFlowTest, SuccessOAuth2) { | 92 TEST_F(PrivetConfirmApiFlowTest, SuccessOAuth2) { |
| 88 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), | 93 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), |
| 89 &token_service_, | 94 &token_service_, |
| 95 account_id_, |
| 90 GURL("http://SoMeUrL.com"), | 96 GURL("http://SoMeUrL.com"), |
| 91 callback_.callback()); | 97 callback_.callback()); |
| 92 | 98 |
| 93 confirm_flow.Start(); | 99 confirm_flow.Start(); |
| 94 | 100 |
| 95 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | 101 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 96 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 102 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 97 | 103 |
| 98 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); | 104 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); |
| 99 | 105 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 fetcher->set_response_code(200); | 148 fetcher->set_response_code(200); |
| 143 | 149 |
| 144 EXPECT_CALL(callback_, ConfirmCallback(PrivetConfirmApiCallFlow::SUCCESS)); | 150 EXPECT_CALL(callback_, ConfirmCallback(PrivetConfirmApiCallFlow::SUCCESS)); |
| 145 | 151 |
| 146 fetcher->delegate()->OnURLFetchComplete(fetcher); | 152 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 147 } | 153 } |
| 148 | 154 |
| 149 TEST_F(PrivetConfirmApiFlowTest, BadToken) { | 155 TEST_F(PrivetConfirmApiFlowTest, BadToken) { |
| 150 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), | 156 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), |
| 151 &token_service_, | 157 &token_service_, |
| 158 account_id_, |
| 152 GURL("http://SoMeUrL.com"), | 159 GURL("http://SoMeUrL.com"), |
| 153 callback_.callback()); | 160 callback_.callback()); |
| 154 | 161 |
| 155 confirm_flow.Start(); | 162 confirm_flow.Start(); |
| 156 | 163 |
| 157 EXPECT_CALL(callback_, | 164 EXPECT_CALL(callback_, |
| 158 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_TOKEN)); | 165 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_TOKEN)); |
| 159 confirm_flow.OnGetTokenFailure(NULL, GoogleServiceAuthError( | 166 confirm_flow.OnGetTokenFailure(NULL, GoogleServiceAuthError( |
| 160 GoogleServiceAuthError::USER_NOT_SIGNED_UP)); | 167 GoogleServiceAuthError::USER_NOT_SIGNED_UP)); |
| 161 } | 168 } |
| 162 | 169 |
| 163 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) { | 170 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) { |
| 164 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), | 171 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), |
| 165 &token_service_, | 172 &token_service_, |
| 173 account_id_, |
| 166 GURL("http://SoMeUrL.com"), | 174 GURL("http://SoMeUrL.com"), |
| 167 callback_.callback()); | 175 callback_.callback()); |
| 168 | 176 |
| 169 confirm_flow.Start(); | 177 confirm_flow.Start(); |
| 170 | 178 |
| 171 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | 179 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 172 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 180 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 173 | 181 |
| 174 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); | 182 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); |
| 175 | 183 |
| 176 fetcher->SetResponseString(kFailedConfirmResponse); | 184 fetcher->SetResponseString(kFailedConfirmResponse); |
| 177 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, | 185 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, |
| 178 net::OK)); | 186 net::OK)); |
| 179 fetcher->set_response_code(200); | 187 fetcher->set_response_code(200); |
| 180 | 188 |
| 181 EXPECT_CALL(callback_, | 189 EXPECT_CALL(callback_, |
| 182 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_FROM_SERVER)); | 190 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_FROM_SERVER)); |
| 183 | 191 |
| 184 fetcher->delegate()->OnURLFetchComplete(fetcher); | 192 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 185 } | 193 } |
| 186 | 194 |
| 187 TEST_F(PrivetConfirmApiFlowTest, BadJson) { | 195 TEST_F(PrivetConfirmApiFlowTest, BadJson) { |
| 188 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), | 196 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), |
| 189 &token_service_, | 197 &token_service_, |
| 198 account_id_, |
| 190 GURL("http://SoMeUrL.com"), | 199 GURL("http://SoMeUrL.com"), |
| 191 callback_.callback()); | 200 callback_.callback()); |
| 192 | 201 |
| 193 confirm_flow.Start(); | 202 confirm_flow.Start(); |
| 194 | 203 |
| 195 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | 204 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 196 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 205 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 197 | 206 |
| 198 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); | 207 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); |
| 199 | 208 |
| 200 fetcher->SetResponseString(kFailedConfirmResponseBadJson); | 209 fetcher->SetResponseString(kFailedConfirmResponseBadJson); |
| 201 fetcher->set_status(net::URLRequestStatus( | 210 fetcher->set_status(net::URLRequestStatus( |
| 202 net::URLRequestStatus::SUCCESS, | 211 net::URLRequestStatus::SUCCESS, |
| 203 net::OK)); | 212 net::OK)); |
| 204 fetcher->set_response_code(200); | 213 fetcher->set_response_code(200); |
| 205 | 214 |
| 206 EXPECT_CALL(callback_, ConfirmCallback | 215 EXPECT_CALL(callback_, ConfirmCallback |
| 207 (PrivetConfirmApiCallFlow::ERROR_MALFORMED_RESPONSE)); | 216 (PrivetConfirmApiCallFlow::ERROR_MALFORMED_RESPONSE)); |
| 208 | 217 |
| 209 fetcher->delegate()->OnURLFetchComplete(fetcher); | 218 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 210 } | 219 } |
| 211 | 220 |
| 212 } // namespace | 221 } // namespace |
| 213 | 222 |
| 214 } // namespace local_discovery | 223 } // namespace local_discovery |
| OLD | NEW |