| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // A complete set of unit tests for GaiaAuthFetcher. | 5 // A complete set of unit tests for GaiaAuthFetcher. |
| 6 // Originally ported from GoogleAuthenticator tests. | 6 // Originally ported from GoogleAuthenticator tests. |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 class MockGaiaConsumer : public GaiaAuthConsumer { | 166 class MockGaiaConsumer : public GaiaAuthConsumer { |
| 167 public: | 167 public: |
| 168 MockGaiaConsumer() {} | 168 MockGaiaConsumer() {} |
| 169 ~MockGaiaConsumer() {} | 169 ~MockGaiaConsumer() {} |
| 170 | 170 |
| 171 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); | 171 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); |
| 172 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, | 172 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, |
| 173 const std::string& token)); | 173 const std::string& token)); |
| 174 MOCK_METHOD1(OnClientOAuthCode, void(const std::string& data)); |
| 174 MOCK_METHOD1(OnClientOAuthSuccess, | 175 MOCK_METHOD1(OnClientOAuthSuccess, |
| 175 void(const GaiaAuthConsumer::ClientOAuthResult& result)); | 176 void(const GaiaAuthConsumer::ClientOAuthResult& result)); |
| 176 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); | 177 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); |
| 177 MOCK_METHOD1(OnUberAuthTokenSuccess, void(const std::string& data)); | 178 MOCK_METHOD1(OnUberAuthTokenSuccess, void(const std::string& data)); |
| 178 MOCK_METHOD1(OnClientLoginFailure, | 179 MOCK_METHOD1(OnClientLoginFailure, |
| 179 void(const GoogleServiceAuthError& error)); | 180 void(const GoogleServiceAuthError& error)); |
| 180 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, | 181 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, |
| 181 const GoogleServiceAuthError& error)); | 182 const GoogleServiceAuthError& error)); |
| 182 MOCK_METHOD1(OnClientOAuthFailure, | 183 MOCK_METHOD1(OnClientOAuthFailure, |
| 183 void(const GoogleServiceAuthError& error)); | 184 void(const GoogleServiceAuthError& error)); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 cookies_, | 389 cookies_, |
| 389 std::string(), | 390 std::string(), |
| 390 net::URLFetcher::GET, | 391 net::URLFetcher::GET, |
| 391 &auth); | 392 &auth); |
| 392 auth.OnURLFetchComplete(&mock_fetcher); | 393 auth.OnURLFetchComplete(&mock_fetcher); |
| 393 EXPECT_FALSE(auth.HasPendingFetch()); | 394 EXPECT_FALSE(auth.HasPendingFetch()); |
| 394 } | 395 } |
| 395 | 396 |
| 396 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) { | 397 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) { |
| 397 MockGaiaConsumer consumer; | 398 MockGaiaConsumer consumer; |
| 399 EXPECT_CALL(consumer, OnClientOAuthCode("test-code")).Times(1); |
| 398 EXPECT_CALL(consumer, OnClientOAuthSuccess( | 400 EXPECT_CALL(consumer, OnClientOAuthSuccess( |
| 399 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1); | 401 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1); |
| 400 | 402 |
| 401 net::TestURLFetcherFactory factory; | 403 net::TestURLFetcherFactory factory; |
| 402 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); | 404 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); |
| 403 auth.StartCookieForOAuthLoginTokenExchange("0"); | 405 auth.StartCookieForOAuthLoginTokenExchange("0"); |
| 404 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 406 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 405 EXPECT_TRUE(NULL != fetcher); | 407 EXPECT_TRUE(NULL != fetcher); |
| 406 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags()); | 408 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags()); |
| 407 EXPECT_EQ(std::string::npos, | 409 EXPECT_EQ(std::string::npos, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 422 EXPECT_TRUE(auth.HasPendingFetch()); | 424 EXPECT_TRUE(auth.HasPendingFetch()); |
| 423 MockFetcher mock_fetcher2( | 425 MockFetcher mock_fetcher2( |
| 424 oauth2_token_source_, | 426 oauth2_token_source_, |
| 425 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | 427 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 426 net::HTTP_OK, cookies_, kGetTokenPairValidResponse, | 428 net::HTTP_OK, cookies_, kGetTokenPairValidResponse, |
| 427 net::URLFetcher::POST, &auth); | 429 net::URLFetcher::POST, &auth); |
| 428 auth.OnURLFetchComplete(&mock_fetcher2); | 430 auth.OnURLFetchComplete(&mock_fetcher2); |
| 429 EXPECT_FALSE(auth.HasPendingFetch()); | 431 EXPECT_FALSE(auth.HasPendingFetch()); |
| 430 } | 432 } |
| 431 | 433 |
| 434 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccessNoTokenFetch) { |
| 435 MockGaiaConsumer consumer; |
| 436 EXPECT_CALL(consumer, OnClientOAuthCode("test-code")).Times(1); |
| 437 EXPECT_CALL(consumer, OnClientOAuthSuccess( |
| 438 GaiaAuthConsumer::ClientOAuthResult("", "", 0))).Times(0); |
| 439 |
| 440 net::TestURLFetcherFactory factory; |
| 441 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); |
| 442 auth.StartCookieForOAuthLoginTokenExchangeWithClientIdAndDeviceId( |
| 443 false, "0", "ABCDE_12345", ""); |
| 444 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 445 EXPECT_TRUE(NULL != fetcher); |
| 446 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags()); |
| 447 EXPECT_EQ(std::string::npos, |
| 448 fetcher->GetOriginalURL().query().find("device_type=chrome")); |
| 449 |
| 450 net::ResponseCookies cookies; |
| 451 cookies.push_back(kGetAuthCodeValidCookie); |
| 452 EXPECT_TRUE(auth.HasPendingFetch()); |
| 453 MockFetcher mock_fetcher1( |
| 454 client_login_to_oauth2_source_, |
| 455 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 456 net::HTTP_OK, |
| 457 cookies, |
| 458 std::string(), |
| 459 net::URLFetcher::POST, |
| 460 &auth); |
| 461 auth.OnURLFetchComplete(&mock_fetcher1); |
| 462 EXPECT_FALSE(auth.HasPendingFetch()); |
| 463 } |
| 464 |
| 432 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies_DeviceId) { | 465 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies_DeviceId) { |
| 433 MockGaiaConsumer consumer; | 466 MockGaiaConsumer consumer; |
| 434 net::TestURLFetcherFactory factory; | 467 net::TestURLFetcherFactory factory; |
| 435 std::string expected_device_id("ABCDE-12345"); | 468 std::string expected_device_id("ABCDE-12345"); |
| 436 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); | 469 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); |
| 437 auth.StartCookieForOAuthLoginTokenExchangeWithDeviceId("0", | 470 auth.StartCookieForOAuthLoginTokenExchangeWithDeviceId("0", |
| 438 expected_device_id); | 471 expected_device_id); |
| 439 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 472 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 440 EXPECT_TRUE(NULL != fetcher); | 473 EXPECT_TRUE(NULL != fetcher); |
| 441 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags()); | 474 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags()); |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); | 754 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); |
| 722 auth.StartGetTokenResponse(std::string(), std::string(), std::string()); | 755 auth.StartGetTokenResponse(std::string(), std::string(), std::string()); |
| 723 | 756 |
| 724 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | 757 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 725 MockFetcher mock_fetcher( | 758 MockFetcher mock_fetcher( |
| 726 GaiaUrls::GetInstance()->oauth2_iframe_url(), | 759 GaiaUrls::GetInstance()->oauth2_iframe_url(), |
| 727 status, net::HTTP_OK, cookies_, kGetTokenPairValidResponse, | 760 status, net::HTTP_OK, cookies_, kGetTokenPairValidResponse, |
| 728 net::URLFetcher::GET, &auth); | 761 net::URLFetcher::GET, &auth); |
| 729 auth.OnURLFetchComplete(&mock_fetcher); | 762 auth.OnURLFetchComplete(&mock_fetcher); |
| 730 } | 763 } |
| OLD | NEW |