| 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(CloudPrintBaseApiFlow::Status)); | 59 MOCK_METHOD1(ConfirmCallback, void(CloudPrintBaseApiFlow::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 CloudPrintBaseApiFlow* cloudprint_flow = | 98 CloudPrintBaseApiFlow* cloudprint_flow = |
| 93 confirm_flow.GetBaseApiFlowForTests(); | 99 confirm_flow.GetBaseApiFlowForTests(); |
| 94 | 100 |
| 95 confirm_flow.Start(); | 101 confirm_flow.Start(); |
| 96 | 102 |
| 97 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | 103 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 98 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 104 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 99 | 105 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 fetcher->set_response_code(200); | 150 fetcher->set_response_code(200); |
| 145 | 151 |
| 146 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS)); | 152 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS)); |
| 147 | 153 |
| 148 fetcher->delegate()->OnURLFetchComplete(fetcher); | 154 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 149 } | 155 } |
| 150 | 156 |
| 151 TEST_F(PrivetConfirmApiFlowTest, BadToken) { | 157 TEST_F(PrivetConfirmApiFlowTest, BadToken) { |
| 152 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), | 158 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), |
| 153 &token_service_, | 159 &token_service_, |
| 160 account_id_, |
| 154 GURL("http://SoMeUrL.com"), | 161 GURL("http://SoMeUrL.com"), |
| 155 callback_.callback()); | 162 callback_.callback()); |
| 156 | 163 |
| 157 confirm_flow.Start(); | 164 confirm_flow.Start(); |
| 158 | 165 |
| 159 CloudPrintBaseApiFlow* cloudprint_flow = | 166 CloudPrintBaseApiFlow* cloudprint_flow = |
| 160 confirm_flow.GetBaseApiFlowForTests(); | 167 confirm_flow.GetBaseApiFlowForTests(); |
| 161 | 168 |
| 162 EXPECT_CALL(callback_, | 169 EXPECT_CALL(callback_, |
| 163 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_TOKEN)); | 170 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_TOKEN)); |
| 164 cloudprint_flow->OnGetTokenFailure(NULL, GoogleServiceAuthError( | 171 cloudprint_flow->OnGetTokenFailure(NULL, GoogleServiceAuthError( |
| 165 GoogleServiceAuthError::USER_NOT_SIGNED_UP)); | 172 GoogleServiceAuthError::USER_NOT_SIGNED_UP)); |
| 166 } | 173 } |
| 167 | 174 |
| 168 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) { | 175 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) { |
| 169 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), | 176 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), |
| 170 &token_service_, | 177 &token_service_, |
| 178 account_id_, |
| 171 GURL("http://SoMeUrL.com"), | 179 GURL("http://SoMeUrL.com"), |
| 172 callback_.callback()); | 180 callback_.callback()); |
| 173 | 181 |
| 174 confirm_flow.Start(); | 182 confirm_flow.Start(); |
| 175 | 183 |
| 176 CloudPrintBaseApiFlow* cloudprint_flow = | 184 CloudPrintBaseApiFlow* cloudprint_flow = |
| 177 confirm_flow.GetBaseApiFlowForTests(); | 185 confirm_flow.GetBaseApiFlowForTests(); |
| 178 | 186 |
| 179 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | 187 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 180 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 188 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 181 | 189 |
| 182 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); | 190 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); |
| 183 | 191 |
| 184 fetcher->SetResponseString(kFailedConfirmResponse); | 192 fetcher->SetResponseString(kFailedConfirmResponse); |
| 185 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, | 193 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, |
| 186 net::OK)); | 194 net::OK)); |
| 187 fetcher->set_response_code(200); | 195 fetcher->set_response_code(200); |
| 188 | 196 |
| 189 EXPECT_CALL(callback_, | 197 EXPECT_CALL(callback_, |
| 190 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_FROM_SERVER)); | 198 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_FROM_SERVER)); |
| 191 | 199 |
| 192 fetcher->delegate()->OnURLFetchComplete(fetcher); | 200 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 193 } | 201 } |
| 194 | 202 |
| 195 TEST_F(PrivetConfirmApiFlowTest, BadJson) { | 203 TEST_F(PrivetConfirmApiFlowTest, BadJson) { |
| 196 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), | 204 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), |
| 197 &token_service_, | 205 &token_service_, |
| 206 account_id_, |
| 198 GURL("http://SoMeUrL.com"), | 207 GURL("http://SoMeUrL.com"), |
| 199 callback_.callback()); | 208 callback_.callback()); |
| 200 | 209 |
| 201 confirm_flow.Start(); | 210 confirm_flow.Start(); |
| 202 | 211 |
| 203 CloudPrintBaseApiFlow* cloudprint_flow = | 212 CloudPrintBaseApiFlow* cloudprint_flow = |
| 204 confirm_flow.GetBaseApiFlowForTests(); | 213 confirm_flow.GetBaseApiFlowForTests(); |
| 205 | 214 |
| 206 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | 215 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); |
| 207 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 216 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 208 | 217 |
| 209 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); | 218 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); |
| 210 | 219 |
| 211 fetcher->SetResponseString(kFailedConfirmResponseBadJson); | 220 fetcher->SetResponseString(kFailedConfirmResponseBadJson); |
| 212 fetcher->set_status(net::URLRequestStatus( | 221 fetcher->set_status(net::URLRequestStatus( |
| 213 net::URLRequestStatus::SUCCESS, | 222 net::URLRequestStatus::SUCCESS, |
| 214 net::OK)); | 223 net::OK)); |
| 215 fetcher->set_response_code(200); | 224 fetcher->set_response_code(200); |
| 216 | 225 |
| 217 EXPECT_CALL(callback_, ConfirmCallback | 226 EXPECT_CALL(callback_, ConfirmCallback |
| 218 (CloudPrintBaseApiFlow::ERROR_MALFORMED_RESPONSE)); | 227 (CloudPrintBaseApiFlow::ERROR_MALFORMED_RESPONSE)); |
| 219 | 228 |
| 220 fetcher->delegate()->OnURLFetchComplete(fetcher); | 229 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 221 } | 230 } |
| 222 | 231 |
| 223 } // namespace | 232 } // namespace |
| 224 | 233 |
| 225 } // namespace local_discovery | 234 } // namespace local_discovery |
| OLD | NEW |