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.
5 // A complete set of unit tests for GaiaAuthFetcher.
6 // Originally ported from GoogleAuthenticator tests.
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"
30 using ::testing::Invoke
;
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
[] =
43 " \"refresh_token\": \"rt1\","
44 " \"access_token\": \"at1\","
45 " \"expires_in\": 3600,"
46 " \"token_type\": \"Bearer\""
49 MockFetcher::MockFetcher(bool success
,
51 const std::string
& results
,
52 net::URLFetcher::RequestType request_type
,
53 net::URLFetcherDelegate
* d
)
54 : TestURLFetcher(0, url
, d
) {
56 net::URLRequestStatus::Status code
;
59 set_response_code(net::HTTP_OK
);
60 code
= net::URLRequestStatus::SUCCESS
;
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
,
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
) {
80 set_response_code(response_code
);
82 SetResponseString(results
);
85 MockFetcher::~MockFetcher() {}
87 void MockFetcher::Start() {
88 delegate()->OnURLFetchComplete(this);
91 class GaiaAuthFetcherTest
: public testing::Test
{
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
) {
112 std::string out_lsid
;
113 std::string out_token
;
115 GaiaAuthFetcher::ParseClientLoginResponse(data
,
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
,
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_
;
156 net::TestURLRequestContextGetter
* GetRequestContext() {
157 if (!request_context_getter_
.get()) {
158 request_context_getter_
= new net::TestURLRequestContextGetter(
159 message_loop_
.message_loop_proxy());
161 return request_context_getter_
.get();
164 base::MessageLoop message_loop_
;
165 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
168 class MockGaiaConsumer
: public GaiaAuthConsumer
{
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_METHOD1(OnGetCheckConnectionInfoSuccess
, void(const std::string
& data
));
195 #define MAYBE_ErrorComparator DISABLED_ErrorComparator
197 #define MAYBE_ErrorComparator ErrorComparator
200 TEST_F(GaiaAuthFetcherTest
, MAYBE_ErrorComparator
) {
201 GoogleServiceAuthError expected_error
=
202 GoogleServiceAuthError::FromConnectionError(-101);
204 GoogleServiceAuthError matching_error
=
205 GoogleServiceAuthError::FromConnectionError(-101);
207 EXPECT_TRUE(expected_error
== matching_error
);
209 expected_error
= GoogleServiceAuthError::FromConnectionError(6);
211 EXPECT_FALSE(expected_error
== matching_error
);
213 expected_error
= GoogleServiceAuthError(GoogleServiceAuthError::NONE
);
215 EXPECT_FALSE(expected_error
== matching_error
);
217 matching_error
= GoogleServiceAuthError(GoogleServiceAuthError::NONE
);
219 EXPECT_TRUE(expected_error
== matching_error
);
222 TEST_F(GaiaAuthFetcherTest
, LoginNetFailure
) {
223 int error_no
= net::ERR_CONNECTION_RESET
;
224 net::URLRequestStatus
status(net::URLRequestStatus::FAILED
, error_no
);
226 GoogleServiceAuthError expected_error
=
227 GoogleServiceAuthError::FromConnectionError(error_no
);
229 MockGaiaConsumer consumer
;
230 EXPECT_CALL(consumer
, OnClientLoginFailure(expected_error
))
233 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
235 MockFetcher
mock_fetcher(
236 client_login_source_
, status
, 0, net::ResponseCookies(), std::string(),
237 net::URLFetcher::GET
, &auth
);
238 auth
.OnURLFetchComplete(&mock_fetcher
);
241 TEST_F(GaiaAuthFetcherTest
, TokenNetFailure
) {
242 int error_no
= net::ERR_CONNECTION_RESET
;
243 net::URLRequestStatus
status(net::URLRequestStatus::FAILED
, error_no
);
245 GoogleServiceAuthError expected_error
=
246 GoogleServiceAuthError::FromConnectionError(error_no
);
248 MockGaiaConsumer consumer
;
249 EXPECT_CALL(consumer
, OnIssueAuthTokenFailure(_
, expected_error
))
252 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
254 MockFetcher
mock_fetcher(
255 issue_auth_token_source_
, status
, 0, cookies_
, std::string(),
256 net::URLFetcher::GET
, &auth
);
257 auth
.OnURLFetchComplete(&mock_fetcher
);
261 TEST_F(GaiaAuthFetcherTest
, LoginDenied
) {
262 std::string
data("Error=BadAuthentication");
263 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
265 GoogleServiceAuthError
expected_error(
266 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
268 MockGaiaConsumer consumer
;
269 EXPECT_CALL(consumer
, OnClientLoginFailure(expected_error
))
272 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
274 MockFetcher
mock_fetcher(
275 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, data
,
276 net::URLFetcher::GET
, &auth
);
277 auth
.OnURLFetchComplete(&mock_fetcher
);
280 TEST_F(GaiaAuthFetcherTest
, ParseRequest
) {
281 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth");
282 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth");
283 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth");
284 RunParsingTest("SID=sid\nAuth=auth\n", "sid", std::string(), "auth");
285 RunParsingTest("LSID=lsid\nAuth=auth\n", std::string(), "lsid", "auth");
286 RunParsingTest("\nAuth=auth\n", std::string(), std::string(), "auth");
287 RunParsingTest("SID=sid", "sid", std::string(), std::string());
290 TEST_F(GaiaAuthFetcherTest
, ParseErrorRequest
) {
291 RunErrorParsingTest("Url=U\n"
294 "CaptchaUrl=C\n", "E", "U", "C", "T");
295 RunErrorParsingTest("CaptchaToken=T\n"
298 "CaptchaUrl=C\n", "E", "U", "C", "T");
299 RunErrorParsingTest("\n\n\nCaptchaToken=T\n"
302 "CaptchaUrl=C\n", "E", "U", "C", "T");
306 TEST_F(GaiaAuthFetcherTest
, OnlineLogin
) {
307 std::string
data("SID=sid\nLSID=lsid\nAuth=auth\n");
309 GaiaAuthConsumer::ClientLoginResult result
;
310 result
.lsid
= "lsid";
312 result
.token
= "auth";
315 MockGaiaConsumer consumer
;
316 EXPECT_CALL(consumer
, OnClientLoginSuccess(result
))
319 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
320 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
321 MockFetcher
mock_fetcher(
322 client_login_source_
, status
, net::HTTP_OK
, cookies_
, data
,
323 net::URLFetcher::GET
, &auth
);
324 auth
.OnURLFetchComplete(&mock_fetcher
);
327 TEST_F(GaiaAuthFetcherTest
, WorkingIssueAuthToken
) {
328 MockGaiaConsumer consumer
;
329 EXPECT_CALL(consumer
, OnIssueAuthTokenSuccess(_
, "token"))
332 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
333 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
334 MockFetcher
mock_fetcher(
335 issue_auth_token_source_
, status
, net::HTTP_OK
, cookies_
, "token",
336 net::URLFetcher::GET
, &auth
);
337 auth
.OnURLFetchComplete(&mock_fetcher
);
340 TEST_F(GaiaAuthFetcherTest
, CheckTwoFactorResponse
) {
341 std::string response
=
342 base::StringPrintf("Error=BadAuthentication\n%s\n",
343 GaiaAuthFetcher::kSecondFactor
);
344 EXPECT_TRUE(GaiaAuthFetcher::IsSecondFactorSuccess(response
));
347 TEST_F(GaiaAuthFetcherTest
, CheckNormalErrorCode
) {
348 std::string response
= "Error=BadAuthentication\n";
349 EXPECT_FALSE(GaiaAuthFetcher::IsSecondFactorSuccess(response
));
352 TEST_F(GaiaAuthFetcherTest
, TwoFactorLogin
) {
353 std::string response
= base::StringPrintf("Error=BadAuthentication\n%s\n",
354 GaiaAuthFetcher::kSecondFactor
);
356 GoogleServiceAuthError error
=
357 GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR
);
359 MockGaiaConsumer consumer
;
360 EXPECT_CALL(consumer
, OnClientLoginFailure(error
))
363 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
364 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
365 MockFetcher
mock_fetcher(
366 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, response
,
367 net::URLFetcher::GET
, &auth
);
368 auth
.OnURLFetchComplete(&mock_fetcher
);
371 TEST_F(GaiaAuthFetcherTest
, WebLoginRequired
) {
372 std::string response
= base::StringPrintf("Error=BadAuthentication\n%s\n",
373 GaiaAuthFetcher::kWebLoginRequired
);
375 GoogleServiceAuthError error
=
376 GoogleServiceAuthError(GoogleServiceAuthError::WEB_LOGIN_REQUIRED
);
378 MockGaiaConsumer consumer
;
379 EXPECT_CALL(consumer
, OnClientLoginFailure(error
))
382 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
383 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
384 MockFetcher
mock_fetcher(
385 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, response
,
386 net::URLFetcher::GET
, &auth
);
387 auth
.OnURLFetchComplete(&mock_fetcher
);
390 TEST_F(GaiaAuthFetcherTest
, CaptchaParse
) {
391 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
392 std::string data
= "Url=http://www.google.com/login/captcha\n"
393 "Error=CaptchaRequired\n"
394 "CaptchaToken=CCTOKEN\n"
395 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n";
396 GoogleServiceAuthError error
=
397 GaiaAuthFetcher::GenerateAuthError(data
, status
);
399 std::string token
= "CCTOKEN";
400 GURL
image_url("http://accounts.google.com/Captcha?ctoken=CCTOKEN");
401 GURL
unlock_url("http://www.google.com/login/captcha");
403 EXPECT_EQ(error
.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED
);
404 EXPECT_EQ(error
.captcha().token
, token
);
405 EXPECT_EQ(error
.captcha().image_url
, image_url
);
406 EXPECT_EQ(error
.captcha().unlock_url
, unlock_url
);
409 TEST_F(GaiaAuthFetcherTest
, AccountDeletedError
) {
410 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
411 std::string data
= "Error=AccountDeleted\n";
412 GoogleServiceAuthError error
=
413 GaiaAuthFetcher::GenerateAuthError(data
, status
);
414 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DELETED
);
417 TEST_F(GaiaAuthFetcherTest
, AccountDisabledError
) {
418 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
419 std::string data
= "Error=AccountDisabled\n";
420 GoogleServiceAuthError error
=
421 GaiaAuthFetcher::GenerateAuthError(data
, status
);
422 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DISABLED
);
425 TEST_F(GaiaAuthFetcherTest
, BadAuthenticationError
) {
426 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
427 std::string data
= "Error=BadAuthentication\n";
428 GoogleServiceAuthError error
=
429 GaiaAuthFetcher::GenerateAuthError(data
, status
);
430 EXPECT_EQ(error
.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
433 TEST_F(GaiaAuthFetcherTest
, IncomprehensibleError
) {
434 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
435 std::string data
= "Error=Gobbledygook\n";
436 GoogleServiceAuthError error
=
437 GaiaAuthFetcher::GenerateAuthError(data
, status
);
438 EXPECT_EQ(error
.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
441 TEST_F(GaiaAuthFetcherTest
, ServiceUnavailableError
) {
442 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
443 std::string data
= "Error=ServiceUnavailable\n";
444 GoogleServiceAuthError error
=
445 GaiaAuthFetcher::GenerateAuthError(data
, status
);
446 EXPECT_EQ(error
.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
449 TEST_F(GaiaAuthFetcherTest
, FullLogin
) {
450 MockGaiaConsumer consumer
;
451 EXPECT_CALL(consumer
, OnClientLoginSuccess(_
))
454 MockURLFetcherFactory
<MockFetcher
> factory
;
456 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
457 auth
.StartClientLogin("username",
462 GaiaAuthFetcher::HostedAccountsAllowed
);
465 TEST_F(GaiaAuthFetcherTest
, FullLoginFailure
) {
466 MockGaiaConsumer consumer
;
467 EXPECT_CALL(consumer
, OnClientLoginFailure(_
))
470 MockURLFetcherFactory
<MockFetcher
> factory
;
471 factory
.set_success(false);
473 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
474 auth
.StartClientLogin("username",
479 GaiaAuthFetcher::HostedAccountsAllowed
);
482 TEST_F(GaiaAuthFetcherTest
, ClientFetchPending
) {
483 MockGaiaConsumer consumer
;
484 EXPECT_CALL(consumer
, OnClientLoginSuccess(_
))
487 net::TestURLFetcherFactory factory
;
489 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
490 auth
.StartClientLogin("username",
495 GaiaAuthFetcher::HostedAccountsAllowed
);
497 EXPECT_TRUE(auth
.HasPendingFetch());
498 MockFetcher
mock_fetcher(
499 client_login_source_
,
500 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
501 net::HTTP_OK
, cookies_
, "SID=sid\nLSID=lsid\nAuth=auth\n",
502 net::URLFetcher::GET
, &auth
);
503 auth
.OnURLFetchComplete(&mock_fetcher
);
504 EXPECT_FALSE(auth
.HasPendingFetch());
507 TEST_F(GaiaAuthFetcherTest
, FullTokenSuccess
) {
508 MockGaiaConsumer consumer
;
509 EXPECT_CALL(consumer
, OnIssueAuthTokenSuccess("service", "token"))
512 net::TestURLFetcherFactory factory
;
513 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
514 auth
.StartIssueAuthToken("sid", "lsid", "service");
516 EXPECT_TRUE(auth
.HasPendingFetch());
517 MockFetcher
mock_fetcher(
518 issue_auth_token_source_
,
519 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
520 net::HTTP_OK
, cookies_
, "token",
521 net::URLFetcher::GET
, &auth
);
522 auth
.OnURLFetchComplete(&mock_fetcher
);
523 EXPECT_FALSE(auth
.HasPendingFetch());
526 TEST_F(GaiaAuthFetcherTest
, FullTokenFailure
) {
527 MockGaiaConsumer consumer
;
528 EXPECT_CALL(consumer
, OnIssueAuthTokenFailure("service", _
))
531 net::TestURLFetcherFactory factory
;
533 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
534 auth
.StartIssueAuthToken("sid", "lsid", "service");
536 EXPECT_TRUE(auth
.HasPendingFetch());
537 MockFetcher
mock_fetcher(
538 issue_auth_token_source_
,
539 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
543 net::URLFetcher::GET
,
545 auth
.OnURLFetchComplete(&mock_fetcher
);
546 EXPECT_FALSE(auth
.HasPendingFetch());
549 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenSuccess
) {
550 MockGaiaConsumer consumer
;
551 EXPECT_CALL(consumer
, OnClientOAuthSuccess(
552 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
554 net::TestURLFetcherFactory factory
;
555 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
556 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
557 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
558 EXPECT_TRUE(NULL
!= fetcher
);
559 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
,
560 fetcher
->GetLoadFlags());
562 net::ResponseCookies cookies
;
563 cookies
.push_back(kGetAuthCodeValidCookie
);
564 EXPECT_TRUE(auth
.HasPendingFetch());
565 MockFetcher
mock_fetcher1(
566 client_login_to_oauth2_source_
,
567 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
571 net::URLFetcher::POST
,
573 auth
.OnURLFetchComplete(&mock_fetcher1
);
574 EXPECT_TRUE(auth
.HasPendingFetch());
575 MockFetcher
mock_fetcher2(
576 oauth2_token_source_
,
577 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
578 net::HTTP_OK
, cookies_
, kGetTokenPairValidResponse
,
579 net::URLFetcher::POST
, &auth
);
580 auth
.OnURLFetchComplete(&mock_fetcher2
);
581 EXPECT_FALSE(auth
.HasPendingFetch());
584 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenWithCookies
) {
585 MockGaiaConsumer consumer
;
586 net::TestURLFetcherFactory factory
;
587 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
588 auth
.StartCookieForOAuthLoginTokenExchange("0");
589 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
590 EXPECT_TRUE(NULL
!= fetcher
);
591 EXPECT_EQ(net::LOAD_NORMAL
, fetcher
->GetLoadFlags());
592 EXPECT_FALSE(EndsWith(fetcher
->upload_data(), "device_type=chrome", true));
595 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenWithCookies_DeviceId
) {
596 MockGaiaConsumer consumer
;
597 net::TestURLFetcherFactory factory
;
598 std::string
expected_device_id("ABCDE-12345");
599 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
600 auth
.StartCookieForOAuthLoginTokenExchangeWithDeviceId("0",
602 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
603 EXPECT_TRUE(NULL
!= fetcher
);
604 EXPECT_EQ(net::LOAD_NORMAL
, fetcher
->GetLoadFlags());
605 EXPECT_TRUE(EndsWith(fetcher
->upload_data(), "device_type=chrome", true));
606 net::HttpRequestHeaders extra_request_headers
;
607 fetcher
->GetExtraRequestHeaders(&extra_request_headers
);
608 std::string device_id
;
609 EXPECT_TRUE(extra_request_headers
.GetHeader("X-Device-ID", &device_id
));
610 EXPECT_EQ(device_id
, expected_device_id
);
613 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenClientLoginToOAuth2Failure
) {
614 MockGaiaConsumer consumer
;
615 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
618 net::TestURLFetcherFactory factory
;
619 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
620 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
622 net::ResponseCookies cookies
;
623 EXPECT_TRUE(auth
.HasPendingFetch());
624 MockFetcher
mock_fetcher(
625 client_login_to_oauth2_source_
,
626 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
630 net::URLFetcher::POST
,
632 auth
.OnURLFetchComplete(&mock_fetcher
);
633 EXPECT_FALSE(auth
.HasPendingFetch());
636 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenOAuth2TokenPairFailure
) {
637 MockGaiaConsumer consumer
;
638 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
641 net::TestURLFetcherFactory factory
;
642 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
643 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
645 net::ResponseCookies cookies
;
646 cookies
.push_back(kGetAuthCodeValidCookie
);
647 EXPECT_TRUE(auth
.HasPendingFetch());
648 MockFetcher
mock_fetcher1(
649 client_login_to_oauth2_source_
,
650 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
654 net::URLFetcher::POST
,
656 auth
.OnURLFetchComplete(&mock_fetcher1
);
657 EXPECT_TRUE(auth
.HasPendingFetch());
658 MockFetcher
mock_fetcher2(
659 oauth2_token_source_
,
660 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
664 net::URLFetcher::POST
,
666 auth
.OnURLFetchComplete(&mock_fetcher2
);
667 EXPECT_FALSE(auth
.HasPendingFetch());
670 TEST_F(GaiaAuthFetcherTest
, MergeSessionSuccess
) {
671 MockGaiaConsumer consumer
;
672 EXPECT_CALL(consumer
, OnMergeSessionSuccess("<html></html>"))
675 net::TestURLFetcherFactory factory
;
677 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
678 auth
.StartMergeSession("myubertoken", std::string());
680 EXPECT_TRUE(auth
.HasPendingFetch());
681 MockFetcher
mock_fetcher(
682 merge_session_source_
,
683 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
684 net::HTTP_OK
, cookies_
, "<html></html>", net::URLFetcher::GET
,
686 auth
.OnURLFetchComplete(&mock_fetcher
);
687 EXPECT_FALSE(auth
.HasPendingFetch());
690 TEST_F(GaiaAuthFetcherTest
, MergeSessionSuccessRedirect
) {
691 MockGaiaConsumer consumer
;
692 EXPECT_CALL(consumer
, OnMergeSessionSuccess("<html></html>"))
695 net::TestURLFetcherFactory factory
;
697 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
698 auth
.StartMergeSession("myubertoken", std::string());
700 // Make sure the fetcher created has the expected flags. Set its url()
701 // properties to reflect a redirect.
702 net::TestURLFetcher
* test_fetcher
= factory
.GetFetcherByID(0);
703 EXPECT_TRUE(test_fetcher
!= NULL
);
704 EXPECT_TRUE(test_fetcher
->GetLoadFlags() == net::LOAD_NORMAL
);
705 EXPECT_TRUE(auth
.HasPendingFetch());
707 GURL
final_url("http://www.google.com/CheckCookie");
708 test_fetcher
->set_url(final_url
);
709 test_fetcher
->set_status(
710 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
711 test_fetcher
->set_response_code(net::HTTP_OK
);
712 test_fetcher
->set_cookies(cookies_
);
713 test_fetcher
->SetResponseString("<html></html>");
715 auth
.OnURLFetchComplete(test_fetcher
);
716 EXPECT_FALSE(auth
.HasPendingFetch());
719 TEST_F(GaiaAuthFetcherTest
, UberAuthTokenSuccess
) {
720 MockGaiaConsumer consumer
;
721 EXPECT_CALL(consumer
, OnUberAuthTokenSuccess("uberToken"))
724 net::TestURLFetcherFactory factory
;
726 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
727 auth
.StartTokenFetchForUberAuthExchange("myAccessToken");
729 EXPECT_TRUE(auth
.HasPendingFetch());
730 MockFetcher
mock_fetcher(
731 uberauth_token_source_
,
732 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
733 net::HTTP_OK
, cookies_
, "uberToken", net::URLFetcher::POST
,
735 auth
.OnURLFetchComplete(&mock_fetcher
);
736 EXPECT_FALSE(auth
.HasPendingFetch());
739 TEST_F(GaiaAuthFetcherTest
, ParseClientLoginToOAuth2Response
) {
741 std::string auth_code
;
742 net::ResponseCookies cookies
;
743 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
744 cookies
, &auth_code
));
745 EXPECT_EQ("", auth_code
);
747 { // Few cookies, nothing appropriate.
748 std::string auth_code
;
749 net::ResponseCookies cookies
;
750 cookies
.push_back(kGetAuthCodeCookieNoSecure
);
751 cookies
.push_back(kGetAuthCodeCookieNoHttpOnly
);
752 cookies
.push_back(kGetAuthCodeCookieNoOAuthCode
);
753 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
754 cookies
, &auth_code
));
755 EXPECT_EQ("", auth_code
);
757 { // Few cookies, one of them is valid.
758 std::string auth_code
;
759 net::ResponseCookies cookies
;
760 cookies
.push_back(kGetAuthCodeCookieNoSecure
);
761 cookies
.push_back(kGetAuthCodeCookieNoHttpOnly
);
762 cookies
.push_back(kGetAuthCodeCookieNoOAuthCode
);
763 cookies
.push_back(kGetAuthCodeValidCookie
);
764 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
765 cookies
, &auth_code
));
766 EXPECT_EQ("test-code", auth_code
);
768 { // Single valid cookie (like in real responses).
769 std::string auth_code
;
770 net::ResponseCookies cookies
;
771 cookies
.push_back(kGetAuthCodeValidCookie
);
772 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
773 cookies
, &auth_code
));
774 EXPECT_EQ("test-code", auth_code
);
778 TEST_F(GaiaAuthFetcherTest
, StartOAuthLogin
) {
779 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA
781 std::string
data("SID=sid\nLSID=lsid\nAuth=auth\n");
783 GaiaAuthConsumer::ClientLoginResult result
;
784 result
.lsid
= "lsid";
786 result
.token
= "auth";
789 MockGaiaConsumer consumer
;
790 EXPECT_CALL(consumer
, OnClientLoginSuccess(result
))
793 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
794 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
795 MockFetcher
mock_fetcher(
796 oauth_login_gurl_
, status
, net::HTTP_OK
, cookies_
, data
,
797 net::URLFetcher::GET
, &auth
);
798 auth
.OnURLFetchComplete(&mock_fetcher
);
801 TEST_F(GaiaAuthFetcherTest
, ListAccounts
) {
802 std::string
data("[\"gaia.l.a.r\", ["
803 "[\"gaia.l.a\", 1, \"First Last\", \"user@gmail.com\", "
804 "\"//googleusercontent.com/A/B/C/D/photo.jpg\", 1, 1, 0]]]");
805 MockGaiaConsumer consumer
;
806 EXPECT_CALL(consumer
, OnListAccountsSuccess(data
)).Times(1);
808 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
809 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
810 MockFetcher
mock_fetcher(
811 GaiaUrls::GetInstance()->ListAccountsURLWithSource(std::string()),
812 status
, net::HTTP_OK
, cookies_
, data
, net::URLFetcher::GET
, &auth
);
813 auth
.OnURLFetchComplete(&mock_fetcher
);
816 TEST_F(GaiaAuthFetcherTest
, GetCheckConnectionInfo
) {
818 "[{\"carryBackToken\": \"token1\", \"url\": \"http://www.google.com\"}]");
819 MockGaiaConsumer consumer
;
820 EXPECT_CALL(consumer
, OnGetCheckConnectionInfoSuccess(data
)).Times(1);
822 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
823 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
824 MockFetcher
mock_fetcher(
825 GaiaUrls::GetInstance()->GetCheckConnectionInfoURLWithSource(
827 status
, net::HTTP_OK
, cookies_
, data
, net::URLFetcher::GET
, &auth
);
828 auth
.OnURLFetchComplete(&mock_fetcher
);