| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/test/ash_test_base.h" | |
| 6 #include "chrome/browser/signin/account_tracker_service_factory.h" | |
| 7 #include "chrome/browser/signin/fake_signin_manager_builder.h" | |
| 8 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 9 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | |
| 10 #include "chrome/test/base/testing_profile.h" | |
| 11 #include "components/signin/core/account_id/account_id.h" | |
| 12 #include "components/signin/core/browser/account_tracker_service.h" | |
| 13 | |
| 14 #if defined(OS_CHROMEOS) | |
| 15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
| 16 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 17 #include "components/user_manager/fake_user_manager.h" | |
| 18 #include "components/user_manager/user.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace ash { | |
| 22 namespace test { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 const char kTestGaiaId[] = "gaia_id"; | |
| 27 const char kTestAccountId[] = "test@test.com"; | |
| 28 | |
| 29 class MultiUserTestingProfile : public TestingProfile { | |
| 30 public: | |
| 31 explicit MultiUserTestingProfile(TestingProfile* profile) | |
| 32 : profile_(profile) {} | |
| 33 ~MultiUserTestingProfile() override {} | |
| 34 | |
| 35 Profile* GetOriginalProfile() override { return this; } | |
| 36 | |
| 37 std::string GetProfileUserName() const override { | |
| 38 const SigninManagerBase* signin_manager = | |
| 39 SigninManagerFactory::GetForProfileIfExists(profile_.get()); | |
| 40 if (signin_manager) | |
| 41 return signin_manager->GetAuthenticatedAccountInfo().email; | |
| 42 | |
| 43 return std::string(); | |
| 44 } | |
| 45 | |
| 46 TestingProfile* profile() { return profile_.get(); } | |
| 47 | |
| 48 private: | |
| 49 std::unique_ptr<TestingProfile> profile_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(MultiUserTestingProfile); | |
| 52 }; | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 class MultiUserUtilTest : public AshTestBase { | |
| 57 public: | |
| 58 MultiUserUtilTest() {} | |
| 59 ~MultiUserUtilTest() override{}; | |
| 60 | |
| 61 void SetUp() override { | |
| 62 AshTestBase::SetUp(); | |
| 63 | |
| 64 #if defined(OS_CHROMEOS) | |
| 65 fake_user_manager_ = new user_manager::FakeUserManager; | |
| 66 user_manager_enabler_.reset( | |
| 67 new chromeos::ScopedUserManagerEnabler(fake_user_manager_)); | |
| 68 #endif | |
| 69 | |
| 70 TestingProfile::Builder builder; | |
| 71 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | |
| 72 BuildFakeSigninManagerBase); | |
| 73 TestingProfile* profile = builder.Build().release(); | |
| 74 profile_.reset(new MultiUserTestingProfile(profile)); | |
| 75 } | |
| 76 | |
| 77 void TearDown() override { | |
| 78 profile_.reset(); | |
| 79 AshTestBase::TearDown(); | |
| 80 } | |
| 81 | |
| 82 // Add a user to account tracker service with given gaia_id and email. | |
| 83 std::string AddUserToAccountTracker(const std::string& gaia_id, | |
| 84 const std::string& email) { | |
| 85 AccountTrackerService* account_tracker_service = | |
| 86 AccountTrackerServiceFactory::GetForProfile(profile_->profile()); | |
| 87 FakeSigninManagerBase* signin_manager = static_cast<FakeSigninManagerBase*>( | |
| 88 SigninManagerFactory::GetForProfile(profile_->profile())); | |
| 89 account_tracker_service->SeedAccountInfo(gaia_id, email); | |
| 90 const std::string id = | |
| 91 account_tracker_service->PickAccountIdForAccount(gaia_id, email); | |
| 92 signin_manager->SignIn(id); | |
| 93 | |
| 94 #if defined(OS_CHROMEOS) | |
| 95 fake_user_manager_->AddUser(multi_user_util::GetAccountIdFromEmail(id)); | |
| 96 fake_user_manager_->UserLoggedIn( | |
| 97 multi_user_util::GetAccountIdFromEmail(id), | |
| 98 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(id), | |
| 99 false /* browser_restart */); | |
| 100 #endif | |
| 101 | |
| 102 return id; | |
| 103 } | |
| 104 | |
| 105 void SimulateTokenRevoked(const std::string& account_id) { | |
| 106 AccountTrackerService* account_tracker_service = | |
| 107 AccountTrackerServiceFactory::GetForProfile(profile_->profile()); | |
| 108 account_tracker_service->RemoveAccount(account_id); | |
| 109 } | |
| 110 | |
| 111 MultiUserTestingProfile* profile() { return profile_.get(); } | |
| 112 | |
| 113 private: | |
| 114 std::unique_ptr<MultiUserTestingProfile> profile_; | |
| 115 #if defined(OS_CHROMEOS) | |
| 116 user_manager::FakeUserManager* fake_user_manager_; // Not owned. | |
| 117 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | |
| 118 #endif | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(MultiUserUtilTest); | |
| 121 }; | |
| 122 | |
| 123 #if defined(OS_CHROMEOS) | |
| 124 // Test that during the session it will always return a valid account id if a | |
| 125 // valid profile is provided, even if this profile's refresh token has been | |
| 126 // revoked. (On Chrome OS we don't force to end the session in this case.) | |
| 127 TEST_F(MultiUserUtilTest, ReturnValidAccountIdIfTokenRevoked) { | |
| 128 std::string id = AddUserToAccountTracker(kTestGaiaId, kTestAccountId); | |
| 129 EXPECT_EQ(kTestAccountId, profile()->GetProfileUserName()); | |
| 130 EXPECT_EQ(kTestAccountId, | |
| 131 multi_user_util::GetAccountIdFromProfile(profile()).GetUserEmail()); | |
| 132 | |
| 133 SimulateTokenRevoked(id); | |
| 134 EXPECT_EQ(std::string(), profile()->GetProfileUserName()); | |
| 135 EXPECT_EQ(kTestAccountId, | |
| 136 multi_user_util::GetAccountIdFromProfile(profile()).GetUserEmail()); | |
| 137 } | |
| 138 #endif | |
| 139 | |
| 140 } // namespace test | |
| 141 } // namespace ash | |
| OLD | NEW |