Revert the default instance number back to zero
[pyi2ncommon] / test / cnfvar / test_model.py
CommitLineData
d31714a0
SA
1# The software in this package is distributed under the GNU General
2# Public License version 2 (with a special exception described below).
3#
4# A copy of GNU General Public License (GPL) is included in this distribution,
5# in the file COPYING.GPL.
6#
7# As a special exception, if other files instantiate templates or use macros
8# or inline functions from this file, or you compile this file and link it
9# with other works to produce a work based on this file, this file
10# does not by itself cause the resulting work to be covered
11# by the GNU General Public License.
12#
13# However the source code for this file must still be made available
14# in accordance with section (3) of the GNU General Public License.
15#
16# This exception does not invalidate any other reasons why a work based
17# on this file might be covered by the GNU General Public License.
18#
19# Copyright (c) 2016-2022 Intra2net AG <info@intra2net.com>
20
21"""
22test_model.py: unit tests for cnfvar/model.py.
23
24Tests classes and functions in cnfvar/model.py
25
26For help see :py:mod:`unittest`
27"""
28
29import unittest
30import json
31from textwrap import dedent
32from tempfile import NamedTemporaryFile
33
34from src.cnfvar import CnfList
35
36CNF_TEST_DATA = dedent("""\
37 1 USER,1: "jake"
38 2 (1) USER_DISABLED,0: "0"
39 3 (1) USER_FULLNAME,0: "Jake"
40 4 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
41 5 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Objekte"
42 6 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
43 7 (1) USER_GROUP_MEMBER_REF,0: "100"
44 8 (1) USER_GROUP_MEMBER_REF,1: "2"
45 9 (1) USER_PASSWORD,0: "test1234"
46 11 USER,2: "jill"
47 12 (11) USER_DISABLED,0: "0"
48 13 (11) USER_FULLNAME,0: "Jill"
49 14 (11) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
50 15 (11) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Objekte"
51 16 (11) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
52 17 (11) USER_GROUP_MEMBER_REF,0: "100"
53 18 (11) USER_GROUP_MEMBER_REF,1: "2"
54 19 (11) USER_PASSWORD,0: "test1234"
55 74 EMAILFILTER_BAN_FILTERLIST,0: "Vordefiniert: Alles verboten"
56 75 (74) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
57 76 (74) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
58 77 (74) EMAILFILTER_BAN_FILTERLIST_FILTER_OFFICE_FILES,0: "BY_FILTERLIST"
59 78 (74) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: ""
60 79 (78) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"
61 80 (78) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
62 81 (74) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"
63 82 (74) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
64 1196 UPDATE_EXPIRED,0: "0"
65 1197 UPDATE_URL_BASE,0: "https://update.intra2net.com/"
66 1198 UPDATE_VALIDATION_GROUP,0: "normal"
67 1199 UPS_LOCAL_KILLPOWER_ENABLE,0: "1"
68 """)
69
70
71EXPECTED_CNF_DATA = dedent("""\
72 1 USER,1: "jake"
73 2 (1) USER_DISABLED,0: "0"
74 3 (1) USER_FULLNAME,0: "Jake"
75 4 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
76 5 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Objekte"
77 6 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
78 7 (1) USER_GROUP_MEMBER_REF,0: "100"
79 8 (1) USER_GROUP_MEMBER_REF,1: "2"
80 9 (1) USER_PASSWORD,0: "test1234"
81 10 USER,2: "jane"
82 11 (10) USER_DISABLED,0: "0"
83 12 (10) USER_FULLNAME,0: "Jane"
84 13 (10) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
85 14 (10) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Objekte"
86 15 (10) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
87 16 (10) USER_GROUP_MEMBER_REF,0: "200"
88 17 (10) USER_GROUP_MEMBER_REF,1: "2"
89 18 (10) USER_PASSWORD,0: "test1234"
90 19 (10) USER_GROUP_MEMBER_REF,2: "5"
91 20 EMAILFILTER_BAN_FILTERLIST,0: "Vordefiniert: Alles verboten"
92 21 (20) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
93 22 (20) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
94 23 (20) EMAILFILTER_BAN_FILTERLIST_FILTER_OFFICE_FILES,0: "BY_FILTERLIST"
95 24 (20) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" # hey this is a comment
96 25 (24) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"
97 26 (24) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
98 27 (20) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"
99 28 (20) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
100 29 UPDATE_EXPIRED,0: "1"
101 30 UPDATE_URL_BASE,0: "https://update.intra2net.com/"
102 31 UPDATE_VALIDATION_GROUP,0: "normal"
103 32 UPS_LOCAL_KILLPOWER_ENABLE,0: "1"
104 """)
105
106
107class TestModel(unittest.TestCase):
108 """Test the multiple capabilities of the CNF modules."""
109
110 def test_querying_and_serialization(self):
111 """Test deserializing, querying and serializing a CnfList."""
112 cnfs = CnfList.from_cnf_string(CNF_TEST_DATA)
113
114 self._modify(cnfs)
115
116 with NamedTemporaryFile() as tmpfile:
117 cnfs.to_cnf_file(tmpfile.name)
118
119 with open(tmpfile.name, "r", encoding="latin1") as f:
120 contents = f.read()
121
122 self.assertEqual(contents.splitlines(), EXPECTED_CNF_DATA.splitlines())
123
124 # make sure the result can be parsed again
125 CnfList.from_cnf_string(str(contents))
126
127 def test_querying_and_serialization_json(self):
128 """Test deserializing, querying and serializing a CnfList as JSON."""
129 # first serialize to JSON without renumbering to keep the structure
130 cnfs = CnfList.from_cnf_string(CNF_TEST_DATA)
131
132 with NamedTemporaryFile() as tmpfile:
133 cnfs.to_json_file(tmpfile.name, renumber=False)
134
135 with open(tmpfile.name, "r") as f:
136 contents = f.read()
137
138 # make sure the JSON structure is sane
139 json.loads(contents)
140
141 # now asserts that reading from JSON works
142 cnfs_2 = CnfList.from_json_string(contents)
143 self._modify(cnfs_2)
144 cnfs_2.renumber()
145
146 self.assertEqual(str(cnfs_2).splitlines(), EXPECTED_CNF_DATA.splitlines())
147
148 # make sure the result can be parsed again
149 CnfList.from_cnf_string(str(cnfs_2))
150
151 def _modify(self, cnfs):
152 """
153 Make test modifications on the list.
154
155 :param cnfs: list of cnfvars to modify
156 :type cnfs: :py:class:`CnfList`
157
158 .. note:: we unify this into a method so we can make sure that the test
159 using cnfvar strings and the test using JSON do the exactly same modifications.
160 """
161 self.assertFalse(cnfs.single_with_name("update_expired").is_enabled())
162 cnfs.single_with_name("update_expired") \
163 .enable()
164 self.assertTrue(cnfs.single_with_name("update_expired").is_enabled())
165
166 user_cnf = cnfs.first_with_value("jill")
167 user_cnf.value = "jane"
168 user_cnf.children[1].value = "Jane"
169 user_cnf.children.first_with_name("user_group_member_ref").value = "200"
c89cc126 170 user_cnf.add_children(("USER_GROUP_MEMBER_REF", 5, -1))
d31714a0
SA
171
172 # check correct types and equality
173 self.assertEqual(user_cnf.instance, 2)
174 self.assertEqual(user_cnf.lineno, 11)
175
176 other_cnf = cnfs.single_with_value("Vordefiniert: Alles verboten")
177 other_cnf.children \
178 .where(lambda c: c.name == "emailfilter_ban_filterlist_mimetypes") \
179 .single() \
180 .comment = "hey this is a comment"
181
182
183if __name__ == '__main__':
184 unittest.main()