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

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

Issue 759823002: Identity API: Use AccountTrackerService for account details (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Aaand update tests again to shut down properly. Created 6 years 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
« no previous file with comments | « chrome/browser/extensions/api/identity/identity_api.cc ('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"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h" 14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/api/identity/identity_api.h" 15 #include "chrome/browser/extensions/api/identity/identity_api.h"
16 #include "chrome/browser/extensions/component_loader.h" 16 #include "chrome/browser/extensions/component_loader.h"
17 #include "chrome/browser/extensions/extension_apitest.h" 17 #include "chrome/browser/extensions/extension_apitest.h"
18 #include "chrome/browser/extensions/extension_browsertest.h" 18 #include "chrome/browser/extensions/extension_browsertest.h"
19 #include "chrome/browser/extensions/extension_function_test_utils.h" 19 #include "chrome/browser/extensions/extension_function_test_utils.h"
20 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/signin/account_reconcilor_factory.h" 22 #include "chrome/browser/signin/account_reconcilor_factory.h"
23 #include "chrome/browser/signin/account_tracker_service_factory.h"
23 #include "chrome/browser/signin/fake_account_reconcilor.h" 24 #include "chrome/browser/signin/fake_account_reconcilor.h"
24 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" 25 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
25 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" 26 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
26 #include "chrome/browser/signin/fake_signin_manager.h" 27 #include "chrome/browser/signin/fake_signin_manager.h"
27 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 28 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
28 #include "chrome/browser/signin/signin_manager_factory.h" 29 #include "chrome/browser/signin/signin_manager_factory.h"
29 #include "chrome/browser/ui/browser.h" 30 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/browser_window.h" 31 #include "chrome/browser/ui/browser_window.h"
31 #include "chrome/common/chrome_switches.h" 32 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/extensions/api/identity.h" 33 #include "chrome/common/extensions/api/identity.h"
33 #include "chrome/test/base/in_process_browser_test.h" 34 #include "chrome/test/base/in_process_browser_test.h"
34 #include "chrome/test/base/test_switches.h" 35 #include "chrome/test/base/test_switches.h"
35 #include "components/crx_file/id_util.h" 36 #include "components/crx_file/id_util.h"
37 #include "components/signin/core/browser/account_tracker_service.h"
36 #include "components/signin/core/browser/signin_manager.h" 38 #include "components/signin/core/browser/signin_manager.h"
37 #include "components/signin/core/common/profile_management_switches.h" 39 #include "components/signin/core/common/profile_management_switches.h"
38 #include "components/signin/core/common/signin_pref_names.h" 40 #include "components/signin/core/common/signin_pref_names.h"
39 #include "content/public/browser/notification_service.h" 41 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/notification_source.h" 42 #include "content/public/browser/notification_source.h"
41 #include "content/public/test/test_utils.h" 43 #include "content/public/test/test_utils.h"
42 #include "extensions/browser/guest_view/guest_view_base.h" 44 #include "extensions/browser/guest_view/guest_view_base.h"
43 #include "extensions/common/manifest_handlers/oauth2_manifest_handler.h" 45 #include "extensions/common/manifest_handlers/oauth2_manifest_handler.h"
44 #include "extensions/common/test_util.h" 46 #include "extensions/common/test_util.h"
45 #include "google_apis/gaia/google_service_auth_error.h" 47 #include "google_apis/gaia/google_service_auth_error.h"
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 525
524 IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest, 526 IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest,
525 TwoAccountsSignedIn) { 527 TwoAccountsSignedIn) {
526 SetAccountState(CreateIds("primary@example.com", "1"), true); 528 SetAccountState(CreateIds("primary@example.com", "1"), true);
527 SetAccountState(CreateIds("secondary@example.com", "2"), true); 529 SetAccountState(CreateIds("secondary@example.com", "2"), true);
528 std::vector<std::string> only_primary; 530 std::vector<std::string> only_primary;
529 only_primary.push_back("1"); 531 only_primary.push_back("1");
530 EXPECT_TRUE(ExpectGetAccounts(only_primary)); 532 EXPECT_TRUE(ExpectGetAccounts(only_primary));
531 } 533 }
532 534
533 class IdentityGetProfileUserInfoFunctionTest : public ExtensionBrowserTest { 535 class IdentityTestWithSignin : public AsyncExtensionBrowserTest {
536 public:
537 void SetUpInProcessBrowserTestFixture() override {
538 AsyncExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
539
540 will_create_browser_context_services_subscription_ =
541 BrowserContextDependencyManager::GetInstance()
542 ->RegisterWillCreateBrowserContextServicesCallbackForTesting(
543 base::Bind(&IdentityTestWithSignin::
544 OnWillCreateBrowserContextServices,
545 base::Unretained(this)))
546 .Pass();
547 }
548
549 void OnWillCreateBrowserContextServices(content::BrowserContext* context) {
550 // Replace the signin manager and token service with fakes. Do this ahead of
551 // creating the browser so that a bunch of classes don't register as
552 // observers and end up needing to unregister when the fake is substituted.
553 SigninManagerFactory::GetInstance()->SetTestingFactory(
554 context, &FakeSigninManagerBase::Build);
555 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
556 context, &BuildFakeProfileOAuth2TokenService);
557 AccountReconcilorFactory::GetInstance()->SetTestingFactory(
558 context, &FakeAccountReconcilor::Build);
559 }
560
561 void SetUpOnMainThread() override {
562 AsyncExtensionBrowserTest::SetUpOnMainThread();
563
564 // Grab references to the fake signin manager and token service.
565 signin_manager_ = static_cast<FakeSigninManagerForTesting*>(
566 SigninManagerFactory::GetInstance()->GetForProfile(profile()));
567 ASSERT_TRUE(signin_manager_);
568 token_service_ = static_cast<FakeProfileOAuth2TokenService*>(
569 ProfileOAuth2TokenServiceFactory::GetInstance()->GetForProfile(
570 profile()));
571 ASSERT_TRUE(token_service_);
572 }
573
574 protected:
575 void SignIn(const std::string account_key) {
576 #if defined(OS_CHROMEOS)
577 signin_manager_->SetAuthenticatedUsername(account_key);
578 #else
579 signin_manager_->SignIn(account_key, "password");
580 #endif
581 token_service_->IssueRefreshTokenForUser(account_key, "refresh_token");
582 }
583
584 void SignIn(const std::string& account_key, const std::string& gaia) {
585 AccountTrackerService* account_tracker =
586 AccountTrackerServiceFactory::GetForProfile(profile());
587 account_tracker->SeedAccountInfo(gaia, account_key);
588 SignIn(account_key);
589 }
590
591 FakeSigninManagerForTesting* signin_manager_;
592 FakeProfileOAuth2TokenService* token_service_;
593
594 scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription>
595 will_create_browser_context_services_subscription_;
596 };
597
598 class IdentityGetProfileUserInfoFunctionTest : public IdentityTestWithSignin {
534 protected: 599 protected:
535 scoped_ptr<api::identity::ProfileUserInfo> RunGetProfileUserInfo() { 600 scoped_ptr<api::identity::ProfileUserInfo> RunGetProfileUserInfo() {
536 scoped_refptr<IdentityGetProfileUserInfoFunction> func( 601 scoped_refptr<IdentityGetProfileUserInfoFunction> func(
537 new IdentityGetProfileUserInfoFunction); 602 new IdentityGetProfileUserInfoFunction);
538 func->set_extension(test_util::CreateEmptyExtension(kExtensionId).get()); 603 func->set_extension(test_util::CreateEmptyExtension(kExtensionId).get());
539 scoped_ptr<base::Value> value( 604 scoped_ptr<base::Value> value(
540 utils::RunFunctionAndReturnSingleResult(func.get(), "[]", browser())); 605 utils::RunFunctionAndReturnSingleResult(func.get(), "[]", browser()));
541 return api::identity::ProfileUserInfo::FromValue(*value.get()); 606 return api::identity::ProfileUserInfo::FromValue(*value.get());
542 } 607 }
543 608
(...skipping 17 matching lines...) Expand all
561 }; 626 };
562 627
563 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, NotSignedIn) { 628 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, NotSignedIn) {
564 scoped_ptr<api::identity::ProfileUserInfo> info = 629 scoped_ptr<api::identity::ProfileUserInfo> info =
565 RunGetProfileUserInfoWithEmail(); 630 RunGetProfileUserInfoWithEmail();
566 EXPECT_TRUE(info->email.empty()); 631 EXPECT_TRUE(info->email.empty());
567 EXPECT_TRUE(info->id.empty()); 632 EXPECT_TRUE(info->id.empty());
568 } 633 }
569 634
570 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, SignedIn) { 635 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, SignedIn) {
571 profile()->GetPrefs() 636 SignIn("president@example.com", "12345");
572 ->SetString(prefs::kGoogleServicesUsername, "president@example.com");
573 profile()->GetPrefs()
574 ->SetString(prefs::kGoogleServicesUserAccountId, "12345");
575
576 scoped_ptr<api::identity::ProfileUserInfo> info = 637 scoped_ptr<api::identity::ProfileUserInfo> info =
577 RunGetProfileUserInfoWithEmail(); 638 RunGetProfileUserInfoWithEmail();
578 EXPECT_EQ("president@example.com", info->email); 639 EXPECT_EQ("president@example.com", info->email);
579 EXPECT_EQ("12345", info->id); 640 EXPECT_EQ("12345", info->id);
580 } 641 }
581 642
582 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, 643 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest,
583 NotSignedInNoEmail) { 644 NotSignedInNoEmail) {
584 scoped_ptr<api::identity::ProfileUserInfo> info = RunGetProfileUserInfo(); 645 scoped_ptr<api::identity::ProfileUserInfo> info = RunGetProfileUserInfo();
585 EXPECT_TRUE(info->email.empty()); 646 EXPECT_TRUE(info->email.empty());
586 EXPECT_TRUE(info->id.empty()); 647 EXPECT_TRUE(info->id.empty());
587 } 648 }
588 649
589 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, 650 IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest,
590 SignedInNoEmail) { 651 SignedInNoEmail) {
591 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 652 SignIn("president@example.com", "12345");
592 "president@example.com");
593 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId,
594 "12345");
595
596 scoped_ptr<api::identity::ProfileUserInfo> info = RunGetProfileUserInfo(); 653 scoped_ptr<api::identity::ProfileUserInfo> info = RunGetProfileUserInfo();
597 EXPECT_TRUE(info->email.empty()); 654 EXPECT_TRUE(info->email.empty());
598 EXPECT_EQ("12345", info->id); 655 EXPECT_EQ("12345", info->id);
599 } 656 }
600 657
601 class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest { 658 class GetAuthTokenFunctionTest : public IdentityTestWithSignin {
602 public: 659 public:
603 void SetUpCommandLine(CommandLine* command_line) override { 660 void SetUpCommandLine(CommandLine* command_line) override {
604 AsyncExtensionBrowserTest::SetUpCommandLine(command_line); 661 IdentityTestWithSignin::SetUpCommandLine(command_line);
605 command_line->AppendSwitch(switches::kExtensionsMultiAccount); 662 command_line->AppendSwitch(switches::kExtensionsMultiAccount);
606 } 663 }
607 664
608 void SetUpInProcessBrowserTestFixture() override {
609 AsyncExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
610
611 will_create_browser_context_services_subscription_ =
612 BrowserContextDependencyManager::GetInstance()
613 ->RegisterWillCreateBrowserContextServicesCallbackForTesting(
614 base::Bind(&GetAuthTokenFunctionTest::
615 OnWillCreateBrowserContextServices,
616 base::Unretained(this)))
617 .Pass();
618 }
619
620 void OnWillCreateBrowserContextServices(content::BrowserContext* context) {
621 // Replace the signin manager and token service with fakes. Do this ahead of
622 // creating the browser so that a bunch of classes don't register as
623 // observers and end up needing to unregister when the fake is substituted.
624 SigninManagerFactory::GetInstance()->SetTestingFactory(
625 context, &FakeSigninManagerBase::Build);
626 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
627 context, &BuildFakeProfileOAuth2TokenService);
628 AccountReconcilorFactory::GetInstance()->SetTestingFactory(
629 context, &FakeAccountReconcilor::Build);
630 }
631
632 void SetUpOnMainThread() override {
633 AsyncExtensionBrowserTest::SetUpOnMainThread();
634
635 // Grab references to the fake signin manager and token service.
636 signin_manager_ = static_cast<FakeSigninManagerForTesting*>(
637 SigninManagerFactory::GetInstance()->GetForProfile(profile()));
638 ASSERT_TRUE(signin_manager_);
639 token_service_ = static_cast<FakeProfileOAuth2TokenService*>(
640 ProfileOAuth2TokenServiceFactory::GetInstance()->GetForProfile(
641 profile()));
642 ASSERT_TRUE(token_service_);
643 }
644
645 void SignIn(const std::string account_key) {
646 #if defined(OS_CHROMEOS)
647 signin_manager_->SetAuthenticatedUsername(account_key);
648 #else
649 signin_manager_->SignIn(account_key, "password");
650 #endif
651 token_service_->IssueRefreshTokenForUser(account_key, "refresh_token");
652 }
653
654 void IssueLoginRefreshTokenForAccount(const std::string account_key) { 665 void IssueLoginRefreshTokenForAccount(const std::string account_key) {
655 token_service_->IssueRefreshTokenForUser(account_key, "refresh_token"); 666 token_service_->IssueRefreshTokenForUser(account_key, "refresh_token");
656 } 667 }
657 668
658 void IssueLoginAccessTokenForAccount(const std::string account_key) { 669 void IssueLoginAccessTokenForAccount(const std::string account_key) {
659 token_service_->IssueAllTokensForAccount( 670 token_service_->IssueAllTokensForAccount(
660 account_key, 671 account_key,
661 "access_token-" + account_key, 672 "access_token-" + account_key,
662 base::Time::Now() + base::TimeDelta::FromSeconds(3600)); 673 base::Time::Now() + base::TimeDelta::FromSeconds(3600));
663 } 674 }
664 675
665 void SetAccountState(gaia::AccountIds ids, bool is_signed_in) { 676 void SetAccountState(gaia::AccountIds ids, bool is_signed_in) {
666 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest( 677 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
667 ids, is_signed_in); 678 ids, is_signed_in);
668 } 679 }
669 680
670 protected: 681 protected:
671 enum OAuth2Fields { 682 enum OAuth2Fields {
672 NONE = 0, 683 NONE = 0,
673 CLIENT_ID = 1, 684 CLIENT_ID = 1,
674 SCOPES = 2, 685 SCOPES = 2,
675 AS_COMPONENT = 4 686 AS_COMPONENT = 4
676 }; 687 };
677 688
678 FakeSigninManagerForTesting* signin_manager_;
679 FakeProfileOAuth2TokenService* token_service_;
680
681 ~GetAuthTokenFunctionTest() override {} 689 ~GetAuthTokenFunctionTest() override {}
682 690
683 // Helper to create an extension with specific OAuth2Info fields set. 691 // Helper to create an extension with specific OAuth2Info fields set.
684 // |fields_to_set| should be computed by using fields of Oauth2Fields enum. 692 // |fields_to_set| should be computed by using fields of Oauth2Fields enum.
685 const Extension* CreateExtension(int fields_to_set) { 693 const Extension* CreateExtension(int fields_to_set) {
686 const Extension* ext; 694 const Extension* ext;
687 base::FilePath manifest_path = 695 base::FilePath manifest_path =
688 test_data_dir_.AppendASCII("platform_apps/oauth2"); 696 test_data_dir_.AppendASCII("platform_apps/oauth2");
689 base::FilePath component_manifest_path = 697 base::FilePath component_manifest_path =
690 test_data_dir_.AppendASCII("packaged_app/component_oauth2"); 698 test_data_dir_.AppendASCII("packaged_app/component_oauth2");
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 745
738 void QueueRequestComplete(IdentityMintRequestQueue::MintType type, 746 void QueueRequestComplete(IdentityMintRequestQueue::MintType type,
739 IdentityMintRequestQueue::Request* request) { 747 IdentityMintRequestQueue::Request* request) {
740 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_); 748 ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_);
741 id_api()->mint_queue()->RequestComplete(type, key, request); 749 id_api()->mint_queue()->RequestComplete(type, key, request);
742 } 750 }
743 751
744 private: 752 private:
745 std::string extension_id_; 753 std::string extension_id_;
746 std::set<std::string> oauth_scopes_; 754 std::set<std::string> oauth_scopes_;
747
748 scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription>
749 will_create_browser_context_services_subscription_;
750 }; 755 };
751 756
752 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest, 757 IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
753 NoClientId) { 758 NoClientId) {
754 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction()); 759 scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
755 func->set_extension(CreateExtension(SCOPES)); 760 func->set_extension(CreateExtension(SCOPES));
756 std::string error = utils::RunFunctionAndReturnError( 761 std::string error = utils::RunFunctionAndReturnError(
757 func.get(), "[{}]", browser()); 762 func.get(), "[{}]", browser());
758 EXPECT_EQ(std::string(errors::kInvalidClientId), error); 763 EXPECT_EQ(std::string(errors::kInvalidClientId), error);
759 EXPECT_FALSE(func->login_ui_shown()); 764 EXPECT_FALSE(func->login_ui_shown());
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1811 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 1816 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
1812 url); 1817 url);
1813 } 1818 }
1814 1819
1815 } // namespace extensions 1820 } // namespace extensions
1816 1821
1817 // Tests the chrome.identity API implemented by custom JS bindings . 1822 // Tests the chrome.identity API implemented by custom JS bindings .
1818 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) { 1823 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) {
1819 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_; 1824 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_;
1820 } 1825 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/identity/identity_api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698