Pull Chrome_Elf out of the renderer process
[chromium-blink-merge.git] / google_apis / gaia / gaia_auth_fetcher_unittest.cc
blob28e5aff83c67ab77b85d6886165681c956b52c87
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.
8 #include <string>
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"
30 using ::testing::Invoke;
31 using ::testing::_;
33 const char kGetAuthCodeValidCookie[] =
34 "oauth_code=test-code; Path=/test; Secure; HttpOnly";
35 const char kGetAuthCodeCookieNoSecure[] =
36 "oauth_code=test-code; Path=/test; HttpOnly";
37 const char kGetAuthCodeCookieNoHttpOnly[] =
38 "oauth_code=test-code; Path=/test; Secure";
39 const char kGetAuthCodeCookieNoOAuthCode[] =
40 "Path=/test; Secure; HttpOnly";
41 const char kGetTokenPairValidResponse[] =
42 "{"
43 " \"refresh_token\": \"rt1\","
44 " \"access_token\": \"at1\","
45 " \"expires_in\": 3600,"
46 " \"token_type\": \"Bearer\""
47 "}";
49 MockFetcher::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;
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;
66 set_status(net::URLRequestStatus(code, 0));
67 SetResponseString(results);
70 MockFetcher::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);
85 MockFetcher::~MockFetcher() {}
87 void MockFetcher::Start() {
88 delegate()->OnURLFetchComplete(this);
91 class 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()) {}
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;
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);
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;
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);
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_;
155 protected:
156 net::TestURLRequestContextGetter* GetRequestContext() {
157 if (!request_context_getter_.get()) {
158 request_context_getter_ = new net::TestURLRequestContextGetter(
159 message_loop_.task_runner());
161 return request_context_getter_.get();
164 base::MessageLoop message_loop_;
165 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
168 class MockGaiaConsumer : public GaiaAuthConsumer {
169 public:
170 MockGaiaConsumer() {}
171 ~MockGaiaConsumer() {}
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 MOCK_METHOD1(OnListAccountsSuccess, void(const std::string& data));
191 MOCK_METHOD0(OnLogOutSuccess, void());
192 MOCK_METHOD1(OnLogOutFailure, void(const GoogleServiceAuthError& error));
193 MOCK_METHOD1(OnGetCheckConnectionInfoSuccess, void(const std::string& data));
194 MOCK_METHOD1(OnListIdpSessionsSuccess, void(const std::string& data));
195 MOCK_METHOD1(OnGetTokenResponseSuccess,
196 void(const GaiaAuthConsumer::ClientOAuthResult& result));
199 #if defined(OS_WIN)
200 #define MAYBE_ErrorComparator DISABLED_ErrorComparator
201 #else
202 #define MAYBE_ErrorComparator ErrorComparator
203 #endif
205 TEST_F(GaiaAuthFetcherTest, MAYBE_ErrorComparator) {
206 GoogleServiceAuthError expected_error =
207 GoogleServiceAuthError::FromConnectionError(-101);
209 GoogleServiceAuthError matching_error =
210 GoogleServiceAuthError::FromConnectionError(-101);
212 EXPECT_TRUE(expected_error == matching_error);
214 expected_error = GoogleServiceAuthError::FromConnectionError(6);
216 EXPECT_FALSE(expected_error == matching_error);
218 expected_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE);
220 EXPECT_FALSE(expected_error == matching_error);
222 matching_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE);
224 EXPECT_TRUE(expected_error == matching_error);
227 TEST_F(GaiaAuthFetcherTest, LoginNetFailure) {
228 int error_no = net::ERR_CONNECTION_RESET;
229 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no);
231 GoogleServiceAuthError expected_error =
232 GoogleServiceAuthError::FromConnectionError(error_no);
234 MockGaiaConsumer consumer;
235 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error))
236 .Times(1);
238 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
240 MockFetcher mock_fetcher(
241 client_login_source_, status, 0, net::ResponseCookies(), std::string(),
242 net::URLFetcher::GET, &auth);
243 auth.OnURLFetchComplete(&mock_fetcher);
246 TEST_F(GaiaAuthFetcherTest, TokenNetFailure) {
247 int error_no = net::ERR_CONNECTION_RESET;
248 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no);
250 GoogleServiceAuthError expected_error =
251 GoogleServiceAuthError::FromConnectionError(error_no);
253 MockGaiaConsumer consumer;
254 EXPECT_CALL(consumer, OnIssueAuthTokenFailure(_, expected_error))
255 .Times(1);
257 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
259 MockFetcher mock_fetcher(
260 issue_auth_token_source_, status, 0, cookies_, std::string(),
261 net::URLFetcher::GET, &auth);
262 auth.OnURLFetchComplete(&mock_fetcher);
266 TEST_F(GaiaAuthFetcherTest, LoginDenied) {
267 std::string data("Error=BadAuthentication");
268 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
270 GoogleServiceAuthError expected_error(
271 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
273 MockGaiaConsumer consumer;
274 EXPECT_CALL(consumer, OnClientLoginFailure(expected_error))
275 .Times(1);
277 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
279 MockFetcher mock_fetcher(
280 client_login_source_, status, net::HTTP_FORBIDDEN, cookies_, data,
281 net::URLFetcher::GET, &auth);
282 auth.OnURLFetchComplete(&mock_fetcher);
285 TEST_F(GaiaAuthFetcherTest, ParseRequest) {
286 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth");
287 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth");
288 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth");
289 RunParsingTest("SID=sid\nAuth=auth\n", "sid", std::string(), "auth");
290 RunParsingTest("LSID=lsid\nAuth=auth\n", std::string(), "lsid", "auth");
291 RunParsingTest("\nAuth=auth\n", std::string(), std::string(), "auth");
292 RunParsingTest("SID=sid", "sid", std::string(), std::string());
295 TEST_F(GaiaAuthFetcherTest, ParseErrorRequest) {
296 RunErrorParsingTest("Url=U\n"
297 "Error=E\n"
298 "CaptchaToken=T\n"
299 "CaptchaUrl=C\n", "E", "U", "C", "T");
300 RunErrorParsingTest("CaptchaToken=T\n"
301 "Error=E\n"
302 "Url=U\n"
303 "CaptchaUrl=C\n", "E", "U", "C", "T");
304 RunErrorParsingTest("\n\n\nCaptchaToken=T\n"
305 "\nError=E\n"
306 "\nUrl=U\n"
307 "CaptchaUrl=C\n", "E", "U", "C", "T");
311 TEST_F(GaiaAuthFetcherTest, OnlineLogin) {
312 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n");
314 GaiaAuthConsumer::ClientLoginResult result;
315 result.lsid = "lsid";
316 result.sid = "sid";
317 result.token = "auth";
318 result.data = data;
320 MockGaiaConsumer consumer;
321 EXPECT_CALL(consumer, OnClientLoginSuccess(result))
322 .Times(1);
324 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
325 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
326 MockFetcher mock_fetcher(
327 client_login_source_, status, net::HTTP_OK, cookies_, data,
328 net::URLFetcher::GET, &auth);
329 auth.OnURLFetchComplete(&mock_fetcher);
332 TEST_F(GaiaAuthFetcherTest, WorkingIssueAuthToken) {
333 MockGaiaConsumer consumer;
334 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess(_, "token"))
335 .Times(1);
337 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
338 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
339 MockFetcher mock_fetcher(
340 issue_auth_token_source_, status, net::HTTP_OK, cookies_, "token",
341 net::URLFetcher::GET, &auth);
342 auth.OnURLFetchComplete(&mock_fetcher);
345 TEST_F(GaiaAuthFetcherTest, CheckTwoFactorResponse) {
346 std::string response =
347 base::StringPrintf("Error=BadAuthentication\n%s\n",
348 GaiaAuthFetcher::kSecondFactor);
349 EXPECT_TRUE(GaiaAuthFetcher::IsSecondFactorSuccess(response));
352 TEST_F(GaiaAuthFetcherTest, CheckNormalErrorCode) {
353 std::string response = "Error=BadAuthentication\n";
354 EXPECT_FALSE(GaiaAuthFetcher::IsSecondFactorSuccess(response));
357 TEST_F(GaiaAuthFetcherTest, TwoFactorLogin) {
358 std::string response = base::StringPrintf("Error=BadAuthentication\n%s\n",
359 GaiaAuthFetcher::kSecondFactor);
361 GoogleServiceAuthError error =
362 GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR);
364 MockGaiaConsumer consumer;
365 EXPECT_CALL(consumer, OnClientLoginFailure(error))
366 .Times(1);
368 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
369 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
370 MockFetcher mock_fetcher(
371 client_login_source_, status, net::HTTP_FORBIDDEN, cookies_, response,
372 net::URLFetcher::GET, &auth);
373 auth.OnURLFetchComplete(&mock_fetcher);
376 TEST_F(GaiaAuthFetcherTest, WebLoginRequired) {
377 std::string response = base::StringPrintf("Error=BadAuthentication\n%s\n",
378 GaiaAuthFetcher::kWebLoginRequired);
380 GoogleServiceAuthError error =
381 GoogleServiceAuthError(GoogleServiceAuthError::WEB_LOGIN_REQUIRED);
383 MockGaiaConsumer consumer;
384 EXPECT_CALL(consumer, OnClientLoginFailure(error))
385 .Times(1);
387 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
388 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
389 MockFetcher mock_fetcher(
390 client_login_source_, status, net::HTTP_FORBIDDEN, cookies_, response,
391 net::URLFetcher::GET, &auth);
392 auth.OnURLFetchComplete(&mock_fetcher);
395 TEST_F(GaiaAuthFetcherTest, CaptchaParse) {
396 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
397 std::string data = "Url=http://www.google.com/login/captcha\n"
398 "Error=CaptchaRequired\n"
399 "CaptchaToken=CCTOKEN\n"
400 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n";
401 GoogleServiceAuthError error =
402 GaiaAuthFetcher::GenerateAuthError(data, status);
404 std::string token = "CCTOKEN";
405 GURL image_url("http://accounts.google.com/Captcha?ctoken=CCTOKEN");
406 GURL unlock_url("http://www.google.com/login/captcha");
408 EXPECT_EQ(error.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED);
409 EXPECT_EQ(error.captcha().token, token);
410 EXPECT_EQ(error.captcha().image_url, image_url);
411 EXPECT_EQ(error.captcha().unlock_url, unlock_url);
414 TEST_F(GaiaAuthFetcherTest, AccountDeletedError) {
415 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
416 std::string data = "Error=AccountDeleted\n";
417 GoogleServiceAuthError error =
418 GaiaAuthFetcher::GenerateAuthError(data, status);
419 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DELETED);
422 TEST_F(GaiaAuthFetcherTest, AccountDisabledError) {
423 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
424 std::string data = "Error=AccountDisabled\n";
425 GoogleServiceAuthError error =
426 GaiaAuthFetcher::GenerateAuthError(data, status);
427 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DISABLED);
430 TEST_F(GaiaAuthFetcherTest, BadAuthenticationError) {
431 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
432 std::string data = "Error=BadAuthentication\n";
433 GoogleServiceAuthError error =
434 GaiaAuthFetcher::GenerateAuthError(data, status);
435 EXPECT_EQ(error.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
438 TEST_F(GaiaAuthFetcherTest, IncomprehensibleError) {
439 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
440 std::string data = "Error=Gobbledygook\n";
441 GoogleServiceAuthError error =
442 GaiaAuthFetcher::GenerateAuthError(data, status);
443 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE);
446 TEST_F(GaiaAuthFetcherTest, ServiceUnavailableError) {
447 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
448 std::string data = "Error=ServiceUnavailable\n";
449 GoogleServiceAuthError error =
450 GaiaAuthFetcher::GenerateAuthError(data, status);
451 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE);
454 TEST_F(GaiaAuthFetcherTest, FullLogin) {
455 MockGaiaConsumer consumer;
456 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
457 .Times(1);
459 MockURLFetcherFactory<MockFetcher> factory;
461 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
462 auth.StartClientLogin("username",
463 "password",
464 "service",
465 std::string(),
466 std::string(),
467 GaiaAuthFetcher::HostedAccountsAllowed);
470 TEST_F(GaiaAuthFetcherTest, FullLoginFailure) {
471 MockGaiaConsumer consumer;
472 EXPECT_CALL(consumer, OnClientLoginFailure(_))
473 .Times(1);
475 MockURLFetcherFactory<MockFetcher> factory;
476 factory.set_success(false);
478 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
479 auth.StartClientLogin("username",
480 "password",
481 "service",
482 std::string(),
483 std::string(),
484 GaiaAuthFetcher::HostedAccountsAllowed);
487 TEST_F(GaiaAuthFetcherTest, ClientFetchPending) {
488 MockGaiaConsumer consumer;
489 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
490 .Times(1);
492 net::TestURLFetcherFactory factory;
494 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
495 auth.StartClientLogin("username",
496 "password",
497 "service",
498 std::string(),
499 std::string(),
500 GaiaAuthFetcher::HostedAccountsAllowed);
502 EXPECT_TRUE(auth.HasPendingFetch());
503 MockFetcher mock_fetcher(
504 client_login_source_,
505 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
506 net::HTTP_OK, cookies_, "SID=sid\nLSID=lsid\nAuth=auth\n",
507 net::URLFetcher::GET, &auth);
508 auth.OnURLFetchComplete(&mock_fetcher);
509 EXPECT_FALSE(auth.HasPendingFetch());
512 TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) {
513 MockGaiaConsumer consumer;
514 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token"))
515 .Times(1);
517 net::TestURLFetcherFactory factory;
518 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
519 auth.StartIssueAuthToken("sid", "lsid", "service");
521 EXPECT_TRUE(auth.HasPendingFetch());
522 MockFetcher mock_fetcher(
523 issue_auth_token_source_,
524 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
525 net::HTTP_OK, cookies_, "token",
526 net::URLFetcher::GET, &auth);
527 auth.OnURLFetchComplete(&mock_fetcher);
528 EXPECT_FALSE(auth.HasPendingFetch());
531 TEST_F(GaiaAuthFetcherTest, FullTokenFailure) {
532 MockGaiaConsumer consumer;
533 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _))
534 .Times(1);
536 net::TestURLFetcherFactory factory;
538 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
539 auth.StartIssueAuthToken("sid", "lsid", "service");
541 EXPECT_TRUE(auth.HasPendingFetch());
542 MockFetcher mock_fetcher(
543 issue_auth_token_source_,
544 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
545 net::HTTP_FORBIDDEN,
546 cookies_,
547 std::string(),
548 net::URLFetcher::GET,
549 &auth);
550 auth.OnURLFetchComplete(&mock_fetcher);
551 EXPECT_FALSE(auth.HasPendingFetch());
554 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) {
555 MockGaiaConsumer consumer;
556 EXPECT_CALL(consumer, OnClientOAuthSuccess(
557 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
559 net::TestURLFetcherFactory factory;
560 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
561 auth.StartCookieForOAuthLoginTokenExchange("0");
562 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
563 EXPECT_TRUE(NULL != fetcher);
564 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
565 EXPECT_EQ(std::string::npos,
566 fetcher->GetOriginalURL().query().find("device_type=chrome"));
568 net::ResponseCookies cookies;
569 cookies.push_back(kGetAuthCodeValidCookie);
570 EXPECT_TRUE(auth.HasPendingFetch());
571 MockFetcher mock_fetcher1(
572 client_login_to_oauth2_source_,
573 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
574 net::HTTP_OK,
575 cookies,
576 std::string(),
577 net::URLFetcher::POST,
578 &auth);
579 auth.OnURLFetchComplete(&mock_fetcher1);
580 EXPECT_TRUE(auth.HasPendingFetch());
581 MockFetcher mock_fetcher2(
582 oauth2_token_source_,
583 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
584 net::HTTP_OK, cookies_, kGetTokenPairValidResponse,
585 net::URLFetcher::POST, &auth);
586 auth.OnURLFetchComplete(&mock_fetcher2);
587 EXPECT_FALSE(auth.HasPendingFetch());
590 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies_DeviceId) {
591 MockGaiaConsumer consumer;
592 net::TestURLFetcherFactory factory;
593 std::string expected_device_id("ABCDE-12345");
594 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
595 auth.StartCookieForOAuthLoginTokenExchangeWithDeviceId("0",
596 expected_device_id);
597 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
598 EXPECT_TRUE(NULL != fetcher);
599 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
600 EXPECT_NE(std::string::npos,
601 fetcher->GetOriginalURL().query().find("device_type=chrome"));
602 net::HttpRequestHeaders extra_request_headers;
603 fetcher->GetExtraRequestHeaders(&extra_request_headers);
604 std::string device_id;
605 EXPECT_TRUE(extra_request_headers.GetHeader("X-Device-ID", &device_id));
606 EXPECT_EQ(device_id, expected_device_id);
609 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenClientLoginToOAuth2Failure) {
610 MockGaiaConsumer consumer;
611 EXPECT_CALL(consumer, OnClientOAuthFailure(_))
612 .Times(1);
614 net::TestURLFetcherFactory factory;
615 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
616 auth.StartCookieForOAuthLoginTokenExchange(std::string());
618 net::ResponseCookies cookies;
619 EXPECT_TRUE(auth.HasPendingFetch());
620 MockFetcher mock_fetcher(
621 client_login_to_oauth2_source_,
622 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
623 net::HTTP_FORBIDDEN,
624 cookies,
625 std::string(),
626 net::URLFetcher::POST,
627 &auth);
628 auth.OnURLFetchComplete(&mock_fetcher);
629 EXPECT_FALSE(auth.HasPendingFetch());
632 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenOAuth2TokenPairFailure) {
633 MockGaiaConsumer consumer;
634 EXPECT_CALL(consumer, OnClientOAuthFailure(_))
635 .Times(1);
637 net::TestURLFetcherFactory factory;
638 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
639 auth.StartCookieForOAuthLoginTokenExchange(std::string());
641 net::ResponseCookies cookies;
642 cookies.push_back(kGetAuthCodeValidCookie);
643 EXPECT_TRUE(auth.HasPendingFetch());
644 MockFetcher mock_fetcher1(
645 client_login_to_oauth2_source_,
646 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
647 net::HTTP_OK,
648 cookies,
649 std::string(),
650 net::URLFetcher::POST,
651 &auth);
652 auth.OnURLFetchComplete(&mock_fetcher1);
653 EXPECT_TRUE(auth.HasPendingFetch());
654 MockFetcher mock_fetcher2(
655 oauth2_token_source_,
656 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
657 net::HTTP_FORBIDDEN,
658 cookies_,
659 std::string(),
660 net::URLFetcher::POST,
661 &auth);
662 auth.OnURLFetchComplete(&mock_fetcher2);
663 EXPECT_FALSE(auth.HasPendingFetch());
666 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccess) {
667 MockGaiaConsumer consumer;
668 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>"))
669 .Times(1);
671 net::TestURLFetcherFactory factory;
673 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
674 auth.StartMergeSession("myubertoken", std::string());
676 EXPECT_TRUE(auth.HasPendingFetch());
677 MockFetcher mock_fetcher(
678 merge_session_source_,
679 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
680 net::HTTP_OK, cookies_, "<html></html>", net::URLFetcher::GET,
681 &auth);
682 auth.OnURLFetchComplete(&mock_fetcher);
683 EXPECT_FALSE(auth.HasPendingFetch());
686 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccessRedirect) {
687 MockGaiaConsumer consumer;
688 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>"))
689 .Times(1);
691 net::TestURLFetcherFactory factory;
693 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
694 auth.StartMergeSession("myubertoken", std::string());
696 // Make sure the fetcher created has the expected flags. Set its url()
697 // properties to reflect a redirect.
698 net::TestURLFetcher* test_fetcher = factory.GetFetcherByID(0);
699 EXPECT_TRUE(test_fetcher != NULL);
700 EXPECT_TRUE(test_fetcher->GetLoadFlags() == net::LOAD_NORMAL);
701 EXPECT_TRUE(auth.HasPendingFetch());
703 GURL final_url("http://www.google.com/CheckCookie");
704 test_fetcher->set_url(final_url);
705 test_fetcher->set_status(
706 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
707 test_fetcher->set_response_code(net::HTTP_OK);
708 test_fetcher->set_cookies(cookies_);
709 test_fetcher->SetResponseString("<html></html>");
711 auth.OnURLFetchComplete(test_fetcher);
712 EXPECT_FALSE(auth.HasPendingFetch());
715 TEST_F(GaiaAuthFetcherTest, UberAuthTokenSuccess) {
716 MockGaiaConsumer consumer;
717 EXPECT_CALL(consumer, OnUberAuthTokenSuccess("uberToken"))
718 .Times(1);
720 net::TestURLFetcherFactory factory;
722 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
723 auth.StartTokenFetchForUberAuthExchange("myAccessToken");
725 EXPECT_TRUE(auth.HasPendingFetch());
726 MockFetcher mock_fetcher(
727 uberauth_token_source_,
728 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
729 net::HTTP_OK, cookies_, "uberToken", net::URLFetcher::POST,
730 &auth);
731 auth.OnURLFetchComplete(&mock_fetcher);
732 EXPECT_FALSE(auth.HasPendingFetch());
735 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) {
736 { // No cookies.
737 std::string auth_code;
738 net::ResponseCookies cookies;
739 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
740 cookies, &auth_code));
741 EXPECT_EQ("", auth_code);
743 { // Few cookies, nothing appropriate.
744 std::string auth_code;
745 net::ResponseCookies cookies;
746 cookies.push_back(kGetAuthCodeCookieNoSecure);
747 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
748 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
749 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
750 cookies, &auth_code));
751 EXPECT_EQ("", auth_code);
753 { // Few cookies, one of them is valid.
754 std::string auth_code;
755 net::ResponseCookies cookies;
756 cookies.push_back(kGetAuthCodeCookieNoSecure);
757 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
758 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
759 cookies.push_back(kGetAuthCodeValidCookie);
760 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
761 cookies, &auth_code));
762 EXPECT_EQ("test-code", auth_code);
764 { // Single valid cookie (like in real responses).
765 std::string auth_code;
766 net::ResponseCookies cookies;
767 cookies.push_back(kGetAuthCodeValidCookie);
768 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
769 cookies, &auth_code));
770 EXPECT_EQ("test-code", auth_code);
774 TEST_F(GaiaAuthFetcherTest, StartOAuthLogin) {
775 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA
776 // responses.
777 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n");
779 GaiaAuthConsumer::ClientLoginResult result;
780 result.lsid = "lsid";
781 result.sid = "sid";
782 result.token = "auth";
783 result.data = data;
785 MockGaiaConsumer consumer;
786 EXPECT_CALL(consumer, OnClientLoginSuccess(result))
787 .Times(1);
789 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
790 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
791 MockFetcher mock_fetcher(
792 oauth_login_gurl_, status, net::HTTP_OK, cookies_, data,
793 net::URLFetcher::GET, &auth);
794 auth.OnURLFetchComplete(&mock_fetcher);
797 TEST_F(GaiaAuthFetcherTest, ListAccounts) {
798 std::string data("[\"gaia.l.a.r\", ["
799 "[\"gaia.l.a\", 1, \"First Last\", \"user@gmail.com\", "
800 "\"//googleusercontent.com/A/B/C/D/photo.jpg\", 1, 1, 0]]]");
801 MockGaiaConsumer consumer;
802 EXPECT_CALL(consumer, OnListAccountsSuccess(data)).Times(1);
804 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
805 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
806 MockFetcher mock_fetcher(
807 GaiaUrls::GetInstance()->ListAccountsURLWithSource(std::string()),
808 status, net::HTTP_OK, cookies_, data, net::URLFetcher::GET, &auth);
809 auth.OnURLFetchComplete(&mock_fetcher);
812 TEST_F(GaiaAuthFetcherTest, LogOutSuccess) {
813 MockGaiaConsumer consumer;
814 EXPECT_CALL(consumer, OnLogOutSuccess()).Times(1);
816 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
817 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
818 MockFetcher mock_fetcher(
819 GaiaUrls::GetInstance()->LogOutURLWithSource(std::string()), status,
820 net::HTTP_OK, cookies_, std::string(), net::URLFetcher::GET, &auth);
821 auth.OnURLFetchComplete(&mock_fetcher);
824 TEST_F(GaiaAuthFetcherTest, LogOutFailure) {
825 int error_no = net::ERR_CONNECTION_RESET;
826 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no);
828 GoogleServiceAuthError expected_error =
829 GoogleServiceAuthError::FromConnectionError(error_no);
830 MockGaiaConsumer consumer;
831 EXPECT_CALL(consumer, OnLogOutFailure(expected_error)).Times(1);
833 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
835 MockFetcher mock_fetcher(
836 GaiaUrls::GetInstance()->LogOutURLWithSource(std::string()), status, 0,
837 cookies_, std::string(), net::URLFetcher::GET, &auth);
838 auth.OnURLFetchComplete(&mock_fetcher);
841 TEST_F(GaiaAuthFetcherTest, GetCheckConnectionInfo) {
842 std::string data(
843 "[{\"carryBackToken\": \"token1\", \"url\": \"http://www.google.com\"}]");
844 MockGaiaConsumer consumer;
845 EXPECT_CALL(consumer, OnGetCheckConnectionInfoSuccess(data)).Times(1);
847 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
848 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
849 MockFetcher mock_fetcher(
850 GaiaUrls::GetInstance()->GetCheckConnectionInfoURLWithSource(
851 std::string()),
852 status, net::HTTP_OK, cookies_, data, net::URLFetcher::GET, &auth);
853 auth.OnURLFetchComplete(&mock_fetcher);
856 TEST_F(GaiaAuthFetcherTest, ListIDPSessions) {
857 std::string data("{\"sessions\":[{\"login_hint\":\"abcdefghijklmnop\"}]}");
858 MockGaiaConsumer consumer;
859 EXPECT_CALL(consumer, OnListIdpSessionsSuccess("abcdefghijklmnop")).Times(1);
861 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
862 auth.StartListIDPSessions(std::string(), std::string());
864 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
865 MockFetcher mock_fetcher(
866 GaiaUrls::GetInstance()->oauth2_iframe_url(),
867 status, net::HTTP_OK, cookies_, data, net::URLFetcher::GET, &auth);
868 auth.OnURLFetchComplete(&mock_fetcher);
871 TEST_F(GaiaAuthFetcherTest, GetTokenResponse) {
872 MockGaiaConsumer consumer;
873 EXPECT_CALL(consumer,
874 OnGetTokenResponseSuccess(
875 GaiaAuthConsumer::ClientOAuthResult(std::string(),
876 "at1",
877 3600))).Times(1);
879 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
880 auth.StartGetTokenResponse(std::string(), std::string(), std::string());
882 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
883 MockFetcher mock_fetcher(
884 GaiaUrls::GetInstance()->oauth2_iframe_url(),
885 status, net::HTTP_OK, cookies_, kGetTokenPairValidResponse,
886 net::URLFetcher::GET, &auth);
887 auth.OnURLFetchComplete(&mock_fetcher);