1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
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.
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.
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.
19 # Copyright (c) 2016-2022 Intra2net AG <info@intra2net.com>
22 test_model.py: unit tests for cnfvar/model.py.
24 Tests classes and functions in cnfvar/model.py
26 For help see :py:mod:`unittest`
31 from textwrap import dedent
32 from tempfile import NamedTemporaryFile
34 from src.cnfvar import CnfList
36 CNF_TEST_DATA = dedent("""\
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"
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"
71 EXPECTED_CNF_DATA = dedent("""\
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"
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"
107 class TestModel(unittest.TestCase):
108 """Test the multiple capabilities of the CNF modules."""
110 def test_querying_and_serialization(self):
111 """Test deserializing, querying and serializing a CnfList."""
112 cnfs = CnfList.from_cnf_string(CNF_TEST_DATA)
116 with NamedTemporaryFile() as tmpfile:
117 cnfs.to_cnf_file(tmpfile.name)
119 with open(tmpfile.name, "r", encoding="latin1") as f:
122 self.assertEqual(contents.splitlines(), EXPECTED_CNF_DATA.splitlines())
124 # make sure the result can be parsed again
125 CnfList.from_cnf_string(str(contents))
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)
132 with NamedTemporaryFile() as tmpfile:
133 cnfs.to_json_file(tmpfile.name, renumber=False)
135 with open(tmpfile.name, "r") as f:
138 # make sure the JSON structure is sane
141 # now asserts that reading from JSON works
142 cnfs_2 = CnfList.from_json_string(contents)
146 self.assertEqual(str(cnfs_2).splitlines(), EXPECTED_CNF_DATA.splitlines())
148 # make sure the result can be parsed again
149 CnfList.from_cnf_string(str(cnfs_2))
151 def _modify(self, cnfs):
153 Make test modifications on the list.
155 :param cnfs: list of cnfvars to modify
156 :type cnfs: :py:class:`CnfList`
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.
161 self.assertFalse(cnfs.single_with_name("update_expired").is_enabled())
162 cnfs.single_with_name("update_expired") \
164 self.assertTrue(cnfs.single_with_name("update_expired").is_enabled())
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"
170 user_cnf.add_children(("USER_GROUP_MEMBER_REF", 5))
172 # check correct types and equality
173 self.assertEqual(user_cnf.instance, 2)
174 self.assertEqual(user_cnf.lineno, 11)
176 other_cnf = cnfs.single_with_value("Vordefiniert: Alles verboten")
178 .where(lambda c: c.name == "emailfilter_ban_filterlist_mimetypes") \
180 .comment = "hey this is a comment"
183 if __name__ == '__main__':