Support policy registration using a preobtained access token.
[chromium-blink-merge.git] / google_apis / gaia / gaia_auth_fetcher_unittest.cc
blob9d2f60aa5f05e384b8b3bcd3f021f584c126d032
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // A complete set of unit tests for GaiaAuthFetcher.
6 // Originally ported from GoogleAuthenticator tests.
8 #include <string>
10 #include "base/json/json_reader.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "google_apis/gaia/gaia_auth_consumer.h"
14 #include "google_apis/gaia/gaia_auth_fetcher.h"
15 #include "google_apis/gaia/gaia_urls.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "google_apis/gaia/mock_url_fetcher_factory.h"
18 #include "google_apis/google_api_keys.h"
19 #include "net/base/load_flags.h"
20 #include "net/base/net_errors.h"
21 #include "net/http/http_status_code.h"
22 #include "net/url_request/test_url_fetcher_factory.h"
23 #include "net/url_request/url_fetcher_delegate.h"
24 #include "net/url_request/url_request_status.h"
25 #include "net/url_request/url_request_test_util.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "url/gurl.h"
30 using ::testing::Invoke;
31 using ::testing::_;
33 const char kGetAuthCodeValidCookie[] =
34 "oauth_code=test-code; Path=/test; Secure; HttpOnly";
35 const char kGetAuthCodeCookieNoSecure[] =
36 "oauth_code=test-code; Path=/test; HttpOnly";
37 const char kGetAuthCodeCookieNoHttpOnly[] =
38 "oauth_code=test-code; Path=/test; Secure";
39 const char kGetAuthCodeCookieNoOAuthCode[] =
40 "Path=/test; Secure; HttpOnly";
41 const char kGetTokenPairValidResponse[] =
42 "{"
43 " \"refresh_token\": \"rt1\","
44 " \"access_token\": \"at1\","
45 " \"expires_in\": 3600,"
46 " \"token_type\": \"Bearer\""
47 "}";
49 MockFetcher::MockFetcher(bool success,
50 const GURL& url,
51 const std::string& results,
52 net::URLFetcher::RequestType request_type,
53 net::URLFetcherDelegate* d)
54 : TestURLFetcher(0, url, d) {
55 set_url(url);
56 net::URLRequestStatus::Status code;
58 if (success) {
59 set_response_code(net::HTTP_OK);
60 code = net::URLRequestStatus::SUCCESS;
61 } else {
62 set_response_code(net::HTTP_FORBIDDEN);
63 code = net::URLRequestStatus::FAILED;
66 set_status(net::URLRequestStatus(code, 0));
67 SetResponseString(results);
70 MockFetcher::MockFetcher(const GURL& url,
71 const net::URLRequestStatus& status,
72 int response_code,
73 const net::ResponseCookies& cookies,
74 const std::string& results,
75 net::URLFetcher::RequestType request_type,
76 net::URLFetcherDelegate* d)
77 : TestURLFetcher(0, url, d) {
78 set_url(url);
79 set_status(status);
80 set_response_code(response_code);
81 set_cookies(cookies);
82 SetResponseString(results);
85 MockFetcher::~MockFetcher() {}
87 void MockFetcher::Start() {
88 delegate()->OnURLFetchComplete(this);
91 class GaiaAuthFetcherTest : public testing::Test {
92 protected:
93 GaiaAuthFetcherTest()
94 : client_login_source_(GaiaUrls::GetInstance()->client_login_url()),
95 issue_auth_token_source_(
96 GaiaUrls::GetInstance()->issue_auth_token_url()),
97 client_login_to_oauth2_source_(
98 GaiaUrls::GetInstance()->client_login_to_oauth2_url()),
99 oauth2_token_source_(GaiaUrls::GetInstance()->oauth2_token_url()),
100 token_auth_source_(GaiaUrls::GetInstance()->token_auth_url()),
101 merge_session_source_(GaiaUrls::GetInstance()->merge_session_url()),
102 uberauth_token_source_(
103 GaiaUrls::GetInstance()->oauth1_login_url().Resolve(
104 "?source=&issueuberauth=1")),
105 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()) {}
107 void RunParsingTest(const std::string& data,
108 const std::string& sid,
109 const std::string& lsid,
110 const std::string& token) {
111 std::string out_sid;
112 std::string out_lsid;
113 std::string out_token;
115 GaiaAuthFetcher::ParseClientLoginResponse(data,
116 &out_sid,
117 &out_lsid,
118 &out_token);
119 EXPECT_EQ(lsid, out_lsid);
120 EXPECT_EQ(sid, out_sid);
121 EXPECT_EQ(token, out_token);
124 void RunErrorParsingTest(const std::string& data,
125 const std::string& error,
126 const std::string& error_url,
127 const std::string& captcha_url,
128 const std::string& captcha_token) {
129 std::string out_error;
130 std::string out_error_url;
131 std::string out_captcha_url;
132 std::string out_captcha_token;
134 GaiaAuthFetcher::ParseClientLoginFailure(data,
135 &out_error,
136 &out_error_url,
137 &out_captcha_url,
138 &out_captcha_token);
139 EXPECT_EQ(error, out_error);
140 EXPECT_EQ(error_url, out_error_url);
141 EXPECT_EQ(captcha_url, out_captcha_url);
142 EXPECT_EQ(captcha_token, out_captcha_token);
145 net::ResponseCookies cookies_;
146 GURL client_login_source_;
147 GURL issue_auth_token_source_;
148 GURL client_login_to_oauth2_source_;
149 GURL oauth2_token_source_;
150 GURL token_auth_source_;
151 GURL merge_session_source_;
152 GURL uberauth_token_source_;
153 GURL oauth_login_gurl_;
155 protected:
156 net::TestURLRequestContextGetter* GetRequestContext() {
157 if (!request_context_getter_) {
158 request_context_getter_ = new net::TestURLRequestContextGetter(
159 message_loop_.message_loop_proxy());
161 return request_context_getter_;
164 base::MessageLoop message_loop_;
165 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
168 class MockGaiaConsumer : public GaiaAuthConsumer {
169 public:
170 MockGaiaConsumer() {}
171 ~MockGaiaConsumer() {}
173 MOCK_METHOD1(OnClientLoginSuccess, void(const ClientLoginResult& result));
174 MOCK_METHOD2(OnIssueAuthTokenSuccess, void(const std::string& service,
175 const std::string& token));
176 MOCK_METHOD1(OnClientOAuthSuccess,
177 void(const GaiaAuthConsumer::ClientOAuthResult& result));
178 MOCK_METHOD1(OnClientOAuthCodeSuccess,
179 void(const std::string& result));
180 MOCK_METHOD1(OnMergeSessionSuccess, void(const std::string& data));
181 MOCK_METHOD1(OnUberAuthTokenSuccess, void(const std::string& data));
182 MOCK_METHOD1(OnClientLoginFailure,
183 void(const GoogleServiceAuthError& error));
184 MOCK_METHOD2(OnIssueAuthTokenFailure, void(const std::string& service,
185 const GoogleServiceAuthError& error));
186 MOCK_METHOD1(OnClientOAuthFailure,
187 void(const GoogleServiceAuthError& error));
188 MOCK_METHOD1(OnClientOAuthCodeFailure,
189 void(const GoogleServiceAuthError& error));
190 MOCK_METHOD1(OnMergeSessionFailure, void(
191 const GoogleServiceAuthError& error));
192 MOCK_METHOD1(OnUberAuthTokenFailure, void(
193 const GoogleServiceAuthError& error));
194 MOCK_METHOD1(OnListAccountsSuccess, void(const std::string& data));
197 #if defined(OS_WIN)
198 #define MAYBE_ErrorComparator DISABLED_ErrorComparator
199 #else
200 #define MAYBE_ErrorComparator ErrorComparator
201 #endif
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))
234 .Times(1);
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))
253 .Times(1);
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))
273 .Times(1);
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"
295 "Error=E\n"
296 "CaptchaToken=T\n"
297 "CaptchaUrl=C\n", "E", "U", "C", "T");
298 RunErrorParsingTest("CaptchaToken=T\n"
299 "Error=E\n"
300 "Url=U\n"
301 "CaptchaUrl=C\n", "E", "U", "C", "T");
302 RunErrorParsingTest("\n\n\nCaptchaToken=T\n"
303 "\nError=E\n"
304 "\nUrl=U\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";
314 result.sid = "sid";
315 result.token = "auth";
316 result.data = data;
318 MockGaiaConsumer consumer;
319 EXPECT_CALL(consumer, OnClientLoginSuccess(result))
320 .Times(1);
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"))
333 .Times(1);
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))
364 .Times(1);
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, CaptchaParse) {
375 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
376 std::string data = "Url=http://www.google.com/login/captcha\n"
377 "Error=CaptchaRequired\n"
378 "CaptchaToken=CCTOKEN\n"
379 "CaptchaUrl=Captcha?ctoken=CCTOKEN\n";
380 GoogleServiceAuthError error =
381 GaiaAuthFetcher::GenerateAuthError(data, status);
383 std::string token = "CCTOKEN";
384 GURL image_url("http://accounts.google.com/Captcha?ctoken=CCTOKEN");
385 GURL unlock_url("http://www.google.com/login/captcha");
387 EXPECT_EQ(error.state(), GoogleServiceAuthError::CAPTCHA_REQUIRED);
388 EXPECT_EQ(error.captcha().token, token);
389 EXPECT_EQ(error.captcha().image_url, image_url);
390 EXPECT_EQ(error.captcha().unlock_url, unlock_url);
393 TEST_F(GaiaAuthFetcherTest, AccountDeletedError) {
394 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
395 std::string data = "Error=AccountDeleted\n";
396 GoogleServiceAuthError error =
397 GaiaAuthFetcher::GenerateAuthError(data, status);
398 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DELETED);
401 TEST_F(GaiaAuthFetcherTest, AccountDisabledError) {
402 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
403 std::string data = "Error=AccountDisabled\n";
404 GoogleServiceAuthError error =
405 GaiaAuthFetcher::GenerateAuthError(data, status);
406 EXPECT_EQ(error.state(), GoogleServiceAuthError::ACCOUNT_DISABLED);
409 TEST_F(GaiaAuthFetcherTest, BadAuthenticationError) {
410 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
411 std::string data = "Error=BadAuthentication\n";
412 GoogleServiceAuthError error =
413 GaiaAuthFetcher::GenerateAuthError(data, status);
414 EXPECT_EQ(error.state(), GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
417 TEST_F(GaiaAuthFetcherTest, IncomprehensibleError) {
418 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
419 std::string data = "Error=Gobbledygook\n";
420 GoogleServiceAuthError error =
421 GaiaAuthFetcher::GenerateAuthError(data, status);
422 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE);
425 TEST_F(GaiaAuthFetcherTest, ServiceUnavailableError) {
426 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
427 std::string data = "Error=ServiceUnavailable\n";
428 GoogleServiceAuthError error =
429 GaiaAuthFetcher::GenerateAuthError(data, status);
430 EXPECT_EQ(error.state(), GoogleServiceAuthError::SERVICE_UNAVAILABLE);
433 TEST_F(GaiaAuthFetcherTest, FullLogin) {
434 MockGaiaConsumer consumer;
435 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
436 .Times(1);
438 MockURLFetcherFactory<MockFetcher> factory;
440 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
441 auth.StartClientLogin("username",
442 "password",
443 "service",
444 std::string(),
445 std::string(),
446 GaiaAuthFetcher::HostedAccountsAllowed);
449 TEST_F(GaiaAuthFetcherTest, FullLoginFailure) {
450 MockGaiaConsumer consumer;
451 EXPECT_CALL(consumer, OnClientLoginFailure(_))
452 .Times(1);
454 MockURLFetcherFactory<MockFetcher> factory;
455 factory.set_success(false);
457 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
458 auth.StartClientLogin("username",
459 "password",
460 "service",
461 std::string(),
462 std::string(),
463 GaiaAuthFetcher::HostedAccountsAllowed);
466 TEST_F(GaiaAuthFetcherTest, ClientFetchPending) {
467 MockGaiaConsumer consumer;
468 EXPECT_CALL(consumer, OnClientLoginSuccess(_))
469 .Times(1);
471 net::TestURLFetcherFactory factory;
473 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
474 auth.StartClientLogin("username",
475 "password",
476 "service",
477 std::string(),
478 std::string(),
479 GaiaAuthFetcher::HostedAccountsAllowed);
481 EXPECT_TRUE(auth.HasPendingFetch());
482 MockFetcher mock_fetcher(
483 client_login_source_,
484 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
485 net::HTTP_OK, cookies_, "SID=sid\nLSID=lsid\nAuth=auth\n",
486 net::URLFetcher::GET, &auth);
487 auth.OnURLFetchComplete(&mock_fetcher);
488 EXPECT_FALSE(auth.HasPendingFetch());
491 TEST_F(GaiaAuthFetcherTest, FullTokenSuccess) {
492 MockGaiaConsumer consumer;
493 EXPECT_CALL(consumer, OnIssueAuthTokenSuccess("service", "token"))
494 .Times(1);
496 net::TestURLFetcherFactory factory;
497 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
498 auth.StartIssueAuthToken("sid", "lsid", "service");
500 EXPECT_TRUE(auth.HasPendingFetch());
501 MockFetcher mock_fetcher(
502 issue_auth_token_source_,
503 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
504 net::HTTP_OK, cookies_, "token",
505 net::URLFetcher::GET, &auth);
506 auth.OnURLFetchComplete(&mock_fetcher);
507 EXPECT_FALSE(auth.HasPendingFetch());
510 TEST_F(GaiaAuthFetcherTest, FullTokenFailure) {
511 MockGaiaConsumer consumer;
512 EXPECT_CALL(consumer, OnIssueAuthTokenFailure("service", _))
513 .Times(1);
515 net::TestURLFetcherFactory factory;
517 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
518 auth.StartIssueAuthToken("sid", "lsid", "service");
520 EXPECT_TRUE(auth.HasPendingFetch());
521 MockFetcher mock_fetcher(
522 issue_auth_token_source_,
523 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
524 net::HTTP_FORBIDDEN,
525 cookies_,
526 std::string(),
527 net::URLFetcher::GET,
528 &auth);
529 auth.OnURLFetchComplete(&mock_fetcher);
530 EXPECT_FALSE(auth.HasPendingFetch());
533 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenSuccess) {
534 MockGaiaConsumer consumer;
535 EXPECT_CALL(consumer, OnClientOAuthSuccess(
536 GaiaAuthConsumer::ClientOAuthResult("rt1", "at1", 3600))).Times(1);
538 net::TestURLFetcherFactory factory;
539 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
540 auth.StartLsoForOAuthLoginTokenExchange("lso_token");
541 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
542 EXPECT_TRUE(NULL != fetcher);
543 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES,
544 fetcher->GetLoadFlags());
546 net::ResponseCookies cookies;
547 cookies.push_back(kGetAuthCodeValidCookie);
548 EXPECT_TRUE(auth.HasPendingFetch());
549 MockFetcher mock_fetcher1(
550 client_login_to_oauth2_source_,
551 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
552 net::HTTP_OK,
553 cookies,
554 std::string(),
555 net::URLFetcher::POST,
556 &auth);
557 auth.OnURLFetchComplete(&mock_fetcher1);
558 EXPECT_TRUE(auth.HasPendingFetch());
559 MockFetcher mock_fetcher2(
560 oauth2_token_source_,
561 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
562 net::HTTP_OK, cookies_, kGetTokenPairValidResponse,
563 net::URLFetcher::POST, &auth);
564 auth.OnURLFetchComplete(&mock_fetcher2);
565 EXPECT_FALSE(auth.HasPendingFetch());
568 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies) {
569 MockGaiaConsumer consumer;
570 net::TestURLFetcherFactory factory;
571 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
572 auth.StartCookieForOAuthLoginTokenExchange("0");
573 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
574 EXPECT_TRUE(NULL != fetcher);
575 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
578 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenClientLoginToOAuth2Failure) {
579 MockGaiaConsumer consumer;
580 EXPECT_CALL(consumer, OnClientOAuthFailure(_))
581 .Times(1);
583 net::TestURLFetcherFactory factory;
584 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
585 auth.StartLsoForOAuthLoginTokenExchange("lso_token");
587 net::ResponseCookies cookies;
588 EXPECT_TRUE(auth.HasPendingFetch());
589 MockFetcher mock_fetcher(
590 client_login_to_oauth2_source_,
591 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
592 net::HTTP_FORBIDDEN,
593 cookies,
594 std::string(),
595 net::URLFetcher::POST,
596 &auth);
597 auth.OnURLFetchComplete(&mock_fetcher);
598 EXPECT_FALSE(auth.HasPendingFetch());
601 TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenOAuth2TokenPairFailure) {
602 MockGaiaConsumer consumer;
603 EXPECT_CALL(consumer, OnClientOAuthFailure(_))
604 .Times(1);
606 net::TestURLFetcherFactory factory;
607 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
608 auth.StartLsoForOAuthLoginTokenExchange("lso_token");
610 net::ResponseCookies cookies;
611 cookies.push_back(kGetAuthCodeValidCookie);
612 EXPECT_TRUE(auth.HasPendingFetch());
613 MockFetcher mock_fetcher1(
614 client_login_to_oauth2_source_,
615 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
616 net::HTTP_OK,
617 cookies,
618 std::string(),
619 net::URLFetcher::POST,
620 &auth);
621 auth.OnURLFetchComplete(&mock_fetcher1);
622 EXPECT_TRUE(auth.HasPendingFetch());
623 MockFetcher mock_fetcher2(
624 oauth2_token_source_,
625 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
626 net::HTTP_FORBIDDEN,
627 cookies_,
628 std::string(),
629 net::URLFetcher::POST,
630 &auth);
631 auth.OnURLFetchComplete(&mock_fetcher2);
632 EXPECT_FALSE(auth.HasPendingFetch());
635 TEST_F(GaiaAuthFetcherTest, OAuthCodeWithCookiesSuccess) {
636 MockGaiaConsumer consumer;
637 EXPECT_CALL(consumer, OnClientOAuthCodeSuccess("test-code")).Times(1);
639 net::TestURLFetcherFactory factory;
640 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
641 auth.StartCookieForOAuthCodeExchange("");
643 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
644 EXPECT_TRUE(NULL != fetcher);
645 EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
646 EXPECT_TRUE(auth.HasPendingFetch());
648 net::ResponseCookies cookies;
649 cookies.push_back(kGetAuthCodeValidCookie);
650 MockFetcher mock_fetcher(
651 client_login_to_oauth2_source_,
652 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
653 net::HTTP_OK,
654 cookies,
655 std::string(),
656 net::URLFetcher::POST,
657 &auth);
658 auth.OnURLFetchComplete(&mock_fetcher);
659 EXPECT_FALSE(auth.HasPendingFetch());
662 TEST_F(GaiaAuthFetcherTest, OAuthCodeWithCookiesFailure) {
663 MockGaiaConsumer consumer;
664 EXPECT_CALL(consumer, OnClientOAuthCodeFailure(_)).Times(1);
666 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
667 auth.StartCookieForOAuthCodeExchange("");
669 EXPECT_TRUE(auth.HasPendingFetch());
670 net::ResponseCookies cookies;
671 MockFetcher mock_fetcher(
672 client_login_to_oauth2_source_,
673 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
674 net::HTTP_FORBIDDEN,
675 cookies,
676 std::string(),
677 net::URLFetcher::POST,
678 &auth);
679 auth.OnURLFetchComplete(&mock_fetcher);
680 EXPECT_FALSE(auth.HasPendingFetch());
683 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccess) {
684 MockGaiaConsumer consumer;
685 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>"))
686 .Times(1);
688 net::TestURLFetcherFactory factory;
690 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
691 auth.StartMergeSession("myubertoken");
693 EXPECT_TRUE(auth.HasPendingFetch());
694 MockFetcher mock_fetcher(
695 merge_session_source_,
696 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
697 net::HTTP_OK, cookies_, "<html></html>", net::URLFetcher::GET,
698 &auth);
699 auth.OnURLFetchComplete(&mock_fetcher);
700 EXPECT_FALSE(auth.HasPendingFetch());
703 TEST_F(GaiaAuthFetcherTest, MergeSessionSuccessRedirect) {
704 MockGaiaConsumer consumer;
705 EXPECT_CALL(consumer, OnMergeSessionSuccess("<html></html>"))
706 .Times(1);
708 net::TestURLFetcherFactory factory;
710 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
711 auth.StartMergeSession("myubertoken");
713 // Make sure the fetcher created has the expected flags. Set its url()
714 // properties to reflect a redirect.
715 net::TestURLFetcher* test_fetcher = factory.GetFetcherByID(0);
716 EXPECT_TRUE(test_fetcher != NULL);
717 EXPECT_TRUE(test_fetcher->GetLoadFlags() == net::LOAD_NORMAL);
718 EXPECT_TRUE(auth.HasPendingFetch());
720 GURL final_url("http://www.google.com/CheckCookie");
721 test_fetcher->set_url(final_url);
722 test_fetcher->set_status(
723 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
724 test_fetcher->set_response_code(net::HTTP_OK);
725 test_fetcher->set_cookies(cookies_);
726 test_fetcher->SetResponseString("<html></html>");
728 auth.OnURLFetchComplete(test_fetcher);
729 EXPECT_FALSE(auth.HasPendingFetch());
732 TEST_F(GaiaAuthFetcherTest, UberAuthTokenSuccess) {
733 MockGaiaConsumer consumer;
734 EXPECT_CALL(consumer, OnUberAuthTokenSuccess("uberToken"))
735 .Times(1);
737 net::TestURLFetcherFactory factory;
739 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
740 auth.StartTokenFetchForUberAuthExchange("myAccessToken");
742 EXPECT_TRUE(auth.HasPendingFetch());
743 MockFetcher mock_fetcher(
744 uberauth_token_source_,
745 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0),
746 net::HTTP_OK, cookies_, "uberToken", net::URLFetcher::POST,
747 &auth);
748 auth.OnURLFetchComplete(&mock_fetcher);
749 EXPECT_FALSE(auth.HasPendingFetch());
752 TEST_F(GaiaAuthFetcherTest, ParseClientLoginToOAuth2Response) {
753 { // No cookies.
754 std::string auth_code;
755 net::ResponseCookies cookies;
756 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
757 cookies, &auth_code));
758 EXPECT_EQ("", auth_code);
760 { // Few cookies, nothing appropriate.
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 EXPECT_FALSE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
767 cookies, &auth_code));
768 EXPECT_EQ("", auth_code);
770 { // Few cookies, one of them is valid.
771 std::string auth_code;
772 net::ResponseCookies cookies;
773 cookies.push_back(kGetAuthCodeCookieNoSecure);
774 cookies.push_back(kGetAuthCodeCookieNoHttpOnly);
775 cookies.push_back(kGetAuthCodeCookieNoOAuthCode);
776 cookies.push_back(kGetAuthCodeValidCookie);
777 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
778 cookies, &auth_code));
779 EXPECT_EQ("test-code", auth_code);
781 { // Single valid cookie (like in real responses).
782 std::string auth_code;
783 net::ResponseCookies cookies;
784 cookies.push_back(kGetAuthCodeValidCookie);
785 EXPECT_TRUE(GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
786 cookies, &auth_code));
787 EXPECT_EQ("test-code", auth_code);
791 TEST_F(GaiaAuthFetcherTest, StartOAuthLogin) {
792 // OAuthLogin returns the same as the ClientLogin endpoint, minus CAPTCHA
793 // responses.
794 std::string data("SID=sid\nLSID=lsid\nAuth=auth\n");
796 GaiaAuthConsumer::ClientLoginResult result;
797 result.lsid = "lsid";
798 result.sid = "sid";
799 result.token = "auth";
800 result.data = data;
802 MockGaiaConsumer consumer;
803 EXPECT_CALL(consumer, OnClientLoginSuccess(result))
804 .Times(1);
806 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
807 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
808 MockFetcher mock_fetcher(
809 oauth_login_gurl_, status, net::HTTP_OK, cookies_, data,
810 net::URLFetcher::GET, &auth);
811 auth.OnURLFetchComplete(&mock_fetcher);
814 TEST_F(GaiaAuthFetcherTest, ListAccounts) {
815 std::string data("[\"gaia.l.a.r\", ["
816 "[\"gaia.l.a\", 1, \"First Last\", \"user@gmail.com\", "
817 "\"//googleusercontent.com/A/B/C/D/photo.jpg\", 1, 1, 0]]]");
818 MockGaiaConsumer consumer;
819 EXPECT_CALL(consumer, OnListAccountsSuccess(data)).Times(1);
821 GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
822 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
823 MockFetcher mock_fetcher(GaiaUrls::GetInstance()->list_accounts_url(),
824 status, net::HTTP_OK, cookies_, data, net::URLFetcher::GET, &auth);
825 auth.OnURLFetchComplete(&mock_fetcher);