| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" |
| 9 |
| 8 #include <string> | 10 #include <string> |
| 9 | 11 |
| 10 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 11 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 12 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 14 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
| 13 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" | 15 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" |
| 14 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" | |
| 15 #include "chrome/common/net/gaia/gaia_urls.h" | 16 #include "chrome/common/net/gaia/gaia_urls.h" |
| 16 #include "chrome/common/net/gaia/google_service_auth_error.h" | 17 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 17 #include "chrome/common/net/http_return.h" | 18 #include "chrome/common/net/http_return.h" |
| 18 #include "chrome/test/testing_profile.h" | 19 #include "chrome/test/testing_profile.h" |
| 19 #include "content/common/test_url_fetcher_factory.h" | 20 #include "content/common/test_url_fetcher_factory.h" |
| 20 #include "content/common/url_fetcher.h" | 21 #include "content/common/url_fetcher.h" |
| 21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 23 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 net::ResponseCookies(), | 58 net::ResponseCookies(), |
| 58 results_); | 59 results_); |
| 59 } | 60 } |
| 60 | 61 |
| 61 | 62 |
| 62 class GaiaAuthFetcherTest : public testing::Test { | 63 class GaiaAuthFetcherTest : public testing::Test { |
| 63 public: | 64 public: |
| 64 GaiaAuthFetcherTest() | 65 GaiaAuthFetcherTest() |
| 65 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), | 66 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), |
| 66 issue_auth_token_source_( | 67 issue_auth_token_source_( |
| 67 GaiaUrls::GetInstance()->issue_auth_token_url()) {} | 68 GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 69 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()) {} |
| 68 | 70 |
| 69 void RunParsingTest(const std::string& data, | 71 void RunParsingTest(const std::string& data, |
| 70 const std::string& sid, | 72 const std::string& sid, |
| 71 const std::string& lsid, | 73 const std::string& lsid, |
| 72 const std::string& token) { | 74 const std::string& token) { |
| 73 std::string out_sid; | 75 std::string out_sid; |
| 74 std::string out_lsid; | 76 std::string out_lsid; |
| 75 std::string out_token; | 77 std::string out_token; |
| 76 | 78 |
| 77 GaiaAuthFetcher::ParseClientLoginResponse(data, | 79 GaiaAuthFetcher::ParseClientLoginResponse(data, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 100 &out_captcha_token); | 102 &out_captcha_token); |
| 101 EXPECT_EQ(error, out_error); | 103 EXPECT_EQ(error, out_error); |
| 102 EXPECT_EQ(error_url, out_error_url); | 104 EXPECT_EQ(error_url, out_error_url); |
| 103 EXPECT_EQ(captcha_url, out_captcha_url); | 105 EXPECT_EQ(captcha_url, out_captcha_url); |
| 104 EXPECT_EQ(captcha_token, out_captcha_token); | 106 EXPECT_EQ(captcha_token, out_captcha_token); |
| 105 } | 107 } |
| 106 | 108 |
| 107 net::ResponseCookies cookies_; | 109 net::ResponseCookies cookies_; |
| 108 GURL client_login_source_; | 110 GURL client_login_source_; |
| 109 GURL issue_auth_token_source_; | 111 GURL issue_auth_token_source_; |
| 112 GURL token_auth_source_; |
| 110 TestingProfile profile_; | 113 TestingProfile profile_; |
| 111 protected: | 114 protected: |
| 112 MessageLoop message_loop_; | 115 MessageLoop message_loop_; |
| 113 }; | 116 }; |
| 114 | 117 |
| 115 class MockGaiaConsumer : public GaiaAuthConsumer { | 118 class MockGaiaConsumer : public GaiaAuthConsumer { |
| 116 public: | 119 public: |
| 117 MockGaiaConsumer() {} | 120 MockGaiaConsumer() {} |
| 118 ~MockGaiaConsumer() {} | 121 ~MockGaiaConsumer() {} |
| 119 | 122 |
| 120 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); | 123 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); |
| 121 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, | 124 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, |
| 122 const std::string& token)); | 125 const std::string& token)); |
| 126 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); |
| 123 MOCK_METHOD1(OnClientLoginFailure, | 127 MOCK_METHOD1(OnClientLoginFailure, |
| 124 void(const GoogleServiceAuthError& error)); | 128 void(const GoogleServiceAuthError& error)); |
| 125 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, | 129 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, |
| 126 const GoogleServiceAuthError& error)); | 130 const GoogleServiceAuthError& error)); |
| 131 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); |
| 127 }; | 132 }; |
| 128 | 133 |
| 129 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { | 134 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { |
| 130 GoogleServiceAuthError expected_error = | 135 GoogleServiceAuthError expected_error = |
| 131 GoogleServiceAuthError::FromConnectionError(-101); | 136 GoogleServiceAuthError::FromConnectionError(-101); |
| 132 | 137 |
| 133 GoogleServiceAuthError matching_error = | 138 GoogleServiceAuthError matching_error = |
| 134 GoogleServiceAuthError::FromConnectionError(-101); | 139 GoogleServiceAuthError::FromConnectionError(-101); |
| 135 | 140 |
| 136 EXPECT_TRUE(expected_error == matching_error); | 141 EXPECT_TRUE(expected_error == matching_error); |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 EXPECT_TRUE(auth.HasPendingFetch()); | 495 EXPECT_TRUE(auth.HasPendingFetch()); |
| 491 auth.OnURLFetchComplete( | 496 auth.OnURLFetchComplete( |
| 492 NULL, | 497 NULL, |
| 493 issue_auth_token_source_, | 498 issue_auth_token_source_, |
| 494 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | 499 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 495 RC_FORBIDDEN, | 500 RC_FORBIDDEN, |
| 496 cookies_, | 501 cookies_, |
| 497 ""); | 502 ""); |
| 498 EXPECT_FALSE(auth.HasPendingFetch()); | 503 EXPECT_FALSE(auth.HasPendingFetch()); |
| 499 } | 504 } |
| 505 |
| 506 TEST_F(GaiaAuthFetcherTest, TokenAuthSuccess) { |
| 507 MockGaiaConsumer consumer; |
| 508 EXPECT_CALL(consumer, OnTokenAuthSuccess("<html></html>")) |
| 509 .Times(1); |
| 510 |
| 511 TestingProfile profile; |
| 512 TestURLFetcherFactory factory; |
| 513 URLFetcher::set_factory(&factory); |
| 514 |
| 515 GaiaAuthFetcher auth(&consumer, std::string(), |
| 516 profile_.GetRequestContext()); |
| 517 auth.StartTokenAuth("myubertoken"); |
| 518 |
| 519 URLFetcher::set_factory(NULL); |
| 520 EXPECT_TRUE(auth.HasPendingFetch()); |
| 521 auth.OnURLFetchComplete( |
| 522 NULL, |
| 523 token_auth_source_, |
| 524 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 525 RC_REQUEST_OK, |
| 526 cookies_, |
| 527 "<html></html>"); |
| 528 EXPECT_FALSE(auth.HasPendingFetch()); |
| 529 } |
| 530 |
| 531 TEST_F(GaiaAuthFetcherTest, TokenAuthUnauthorizedFailure) { |
| 532 MockGaiaConsumer consumer; |
| 533 EXPECT_CALL(consumer, OnTokenAuthFailure(_)) |
| 534 .Times(1); |
| 535 |
| 536 TestingProfile profile; |
| 537 TestURLFetcherFactory factory; |
| 538 URLFetcher::set_factory(&factory); |
| 539 |
| 540 GaiaAuthFetcher auth(&consumer, std::string(), |
| 541 profile_.GetRequestContext()); |
| 542 auth.StartTokenAuth("badubertoken"); |
| 543 |
| 544 URLFetcher::set_factory(NULL); |
| 545 EXPECT_TRUE(auth.HasPendingFetch()); |
| 546 auth.OnURLFetchComplete( |
| 547 NULL, |
| 548 token_auth_source_, |
| 549 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 550 RC_UNAUTHORIZED, |
| 551 cookies_, |
| 552 ""); |
| 553 EXPECT_FALSE(auth.HasPendingFetch()); |
| 554 } |
| 555 |
| 556 TEST_F(GaiaAuthFetcherTest, TokenAuthNetFailure) { |
| 557 MockGaiaConsumer consumer; |
| 558 EXPECT_CALL(consumer, OnTokenAuthFailure(_)) |
| 559 .Times(1); |
| 560 |
| 561 TestingProfile profile; |
| 562 TestURLFetcherFactory factory; |
| 563 URLFetcher::set_factory(&factory); |
| 564 |
| 565 GaiaAuthFetcher auth(&consumer, std::string(), |
| 566 profile_.GetRequestContext()); |
| 567 auth.StartTokenAuth("badubertoken"); |
| 568 |
| 569 URLFetcher::set_factory(NULL); |
| 570 EXPECT_TRUE(auth.HasPendingFetch()); |
| 571 auth.OnURLFetchComplete( |
| 572 NULL, |
| 573 token_auth_source_, |
| 574 net::URLRequestStatus(net::URLRequestStatus::FAILED, 0), |
| 575 RC_REQUEST_OK, |
| 576 cookies_, |
| 577 ""); |
| 578 EXPECT_FALSE(auth.HasPendingFetch()); |
| 579 } |
| OLD | NEW |