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/message_loop.h"
12 #include "base/stringprintf.h"
13 #include "base/values.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "google_apis/gaia/gaia_auth_consumer.h"
16 #include "google_apis/gaia/gaia_auth_fetcher.h"
17 #include "google_apis/gaia/gaia_urls.h"
18 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "google_apis/gaia/mock_url_fetcher_factory.h"
20 #include "google_apis/google_api_keys.h"
21 #include "googleurl/src/gurl.h"
22 #include "net/base/load_flags.h"
23 #include "net/base/net_errors.h"
24 #include "net/http/http_status_code.h"
25 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "net/url_request/url_fetcher_delegate.h"
27 #include "net/url_request/url_request_status.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
32 using ::testing::Invoke
;
35 static const char kGetAuthCodeValidCookie
[] =
36 "oauth_code=test-code; Path=/test; Secure; HttpOnly";
37 static const char kGetAuthCodeCookieNoSecure
[] =
38 "oauth_code=test-code; Path=/test; HttpOnly";
39 static const char kGetAuthCodeCookieNoHttpOnly
[] =
40 "oauth_code=test-code; Path=/test; Secure";
41 static const char kGetAuthCodeCookieNoOAuthCode
[] =
42 "Path=/test; Secure; HttpOnly";
43 static const char kGetTokenPairValidResponse
[] =
45 " \"refresh_token\": \"rt1\","
46 " \"access_token\": \"at1\","
47 " \"expires_in\": 3600,"
48 " \"token_type\": \"Bearer\""
50 static const char kClientOAuthValidResponse
[] =
53 " \"refresh_token\": \"rt1\","
54 " \"access_token\": \"at1\","
55 " \"expires_in\": 3600,"
56 " \"token_type\": \"Bearer\""
60 static void ExpectCaptchaChallenge(const GoogleServiceAuthError
& error
) {
61 // Make sure this is a captcha server challange.
62 EXPECT_EQ(GoogleServiceAuthError::CAPTCHA_REQUIRED
, error
.state());
63 EXPECT_EQ("challengetokenblob", error
.captcha().token
);
64 EXPECT_EQ("http://www.audio.com/", error
.captcha().audio_url
.spec());
65 EXPECT_EQ("http://www.image.com/", error
.captcha().image_url
.spec());
66 EXPECT_EQ(640, error
.captcha().image_width
);
67 EXPECT_EQ(480, error
.captcha().image_height
);
70 static void ExpectBadAuth(const GoogleServiceAuthError
& error
) {
71 EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
, error
.state());
74 static void ExpectTwoFactorChallenge(const GoogleServiceAuthError
& error
) {
75 // Make sure this is a captcha server challange.
76 EXPECT_EQ(GoogleServiceAuthError::TWO_FACTOR
, error
.state());
77 EXPECT_EQ("challengetokenblob", error
.second_factor().token
);
78 EXPECT_EQ("prompt_text", error
.second_factor().prompt_text
);
79 EXPECT_EQ("alternate_text", error
.second_factor().alternate_text
);
80 EXPECT_EQ(10, error
.second_factor().field_length
);
85 MockFetcher::MockFetcher(bool success
,
87 const std::string
& results
,
88 net::URLFetcher::RequestType request_type
,
89 net::URLFetcherDelegate
* d
)
90 : TestURLFetcher(0, url
, d
) {
92 net::URLRequestStatus::Status code
;
95 set_response_code(net::HTTP_OK
);
96 code
= net::URLRequestStatus::SUCCESS
;
98 set_response_code(net::HTTP_FORBIDDEN
);
99 code
= net::URLRequestStatus::FAILED
;
102 set_status(net::URLRequestStatus(code
, 0));
103 SetResponseString(results
);
106 MockFetcher::MockFetcher(const GURL
& url
,
107 const net::URLRequestStatus
& status
,
109 const net::ResponseCookies
& cookies
,
110 const std::string
& results
,
111 net::URLFetcher::RequestType request_type
,
112 net::URLFetcherDelegate
* d
)
113 : TestURLFetcher(0, url
, d
) {
116 set_response_code(response_code
);
117 set_cookies(cookies
);
118 SetResponseString(results
);
121 MockFetcher::~MockFetcher() {}
123 void MockFetcher::Start() {
124 delegate()->OnURLFetchComplete(this);
127 class GaiaAuthFetcherTest
: public testing::Test
{
129 GaiaAuthFetcherTest()
130 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()),
131 issue_auth_token_source_(
132 GaiaUrls::GetInstance()->issue_auth_token_url()),
133 client_login_to_oauth2_source_(
134 GaiaUrls::GetInstance()->client_login_to_oauth2_url()),
135 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()),
136 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()),
137 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()),
138 uberauth_token_source_(base::StringPrintf(
139 "%s?source=&issueuberauth=1",
140 GaiaUrls::GetInstance()->oauth1_login_url().c_str())),
141 client_oauth_source_(GaiaUrls::GetInstance()->client_oauth_url()),
142 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()) {}
144 void RunParsingTest(const std::string
& data
,
145 const std::string
& sid
,
146 const std::string
& lsid
,
147 const std::string
& token
) {
149 std::string out_lsid
;
150 std::string out_token
;
152 GaiaAuthFetcher::ParseClientLoginResponse(data
,
156 EXPECT_EQ(lsid
, out_lsid
);
157 EXPECT_EQ(sid
, out_sid
);
158 EXPECT_EQ(token
, out_token
);
161 void RunErrorParsingTest(const std::string
& data
,
162 const std::string
& error
,
163 const std::string
& error_url
,
164 const std::string
& captcha_url
,
165 const std::string
& captcha_token
) {
166 std::string out_error
;
167 std::string out_error_url
;
168 std::string out_captcha_url
;
169 std::string out_captcha_token
;
171 GaiaAuthFetcher::ParseClientLoginFailure(data
,
176 EXPECT_EQ(error
, out_error
);
177 EXPECT_EQ(error_url
, out_error_url
);
178 EXPECT_EQ(captcha_url
, out_captcha_url
);
179 EXPECT_EQ(captcha_token
, out_captcha_token
);
182 net::ResponseCookies cookies_
;
183 GURL client_login_source_
;
184 GURL issue_auth_token_source_
;
185 GURL client_login_to_oauth2_source_
;
186 GURL oauth2_token_source_
;
187 GURL token_auth_source_
;
188 GURL merge_session_source_
;
189 GURL uberauth_token_source_
;
190 GURL client_oauth_source_
;
191 GURL oauth_login_gurl_
;
192 TestingProfile profile_
;
194 MessageLoop message_loop_
;
197 class MockGaiaConsumer
: public GaiaAuthConsumer
{
199 MockGaiaConsumer() {}
200 ~MockGaiaConsumer() {}
202 MOCK_METHOD1(OnClientLoginSuccess
, void(const ClientLoginResult
& result
));
203 MOCK_METHOD2(OnIssueAuthTokenSuccess
, void(const std::string
& service
,
204 const std::string
& token
));
205 MOCK_METHOD1(OnClientOAuthSuccess
,
206 void(const GaiaAuthConsumer::ClientOAuthResult
& result
));
207 MOCK_METHOD1(OnMergeSessionSuccess
, void(const std::string
& data
));
208 MOCK_METHOD1(OnUberAuthTokenSuccess
, void(const std::string
& data
));
209 MOCK_METHOD1(OnClientLoginFailure
,
210 void(const GoogleServiceAuthError
& error
));
211 MOCK_METHOD2(OnIssueAuthTokenFailure
, void(const std::string
& service
,
212 const GoogleServiceAuthError
& error
));
213 MOCK_METHOD1(OnClientOAuthFailure
,
214 void(const GoogleServiceAuthError
& error
));
215 MOCK_METHOD1(OnMergeSessionFailure
, void(
216 const GoogleServiceAuthError
& error
));
217 MOCK_METHOD1(OnUberAuthTokenFailure
, void(
218 const GoogleServiceAuthError
& error
));
222 #define MAYBE_ErrorComparator DISABLED_ErrorComparator
224 #define MAYBE_ErrorComparator ErrorComparator
227 TEST_F(GaiaAuthFetcherTest
, MAYBE_ErrorComparator
) {
228 GoogleServiceAuthError expected_error
=
229 GoogleServiceAuthError::FromConnectionError(-101);
231 GoogleServiceAuthError matching_error
=
232 GoogleServiceAuthError::FromConnectionError(-101);
234 EXPECT_TRUE(expected_error
== matching_error
);
236 expected_error
= GoogleServiceAuthError::FromConnectionError(6);
238 EXPECT_FALSE(expected_error
== matching_error
);
240 expected_error
= GoogleServiceAuthError(GoogleServiceAuthError::NONE
);
242 EXPECT_FALSE(expected_error
== matching_error
);
244 matching_error
= GoogleServiceAuthError(GoogleServiceAuthError::NONE
);
246 EXPECT_TRUE(expected_error
== matching_error
);
249 TEST_F(GaiaAuthFetcherTest
, LoginNetFailure
) {
250 int error_no
= net::ERR_CONNECTION_RESET
;
251 net::URLRequestStatus
status(net::URLRequestStatus::FAILED
, error_no
);
253 GoogleServiceAuthError expected_error
=
254 GoogleServiceAuthError::FromConnectionError(error_no
);
256 MockGaiaConsumer consumer
;
257 EXPECT_CALL(consumer
, OnClientLoginFailure(expected_error
))
260 GaiaAuthFetcher
auth(&consumer
, std::string(),
261 profile_
.GetRequestContext());
263 MockFetcher
mock_fetcher(
264 client_login_source_
, status
, 0, net::ResponseCookies(), std::string(),
265 net::URLFetcher::GET
, &auth
);
266 auth
.OnURLFetchComplete(&mock_fetcher
);
269 TEST_F(GaiaAuthFetcherTest
, TokenNetFailure
) {
270 int error_no
= net::ERR_CONNECTION_RESET
;
271 net::URLRequestStatus
status(net::URLRequestStatus::FAILED
, error_no
);
273 GoogleServiceAuthError expected_error
=
274 GoogleServiceAuthError::FromConnectionError(error_no
);
276 MockGaiaConsumer consumer
;
277 EXPECT_CALL(consumer
, OnIssueAuthTokenFailure(_
, expected_error
))
280 GaiaAuthFetcher
auth(&consumer
, std::string(),
281 profile_
.GetRequestContext());
283 MockFetcher
mock_fetcher(
284 issue_auth_token_source_
, status
, 0, cookies_
, std::string(),
285 net::URLFetcher::GET
, &auth
);
286 auth
.OnURLFetchComplete(&mock_fetcher
);
290 TEST_F(GaiaAuthFetcherTest
, LoginDenied
) {
291 std::string
data("Error=BadAuthentication");
292 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
294 GoogleServiceAuthError
expected_error(
295 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
297 MockGaiaConsumer consumer
;
298 EXPECT_CALL(consumer
, OnClientLoginFailure(expected_error
))
301 GaiaAuthFetcher
auth(&consumer
, std::string(),
302 profile_
.GetRequestContext());
304 MockFetcher
mock_fetcher(
305 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, data
,
306 net::URLFetcher::GET
, &auth
);
307 auth
.OnURLFetchComplete(&mock_fetcher
);
310 TEST_F(GaiaAuthFetcherTest
, ParseRequest
) {
311 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth");
312 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth");
313 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth");
314 RunParsingTest("SID=sid\nAuth=auth\n", "sid", std::string(), "auth");
315 RunParsingTest("LSID=lsid\nAuth=auth\n", std::string(), "lsid", "auth");
316 RunParsingTest("\nAuth=auth\n", std::string(), std::string(), "auth");
317 RunParsingTest("SID=sid", "sid", std::string(), std::string());
320 TEST_F(GaiaAuthFetcherTest
, ParseErrorRequest
) {
321 RunErrorParsingTest("Url=U\n"
324 "CaptchaUrl=C\n", "E", "U", "C", "T");
325 RunErrorParsingTest("CaptchaToken=T\n"
328 "CaptchaUrl=C\n", "E", "U", "C", "T");
329 RunErrorParsingTest("\n\n\nCaptchaToken=T\n"
332 "CaptchaUrl=C\n", "E", "U", "C", "T");
336 TEST_F(GaiaAuthFetcherTest
, OnlineLogin
) {
337 std::string
data("SID=sid\nLSID=lsid\nAuth=auth\n");
339 GaiaAuthConsumer::ClientLoginResult result
;
340 result
.lsid
= "lsid";
342 result
.token
= "auth";
345 MockGaiaConsumer consumer
;
346 EXPECT_CALL(consumer
, OnClientLoginSuccess(result
))
349 GaiaAuthFetcher
auth(&consumer
, std::string(),
350 profile_
.GetRequestContext());
351 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
352 MockFetcher
mock_fetcher(
353 client_login_source_
, status
, net::HTTP_OK
, cookies_
, data
,
354 net::URLFetcher::GET
, &auth
);
355 auth
.OnURLFetchComplete(&mock_fetcher
);
358 TEST_F(GaiaAuthFetcherTest
, WorkingIssueAuthToken
) {
359 MockGaiaConsumer consumer
;
360 EXPECT_CALL(consumer
, OnIssueAuthTokenSuccess(_
, "token"))
363 GaiaAuthFetcher
auth(&consumer
, std::string(),
364 profile_
.GetRequestContext());
365 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
366 MockFetcher
mock_fetcher(
367 issue_auth_token_source_
, status
, net::HTTP_OK
, cookies_
, "token",
368 net::URLFetcher::GET
, &auth
);
369 auth
.OnURLFetchComplete(&mock_fetcher
);
372 TEST_F(GaiaAuthFetcherTest
, CheckTwoFactorResponse
) {
373 std::string response
=
374 base::StringPrintf("Error=BadAuthentication\n%s\n",
375 GaiaAuthFetcher::kSecondFactor
);
376 EXPECT_TRUE(GaiaAuthFetcher::IsSecondFactorSuccess(response
));
379 TEST_F(GaiaAuthFetcherTest
, CheckNormalErrorCode
) {
380 std::string response
= "Error=BadAuthentication\n";
381 EXPECT_FALSE(GaiaAuthFetcher::IsSecondFactorSuccess(response
));
384 TEST_F(GaiaAuthFetcherTest
, TwoFactorLogin
) {
385 std::string response
= base::StringPrintf("Error=BadAuthentication\n%s\n",
386 GaiaAuthFetcher::kSecondFactor
);
388 GoogleServiceAuthError error
=
389 GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR
);
391 MockGaiaConsumer consumer
;
392 EXPECT_CALL(consumer
, OnClientLoginFailure(error
))
395 GaiaAuthFetcher
auth(&consumer
, std::string(),
396 profile_
.GetRequestContext());
397 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
398 MockFetcher
mock_fetcher(
399 client_login_source_
, status
, net::HTTP_FORBIDDEN
, cookies_
, response
,
400 net::URLFetcher::GET
, &auth
);
401 auth
.OnURLFetchComplete(&mock_fetcher
);
404 TEST_F(GaiaAuthFetcherTest
, CaptchaParse
) {
405 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
406 std::string data
= "Url=http://www.google.com/login/captcha\n"
407 "Error=CaptchaRequired\n"
408 "CaptchaToken=CCTOKEN\n"
409 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n";
410 GoogleServiceAuthError error
=
411 GaiaAuthFetcher::GenerateAuthError(data
, status
);
413 std::string token
= "CCTOKEN";
414 GURL
image_url("http://accounts.google.com/Captcha?ctoken=CCTOKEN");
415 GURL
unlock_url("http://www.google.com/login/captcha");
417 EXPECT_EQ(error
.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED
);
418 EXPECT_EQ(error
.captcha().token
, token
);
419 EXPECT_EQ(error
.captcha().image_url
, image_url
);
420 EXPECT_EQ(error
.captcha().unlock_url
, unlock_url
);
423 TEST_F(GaiaAuthFetcherTest
, AccountDeletedError
) {
424 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
425 std::string data
= "Error=AccountDeleted\n";
426 GoogleServiceAuthError error
=
427 GaiaAuthFetcher::GenerateAuthError(data
, status
);
428 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DELETED
);
431 TEST_F(GaiaAuthFetcherTest
, AccountDisabledError
) {
432 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
433 std::string data
= "Error=AccountDisabled\n";
434 GoogleServiceAuthError error
=
435 GaiaAuthFetcher::GenerateAuthError(data
, status
);
436 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DISABLED
);
439 TEST_F(GaiaAuthFetcherTest
,BadAuthenticationError
) {
440 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
441 std::string data
= "Error=BadAuthentication\n";
442 GoogleServiceAuthError error
=
443 GaiaAuthFetcher::GenerateAuthError(data
, status
);
444 EXPECT_EQ(error
.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
447 TEST_F(GaiaAuthFetcherTest
,IncomprehensibleError
) {
448 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
449 std::string data
= "Error=Gobbledygook\n";
450 GoogleServiceAuthError error
=
451 GaiaAuthFetcher::GenerateAuthError(data
, status
);
452 EXPECT_EQ(error
.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
455 TEST_F(GaiaAuthFetcherTest
,ServiceUnavailableError
) {
456 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
457 std::string data
= "Error=ServiceUnavailable\n";
458 GoogleServiceAuthError error
=
459 GaiaAuthFetcher::GenerateOAuthLoginError(data
, status
);
460 EXPECT_EQ(error
.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
463 TEST_F(GaiaAuthFetcherTest
, OAuthAccountDeletedError
) {
464 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
465 std::string data
= "Error=adel\n";
466 GoogleServiceAuthError error
=
467 GaiaAuthFetcher::GenerateOAuthLoginError(data
, status
);
468 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DELETED
);
471 TEST_F(GaiaAuthFetcherTest
, OAuthAccountDisabledError
) {
472 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
473 std::string data
= "Error=adis\n";
474 GoogleServiceAuthError error
=
475 GaiaAuthFetcher::GenerateOAuthLoginError(data
, status
);
476 EXPECT_EQ(error
.state(), GoogleServiceAuthError::ACCOUNT_DISABLED
);
479 TEST_F(GaiaAuthFetcherTest
, OAuthBadAuthenticationError
) {
480 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
481 std::string data
= "Error=badauth\n";
482 GoogleServiceAuthError error
=
483 GaiaAuthFetcher::GenerateOAuthLoginError(data
, status
);
484 EXPECT_EQ(error
.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
487 TEST_F(GaiaAuthFetcherTest
, OAuthServiceUnavailableError
) {
488 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
489 std::string data
= "Error=ire\n";
490 GoogleServiceAuthError error
=
491 GaiaAuthFetcher::GenerateOAuthLoginError(data
, status
);
492 EXPECT_EQ(error
.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
495 TEST_F(GaiaAuthFetcherTest
, FullLogin
) {
496 MockGaiaConsumer consumer
;
497 EXPECT_CALL(consumer
, OnClientLoginSuccess(_
))
500 MockURLFetcherFactory
<MockFetcher
> factory
;
502 GaiaAuthFetcher
auth(&consumer
, std::string(),
503 profile_
.GetRequestContext());
504 auth
.StartClientLogin("username",
509 GaiaAuthFetcher::HostedAccountsAllowed
);
512 TEST_F(GaiaAuthFetcherTest
, FullLoginFailure
) {
513 MockGaiaConsumer consumer
;
514 EXPECT_CALL(consumer
, OnClientLoginFailure(_
))
517 MockURLFetcherFactory
<MockFetcher
> factory
;
518 factory
.set_success(false);
520 GaiaAuthFetcher
auth(&consumer
, std::string(),
521 profile_
.GetRequestContext());
522 auth
.StartClientLogin("username",
527 GaiaAuthFetcher::HostedAccountsAllowed
);
530 TEST_F(GaiaAuthFetcherTest
, ClientFetchPending
) {
531 MockGaiaConsumer consumer
;
532 EXPECT_CALL(consumer
, OnClientLoginSuccess(_
))
535 net::TestURLFetcherFactory factory
;
537 GaiaAuthFetcher
auth(&consumer
, std::string(),
538 profile_
.GetRequestContext());
539 auth
.StartClientLogin("username",
544 GaiaAuthFetcher::HostedAccountsAllowed
);
546 EXPECT_TRUE(auth
.HasPendingFetch());
547 MockFetcher
mock_fetcher(
548 client_login_source_
,
549 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
550 net::HTTP_OK
, cookies_
, "SID=sid\nLSID=lsid\nAuth=auth\n",
551 net::URLFetcher::GET
, &auth
);
552 auth
.OnURLFetchComplete(&mock_fetcher
);
553 EXPECT_FALSE(auth
.HasPendingFetch());
556 TEST_F(GaiaAuthFetcherTest
, FullTokenSuccess
) {
557 MockGaiaConsumer consumer
;
558 EXPECT_CALL(consumer
, OnIssueAuthTokenSuccess("service", "token"))
561 net::TestURLFetcherFactory factory
;
562 GaiaAuthFetcher
auth(&consumer
, std::string(),
563 profile_
.GetRequestContext());
564 auth
.StartIssueAuthToken("sid", "lsid", "service");
566 EXPECT_TRUE(auth
.HasPendingFetch());
567 MockFetcher
mock_fetcher(
568 issue_auth_token_source_
,
569 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
570 net::HTTP_OK
, cookies_
, "token",
571 net::URLFetcher::GET
, &auth
);
572 auth
.OnURLFetchComplete(&mock_fetcher
);
573 EXPECT_FALSE(auth
.HasPendingFetch());
576 TEST_F(GaiaAuthFetcherTest
, FullTokenFailure
) {
577 MockGaiaConsumer consumer
;
578 EXPECT_CALL(consumer
, OnIssueAuthTokenFailure("service", _
))
581 net::TestURLFetcherFactory factory
;
583 GaiaAuthFetcher
auth(&consumer
, std::string(),
584 profile_
.GetRequestContext());
585 auth
.StartIssueAuthToken("sid", "lsid", "service");
587 EXPECT_TRUE(auth
.HasPendingFetch());
588 MockFetcher
mock_fetcher(
589 issue_auth_token_source_
,
590 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
594 net::URLFetcher::GET
,
596 auth
.OnURLFetchComplete(&mock_fetcher
);
597 EXPECT_FALSE(auth
.HasPendingFetch());
600 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenSuccess
) {
601 MockGaiaConsumer consumer
;
602 EXPECT_CALL(consumer
, OnClientOAuthSuccess(
603 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
605 net::TestURLFetcherFactory factory
;
606 GaiaAuthFetcher
auth(&consumer
, std::string(),
607 profile_
.GetRequestContext());
608 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
609 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
610 EXPECT_TRUE(NULL
!= fetcher
);
611 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
,
612 fetcher
->GetLoadFlags());
614 net::ResponseCookies cookies
;
615 cookies
.push_back(kGetAuthCodeValidCookie
);
616 EXPECT_TRUE(auth
.HasPendingFetch());
617 MockFetcher
mock_fetcher1(
618 client_login_to_oauth2_source_
,
619 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
623 net::URLFetcher::POST
,
625 auth
.OnURLFetchComplete(&mock_fetcher1
);
626 EXPECT_TRUE(auth
.HasPendingFetch());
627 MockFetcher
mock_fetcher2(
628 oauth2_token_source_
,
629 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
630 net::HTTP_OK
, cookies_
, kGetTokenPairValidResponse
,
631 net::URLFetcher::POST
, &auth
);
632 auth
.OnURLFetchComplete(&mock_fetcher2
);
633 EXPECT_FALSE(auth
.HasPendingFetch());
636 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenWithCookies
) {
637 MockGaiaConsumer consumer
;
638 net::TestURLFetcherFactory factory
;
639 GaiaAuthFetcher
auth(&consumer
, std::string(),
640 profile_
.GetRequestContext());
641 auth
.StartCookieForOAuthLoginTokenExchange("0");
642 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
643 EXPECT_TRUE(NULL
!= fetcher
);
644 EXPECT_EQ(net::LOAD_NORMAL
, fetcher
->GetLoadFlags());
647 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenClientLoginToOAuth2Failure
) {
648 MockGaiaConsumer consumer
;
649 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
652 net::TestURLFetcherFactory factory
;
653 GaiaAuthFetcher
auth(&consumer
, std::string(),
654 profile_
.GetRequestContext());
655 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
657 net::ResponseCookies cookies
;
658 EXPECT_TRUE(auth
.HasPendingFetch());
659 MockFetcher
mock_fetcher(
660 client_login_to_oauth2_source_
,
661 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
665 net::URLFetcher::POST
,
667 auth
.OnURLFetchComplete(&mock_fetcher
);
668 EXPECT_FALSE(auth
.HasPendingFetch());
671 TEST_F(GaiaAuthFetcherTest
, OAuthLoginTokenOAuth2TokenPairFailure
) {
672 MockGaiaConsumer consumer
;
673 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
676 net::TestURLFetcherFactory factory
;
677 GaiaAuthFetcher
auth(&consumer
, std::string(),
678 profile_
.GetRequestContext());
679 auth
.StartLsoForOAuthLoginTokenExchange("lso_token");
681 net::ResponseCookies cookies
;
682 cookies
.push_back(kGetAuthCodeValidCookie
);
683 EXPECT_TRUE(auth
.HasPendingFetch());
684 MockFetcher
mock_fetcher1(
685 client_login_to_oauth2_source_
,
686 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
690 net::URLFetcher::POST
,
692 auth
.OnURLFetchComplete(&mock_fetcher1
);
693 EXPECT_TRUE(auth
.HasPendingFetch());
694 MockFetcher
mock_fetcher2(
695 oauth2_token_source_
,
696 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
700 net::URLFetcher::POST
,
702 auth
.OnURLFetchComplete(&mock_fetcher2
);
703 EXPECT_FALSE(auth
.HasPendingFetch());
706 TEST_F(GaiaAuthFetcherTest
, MergeSessionSuccess
) {
707 MockGaiaConsumer consumer
;
708 EXPECT_CALL(consumer
, OnMergeSessionSuccess("<html></html>"))
711 net::TestURLFetcherFactory factory
;
713 GaiaAuthFetcher
auth(&consumer
, std::string(),
714 profile_
.GetRequestContext());
715 auth
.StartMergeSession("myubertoken");
717 EXPECT_TRUE(auth
.HasPendingFetch());
718 MockFetcher
mock_fetcher(
719 merge_session_source_
,
720 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
721 net::HTTP_OK
, cookies_
, "<html></html>", net::URLFetcher::GET
,
723 auth
.OnURLFetchComplete(&mock_fetcher
);
724 EXPECT_FALSE(auth
.HasPendingFetch());
727 TEST_F(GaiaAuthFetcherTest
, MergeSessionSuccessRedirect
) {
728 MockGaiaConsumer consumer
;
729 EXPECT_CALL(consumer
, OnMergeSessionSuccess("<html></html>"))
732 net::TestURLFetcherFactory factory
;
734 GaiaAuthFetcher
auth(&consumer
, std::string(),
735 profile_
.GetRequestContext());
736 auth
.StartMergeSession("myubertoken");
738 // Make sure the fetcher created has the expected flags. Set its url()
739 // properties to reflect a redirect.
740 net::TestURLFetcher
* test_fetcher
= factory
.GetFetcherByID(0);
741 EXPECT_TRUE(test_fetcher
!= NULL
);
742 EXPECT_TRUE(test_fetcher
->GetLoadFlags() == net::LOAD_NORMAL
);
743 EXPECT_TRUE(auth
.HasPendingFetch());
745 GURL
final_url("http://www.google.com/CheckCookie");
746 test_fetcher
->set_url(final_url
);
747 test_fetcher
->set_status(
748 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
749 test_fetcher
->set_response_code(net::HTTP_OK
);
750 test_fetcher
->set_cookies(cookies_
);
751 test_fetcher
->SetResponseString("<html></html>");
753 auth
.OnURLFetchComplete(test_fetcher
);
754 EXPECT_FALSE(auth
.HasPendingFetch());
757 TEST_F(GaiaAuthFetcherTest
, UberAuthTokenSuccess
) {
758 MockGaiaConsumer consumer
;
759 EXPECT_CALL(consumer
, OnUberAuthTokenSuccess("uberToken"))
762 net::TestURLFetcherFactory factory
;
764 GaiaAuthFetcher
auth(&consumer
, std::string(),
765 profile_
.GetRequestContext());
766 auth
.StartTokenFetchForUberAuthExchange("myAccessToken");
768 EXPECT_TRUE(auth
.HasPendingFetch());
769 MockFetcher
mock_fetcher(
770 uberauth_token_source_
,
771 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0),
772 net::HTTP_OK
, cookies_
, "uberToken", net::URLFetcher::POST
,
774 auth
.OnURLFetchComplete(&mock_fetcher
);
775 EXPECT_FALSE(auth
.HasPendingFetch());
778 TEST_F(GaiaAuthFetcherTest
, ParseClientLoginToOAuth2Response
) {
780 std::string auth_code
;
781 net::ResponseCookies cookies
;
782 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
783 cookies
, &auth_code
));
784 EXPECT_EQ("", auth_code
);
786 { // Few cookies, nothing appropriate.
787 std::string auth_code
;
788 net::ResponseCookies cookies
;
789 cookies
.push_back(kGetAuthCodeCookieNoSecure
);
790 cookies
.push_back(kGetAuthCodeCookieNoHttpOnly
);
791 cookies
.push_back(kGetAuthCodeCookieNoOAuthCode
);
792 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
793 cookies
, &auth_code
));
794 EXPECT_EQ("", auth_code
);
796 { // Few cookies, one of them is valid.
797 std::string auth_code
;
798 net::ResponseCookies cookies
;
799 cookies
.push_back(kGetAuthCodeCookieNoSecure
);
800 cookies
.push_back(kGetAuthCodeCookieNoHttpOnly
);
801 cookies
.push_back(kGetAuthCodeCookieNoOAuthCode
);
802 cookies
.push_back(kGetAuthCodeValidCookie
);
803 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
804 cookies
, &auth_code
));
805 EXPECT_EQ("test-code", auth_code
);
807 { // Single valid cookie (like in real responses).
808 std::string auth_code
;
809 net::ResponseCookies cookies
;
810 cookies
.push_back(kGetAuthCodeValidCookie
);
811 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
812 cookies
, &auth_code
));
813 EXPECT_EQ("test-code", auth_code
);
817 TEST_F(GaiaAuthFetcherTest
, ClientOAuthSuccess
) {
818 MockURLFetcherFactory
<MockFetcher
> factory
;
819 factory
.set_results(kClientOAuthValidResponse
);
821 MockGaiaConsumer consumer
;
822 EXPECT_CALL(consumer
, OnClientOAuthSuccess(
823 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
825 GaiaAuthFetcher
auth(&consumer
, "tests", profile_
.GetRequestContext());
826 std::vector
<std::string
> scopes
;
827 scopes
.push_back(GaiaUrls::GetInstance()->oauth1_login_scope());
828 scopes
.push_back("https://some.other.scope.com");
829 auth
.StartClientOAuth("username", "password", scopes
, std::string(), "en");
831 std::string expected_text
= base::StringPrintf(
833 "\"email\": \"username\","
834 "\"password\": \"password\","
835 "\"scopes\": [\"https://www.google.com/accounts/OAuthLogin\","
836 " \"https://some.other.scope.com\"],"
837 "\"oauth2_client_id\": \"%s\","
838 "\"friendly_device_name\": \"tests\","
839 "\"accepts_challenges\": [\"Captcha\", \"TwoStep\"],"
840 "\"locale\": \"en\","
841 "\"fallback\": { \"name\": \"GetOAuth2Token\" }"
843 google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN
).c_str());
845 scoped_ptr
<base::Value
> actual(base::JSONReader::Read(auth
.request_body_
));
846 scoped_ptr
<base::Value
> expected(base::JSONReader::Read(expected_text
));
847 EXPECT_TRUE(expected
->Equals(actual
.get()));
850 TEST_F(GaiaAuthFetcherTest
, ClientOAuthWithQuote
) {
851 MockURLFetcherFactory
<MockFetcher
> factory
;
852 factory
.set_results(kClientOAuthValidResponse
);
854 MockGaiaConsumer consumer
;
855 EXPECT_CALL(consumer
, OnClientOAuthSuccess(
856 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
858 GaiaAuthFetcher
auth(&consumer
, "te\"sts", profile_
.GetRequestContext());
859 std::vector
<std::string
> scopes
;
860 scopes
.push_back("https://some.\"other.scope.com");
861 auth
.StartClientOAuth(
862 "user\"name", "pass\"word", scopes
, std::string(), "e\"n");
864 std::string expected_text
= base::StringPrintf(
866 "\"email\": \"user\\\"name\","
867 "\"password\": \"pass\\\"word\","
868 "\"scopes\": [\"https://some.\\\"other.scope.com\"],"
869 "\"oauth2_client_id\": \"%s\","
870 "\"friendly_device_name\": \"te\\\"sts\","
871 "\"accepts_challenges\": [\"Captcha\", \"TwoStep\"],"
872 "\"locale\": \"e\\\"n\","
873 "\"fallback\": { \"name\": \"GetOAuth2Token\" }"
875 google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN
).c_str());
876 scoped_ptr
<base::Value
> actual(base::JSONReader::Read(auth
.request_body_
));
877 scoped_ptr
<base::Value
> expected(base::JSONReader::Read(expected_text
));
878 EXPECT_TRUE(expected
->Equals(actual
.get()));
881 TEST_F(GaiaAuthFetcherTest
, ClientOAuthBadAuth
) {
882 MockURLFetcherFactory
<MockFetcher
> factory
;
883 factory
.set_success(false);
884 factory
.set_results("{"
885 " \"cause\" : \"BadAuthentication\","
887 " \"name\" : \"Terminating\","
888 " \"url\" : \"https://www.terminating.com\""
892 MockGaiaConsumer consumer
;
893 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
894 .WillOnce(Invoke(ExpectBadAuth
));
896 GaiaAuthFetcher
auth(&consumer
, "tests", profile_
.GetRequestContext());
897 std::vector
<std::string
> scopes
;
898 scopes
.push_back(GaiaUrls::GetInstance()->oauth1_login_scope());
899 auth
.StartClientOAuth("username", "password", scopes
, std::string(), "en");
902 TEST_F(GaiaAuthFetcherTest
, ClientOAuthCaptchaChallenge
) {
903 MockURLFetcherFactory
<MockFetcher
> factory
;
904 factory
.set_success(false);
905 factory
.set_results("{"
906 " \"cause\" : \"NeedsAdditional\","
908 " \"name\" : \"Terminating\","
909 " \"url\" : \"https://www.terminating.com\""
912 " \"name\" : \"Captcha\","
913 " \"image_url\" : \"http://www.image.com/\","
914 " \"image_width\" : 640,"
915 " \"image_height\" : 480,"
916 " \"audio_url\" : \"http://www.audio.com/\","
917 " \"challenge_token\" : \"challengetokenblob\""
921 MockGaiaConsumer consumer
;
922 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
923 .WillOnce(Invoke(ExpectCaptchaChallenge
));
925 GaiaAuthFetcher
auth(&consumer
, "tests", profile_
.GetRequestContext());
926 std::vector
<std::string
> scopes
;
927 scopes
.push_back(GaiaUrls::GetInstance()->oauth1_login_scope());
928 auth
.StartClientOAuth("username", "password", scopes
, std::string(), "en");
931 TEST_F(GaiaAuthFetcherTest
, ClientOAuthTwoFactorChallenge
) {
932 MockURLFetcherFactory
<MockFetcher
> factory
;
933 factory
.set_success(false);
934 factory
.set_results("{"
935 " \"cause\" : \"NeedsAdditional\","
937 " \"name\" : \"Terminating\","
938 " \"url\" : \"https://www.terminating.com\""
941 " \"name\" : \"TwoStep\","
942 " \"prompt_text\" : \"prompt_text\","
943 " \"alternate_text\" : \"alternate_text\","
944 " \"challenge_token\" : \"challengetokenblob\","
945 " \"field_length\" : 10"
949 MockGaiaConsumer consumer
;
950 EXPECT_CALL(consumer
, OnClientOAuthFailure(_
))
951 .WillOnce(Invoke(ExpectTwoFactorChallenge
));
953 GaiaAuthFetcher
auth(&consumer
, "tests", profile_
.GetRequestContext());
954 std::vector
<std::string
> scopes
;
955 scopes
.push_back(GaiaUrls::GetInstance()->oauth1_login_scope());
956 auth
.StartClientOAuth("username", "password", scopes
, std::string(), "en");
959 TEST_F(GaiaAuthFetcherTest
, ClientOAuthChallengeSuccess
) {
960 MockURLFetcherFactory
<MockFetcher
> factory
;
961 factory
.set_results(kClientOAuthValidResponse
);
963 MockGaiaConsumer consumer
;
964 EXPECT_CALL(consumer
, OnClientOAuthSuccess(
965 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(2);
967 GaiaAuthFetcher
auth1(&consumer
, std::string(), profile_
.GetRequestContext());
968 auth1
.StartClientOAuthChallengeResponse(GoogleServiceAuthError::TWO_FACTOR
,
969 "token", "mysolution");
971 scoped_ptr
<base::Value
> actual1(base::JSONReader::Read(auth1
.request_body_
));
972 scoped_ptr
<base::Value
> expected1(base::JSONReader::Read(
974 " \"challenge_reply\" : {"
975 " \"name\" : \"TwoStep\","
976 " \"challenge_token\" : \"token\","
977 " \"otp\" : \"mysolution\""
980 EXPECT_TRUE(expected1
->Equals(actual1
.get()));
982 GaiaAuthFetcher
auth2(&consumer
, "tests", profile_
.GetRequestContext());
983 auth2
.StartClientOAuthChallengeResponse(
984 GoogleServiceAuthError::CAPTCHA_REQUIRED
, "token", "mysolution");
986 scoped_ptr
<base::Value
> actual2(base::JSONReader::Read(auth2
.request_body_
));
987 scoped_ptr
<base::Value
> expected2(base::JSONReader::Read(
989 " \"challenge_reply\" : {"
990 " \"name\" : \"Captcha\","
991 " \"challenge_token\" : \"token\","
992 " \"solution\" : \"mysolution\""
995 EXPECT_TRUE(expected2
->Equals(actual2
.get()));
998 TEST_F(GaiaAuthFetcherTest
, ClientOAuthChallengeQuote
) {
999 MockURLFetcherFactory
<MockFetcher
> factory
;
1000 factory
.set_results(kClientOAuthValidResponse
);
1002 MockGaiaConsumer consumer
;
1003 EXPECT_CALL(consumer
, OnClientOAuthSuccess(
1004 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
1006 GaiaAuthFetcher
auth(&consumer
, std::string(), profile_
.GetRequestContext());
1007 auth
.StartClientOAuthChallengeResponse(GoogleServiceAuthError::TWO_FACTOR
,
1008 "to\"ken", "my\"solution");
1010 scoped_ptr
<base::Value
> actual(base::JSONReader::Read(auth
.request_body_
));
1011 scoped_ptr
<base::Value
> expected(base::JSONReader::Read(
1013 " \"challenge_reply\" : {"
1014 " \"name\" : \"TwoStep\","
1015 " \"challenge_token\" : \"to\\\"ken\","
1016 " \"otp\" : \"my\\\"solution\""
1019 EXPECT_TRUE(expected
->Equals(actual
.get()));
1022 TEST_F(GaiaAuthFetcherTest
, StartOAuthLogin
) {
1023 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA
1025 std::string
data("SID=sid\nLSID=lsid\nAuth=auth\n");
1027 GaiaAuthConsumer::ClientLoginResult result
;
1028 result
.lsid
= "lsid";
1030 result
.token
= "auth";
1033 MockGaiaConsumer consumer
;
1034 EXPECT_CALL(consumer
, OnClientLoginSuccess(result
))
1037 GaiaAuthFetcher
auth(&consumer
, std::string(),
1038 profile_
.GetRequestContext());
1039 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
1040 MockFetcher
mock_fetcher(
1041 oauth_login_gurl_
, status
, net::HTTP_OK
, cookies_
, data
,
1042 net::URLFetcher::GET
, &auth
);
1043 auth
.OnURLFetchComplete(&mock_fetcher
);