Convert the old cnfvar module into a cnfvar string parsing module
[pyi2ncommon] / test / cnfvar / test_string.py
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
24 import os
25 import unittest
26
27 from src.cnfvar import string as cnfvar_old
28
29 #
30 #                                  test data
31 #
32
33 # model cnf tree
34 demo_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
62 demo_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
86 demo_nonascii = r"""
87 1  USER,1: "admin"
88 2     (1) USER_DISABLED,0: "0"
89 3     (1) USER_FULLNAME,0: "Administrator"
90 4     (1) USER_GROUPWARE_FOLDER_CALENDAR,0: "INBOX/Kalender"
91 5     (1) USER_GROUPWARE_FOLDER_CONTACTS,0: "INBOX/Kontakte"
92 6     (1) USER_GROUPWARE_FOLDER_DRAFTS,0: "INBOX/Entwürfe"
93 7     (1) USER_GROUPWARE_FOLDER_NOTES,0: "INBOX/Notizen"
94 8     (1) USER_GROUPWARE_FOLDER_OUTBOX,0: "INBOX/Gesendete Elemente"
95 9     (1) USER_GROUPWARE_FOLDER_TASKS,0: "INBOX/Aufgaben"
96 10    (1) USER_GROUPWARE_FOLDER_TRASH,0: "INBOX/Gelöschte Elemente"
97 11    (1) USER_GROUP_MEMBER_REF,0: "1"
98 12    (1) USER_GROUP_MEMBER_REF,1: "2"
99 13    (1) USER_PASSWORD,0: "test1234"
100 """
101
102 demo_latin1crap = demo_nonascii.encode('latin1')
103
104 demo_cnf_group = """
105 1 GROUP,1: "Administratoren"
106 2    (1) GROUP_ACCESS_GO_ONLINE_ALLOWED,0: "1"
107 3    (1) GROUP_EMAILFILTER_BAN_FILTERLIST_REF,0: "-1"
108 4    (1) GROUP_EMAIL_RELAY_RIGHTS,0: "RELAY_FROM_INTRANET"
109 5    (1) GROUP_PROXY_PROFILE_REF,0: "1"
110 """
111
112 demo_cnf_group_bytes = demo_cnf_group.encode("latin-1")
113
114 demo_cnf_filter = b"""
115 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
116 2    (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
117 3    (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
118 4    (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: ""
119 5       (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"
120 6       (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
121 7    (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"
122 8    (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
123 """
124
125 demo_cnf_comments = b"""
126 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
127 2    (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
128 3    (1) EMAILFILTER_BAN_FILTERLIST_EXTENSIONS,0: ""
129 4    (1) EMAILFILTER_BAN_FILTERLIST_MIMETYPES,0: "" # foo
130 5       (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,0: "text/plain"#bar
131 6       (4) EMAILFILTER_BAN_FILTERLIST_MIMETYPES_NAME,1: "text/html"
132 7    (1) EMAILFILTER_BAN_FILTERLIST_MODE,0: "ALLOW"     # baz
133 8    (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
134 """
135
136 demo_cnf_escaped_quotes = """
137 1 HERE_BE_QUOTES,0: "\""
138 2 HERE_BE_QUOTES,1: "foo\"bar\"\"\"baz"
139 3 HERE_BE_QUOTES,2: "unquo\\\"table"
140 4 HERE_BE_QUOTES,3: "unquo\\\\\"\"table"
141 """
142
143 #
144 #                                 test class
145 #
146
147 class 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
196 class 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
212 if __name__ == '__main__':
213     unittest.main()