1 // Copyright 2019 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 "chrome/browser/apps/user_type_filter.h"
6 
7 #include <memory>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include "base/values.h"
13 #include "chrome/browser/policy/profile_policy_connector.h"
14 #include "chrome/browser/supervised_user/supervised_user_constants.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "content/public/test/browser_task_environment.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace apps {
20 
21 namespace {
22 
23 // Helper that simulates Json file with embedded user type filter.
CreateJsonWithFilter(const std::vector<std::string> & user_types)24 std::unique_ptr<base::DictionaryValue> CreateJsonWithFilter(
25     const std::vector<std::string>& user_types) {
26   auto root = std::make_unique<base::DictionaryValue>();
27   base::ListValue filter;
28   for (const auto& user_type : user_types)
29     filter.Append(base::Value(user_type));
30   root->SetKey(kKeyUserType, std::move(filter));
31   return root;
32 }
33 
34 }  // namespace
35 
36 class UserTypeFilterTest : public testing::Test {
37  public:
38   UserTypeFilterTest() = default;
39   UserTypeFilterTest(const UserTypeFilterTest&) = delete;
40   UserTypeFilterTest& operator=(const UserTypeFilterTest&) = delete;
41   ~UserTypeFilterTest() override = default;
42 
43  protected:
44   // Helper that creates simple test profile.
CreateProfile()45   std::unique_ptr<TestingProfile> CreateProfile() {
46     TestingProfile::Builder profile_builder;
47     return profile_builder.Build();
48   }
49 
50   // Helper that creates simple test guest profile.
CreateGuestProfile()51   std::unique_ptr<TestingProfile> CreateGuestProfile() {
52     TestingProfile::Builder profile_builder;
53     profile_builder.SetGuestSession();
54     return profile_builder.Build();
55   }
56 
Match(const std::unique_ptr<TestingProfile> & profile,const std::unique_ptr<base::Value> & json_root)57   bool Match(const std::unique_ptr<TestingProfile>& profile,
58              const std::unique_ptr<base::Value>& json_root) {
59     return UserTypeMatchesJsonUserType(
60         DetermineUserType(profile.get()), std::string() /* app_id */,
61         json_root.get(), nullptr /* default_user_types */);
62   }
63 
MatchDefault(const std::unique_ptr<TestingProfile> & profile,const base::ListValue & default_user_types)64   bool MatchDefault(const std::unique_ptr<TestingProfile>& profile,
65                     const base::ListValue& default_user_types) {
66     base::DictionaryValue json_root;
67     return UserTypeMatchesJsonUserType(DetermineUserType(profile.get()),
68                                        std::string() /* app_id */, &json_root,
69                                        &default_user_types);
70   }
71 
72  private:
73   // To support context of browser threads.
74   content::BrowserTaskEnvironment task_environment_;
75 };
76 
77 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
TEST_F(UserTypeFilterTest,ChildUser)78 TEST_F(UserTypeFilterTest, ChildUser) {
79   const auto profile = CreateProfile();
80   profile->SetSupervisedUserId(supervised_users::kChildAccountSUID);
81   EXPECT_FALSE(Match(profile, CreateJsonWithFilter({kUserTypeUnmanaged})));
82   EXPECT_TRUE(Match(profile, CreateJsonWithFilter({kUserTypeChild})));
83   EXPECT_TRUE(Match(
84       profile, CreateJsonWithFilter({kUserTypeUnmanaged, kUserTypeChild})));
85 }
86 #endif  // BUILDFLAG(ENABLE_SUPERVISED_USERS)
87 
TEST_F(UserTypeFilterTest,GuestUser)88 TEST_F(UserTypeFilterTest, GuestUser) {
89   auto profile = CreateGuestProfile();
90   EXPECT_FALSE(Match(profile, CreateJsonWithFilter({kUserTypeUnmanaged})));
91   EXPECT_TRUE(Match(profile, CreateJsonWithFilter({kUserTypeGuest})));
92   EXPECT_TRUE(Match(
93       profile, CreateJsonWithFilter({kUserTypeUnmanaged, kUserTypeGuest})));
94 }
95 
TEST_F(UserTypeFilterTest,ManagedUser)96 TEST_F(UserTypeFilterTest, ManagedUser) {
97   const auto profile = CreateProfile();
98   profile->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);
99   EXPECT_FALSE(Match(profile, CreateJsonWithFilter({kUserTypeUnmanaged})));
100   EXPECT_TRUE(Match(profile, CreateJsonWithFilter({kUserTypeManaged})));
101   EXPECT_TRUE(Match(
102       profile, CreateJsonWithFilter({kUserTypeUnmanaged, kUserTypeManaged})));
103 }
104 
TEST_F(UserTypeFilterTest,SupervisedUser)105 TEST_F(UserTypeFilterTest, SupervisedUser) {
106   const auto profile = CreateProfile();
107   profile->SetSupervisedUserId("asdf");
108   EXPECT_FALSE(Match(profile, CreateJsonWithFilter({kUserTypeUnmanaged})));
109   EXPECT_TRUE(Match(profile, CreateJsonWithFilter({kUserTypeSupervised})));
110   EXPECT_TRUE(Match(profile, CreateJsonWithFilter(
111                                  {kUserTypeUnmanaged, kUserTypeSupervised})));
112 }
113 
TEST_F(UserTypeFilterTest,UnmanagedUser)114 TEST_F(UserTypeFilterTest, UnmanagedUser) {
115   EXPECT_TRUE(
116       Match(CreateProfile(), CreateJsonWithFilter({kUserTypeUnmanaged})));
117 }
118 
TEST_F(UserTypeFilterTest,EmptyFilter)119 TEST_F(UserTypeFilterTest, EmptyFilter) {
120   EXPECT_FALSE(Match(CreateProfile(), CreateJsonWithFilter({})));
121 }
122 
TEST_F(UserTypeFilterTest,DefaultFilter)123 TEST_F(UserTypeFilterTest, DefaultFilter) {
124   auto profile = CreateProfile();
125   base::ListValue default_filter;
126   default_filter.Append(base::Value(kUserTypeUnmanaged));
127   default_filter.Append(base::Value(kUserTypeGuest));
128 
129   // Unmanaged user.
130   EXPECT_TRUE(MatchDefault(profile, default_filter));
131   // Guest user.
132   EXPECT_TRUE(MatchDefault(CreateGuestProfile(), default_filter));
133 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
134   // Child user.
135   profile->SetSupervisedUserId(supervised_users::kChildAccountSUID);
136   EXPECT_FALSE(MatchDefault(profile, default_filter));
137   // Supervised user.
138   // TODO(crbug.com/971311): Remove the next assert test once legacy supervised
139   // user code has been fully removed.
140   profile->SetSupervisedUserId("asdf");
141   EXPECT_FALSE(MatchDefault(profile, default_filter));
142 #endif  // BUILDFLAG(ENABLE_SUPERVISED_USERS)
143   // Managed user.
144   profile = CreateProfile();
145   profile->GetProfilePolicyConnector()->OverrideIsManagedForTesting(true);
146   EXPECT_FALSE(MatchDefault(profile, default_filter));
147 }
148 
149 }  // namespace apps
150