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
|
// Copyright 2022 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/enterprise/signals/user_delegate_impl.h"
#include <set>
#include "base/files/file_path.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/enterprise/connectors/device_trust/fake_device_trust_connector_service.h"
#include "chrome/test/base/testing_profile.h"
#include "components/policy/core/common/policy_types.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_types.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace enterprise_signals {
using DTCPolicyLevel = enterprise_connectors::DTCPolicyLevel;
namespace {
constexpr char kUserEmail[] = "someEmail@example.com";
constexpr char kOtherUserEmail[] = "someOtherUser@example.com";
constexpr GaiaId::Literal kOtherUserGaiaId("some-other-user-gaia");
base::Value::List GetUrls() {
base::Value::List trusted_urls;
trusted_urls.Append("https://www.example.com");
trusted_urls.Append("example2.example.com");
return trusted_urls;
}
} // namespace
class UserDelegateImplTest : public testing::Test {
protected:
void CreateDelegate(
bool is_managed_user = true,
std::optional<base::FilePath> profile_path = std::nullopt) {
TestingProfile::Builder builder;
builder.OverridePolicyConnectorIsManagedForTesting(is_managed_user);
if (profile_path) {
builder.SetPath(profile_path.value());
}
testing_profile_ = builder.Build();
fake_dt_connector_service_ = std::make_unique<
enterprise_connectors::FakeDeviceTrustConnectorService>(
testing_profile_->GetTestingPrefService());
user_delegate_ = std::make_unique<UserDelegateImpl>(
testing_profile_.get(), identity_test_env_.identity_manager(),
fake_dt_connector_service_.get());
}
content::BrowserTaskEnvironment task_environment_;
signin::IdentityTestEnvironment identity_test_env_;
std::unique_ptr<TestingProfile> testing_profile_;
std::unique_ptr<enterprise_connectors::FakeDeviceTrustConnectorService>
fake_dt_connector_service_;
std::unique_ptr<UserDelegateImpl> user_delegate_;
};
#if BUILDFLAG(IS_CHROMEOS)
// Tests that the sign-in profile is considered as sign-in context.
TEST_F(UserDelegateImplTest, IsSigninContext_True) {
CreateDelegate(/*is_managed_user=*/true,
base::FilePath(ash::kSigninBrowserContextBaseName));
EXPECT_TRUE(user_delegate_->IsSigninContext());
}
// Tests that a regular profile is not considered as sign-in context.
TEST_F(UserDelegateImplTest, IsSigninContext_False) {
CreateDelegate();
EXPECT_FALSE(user_delegate_->IsSigninContext());
}
#endif // BUILDFLAG(IS_CHROMEOS)
// Tests that IsManagedUser returns false when the user is not managed.
TEST_F(UserDelegateImplTest, IsManagedUser_False) {
CreateDelegate(/*is_managed_user=*/false);
EXPECT_FALSE(user_delegate_->IsManagedUser());
}
// Tests that IsManagedUser returns true when the user is managed.
TEST_F(UserDelegateImplTest, IsManagedUser_True) {
CreateDelegate();
EXPECT_TRUE(user_delegate_->IsManagedUser());
}
// Tests that IsSameUser returns false when the identity manager
// is a nullptr.
TEST_F(UserDelegateImplTest, IsSameUser_NullManager) {
// Instantiate all of the dependencies and reset the delegate.
CreateDelegate();
user_delegate_ = std::make_unique<UserDelegateImpl>(
testing_profile_.get(), nullptr, fake_dt_connector_service_.get());
auto account = identity_test_env_.MakePrimaryAccountAvailable(
kUserEmail, signin::ConsentLevel::kSignin);
EXPECT_FALSE(user_delegate_->IsSameUser(account.gaia));
}
// Tests that IsSameUser returns false when given a different user.
TEST_F(UserDelegateImplTest, IsSameManagedUser_DifferentUser) {
auto account = identity_test_env_.MakePrimaryAccountAvailable(
kUserEmail, signin::ConsentLevel::kSignin);
auto other_account = identity_test_env_.MakeAccountAvailable(
kOtherUserEmail, {.set_cookie = true, .gaia_id = kOtherUserGaiaId});
CreateDelegate();
EXPECT_FALSE(user_delegate_->IsSameUser(kOtherUserGaiaId));
}
// Tests that IsSameUser returns false when there is no primary user.
TEST_F(UserDelegateImplTest, IsSameUser_NoPrimaryUser) {
auto other_account = identity_test_env_.MakeAccountAvailable(
kOtherUserEmail, {.set_cookie = true, .gaia_id = kOtherUserGaiaId});
CreateDelegate();
EXPECT_FALSE(user_delegate_->IsSameUser(kOtherUserGaiaId));
}
// Tests that IsSameUser returns true when given the same user, and the
// user did not give Sync consent.
TEST_F(UserDelegateImplTest, IsSameUser_SameUser_Signin) {
auto account = identity_test_env_.MakePrimaryAccountAvailable(
kUserEmail, signin::ConsentLevel::kSignin);
CreateDelegate();
EXPECT_TRUE(user_delegate_->IsSameUser(account.gaia));
}
// Tests that IsSameUser returns true when given the same user, and the
// user gave Sync consent.
TEST_F(UserDelegateImplTest, IsSameUser_SameUser_Sync) {
auto account = identity_test_env_.MakePrimaryAccountAvailable(
kUserEmail, signin::ConsentLevel::kSync);
CreateDelegate();
EXPECT_TRUE(user_delegate_->IsSameUser(account.gaia));
}
// Tests that GetPolicyScopesNeedingSignals returns an empty set when
// no policies are enabled.
TEST_F(UserDelegateImplTest, GetPolicyScopesNeedingSignals_Empty) {
CreateDelegate();
EXPECT_EQ(user_delegate_->GetPolicyScopesNeedingSignals(),
std::set<policy::PolicyScope>());
}
// Tests what GetPolicyScopesNeedingSignals returns when the policy is enabled
// at the user level.
TEST_F(UserDelegateImplTest, GetPolicyScopesNeedingSignals_User) {
CreateDelegate();
fake_dt_connector_service_->UpdateInlinePolicy(GetUrls(),
DTCPolicyLevel::kUser);
EXPECT_EQ(user_delegate_->GetPolicyScopesNeedingSignals(),
std::set<policy::PolicyScope>({policy::POLICY_SCOPE_USER}));
}
// Tests what GetPolicyScopesNeedingSignals returns when the policy is enabled
// at the browser level.
TEST_F(UserDelegateImplTest, GetPolicyScopesNeedingSignals_Browser) {
CreateDelegate();
fake_dt_connector_service_->UpdateInlinePolicy(GetUrls(),
DTCPolicyLevel::kBrowser);
EXPECT_EQ(user_delegate_->GetPolicyScopesNeedingSignals(),
std::set<policy::PolicyScope>({policy::POLICY_SCOPE_MACHINE}));
}
// Tests what GetPolicyScopesNeedingSignals returns when the policy is enabled
// at the both the user and browser levels.
TEST_F(UserDelegateImplTest, GetPolicyScopesNeedingSignals_UserAndBrowser) {
CreateDelegate();
fake_dt_connector_service_->UpdateInlinePolicy(GetUrls(),
DTCPolicyLevel::kBrowser);
fake_dt_connector_service_->UpdateInlinePolicy(GetUrls(),
DTCPolicyLevel::kUser);
EXPECT_EQ(user_delegate_->GetPolicyScopesNeedingSignals(),
std::set<policy::PolicyScope>(
{policy::POLICY_SCOPE_MACHINE, policy::POLICY_SCOPE_USER}));
}
} // namespace enterprise_signals
|