OLD | NEW |
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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/extensions/api/identity/identity_api.h" | 14 #include "chrome/browser/extensions/api/identity/identity_api.h" |
15 #include "chrome/browser/extensions/component_loader.h" | 15 #include "chrome/browser/extensions/component_loader.h" |
16 #include "chrome/browser/extensions/extension_apitest.h" | 16 #include "chrome/browser/extensions/extension_apitest.h" |
17 #include "chrome/browser/extensions/extension_browsertest.h" | 17 #include "chrome/browser/extensions/extension_browsertest.h" |
18 #include "chrome/browser/extensions/extension_function_test_utils.h" | 18 #include "chrome/browser/extensions/extension_function_test_utils.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
20 #include "chrome/browser/guest_view/guest_view_base.h" | 20 #include "chrome/browser/guest_view/guest_view_base.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 23 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| 24 #include "chrome/browser/signin/fake_signin_manager.h" |
| 25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
22 #include "chrome/browser/signin/signin_manager_factory.h" | 26 #include "chrome/browser/signin/signin_manager_factory.h" |
23 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
24 #include "chrome/browser/ui/browser_window.h" | 28 #include "chrome/browser/ui/browser_window.h" |
25 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
26 #include "chrome/common/extensions/api/identity.h" | 30 #include "chrome/common/extensions/api/identity.h" |
27 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" | 31 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" |
28 #include "chrome/test/base/in_process_browser_test.h" | 32 #include "chrome/test/base/in_process_browser_test.h" |
29 #include "chrome/test/base/test_switches.h" | 33 #include "chrome/test/base/test_switches.h" |
30 #include "components/signin/core/browser/signin_manager.h" | 34 #include "components/signin/core/browser/signin_manager.h" |
31 #include "components/signin/core/common/profile_management_switches.h" | 35 #include "components/signin/core/common/profile_management_switches.h" |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 ~MockGetAuthTokenFunction() {} | 344 ~MockGetAuthTokenFunction() {} |
341 bool login_access_token_result_; | 345 bool login_access_token_result_; |
342 bool login_ui_result_; | 346 bool login_ui_result_; |
343 bool scope_ui_result_; | 347 bool scope_ui_result_; |
344 GaiaWebAuthFlow::Failure scope_ui_failure_; | 348 GaiaWebAuthFlow::Failure scope_ui_failure_; |
345 std::string scope_ui_oauth_error_; | 349 std::string scope_ui_oauth_error_; |
346 bool login_ui_shown_; | 350 bool login_ui_shown_; |
347 bool scope_ui_shown_; | 351 bool scope_ui_shown_; |
348 }; | 352 }; |
349 | 353 |
| 354 // TODO(courage): Replace MockGetAuthTokenFunction with |
| 355 // FakeGetAuthTokenFunction in all tests. |
| 356 |
| 357 class FakeGetAuthTokenFunction : public IdentityGetAuthTokenFunction { |
| 358 public: |
| 359 FakeGetAuthTokenFunction() |
| 360 : login_access_token_result_(true), |
| 361 login_ui_result_(true), |
| 362 scope_ui_result_(true), |
| 363 login_ui_shown_(false), |
| 364 scope_ui_shown_(false) {} |
| 365 |
| 366 void set_login_access_token_result(bool result) { |
| 367 login_access_token_result_ = result; |
| 368 } |
| 369 |
| 370 void set_login_ui_result(bool result) { login_ui_result_ = result; } |
| 371 |
| 372 void set_mint_token_flow(scoped_ptr<OAuth2MintTokenFlow> flow) { |
| 373 flow_ = flow.Pass(); |
| 374 } |
| 375 |
| 376 void set_scope_ui_failure(GaiaWebAuthFlow::Failure failure) { |
| 377 scope_ui_result_ = false; |
| 378 scope_ui_failure_ = failure; |
| 379 } |
| 380 |
| 381 void set_scope_ui_oauth_error(const std::string& oauth_error) { |
| 382 scope_ui_result_ = false; |
| 383 scope_ui_failure_ = GaiaWebAuthFlow::OAUTH_ERROR; |
| 384 scope_ui_oauth_error_ = oauth_error; |
| 385 } |
| 386 |
| 387 bool login_ui_shown() const { return login_ui_shown_; } |
| 388 |
| 389 bool scope_ui_shown() const { return scope_ui_shown_; } |
| 390 |
| 391 std::string login_access_token() const { return login_access_token_; } |
| 392 |
| 393 virtual void ShowLoginPopup() OVERRIDE { |
| 394 EXPECT_FALSE(login_ui_shown_); |
| 395 login_ui_shown_ = true; |
| 396 if (login_ui_result_) |
| 397 SigninSuccess(); |
| 398 else |
| 399 SigninFailed(); |
| 400 } |
| 401 |
| 402 virtual void ShowOAuthApprovalDialog( |
| 403 const IssueAdviceInfo& issue_advice) OVERRIDE { |
| 404 scope_ui_shown_ = true; |
| 405 |
| 406 if (scope_ui_result_) { |
| 407 OnGaiaFlowCompleted(kAccessToken, "3600"); |
| 408 } else if (scope_ui_failure_ == GaiaWebAuthFlow::SERVICE_AUTH_ERROR) { |
| 409 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED); |
| 410 OnGaiaFlowFailure(scope_ui_failure_, error, ""); |
| 411 } else { |
| 412 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); |
| 413 OnGaiaFlowFailure(scope_ui_failure_, error, scope_ui_oauth_error_); |
| 414 } |
| 415 } |
| 416 |
| 417 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( |
| 418 const std::string& login_access_token) OVERRIDE { |
| 419 EXPECT_TRUE(login_access_token_.empty()); |
| 420 login_access_token_ = login_access_token; |
| 421 return flow_.release(); |
| 422 } |
| 423 |
| 424 private: |
| 425 virtual ~FakeGetAuthTokenFunction() {} |
| 426 bool login_access_token_result_; |
| 427 bool login_ui_result_; |
| 428 bool scope_ui_result_; |
| 429 GaiaWebAuthFlow::Failure scope_ui_failure_; |
| 430 std::string scope_ui_oauth_error_; |
| 431 bool login_ui_shown_; |
| 432 bool scope_ui_shown_; |
| 433 |
| 434 scoped_ptr<OAuth2MintTokenFlow> flow_; |
| 435 |
| 436 std::string login_access_token_; |
| 437 }; |
| 438 |
350 class MockQueuedMintRequest : public IdentityMintRequestQueue::Request { | 439 class MockQueuedMintRequest : public IdentityMintRequestQueue::Request { |
351 public: | 440 public: |
352 MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType)); | 441 MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType)); |
353 }; | 442 }; |
354 | 443 |
| 444 AccountIds CreateIds(std::string email, std::string obfid) { |
| 445 AccountIds ids; |
| 446 ids.account_key = email; |
| 447 ids.email = email; |
| 448 ids.gaia = obfid; |
| 449 return ids; |
| 450 } |
| 451 |
355 class IdentityGetAccountsFunctionTest : public ExtensionBrowserTest { | 452 class IdentityGetAccountsFunctionTest : public ExtensionBrowserTest { |
356 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 453 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 454 ExtensionBrowserTest::SetUpCommandLine(command_line); |
357 command_line->AppendSwitch(switches::kExtensionsMultiAccount); | 455 command_line->AppendSwitch(switches::kExtensionsMultiAccount); |
358 } | 456 } |
359 | 457 |
360 protected: | 458 protected: |
361 void SetAccountState(AccountIds ids, bool is_signed_in) { | 459 void SetAccountState(AccountIds ids, bool is_signed_in) { |
362 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest( | 460 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest( |
363 ids, is_signed_in); | 461 ids, is_signed_in); |
364 } | 462 } |
365 | 463 |
366 AccountIds CreateIds(std::string email, std::string obfid) { | |
367 AccountIds ids; | |
368 ids.account_key = email; | |
369 ids.email = email; | |
370 ids.gaia = obfid; | |
371 return ids; | |
372 } | |
373 | |
374 testing::AssertionResult ExpectGetAccounts( | 464 testing::AssertionResult ExpectGetAccounts( |
375 const std::vector<std::string>& accounts) { | 465 const std::vector<std::string>& accounts) { |
376 scoped_refptr<IdentityGetAccountsFunction> func( | 466 scoped_refptr<IdentityGetAccountsFunction> func( |
377 new IdentityGetAccountsFunction); | 467 new IdentityGetAccountsFunction); |
378 func->set_extension(utils::CreateEmptyExtension(kExtensionId).get()); | 468 func->set_extension(utils::CreateEmptyExtension(kExtensionId).get()); |
379 if (!utils::RunFunction( | 469 if (!utils::RunFunction( |
380 func.get(), std::string("[]"), browser(), utils::NONE)) { | 470 func.get(), std::string("[]"), browser(), utils::NONE)) { |
381 return GenerateFailureResult(accounts, NULL) | 471 return GenerateFailureResult(accounts, NULL) |
382 << "getAccounts did not return a result."; | 472 << "getAccounts did not return a result."; |
383 } | 473 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest, | 575 IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest, |
486 TwoAccountsSignedIn) { | 576 TwoAccountsSignedIn) { |
487 SetAccountState(CreateIds("primary@example.com", "1"), true); | 577 SetAccountState(CreateIds("primary@example.com", "1"), true); |
488 SetAccountState(CreateIds("secondary@example.com", "2"), true); | 578 SetAccountState(CreateIds("secondary@example.com", "2"), true); |
489 std::vector<std::string> only_primary; | 579 std::vector<std::string> only_primary; |
490 only_primary.push_back("1"); | 580 only_primary.push_back("1"); |
491 EXPECT_TRUE(ExpectGetAccounts(only_primary)); | 581 EXPECT_TRUE(ExpectGetAccounts(only_primary)); |
492 } | 582 } |
493 | 583 |
494 class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest { | 584 class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest { |
| 585 public: |
| 586 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 587 AsyncExtensionBrowserTest::SetUpCommandLine(command_line); |
| 588 command_line->AppendSwitch(switches::kExtensionsMultiAccount); |
| 589 } |
| 590 |
| 591 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 592 AsyncExtensionBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 593 |
| 594 will_create_browser_context_services_subscription_ = |
| 595 BrowserContextDependencyManager::GetInstance() |
| 596 ->RegisterWillCreateBrowserContextServicesCallbackForTesting( |
| 597 base::Bind(&GetAuthTokenFunctionTest:: |
| 598 OnWillCreateBrowserContextServices, |
| 599 base::Unretained(this))) |
| 600 .Pass(); |
| 601 } |
| 602 |
| 603 void OnWillCreateBrowserContextServices(content::BrowserContext* context) { |
| 604 // Replace the signin manager and token service with fakes. Do this ahead of |
| 605 // creating the browser so that a bunch of classes don't register as |
| 606 // observers and end up needing to unregister when the fake is substituted. |
| 607 SigninManagerFactory::GetInstance()->SetTestingFactory( |
| 608 context, &FakeSigninManagerBase::Build); |
| 609 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( |
| 610 context, &BuildFakeProfileOAuth2TokenService); |
| 611 } |
| 612 |
| 613 virtual void SetUpOnMainThread() OVERRIDE { |
| 614 AsyncExtensionBrowserTest::SetUpOnMainThread(); |
| 615 |
| 616 // Grab references to the fake signin manager and token service. |
| 617 signin_manager_ = static_cast<FakeSigninManagerForTesting*>( |
| 618 SigninManagerFactory::GetInstance()->GetForProfile(profile())); |
| 619 ASSERT_TRUE(signin_manager_); |
| 620 token_service_ = static_cast<FakeProfileOAuth2TokenService*>( |
| 621 ProfileOAuth2TokenServiceFactory::GetInstance()->GetForProfile( |
| 622 profile())); |
| 623 ASSERT_TRUE(token_service_); |
| 624 } |
| 625 |
| 626 void SignIn(const std::string account_key) { |
| 627 #if defined(OS_CHROMEOS) |
| 628 signin_manager_->SetAuthenticatedUsername(account_key); |
| 629 #else |
| 630 signin_manager_->SignIn(account_key, "password"); |
| 631 #endif |
| 632 } |
| 633 |
| 634 void SetAccountState(AccountIds ids, bool is_signed_in) { |
| 635 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest( |
| 636 ids, is_signed_in); |
| 637 } |
| 638 |
495 protected: | 639 protected: |
496 enum OAuth2Fields { | 640 enum OAuth2Fields { |
497 NONE = 0, | 641 NONE = 0, |
498 CLIENT_ID = 1, | 642 CLIENT_ID = 1, |
499 SCOPES = 2, | 643 SCOPES = 2, |
500 AS_COMPONENT = 4 | 644 AS_COMPONENT = 4 |
501 }; | 645 }; |
502 | 646 |
| 647 FakeSigninManagerForTesting* signin_manager_; |
| 648 FakeProfileOAuth2TokenService* token_service_; |
| 649 |
503 virtual ~GetAuthTokenFunctionTest() {} | 650 virtual ~GetAuthTokenFunctionTest() {} |
504 | 651 |
505 // Helper to create an extension with specific OAuth2Info fields set. | 652 // Helper to create an extension with specific OAuth2Info fields set. |
506 // |fields_to_set| should be computed by using fields of Oauth2Fields enum. | 653 // |fields_to_set| should be computed by using fields of Oauth2Fields enum. |
507 const Extension* CreateExtension(int fields_to_set) { | 654 const Extension* CreateExtension(int fields_to_set) { |
508 const Extension* ext; | 655 const Extension* ext; |
509 base::FilePath manifest_path = | 656 base::FilePath manifest_path = |
510 test_data_dir_.AppendASCII("platform_apps/oauth2"); | 657 test_data_dir_.AppendASCII("platform_apps/oauth2"); |
511 base::FilePath component_manifest_path = | 658 base::FilePath component_manifest_path = |
512 test_data_dir_.AppendASCII("packaged_app/component_oauth2"); | 659 test_data_dir_.AppendASCII("packaged_app/component_oauth2"); |
(...skipping 24 matching lines...) Expand all Loading... |
537 SigninManagerBase* signin_manager = | 684 SigninManagerBase* signin_manager = |
538 SigninManagerFactory::GetForProfile(browser()->profile()); | 685 SigninManagerFactory::GetForProfile(browser()->profile()); |
539 return signin_manager->GetAuthenticatedAccountId(); | 686 return signin_manager->GetAuthenticatedAccountId(); |
540 } | 687 } |
541 | 688 |
542 void SetCachedToken(const IdentityTokenCacheValue& token_data) { | 689 void SetCachedToken(const IdentityTokenCacheValue& token_data) { |
543 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); | 690 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); |
544 id_api()->SetCachedToken(key, token_data); | 691 id_api()->SetCachedToken(key, token_data); |
545 } | 692 } |
546 | 693 |
547 const IdentityTokenCacheValue& GetCachedToken() { | 694 const IdentityTokenCacheValue& GetCachedToken(std::string account_id) { |
548 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); | 695 if (account_id.empty()) |
| 696 account_id = GetPrimaryAccountId(); |
| 697 ExtensionTokenKey key(extension_id_, account_id, oauth_scopes_); |
549 return id_api()->GetCachedToken(key); | 698 return id_api()->GetCachedToken(key); |
550 } | 699 } |
551 | 700 |
552 void QueueRequestStart(IdentityMintRequestQueue::MintType type, | 701 void QueueRequestStart(IdentityMintRequestQueue::MintType type, |
553 IdentityMintRequestQueue::Request* request) { | 702 IdentityMintRequestQueue::Request* request) { |
554 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); | 703 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); |
555 id_api()->mint_queue()->RequestStart(type, key, request); | 704 id_api()->mint_queue()->RequestStart(type, key, request); |
556 } | 705 } |
557 | 706 |
558 void QueueRequestComplete(IdentityMintRequestQueue::MintType type, | 707 void QueueRequestComplete(IdentityMintRequestQueue::MintType type, |
559 IdentityMintRequestQueue::Request* request) { | 708 IdentityMintRequestQueue::Request* request) { |
560 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); | 709 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); |
561 id_api()->mint_queue()->RequestComplete(type, key, request); | 710 id_api()->mint_queue()->RequestComplete(type, key, request); |
562 } | 711 } |
563 | 712 |
564 private: | 713 private: |
565 std::string extension_id_; | 714 std::string extension_id_; |
566 std::set<std::string> oauth_scopes_; | 715 std::set<std::string> oauth_scopes_; |
| 716 |
| 717 scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription> |
| 718 will_create_browser_context_services_subscription_; |
567 }; | 719 }; |
568 | 720 |
569 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 721 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
570 NoClientId) { | 722 NoClientId) { |
571 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 723 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
572 func->set_extension(CreateExtension(SCOPES)); | 724 func->set_extension(CreateExtension(SCOPES)); |
573 std::string error = utils::RunFunctionAndReturnError( | 725 std::string error = utils::RunFunctionAndReturnError( |
574 func.get(), "[{}]", browser()); | 726 func.get(), "[{}]", browser()); |
575 EXPECT_EQ(std::string(errors::kInvalidClientId), error); | 727 EXPECT_EQ(std::string(errors::kInvalidClientId), error); |
576 EXPECT_FALSE(func->login_ui_shown()); | 728 EXPECT_FALSE(func->login_ui_shown()); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( | 789 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( |
638 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); | 790 TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS, func.get()); |
639 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); | 791 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); |
640 std::string error = utils::RunFunctionAndReturnError( | 792 std::string error = utils::RunFunctionAndReturnError( |
641 func.get(), "[{}]", browser()); | 793 func.get(), "[{}]", browser()); |
642 EXPECT_EQ(std::string(errors::kNoGrant), error); | 794 EXPECT_EQ(std::string(errors::kNoGrant), error); |
643 EXPECT_FALSE(func->login_ui_shown()); | 795 EXPECT_FALSE(func->login_ui_shown()); |
644 EXPECT_FALSE(func->scope_ui_shown()); | 796 EXPECT_FALSE(func->scope_ui_shown()); |
645 | 797 |
646 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_ADVICE, | 798 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_ADVICE, |
647 GetCachedToken().status()); | 799 GetCachedToken(std::string()).status()); |
648 } | 800 } |
649 | 801 |
650 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 802 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
651 NonInteractiveMintBadCredentials) { | 803 NonInteractiveMintBadCredentials) { |
652 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 804 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
653 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); | 805 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); |
654 EXPECT_CALL(*func.get(), HasLoginToken()) | 806 EXPECT_CALL(*func.get(), HasLoginToken()) |
655 .WillOnce(Return(true)); | 807 .WillOnce(Return(true)); |
656 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( | 808 TestOAuth2MintTokenFlow* flow = new TestOAuth2MintTokenFlow( |
657 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get()); | 809 TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS, func.get()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); | 852 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()); |
701 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); | 853 EXPECT_CALL(*func.get(), CreateMintTokenFlow(_)).WillOnce(Return(flow)); |
702 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 854 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
703 func.get(), "[{}]", browser())); | 855 func.get(), "[{}]", browser())); |
704 std::string access_token; | 856 std::string access_token; |
705 EXPECT_TRUE(value->GetAsString(&access_token)); | 857 EXPECT_TRUE(value->GetAsString(&access_token)); |
706 EXPECT_EQ(std::string(kAccessToken), access_token); | 858 EXPECT_EQ(std::string(kAccessToken), access_token); |
707 EXPECT_FALSE(func->login_ui_shown()); | 859 EXPECT_FALSE(func->login_ui_shown()); |
708 EXPECT_FALSE(func->scope_ui_shown()); | 860 EXPECT_FALSE(func->scope_ui_shown()); |
709 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, | 861 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
710 GetCachedToken().status()); | 862 GetCachedToken(std::string()).status()); |
711 } | 863 } |
712 | 864 |
713 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, | 865 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, |
714 InteractiveLoginCanceled) { | 866 InteractiveLoginCanceled) { |
715 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 867 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
716 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); | 868 func->set_extension(CreateExtension(CLIENT_ID | SCOPES)); |
717 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false)); | 869 EXPECT_CALL(*func.get(), HasLoginToken()).WillOnce(Return(false)); |
718 func->set_login_ui_result(false); | 870 func->set_login_ui_result(false); |
719 std::string error = utils::RunFunctionAndReturnError( | 871 std::string error = utils::RunFunctionAndReturnError( |
720 func.get(), "[{\"interactive\": true}]", browser()); | 872 func.get(), "[{\"interactive\": true}]", browser()); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
956 | 1108 |
957 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 1109 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
958 func.get(), "[{\"interactive\": true}]", browser())); | 1110 func.get(), "[{\"interactive\": true}]", browser())); |
959 std::string access_token; | 1111 std::string access_token; |
960 EXPECT_TRUE(value->GetAsString(&access_token)); | 1112 EXPECT_TRUE(value->GetAsString(&access_token)); |
961 EXPECT_EQ(std::string(kAccessToken), access_token); | 1113 EXPECT_EQ(std::string(kAccessToken), access_token); |
962 EXPECT_FALSE(func->login_ui_shown()); | 1114 EXPECT_FALSE(func->login_ui_shown()); |
963 EXPECT_TRUE(func->scope_ui_shown()); | 1115 EXPECT_TRUE(func->scope_ui_shown()); |
964 | 1116 |
965 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, | 1117 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
966 GetCachedToken().status()); | 1118 GetCachedToken(std::string()).status()); |
967 } | 1119 } |
968 | 1120 |
969 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveQueue) { | 1121 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, NoninteractiveQueue) { |
970 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); | 1122 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
971 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 1123 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
972 func->set_extension(extension.get()); | 1124 func->set_extension(extension.get()); |
973 | 1125 |
974 // Create a fake request to block the queue. | 1126 // Create a fake request to block the queue. |
975 MockQueuedMintRequest queued_request; | 1127 MockQueuedMintRequest queued_request; |
976 IdentityMintRequestQueue::MintType type = | 1128 IdentityMintRequestQueue::MintType type = |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1217 .WillOnce(Return(flow)); | 1369 .WillOnce(Return(flow)); |
1218 | 1370 |
1219 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( | 1371 scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult( |
1220 func.get(), "[{\"interactive\": true}]", browser())); | 1372 func.get(), "[{\"interactive\": true}]", browser())); |
1221 std::string access_token; | 1373 std::string access_token; |
1222 EXPECT_TRUE(value->GetAsString(&access_token)); | 1374 EXPECT_TRUE(value->GetAsString(&access_token)); |
1223 EXPECT_EQ(std::string(kAccessToken), access_token); | 1375 EXPECT_EQ(std::string(kAccessToken), access_token); |
1224 EXPECT_TRUE(func->login_ui_shown()); | 1376 EXPECT_TRUE(func->login_ui_shown()); |
1225 EXPECT_TRUE(func->scope_ui_shown()); | 1377 EXPECT_TRUE(func->scope_ui_shown()); |
1226 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, | 1378 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
1227 GetCachedToken().status()); | 1379 GetCachedToken(std::string()).status()); |
1228 } | 1380 } |
1229 | 1381 |
1230 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithChromeClientId) { | 1382 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithChromeClientId) { |
1231 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 1383 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
1232 scoped_refptr<const Extension> extension( | 1384 scoped_refptr<const Extension> extension( |
1233 CreateExtension(SCOPES | AS_COMPONENT)); | 1385 CreateExtension(SCOPES | AS_COMPONENT)); |
1234 func->set_extension(extension.get()); | 1386 func->set_extension(extension.get()); |
1235 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension.get()); | 1387 const OAuth2Info& oauth2_info = OAuth2Info::GetOAuth2Info(extension.get()); |
1236 EXPECT_TRUE(oauth2_info.client_id.empty()); | 1388 EXPECT_TRUE(oauth2_info.client_id.empty()); |
1237 EXPECT_FALSE(func->GetOAuth2ClientId().empty()); | 1389 EXPECT_FALSE(func->GetOAuth2ClientId().empty()); |
1238 EXPECT_NE("client1", func->GetOAuth2ClientId()); | 1390 EXPECT_NE("client1", func->GetOAuth2ClientId()); |
1239 } | 1391 } |
1240 | 1392 |
1241 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithNormalClientId) { | 1393 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, ComponentWithNormalClientId) { |
1242 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); | 1394 scoped_refptr<MockGetAuthTokenFunction> func(new MockGetAuthTokenFunction()); |
1243 scoped_refptr<const Extension> extension( | 1395 scoped_refptr<const Extension> extension( |
1244 CreateExtension(CLIENT_ID | SCOPES | AS_COMPONENT)); | 1396 CreateExtension(CLIENT_ID | SCOPES | AS_COMPONENT)); |
1245 func->set_extension(extension.get()); | 1397 func->set_extension(extension.get()); |
1246 EXPECT_EQ("client1", func->GetOAuth2ClientId()); | 1398 EXPECT_EQ("client1", func->GetOAuth2ClientId()); |
1247 } | 1399 } |
1248 | 1400 |
| 1401 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiDefaultUser) { |
| 1402 SignIn("primary@example.com"); |
| 1403 token_service_->IssueRefreshTokenForUser("primary@example.com", |
| 1404 "refresh_token"); |
| 1405 SetAccountState(CreateIds("primary@example.com", "1"), true); |
| 1406 SetAccountState(CreateIds("secondary@example.com", "2"), true); |
| 1407 |
| 1408 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction()); |
| 1409 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 1410 func->set_extension(extension.get()); |
| 1411 func->set_mint_token_flow( |
| 1412 scoped_ptr<OAuth2MintTokenFlow>(new TestOAuth2MintTokenFlow( |
| 1413 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()))); |
| 1414 |
| 1415 RunFunctionAsync(func.get(), "[{}]"); |
| 1416 |
| 1417 token_service_->IssueAllTokensForAccount( |
| 1418 "primary@example.com", |
| 1419 "access_token-primary@example.com", |
| 1420 base::Time::Now() + base::TimeDelta::FromSeconds(3600)); |
| 1421 |
| 1422 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); |
| 1423 std::string access_token; |
| 1424 EXPECT_TRUE(value->GetAsString(&access_token)); |
| 1425 EXPECT_EQ(std::string(kAccessToken), access_token); |
| 1426 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
| 1427 GetCachedToken(std::string()).status()); |
| 1428 EXPECT_EQ("access_token-primary@example.com", func->login_access_token()); |
| 1429 } |
| 1430 |
| 1431 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiPrimaryUser) { |
| 1432 SignIn("primary@example.com"); |
| 1433 token_service_->IssueRefreshTokenForUser("primary@example.com", |
| 1434 "refresh_token"); |
| 1435 SetAccountState(CreateIds("primary@example.com", "1"), true); |
| 1436 SetAccountState(CreateIds("secondary@example.com", "2"), true); |
| 1437 |
| 1438 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction()); |
| 1439 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 1440 func->set_extension(extension.get()); |
| 1441 func->set_mint_token_flow( |
| 1442 scoped_ptr<OAuth2MintTokenFlow>(new TestOAuth2MintTokenFlow( |
| 1443 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()))); |
| 1444 |
| 1445 RunFunctionAsync(func.get(), "[{\"account\": { \"id\": \"1\" } }]"); |
| 1446 |
| 1447 token_service_->IssueAllTokensForAccount( |
| 1448 "primary@example.com", |
| 1449 "access_token-primary@example.com", |
| 1450 base::Time::Now() + base::TimeDelta::FromSeconds(3600)); |
| 1451 |
| 1452 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); |
| 1453 std::string access_token; |
| 1454 EXPECT_TRUE(value->GetAsString(&access_token)); |
| 1455 EXPECT_EQ(std::string(kAccessToken), access_token); |
| 1456 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
| 1457 GetCachedToken(std::string()).status()); |
| 1458 EXPECT_EQ("access_token-primary@example.com", func->login_access_token()); |
| 1459 } |
| 1460 |
| 1461 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, MultiSecondaryUser) { |
| 1462 SignIn("primary@example.com"); |
| 1463 token_service_->IssueRefreshTokenForUser("secondary@example.com", |
| 1464 "refresh_token"); |
| 1465 SetAccountState(CreateIds("primary@example.com", "1"), true); |
| 1466 SetAccountState(CreateIds("secondary@example.com", "2"), true); |
| 1467 |
| 1468 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction()); |
| 1469 scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES)); |
| 1470 func->set_extension(extension.get()); |
| 1471 func->set_mint_token_flow( |
| 1472 scoped_ptr<OAuth2MintTokenFlow>(new TestOAuth2MintTokenFlow( |
| 1473 TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS, func.get()))); |
| 1474 |
| 1475 RunFunctionAsync(func.get(), "[{\"account\": { \"id\": \"2\" } }]"); |
| 1476 |
| 1477 token_service_->IssueAllTokensForAccount( |
| 1478 "secondary@example.com", |
| 1479 "access_token-secondary@example.com", |
| 1480 base::Time::Now() + base::TimeDelta::FromSeconds(3600)); |
| 1481 |
| 1482 scoped_ptr<base::Value> value(WaitForSingleResult(func.get())); |
| 1483 std::string access_token; |
| 1484 EXPECT_TRUE(value->GetAsString(&access_token)); |
| 1485 EXPECT_EQ(std::string(kAccessToken), access_token); |
| 1486 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
| 1487 GetCachedToken("secondary@example.com").status()); |
| 1488 EXPECT_EQ("access_token-secondary@example.com", func->login_access_token()); |
| 1489 } |
| 1490 |
| 1491 // TODO(courage): negative cases for secondary accounts |
| 1492 |
1249 class RemoveCachedAuthTokenFunctionTest : public ExtensionBrowserTest { | 1493 class RemoveCachedAuthTokenFunctionTest : public ExtensionBrowserTest { |
1250 protected: | 1494 protected: |
1251 bool InvalidateDefaultToken() { | 1495 bool InvalidateDefaultToken() { |
1252 scoped_refptr<IdentityRemoveCachedAuthTokenFunction> func( | 1496 scoped_refptr<IdentityRemoveCachedAuthTokenFunction> func( |
1253 new IdentityRemoveCachedAuthTokenFunction); | 1497 new IdentityRemoveCachedAuthTokenFunction); |
1254 func->set_extension(utils::CreateEmptyExtension(kExtensionId).get()); | 1498 func->set_extension(utils::CreateEmptyExtension(kExtensionId).get()); |
1255 return utils::RunFunction( | 1499 return utils::RunFunction( |
1256 func.get(), | 1500 func.get(), |
1257 std::string("[{\"token\": \"") + kAccessToken + "\"}]", | 1501 std::string("[{\"token\": \"") + kAccessToken + "\"}]", |
1258 browser(), | 1502 browser(), |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1310 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, | 1554 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN, |
1311 GetCachedToken().status()); | 1555 GetCachedToken().status()); |
1312 EXPECT_TRUE(InvalidateDefaultToken()); | 1556 EXPECT_TRUE(InvalidateDefaultToken()); |
1313 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, | 1557 EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_NOTFOUND, |
1314 GetCachedToken().status()); | 1558 GetCachedToken().status()); |
1315 } | 1559 } |
1316 | 1560 |
1317 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest { | 1561 class LaunchWebAuthFlowFunctionTest : public AsyncExtensionBrowserTest { |
1318 public: | 1562 public: |
1319 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 1563 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 1564 AsyncExtensionBrowserTest::SetUpCommandLine(command_line); |
1320 // Reduce performance test variance by disabling background networking. | 1565 // Reduce performance test variance by disabling background networking. |
1321 command_line->AppendSwitch(switches::kDisableBackgroundNetworking); | 1566 command_line->AppendSwitch(switches::kDisableBackgroundNetworking); |
1322 } | 1567 } |
1323 }; | 1568 }; |
1324 | 1569 |
1325 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, UserCloseWindow) { | 1570 IN_PROC_BROWSER_TEST_F(LaunchWebAuthFlowFunctionTest, UserCloseWindow) { |
1326 net::SpawnedTestServer https_server( | 1571 net::SpawnedTestServer https_server( |
1327 net::SpawnedTestServer::TYPE_HTTPS, | 1572 net::SpawnedTestServer::TYPE_HTTPS, |
1328 net::SpawnedTestServer::kLocalhost, | 1573 net::SpawnedTestServer::kLocalhost, |
1329 base::FilePath(FILE_PATH_LITERAL( | 1574 base::FilePath(FILE_PATH_LITERAL( |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1475 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), | 1720 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), |
1476 url); | 1721 url); |
1477 } | 1722 } |
1478 | 1723 |
1479 } // namespace extensions | 1724 } // namespace extensions |
1480 | 1725 |
1481 // Tests the chrome.identity API implemented by custom JS bindings . | 1726 // Tests the chrome.identity API implemented by custom JS bindings . |
1482 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) { | 1727 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) { |
1483 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_; | 1728 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_; |
1484 } | 1729 } |
OLD | NEW |