| OLD | NEW |
| 1 // Copyright (c) 2011 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 "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 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 class GaiaAuthFetcherTest : public testing::Test { | 109 class GaiaAuthFetcherTest : public testing::Test { |
| 110 public: | 110 public: |
| 111 GaiaAuthFetcherTest() | 111 GaiaAuthFetcherTest() |
| 112 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), | 112 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), |
| 113 issue_auth_token_source_( | 113 issue_auth_token_source_( |
| 114 GaiaUrls::GetInstance()->issue_auth_token_url()), | 114 GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 115 client_login_to_oauth2_source_( | 115 client_login_to_oauth2_source_( |
| 116 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | 116 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), |
| 117 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()), | 117 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()), |
| 118 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), | 118 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), |
| 119 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()) {} | 119 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()), |
| 120 uberauth_token_source_(base::StringPrintf( |
| 121 "%s?source=&issueuberauth=1", |
| 122 GaiaUrls::GetInstance()->oauth1_login_url().c_str())) {} |
| 120 | 123 |
| 121 void RunParsingTest(const std::string& data, | 124 void RunParsingTest(const std::string& data, |
| 122 const std::string& sid, | 125 const std::string& sid, |
| 123 const std::string& lsid, | 126 const std::string& lsid, |
| 124 const std::string& token) { | 127 const std::string& token) { |
| 125 std::string out_sid; | 128 std::string out_sid; |
| 126 std::string out_lsid; | 129 std::string out_lsid; |
| 127 std::string out_token; | 130 std::string out_token; |
| 128 | 131 |
| 129 GaiaAuthFetcher::ParseClientLoginResponse(data, | 132 GaiaAuthFetcher::ParseClientLoginResponse(data, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 156 EXPECT_EQ(captcha_token, out_captcha_token); | 159 EXPECT_EQ(captcha_token, out_captcha_token); |
| 157 } | 160 } |
| 158 | 161 |
| 159 net::ResponseCookies cookies_; | 162 net::ResponseCookies cookies_; |
| 160 GURL client_login_source_; | 163 GURL client_login_source_; |
| 161 GURL issue_auth_token_source_; | 164 GURL issue_auth_token_source_; |
| 162 GURL client_login_to_oauth2_source_; | 165 GURL client_login_to_oauth2_source_; |
| 163 GURL oauth2_token_source_; | 166 GURL oauth2_token_source_; |
| 164 GURL token_auth_source_; | 167 GURL token_auth_source_; |
| 165 GURL merge_session_source_; | 168 GURL merge_session_source_; |
| 169 GURL uberauth_token_source_; |
| 166 TestingProfile profile_; | 170 TestingProfile profile_; |
| 167 protected: | 171 protected: |
| 168 MessageLoop message_loop_; | 172 MessageLoop message_loop_; |
| 169 }; | 173 }; |
| 170 | 174 |
| 171 class MockGaiaConsumer : public GaiaAuthConsumer { | 175 class MockGaiaConsumer : public GaiaAuthConsumer { |
| 172 public: | 176 public: |
| 173 MockGaiaConsumer() {} | 177 MockGaiaConsumer() {} |
| 174 ~MockGaiaConsumer() {} | 178 ~MockGaiaConsumer() {} |
| 175 | 179 |
| 176 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); | 180 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); |
| 177 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, | 181 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, |
| 178 const std::string& token)); | 182 const std::string& token)); |
| 179 MOCK_METHOD3(OnOAuthLoginTokenSuccess, | 183 MOCK_METHOD3(OnOAuthLoginTokenSuccess, |
| 180 void(const std::string& refresh_token, | 184 void(const std::string& refresh_token, |
| 181 const std::string& access_token, | 185 const std::string& access_token, |
| 182 int expires_in_secs)); | 186 int expires_in_secs)); |
| 183 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); | 187 MOCK_METHOD1(OnTokenAuthSuccess, void(const std::string& data)); |
| 184 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); | 188 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); |
| 189 MOCK_METHOD1(OnUberAuthTokenSuccess, void(const std::string& data)); |
| 185 MOCK_METHOD1(OnClientLoginFailure, | 190 MOCK_METHOD1(OnClientLoginFailure, |
| 186 void(const GoogleServiceAuthError& error)); | 191 void(const GoogleServiceAuthError& error)); |
| 187 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, | 192 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, |
| 188 const GoogleServiceAuthError& error)); | 193 const GoogleServiceAuthError& error)); |
| 189 MOCK_METHOD1(OnOAuthLoginTokenFailure, | 194 MOCK_METHOD1(OnOAuthLoginTokenFailure, |
| 190 void(const GoogleServiceAuthError& error)); | 195 void(const GoogleServiceAuthError& error)); |
| 191 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); | 196 MOCK_METHOD1(OnTokenAuthFailure, void(const GoogleServiceAuthError& error)); |
| 192 MOCK_METHOD1(OnMergeSessionFailure, void( | 197 MOCK_METHOD1(OnMergeSessionFailure, void( |
| 193 const GoogleServiceAuthError& error)); | 198 const GoogleServiceAuthError& error)); |
| 199 MOCK_METHOD1(OnUberAuthTokenFailure, void( |
| 200 const GoogleServiceAuthError& error)); |
| 194 }; | 201 }; |
| 195 | 202 |
| 196 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { | 203 TEST_F(GaiaAuthFetcherTest, ErrorComparator) { |
| 197 GoogleServiceAuthError expected_error = | 204 GoogleServiceAuthError expected_error = |
| 198 GoogleServiceAuthError::FromConnectionError(-101); | 205 GoogleServiceAuthError::FromConnectionError(-101); |
| 199 | 206 |
| 200 GoogleServiceAuthError matching_error = | 207 GoogleServiceAuthError matching_error = |
| 201 GoogleServiceAuthError::FromConnectionError(-101); | 208 GoogleServiceAuthError::FromConnectionError(-101); |
| 202 | 209 |
| 203 EXPECT_TRUE(expected_error == matching_error); | 210 EXPECT_TRUE(expected_error == matching_error); |
| (...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 test_fetcher->set_status( | 772 test_fetcher->set_status( |
| 766 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | 773 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
| 767 test_fetcher->set_response_code(RC_REQUEST_OK); | 774 test_fetcher->set_response_code(RC_REQUEST_OK); |
| 768 test_fetcher->set_cookies(cookies_); | 775 test_fetcher->set_cookies(cookies_); |
| 769 test_fetcher->SetResponseString("<html></html>"); | 776 test_fetcher->SetResponseString("<html></html>"); |
| 770 | 777 |
| 771 auth.OnURLFetchComplete(test_fetcher); | 778 auth.OnURLFetchComplete(test_fetcher); |
| 772 EXPECT_FALSE(auth.HasPendingFetch()); | 779 EXPECT_FALSE(auth.HasPendingFetch()); |
| 773 } | 780 } |
| 774 | 781 |
| 782 TEST_F(GaiaAuthFetcherTest, UberAuthTokenSuccess) { |
| 783 MockGaiaConsumer consumer; |
| 784 EXPECT_CALL(consumer, OnUberAuthTokenSuccess("uberToken")) |
| 785 .Times(1); |
| 786 |
| 787 TestingProfile profile; |
| 788 TestURLFetcherFactory factory; |
| 789 |
| 790 GaiaAuthFetcher auth(&consumer, std::string(), |
| 791 profile_.GetRequestContext()); |
| 792 auth.StartUberAuthTokenFetch("myAccessToken"); |
| 793 |
| 794 EXPECT_TRUE(auth.HasPendingFetch()); |
| 795 MockFetcher mock_fetcher( |
| 796 uberauth_token_source_, |
| 797 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), |
| 798 RC_REQUEST_OK, cookies_, "uberToken", content::URLFetcher::POST, |
| 799 &auth); |
| 800 auth.OnURLFetchComplete(&mock_fetcher); |
| 801 EXPECT_FALSE(auth.HasPendingFetch()); |
| 802 } |
| 803 |
| 775 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) { | 804 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) { |
| 776 { // No cookies. | 805 { // No cookies. |
| 777 std::string auth_code; | 806 std::string auth_code; |
| 778 net::ResponseCookies cookies; | 807 net::ResponseCookies cookies; |
| 779 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( | 808 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( |
| 780 cookies, &auth_code)); | 809 cookies, &auth_code)); |
| 781 EXPECT_EQ("", auth_code); | 810 EXPECT_EQ("", auth_code); |
| 782 } | 811 } |
| 783 { // Few cookies, nothing appropriate. | 812 { // Few cookies, nothing appropriate. |
| 784 std::string auth_code; | 813 std::string auth_code; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 std::string rt; | 885 std::string rt; |
| 857 std::string at; | 886 std::string at; |
| 858 int exp = -1; | 887 int exp = -1; |
| 859 EXPECT_TRUE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse( | 888 EXPECT_TRUE(GaiaAuthFetcher::ParseOAuth2TokenPairResponse( |
| 860 kGetTokenPairValidResponse, &rt, &at, &exp)); | 889 kGetTokenPairValidResponse, &rt, &at, &exp)); |
| 861 EXPECT_EQ("rt1", rt); | 890 EXPECT_EQ("rt1", rt); |
| 862 EXPECT_EQ("at1", at); | 891 EXPECT_EQ("at1", at); |
| 863 EXPECT_EQ(3600, exp); | 892 EXPECT_EQ(3600, exp); |
| 864 } | 893 } |
| 865 } | 894 } |
| OLD | NEW |