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 ast
5import copy
6from mock import patch
7import os
8import unittest
9
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.cros.enterprise import enterprise_policy_utils as epu
12
13
14"""
15
16This is the unittest file for enterprise_policy_utils.
17If you modify that file, you should be at minimum re-running this file.
18
19Add and correct tests as changes are made to the utils file.
20
21To run the tests, use the following command from your DEV box (outside_chroot):
22
23src/third_party/autotest/files/utils$ python unittest_suite.py \
24autotest_lib.client.cros.enterprise.enterprise_policy_utils_unittest --debug
25
26Most of the test data are large dictionaries mocking real data. They are stored
27in the ent_policy_unittest_data file (located in this directory).
28
29"""
30
31# Load the test data
32TEST_DATA = {}
33folderDir = os.path.join(os.path.dirname(__file__))
34fileName = 'ent_policy_unittest_data'
35fullPath = os.path.join(folderDir, fileName)
36
37with open(fullPath) as t:
38    f = (t.readlines())
39    for variable in f:
40        name, data = variable.split('=')
41        TEST_DATA[name] = ast.literal_eval(data)
42
43# Set the base path for the Mock
44PATCH_BASE = 'autotest_lib.client.cros.enterprise.enterprise_policy_utils'
45
46
47class TestPolicyUtils_get_all_policies(unittest.TestCase):
48    """
49    Test the "get_all_policies" function.
50
51    Mock the reply from the API with an example policy.
52
53    """
54    FX_NAME = '_get_pol_from_api'
55    PATCH_PATH = '{}.{}'.format(PATCH_BASE, FX_NAME)
56
57    @patch(PATCH_PATH)
58    def test_Normal(self, get_pol_mock):
59        get_pol_mock.return_value = TEST_DATA['TEST_RAW']
60        self.assertEqual(epu.get_all_policies(None), TEST_DATA['TEST_RAW'])
61
62    @patch(PATCH_PATH)
63    def test_NoAPIResponse(self, get_pol_mock):
64        get_pol_mock.return_value = None
65        self.assertRaises(error.TestError, epu.get_all_policies, None)
66
67
68class TestPolicyUtils_reformat_policies(unittest.TestCase):
69    """Test the _reformat_policies function and the following private
70        methods:
71            _remove_visual_formatting
72
73    """
74
75    def test_NormalData(self):
76        # Tests a policy with a "chromePolicies" and 1 "extensionPolicies".
77        subtest_data = copy.deepcopy(TEST_DATA['TEST_RAW'])
78        epu._reformat_policies(subtest_data)
79        self.assertEqual(subtest_data, TEST_DATA['TEST_RAW'])
80
81    def test_no_data(self):
82        subtest_data = {}
83        epu._reformat_policies(subtest_data)
84        self.assertEqual(subtest_data, {})
85
86        subtest_data2 = {'deviceLocalAccountPolicies': {},
87                         'extensionPolicies': {},
88                         'chromePolicies': {}}
89        expected = copy.deepcopy(subtest_data2)
90        epu._reformat_policies(subtest_data2)
91        self.assertEqual(subtest_data2, expected)
92
93    def test_partial_data(self):
94        # "chromePolicies" contains data, "extensionPolicies" has an extension
95        # with no policies.
96        subtest_data = copy.deepcopy(TEST_DATA['PARTIAL_RAW'])
97        epu._reformat_policies(subtest_data)
98        self.assertEqual(subtest_data, TEST_DATA['PARTIAL_RAW'])
99
100    def test_mult_extension(self):
101        subtest_data = copy.deepcopy(TEST_DATA['TEST_MULTI_EXT'])
102        epu._reformat_policies(subtest_data)
103        self.assertEqual(subtest_data, TEST_DATA['TEST_MULTI_EXT'])
104
105    def test_unicode_dict(self):
106        # Specifically will check if the _remove_visual_formatting
107        # function will remove visual formatting. e.g. "\n        "
108        subtest_data = copy.deepcopy(TEST_DATA['POL_WITH_UNICODE'])
109        epu._reformat_policies(subtest_data)
110        self.assertEqual(subtest_data, TEST_DATA['POL_WITH_UNICODE_PARSED'])
111
112    def test_string_value(self):
113        # Checks that a unicode string is not modified.
114        subtest_data = copy.deepcopy(TEST_DATA['POL_WITH_STRING'])
115        epu._reformat_policies(subtest_data)
116        self.assertEqual(subtest_data, TEST_DATA['POL_WITH_STRING'])
117
118
119if __name__ == '__main__':
120    unittest.main()
121