1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/account_manager/account_manager_edu_coexistence_controller.h"
#include <memory>
#include <string>
#include "ash/constants/ash_pref_names.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/test/test_future.h"
#include "base/values.h"
#include "chrome/browser/ash/child_accounts/edu_coexistence_tos_store_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/edu_coexistence/edu_coexistence_login_handler.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/account_manager/account_manager_facade_factory.h"
#include "chromeos/ash/components/account_manager/account_manager_factory.h"
#include "components/account_id/account_id.h"
#include "components/account_manager_core/account.h"
#include "components/account_manager_core/account_manager_facade.h"
#include "components/account_manager_core/chromeos/account_manager.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "google_apis/gaia/gaia_id.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
constexpr char kValidToken[] = "valid-token";
constexpr char kPrimaryAccount[] = "primaryaccount@gmail.com";
constexpr GaiaId::Literal kPrimaryAccountGaiaId("primary-account-id");
constexpr char kSecondaryAccount1[] = "secondaryAccount1@gmail.com";
constexpr GaiaId::Literal kSecondaryAccount1GaiaId("secondary-account-1");
constexpr char kSecondaryAccount2[] = "secondaryAccount2@gmail.com";
constexpr GaiaId::Literal kSecondaryAccount2GaiaId("secondary-account-2");
constexpr char kSecondaryAccount3[] = "secondaryAccount3@gmail.com";
constexpr GaiaId::Literal kSecondaryAccount3GaiaId("secondary-account-3");
constexpr char kSecondaryAccount4[] = "secondaryAccount4@gmail.com";
constexpr GaiaId::Literal kSecondaryAccount4GaiaId("secondary-account-4");
const AccountId kDeviceAccount =
AccountId::FromUserEmailGaiaId(kPrimaryAccount, kPrimaryAccountGaiaId);
::account_manager::Account GetAccountFor(const std::string& email,
const GaiaId& gaia_id) {
::account_manager::AccountKey key =
::account_manager::AccountKey::FromGaiaId(gaia_id);
return {key, email};
}
void AddAccount(account_manager::AccountManager* account_manager,
const std::string& email,
const GaiaId& gaia_id) {
::account_manager::AccountKey key =
::account_manager::AccountKey::FromGaiaId(gaia_id);
account_manager->UpsertAccount(key, email, kValidToken);
}
} // namespace
class AccountManagerEducoexistenceControllerTest : public testing::Test {
public:
AccountManagerEducoexistenceControllerTest() = default;
AccountManagerEducoexistenceControllerTest(
const AccountManagerEducoexistenceControllerTest&) = delete;
AccountManagerEducoexistenceControllerTest& operator=(
const AccountManagerEducoexistenceControllerTest&) = delete;
~AccountManagerEducoexistenceControllerTest() override = default;
void SetUp() override;
void UpdatEduCoexistenceToSAcceptedVersion(const GaiaId& gaia_id,
const std::string& tosVersion);
void UpdateEduCoexistenceToSVersion(const std::string& new_version);
bool HasInvalidGaiaToken(const ::account_manager::Account& account);
protected:
Profile* profile() { return &testing_profile_; }
account_manager::AccountManager* account_manager() {
return account_manager_;
}
account_manager::AccountManagerFacade* account_manager_facade() {
return account_manager_facade_;
}
private:
// To support context of browser threads.
content::BrowserTaskEnvironment task_environment_;
raw_ptr<account_manager::AccountManager> account_manager_ = nullptr;
raw_ptr<account_manager::AccountManagerFacade> account_manager_facade_ =
nullptr;
network::TestURLLoaderFactory test_url_loader_factory_;
TestingProfile testing_profile_;
};
void AccountManagerEducoexistenceControllerTest::SetUp() {
testing_profile_.SetIsSupervisedProfile();
account_manager_ = TestingBrowserProcess::GetGlobal()
->platform_part()
->GetAccountManagerFactory()
->GetAccountManager(profile()->GetPath().value());
account_manager_facade_ =
GetAccountManagerFacade(profile()->GetPath().value());
AddAccount(account_manager(), kPrimaryAccount, kPrimaryAccountGaiaId);
}
void AccountManagerEducoexistenceControllerTest::
UpdatEduCoexistenceToSAcceptedVersion(const GaiaId& gaia_id,
const std::string& tosVersion) {
edu_coexistence::UpdateAcceptedToSVersionPref(
profile(), edu_coexistence::UserConsentInfo(gaia_id, tosVersion));
}
void AccountManagerEducoexistenceControllerTest::UpdateEduCoexistenceToSVersion(
const std::string& new_version) {
profile()->GetPrefs()->SetString(prefs::kEduCoexistenceToSVersion,
new_version);
}
bool AccountManagerEducoexistenceControllerTest::HasInvalidGaiaToken(
const ::account_manager::Account& account) {
base::test::TestFuture<bool> future;
account_manager()->HasDummyGaiaToken(account.key, future.GetCallback());
return future.Get();
}
TEST_F(AccountManagerEducoexistenceControllerTest,
AccountsInPrefWithInvalidTokenShouldBeInvalidated) {
// Account will be invalidated.
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount1GaiaId, "0");
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount1GaiaId),
"0");
// Account will not be invalidated fine.
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount2GaiaId, "5");
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount2GaiaId),
"5");
AddAccount(account_manager(), kSecondaryAccount1, kSecondaryAccount1GaiaId);
AddAccount(account_manager(), kSecondaryAccount2, kSecondaryAccount2GaiaId);
EXPECT_FALSE(HasInvalidGaiaToken(
GetAccountFor(kSecondaryAccount1, kSecondaryAccount1GaiaId)));
EXPECT_FALSE(HasInvalidGaiaToken(
GetAccountFor(kSecondaryAccount2, kSecondaryAccount2GaiaId)));
UpdateEduCoexistenceToSVersion("5");
EduCoexistenceConsentInvalidationController
edu_coexistence_invalidation_controller(profile(), account_manager(),
account_manager_facade(),
kDeviceAccount);
edu_coexistence_invalidation_controller.Init();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(HasInvalidGaiaToken(
GetAccountFor(kSecondaryAccount1, kSecondaryAccount1GaiaId)));
EXPECT_FALSE(HasInvalidGaiaToken(
GetAccountFor(kSecondaryAccount2, kSecondaryAccount2GaiaId)));
}
TEST_F(AccountManagerEducoexistenceControllerTest,
AccountsNotInPrefShouldBeAddedToUserPref) {
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount1GaiaId, "5");
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount2GaiaId, "6");
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount3GaiaId, "7");
AddAccount(account_manager(), kSecondaryAccount1, kSecondaryAccount1GaiaId);
AddAccount(account_manager(), kSecondaryAccount2, kSecondaryAccount2GaiaId);
AddAccount(account_manager(), kSecondaryAccount3, kSecondaryAccount3GaiaId);
// Note: kSecondaryAccount4 is not present in pref.
AddAccount(account_manager(), kSecondaryAccount4, kSecondaryAccount4GaiaId);
EduCoexistenceConsentInvalidationController
edu_coexistence_invalidation_controller(profile(), account_manager(),
account_manager_facade(),
kDeviceAccount);
edu_coexistence_invalidation_controller.Init();
base::RunLoop().RunUntilIdle();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount4GaiaId),
edu_coexistence::kMinTOSVersionNumber);
}
TEST_F(AccountManagerEducoexistenceControllerTest,
AccountsNotInAccountManagerShouldBeRemovedFromUserPref) {
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount1GaiaId, "5");
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount2GaiaId, "6");
UpdatEduCoexistenceToSAcceptedVersion(kSecondaryAccount3GaiaId, "7");
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount1GaiaId),
"5");
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount2GaiaId),
"6");
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount3GaiaId),
"7");
// kSecondaryAccount1 was unfortunately removed and is not present in account
// manager.
AddAccount(account_manager(), kSecondaryAccount2, kSecondaryAccount2GaiaId);
AddAccount(account_manager(), kSecondaryAccount3, kSecondaryAccount3GaiaId);
EduCoexistenceConsentInvalidationController
edu_coexistence_invalidation_controller(profile(), account_manager(),
account_manager_facade(),
kDeviceAccount);
edu_coexistence_invalidation_controller.Init();
base::RunLoop().RunUntilIdle();
// kSecondaryAccount1 is not present
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount1GaiaId),
"");
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount2GaiaId),
"6");
EXPECT_EQ(edu_coexistence::GetAcceptedToSVersion(profile(),
kSecondaryAccount3GaiaId),
"7");
}
} // namespace ash
|