Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/browser/extensions/api/identity/identity_apitest.cc

Issue 410553002: Cleanup: Remove redundant MockGetAuthTokenFunction, add missing cases (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add negative cases for secondary accounts Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/extensions/api/identity/identity_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 embedder_web_contents_->Close(); 262 embedder_web_contents_->Close();
263 } 263 }
264 264
265 private: 265 private:
266 GURL url_; 266 GURL url_;
267 content::WebContents* embedder_web_contents_; 267 content::WebContents* embedder_web_contents_;
268 }; 268 };
269 269
270 } // namespace 270 } // namespace
271 271
272 class MockGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
273 public:
274 MockGetAuthTokenFunction() : login_access_token_result_(true),
275 login_ui_result_(true),
276 scope_ui_result_(true),
277 login_ui_shown_(false),
278 scope_ui_shown_(false) {
279 }
280
281 void set_login_access_token_result(bool result) {
282 login_access_token_result_ = result;
283 }
284
285 void set_login_ui_result(bool result) {
286 login_ui_result_ = result;
287 }
288
289 void set_scope_ui_failure(GaiaWebAuthFlow::Failure failure) {
290 scope_ui_result_ = false;
291 scope_ui_failure_ = failure;
292 }
293
294 void set_scope_ui_oauth_error(const std::string& oauth_error) {
295 scope_ui_result_ = false;
296 scope_ui_failure_ = GaiaWebAuthFlow::OAUTH_ERROR;
297 scope_ui_oauth_error_ = oauth_error;
298 }
299
300 bool login_ui_shown() const {
301 return login_ui_shown_;
302 }
303
304 bool scope_ui_shown() const {
305 return scope_ui_shown_;
306 }
307
308 const ExtensionTokenKey* extension_token_key() { return token_key_.get(); }
309
310 virtual void StartLoginAccessTokenRequest() OVERRIDE {
311 if (login_access_token_result_) {
312 OnGetTokenSuccess(login_token_request_.get(), "access_token",
313 base::Time::Now() + base::TimeDelta::FromHours(1LL));
314 } else {
315 GoogleServiceAuthError error(
316 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
317 OnGetTokenFailure(login_token_request_.get(), error);
318 }
319 }
320
321 virtual void ShowLoginPopup() OVERRIDE {
322 EXPECT_FALSE(login_ui_shown_);
323 login_ui_shown_ = true;
324 if (login_ui_result_)
325 SigninSuccess();
326 else
327 SigninFailed();
328 }
329
330 virtual void ShowOAuthApprovalDialog(
331 const IssueAdviceInfo& issue_advice) OVERRIDE {
332 scope_ui_shown_ = true;
333
334 if (scope_ui_result_) {
335 OnGaiaFlowCompleted(kAccessToken, "3600");
336 } else if (scope_ui_failure_ == GaiaWebAuthFlow::SERVICE_AUTH_ERROR) {
337 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
338 OnGaiaFlowFailure(scope_ui_failure_, error, "");
339 } else {
340 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
341 OnGaiaFlowFailure(scope_ui_failure_, error, scope_ui_oauth_error_);
342 }
343 }
344
345 MOCK_CONST_METHOD0(HasLoginToken, bool());
346 MOCK_METHOD1(CreateMintTokenFlow,
347 OAuth2MintTokenFlow* (const std::string& login_access_token));
348
349 private:
350 ~MockGetAuthTokenFunction() {}
351 bool login_access_token_result_;
352 bool login_ui_result_;
353 bool scope_ui_result_;
354 GaiaWebAuthFlow::Failure scope_ui_failure_;
355 std::string scope_ui_oauth_error_;
356 bool login_ui_shown_;
357 bool scope_ui_shown_;
358 };
359
360 // TODO(courage): Replace MockGetAuthTokenFunction with
361 // FakeGetAuthTokenFunction in all tests.
362
363 class FakeGetAuthTokenFunction : public IdentityGetAuthTokenFunction { 272 class FakeGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
364 public: 273 public:
365 FakeGetAuthTokenFunction() 274 FakeGetAuthTokenFunction()
366 : login_access_token_result_(true), 275 : login_access_token_result_(true),
276 auto_login_access_token_(true),
367 login_ui_result_(true), 277 login_ui_result_(true),
368 scope_ui_result_(true), 278 scope_ui_result_(true),
369 login_ui_shown_(false), 279 login_ui_shown_(false),
370 scope_ui_shown_(false) {} 280 scope_ui_shown_(false) {}
371 281
372 void set_login_access_token_result(bool result) { 282 void set_login_access_token_result(bool result) {
373 login_access_token_result_ = result; 283 login_access_token_result_ = result;
374 } 284 }
375 285
286 void set_auto_login_access_token(bool automatic) {
287 auto_login_access_token_ = automatic;
288 }
289
376 void set_login_ui_result(bool result) { login_ui_result_ = result; } 290 void set_login_ui_result(bool result) { login_ui_result_ = result; }
377 291
378 void set_mint_token_flow(scoped_ptr<OAuth2MintTokenFlow> flow) { 292 void set_mint_token_flow(scoped_ptr<OAuth2MintTokenFlow> flow) {
379 flow_ = flow.Pass(); 293 flow_ = flow.Pass();
380 } 294 }
381 295
296 void set_mint_token_result(TestOAuth2MintTokenFlow::ResultType result_type) {
297 set_mint_token_flow(scoped_ptr<TestOAuth2MintTokenFlow>(
298 new TestOAuth2MintTokenFlow(result_type, this))
299 .PassAs<OAuth2MintTokenFlow>());
300 }
301
382 void set_scope_ui_failure(GaiaWebAuthFlow::Failure failure) { 302 void set_scope_ui_failure(GaiaWebAuthFlow::Failure failure) {
383 scope_ui_result_ = false; 303 scope_ui_result_ = false;
384 scope_ui_failure_ = failure; 304 scope_ui_failure_ = failure;
385 } 305 }
386 306
387 void set_scope_ui_oauth_error(const std::string& oauth_error) { 307 void set_scope_ui_oauth_error(const std::string& oauth_error) {
388 scope_ui_result_ = false; 308 scope_ui_result_ = false;
389 scope_ui_failure_ = GaiaWebAuthFlow::OAUTH_ERROR; 309 scope_ui_failure_ = GaiaWebAuthFlow::OAUTH_ERROR;
390 scope_ui_oauth_error_ = oauth_error; 310 scope_ui_oauth_error_ = oauth_error;
391 } 311 }
392 312
393 bool login_ui_shown() const { return login_ui_shown_; } 313 bool login_ui_shown() const { return login_ui_shown_; }
394 314
395 bool scope_ui_shown() const { return scope_ui_shown_; } 315 bool scope_ui_shown() const { return scope_ui_shown_; }
396 316
397 std::string login_access_token() const { return login_access_token_; } 317 std::string login_access_token() const { return login_access_token_; }
398 318
319 virtual void StartLoginAccessTokenRequest() OVERRIDE {
320 if (auto_login_access_token_) {
321 if (login_access_token_result_) {
322 OnGetTokenSuccess(login_token_request_.get(),
323 "access_token",
324 base::Time::Now() + base::TimeDelta::FromHours(1LL));
325 } else {
326 GoogleServiceAuthError error(
327 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
328 OnGetTokenFailure(login_token_request_.get(), error);
329 }
330 } else {
331 // Make a request to the token service. The test now must tell
332 // the token service to issue an access token (or an error).
333 IdentityGetAuthTokenFunction::StartLoginAccessTokenRequest();
334 }
335 }
336
399 virtual void ShowLoginPopup() OVERRIDE { 337 virtual void ShowLoginPopup() OVERRIDE {
400 EXPECT_FALSE(login_ui_shown_); 338 EXPECT_FALSE(login_ui_shown_);
401 login_ui_shown_ = true; 339 login_ui_shown_ = true;
402 if (login_ui_result_) 340 if (login_ui_result_)
403 SigninSuccess(); 341 SigninSuccess();
404 else 342 else
405 SigninFailed(); 343 SigninFailed();
406 } 344 }
407 345
408 virtual void ShowOAuthApprovalDialog( 346 virtual void ShowOAuthApprovalDialog(
409 const IssueAdviceInfo& issue_advice) OVERRIDE { 347 const IssueAdviceInfo& issue_advice) OVERRIDE {
410 scope_ui_shown_ = true; 348 scope_ui_shown_ = true;
411 349
412 if (scope_ui_result_) { 350 if (scope_ui_result_) {
413 OnGaiaFlowCompleted(kAccessToken, "3600"); 351 OnGaiaFlowCompleted(kAccessToken, "3600");
414 } else if (scope_ui_failure_ == GaiaWebAuthFlow::SERVICE_AUTH_ERROR) { 352 } else if (scope_ui_failure_ == GaiaWebAuthFlow::SERVICE_AUTH_ERROR) {
415 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED); 353 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
416 OnGaiaFlowFailure(scope_ui_failure_, error, ""); 354 OnGaiaFlowFailure(scope_ui_failure_, error, "");
417 } else { 355 } else {
418 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); 356 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
419 OnGaiaFlowFailure(scope_ui_failure_, error, scope_ui_oauth_error_); 357 OnGaiaFlowFailure(scope_ui_failure_, error, scope_ui_oauth_error_);
420 } 358 }
421 } 359 }
422 360
423 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( 361 virtual OAuth2MintTokenFlow* CreateMintTokenFlow(
424 const std::string& login_access_token) OVERRIDE { 362 const std::string& login_access_token) OVERRIDE {
425 EXPECT_TRUE(login_access_token_.empty()); 363 EXPECT_TRUE(login_access_token_.empty());
364 // Save the login token used to create the flow so tests can see
365 // what account was used.
426 login_access_token_ = login_access_token; 366 login_access_token_ = login_access_token;
427 return flow_.release(); 367 return flow_.release();
428 } 368 }
429 369
430 private: 370 private:
431 virtual ~FakeGetAuthTokenFunction() {} 371 virtual ~FakeGetAuthTokenFunction() {}
432 bool login_access_token_result_; 372 bool login_access_token_result_;
373 bool auto_login_access_token_;
433 bool login_ui_result_; 374 bool login_ui_result_;
434 bool scope_ui_result_; 375 bool scope_ui_result_;
435 GaiaWebAuthFlow::Failure scope_ui_failure_; 376 GaiaWebAuthFlow::Failure scope_ui_failure_;
436 std::string scope_ui_oauth_error_; 377 std::string scope_ui_oauth_error_;
437 bool login_ui_shown_; 378 bool login_ui_shown_;
438 bool scope_ui_shown_; 379 bool scope_ui_shown_;
439 380
440 scoped_ptr<OAuth2MintTokenFlow> flow_; 381 scoped_ptr<OAuth2MintTokenFlow> flow_;
441 382
442 std::string login_access_token_; 383 std::string login_access_token_;
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 profile())); 639 profile()));
699 ASSERT_TRUE(token_service_); 640 ASSERT_TRUE(token_service_);
700 } 641 }
701 642
702 void SignIn(const std::string account_key) { 643 void SignIn(const std::string account_key) {
703 #if defined(OS_CHROMEOS) 644 #if defined(OS_CHROMEOS)
704 signin_manager_->SetAuthenticatedUsername(account_key); 645 signin_manager_->SetAuthenticatedUsername(account_key);
705 #else 646 #else
706 signin_manager_->SignIn(account_key, "password"); 647 signin_manager_->SignIn(account_key, "password");
707 #endif 648 #endif
649 token_service_->IssueRefreshTokenForUser(account_key, "refresh_token");
650 }
651
652 void IssueLoginRefreshTokenForAccount(const std::string account_key) {
653 token_service_->IssueRefreshTokenForUser(account_key, "refresh_token");
654 }
655
656 void IssueLoginAccessTokenForAccount(const std::string account_key) {
657 token_service_->IssueAllTokensForAccount(
658 account_key,
659 "access_token-" + account_key,
660 base::Time::Now() + base::TimeDelta::FromSeconds(3600));
708 } 661 }
709 662
710 void SetAccountState(gaia::AccountIds ids, bool is_signed_in) { 663 void SetAccountState(gaia::AccountIds ids, bool is_signed_in) {
711 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest( 664 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
712 ids, is_signed_in); 665 ids, is_signed_in);
713 } 666 }
714 667
715 protected: 668 protected:
716 enum OAuth2Fields { 669 enum OAuth2Fields {
717 NONE = 0, 670 NONE = 0,
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 private: 742 private:
790 std::string extension_id_; 743 std::string extension_id_;
791 std::set<std::string> oauth_scopes_; 744 std::set<std::string> oauth_scopes_;
792 745
793 scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription> 746 scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription>
794 will_create_browser_context_services_subscription_; 747 will_create_browser_context_services_subscription_;
795 }; 748 };
796 749
797 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 750 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
798 NoClientId) { 751 NoClientId) {
799 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 752 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
800 func->set_extension(CreateExtension(SCOPES)); 753 func->set_extension(CreateExtension(SCOPES));
801 std::string error = utils::RunFunctionAndReturnError( 754 std::string error = utils::RunFunctionAndReturnError(
802 func.get(), "[{}]", browser()); 755 func.get(), "[{}]", browser());
803 EXPECT_EQ(std::string(errors::kInvalidClientId), error); 756 EXPECT_EQ(std::string(errors::kInvalidClientId), error);
804 EXPECT_FALSE(func->login_ui_shown()); 757 EXPECT_FALSE(func->login_ui_shown());
805 EXPECT_FALSE(func->scope_ui_shown()); 758 EXPECT_FALSE(func->scope_ui_shown());
806 } 759 }
807 760
808 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 761 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
809 NoScopes) { 762 NoScopes) {
810 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 763 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
811 func->set_extension(CreateExtension(CLIENT_ID)); 764 func->set_extension(CreateExtension(CLIENT_ID));
812 std::string error = utils::RunFunctionAndReturnError( 765 std::string error = utils::RunFunctionAndReturnError(
813 func.get(), "[{}]", browser()); 766 func.get(), "[{}]", browser());
814 EXPECT_EQ(std::string(errors::kInvalidScopes), error); 767 EXPECT_EQ(std::string(errors::kInvalidScopes), error);
815 EXPECT_FALSE(func->login_ui_shown()); 768 EXPECT_FALSE(func->login_ui_shown());
816 EXPECT_FALSE(func->scope_ui_shown()); 769 EXPECT_FALSE(func->scope_ui_shown());
817 } 770 }
818 771
819 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 772 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
820 NonInteractiveNotSignedIn) { 773 NonInteractiveNotSignedIn) {
821 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 774 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
822 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 775 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
823 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
824 std::string error = utils::RunFunctionAndReturnError( 776 std::string error = utils::RunFunctionAndReturnError(
825 func.get(), "[{}]", browser()); 777 func.get(), "[{}]", browser());
826 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error); 778 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
827 EXPECT_FALSE(func->login_ui_shown()); 779 EXPECT_FALSE(func->login_ui_shown());
828 EXPECT_FALSE(func->scope_ui_shown()); 780 EXPECT_FALSE(func->scope_ui_shown());
829 } 781 }
830 782
831 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 783 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
832 NonInteractiveMintFailure) { 784 NonInteractiveMintFailure) {
833 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 785 SignIn("primary@example.com");
786 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
834 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 787 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
835 EXPECT_CALL(*func.get(), HasLoginToken()) 788 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE);
836 .WillOnce(Return(true)); 789 std::string error =
837 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 790 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
838 TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE, func.get());
839 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
840 std::string error = utils::RunFunctionAndReturnError(
841 func.get(), "[{}]", browser());
842 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); 791 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
843 EXPECT_FALSE(func->login_ui_shown()); 792 EXPECT_FALSE(func->login_ui_shown());
844 EXPECT_FALSE(func->scope_ui_shown()); 793 EXPECT_FALSE(func->scope_ui_shown());
845 } 794 }
846 795
847 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 796 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
848 NonInteractiveLoginAccessTokenFailure) { 797 NonInteractiveLoginAccessTokenFailure) {
849 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 798 SignIn("primary@example.com");
799 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
850 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 800 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
851 EXPECT_CALL(*func.get(), HasLoginToken())
852 .WillOnce(Return(true));
853 func->set_login_access_token_result(false); 801 func->set_login_access_token_result(false);
854 std::string error = utils::RunFunctionAndReturnError( 802 std::string error = utils::RunFunctionAndReturnError(
855 func.get(), "[{}]", browser()); 803 func.get(), "[{}]", browser());
856 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); 804 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
857 } 805 }
858 806
859 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 807 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
860 NonInteractiveMintAdviceSuccess) { 808 NonInteractiveMintAdviceSuccess) {
809 SignIn("primary@example.com");
861 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 810 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
862 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 811 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
863 func->set_extension(extension.get()); 812 func->set_extension(extension.get());
864 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 813 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
865 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 814 std::string error =
866 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); 815 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
867 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
868 std::string error = utils::RunFunctionAndReturnError(
869 func.get(), "[{}]", browser());
870 EXPECT_EQ(std::string(errors::kNoGrant), error); 816 EXPECT_EQ(std::string(errors::kNoGrant), error);
871 EXPECT_FALSE(func->login_ui_shown()); 817 EXPECT_FALSE(func->login_ui_shown());
872 EXPECT_FALSE(func->scope_ui_shown()); 818 EXPECT_FALSE(func->scope_ui_shown());
873 819
874 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_ADVICE, 820 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_ADVICE,
875 GetCachedToken(std::string()).status()); 821 GetCachedToken(std::string()).status());
876 } 822 }
877 823
878 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 824 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
879 NonInteractiveMintBadCredentials) { 825 NonInteractiveMintBadCredentials) {
880 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 826 SignIn("primary@example.com");
827 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
881 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 828 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
882 EXPECT_CALL(*func.get(), HasLoginToken()) 829 func->set_mint_token_result(
883 .WillOnce(Return(true)); 830 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS);
884 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 831 std::string error =
885 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get()); 832 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
886 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
887 std::string error = utils::RunFunctionAndReturnError(
888 func.get(), "[{}]", browser());
889 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); 833 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
890 EXPECT_FALSE(func->login_ui_shown()); 834 EXPECT_FALSE(func->login_ui_shown());
891 EXPECT_FALSE(func->scope_ui_shown()); 835 EXPECT_FALSE(func->scope_ui_shown());
892 } 836 }
893 837
894 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 838 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
895 NonInteractiveMintServiceError) { 839 NonInteractiveMintServiceError) {
896 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 840 SignIn("primary@example.com");
841 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
897 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 842 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
898 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 843 func->set_mint_token_result(
899 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 844 TestOAuth2MintTokenFlow::MINT_TOKEN_SERVICE_ERROR);
900 TestOAuth2MintTokenFlow::MINT_TOKEN_SERVICE_ERROR, func.get());
901 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
902 std::string error = 845 std::string error =
903 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser()); 846 utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
904 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); 847 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
905 EXPECT_FALSE(func->login_ui_shown()); 848 EXPECT_FALSE(func->login_ui_shown());
906 EXPECT_FALSE(func->scope_ui_shown()); 849 EXPECT_FALSE(func->scope_ui_shown());
907 } 850 }
908 851
909 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 852 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
910 NoOptionsSuccess) { 853 NoOptionsSuccess) {
854 SignIn("primary@example.com");
911 #if defined(OS_WIN) && defined(USE_ASH) 855 #if defined(OS_WIN) && defined(USE_ASH)
912 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 856 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
913 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 857 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
914 return; 858 return;
915 #endif 859 #endif
916 860
917 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 861 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
918 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 862 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
919 func->set_extension(extension.get()); 863 func->set_extension(extension.get());
920 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 864 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
921 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 865 scoped_ptr<base::Value> value(
922 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); 866 utils::RunFunctionAndReturnSingleResult(func.get(), "[]", browser()));
923 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
924 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
925 func.get(), "[]", browser()));
926 std::string access_token; 867 std::string access_token;
927 EXPECT_TRUE(value->GetAsString(&access_token)); 868 EXPECT_TRUE(value->GetAsString(&access_token));
928 EXPECT_EQ(std::string(kAccessToken), access_token); 869 EXPECT_EQ(std::string(kAccessToken), access_token);
929 EXPECT_FALSE(func->login_ui_shown()); 870 EXPECT_FALSE(func->login_ui_shown());
930 EXPECT_FALSE(func->scope_ui_shown()); 871 EXPECT_FALSE(func->scope_ui_shown());
931 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, 872 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
932 GetCachedToken(std::string()).status()); 873 GetCachedToken(std::string()).status());
933 } 874 }
934 875
935 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 876 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
936 NonInteractiveSuccess) { 877 NonInteractiveSuccess) {
878 SignIn("primary@example.com");
937 #if defined(OS_WIN) && defined(USE_ASH) 879 #if defined(OS_WIN) && defined(USE_ASH)
938 // Disable this test in Metro+Ash for now (http://crbug.com/262796). 880 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
939 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests)) 881 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
940 return; 882 return;
941 #endif 883 #endif
942 884
943 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 885 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
944 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 886 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
945 func->set_extension(extension.get()); 887 func->set_extension(extension.get());
946 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 888 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
947 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
948 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
949 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
950 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 889 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
951 func.get(), "[{}]", browser())); 890 func.get(), "[{}]", browser()));
952 std::string access_token; 891 std::string access_token;
953 EXPECT_TRUE(value->GetAsString(&access_token)); 892 EXPECT_TRUE(value->GetAsString(&access_token));
954 EXPECT_EQ(std::string(kAccessToken), access_token); 893 EXPECT_EQ(std::string(kAccessToken), access_token);
955 EXPECT_FALSE(func->login_ui_shown()); 894 EXPECT_FALSE(func->login_ui_shown());
956 EXPECT_FALSE(func->scope_ui_shown()); 895 EXPECT_FALSE(func->scope_ui_shown());
957 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, 896 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
958 GetCachedToken(std::string()).status()); 897 GetCachedToken(std::string()).status());
959 } 898 }
960 899
961 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 900 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
962 InteractiveLoginCanceled) { 901 InteractiveLoginCanceled) {
963 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 902 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
964 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 903 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
965 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
966 func->set_login_ui_result(false); 904 func->set_login_ui_result(false);
967 std::string error = utils::RunFunctionAndReturnError( 905 std::string error = utils::RunFunctionAndReturnError(
968 func.get(), "[{\"interactive\": true}]", browser()); 906 func.get(), "[{\"interactive\": true}]", browser());
969 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error); 907 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
970 EXPECT_TRUE(func->login_ui_shown()); 908 EXPECT_TRUE(func->login_ui_shown());
971 EXPECT_FALSE(func->scope_ui_shown()); 909 EXPECT_FALSE(func->scope_ui_shown());
972 } 910 }
973 911
974 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 912 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
975 InteractiveMintBadCredentialsLoginCanceled) { 913 InteractiveMintBadCredentialsLoginCanceled) {
976 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 914 SignIn("primary@example.com");
915 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
977 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 916 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
978 EXPECT_CALL(*func.get(), HasLoginToken()) 917 func->set_mint_token_result(
979 .WillOnce(Return(true)); 918 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS);
980 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
981 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get());
982 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
983 func->set_login_ui_result(false); 919 func->set_login_ui_result(false);
984 std::string error = utils::RunFunctionAndReturnError( 920 std::string error = utils::RunFunctionAndReturnError(
985 func.get(), "[{\"interactive\": true}]", browser()); 921 func.get(), "[{\"interactive\": true}]", browser());
986 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error); 922 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
987 EXPECT_TRUE(func->login_ui_shown()); 923 EXPECT_TRUE(func->login_ui_shown());
988 EXPECT_FALSE(func->scope_ui_shown()); 924 EXPECT_FALSE(func->scope_ui_shown());
989 } 925 }
990 926
991 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 927 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
992 InteractiveLoginSuccessNoToken) { 928 InteractiveLoginSuccessNoToken) {
993 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 929 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
994 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 930 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
995 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
996 func->set_login_ui_result(false); 931 func->set_login_ui_result(false);
997 std::string error = utils::RunFunctionAndReturnError( 932 std::string error = utils::RunFunctionAndReturnError(
998 func.get(), "[{\"interactive\": true}]", browser()); 933 func.get(), "[{\"interactive\": true}]", browser());
999 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error); 934 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
1000 EXPECT_TRUE(func->login_ui_shown()); 935 EXPECT_TRUE(func->login_ui_shown());
1001 EXPECT_FALSE(func->scope_ui_shown()); 936 EXPECT_FALSE(func->scope_ui_shown());
1002 } 937 }
1003 938
1004 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 939 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1005 InteractiveLoginSuccessMintFailure) { 940 InteractiveLoginSuccessMintFailure) {
1006 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 941 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1007 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 942 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1008 EXPECT_CALL(*func.get(), HasLoginToken())
1009 .WillOnce(Return(false));
1010 func->set_login_ui_result(true); 943 func->set_login_ui_result(true);
1011 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 944 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE);
1012 TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE, func.get());
1013 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1014 std::string error = utils::RunFunctionAndReturnError( 945 std::string error = utils::RunFunctionAndReturnError(
1015 func.get(), "[{\"interactive\": true}]", browser()); 946 func.get(), "[{\"interactive\": true}]", browser());
1016 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); 947 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
1017 EXPECT_TRUE(func->login_ui_shown()); 948 EXPECT_TRUE(func->login_ui_shown());
1018 EXPECT_FALSE(func->scope_ui_shown()); 949 EXPECT_FALSE(func->scope_ui_shown());
1019 } 950 }
1020 951
1021 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 952 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1022 InteractiveLoginSuccessLoginAccessTokenFailure) { 953 InteractiveLoginSuccessLoginAccessTokenFailure) {
1023 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 954 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1024 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 955 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1025 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
1026 func->set_login_ui_result(true); 956 func->set_login_ui_result(true);
1027 func->set_login_access_token_result(false); 957 func->set_login_access_token_result(false);
1028 std::string error = utils::RunFunctionAndReturnError( 958 std::string error = utils::RunFunctionAndReturnError(
1029 func.get(), "[{\"interactive\": true}]", browser()); 959 func.get(), "[{\"interactive\": true}]", browser());
1030 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); 960 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
1031 EXPECT_TRUE(func->login_ui_shown()); 961 EXPECT_TRUE(func->login_ui_shown());
1032 EXPECT_FALSE(func->scope_ui_shown()); 962 EXPECT_FALSE(func->scope_ui_shown());
1033 } 963 }
1034 964
1035 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 965 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1036 InteractiveLoginSuccessMintSuccess) { 966 InteractiveLoginSuccessMintSuccess) {
1037 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 967 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1038 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 968 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1039 EXPECT_CALL(*func.get(), HasLoginToken())
1040 .WillOnce(Return(false));
1041 func->set_login_ui_result(true); 969 func->set_login_ui_result(true);
1042 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 970 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1043 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
1044 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1045 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 971 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1046 func.get(), "[{\"interactive\": true}]", browser())); 972 func.get(), "[{\"interactive\": true}]", browser()));
1047 std::string access_token; 973 std::string access_token;
1048 EXPECT_TRUE(value->GetAsString(&access_token)); 974 EXPECT_TRUE(value->GetAsString(&access_token));
1049 EXPECT_EQ(std::string(kAccessToken), access_token); 975 EXPECT_EQ(std::string(kAccessToken), access_token);
1050 EXPECT_TRUE(func->login_ui_shown()); 976 EXPECT_TRUE(func->login_ui_shown());
1051 EXPECT_FALSE(func->scope_ui_shown()); 977 EXPECT_FALSE(func->scope_ui_shown());
1052 } 978 }
1053 979
1054 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 980 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1055 InteractiveLoginSuccessApprovalAborted) { 981 InteractiveLoginSuccessApprovalAborted) {
1056 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 982 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1057 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 983 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1058 EXPECT_CALL(*func.get(), HasLoginToken())
1059 .WillOnce(Return(false));
1060 func->set_login_ui_result(true); 984 func->set_login_ui_result(true);
1061 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 985 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1062 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1063 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1064 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED); 986 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED);
1065 std::string error = utils::RunFunctionAndReturnError( 987 std::string error = utils::RunFunctionAndReturnError(
1066 func.get(), "[{\"interactive\": true}]", browser()); 988 func.get(), "[{\"interactive\": true}]", browser());
1067 EXPECT_EQ(std::string(errors::kUserRejected), error); 989 EXPECT_EQ(std::string(errors::kUserRejected), error);
1068 EXPECT_TRUE(func->login_ui_shown()); 990 EXPECT_TRUE(func->login_ui_shown());
1069 EXPECT_TRUE(func->scope_ui_shown()); 991 EXPECT_TRUE(func->scope_ui_shown());
1070 } 992 }
1071 993
1072 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 994 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1073 InteractiveLoginSuccessApprovalSuccess) { 995 InteractiveLoginSuccessApprovalSuccess) {
1074 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 996 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1075 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 997 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1076 func->set_extension(extension.get()); 998 func->set_extension(extension.get());
1077 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false));
1078 func->set_login_ui_result(true); 999 func->set_login_ui_result(true);
1079 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 1000 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1080 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1081 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_))
1082 .WillOnce(Return(flow));
1083 1001
1084 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1002 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1085 func.get(), "[{\"interactive\": true}]", browser())); 1003 func.get(), "[{\"interactive\": true}]", browser()));
1086 std::string access_token; 1004 std::string access_token;
1087 EXPECT_TRUE(value->GetAsString(&access_token)); 1005 EXPECT_TRUE(value->GetAsString(&access_token));
1088 EXPECT_EQ(std::string(kAccessToken), access_token); 1006 EXPECT_EQ(std::string(kAccessToken), access_token);
1089 EXPECT_TRUE(func->login_ui_shown()); 1007 EXPECT_TRUE(func->login_ui_shown());
1090 EXPECT_TRUE(func->scope_ui_shown()); 1008 EXPECT_TRUE(func->scope_ui_shown());
1091 } 1009 }
1092 1010
1093 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1011 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1094 InteractiveApprovalAborted) { 1012 InteractiveApprovalAborted) {
1095 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1013 SignIn("primary@example.com");
1014 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1096 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 1015 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1097 EXPECT_CALL(*func.get(), HasLoginToken()) 1016 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1098 .WillOnce(Return(true));
1099 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1100 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1101 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1102 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED); 1017 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED);
1103 std::string error = utils::RunFunctionAndReturnError( 1018 std::string error = utils::RunFunctionAndReturnError(
1104 func.get(), "[{\"interactive\": true}]", browser()); 1019 func.get(), "[{\"interactive\": true}]", browser());
1105 EXPECT_EQ(std::string(errors::kUserRejected), error); 1020 EXPECT_EQ(std::string(errors::kUserRejected), error);
1106 EXPECT_FALSE(func->login_ui_shown()); 1021 EXPECT_FALSE(func->login_ui_shown());
1107 EXPECT_TRUE(func->scope_ui_shown()); 1022 EXPECT_TRUE(func->scope_ui_shown());
1108 } 1023 }
1109 1024
1110 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1025 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1111 InteractiveApprovalLoadFailed) { 1026 InteractiveApprovalLoadFailed) {
1112 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1027 SignIn("primary@example.com");
1028 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1113 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 1029 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1114 EXPECT_CALL(*func.get(), HasLoginToken()) 1030 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1115 .WillOnce(Return(true));
1116 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1117 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1118 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1119 func->set_scope_ui_failure(GaiaWebAuthFlow::LOAD_FAILED); 1031 func->set_scope_ui_failure(GaiaWebAuthFlow::LOAD_FAILED);
1120 std::string error = utils::RunFunctionAndReturnError( 1032 std::string error = utils::RunFunctionAndReturnError(
1121 func.get(), "[{\"interactive\": true}]", browser()); 1033 func.get(), "[{\"interactive\": true}]", browser());
1122 EXPECT_EQ(std::string(errors::kPageLoadFailure), error); 1034 EXPECT_EQ(std::string(errors::kPageLoadFailure), error);
1123 EXPECT_FALSE(func->login_ui_shown()); 1035 EXPECT_FALSE(func->login_ui_shown());
1124 EXPECT_TRUE(func->scope_ui_shown()); 1036 EXPECT_TRUE(func->scope_ui_shown());
1125 } 1037 }
1126 1038
1127 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1039 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1128 InteractiveApprovalInvalidRedirect) { 1040 InteractiveApprovalInvalidRedirect) {
1129 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1041 SignIn("primary@example.com");
1042 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1130 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 1043 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1131 EXPECT_CALL(*func.get(), HasLoginToken()) 1044 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1132 .WillOnce(Return(true));
1133 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1134 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1135 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1136 func->set_scope_ui_failure(GaiaWebAuthFlow::INVALID_REDIRECT); 1045 func->set_scope_ui_failure(GaiaWebAuthFlow::INVALID_REDIRECT);
1137 std::string error = utils::RunFunctionAndReturnError( 1046 std::string error = utils::RunFunctionAndReturnError(
1138 func.get(), "[{\"interactive\": true}]", browser()); 1047 func.get(), "[{\"interactive\": true}]", browser());
1139 EXPECT_EQ(std::string(errors::kInvalidRedirect), error); 1048 EXPECT_EQ(std::string(errors::kInvalidRedirect), error);
1140 EXPECT_FALSE(func->login_ui_shown()); 1049 EXPECT_FALSE(func->login_ui_shown());
1141 EXPECT_TRUE(func->scope_ui_shown()); 1050 EXPECT_TRUE(func->scope_ui_shown());
1142 } 1051 }
1143 1052
1144 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1053 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1145 InteractiveApprovalConnectionFailure) { 1054 InteractiveApprovalConnectionFailure) {
1146 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1055 SignIn("primary@example.com");
1056 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1147 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); 1057 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1148 EXPECT_CALL(*func.get(), HasLoginToken()) 1058 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1149 .WillOnce(Return(true));
1150 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1151 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1152 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1153 func->set_scope_ui_failure(GaiaWebAuthFlow::SERVICE_AUTH_ERROR); 1059 func->set_scope_ui_failure(GaiaWebAuthFlow::SERVICE_AUTH_ERROR);
1154 std::string error = utils::RunFunctionAndReturnError( 1060 std::string error = utils::RunFunctionAndReturnError(
1155 func.get(), "[{\"interactive\": true}]", browser()); 1061 func.get(), "[{\"interactive\": true}]", browser());
1156 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false)); 1062 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
1157 EXPECT_FALSE(func->login_ui_shown()); 1063 EXPECT_FALSE(func->login_ui_shown());
1158 EXPECT_TRUE(func->scope_ui_shown()); 1064 EXPECT_TRUE(func->scope_ui_shown());
1159 } 1065 }
1160 1066
1161 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1067 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1162 InteractiveApprovalOAuthErrors) { 1068 InteractiveApprovalOAuthErrors) {
1069 SignIn("primary@example.com");
1163 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1070 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1164 1071
1165 std::map<std::string, std::string> error_map; 1072 std::map<std::string, std::string> error_map;
1166 error_map.insert(std::make_pair("access_denied", errors::kUserRejected)); 1073 error_map.insert(std::make_pair("access_denied", errors::kUserRejected));
1167 error_map.insert(std::make_pair("invalid_scope", errors::kInvalidScopes)); 1074 error_map.insert(std::make_pair("invalid_scope", errors::kInvalidScopes));
1168 error_map.insert(std::make_pair( 1075 error_map.insert(std::make_pair(
1169 "unmapped_error", std::string(errors::kAuthFailure) + "unmapped_error")); 1076 "unmapped_error", std::string(errors::kAuthFailure) + "unmapped_error"));
1170 1077
1171 for (std::map<std::string, std::string>::const_iterator 1078 for (std::map<std::string, std::string>::const_iterator
1172 it = error_map.begin(); 1079 it = error_map.begin();
1173 it != error_map.end(); 1080 it != error_map.end();
1174 ++it) { 1081 ++it) {
1175 scoped_refptr<MockGetAuthTokenFunction> func( 1082 scoped_refptr<FakeGetAuthTokenFunction> func(
1176 new MockGetAuthTokenFunction()); 1083 new FakeGetAuthTokenFunction());
1177 func->set_extension(extension.get()); 1084 func->set_extension(extension.get());
1178 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true));
1179 // Make sure we don't get a cached issue_advice result, which would cause 1085 // Make sure we don't get a cached issue_advice result, which would cause
1180 // flow to be leaked. 1086 // flow to be leaked.
1181 id_api()->EraseAllCachedTokens(); 1087 id_api()->EraseAllCachedTokens();
1182 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 1088 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1183 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1184 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1185 func->set_scope_ui_oauth_error(it->first); 1089 func->set_scope_ui_oauth_error(it->first);
1186 std::string error = utils::RunFunctionAndReturnError( 1090 std::string error = utils::RunFunctionAndReturnError(
1187 func.get(), "[{\"interactive\": true}]", browser()); 1091 func.get(), "[{\"interactive\": true}]", browser());
1188 EXPECT_EQ(it->second, error); 1092 EXPECT_EQ(it->second, error);
1189 EXPECT_FALSE(func->login_ui_shown()); 1093 EXPECT_FALSE(func->login_ui_shown());
1190 EXPECT_TRUE(func->scope_ui_shown()); 1094 EXPECT_TRUE(func->scope_ui_shown());
1191 } 1095 }
1192 } 1096 }
1193 1097
1194 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1098 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1195 InteractiveApprovalSuccess) { 1099 InteractiveApprovalSuccess) {
1100 SignIn("primary@example.com");
1196 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1101 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1197 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1102 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1198 func->set_extension(extension.get()); 1103 func->set_extension(extension.get());
1199 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1104 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1200 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1201 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1202 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_))
1203 .WillOnce(Return(flow));
1204 1105
1205 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1106 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1206 func.get(), "[{\"interactive\": true}]", browser())); 1107 func.get(), "[{\"interactive\": true}]", browser()));
1207 std::string access_token; 1108 std::string access_token;
1208 EXPECT_TRUE(value->GetAsString(&access_token)); 1109 EXPECT_TRUE(value->GetAsString(&access_token));
1209 EXPECT_EQ(std::string(kAccessToken), access_token); 1110 EXPECT_EQ(std::string(kAccessToken), access_token);
1210 EXPECT_FALSE(func->login_ui_shown()); 1111 EXPECT_FALSE(func->login_ui_shown());
1211 EXPECT_TRUE(func->scope_ui_shown()); 1112 EXPECT_TRUE(func->scope_ui_shown());
1212 1113
1213 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, 1114 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
1214 GetCachedToken(std::string()).status()); 1115 GetCachedToken(std::string()).status());
1215 } 1116 }
1216 1117
1217 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveQueue) { 1118 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveQueue) {
1119 SignIn("primary@example.com");
1218 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1120 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1219 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1121 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1220 func->set_extension(extension.get()); 1122 func->set_extension(extension.get());
1221 1123
1222 // Create a fake request to block the queue. 1124 // Create a fake request to block the queue.
1223 MockQueuedMintRequest queued_request; 1125 MockQueuedMintRequest queued_request;
1224 IdentityMintRequestQueue::MintType type = 1126 IdentityMintRequestQueue::MintType type =
1225 IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE; 1127 IdentityMintRequestQueue::MINT_TYPE_NONINTERACTIVE;
1226 1128
1227 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1); 1129 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1);
1228 QueueRequestStart(type, &queued_request); 1130 QueueRequestStart(type, &queued_request);
1229 1131
1230 // The real request will start processing, but wait in the queue behind 1132 // The real request will start processing, but wait in the queue behind
1231 // the blocker. 1133 // the blocker.
1232 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true));
1233 RunFunctionAsync(func.get(), "[{}]"); 1134 RunFunctionAsync(func.get(), "[{}]");
1234 // Verify that we have fetched the login token at this point. 1135 // Verify that we have fetched the login token at this point.
1235 testing::Mock::VerifyAndClearExpectations(func.get()); 1136 testing::Mock::VerifyAndClearExpectations(func.get());
1236 1137
1237 // The flow will be created after the first queued request clears. 1138 // The flow will be created after the first queued request clears.
1238 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 1139 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1239 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
1240 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1241 1140
1242 QueueRequestComplete(type, &queued_request); 1141 QueueRequestComplete(type, &queued_request);
1243 1142
1244 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); 1143 scoped_ptr<base::Value> value(WaitForSingleResult(func.get()));
1245 std::string access_token; 1144 std::string access_token;
1246 EXPECT_TRUE(value->GetAsString(&access_token)); 1145 EXPECT_TRUE(value->GetAsString(&access_token));
1247 EXPECT_EQ(std::string(kAccessToken), access_token); 1146 EXPECT_EQ(std::string(kAccessToken), access_token);
1248 EXPECT_FALSE(func->login_ui_shown()); 1147 EXPECT_FALSE(func->login_ui_shown());
1249 EXPECT_FALSE(func->scope_ui_shown()); 1148 EXPECT_FALSE(func->scope_ui_shown());
1250 } 1149 }
1251 1150
1252 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, InteractiveQueue) { 1151 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, InteractiveQueue) {
1152 SignIn("primary@example.com");
1253 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1153 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1254 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1154 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1255 func->set_extension(extension.get()); 1155 func->set_extension(extension.get());
1256 1156
1257 // Create a fake request to block the queue. 1157 // Create a fake request to block the queue.
1258 MockQueuedMintRequest queued_request; 1158 MockQueuedMintRequest queued_request;
1259 IdentityMintRequestQueue::MintType type = 1159 IdentityMintRequestQueue::MintType type =
1260 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE; 1160 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE;
1261 1161
1262 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1); 1162 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1);
1263 QueueRequestStart(type, &queued_request); 1163 QueueRequestStart(type, &queued_request);
1264 1164
1265 // The real request will start processing, but wait in the queue behind 1165 // The real request will start processing, but wait in the queue behind
1266 // the blocker. 1166 // the blocker.
1267 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1167 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1268 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow(
1269 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1270 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow1));
1271 RunFunctionAsync(func.get(), "[{\"interactive\": true}]"); 1168 RunFunctionAsync(func.get(), "[{\"interactive\": true}]");
1272 // Verify that we have fetched the login token and run the first flow. 1169 // Verify that we have fetched the login token and run the first flow.
1273 testing::Mock::VerifyAndClearExpectations(func.get()); 1170 testing::Mock::VerifyAndClearExpectations(func.get());
1274 EXPECT_FALSE(func->scope_ui_shown()); 1171 EXPECT_FALSE(func->scope_ui_shown());
1275 1172
1276 // The UI will be displayed and a token retrieved after the first 1173 // The UI will be displayed and a token retrieved after the first
1277 // queued request clears. 1174 // queued request clears.
1278 QueueRequestComplete(type, &queued_request); 1175 QueueRequestComplete(type, &queued_request);
1279 1176
1280 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); 1177 scoped_ptr<base::Value> value(WaitForSingleResult(func.get()));
1281 std::string access_token; 1178 std::string access_token;
1282 EXPECT_TRUE(value->GetAsString(&access_token)); 1179 EXPECT_TRUE(value->GetAsString(&access_token));
1283 EXPECT_EQ(std::string(kAccessToken), access_token); 1180 EXPECT_EQ(std::string(kAccessToken), access_token);
1284 EXPECT_FALSE(func->login_ui_shown()); 1181 EXPECT_FALSE(func->login_ui_shown());
1285 EXPECT_TRUE(func->scope_ui_shown()); 1182 EXPECT_TRUE(func->scope_ui_shown());
1286 } 1183 }
1287 1184
1288 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, InteractiveQueueShutdown) { 1185 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, InteractiveQueueShutdown) {
1186 SignIn("primary@example.com");
1289 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1187 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1290 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1188 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1291 func->set_extension(extension.get()); 1189 func->set_extension(extension.get());
1292 1190
1293 // Create a fake request to block the queue. 1191 // Create a fake request to block the queue.
1294 MockQueuedMintRequest queued_request; 1192 MockQueuedMintRequest queued_request;
1295 IdentityMintRequestQueue::MintType type = 1193 IdentityMintRequestQueue::MintType type =
1296 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE; 1194 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE;
1297 1195
1298 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1); 1196 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1);
1299 QueueRequestStart(type, &queued_request); 1197 QueueRequestStart(type, &queued_request);
1300 1198
1301 // The real request will start processing, but wait in the queue behind 1199 // The real request will start processing, but wait in the queue behind
1302 // the blocker. 1200 // the blocker.
1303 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1201 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1304 TestOAuth2MintTokenFlow* flow1 = new TestOAuth2MintTokenFlow(
1305 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1306 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow1));
1307 RunFunctionAsync(func.get(), "[{\"interactive\": true}]"); 1202 RunFunctionAsync(func.get(), "[{\"interactive\": true}]");
1308 // Verify that we have fetched the login token and run the first flow. 1203 // Verify that we have fetched the login token and run the first flow.
1309 testing::Mock::VerifyAndClearExpectations(func.get()); 1204 testing::Mock::VerifyAndClearExpectations(func.get());
1310 EXPECT_FALSE(func->scope_ui_shown()); 1205 EXPECT_FALSE(func->scope_ui_shown());
1311 1206
1312 // After the request is canceled, the function will complete. 1207 // After the request is canceled, the function will complete.
1313 func->OnShutdown(); 1208 func->OnShutdown();
1314 EXPECT_EQ(std::string(errors::kCanceled), WaitForError(func.get())); 1209 EXPECT_EQ(std::string(errors::kCanceled), WaitForError(func.get()));
1315 EXPECT_FALSE(func->login_ui_shown()); 1210 EXPECT_FALSE(func->login_ui_shown());
1316 EXPECT_FALSE(func->scope_ui_shown()); 1211 EXPECT_FALSE(func->scope_ui_shown());
1317 1212
1318 QueueRequestComplete(type, &queued_request); 1213 QueueRequestComplete(type, &queued_request);
1319 } 1214 }
1320 1215
1321 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveShutdown) { 1216 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveShutdown) {
1217 SignIn("primary@example.com");
1322 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1218 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1323 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1219 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1324 func->set_extension(extension.get()); 1220 func->set_extension(extension.get());
1325 1221
1326 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1222 scoped_ptr<TestHangOAuth2MintTokenFlow> flow(
1327 TestHangOAuth2MintTokenFlow* flow = new TestHangOAuth2MintTokenFlow(); 1223 new TestHangOAuth2MintTokenFlow());
1328 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); 1224 func->set_mint_token_flow(flow.PassAs<OAuth2MintTokenFlow>());
1329 RunFunctionAsync(func.get(), "[{\"interactive\": false}]"); 1225 RunFunctionAsync(func.get(), "[{\"interactive\": false}]");
1330 1226
1331 // After the request is canceled, the function will complete. 1227 // After the request is canceled, the function will complete.
1332 func->OnShutdown(); 1228 func->OnShutdown();
1333 EXPECT_EQ(std::string(errors::kCanceled), WaitForError(func.get())); 1229 EXPECT_EQ(std::string(errors::kCanceled), WaitForError(func.get()));
1334 } 1230 }
1335 1231
1336 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1232 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1337 InteractiveQueuedNoninteractiveFails) { 1233 InteractiveQueuedNoninteractiveFails) {
1234 SignIn("primary@example.com");
1338 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1235 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1339 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1236 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1340 func->set_extension(extension.get()); 1237 func->set_extension(extension.get());
1341 1238
1342 // Create a fake request to block the interactive queue. 1239 // Create a fake request to block the interactive queue.
1343 MockQueuedMintRequest queued_request; 1240 MockQueuedMintRequest queued_request;
1344 IdentityMintRequestQueue::MintType type = 1241 IdentityMintRequestQueue::MintType type =
1345 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE; 1242 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE;
1346 1243
1347 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1); 1244 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1);
1348 QueueRequestStart(type, &queued_request); 1245 QueueRequestStart(type, &queued_request);
1349 1246
1350 // Non-interactive requests fail without hitting GAIA, because a 1247 // Non-interactive requests fail without hitting GAIA, because a
1351 // consent UI is known to be up. 1248 // consent UI is known to be up.
1352 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true));
1353 std::string error = utils::RunFunctionAndReturnError( 1249 std::string error = utils::RunFunctionAndReturnError(
1354 func.get(), "[{}]", browser()); 1250 func.get(), "[{}]", browser());
1355 EXPECT_EQ(std::string(errors::kNoGrant), error); 1251 EXPECT_EQ(std::string(errors::kNoGrant), error);
1356 EXPECT_FALSE(func->login_ui_shown()); 1252 EXPECT_FALSE(func->login_ui_shown());
1357 EXPECT_FALSE(func->scope_ui_shown()); 1253 EXPECT_FALSE(func->scope_ui_shown());
1358 1254
1359 QueueRequestComplete(type, &queued_request); 1255 QueueRequestComplete(type, &queued_request);
1360 } 1256 }
1361 1257
1362 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1258 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1363 NonInteractiveCacheHit) { 1259 NonInteractiveCacheHit) {
1260 SignIn("primary@example.com");
1364 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1261 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1365 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1262 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1366 func->set_extension(extension.get()); 1263 func->set_extension(extension.get());
1367 1264
1368 // pre-populate the cache with a token 1265 // pre-populate the cache with a token
1369 IdentityTokenCacheValue token(kAccessToken, 1266 IdentityTokenCacheValue token(kAccessToken,
1370 base::TimeDelta::FromSeconds(3600)); 1267 base::TimeDelta::FromSeconds(3600));
1371 SetCachedToken(token); 1268 SetCachedToken(token);
1372 1269
1373 // Get a token. Should not require a GAIA request. 1270 // Get a token. Should not require a GAIA request.
1374 EXPECT_CALL(*func.get(), HasLoginToken())
1375 .WillOnce(Return(true));
1376 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1271 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1377 func.get(), "[{}]", browser())); 1272 func.get(), "[{}]", browser()));
1378 std::string access_token; 1273 std::string access_token;
1379 EXPECT_TRUE(value->GetAsString(&access_token)); 1274 EXPECT_TRUE(value->GetAsString(&access_token));
1380 EXPECT_EQ(std::string(kAccessToken), access_token); 1275 EXPECT_EQ(std::string(kAccessToken), access_token);
1381 EXPECT_FALSE(func->login_ui_shown()); 1276 EXPECT_FALSE(func->login_ui_shown());
1382 EXPECT_FALSE(func->scope_ui_shown()); 1277 EXPECT_FALSE(func->scope_ui_shown());
1383 } 1278 }
1384 1279
1385 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1280 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1386 NonInteractiveIssueAdviceCacheHit) { 1281 NonInteractiveIssueAdviceCacheHit) {
1282 SignIn("primary@example.com");
1387 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1283 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1388 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1284 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1389 func->set_extension(extension.get()); 1285 func->set_extension(extension.get());
1390 1286
1391 // pre-populate the cache with advice 1287 // pre-populate the cache with advice
1392 IssueAdviceInfo info; 1288 IssueAdviceInfo info;
1393 IdentityTokenCacheValue token(info); 1289 IdentityTokenCacheValue token(info);
1394 SetCachedToken(token); 1290 SetCachedToken(token);
1395 1291
1396 // Should return an error without a GAIA request. 1292 // Should return an error without a GAIA request.
1397 EXPECT_CALL(*func.get(), HasLoginToken())
1398 .WillOnce(Return(true));
1399 std::string error = utils::RunFunctionAndReturnError( 1293 std::string error = utils::RunFunctionAndReturnError(
1400 func.get(), "[{}]", browser()); 1294 func.get(), "[{}]", browser());
1401 EXPECT_EQ(std::string(errors::kNoGrant), error); 1295 EXPECT_EQ(std::string(errors::kNoGrant), error);
1402 EXPECT_FALSE(func->login_ui_shown()); 1296 EXPECT_FALSE(func->login_ui_shown());
1403 EXPECT_FALSE(func->scope_ui_shown()); 1297 EXPECT_FALSE(func->scope_ui_shown());
1404 } 1298 }
1405 1299
1406 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1300 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1407 InteractiveCacheHit) { 1301 InteractiveCacheHit) {
1302 SignIn("primary@example.com");
1408 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1303 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1409 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1304 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1410 func->set_extension(extension.get()); 1305 func->set_extension(extension.get());
1411 1306
1412 // Create a fake request to block the queue. 1307 // Create a fake request to block the queue.
1413 MockQueuedMintRequest queued_request; 1308 MockQueuedMintRequest queued_request;
1414 IdentityMintRequestQueue::MintType type = 1309 IdentityMintRequestQueue::MintType type =
1415 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE; 1310 IdentityMintRequestQueue::MINT_TYPE_INTERACTIVE;
1416 1311
1417 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1); 1312 EXPECT_CALL(queued_request, StartMintToken(type)).Times(1);
1418 QueueRequestStart(type, &queued_request); 1313 QueueRequestStart(type, &queued_request);
1419 1314
1420 // The real request will start processing, but wait in the queue behind 1315 // The real request will start processing, but wait in the queue behind
1421 // the blocker. 1316 // the blocker.
1422 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1317 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1423 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1424 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1425 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1426 RunFunctionAsync(func.get(), "[{\"interactive\": true}]"); 1318 RunFunctionAsync(func.get(), "[{\"interactive\": true}]");
1427 1319
1428 // Populate the cache with a token while the request is blocked. 1320 // Populate the cache with a token while the request is blocked.
1429 IdentityTokenCacheValue token(kAccessToken, 1321 IdentityTokenCacheValue token(kAccessToken,
1430 base::TimeDelta::FromSeconds(3600)); 1322 base::TimeDelta::FromSeconds(3600));
1431 SetCachedToken(token); 1323 SetCachedToken(token);
1432 1324
1433 // When we wake up the request, it returns the cached token without 1325 // When we wake up the request, it returns the cached token without
1434 // displaying a UI, or hitting GAIA. 1326 // displaying a UI, or hitting GAIA.
1435 1327
1436 QueueRequestComplete(type, &queued_request); 1328 QueueRequestComplete(type, &queued_request);
1437 1329
1438 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); 1330 scoped_ptr<base::Value> value(WaitForSingleResult(func.get()));
1439 std::string access_token; 1331 std::string access_token;
1440 EXPECT_TRUE(value->GetAsString(&access_token)); 1332 EXPECT_TRUE(value->GetAsString(&access_token));
1441 EXPECT_EQ(std::string(kAccessToken), access_token); 1333 EXPECT_EQ(std::string(kAccessToken), access_token);
1442 EXPECT_FALSE(func->login_ui_shown()); 1334 EXPECT_FALSE(func->login_ui_shown());
1443 EXPECT_FALSE(func->scope_ui_shown()); 1335 EXPECT_FALSE(func->scope_ui_shown());
1444 } 1336 }
1445 1337
1446 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 1338 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1447 LoginInvalidatesTokenCache) { 1339 LoginInvalidatesTokenCache) {
1448 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1340 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1449 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1341 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1450 func->set_extension(extension.get()); 1342 func->set_extension(extension.get());
1451 1343
1452 // pre-populate the cache with a token 1344 // pre-populate the cache with a token
1453 IdentityTokenCacheValue token(kAccessToken, 1345 IdentityTokenCacheValue token(kAccessToken,
1454 base::TimeDelta::FromSeconds(3600)); 1346 base::TimeDelta::FromSeconds(3600));
1455 SetCachedToken(token); 1347 SetCachedToken(token);
1456 1348
1457 // Because the user is not signed in, the token will be removed, 1349 // Because the user is not signed in, the token will be removed,
1458 // and we'll hit GAIA for new tokens. 1350 // and we'll hit GAIA for new tokens.
1459 EXPECT_CALL(*func.get(), HasLoginToken())
1460 .WillOnce(Return(false));
1461 func->set_login_ui_result(true); 1351 func->set_login_ui_result(true);
1462 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( 1352 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1463 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get());
1464 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_))
1465 .WillOnce(Return(flow));
1466 1353
1467 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1354 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1468 func.get(), "[{\"interactive\": true}]", browser())); 1355 func.get(), "[{\"interactive\": true}]", browser()));
1469 std::string access_token; 1356 std::string access_token;
1470 EXPECT_TRUE(value->GetAsString(&access_token)); 1357 EXPECT_TRUE(value->GetAsString(&access_token));
1471 EXPECT_EQ(std::string(kAccessToken), access_token); 1358 EXPECT_EQ(std::string(kAccessToken), access_token);
1472 EXPECT_TRUE(func->login_ui_shown()); 1359 EXPECT_TRUE(func->login_ui_shown());
1473 EXPECT_TRUE(func->scope_ui_shown()); 1360 EXPECT_TRUE(func->scope_ui_shown());
1474 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, 1361 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
1475 GetCachedToken(std::string()).status()); 1362 GetCachedToken(std::string()).status());
1476 } 1363 }
1477 1364
1478 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithChromeClientId) { 1365 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithChromeClientId) {
1479 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1366 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1480 scoped_refptr<const Extension> extension( 1367 scoped_refptr<const Extension> extension(
1481 CreateExtension(SCOPES | AS_COMPONENT)); 1368 CreateExtension(SCOPES | AS_COMPONENT));
1482 func->set_extension(extension.get()); 1369 func->set_extension(extension.get());
1483 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension.get()); 1370 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension.get());
1484 EXPECT_TRUE(oauth2_info.client_id.empty()); 1371 EXPECT_TRUE(oauth2_info.client_id.empty());
1485 EXPECT_FALSE(func->GetOAuth2ClientId().empty()); 1372 EXPECT_FALSE(func->GetOAuth2ClientId().empty());
1486 EXPECT_NE("client1", func->GetOAuth2ClientId()); 1373 EXPECT_NE("client1", func->GetOAuth2ClientId());
1487 } 1374 }
1488 1375
1489 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithNormalClientId) { 1376 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithNormalClientId) {
1490 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1377 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1491 scoped_refptr<const Extension> extension( 1378 scoped_refptr<const Extension> extension(
1492 CreateExtension(CLIENT_ID | SCOPES | AS_COMPONENT)); 1379 CreateExtension(CLIENT_ID | SCOPES | AS_COMPONENT));
1493 func->set_extension(extension.get()); 1380 func->set_extension(extension.get());
1494 EXPECT_EQ("client1", func->GetOAuth2ClientId()); 1381 EXPECT_EQ("client1", func->GetOAuth2ClientId());
1495 } 1382 }
1496 1383
1497 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiDefaultUser) { 1384 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiDefaultUser) {
1498 SignIn("primary@example.com"); 1385 SignIn("primary@example.com");
1499 token_service_->IssueRefreshTokenForUser("primary@example.com",
1500 "refresh_token");
1501 SetAccountState(CreateIds("primary@example.com", "1"), true); 1386 SetAccountState(CreateIds("primary@example.com", "1"), true);
1502 SetAccountState(CreateIds("secondary@example.com", "2"), true); 1387 SetAccountState(CreateIds("secondary@example.com", "2"), true);
1503 1388
1504 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction()); 1389 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1505 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1390 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1506 func->set_extension(extension.get()); 1391 func->set_extension(extension.get());
1507 func->set_mint_token_flow( 1392 func->set_auto_login_access_token(false);
1508 scoped_ptr<OAuth2MintTokenFlow>(new TestOAuth2MintTokenFlow( 1393 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1509 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get())));
1510 1394
1511 RunFunctionAsync(func.get(), "[{}]"); 1395 RunFunctionAsync(func.get(), "[{}]");
1512 1396
1513 token_service_->IssueAllTokensForAccount( 1397 IssueLoginAccessTokenForAccount("primary@example.com");
1514 "primary@example.com",
1515 "access_token-primary@example.com",
1516 base::Time::Now() + base::TimeDelta::FromSeconds(3600));
1517 1398
1518 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); 1399 scoped_ptr<base::Value> value(WaitForSingleResult(func.get()));
1519 std::string access_token; 1400 std::string access_token;
1520 EXPECT_TRUE(value->GetAsString(&access_token)); 1401 EXPECT_TRUE(value->GetAsString(&access_token));
1521 EXPECT_EQ(std::string(kAccessToken), access_token); 1402 EXPECT_EQ(std::string(kAccessToken), access_token);
1522 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, 1403 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
1523 GetCachedToken(std::string()).status()); 1404 GetCachedToken(std::string()).status());
1524 EXPECT_EQ("access_token-primary@example.com", func->login_access_token()); 1405 EXPECT_EQ("access_token-primary@example.com", func->login_access_token());
1525 } 1406 }
1526 1407
1527 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiPrimaryUser) { 1408 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiPrimaryUser) {
1528 SignIn("primary@example.com"); 1409 SignIn("primary@example.com");
1529 token_service_->IssueRefreshTokenForUser("primary@example.com", 1410 IssueLoginRefreshTokenForAccount("secondary@example.com");
1530 "refresh_token");
1531 SetAccountState(CreateIds("primary@example.com", "1"), true); 1411 SetAccountState(CreateIds("primary@example.com", "1"), true);
1532 SetAccountState(CreateIds("secondary@example.com", "2"), true); 1412 SetAccountState(CreateIds("secondary@example.com", "2"), true);
1533 1413
1534 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction()); 1414 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1535 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1415 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1536 func->set_extension(extension.get()); 1416 func->set_extension(extension.get());
1537 func->set_mint_token_flow( 1417 func->set_auto_login_access_token(false);
1538 scoped_ptr<OAuth2MintTokenFlow>(new TestOAuth2MintTokenFlow( 1418 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1539 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get())));
1540 1419
1541 RunFunctionAsync(func.get(), "[{\"account\": { \"id\": \"1\" } }]"); 1420 RunFunctionAsync(func.get(), "[{\"account\": { \"id\": \"1\" } }]");
1542 1421
1543 token_service_->IssueAllTokensForAccount( 1422 IssueLoginAccessTokenForAccount("primary@example.com");
1544 "primary@example.com",
1545 "access_token-primary@example.com",
1546 base::Time::Now() + base::TimeDelta::FromSeconds(3600));
1547 1423
1548 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); 1424 scoped_ptr<base::Value> value(WaitForSingleResult(func.get()));
1549 std::string access_token; 1425 std::string access_token;
1550 EXPECT_TRUE(value->GetAsString(&access_token)); 1426 EXPECT_TRUE(value->GetAsString(&access_token));
1551 EXPECT_EQ(std::string(kAccessToken), access_token); 1427 EXPECT_EQ(std::string(kAccessToken), access_token);
1552 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, 1428 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
1553 GetCachedToken(std::string()).status()); 1429 GetCachedToken(std::string()).status());
1554 EXPECT_EQ("access_token-primary@example.com", func->login_access_token()); 1430 EXPECT_EQ("access_token-primary@example.com", func->login_access_token());
1555 } 1431 }
1556 1432
1557 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiSecondaryUser) { 1433 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiSecondaryUser) {
1558 SignIn("primary@example.com"); 1434 SignIn("primary@example.com");
1559 token_service_->IssueRefreshTokenForUser("secondary@example.com", 1435 IssueLoginRefreshTokenForAccount("secondary@example.com");
1560 "refresh_token");
1561 SetAccountState(CreateIds("primary@example.com", "1"), true); 1436 SetAccountState(CreateIds("primary@example.com", "1"), true);
1562 SetAccountState(CreateIds("secondary@example.com", "2"), true); 1437 SetAccountState(CreateIds("secondary@example.com", "2"), true);
1563 1438
1564 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction()); 1439 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1565 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1440 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1566 func->set_extension(extension.get()); 1441 func->set_extension(extension.get());
1567 func->set_mint_token_flow( 1442 func->set_auto_login_access_token(false);
1568 scoped_ptr<OAuth2MintTokenFlow>(new TestOAuth2MintTokenFlow( 1443 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1569 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get())));
1570 1444
1571 RunFunctionAsync(func.get(), "[{\"account\": { \"id\": \"2\" } }]"); 1445 RunFunctionAsync(func.get(), "[{\"account\": { \"id\": \"2\" } }]");
1572 1446
1573 token_service_->IssueAllTokensForAccount( 1447 IssueLoginAccessTokenForAccount("secondary@example.com");
1574 "secondary@example.com",
1575 "access_token-secondary@example.com",
1576 base::Time::Now() + base::TimeDelta::FromSeconds(3600));
1577 1448
1578 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); 1449 scoped_ptr<base::Value> value(WaitForSingleResult(func.get()));
1579 std::string access_token; 1450 std::string access_token;
1580 EXPECT_TRUE(value->GetAsString(&access_token)); 1451 EXPECT_TRUE(value->GetAsString(&access_token));
1581 EXPECT_EQ(std::string(kAccessToken), access_token); 1452 EXPECT_EQ(std::string(kAccessToken), access_token);
1582 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, 1453 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
1583 GetCachedToken("secondary@example.com").status()); 1454 GetCachedToken("secondary@example.com").status());
1584 EXPECT_EQ("access_token-secondary@example.com", func->login_access_token()); 1455 EXPECT_EQ("access_token-secondary@example.com", func->login_access_token());
1585 } 1456 }
1586 1457
1587 // TODO(courage): negative cases for secondary accounts 1458 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiUnknownUser) {
1459 SignIn("primary@example.com");
1460 IssueLoginRefreshTokenForAccount("secondary@example.com");
1461 SetAccountState(CreateIds("primary@example.com", "1"), true);
1462 SetAccountState(CreateIds("secondary@example.com", "2"), true);
1463
1464 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1465 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1466 func->set_extension(extension.get());
1467 func->set_auto_login_access_token(false);
1468
1469 std::string error = utils::RunFunctionAndReturnError(
1470 func.get(), "[{\"account\": { \"id\": \"3\" } }]", browser());
1471 EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
1472 }
1473
1474 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1475 MultiSecondaryNonInteractiveMintFailure) {
1476 SignIn("primary@example.com");
1477 IssueLoginRefreshTokenForAccount("secondary@example.com");
1478 SetAccountState(CreateIds("primary@example.com", "1"), true);
1479 SetAccountState(CreateIds("secondary@example.com", "2"), true);
1480
1481 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1482 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1483 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE);
1484 std::string error = utils::RunFunctionAndReturnError(
1485 func.get(), "[{\"account\": { \"id\": \"2\" } }]", browser());
1486 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
1487 EXPECT_FALSE(func->login_ui_shown());
1488 EXPECT_FALSE(func->scope_ui_shown());
1489 }
1490
1491 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1492 MultiSecondaryNonInteractiveLoginAccessTokenFailure) {
1493 SignIn("primary@example.com");
1494 IssueLoginRefreshTokenForAccount("secondary@example.com");
1495 SetAccountState(CreateIds("primary@example.com", "1"), true);
1496 SetAccountState(CreateIds("secondary@example.com", "2"), true);
1497
1498 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1499 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1500 func->set_login_access_token_result(false);
1501 std::string error = utils::RunFunctionAndReturnError(
1502 func.get(), "[{\"account\": { \"id\": \"2\" } }]", browser());
1503 EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
1504 }
1505
1506 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
1507 MultiSecondaryInteractiveApprovalAborted) {
1508 SignIn("primary@example.com");
1509 IssueLoginRefreshTokenForAccount("secondary@example.com");
1510 SetAccountState(CreateIds("primary@example.com", "1"), true);
1511 SetAccountState(CreateIds("secondary@example.com", "2"), true);
1512
1513 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1514 func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
1515 func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
1516 func->set_scope_ui_failure(GaiaWebAuthFlow::WINDOW_CLOSED);
1517 std::string error = utils::RunFunctionAndReturnError(
1518 func.get(),
1519 "[{\"account\": { \"id\": \"2\" }, \"interactive\": true}]",
1520 browser());
1521 EXPECT_EQ(std::string(errors::kUserRejected), error);
1522 EXPECT_FALSE(func->login_ui_shown());
1523 EXPECT_TRUE(func->scope_ui_shown());
1524 }
1588 1525
1589 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesDefault) { 1526 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesDefault) {
1590 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1527 SignIn("primary@example.com");
1528 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1591 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1529 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1592 func->set_extension(extension.get()); 1530 func->set_extension(extension.get());
1593 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1531 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1594 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1595 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
1596 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1597 scoped_ptr<base::Value> value( 1532 scoped_ptr<base::Value> value(
1598 utils::RunFunctionAndReturnSingleResult(func.get(), "[{}]", browser())); 1533 utils::RunFunctionAndReturnSingleResult(func.get(), "[{}]", browser()));
1599 std::string access_token; 1534 std::string access_token;
1600 EXPECT_TRUE(value->GetAsString(&access_token)); 1535 EXPECT_TRUE(value->GetAsString(&access_token));
1601 EXPECT_EQ(std::string(kAccessToken), access_token); 1536 EXPECT_EQ(std::string(kAccessToken), access_token);
1602 1537
1603 const ExtensionTokenKey* token_key = func->extension_token_key(); 1538 const ExtensionTokenKey* token_key = func->GetExtensionTokenKeyForTest();
1604 EXPECT_EQ(2ul, token_key->scopes.size()); 1539 EXPECT_EQ(2ul, token_key->scopes.size());
1605 EXPECT_TRUE(ContainsKey(token_key->scopes, "scope1")); 1540 EXPECT_TRUE(ContainsKey(token_key->scopes, "scope1"));
1606 EXPECT_TRUE(ContainsKey(token_key->scopes, "scope2")); 1541 EXPECT_TRUE(ContainsKey(token_key->scopes, "scope2"));
1607 } 1542 }
1608 1543
1609 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesEmpty) { 1544 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesEmpty) {
1610 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1545 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1611 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1546 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1612 func->set_extension(extension.get()); 1547 func->set_extension(extension.get());
1613 1548
1614 std::string error(utils::RunFunctionAndReturnError( 1549 std::string error(utils::RunFunctionAndReturnError(
1615 func.get(), "[{\"scopes\": []}]", browser())); 1550 func.get(), "[{\"scopes\": []}]", browser()));
1616 1551
1617 EXPECT_EQ(errors::kInvalidScopes, error); 1552 EXPECT_EQ(errors::kInvalidScopes, error);
1618 } 1553 }
1619 1554
1620 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesEmail) { 1555 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesEmail) {
1621 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1556 SignIn("primary@example.com");
1557 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1622 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1558 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1623 func->set_extension(extension.get()); 1559 func->set_extension(extension.get());
1624 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1560 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1625 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1626 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
1627 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1628 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1561 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1629 func.get(), "[{\"scopes\": [\"email\"]}]", browser())); 1562 func.get(), "[{\"scopes\": [\"email\"]}]", browser()));
1630 std::string access_token; 1563 std::string access_token;
1631 EXPECT_TRUE(value->GetAsString(&access_token)); 1564 EXPECT_TRUE(value->GetAsString(&access_token));
1632 EXPECT_EQ(std::string(kAccessToken), access_token); 1565 EXPECT_EQ(std::string(kAccessToken), access_token);
1633 1566
1634 const ExtensionTokenKey* token_key = func->extension_token_key(); 1567 const ExtensionTokenKey* token_key = func->GetExtensionTokenKeyForTest();
1635 EXPECT_EQ(1ul, token_key->scopes.size()); 1568 EXPECT_EQ(1ul, token_key->scopes.size());
1636 EXPECT_TRUE(ContainsKey(token_key->scopes, "email")); 1569 EXPECT_TRUE(ContainsKey(token_key->scopes, "email"));
1637 } 1570 }
1638 1571
1639 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesEmailFooBar) { 1572 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ScopesEmailFooBar) {
1640 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); 1573 SignIn("primary@example.com");
1574 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
1641 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); 1575 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
1642 func->set_extension(extension.get()); 1576 func->set_extension(extension.get());
1643 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(true)); 1577 func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
1644 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow(
1645 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get());
1646 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow));
1647 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( 1578 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
1648 func.get(), "[{\"scopes\": [\"email\", \"foo\", \"bar\"]}]", browser())); 1579 func.get(), "[{\"scopes\": [\"email\", \"foo\", \"bar\"]}]", browser()));
1649 std::string access_token; 1580 std::string access_token;
1650 EXPECT_TRUE(value->GetAsString(&access_token)); 1581 EXPECT_TRUE(value->GetAsString(&access_token));
1651 EXPECT_EQ(std::string(kAccessToken), access_token); 1582 EXPECT_EQ(std::string(kAccessToken), access_token);
1652 1583
1653 const ExtensionTokenKey* token_key = func->extension_token_key(); 1584 const ExtensionTokenKey* token_key = func->GetExtensionTokenKeyForTest();
1654 EXPECT_EQ(3ul, token_key->scopes.size()); 1585 EXPECT_EQ(3ul, token_key->scopes.size());
1655 EXPECT_TRUE(ContainsKey(token_key->scopes, "email")); 1586 EXPECT_TRUE(ContainsKey(token_key->scopes, "email"));
1656 EXPECT_TRUE(ContainsKey(token_key->scopes, "foo")); 1587 EXPECT_TRUE(ContainsKey(token_key->scopes, "foo"));
1657 EXPECT_TRUE(ContainsKey(token_key->scopes, "bar")); 1588 EXPECT_TRUE(ContainsKey(token_key->scopes, "bar"));
1658 } 1589 }
1659 1590
1660 class RemoveCachedAuthTokenFunctionTest : public ExtensionBrowserTest { 1591 class RemoveCachedAuthTokenFunctionTest : public ExtensionBrowserTest {
1661 protected: 1592 protected:
1662 bool InvalidateDefaultToken() { 1593 bool InvalidateDefaultToken() {
1663 scoped_refptr<IdentityRemoveCachedAuthTokenFunction> func( 1594 scoped_refptr<IdentityRemoveCachedAuthTokenFunction> func(
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
1887 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 1818 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
1888 url); 1819 url);
1889 } 1820 }
1890 1821
1891 } // namespace extensions 1822 } // namespace extensions
1892 1823
1893 // Tests the chrome.identity API implemented by custom JS bindings . 1824 // Tests the chrome.identity API implemented by custom JS bindings .
1894 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) { 1825 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) {
1895 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_; 1826 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_;
1896 } 1827 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/identity/identity_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698