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" | 8 #include "chrome/common/net/gaia/gaia_auth_fetcher_unittest.h" |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 14 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
15 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" | 15 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" |
16 #include "chrome/common/net/gaia/gaia_urls.h" | 16 #include "chrome/common/net/gaia/gaia_urls.h" |
17 #include "chrome/common/net/gaia/google_service_auth_error.h" | 17 #include "chrome/common/net/gaia/google_service_auth_error.h" |
18 #include "chrome/common/net/http_return.h" | 18 #include "chrome/common/net/http_return.h" |
19 #include "chrome/test/base/testing_browser_process_test.h" | 19 #include "chrome/test/base/testing_browser_process_test.h" |
20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
21 #include "content/common/url_fetcher.h" | 21 #include "content/common/url_fetcher.h" |
22 #include "content/test/test_url_fetcher_factory.h" | 22 #include "content/test/test_url_fetcher_factory.h" |
23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 24 #include "net/base/load_flags.h" |
24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
25 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 using ::testing::_; | 30 using ::testing::_; |
30 | 31 |
31 MockFetcher::MockFetcher(bool success, | 32 MockFetcher::MockFetcher(bool success, |
32 const GURL& url, | 33 const GURL& url, |
33 const std::string& results, | 34 const std::string& results, |
(...skipping 26 matching lines...) Expand all Loading... |
60 results_); | 61 results_); |
61 } | 62 } |
62 | 63 |
63 | 64 |
64 class GaiaAuthFetcherTest : public TestingBrowserProcessTest { | 65 class GaiaAuthFetcherTest : public TestingBrowserProcessTest { |
65 public: | 66 public: |
66 GaiaAuthFetcherTest() | 67 GaiaAuthFetcherTest() |
67 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), | 68 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), |
68 issue_auth_token_source_( | 69 issue_auth_token_source_( |
69 GaiaUrls::GetInstance()->issue_auth_token_url()), | 70 GaiaUrls::GetInstance()->issue_auth_token_url()), |
70 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()) {} | 71 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), |
| 72 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {} |
71 | 73 |
72 void RunParsingTest(const std::string& data, | 74 void RunParsingTest(const std::string& data, |
73 const std::string& sid, | 75 const std::string& sid, |
74 const std::string& lsid, | 76 const std::string& lsid, |
75 const std::string& token) { | 77 const std::string& token) { |
76 std::string out_sid; | 78 std::string out_sid; |
77 std::string out_lsid; | 79 std::string out_lsid; |
78 std::string out_token; | 80 std::string out_token; |
79 | 81 |
80 GaiaAuthFetcher::ParseClientLoginResponse(data, | 82 GaiaAuthFetcher::ParseClientLoginResponse(data, |
(...skipping 23 matching lines...) Expand all Loading... |
104 EXPECT_EQ(error, out_error); | 106 EXPECT_EQ(error, out_error); |
105 EXPECT_EQ(error_url, out_error_url); | 107 EXPECT_EQ(error_url, out_error_url); |
106 EXPECT_EQ(captcha_url, out_captcha_url); | 108 EXPECT_EQ(captcha_url, out_captcha_url); |
107 EXPECT_EQ(captcha_token, out_captcha_token); | 109 EXPECT_EQ(captcha_token, out_captcha_token); |
108 } | 110 } |
109 | 111 |
110 net::ResponseCookies cookies_; | 112 net::ResponseCookies cookies_; |
111 GURL client_login_source_; | 113 GURL client_login_source_; |
112 GURL issue_auth_token_source_; | 114 GURL issue_auth_token_source_; |
113 GURL token_auth_source_; | 115 GURL token_auth_source_; |
| 116 GURL merge_session_source_; |
114 TestingProfile profile_; | 117 TestingProfile profile_; |
115 protected: | 118 protected: |
116 MessageLoop message_loop_; | 119 MessageLoop message_loop_; |
117 }; | 120 }; |
118 | 121 |
119 class MockGaiaConsumer : public GaiaAuthConsumer { | 122 class MockGaiaConsumer : public GaiaAuthConsumer { |
120 public: | 123 public: |
121 MockGaiaConsumer() {} | 124 MockGaiaConsumer() {} |
122 ~MockGaiaConsumer() {} | 125 ~MockGaiaConsumer() {} |
123 | 126 |
124 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); | 127 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); |
125 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, | 128 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, |
126 const std::string& token)); | 129 const std::string& token)); |
127 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); | 130 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); |
| 131 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); |
128 MOCK_METHOD1(OnClientLoginFailure, | 132 MOCK_METHOD1(OnClientLoginFailure, |
129 void(const GoogleServiceAuthError& error)); | 133 void(const GoogleServiceAuthError& error)); |
130 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, | 134 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, |
131 const GoogleServiceAuthError& error)); | 135 const GoogleServiceAuthError& error)); |
132 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); | 136 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); |
| 137 MOCK_METHOD1(OnMergeSessionFailure, void( |
| 138 const GoogleServiceAuthError& error)); |
133 }; | 139 }; |
134 | 140 |
135 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { | 141 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { |
136 GoogleServiceAuthError expected_error = | 142 GoogleServiceAuthError expected_error = |
137 GoogleServiceAuthError::FromConnectionError(-101); | 143 GoogleServiceAuthError::FromConnectionError(-101); |
138 | 144 |
139 GoogleServiceAuthError matching_error = | 145 GoogleServiceAuthError matching_error = |
140 GoogleServiceAuthError::FromConnectionError(-101); | 146 GoogleServiceAuthError::FromConnectionError(-101); |
141 | 147 |
142 EXPECT_TRUE(expected_error == matching_error); | 148 EXPECT_TRUE(expected_error == matching_error); |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 EXPECT_TRUE(auth.HasPendingFetch()); | 591 EXPECT_TRUE(auth.HasPendingFetch()); |
586 auth.OnURLFetchComplete( | 592 auth.OnURLFetchComplete( |
587 NULL, | 593 NULL, |
588 token_auth_source_, | 594 token_auth_source_, |
589 net::URLRequestStatus(net::URLRequestStatus::FAILED, 0), | 595 net::URLRequestStatus(net::URLRequestStatus::FAILED, 0), |
590 RC_REQUEST_OK, | 596 RC_REQUEST_OK, |
591 cookies_, | 597 cookies_, |
592 ""); | 598 ""); |
593 EXPECT_FALSE(auth.HasPendingFetch()); | 599 EXPECT_FALSE(auth.HasPendingFetch()); |
594 } | 600 } |
| 601 |
| 602 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccess) { |
| 603 MockGaiaConsumer consumer; |
| 604 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>")) |
| 605 .Times(1); |
| 606 |
| 607 TestingProfile profile; |
| 608 TestURLFetcherFactory factory; |
| 609 |
| 610 GaiaAuthFetcher auth(&consumer, std::string(), |
| 611 profile_.GetRequestContext()); |
| 612 auth.StartMergeSession("myubertoken"); |
| 613 |
| 614 EXPECT_TRUE(auth.HasPendingFetch()); |
| 615 auth.OnURLFetchComplete( |
| 616 NULL, |
| 617 merge_session_source_, |
| 618 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 619 RC_REQUEST_OK, |
| 620 cookies_, |
| 621 "<html></html>"); |
| 622 EXPECT_FALSE(auth.HasPendingFetch()); |
| 623 } |
| 624 |
| 625 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccessRedirect) { |
| 626 MockGaiaConsumer consumer; |
| 627 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>")) |
| 628 .Times(1); |
| 629 |
| 630 TestingProfile profile; |
| 631 TestURLFetcherFactory factory; |
| 632 |
| 633 GaiaAuthFetcher auth(&consumer, std::string(), |
| 634 profile_.GetRequestContext()); |
| 635 auth.StartMergeSession("myubertoken"); |
| 636 |
| 637 // Make sure the fetcher created has the expected flags. Set its url() |
| 638 // properties to reflect a redirect. |
| 639 TestURLFetcher* test_fetcher = factory.GetFetcherByID(0); |
| 640 EXPECT_TRUE(test_fetcher != NULL); |
| 641 EXPECT_TRUE(test_fetcher->load_flags() == net::LOAD_NORMAL); |
| 642 EXPECT_TRUE(auth.HasPendingFetch()); |
| 643 |
| 644 GURL final_url("http://www.google.com/CheckCookie"); |
| 645 test_fetcher->set_url(final_url); |
| 646 |
| 647 auth.OnURLFetchComplete( |
| 648 test_fetcher, |
| 649 test_fetcher->url(), |
| 650 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 651 RC_REQUEST_OK, |
| 652 cookies_, |
| 653 "<html></html>"); |
| 654 EXPECT_FALSE(auth.HasPendingFetch()); |
| 655 } |
OLD | NEW |