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