Revert of Merge duplicate code in invalidation helper. (patchset #2 id:40001 of https...
[chromium-blink-merge.git] / google_apis / gaia / gaia_auth_fetcher_unittest.cc
blob307a2bcec2b99eafe9ea4d57480e69eb3dbb8fc0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // A complete set of unit tests for GaiaAuthFetcher.
6 // Originally ported from GoogleAuthenticator tests.
8 #include <string>
10 #include "base/json/json_reader.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "google_apis/gaia/gaia_auth_consumer.h"
14 #include "google_apis/gaia/gaia_auth_fetcher.h"
15 #include "google_apis/gaia/gaia_urls.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "google_apis/gaia/mock_url_fetcher_factory.h"
18 #include "google_apis/google_api_keys.h"
19 #include "net/base/load_flags.h"
20 #include "net/base/net_errors.h"
21 #include "net/http/http_status_code.h"
22 #include "net/url_request/test_url_fetcher_factory.h"
23 #include "net/url_request/url_fetcher_delegate.h"
24 #include "net/url_request/url_request_status.h"
25 #include "net/url_request/url_request_test_util.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "url/gurl.h"
30 using ::testing::Invoke;
31 using ::testing::_;
33 const char kGetAuthCodeValidCookie[] =
34 "oauth_code=test-code; Path=/test; Secure; HttpOnly";
35 const char kGetAuthCodeCookieNoSecure[] =
36 "oauth_code=test-code; Path=/test; HttpOnly";
37 const char kGetAuthCodeCookieNoHttpOnly[] =
38 "oauth_code=test-code; Path=/test; Secure";
39 const char kGetAuthCodeCookieNoOAuthCode[] =
40 "Path=/test; Secure; HttpOnly";
41 const char kGetTokenPairValidResponse[] =
42 "{"
43 " \"refresh_token\": \"rt1\","
44 " \"access_token\": \"at1\","
45 " \"expires_in\": 3600,"
46 " \"token_type\": \"Bearer\""
47 "}";
49 MockFetcher::MockFetcher(bool success,
50 const GURL& url,
51 const std::string& results,
52 net::URLFetcher::RequestType request_type,
53 net::URLFetcherDelegate* d)
54 : TestURLFetcher(0, url, d) {
55 set_url(url);
56 net::Error error;
58 if (success) {
59 error = net::OK;
60 set_response_code(net::HTTP_OK);
61 } else {
62 error = net::ERR_FAILED;
65 set_status(net::URLRequestStatus::FromError(error));
66 SetResponseString(results);
69 MockFetcher::MockFetcher(const GURL& url,
70 const net::URLRequestStatus& status,
71 int response_code,
72 const net::ResponseCookies& cookies,
73 const std::string& results,
74 net::URLFetcher::RequestType request_type,
75 net::URLFetcherDelegate* d)
76 : TestURLFetcher(0, url, d) {
77 set_url(url);
78 set_status(status);
79 set_response_code(response_code);
80 set_cookies(cookies);
81 SetResponseString(results);
84 MockFetcher::~MockFetcher() {}
86 void MockFetcher::Start() {
87 delegate()->OnURLFetchComplete(this);
90 class GaiaAuthFetcherTest : public testing::Test {
91 protected:
92 GaiaAuthFetcherTest()
93 : issue_auth_token_source_(GaiaUrls::GetInstance()
94 ->issue_auth_token_url()),
95 client_login_to_oauth2_source_(GaiaUrls::GetInstance()
96 ->client_login_to_oauth2_url()),
97 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()),
98 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()),
99 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()),
100 uberauth_token_source_(GaiaUrls::GetInstance()
101 ->oauth1_login_url()
102 .Resolve("?source=&issueuberauth=1")),
103 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()) {}
105 void RunParsingTest(const std::string& data,
106 const std::string& sid,
107 const std::string& lsid,
108 const std::string& token) {
109 std::string out_sid;
110 std::string out_lsid;
111 std::string out_token;
113 GaiaAuthFetcher::ParseClientLoginResponse(data,
114 &out_sid,
115 &out_lsid,
116 &out_token);
117 EXPECT_EQ(lsid, out_lsid);
118 EXPECT_EQ(sid, out_sid);
119 EXPECT_EQ(token, out_token);
122 void RunErrorParsingTest(const std::string& data,
123 const std::string& error,
124 const std::string& error_url,
125 const std::string& captcha_url,
126 const std::string& captcha_token) {
127 std::string out_error;
128 std::string out_error_url;
129 std::string out_captcha_url;
130 std::string out_captcha_token;
132 GaiaAuthFetcher::ParseClientLoginFailure(data,
133 &out_error,
134 &out_error_url,
135 &out_captcha_url,
136 &out_captcha_token);
137 EXPECT_EQ(error, out_error);
138 EXPECT_EQ(error_url, out_error_url);
139 EXPECT_EQ(captcha_url, out_captcha_url);
140 EXPECT_EQ(captcha_token, out_captcha_token);
143 net::ResponseCookies cookies_;
144 GURL issue_auth_token_source_;
145 GURL client_login_to_oauth2_source_;
146 GURL oauth2_token_source_;
147 GURL token_auth_source_;
148 GURL merge_session_source_;
149 GURL uberauth_token_source_;
150 GURL oauth_login_gurl_;
152 protected:
153 net::TestURLRequestContextGetter* GetRequestContext() {
154 if (!request_context_getter_.get()) {
155 request_context_getter_ = new net::TestURLRequestContextGetter(
156 message_loop_.task_runner());
158 return request_context_getter_.get();
161 base::MessageLoop message_loop_;
162 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
165 class MockGaiaConsumer : public GaiaAuthConsumer {
166 public:
167 MockGaiaConsumer() {}
168 ~MockGaiaConsumer() {}
170 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result));
171 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service,
172 const std::string& token));
173 MOCK_METHOD1(OnClientOAuthSuccess,
174 void(const GaiaAuthConsumer::ClientOAuthResult& result));
175 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data));
176 MOCK_METHOD1(OnUberAuthTokenSuccess, void(const std::string& data));
177 MOCK_METHOD1(OnClientLoginFailure,
178 void(const GoogleServiceAuthError& error));
179 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service,
180 const GoogleServiceAuthError& error));
181 MOCK_METHOD1(OnClientOAuthFailure,
182 void(const GoogleServiceAuthError& error));
183 MOCK_METHOD1(OnMergeSessionFailure, void(
184 const GoogleServiceAuthError& error));
185 MOCK_METHOD1(OnUberAuthTokenFailure, void(
186 const GoogleServiceAuthError& error));
187 MOCK_METHOD1(OnListAccountsSuccess, void(const std::string& data));
188 MOCK_METHOD0(OnLogOutSuccess, void());
189 MOCK_METHOD1(OnLogOutFailure, void(const GoogleServiceAuthError& error));
190 MOCK_METHOD1(OnGetCheckConnectionInfoSuccess, void(const std::string& data));
191 MOCK_METHOD1(OnListIdpSessionsSuccess, void(const std::string& data));
192 MOCK_METHOD1(OnGetTokenResponseSuccess,
193 void(const GaiaAuthConsumer::ClientOAuthResult& result));
196 #if defined(OS_WIN)
197 #define MAYBE_ErrorComparator DISABLED_ErrorComparator
198 #else
199 #define MAYBE_ErrorComparator ErrorComparator
200 #endif
202 TEST_F(GaiaAuthFetcherTest, MAYBE_ErrorComparator) {
203 GoogleServiceAuthError expected_error =
204 GoogleServiceAuthError::FromConnectionError(-101);
206 GoogleServiceAuthError matching_error =
207 GoogleServiceAuthError::FromConnectionError(-101);
209 EXPECT_TRUE(expected_error == matching_error);
211 expected_error = GoogleServiceAuthError::FromConnectionError(6);
213 EXPECT_FALSE(expected_error == matching_error);
215 expected_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE);
217 EXPECT_FALSE(expected_error == matching_error);
219 matching_error = GoogleServiceAuthError(GoogleServiceAuthError::NONE);
221 EXPECT_TRUE(expected_error == matching_error);
224 TEST_F(GaiaAuthFetcherTest, TokenNetFailure) {
225 int error_no = net::ERR_CONNECTION_RESET;
226 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no);
228 GoogleServiceAuthError expected_error =
229 GoogleServiceAuthError::FromConnectionError(error_no);
231 MockGaiaConsumer consumer;
232 EXPECT_CALL(consumer, OnIssueAuthTokenFailure(_, expected_error))
233 .Times(1);
235 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
237 MockFetcher mock_fetcher(
238 issue_auth_token_source_, status, 0, cookies_, std::string(),
239 net::URLFetcher::GET, &auth);
240 auth.OnURLFetchComplete(&mock_fetcher);
244 TEST_F(GaiaAuthFetcherTest, ParseRequest) {
245 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth\n", "sid", "lsid", "auth");
246 RunParsingTest("LSID=lsid\nSID=sid\nAuth=auth\n", "sid", "lsid", "auth");
247 RunParsingTest("SID=sid\nLSID=lsid\nAuth=auth", "sid", "lsid", "auth");
248 RunParsingTest("SID=sid\nAuth=auth\n", "sid", std::string(), "auth");
249 RunParsingTest("LSID=lsid\nAuth=auth\n", std::string(), "lsid", "auth");
250 RunParsingTest("\nAuth=auth\n", std::string(), std::string(), "auth");
251 RunParsingTest("SID=sid", "sid", std::string(), std::string());
254 TEST_F(GaiaAuthFetcherTest, ParseErrorRequest) {
255 RunErrorParsingTest("Url=U\n"
256 "Error=E\n"
257 "CaptchaToken=T\n"
258 "CaptchaUrl=C\n", "E", "U", "C", "T");
259 RunErrorParsingTest("CaptchaToken=T\n"
260 "Error=E\n"
261 "Url=U\n"
262 "CaptchaUrl=C\n", "E", "U", "C", "T");
263 RunErrorParsingTest("\n\n\nCaptchaToken=T\n"
264 "\nError=E\n"
265 "\nUrl=U\n"
266 "CaptchaUrl=C\n", "E", "U", "C", "T");
269 TEST_F(GaiaAuthFetcherTest, WorkingIssueAuthToken) {
270 MockGaiaConsumer consumer;
271 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess(_, "token"))
272 .Times(1);
274 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
275 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
276 MockFetcher mock_fetcher(
277 issue_auth_token_source_, status, net::HTTP_OK, cookies_, "token",
278 net::URLFetcher::GET, &auth);
279 auth.OnURLFetchComplete(&mock_fetcher);
282 TEST_F(GaiaAuthFetcherTest, CheckTwoFactorResponse) {
283 std::string response =
284 base::StringPrintf("Error=BadAuthentication\n%s\n",
285 GaiaAuthFetcher::kSecondFactor);
286 EXPECT_TRUE(GaiaAuthFetcher::IsSecondFactorSuccess(response));
289 TEST_F(GaiaAuthFetcherTest, CheckNormalErrorCode) {
290 std::string response = "Error=BadAuthentication\n";
291 EXPECT_FALSE(GaiaAuthFetcher::IsSecondFactorSuccess(response));
294 TEST_F(GaiaAuthFetcherTest, CaptchaParse) {
295 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
296 std::string data = "Url=http://www.google.com/login/captcha\n"
297 "Error=CaptchaRequired\n"
298 "CaptchaToken=CCTOKEN\n"
299 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n";
300 GoogleServiceAuthError error =
301 GaiaAuthFetcher::GenerateAuthError(data, status);
303 std::string token = "CCTOKEN";
304 GURL image_url("http://accounts.google.com/Captcha?ctoken=CCTOKEN");
305 GURL unlock_url("http://www.google.com/login/captcha");
307 EXPECT_EQ(error.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED);
308 EXPECT_EQ(error.captcha().token, token);
309 EXPECT_EQ(error.captcha().image_url, image_url);
310 EXPECT_EQ(error.captcha().unlock_url, unlock_url);
313 TEST_F(GaiaAuthFetcherTest, AccountDeletedError) {
314 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
315 std::string data = "Error=AccountDeleted\n";
316 GoogleServiceAuthError error =
317 GaiaAuthFetcher::GenerateAuthError(data, status);
318 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DELETED);
321 TEST_F(GaiaAuthFetcherTest, AccountDisabledError) {
322 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
323 std::string data = "Error=AccountDisabled\n";
324 GoogleServiceAuthError error =
325 GaiaAuthFetcher::GenerateAuthError(data, status);
326 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DISABLED);
329 TEST_F(GaiaAuthFetcherTest, BadAuthenticationError) {
330 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
331 std::string data = "Error=BadAuthentication\n";
332 GoogleServiceAuthError error =
333 GaiaAuthFetcher::GenerateAuthError(data, status);
334 EXPECT_EQ(error.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
337 TEST_F(GaiaAuthFetcherTest, IncomprehensibleError) {
338 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
339 std::string data = "Error=Gobbledygook\n";
340 GoogleServiceAuthError error =
341 GaiaAuthFetcher::GenerateAuthError(data, status);
342 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE);
345 TEST_F(GaiaAuthFetcherTest, ServiceUnavailableError) {
346 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
347 std::string data = "Error=ServiceUnavailable\n";
348 GoogleServiceAuthError error =
349 GaiaAuthFetcher::GenerateAuthError(data, status);
350 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE);
353 TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) {
354 MockGaiaConsumer consumer;
355 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token"))
356 .Times(1);
358 net::TestURLFetcherFactory factory;
359 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
360 auth.StartIssueAuthToken("sid", "lsid", "service");
362 EXPECT_TRUE(auth.HasPendingFetch());
363 MockFetcher mock_fetcher(
364 issue_auth_token_source_,
365 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
366 net::HTTP_OK, cookies_, "token",
367 net::URLFetcher::GET, &auth);
368 auth.OnURLFetchComplete(&mock_fetcher);
369 EXPECT_FALSE(auth.HasPendingFetch());
372 TEST_F(GaiaAuthFetcherTest, FullTokenFailure) {
373 MockGaiaConsumer consumer;
374 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _))
375 .Times(1);
377 net::TestURLFetcherFactory factory;
379 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
380 auth.StartIssueAuthToken("sid", "lsid", "service");
382 EXPECT_TRUE(auth.HasPendingFetch());
383 MockFetcher mock_fetcher(
384 issue_auth_token_source_,
385 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
386 net::HTTP_FORBIDDEN,
387 cookies_,
388 std::string(),
389 net::URLFetcher::GET,
390 &auth);
391 auth.OnURLFetchComplete(&mock_fetcher);
392 EXPECT_FALSE(auth.HasPendingFetch());
395 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) {
396 MockGaiaConsumer consumer;
397 EXPECT_CALL(consumer, OnClientOAuthSuccess(
398 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
400 net::TestURLFetcherFactory factory;
401 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
402 auth.StartCookieForOAuthLoginTokenExchange("0");
403 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
404 EXPECT_TRUE(NULL != fetcher);
405 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
406 EXPECT_EQ(std::string::npos,
407 fetcher->GetOriginalURL().query().find("device_type=chrome"));
409 net::ResponseCookies cookies;
410 cookies.push_back(kGetAuthCodeValidCookie);
411 EXPECT_TRUE(auth.HasPendingFetch());
412 MockFetcher mock_fetcher1(
413 client_login_to_oauth2_source_,
414 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
415 net::HTTP_OK,
416 cookies,
417 std::string(),
418 net::URLFetcher::POST,
419 &auth);
420 auth.OnURLFetchComplete(&mock_fetcher1);
421 EXPECT_TRUE(auth.HasPendingFetch());
422 MockFetcher mock_fetcher2(
423 oauth2_token_source_,
424 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
425 net::HTTP_OK, cookies_, kGetTokenPairValidResponse,
426 net::URLFetcher::POST, &auth);
427 auth.OnURLFetchComplete(&mock_fetcher2);
428 EXPECT_FALSE(auth.HasPendingFetch());
431 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies_DeviceId) {
432 MockGaiaConsumer consumer;
433 net::TestURLFetcherFactory factory;
434 std::string expected_device_id("ABCDE-12345");
435 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
436 auth.StartCookieForOAuthLoginTokenExchangeWithDeviceId("0",
437 expected_device_id);
438 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
439 EXPECT_TRUE(NULL != fetcher);
440 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
441 EXPECT_NE(std::string::npos,
442 fetcher->GetOriginalURL().query().find("device_type=chrome"));
443 net::HttpRequestHeaders extra_request_headers;
444 fetcher->GetExtraRequestHeaders(&extra_request_headers);
445 std::string device_id;
446 EXPECT_TRUE(extra_request_headers.GetHeader("X-Device-ID", &device_id));
447 EXPECT_EQ(device_id, expected_device_id);
450 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenClientLoginToOAuth2Failure) {
451 MockGaiaConsumer consumer;
452 EXPECT_CALL(consumer, OnClientOAuthFailure(_))
453 .Times(1);
455 net::TestURLFetcherFactory factory;
456 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
457 auth.StartCookieForOAuthLoginTokenExchange(std::string());
459 net::ResponseCookies cookies;
460 EXPECT_TRUE(auth.HasPendingFetch());
461 MockFetcher mock_fetcher(
462 client_login_to_oauth2_source_,
463 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
464 net::HTTP_FORBIDDEN,
465 cookies,
466 std::string(),
467 net::URLFetcher::POST,
468 &auth);
469 auth.OnURLFetchComplete(&mock_fetcher);
470 EXPECT_FALSE(auth.HasPendingFetch());
473 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenOAuth2TokenPairFailure) {
474 MockGaiaConsumer consumer;
475 EXPECT_CALL(consumer, OnClientOAuthFailure(_))
476 .Times(1);
478 net::TestURLFetcherFactory factory;
479 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
480 auth.StartCookieForOAuthLoginTokenExchange(std::string());
482 net::ResponseCookies cookies;
483 cookies.push_back(kGetAuthCodeValidCookie);
484 EXPECT_TRUE(auth.HasPendingFetch());
485 MockFetcher mock_fetcher1(
486 client_login_to_oauth2_source_,
487 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
488 net::HTTP_OK,
489 cookies,
490 std::string(),
491 net::URLFetcher::POST,
492 &auth);
493 auth.OnURLFetchComplete(&mock_fetcher1);
494 EXPECT_TRUE(auth.HasPendingFetch());
495 MockFetcher mock_fetcher2(
496 oauth2_token_source_,
497 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
498 net::HTTP_FORBIDDEN,
499 cookies_,
500 std::string(),
501 net::URLFetcher::POST,
502 &auth);
503 auth.OnURLFetchComplete(&mock_fetcher2);
504 EXPECT_FALSE(auth.HasPendingFetch());
507 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccess) {
508 MockGaiaConsumer consumer;
509 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>"))
510 .Times(1);
512 net::TestURLFetcherFactory factory;
514 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
515 auth.StartMergeSession("myubertoken", std::string());
517 EXPECT_TRUE(auth.HasPendingFetch());
518 MockFetcher mock_fetcher(
519 merge_session_source_,
520 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
521 net::HTTP_OK, cookies_, "<html></html>", net::URLFetcher::GET,
522 &auth);
523 auth.OnURLFetchComplete(&mock_fetcher);
524 EXPECT_FALSE(auth.HasPendingFetch());
527 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccessRedirect) {
528 MockGaiaConsumer consumer;
529 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>"))
530 .Times(1);
532 net::TestURLFetcherFactory factory;
534 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
535 auth.StartMergeSession("myubertoken", std::string());
537 // Make sure the fetcher created has the expected flags. Set its url()
538 // properties to reflect a redirect.
539 net::TestURLFetcher* test_fetcher = factory.GetFetcherByID(0);
540 EXPECT_TRUE(test_fetcher != NULL);
541 EXPECT_TRUE(test_fetcher->GetLoadFlags() == net::LOAD_NORMAL);
542 EXPECT_TRUE(auth.HasPendingFetch());
544 GURL final_url("http://www.google.com/CheckCookie");
545 test_fetcher->set_url(final_url);
546 test_fetcher->set_status(
547 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
548 test_fetcher->set_response_code(net::HTTP_OK);
549 test_fetcher->set_cookies(cookies_);
550 test_fetcher->SetResponseString("<html></html>");
552 auth.OnURLFetchComplete(test_fetcher);
553 EXPECT_FALSE(auth.HasPendingFetch());
556 TEST_F(GaiaAuthFetcherTest, UberAuthTokenSuccess) {
557 MockGaiaConsumer consumer;
558 EXPECT_CALL(consumer, OnUberAuthTokenSuccess("uberToken"))
559 .Times(1);
561 net::TestURLFetcherFactory factory;
563 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
564 auth.StartTokenFetchForUberAuthExchange("myAccessToken");
566 EXPECT_TRUE(auth.HasPendingFetch());
567 MockFetcher mock_fetcher(
568 uberauth_token_source_,
569 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
570 net::HTTP_OK, cookies_, "uberToken", net::URLFetcher::POST,
571 &auth);
572 auth.OnURLFetchComplete(&mock_fetcher);
573 EXPECT_FALSE(auth.HasPendingFetch());
576 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) {
577 { // No cookies.
578 std::string auth_code;
579 net::ResponseCookies cookies;
580 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
581 cookies, &auth_code));
582 EXPECT_EQ("", auth_code);
584 { // Few cookies, nothing appropriate.
585 std::string auth_code;
586 net::ResponseCookies cookies;
587 cookies.push_back(kGetAuthCodeCookieNoSecure);
588 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
589 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
590 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
591 cookies, &auth_code));
592 EXPECT_EQ("", auth_code);
594 { // Few cookies, one of them is valid.
595 std::string auth_code;
596 net::ResponseCookies cookies;
597 cookies.push_back(kGetAuthCodeCookieNoSecure);
598 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
599 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
600 cookies.push_back(kGetAuthCodeValidCookie);
601 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
602 cookies, &auth_code));
603 EXPECT_EQ("test-code", auth_code);
605 { // Single valid cookie (like in real responses).
606 std::string auth_code;
607 net::ResponseCookies cookies;
608 cookies.push_back(kGetAuthCodeValidCookie);
609 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
610 cookies, &auth_code));
611 EXPECT_EQ("test-code", auth_code);
615 TEST_F(GaiaAuthFetcherTest, StartOAuthLogin) {
616 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA
617 // responses.
618 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n");
620 GaiaAuthConsumer::ClientLoginResult result;
621 result.lsid = "lsid";
622 result.sid = "sid";
623 result.token = "auth";
624 result.data = data;
626 MockGaiaConsumer consumer;
627 EXPECT_CALL(consumer, OnClientLoginSuccess(result))
628 .Times(1);
630 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
631 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
632 MockFetcher mock_fetcher(
633 oauth_login_gurl_, status, net::HTTP_OK, cookies_, data,
634 net::URLFetcher::GET, &auth);
635 auth.OnURLFetchComplete(&mock_fetcher);
638 TEST_F(GaiaAuthFetcherTest, ListAccounts) {
639 std::string data("[\"gaia.l.a.r\", ["
640 "[\"gaia.l.a\", 1, \"First Last\", \"user@gmail.com\", "
641 "\"//googleusercontent.com/A/B/C/D/photo.jpg\", 1, 1, 0]]]");
642 MockGaiaConsumer consumer;
643 EXPECT_CALL(consumer, OnListAccountsSuccess(data)).Times(1);
645 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
646 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
647 MockFetcher mock_fetcher(
648 GaiaUrls::GetInstance()->ListAccountsURLWithSource(std::string()),
649 status, net::HTTP_OK, cookies_, data, net::URLFetcher::GET, &auth);
650 auth.OnURLFetchComplete(&mock_fetcher);
653 TEST_F(GaiaAuthFetcherTest, LogOutSuccess) {
654 MockGaiaConsumer consumer;
655 EXPECT_CALL(consumer, OnLogOutSuccess()).Times(1);
657 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
658 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
659 MockFetcher mock_fetcher(
660 GaiaUrls::GetInstance()->LogOutURLWithSource(std::string()), status,
661 net::HTTP_OK, cookies_, std::string(), net::URLFetcher::GET, &auth);
662 auth.OnURLFetchComplete(&mock_fetcher);
665 TEST_F(GaiaAuthFetcherTest, LogOutFailure) {
666 int error_no = net::ERR_CONNECTION_RESET;
667 net::URLRequestStatus status(net::URLRequestStatus::FAILED, error_no);
669 GoogleServiceAuthError expected_error =
670 GoogleServiceAuthError::FromConnectionError(error_no);
671 MockGaiaConsumer consumer;
672 EXPECT_CALL(consumer, OnLogOutFailure(expected_error)).Times(1);
674 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
676 MockFetcher mock_fetcher(
677 GaiaUrls::GetInstance()->LogOutURLWithSource(std::string()), status, 0,
678 cookies_, std::string(), net::URLFetcher::GET, &auth);
679 auth.OnURLFetchComplete(&mock_fetcher);
682 TEST_F(GaiaAuthFetcherTest, GetCheckConnectionInfo) {
683 std::string data(
684 "[{\"carryBackToken\": \"token1\", \"url\": \"http://www.google.com\"}]");
685 MockGaiaConsumer consumer;
686 EXPECT_CALL(consumer, OnGetCheckConnectionInfoSuccess(data)).Times(1);
688 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
689 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
690 MockFetcher mock_fetcher(
691 GaiaUrls::GetInstance()->GetCheckConnectionInfoURLWithSource(
692 std::string()),
693 status, net::HTTP_OK, cookies_, data, net::URLFetcher::GET, &auth);
694 auth.OnURLFetchComplete(&mock_fetcher);
697 TEST_F(GaiaAuthFetcherTest, ListIDPSessions) {
698 std::string data("{\"sessions\":[{\"login_hint\":\"abcdefghijklmnop\"}]}");
699 MockGaiaConsumer consumer;
700 EXPECT_CALL(consumer, OnListIdpSessionsSuccess("abcdefghijklmnop")).Times(1);
702 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
703 auth.StartListIDPSessions(std::string(), std::string());
705 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
706 MockFetcher mock_fetcher(
707 GaiaUrls::GetInstance()->oauth2_iframe_url(),
708 status, net::HTTP_OK, cookies_, data, net::URLFetcher::GET, &auth);
709 auth.OnURLFetchComplete(&mock_fetcher);
712 TEST_F(GaiaAuthFetcherTest, GetTokenResponse) {
713 MockGaiaConsumer consumer;
714 EXPECT_CALL(consumer,
715 OnGetTokenResponseSuccess(
716 GaiaAuthConsumer::ClientOAuthResult(std::string(),
717 "at1",
718 3600))).Times(1);
720 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
721 auth.StartGetTokenResponse(std::string(), std::string(), std::string());
723 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
724 MockFetcher mock_fetcher(
725 GaiaUrls::GetInstance()->oauth2_iframe_url(),
726 status, net::HTTP_OK, cookies_, kGetTokenPairValidResponse,
727 net::URLFetcher::GET, &auth);
728 auth.OnURLFetchComplete(&mock_fetcher);