gaia_auth_fetcher_unittest.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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 3// found in the LICENSE file. 4// 5// A complete set of unit tests for GaiaAuthFetcher. 6// Originally ported from GoogleAuthenticator tests. 7 8#include <string> 9 10#include "base/json/json_reader.h" 11#include "base/strings/stringprintf.h" 12#include "base/values.h" 13#include "google_apis/gaia/gaia_auth_consumer.h" 14#include "google_apis/gaia/gaia_auth_fetcher.h" 15#include "google_apis/gaia/gaia_urls.h" 16#include "google_apis/gaia/google_service_auth_error.h" 17#include "google_apis/gaia/mock_url_fetcher_factory.h" 18#include "google_apis/google_api_keys.h" 19#include "net/base/load_flags.h" 20#include "net/base/net_errors.h" 21#include "net/http/http_status_code.h" 22#include "net/url_request/test_url_fetcher_factory.h" 23#include "net/url_request/url_fetcher_delegate.h" 24#include "net/url_request/url_request_status.h" 25#include "net/url_request/url_request_test_util.h" 26#include "testing/gmock/include/gmock/gmock.h" 27#include "testing/gtest/include/gtest/gtest.h" 28#include "url/gurl.h" 29 30using ::testing::Invoke; 31using ::testing::_; 32 33const char kGetAuthCodeValidCookie[] = 34 "oauth_code=test-code; Path=/test; Secure; HttpOnly"; 35const char kGetAuthCodeCookieNoSecure[] = 36 "oauth_code=test-code; Path=/test; HttpOnly"; 37const char kGetAuthCodeCookieNoHttpOnly[] = 38 "oauth_code=test-code; Path=/test; Secure"; 39const char kGetAuthCodeCookieNoOAuthCode[] = 40 "Path=/test; Secure; HttpOnly"; 41const char kGetTokenPairValidResponse[] = 42 "{" 43 " \"refresh_token\": \"rt1\"," 44 " \"access_token\": \"at1\"," 45 " \"expires_in\": 3600," 46 " \"token_type\": \"Bearer\"" 47 "}"; 48 49MockFetcher::MockFetcher(bool success, 50 const GURL& url, 51 const std::string& results, 52 net::URLFetcher::RequestType request_type, 53 net::URLFetcherDelegate* d) 54 : TestURLFetcher(0, url, d) { 55 set_url(url); 56 net::URLRequestStatus::Status code; 57 58 if (success) { 59 set_response_code(net::HTTP_OK); 60 code = net::URLRequestStatus::SUCCESS; 61 } else { 62 set_response_code(net::HTTP_FORBIDDEN); 63 code = net::URLRequestStatus::FAILED; 64 } 65 66 set_status(net::URLRequestStatus(code, 0)); 67 SetResponseString(results); 68} 69 70MockFetcher::MockFetcher(const GURL& url, 71 const net::URLRequestStatus& status, 72 int response_code, 73 const net::ResponseCookies& cookies, 74 const std::string& results, 75 net::URLFetcher::RequestType request_type, 76 net::URLFetcherDelegate* d) 77 : TestURLFetcher(0, url, d) { 78 set_url(url); 79 set_status(status); 80 set_response_code(response_code); 81 set_cookies(cookies); 82 SetResponseString(results); 83} 84 85MockFetcher::~MockFetcher() {} 86 87void MockFetcher::Start() { 88 delegate()->OnURLFetchComplete(this); 89} 90 91class GaiaAuthFetcherTest : public testing::Test { 92 protected: 93 GaiaAuthFetcherTest() 94 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()), 95 issue_auth_token_source_( 96 GaiaUrls::GetInstance()->issue_auth_token_url()), 97 client_login_to_oauth2_source_( 98 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), 99 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()), 100 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()), 101 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()), 102 uberauth_token_source_( 103 GaiaUrls::GetInstance()->oauth1_login_url().Resolve( 104 "?source=&issueuberauth=1")), 105 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()) {} 106 107 void RunParsingTest(const std::string& data, 108 const std::string& sid, 109 const std::string& lsid, 110 const std::string& token) { 111 std::string out_sid; 112 std::string out_lsid; 113 std::string out_token; 114 115 GaiaAuthFetcher::ParseClientLoginResponse(data, 116 &out_sid, 117 &out_lsid, 118 &out_token); 119 EXPECT_EQ(lsid, out_lsid); 120 EXPECT_EQ(sid, out_sid); 121 EXPECT_EQ(token, out_token); 122 } 123 124 void RunErrorParsingTest(const std::string& data, 125 const std::string& error, 126 const std::string& error_url, 127 const std::string& captcha_url, 128 const std::string& captcha_token) { 129 std::string out_error; 130 std::string out_error_url; 131 std::string out_captcha_url; 132 std::string out_captcha_token; 133 134 GaiaAuthFetcher::ParseClientLoginFailure(data, 135 &out_error, 136 &out_error_url, 137 &out_captcha_url, 138 &out_captcha_token); 139 EXPECT_EQ(error, out_error); 140 EXPECT_EQ(error_url, out_error_url); 141 EXPECT_EQ(captcha_url, out_captcha_url); 142 EXPECT_EQ(captcha_token, out_captcha_token); 143 } 144 145 net::ResponseCookies cookies_; 146 GURL client_login_source_; 147 GURL issue_auth_token_source_; 148 GURL client_login_to_oauth2_source_; 149 GURL oauth2_token_source_; 150 GURL token_auth_source_; 151 GURL merge_session_source_; 152 GURL uberauth_token_source_; 153 GURL oauth_login_gurl_; 154 155 protected: 156 net::TestURLRequestContextGetter* GetRequestContext() { 157 if (!request_context_getter_) { 158 request_context_getter_ = new net::TestURLRequestContextGetter( 159 message_loop_.message_loop_proxy()); 160 } 161 return request_context_getter_; 162 } 163 164 base::MessageLoop message_loop_; 165 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; 166}; 167 168class MockGaiaConsumer : public GaiaAuthConsumer { 169 public: 170 MockGaiaConsumer() {} 171 ~MockGaiaConsumer() {} 172 173 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result)); 174 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service, 175 const std::string& token)); 176 MOCK_METHOD1(OnClientOAuthSuccess, 177 void(const GaiaAuthConsumer::ClientOAuthResult& result)); 178 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data)); 179 MOCK_METHOD1(OnUberAuthTokenSuccess, void(const std::string& data)); 180 MOCK_METHOD1(OnClientLoginFailure, 181 void(const GoogleServiceAuthError& error)); 182 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service, 183 const GoogleServiceAuthError& error)); 184 MOCK_METHOD1(OnClientOAuthFailure, 185 void(const GoogleServiceAuthError& error)); 186 MOCK_METHOD1(OnMergeSessionFailure, void( 187 const GoogleServiceAuthError& error)); 188 MOCK_METHOD1(OnUberAuthTokenFailure, void( 189 const GoogleServiceAuthError& error)); 190}; 191 192#if defined(OS_WIN) 193#define MAYBE_ErrorComparator DISABLED_ErrorComparator 194#else 195#define MAYBE_ErrorComparator ErrorComparator 196#endif 197 198TEST_F(GaiaAuthFetcherTest, MAYBE_ErrorComparator) { 199 GoogleServiceAuthError expected_error = 200 GoogleServiceAuthError::FromConnectionError(-101); 201 202 GoogleServiceAuthError matching_error = 203 GoogleServiceAuthError::FromConnectionError(-101); 204 205 EXPECT_TRUE(expected_error == matching_error); 206 207 expected_error = GoogleServiceAuthError::FromConnectionError(6); 208 209 EXPECT_FALSE(expected_error == matching_error); 210 211 expected_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE); 212 213 EXPECT_FALSE(expected_error == matching_error); 214 215 matching_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE); 216 217 EXPECT_TRUE(expected_error == matching_error); 218} 219 220TEST_F(GaiaAuthFetcherTest, LoginNetFailure) { 221 int error_no = net::ERR_CONNECTION_RESET; 222 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no); 223 224 GoogleServiceAuthError expected_error = 225 GoogleServiceAuthError::FromConnectionError(error_no); 226 227 MockGaiaConsumer consumer; 228 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) 229 .Times(1); 230 231 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 232 233 MockFetcher mock_fetcher( 234 client_login_source_, status, 0, net::ResponseCookies(), std::string(), 235 net::URLFetcher::GET, &auth); 236 auth.OnURLFetchComplete(&mock_fetcher); 237} 238 239TEST_F(GaiaAuthFetcherTest, TokenNetFailure) { 240 int error_no = net::ERR_CONNECTION_RESET; 241 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no); 242 243 GoogleServiceAuthError expected_error = 244 GoogleServiceAuthError::FromConnectionError(error_no); 245 246 MockGaiaConsumer consumer; 247 EXPECT_CALL(consumer, OnIssueAuthTokenFailure(_, expected_error)) 248 .Times(1); 249 250 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 251 252 MockFetcher mock_fetcher( 253 issue_auth_token_source_, status, 0, cookies_, std::string(), 254 net::URLFetcher::GET, &auth); 255 auth.OnURLFetchComplete(&mock_fetcher); 256} 257 258 259TEST_F(GaiaAuthFetcherTest, LoginDenied) { 260 std::string data("Error=BadAuthentication"); 261 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 262 263 GoogleServiceAuthError expected_error( 264 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 265 266 MockGaiaConsumer consumer; 267 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error)) 268 .Times(1); 269 270 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 271 272 MockFetcher mock_fetcher( 273 client_login_source_, status, net::HTTP_FORBIDDEN, cookies_, data, 274 net::URLFetcher::GET, &auth); 275 auth.OnURLFetchComplete(&mock_fetcher); 276} 277 278TEST_F(GaiaAuthFetcherTest, ParseRequest) { 279 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth"); 280 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth"); 281 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth"); 282 RunParsingTest("SID=sid\nAuth=auth\n", "sid", std::string(), "auth"); 283 RunParsingTest("LSID=lsid\nAuth=auth\n", std::string(), "lsid", "auth"); 284 RunParsingTest("\nAuth=auth\n", std::string(), std::string(), "auth"); 285 RunParsingTest("SID=sid", "sid", std::string(), std::string()); 286} 287 288TEST_F(GaiaAuthFetcherTest, ParseErrorRequest) { 289 RunErrorParsingTest("Url=U\n" 290 "Error=E\n" 291 "CaptchaToken=T\n" 292 "CaptchaUrl=C\n", "E", "U", "C", "T"); 293 RunErrorParsingTest("CaptchaToken=T\n" 294 "Error=E\n" 295 "Url=U\n" 296 "CaptchaUrl=C\n", "E", "U", "C", "T"); 297 RunErrorParsingTest("\n\n\nCaptchaToken=T\n" 298 "\nError=E\n" 299 "\nUrl=U\n" 300 "CaptchaUrl=C\n", "E", "U", "C", "T"); 301} 302 303 304TEST_F(GaiaAuthFetcherTest, OnlineLogin) { 305 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n"); 306 307 GaiaAuthConsumer::ClientLoginResult result; 308 result.lsid = "lsid"; 309 result.sid = "sid"; 310 result.token = "auth"; 311 result.data = data; 312 313 MockGaiaConsumer consumer; 314 EXPECT_CALL(consumer, OnClientLoginSuccess(result)) 315 .Times(1); 316 317 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 318 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 319 MockFetcher mock_fetcher( 320 client_login_source_, status, net::HTTP_OK, cookies_, data, 321 net::URLFetcher::GET, &auth); 322 auth.OnURLFetchComplete(&mock_fetcher); 323} 324 325TEST_F(GaiaAuthFetcherTest, WorkingIssueAuthToken) { 326 MockGaiaConsumer consumer; 327 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess(_, "token")) 328 .Times(1); 329 330 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 331 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 332 MockFetcher mock_fetcher( 333 issue_auth_token_source_, status, net::HTTP_OK, cookies_, "token", 334 net::URLFetcher::GET, &auth); 335 auth.OnURLFetchComplete(&mock_fetcher); 336} 337 338TEST_F(GaiaAuthFetcherTest, CheckTwoFactorResponse) { 339 std::string response = 340 base::StringPrintf("Error=BadAuthentication\n%s\n", 341 GaiaAuthFetcher::kSecondFactor); 342 EXPECT_TRUE(GaiaAuthFetcher::IsSecondFactorSuccess(response)); 343} 344 345TEST_F(GaiaAuthFetcherTest, CheckNormalErrorCode) { 346 std::string response = "Error=BadAuthentication\n"; 347 EXPECT_FALSE(GaiaAuthFetcher::IsSecondFactorSuccess(response)); 348} 349 350TEST_F(GaiaAuthFetcherTest, TwoFactorLogin) { 351 std::string response = base::StringPrintf("Error=BadAuthentication\n%s\n", 352 GaiaAuthFetcher::kSecondFactor); 353 354 GoogleServiceAuthError error = 355 GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR); 356 357 MockGaiaConsumer consumer; 358 EXPECT_CALL(consumer, OnClientLoginFailure(error)) 359 .Times(1); 360 361 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 362 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 363 MockFetcher mock_fetcher( 364 client_login_source_, status, net::HTTP_FORBIDDEN, cookies_, response, 365 net::URLFetcher::GET, &auth); 366 auth.OnURLFetchComplete(&mock_fetcher); 367} 368 369TEST_F(GaiaAuthFetcherTest, CaptchaParse) { 370 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 371 std::string data = "Url=http://www.google.com/login/captcha\n" 372 "Error=CaptchaRequired\n" 373 "CaptchaToken=CCTOKEN\n" 374 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n"; 375 GoogleServiceAuthError error = 376 GaiaAuthFetcher::GenerateAuthError(data, status); 377 378 std::string token = "CCTOKEN"; 379 GURL image_url("http://accounts.google.com/Captcha?ctoken=CCTOKEN"); 380 GURL unlock_url("http://www.google.com/login/captcha"); 381 382 EXPECT_EQ(error.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED); 383 EXPECT_EQ(error.captcha().token, token); 384 EXPECT_EQ(error.captcha().image_url, image_url); 385 EXPECT_EQ(error.captcha().unlock_url, unlock_url); 386} 387 388TEST_F(GaiaAuthFetcherTest, AccountDeletedError) { 389 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 390 std::string data = "Error=AccountDeleted\n"; 391 GoogleServiceAuthError error = 392 GaiaAuthFetcher::GenerateAuthError(data, status); 393 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DELETED); 394} 395 396TEST_F(GaiaAuthFetcherTest, AccountDisabledError) { 397 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 398 std::string data = "Error=AccountDisabled\n"; 399 GoogleServiceAuthError error = 400 GaiaAuthFetcher::GenerateAuthError(data, status); 401 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DISABLED); 402} 403 404TEST_F(GaiaAuthFetcherTest,BadAuthenticationError) { 405 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 406 std::string data = "Error=BadAuthentication\n"; 407 GoogleServiceAuthError error = 408 GaiaAuthFetcher::GenerateAuthError(data, status); 409 EXPECT_EQ(error.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 410} 411 412TEST_F(GaiaAuthFetcherTest,IncomprehensibleError) { 413 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 414 std::string data = "Error=Gobbledygook\n"; 415 GoogleServiceAuthError error = 416 GaiaAuthFetcher::GenerateAuthError(data, status); 417 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE); 418} 419 420TEST_F(GaiaAuthFetcherTest,ServiceUnavailableError) { 421 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 422 std::string data = "Error=ServiceUnavailable\n"; 423 GoogleServiceAuthError error = 424 GaiaAuthFetcher::GenerateOAuthLoginError(data, status); 425 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE); 426} 427 428TEST_F(GaiaAuthFetcherTest, OAuthAccountDeletedError) { 429 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 430 std::string data = "Error=adel\n"; 431 GoogleServiceAuthError error = 432 GaiaAuthFetcher::GenerateOAuthLoginError(data, status); 433 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DELETED); 434} 435 436TEST_F(GaiaAuthFetcherTest, OAuthAccountDisabledError) { 437 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 438 std::string data = "Error=adis\n"; 439 GoogleServiceAuthError error = 440 GaiaAuthFetcher::GenerateOAuthLoginError(data, status); 441 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DISABLED); 442} 443 444TEST_F(GaiaAuthFetcherTest, OAuthBadAuthenticationError) { 445 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 446 std::string data = "Error=badauth\n"; 447 GoogleServiceAuthError error = 448 GaiaAuthFetcher::GenerateOAuthLoginError(data, status); 449 EXPECT_EQ(error.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 450} 451 452TEST_F(GaiaAuthFetcherTest, OAuthServiceUnavailableError) { 453 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 454 std::string data = "Error=ire\n"; 455 GoogleServiceAuthError error = 456 GaiaAuthFetcher::GenerateOAuthLoginError(data, status); 457 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE); 458} 459 460TEST_F(GaiaAuthFetcherTest, FullLogin) { 461 MockGaiaConsumer consumer; 462 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) 463 .Times(1); 464 465 MockURLFetcherFactory<MockFetcher> factory; 466 467 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 468 auth.StartClientLogin("username", 469 "password", 470 "service", 471 std::string(), 472 std::string(), 473 GaiaAuthFetcher::HostedAccountsAllowed); 474} 475 476TEST_F(GaiaAuthFetcherTest, FullLoginFailure) { 477 MockGaiaConsumer consumer; 478 EXPECT_CALL(consumer, OnClientLoginFailure(_)) 479 .Times(1); 480 481 MockURLFetcherFactory<MockFetcher> factory; 482 factory.set_success(false); 483 484 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 485 auth.StartClientLogin("username", 486 "password", 487 "service", 488 std::string(), 489 std::string(), 490 GaiaAuthFetcher::HostedAccountsAllowed); 491} 492 493TEST_F(GaiaAuthFetcherTest, ClientFetchPending) { 494 MockGaiaConsumer consumer; 495 EXPECT_CALL(consumer, OnClientLoginSuccess(_)) 496 .Times(1); 497 498 net::TestURLFetcherFactory factory; 499 500 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 501 auth.StartClientLogin("username", 502 "password", 503 "service", 504 std::string(), 505 std::string(), 506 GaiaAuthFetcher::HostedAccountsAllowed); 507 508 EXPECT_TRUE(auth.HasPendingFetch()); 509 MockFetcher mock_fetcher( 510 client_login_source_, 511 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 512 net::HTTP_OK, cookies_, "SID=sid\nLSID=lsid\nAuth=auth\n", 513 net::URLFetcher::GET, &auth); 514 auth.OnURLFetchComplete(&mock_fetcher); 515 EXPECT_FALSE(auth.HasPendingFetch()); 516} 517 518TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) { 519 MockGaiaConsumer consumer; 520 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token")) 521 .Times(1); 522 523 net::TestURLFetcherFactory factory; 524 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 525 auth.StartIssueAuthToken("sid", "lsid", "service"); 526 527 EXPECT_TRUE(auth.HasPendingFetch()); 528 MockFetcher mock_fetcher( 529 issue_auth_token_source_, 530 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 531 net::HTTP_OK, cookies_, "token", 532 net::URLFetcher::GET, &auth); 533 auth.OnURLFetchComplete(&mock_fetcher); 534 EXPECT_FALSE(auth.HasPendingFetch()); 535} 536 537TEST_F(GaiaAuthFetcherTest, FullTokenFailure) { 538 MockGaiaConsumer consumer; 539 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _)) 540 .Times(1); 541 542 net::TestURLFetcherFactory factory; 543 544 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 545 auth.StartIssueAuthToken("sid", "lsid", "service"); 546 547 EXPECT_TRUE(auth.HasPendingFetch()); 548 MockFetcher mock_fetcher( 549 issue_auth_token_source_, 550 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 551 net::HTTP_FORBIDDEN, 552 cookies_, 553 std::string(), 554 net::URLFetcher::GET, 555 &auth); 556 auth.OnURLFetchComplete(&mock_fetcher); 557 EXPECT_FALSE(auth.HasPendingFetch()); 558} 559 560TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) { 561 MockGaiaConsumer consumer; 562 EXPECT_CALL(consumer, OnClientOAuthSuccess( 563 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1); 564 565 net::TestURLFetcherFactory factory; 566 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 567 auth.StartLsoForOAuthLoginTokenExchange("lso_token"); 568 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 569 EXPECT_TRUE(NULL != fetcher); 570 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, 571 fetcher->GetLoadFlags()); 572 573 net::ResponseCookies cookies; 574 cookies.push_back(kGetAuthCodeValidCookie); 575 EXPECT_TRUE(auth.HasPendingFetch()); 576 MockFetcher mock_fetcher1( 577 client_login_to_oauth2_source_, 578 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 579 net::HTTP_OK, 580 cookies, 581 std::string(), 582 net::URLFetcher::POST, 583 &auth); 584 auth.OnURLFetchComplete(&mock_fetcher1); 585 EXPECT_TRUE(auth.HasPendingFetch()); 586 MockFetcher mock_fetcher2( 587 oauth2_token_source_, 588 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 589 net::HTTP_OK, cookies_, kGetTokenPairValidResponse, 590 net::URLFetcher::POST, &auth); 591 auth.OnURLFetchComplete(&mock_fetcher2); 592 EXPECT_FALSE(auth.HasPendingFetch()); 593} 594 595TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies) { 596 MockGaiaConsumer consumer; 597 net::TestURLFetcherFactory factory; 598 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 599 auth.StartCookieForOAuthLoginTokenExchange("0"); 600 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 601 EXPECT_TRUE(NULL != fetcher); 602 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags()); 603} 604 605TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenClientLoginToOAuth2Failure) { 606 MockGaiaConsumer consumer; 607 EXPECT_CALL(consumer, OnClientOAuthFailure(_)) 608 .Times(1); 609 610 net::TestURLFetcherFactory factory; 611 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 612 auth.StartLsoForOAuthLoginTokenExchange("lso_token"); 613 614 net::ResponseCookies cookies; 615 EXPECT_TRUE(auth.HasPendingFetch()); 616 MockFetcher mock_fetcher( 617 client_login_to_oauth2_source_, 618 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 619 net::HTTP_FORBIDDEN, 620 cookies, 621 std::string(), 622 net::URLFetcher::POST, 623 &auth); 624 auth.OnURLFetchComplete(&mock_fetcher); 625 EXPECT_FALSE(auth.HasPendingFetch()); 626} 627 628TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenOAuth2TokenPairFailure) { 629 MockGaiaConsumer consumer; 630 EXPECT_CALL(consumer, OnClientOAuthFailure(_)) 631 .Times(1); 632 633 net::TestURLFetcherFactory factory; 634 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 635 auth.StartLsoForOAuthLoginTokenExchange("lso_token"); 636 637 net::ResponseCookies cookies; 638 cookies.push_back(kGetAuthCodeValidCookie); 639 EXPECT_TRUE(auth.HasPendingFetch()); 640 MockFetcher mock_fetcher1( 641 client_login_to_oauth2_source_, 642 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 643 net::HTTP_OK, 644 cookies, 645 std::string(), 646 net::URLFetcher::POST, 647 &auth); 648 auth.OnURLFetchComplete(&mock_fetcher1); 649 EXPECT_TRUE(auth.HasPendingFetch()); 650 MockFetcher mock_fetcher2( 651 oauth2_token_source_, 652 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 653 net::HTTP_FORBIDDEN, 654 cookies_, 655 std::string(), 656 net::URLFetcher::POST, 657 &auth); 658 auth.OnURLFetchComplete(&mock_fetcher2); 659 EXPECT_FALSE(auth.HasPendingFetch()); 660} 661 662TEST_F(GaiaAuthFetcherTest, MergeSessionSuccess) { 663 MockGaiaConsumer consumer; 664 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>")) 665 .Times(1); 666 667 net::TestURLFetcherFactory factory; 668 669 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 670 auth.StartMergeSession("myubertoken"); 671 672 EXPECT_TRUE(auth.HasPendingFetch()); 673 MockFetcher mock_fetcher( 674 merge_session_source_, 675 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 676 net::HTTP_OK, cookies_, "<html></html>", net::URLFetcher::GET, 677 &auth); 678 auth.OnURLFetchComplete(&mock_fetcher); 679 EXPECT_FALSE(auth.HasPendingFetch()); 680} 681 682TEST_F(GaiaAuthFetcherTest, MergeSessionSuccessRedirect) { 683 MockGaiaConsumer consumer; 684 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>")) 685 .Times(1); 686 687 net::TestURLFetcherFactory factory; 688 689 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 690 auth.StartMergeSession("myubertoken"); 691 692 // Make sure the fetcher created has the expected flags. Set its url() 693 // properties to reflect a redirect. 694 net::TestURLFetcher* test_fetcher = factory.GetFetcherByID(0); 695 EXPECT_TRUE(test_fetcher != NULL); 696 EXPECT_TRUE(test_fetcher->GetLoadFlags() == net::LOAD_NORMAL); 697 EXPECT_TRUE(auth.HasPendingFetch()); 698 699 GURL final_url("http://www.google.com/CheckCookie"); 700 test_fetcher->set_url(final_url); 701 test_fetcher->set_status( 702 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); 703 test_fetcher->set_response_code(net::HTTP_OK); 704 test_fetcher->set_cookies(cookies_); 705 test_fetcher->SetResponseString("<html></html>"); 706 707 auth.OnURLFetchComplete(test_fetcher); 708 EXPECT_FALSE(auth.HasPendingFetch()); 709} 710 711TEST_F(GaiaAuthFetcherTest, UberAuthTokenSuccess) { 712 MockGaiaConsumer consumer; 713 EXPECT_CALL(consumer, OnUberAuthTokenSuccess("uberToken")) 714 .Times(1); 715 716 net::TestURLFetcherFactory factory; 717 718 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 719 auth.StartTokenFetchForUberAuthExchange("myAccessToken"); 720 721 EXPECT_TRUE(auth.HasPendingFetch()); 722 MockFetcher mock_fetcher( 723 uberauth_token_source_, 724 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), 725 net::HTTP_OK, cookies_, "uberToken", net::URLFetcher::POST, 726 &auth); 727 auth.OnURLFetchComplete(&mock_fetcher); 728 EXPECT_FALSE(auth.HasPendingFetch()); 729} 730 731TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) { 732 { // No cookies. 733 std::string auth_code; 734 net::ResponseCookies cookies; 735 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( 736 cookies, &auth_code)); 737 EXPECT_EQ("", auth_code); 738 } 739 { // Few cookies, nothing appropriate. 740 std::string auth_code; 741 net::ResponseCookies cookies; 742 cookies.push_back(kGetAuthCodeCookieNoSecure); 743 cookies.push_back(kGetAuthCodeCookieNoHttpOnly); 744 cookies.push_back(kGetAuthCodeCookieNoOAuthCode); 745 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( 746 cookies, &auth_code)); 747 EXPECT_EQ("", auth_code); 748 } 749 { // Few cookies, one of them is valid. 750 std::string auth_code; 751 net::ResponseCookies cookies; 752 cookies.push_back(kGetAuthCodeCookieNoSecure); 753 cookies.push_back(kGetAuthCodeCookieNoHttpOnly); 754 cookies.push_back(kGetAuthCodeCookieNoOAuthCode); 755 cookies.push_back(kGetAuthCodeValidCookie); 756 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( 757 cookies, &auth_code)); 758 EXPECT_EQ("test-code", auth_code); 759 } 760 { // Single valid cookie (like in real responses). 761 std::string auth_code; 762 net::ResponseCookies cookies; 763 cookies.push_back(kGetAuthCodeValidCookie); 764 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response( 765 cookies, &auth_code)); 766 EXPECT_EQ("test-code", auth_code); 767 } 768} 769 770TEST_F(GaiaAuthFetcherTest, StartOAuthLogin) { 771 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA 772 // responses. 773 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n"); 774 775 GaiaAuthConsumer::ClientLoginResult result; 776 result.lsid = "lsid"; 777 result.sid = "sid"; 778 result.token = "auth"; 779 result.data = data; 780 781 MockGaiaConsumer consumer; 782 EXPECT_CALL(consumer, OnClientLoginSuccess(result)) 783 .Times(1); 784 785 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext()); 786 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); 787 MockFetcher mock_fetcher( 788 oauth_login_gurl_, status, net::HTTP_OK, cookies_, data, 789 net::URLFetcher::GET, &auth); 790 auth.OnURLFetchComplete(&mock_fetcher); 791} 792