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
));
192 MOCK_METHOD1(OnListIdpSessionsSuccess
, void(const std::string
& data
));
193 MOCK_METHOD1(OnGetTokenResponseSuccess
,
194 void(const GaiaAuthConsumer::ClientOAuthResult
& result
));
198 #define MAYBE_ErrorComparator DISABLED_ErrorComparator
200 #define MAYBE_ErrorComparator ErrorComparator
203 TEST_F(GaiaAuthFetcherTest
, MAYBE_ErrorComparator
) {
204 GoogleServiceAuthError expected_error
=
205 GoogleServiceAuthError::FromConnectionError(-101);
207 GoogleServiceAuthError matching_error
=
208 GoogleServiceAuthError::FromConnectionError(-101);
210 EXPECT_TRUE(expected_error
== matching_error
);
212 expected_error
= GoogleServiceAuthError::FromConnectionError(6);
214 EXPECT_FALSE(expected_error
== matching_error
);
216 expected_error
= GoogleServiceAuthError(GoogleServiceAuthError::NONE
);
218 EXPECT_FALSE(expected_error
== matching_error
);
220 matching_error
= GoogleServiceAuthError(GoogleServiceAuthError::NONE
);
222 EXPECT_TRUE(expected_error
== matching_error
);
225 TEST_F(GaiaAuthFetcherTest
, LoginNetFailure
) {
226 int error_no
= net::ERR_CONNECTION_RESET
;
227 net::URLRequestStatus
status(net::URLRequestStatus::FAILED
, error_no
);
229 GoogleServiceAuthError expected_error
=
230 GoogleServiceAuthError::FromConnectionError(error_no
);
232 MockGaiaConsumer consumer
;
233 EXPECT_CALL(consumer
, OnClientLoginFailure(expected_error
))
236 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
238 MockFetcher
mock_fetcher(
239 client_login_source_
, status
, 0, net::ResponseCookies(), std::string(),
240 net::URLFetcher::GET
, &auth
);
241 auth
.OnURLFetchComplete(&mock_fetcher
);
244 TEST_F(GaiaAuthFetcherTest
, TokenNetFailure
) {
245 int error_no
= net::ERR_CONNECTION_RESET
;
246 net::URLRequestStatus
status(net::URLRequestStatus::FAILED
, error_no
);
248 GoogleServiceAuthError expected_error
=
249 GoogleServiceAuthError::FromConnectionError(error_no
);
251 MockGaiaConsumer consumer
;
252 EXPECT_CALL(consumer
, OnIssueAuthTokenFailure(_
, expected_error
))
255 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
257 MockFetcher
mock_fetcher(
258 issue_auth_token_source_
, status
, 0, cookies_
, std::string(),
259 net::URLFetcher::GET
, &auth
);
260 auth
.OnURLFetchComplete(&mock_fetcher
);
264 TEST_F(GaiaAuthFetcherTest
, LoginDenied
) {
265 std::string
data("Error=BadAuthentication");
266 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
268 GoogleServiceAuthError
expected_error(
269 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
271 MockGaiaConsumer consumer
;
272 EXPECT_CALL(consumer
, OnClientLoginFailure(expected_error
))
275 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
277 MockFetcher
mock_fetcher(
278 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, data
,
279 net::URLFetcher::GET
, &auth
);
280 auth
.OnURLFetchComplete(&mock_fetcher
);
283 TEST_F(GaiaAuthFetcherTest
, ParseRequest
) {
284 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth");
285 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth");
286 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth");
287 RunParsingTest("SID=sid\nAuth=auth\n", "sid", std::string(), "auth");
288 RunParsingTest("LSID=lsid\nAuth=auth\n", std::string(), "lsid", "auth");
289 RunParsingTest("\nAuth=auth\n", std::string(), std::string(), "auth");
290 RunParsingTest("SID=sid", "sid", std::string(), std::string());
293 TEST_F(GaiaAuthFetcherTest
, ParseErrorRequest
) {
294 RunErrorParsingTest("Url=U\n"
297 "CaptchaUrl=C\n", "E", "U", "C", "T");
298 RunErrorParsingTest("CaptchaToken=T\n"
301 "CaptchaUrl=C\n", "E", "U", "C", "T");
302 RunErrorParsingTest("\n\n\nCaptchaToken=T\n"
305 "CaptchaUrl=C\n", "E", "U", "C", "T");
309 TEST_F(GaiaAuthFetcherTest
, OnlineLogin
) {
310 std::string
data("SID=sid\nLSID=lsid\nAuth=auth\n");
312 GaiaAuthConsumer::ClientLoginResult result
;
313 result
.lsid
= "lsid";
315 result
.token
= "auth";
318 MockGaiaConsumer consumer
;
319 EXPECT_CALL(consumer
, OnClientLoginSuccess(result
))
322 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
323 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
324 MockFetcher
mock_fetcher(
325 client_login_source_
, status
, net::HTTP_OK
, cookies_
, data
,
326 net::URLFetcher::GET
, &auth
);
327 auth
.OnURLFetchComplete(&mock_fetcher
);
330 TEST_F(GaiaAuthFetcherTest
, WorkingIssueAuthToken
) {
331 MockGaiaConsumer consumer
;
332 EXPECT_CALL(consumer
, OnIssueAuthTokenSuccess(_
, "token"))
335 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
336 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
337 MockFetcher
mock_fetcher(
338 issue_auth_token_source_
, status
, net::HTTP_OK
, cookies_
, "token",
339 net::URLFetcher::GET
, &auth
);
340 auth
.OnURLFetchComplete(&mock_fetcher
);
343 TEST_F(GaiaAuthFetcherTest
, CheckTwoFactorResponse
) {
344 std::string response
=
345 base::StringPrintf("Error=BadAuthentication\n%s\n",
346 GaiaAuthFetcher::kSecondFactor
);
347 EXPECT_TRUE(GaiaAuthFetcher::IsSecondFactorSuccess(response
));
350 TEST_F(GaiaAuthFetcherTest
, CheckNormalErrorCode
) {
351 std::string response
= "Error=BadAuthentication\n";
352 EXPECT_FALSE(GaiaAuthFetcher::IsSecondFactorSuccess(response
));
355 TEST_F(GaiaAuthFetcherTest
, TwoFactorLogin
) {
356 std::string response
= base::StringPrintf("Error=BadAuthentication\n%s\n",
357 GaiaAuthFetcher::kSecondFactor
);
359 GoogleServiceAuthError error
=
360 GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR
);
362 MockGaiaConsumer consumer
;
363 EXPECT_CALL(consumer
, OnClientLoginFailure(error
))
366 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
367 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
368 MockFetcher
mock_fetcher(
369 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, response
,
370 net::URLFetcher::GET
, &auth
);
371 auth
.OnURLFetchComplete(&mock_fetcher
);
374 TEST_F(GaiaAuthFetcherTest
, WebLoginRequired
) {
375 std::string response
= base::StringPrintf("Error=BadAuthentication\n%s\n",
376 GaiaAuthFetcher::kWebLoginRequired
);
378 GoogleServiceAuthError error
=
379 GoogleServiceAuthError(GoogleServiceAuthError::WEB_LOGIN_REQUIRED
);
381 MockGaiaConsumer consumer
;
382 EXPECT_CALL(consumer
, OnClientLoginFailure(error
))
385 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
386 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
387 MockFetcher
mock_fetcher(
388 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, response
,
389 net::URLFetcher::GET
, &auth
);
390 auth
.OnURLFetchComplete(&mock_fetcher
);
393 TEST_F(GaiaAuthFetcherTest
, CaptchaParse
) {
394 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
395 std::string data
= "Url=http://www.google.com/login/captcha\n"
396 "Error=CaptchaRequired\n"
397 "CaptchaToken=CCTOKEN\n"
398 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n";
399 GoogleServiceAuthError error
=
400 GaiaAuthFetcher::GenerateAuthError(data
, status
);
402 std::string token
= "CCTOKEN";
403 GURL
image_url("http://accounts.google.com/Captcha?ctoken=CCTOKEN");
404 GURL
unlock_url("http://www.google.com/login/captcha");
406 EXPECT_EQ(error
.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED
);
407 EXPECT_EQ(error
.captcha().token
, token
);
408 EXPECT_EQ(error
.captcha().image_url
, image_url
);
409 EXPECT_EQ(error
.captcha().unlock_url
, unlock_url
);
412 TEST_F(GaiaAuthFetcherTest
, AccountDeletedError
) {
413 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
414 std::string data
= "Error=AccountDeleted\n";
415 GoogleServiceAuthError error
=
416 GaiaAuthFetcher::GenerateAuthError(data
, status
);
417 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DELETED
);
420 TEST_F(GaiaAuthFetcherTest
, AccountDisabledError
) {
421 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
422 std::string data
= "Error=AccountDisabled\n";
423 GoogleServiceAuthError error
=
424 GaiaAuthFetcher::GenerateAuthError(data
, status
);
425 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DISABLED
);
428 TEST_F(GaiaAuthFetcherTest
, BadAuthenticationError
) {
429 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
430 std::string data
= "Error=BadAuthentication\n";
431 GoogleServiceAuthError error
=
432 GaiaAuthFetcher::GenerateAuthError(data
, status
);
433 EXPECT_EQ(error
.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
436 TEST_F(GaiaAuthFetcherTest
, IncomprehensibleError
) {
437 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
438 std::string data
= "Error=Gobbledygook\n";
439 GoogleServiceAuthError error
=
440 GaiaAuthFetcher::GenerateAuthError(data
, status
);
441 EXPECT_EQ(error
.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
444 TEST_F(GaiaAuthFetcherTest
, ServiceUnavailableError
) {
445 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
446 std::string data
= "Error=ServiceUnavailable\n";
447 GoogleServiceAuthError error
=
448 GaiaAuthFetcher::GenerateAuthError(data
, status
);
449 EXPECT_EQ(error
.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
452 TEST_F(GaiaAuthFetcherTest
, FullLogin
) {
453 MockGaiaConsumer consumer
;
454 EXPECT_CALL(consumer
, OnClientLoginSuccess(_
))
457 MockURLFetcherFactory
<MockFetcher
> factory
;
459 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
460 auth
.StartClientLogin("username",
465 GaiaAuthFetcher::HostedAccountsAllowed
);
468 TEST_F(GaiaAuthFetcherTest
, FullLoginFailure
) {
469 MockGaiaConsumer consumer
;
470 EXPECT_CALL(consumer
, OnClientLoginFailure(_
))
473 MockURLFetcherFactory
<MockFetcher
> factory
;
474 factory
.set_success(false);
476 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
477 auth
.StartClientLogin("username",
482 GaiaAuthFetcher::HostedAccountsAllowed
);
485 TEST_F(GaiaAuthFetcherTest
, ClientFetchPending
) {
486 MockGaiaConsumer consumer
;
487 EXPECT_CALL(consumer
, OnClientLoginSuccess(_
))
490 net::TestURLFetcherFactory factory
;
492 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
493 auth
.StartClientLogin("username",
498 GaiaAuthFetcher::HostedAccountsAllowed
);
500 EXPECT_TRUE(auth
.HasPendingFetch());
501 MockFetcher
mock_fetcher(
502 client_login_source_
,
503 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
504 net::HTTP_OK
, cookies_
, "SID=sid\nLSID=lsid\nAuth=auth\n",
505 net::URLFetcher::GET
, &auth
);
506 auth
.OnURLFetchComplete(&mock_fetcher
);
507 EXPECT_FALSE(auth
.HasPendingFetch());
510 TEST_F(GaiaAuthFetcherTest
, FullTokenSuccess
) {
511 MockGaiaConsumer consumer
;
512 EXPECT_CALL(consumer
, OnIssueAuthTokenSuccess("service", "token"))
515 net::TestURLFetcherFactory factory
;
516 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
517 auth
.StartIssueAuthToken("sid", "lsid", "service");
519 EXPECT_TRUE(auth
.HasPendingFetch());
520 MockFetcher
mock_fetcher(
521 issue_auth_token_source_
,
522 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
523 net::HTTP_OK
, cookies_
, "token",
524 net::URLFetcher::GET
, &auth
);
525 auth
.OnURLFetchComplete(&mock_fetcher
);
526 EXPECT_FALSE(auth
.HasPendingFetch());
529 TEST_F(GaiaAuthFetcherTest
, FullTokenFailure
) {
530 MockGaiaConsumer consumer
;
531 EXPECT_CALL(consumer
, OnIssueAuthTokenFailure("service", _
))
534 net::TestURLFetcherFactory factory
;
536 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
537 auth
.StartIssueAuthToken("sid", "lsid", "service");
539 EXPECT_TRUE(auth
.HasPendingFetch());
540 MockFetcher
mock_fetcher(
541 issue_auth_token_source_
,
542 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
546 net::URLFetcher::GET
,
548 auth
.OnURLFetchComplete(&mock_fetcher
);
549 EXPECT_FALSE(auth
.HasPendingFetch());
552 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenSuccess
) {
553 MockGaiaConsumer consumer
;
554 EXPECT_CALL(consumer
, OnClientOAuthSuccess(
555 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
557 net::TestURLFetcherFactory factory
;
558 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
559 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
560 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
561 EXPECT_TRUE(NULL
!= fetcher
);
562 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
,
563 fetcher
->GetLoadFlags());
565 net::ResponseCookies cookies
;
566 cookies
.push_back(kGetAuthCodeValidCookie
);
567 EXPECT_TRUE(auth
.HasPendingFetch());
568 MockFetcher
mock_fetcher1(
569 client_login_to_oauth2_source_
,
570 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
574 net::URLFetcher::POST
,
576 auth
.OnURLFetchComplete(&mock_fetcher1
);
577 EXPECT_TRUE(auth
.HasPendingFetch());
578 MockFetcher
mock_fetcher2(
579 oauth2_token_source_
,
580 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
581 net::HTTP_OK
, cookies_
, kGetTokenPairValidResponse
,
582 net::URLFetcher::POST
, &auth
);
583 auth
.OnURLFetchComplete(&mock_fetcher2
);
584 EXPECT_FALSE(auth
.HasPendingFetch());
587 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenWithCookies
) {
588 MockGaiaConsumer consumer
;
589 net::TestURLFetcherFactory factory
;
590 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
591 auth
.StartCookieForOAuthLoginTokenExchange("0");
592 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
593 EXPECT_TRUE(NULL
!= fetcher
);
594 EXPECT_EQ(net::LOAD_NORMAL
, fetcher
->GetLoadFlags());
595 EXPECT_FALSE(EndsWith(fetcher
->upload_data(), "device_type=chrome", true));
598 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenWithCookies_DeviceId
) {
599 MockGaiaConsumer consumer
;
600 net::TestURLFetcherFactory factory
;
601 std::string
expected_device_id("ABCDE-12345");
602 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
603 auth
.StartCookieForOAuthLoginTokenExchangeWithDeviceId("0",
605 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
606 EXPECT_TRUE(NULL
!= fetcher
);
607 EXPECT_EQ(net::LOAD_NORMAL
, fetcher
->GetLoadFlags());
608 EXPECT_TRUE(EndsWith(fetcher
->upload_data(), "device_type=chrome", true));
609 net::HttpRequestHeaders extra_request_headers
;
610 fetcher
->GetExtraRequestHeaders(&extra_request_headers
);
611 std::string device_id
;
612 EXPECT_TRUE(extra_request_headers
.GetHeader("X-Device-ID", &device_id
));
613 EXPECT_EQ(device_id
, expected_device_id
);
616 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenClientLoginToOAuth2Failure
) {
617 MockGaiaConsumer consumer
;
618 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
621 net::TestURLFetcherFactory factory
;
622 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
623 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
625 net::ResponseCookies cookies
;
626 EXPECT_TRUE(auth
.HasPendingFetch());
627 MockFetcher
mock_fetcher(
628 client_login_to_oauth2_source_
,
629 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
633 net::URLFetcher::POST
,
635 auth
.OnURLFetchComplete(&mock_fetcher
);
636 EXPECT_FALSE(auth
.HasPendingFetch());
639 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenOAuth2TokenPairFailure
) {
640 MockGaiaConsumer consumer
;
641 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
644 net::TestURLFetcherFactory factory
;
645 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
646 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
648 net::ResponseCookies cookies
;
649 cookies
.push_back(kGetAuthCodeValidCookie
);
650 EXPECT_TRUE(auth
.HasPendingFetch());
651 MockFetcher
mock_fetcher1(
652 client_login_to_oauth2_source_
,
653 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
657 net::URLFetcher::POST
,
659 auth
.OnURLFetchComplete(&mock_fetcher1
);
660 EXPECT_TRUE(auth
.HasPendingFetch());
661 MockFetcher
mock_fetcher2(
662 oauth2_token_source_
,
663 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
667 net::URLFetcher::POST
,
669 auth
.OnURLFetchComplete(&mock_fetcher2
);
670 EXPECT_FALSE(auth
.HasPendingFetch());
673 TEST_F(GaiaAuthFetcherTest
, MergeSessionSuccess
) {
674 MockGaiaConsumer consumer
;
675 EXPECT_CALL(consumer
, OnMergeSessionSuccess("<html></html>"))
678 net::TestURLFetcherFactory factory
;
680 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
681 auth
.StartMergeSession("myubertoken", std::string());
683 EXPECT_TRUE(auth
.HasPendingFetch());
684 MockFetcher
mock_fetcher(
685 merge_session_source_
,
686 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
687 net::HTTP_OK
, cookies_
, "<html></html>", net::URLFetcher::GET
,
689 auth
.OnURLFetchComplete(&mock_fetcher
);
690 EXPECT_FALSE(auth
.HasPendingFetch());
693 TEST_F(GaiaAuthFetcherTest
, MergeSessionSuccessRedirect
) {
694 MockGaiaConsumer consumer
;
695 EXPECT_CALL(consumer
, OnMergeSessionSuccess("<html></html>"))
698 net::TestURLFetcherFactory factory
;
700 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
701 auth
.StartMergeSession("myubertoken", std::string());
703 // Make sure the fetcher created has the expected flags. Set its url()
704 // properties to reflect a redirect.
705 net::TestURLFetcher
* test_fetcher
= factory
.GetFetcherByID(0);
706 EXPECT_TRUE(test_fetcher
!= NULL
);
707 EXPECT_TRUE(test_fetcher
->GetLoadFlags() == net::LOAD_NORMAL
);
708 EXPECT_TRUE(auth
.HasPendingFetch());
710 GURL
final_url("http://www.google.com/CheckCookie");
711 test_fetcher
->set_url(final_url
);
712 test_fetcher
->set_status(
713 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
714 test_fetcher
->set_response_code(net::HTTP_OK
);
715 test_fetcher
->set_cookies(cookies_
);
716 test_fetcher
->SetResponseString("<html></html>");
718 auth
.OnURLFetchComplete(test_fetcher
);
719 EXPECT_FALSE(auth
.HasPendingFetch());
722 TEST_F(GaiaAuthFetcherTest
, UberAuthTokenSuccess
) {
723 MockGaiaConsumer consumer
;
724 EXPECT_CALL(consumer
, OnUberAuthTokenSuccess("uberToken"))
727 net::TestURLFetcherFactory factory
;
729 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
730 auth
.StartTokenFetchForUberAuthExchange("myAccessToken");
732 EXPECT_TRUE(auth
.HasPendingFetch());
733 MockFetcher
mock_fetcher(
734 uberauth_token_source_
,
735 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
736 net::HTTP_OK
, cookies_
, "uberToken", net::URLFetcher::POST
,
738 auth
.OnURLFetchComplete(&mock_fetcher
);
739 EXPECT_FALSE(auth
.HasPendingFetch());
742 TEST_F(GaiaAuthFetcherTest
, ParseClientLoginToOAuth2Response
) {
744 std::string auth_code
;
745 net::ResponseCookies cookies
;
746 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
747 cookies
, &auth_code
));
748 EXPECT_EQ("", auth_code
);
750 { // Few cookies, nothing appropriate.
751 std::string auth_code
;
752 net::ResponseCookies cookies
;
753 cookies
.push_back(kGetAuthCodeCookieNoSecure
);
754 cookies
.push_back(kGetAuthCodeCookieNoHttpOnly
);
755 cookies
.push_back(kGetAuthCodeCookieNoOAuthCode
);
756 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
757 cookies
, &auth_code
));
758 EXPECT_EQ("", auth_code
);
760 { // Few cookies, one of them is valid.
761 std::string auth_code
;
762 net::ResponseCookies cookies
;
763 cookies
.push_back(kGetAuthCodeCookieNoSecure
);
764 cookies
.push_back(kGetAuthCodeCookieNoHttpOnly
);
765 cookies
.push_back(kGetAuthCodeCookieNoOAuthCode
);
766 cookies
.push_back(kGetAuthCodeValidCookie
);
767 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
768 cookies
, &auth_code
));
769 EXPECT_EQ("test-code", auth_code
);
771 { // Single valid cookie (like in real responses).
772 std::string auth_code
;
773 net::ResponseCookies cookies
;
774 cookies
.push_back(kGetAuthCodeValidCookie
);
775 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
776 cookies
, &auth_code
));
777 EXPECT_EQ("test-code", auth_code
);
781 TEST_F(GaiaAuthFetcherTest
, StartOAuthLogin
) {
782 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA
784 std::string
data("SID=sid\nLSID=lsid\nAuth=auth\n");
786 GaiaAuthConsumer::ClientLoginResult result
;
787 result
.lsid
= "lsid";
789 result
.token
= "auth";
792 MockGaiaConsumer consumer
;
793 EXPECT_CALL(consumer
, OnClientLoginSuccess(result
))
796 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
797 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
798 MockFetcher
mock_fetcher(
799 oauth_login_gurl_
, status
, net::HTTP_OK
, cookies_
, data
,
800 net::URLFetcher::GET
, &auth
);
801 auth
.OnURLFetchComplete(&mock_fetcher
);
804 TEST_F(GaiaAuthFetcherTest
, ListAccounts
) {
805 std::string
data("[\"gaia.l.a.r\", ["
806 "[\"gaia.l.a\", 1, \"First Last\", \"user@gmail.com\", "
807 "\"//googleusercontent.com/A/B/C/D/photo.jpg\", 1, 1, 0]]]");
808 MockGaiaConsumer consumer
;
809 EXPECT_CALL(consumer
, OnListAccountsSuccess(data
)).Times(1);
811 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
812 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
813 MockFetcher
mock_fetcher(
814 GaiaUrls::GetInstance()->ListAccountsURLWithSource(std::string()),
815 status
, net::HTTP_OK
, cookies_
, data
, net::URLFetcher::GET
, &auth
);
816 auth
.OnURLFetchComplete(&mock_fetcher
);
819 TEST_F(GaiaAuthFetcherTest
, GetCheckConnectionInfo
) {
821 "[{\"carryBackToken\": \"token1\", \"url\": \"http://www.google.com\"}]");
822 MockGaiaConsumer consumer
;
823 EXPECT_CALL(consumer
, OnGetCheckConnectionInfoSuccess(data
)).Times(1);
825 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
826 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
827 MockFetcher
mock_fetcher(
828 GaiaUrls::GetInstance()->GetCheckConnectionInfoURLWithSource(
830 status
, net::HTTP_OK
, cookies_
, data
, net::URLFetcher::GET
, &auth
);
831 auth
.OnURLFetchComplete(&mock_fetcher
);
834 TEST_F(GaiaAuthFetcherTest
, ListIDPSessions
) {
835 std::string
data("{\"sessions\":[{\"login_hint\":\"abcdefghijklmnop\"}]}");
836 MockGaiaConsumer consumer
;
837 EXPECT_CALL(consumer
, OnListIdpSessionsSuccess("abcdefghijklmnop")).Times(1);
839 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
840 auth
.StartListIDPSessions(std::string(), std::string());
842 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
843 MockFetcher
mock_fetcher(
844 GaiaUrls::GetInstance()->oauth2_iframe_url(),
845 status
, net::HTTP_OK
, cookies_
, data
, net::URLFetcher::GET
, &auth
);
846 auth
.OnURLFetchComplete(&mock_fetcher
);
849 TEST_F(GaiaAuthFetcherTest
, GetTokenResponse
) {
850 MockGaiaConsumer consumer
;
851 EXPECT_CALL(consumer
,
852 OnGetTokenResponseSuccess(
853 GaiaAuthConsumer::ClientOAuthResult(std::string(),
857 GaiaAuthFetcher
auth(&consumer
, std::string(), GetRequestContext());
858 auth
.StartGetTokenResponse(std::string(), std::string(), std::string());
860 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
861 MockFetcher
mock_fetcher(
862 GaiaUrls::GetInstance()->oauth2_iframe_url(),
863 status
, net::HTTP_OK
, cookies_
, kGetTokenPairValidResponse
,
864 net::URLFetcher::GET
, &auth
);
865 auth
.OnURLFetchComplete(&mock_fetcher
);