Convert the old cnfvar module into a cnfvar string parsing module
[pyi2ncommon] / test / cnfvar / test_string.py
CommitLineData
b0075fa2
PD
1#!/usr/bin/env python
2# This Python file uses the following encoding: utf-8
3
4# The software in this package is distributed under the GNU General
5# Public License version 2 (with a special exception described below).
6#
7# A copy of GNU General Public License (GPL) is included in this distribution,
8# in the file COPYING.GPL.
9#
10# As a special exception, if other files instantiate templates or use macros
11# or inline functions from this file, or you compile this file and link it
12# with other works to produce a work based on this file, this file
13# does not by itself cause the resulting work to be covered
14# by the GNU General Public License.
15#
16# However the source code for this file must still be made available
17# in accordance with section (3) of the GNU General Public License.
18#
19# This exception does not invalidate any other reasons why a work based
20# on this file might be covered by the GNU General Public License.
21#
22# Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
23
24import os
25import unittest
26
27from src.cnfvar import string as cnfvar_old
28
29#
30# test data
31#
32
33# model cnf tree
34demo_cnfvar = {"cnf": [
35 {
36 "varname": "MY_FAVORITE_CNF_VAR",
37 "instance": 1337,
38 "number": 42,
39 "data": "string conf content",
40 "comment": ""
41 },
42 {
43 "varname": "SOME_NESTED_CNF_VAR",
44 "instance": 0,
45 "number": 23,
46 "data": "999",
47 "comment": "",
48 "children": [
49 {
50 "varname": "SOME_CHILD_VAR",
51 "instance": 1,
52 "number": 24,
53 "data": "2014",
54 "parent": 23,
55 "comment": ""
56 }
57 ]
58 },
59]}
60
61# duplicate line number
62demo_invalid_cnfvar = {"cnf": [
63 {
64 "varname": "SOME_PARTICULARLY_TASTY_CNF_VAR",
65 "instance": 1337,
66 "number": 23,
67 "data": "classic wingers",
68 "comment": ""
69 },
70 {
71 "varname": "EXTRAORDINARILY_FANCY_CNF_VAR",
72 "instance": 1,
73 "number": 42,
74 "data": "ab mentions",
75 "comment": ""
76 },
77 {
78 "varname": "ANOTHER_POPULAR_CNF_VAR",
79 "instance": 0,
80 "number": 42,
81 "data": "notches",
82 "comment": ""
83 }
84]}
85
86demo_nonascii = r"""
871 USER,1: "admin"
882 (1) USER_DISABLED,0: "0"
893 (1) USER_FULLNAME,0: "Administrator"
904 (1) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
915 (1) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
926 (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
937 (1) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
948 (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
959 (1) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
9610 (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
9711 (1) USER_GROUP_MEMBER_REF,0: "1"
9812 (1) USER_GROUP_MEMBER_REF,1: "2"
9913 (1) USER_PASSWORD,0: "test1234"
100"""
101
102demo_latin1crap = demo_nonascii.encode('latin1')
103
104demo_cnf_group = """
1051 GROUP,1: "Administratoren"
1062 (1) GROUP_ACCESS_GO_ONLINE_ALLOWED,0: "1"
1073 (1) GROUP_EMAILFILTER_BAN_FILTERLIST_REF,0: "-1"
1084 (1) GROUP_EMAIL_RELAY_RIGHTS,0: "RELAY_FROM_INTRANET"
1095 (1) GROUP_PROXY_PROFILE_REF,0: "1"
110"""
111
112demo_cnf_group_bytes = demo_cnf_group.encode("latin-1")
113
114demo_cnf_filter = b"""
1151 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
1162 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
1173 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
1184 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: ""
1195 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"
1206 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
1217 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"
1228 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
123"""
124
125demo_cnf_comments = b"""
1261 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
1272 (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
1283 (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
1294 (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" # foo
1305 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"#bar
1316 (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
1327 (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW" # baz
1338 (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
134"""
135
136demo_cnf_escaped_quotes = """
1371 HERE_BE_QUOTES,0: "\""
1382 HERE_BE_QUOTES,1: "foo\"bar\"\"\"baz"
1393 HERE_BE_QUOTES,2: "unquo\\\"table"
1404 HERE_BE_QUOTES,3: "unquo\\\\\"\"table"
141"""
142
143#
144# test class
145#
146
147class CnfVarUnittest(unittest.TestCase):
148
149 def test_print_cnf(self):
150 with open(os.devnull, "w") as devnull:
151 print(demo_cnfvar, file=devnull)
152
153 def test_parse_cnf_simple_str(self):
154 cnf = cnfvar_old.read_cnf(demo_cnf_group)
155 with open(os.devnull, "w") as devnull:
156 print(cnf, file=devnull)
157
158 def test_parse_cnf_simple_bytes(self):
159 cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes)
160 with open(os.devnull, "w") as devnull:
161 print(cnf, file=devnull)
162
163 def test_parse_cnf_nested(self):
164 cnf = cnfvar_old.read_cnf(demo_cnf_filter)
165 with open(os.devnull, "w") as devnull:
166 print(cnf, file=devnull)
167
168 def test_parse_cnf_comments(self):
169 cnf = cnfvar_old.read_cnf(demo_cnf_comments)
170 with open(os.devnull, "w") as devnull:
171 print(cnf, file=devnull)
172
173 def test_print_cnf_garbage(self):
174 try:
175 with open(os.devnull, "w") as devnull:
176 print(demo_invalid_cnfvar, file=devnull)
177 except cnfvar_old.InvalidCNF:
178 print ("Caught the duplicate line, bravo!")
179
180 def test_parse_cnf_quotes(self):
181 cnf = cnfvar_old.read_cnf(demo_cnf_escaped_quotes)
182 with open(os.devnull, "w") as devnull:
183 print(demo_invalid_cnfvar, file=devnull)
184
185 def test_read_nonascii(self):
186 cnf = cnfvar_old.read_cnf(demo_nonascii)
187 with open(os.devnull, "w") as devnull:
188 print(cnf, file=devnull)
189
190 def test_read_latin1(self):
191 cnf = cnfvar_old.read_cnf(demo_latin1crap)
192 with open(os.devnull, "w") as devnull:
193 print(cnf, file=devnull)
194
195
196class CnfVarUnittestVarnameCase(unittest.TestCase):
197 """Tests for verifying that uppercasing/lowercasing of varname works."""
198 # TODO: rethink whether this lower-casing is worth all the effort it causes
199
200 def test_read_cnf_lowercase(self):
201 """Test that after reading, varnames are lowercase."""
202 cnf = cnfvar_old.read_cnf(demo_cnf_group_bytes)
203 for parentvar in cnf['cnf']:
204 self.assertEqual(parentvar['varname'],
205 parentvar['varname'].lower())
206 if 'children' in parentvar:
207 for childvar in parentvar['children']:
208 self.assertEqual(parentvar['varname'],
209 parentvar['varname'].lower())
210
211
212if __name__ == '__main__':
213 unittest.main()