• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The Chromium OS 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.
4import unittest
5
6
7from autotest_lib.client.cros.enterprise import policy_group
8"""
9
10d8888b. d88888b  .d8b.  d8888b. .88b  d88. d88888b
1188  `8D 88'     d8' `8b 88  `8D 88'YbdP`88 88'
1288oobY' 88ooooo 88ooo88 88   88 88  88  88 88ooooo
1388`8b   88~~~~~ 88~~~88 88   88 88  88  88 88~~~~~
1488 `88. 88.     88   88 88  .8D 88  88  88 88.
1588   YD Y88888P YP   YP Y8888D' YP  YP  YP Y88888P
16
17This is the unittest file for enterprise_policy_utils.
18If you modify that file, you should be at minimum re-running this file.
19
20Add and correct tests as changes are made to the utils file.
21
22To run the tests, use the following command from your DEV box (outside_chroot):
23
24src/third_party/autotest/files/utils$ python unittest_suite.py \
25autotest_lib.client.cros.enterprise.test_policy_group --debug
26
27Most of the test data are large dictionaries mocking real data. They are stored
28in the ent_policy_unittest_data file (located in this directory).
29
30"""
31
32
33class test_policyGroup(unittest.TestCase):
34
35    def setupFullPolicy(self):
36        test_policy = {'Extension1': {'Policy1': 'Value1'},
37                       'Extension2': {'Policy2': 'Value2'}}
38        test_user_policy = {'Userpolicy1': 1}
39        test_device_policy = {'SystemTimezone': 'v1'}
40        test_suggested_user_policy = {'Suggested1': '1'}
41        self.policy_group_object = policy_group.AllPolicies(True)
42        self.policy_group_object.set_extension_policy(test_policy)
43        self.policy_group_object.set_policy('chrome', test_user_policy, 'user')
44        self.policy_group_object.set_policy('chrome', test_device_policy,
45                                            'device')
46        self.policy_group_object.set_policy('chrome',
47                                            test_suggested_user_policy,
48                                            'suggested_user')
49
50    def test_kiosk_setting(self):
51        self.policy_group_object = policy_group.AllPolicies(True)
52
53        k_id = 'afhcomalholahplbjhnmahkoekoijban'
54
55        z = {
56            'DeviceLocalAccounts': [
57                {'account_id': k_id,
58                 'kiosk_app': {'app_id': k_id},
59                 'type': 1}],
60            'DeviceLocalAccountAutoLoginId': k_id
61        }
62        self.policy_group_object.set_policy('chrome', z,
63                                            'device')
64
65        self.policy_group_object.createNewFakeDMServerJson()
66        self.policy_group_object.updateDMJson()
67        self.policy_group_object._DMJSON['policy_user'] = 'TEST'
68        kiosk_setting = (self.policy_group_object._DMJSON
69                         ['google/chromeos/device']
70                         ['device_local_accounts.account'])
71
72        expected_kiosk_setting = (
73            [{'type': 1,
74              'kiosk_app': {'app_id': 'afhcomalholahplbjhnmahkoekoijban'},
75              'account_id': 'afhcomalholahplbjhnmahkoekoijban'}]
76        )
77        self.assertEqual(kiosk_setting, expected_kiosk_setting)
78
79    def test_Normal(self):
80        policy_group.AllPolicies()
81        self.assertEqual(1, 1)
82
83    def test_extension(self):
84        # Set the "actual" Extension policy
85        extension_test = policy_group.AllPolicies(True)
86        test_set = {'Extension1': {'Policy1': 'Hidden1'}}
87        test_set2 = {'Extension1': {'Policy1': 'Displayed'}}
88
89        # Set the "Visual" Extension policy (ie, What should be displayed)
90        extension_test.set_extension_policy(test_set)
91        extension_test.set_extension_policy(test_set2, True)
92
93        # Verify its displayed correctly when the "visual" dict is requested.
94        self.assertEqual(
95            (extension_test.get_policy_as_dict(True)['extensionPolicies']
96                ['Extension1']['Policy1']['value']),
97            'Displayed')
98
99        # Verify the DM Json remains correct.
100        extension_test.updateDMJson()
101        self.assertEqual(
102            (extension_test._DMJSON['google/chrome/extension']
103             ['Extension1']['Policy1']),
104            'Hidden1')
105
106        # Test the == function will use the "Displayed" value for the check,
107        # not the configured
108
109        extension_test_received = policy_group.AllPolicies()
110        received_set = {
111            'Extension1': {
112                'Policy1': {
113                    'scope': 'user',
114                    'level': 'mandatory',
115                    'value': 'Displayed',
116                    'source': 'cloud'}}}
117
118        extension_test_received.set_extension_policy(received_set)
119
120        self.assertTrue(extension_test == extension_test_received)
121
122    def test_set_policy(self):
123        policy_group_object = policy_group.AllPolicies(True)
124        policy_group_object.set_policy('chrome',
125                                       {'test_policy': 'TestValue'},
126                                       'user')
127        self.assertIn('test_policy', policy_group_object.chrome)
128        # Testing the other values will be covered in the policy unittest
129        self.assertEqual(policy_group_object.chrome['test_policy'].value,
130                         'TestValue')
131
132    def test_ExtPolicy(self):
133        test_policy = {'Extension1': {'Policy1': 'Value1'},
134                       'Extension2': {'Policy2': 'Value2'}}
135        policy_group_object = policy_group.AllPolicies(True)
136        policy_group_object.set_extension_policy(test_policy)
137        for Extension in test_policy:
138            self.assertIn(Extension,
139                          policy_group_object.extension_configured_data)
140            for policy in test_policy[Extension]:
141                self.assertIn(policy,
142                              policy_group_object.extension_configured_data[Extension])
143
144    def test_get_policy_as_dict(self):
145        self.setupFullPolicy()
146        expected_policyDict = {
147            'deviceLocalAccountPolicies': {},
148            'extensionPolicies':
149                {'Extension2':
150                    {'Policy2':
151                        {'scope': 'user',
152                         'level': 'mandatory',
153                         'value': 'Value2',
154                         'source': 'cloud'}},
155                 'Extension1':
156                    {'Policy1':
157                        {'scope': 'user',
158                         'level': 'mandatory',
159                         'value': 'Value1',
160                         'source': 'cloud'}}},
161            'chromePolicies':
162                {'Suggested1':
163                    {'scope': 'user',
164                     'level': 'recommended',
165                     'value': '1',
166                     'source': 'cloud'},
167                 'SystemTimezone':
168                    {'scope': 'machine',
169                     'level': 'mandatory',
170                     'value': 'v1',
171                     'source': 'cloud'},
172                 'Userpolicy1':
173                    {'scope': 'user',
174                     'level': 'mandatory',
175                     'value': 1,
176                     'source': 'cloud'}}}
177        self.assertEqual(self.policy_group_object.get_policy_as_dict(),
178                         expected_policyDict)
179
180    def test_UpdateDMJson(self):
181        self.setupFullPolicy()
182        self.policy_group_object.updateDMJson()
183        expected_DMJson = {
184            'invalidation_name': 'test_policy',
185            'invalidation_source': 16,
186            'google/chromeos/device':
187                {'system_timezone.timezone': 'v1'},
188            'current_key_index': 0,
189            'google/chrome/extension':
190                {'Extension2': {'Policy2': 'Value2'},
191                 'Extension1': {'Policy1': 'Value1'}},
192            'managed_users': ['*'],
193            'google/chromeos/user':
194                {'recommended': {'Suggested1': '1'},
195                 'mandatory': {'Userpolicy1': 1}},
196            'policy_user': None}
197        self.assertEqual(self.policy_group_object._DMJSON, expected_DMJson)
198
199
200if __name__ == '__main__':
201    unittest.main()
202