Mercurial > libervia-backend
comparison src/test/test_memory.py @ 915:6f96ee4d8cc0
tests: tests fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 21 Mar 2014 16:19:46 +0100 |
parents | src/test/test_memory_memory.py@9bac2fc74968 |
children | b3f383ab39da |
comparison
equal
deleted
inserted
replaced
914:1a3ba959f0ab | 915:6f96ee4d8cc0 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.test import helpers | |
22 from twisted.trial import unittest | |
23 import traceback | |
24 import logging | |
25 from constants import Const | |
26 from xml.dom import minidom | |
27 | |
28 | |
29 class MemoryTest(unittest.TestCase): | |
30 | |
31 def setUp(self): | |
32 self.host = helpers.FakeSAT() | |
33 | |
34 def _getParamXML(self, param="1", security_level=None): | |
35 def getParam(name): | |
36 return """ | |
37 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/> | |
38 """ % {'param_name': name, | |
39 'param_label': _(name), | |
40 'security': '' if security_level is None else ('security="%d"' % security_level) | |
41 } | |
42 if param == "1": | |
43 params = getParam(Const.ENABLE_UNIBOX_PARAM) | |
44 elif param == "2": | |
45 params = getParam(Const.PARAM_IN_QUOTES) | |
46 else: | |
47 params = getParam(Const.ENABLE_UNIBOX_PARAM) + getParam(Const.PARAM_IN_QUOTES) | |
48 return """ | |
49 <params> | |
50 <individual> | |
51 <category name="%(category_name)s" label="%(category_label)s"> | |
52 %(params)s | |
53 </category> | |
54 </individual> | |
55 </params> | |
56 """ % { | |
57 'category_name': Const.COMPOSITION_KEY, | |
58 'category_label': _(Const.COMPOSITION_KEY), | |
59 'params': params | |
60 } | |
61 | |
62 def _paramExists(self, param="1", src=None): | |
63 """@return: True is the param (category, name) exists""" | |
64 if param == "1": | |
65 name = Const.ENABLE_UNIBOX_PARAM | |
66 else: | |
67 name = Const.PARAM_IN_QUOTES | |
68 category = Const.COMPOSITION_KEY | |
69 if src is None: | |
70 src = self.host.memory.params.dom.documentElement | |
71 for type_node in src.childNodes: | |
72 # when src comes self.host.memory.params.dom, we have here | |
73 # some "individual" or "general" elements, when it comes | |
74 # from Memory.getParams we have here a "params" elements | |
75 if type_node.nodeName not in ("individual", "general", "params"): | |
76 continue | |
77 for cat_node in type_node.childNodes: | |
78 if cat_node.nodeName != "category" or cat_node.getAttribute("name") != category: | |
79 continue | |
80 for param in cat_node.childNodes: | |
81 if param.nodeName == "param" and param.getAttribute("name") == name: | |
82 return True | |
83 return False | |
84 | |
85 def assertParam_generic(self, param="1", src=None, exists=True, deferred=False): | |
86 msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n" | |
87 if deferred == False: | |
88 # in this stack we can see the line where the error came from, | |
89 # if limit=5, 6 is not enough you can increase the value | |
90 msg += "\n".join(traceback.format_stack(limit=5 if exists else 6)) | |
91 assertion = self._paramExists(param, src) | |
92 getattr(self, "assert%s" % exists)(assertion, msg) | |
93 | |
94 def assertParamExists(self, param="1", src=None): | |
95 self.assertParam_generic(param, src, True) | |
96 | |
97 def assertParamNotExists(self, param="1", src=None): | |
98 self.assertParam_generic(param, src, False) | |
99 | |
100 def assertParamExists_async(self, src, param="1"): | |
101 """@param src: a deferred result from Memory.getParams""" | |
102 self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), True, True) | |
103 | |
104 def assertParamNotExists_async(self, src, param="1"): | |
105 """@param src: a deferred result from Memory.getParams""" | |
106 self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), False, True) | |
107 | |
108 def _getParams(self, security_limit, app='', profile_key='@NONE@'): | |
109 if profile_key == '@NONE@': | |
110 profile_key = '@DEFAULT@' | |
111 return self.host.memory.getParams(security_limit, app, profile_key) | |
112 | |
113 def test_updateParams(self): | |
114 self.host.memory.init() | |
115 # check if the update works | |
116 self.host.memory.updateParams(self._getParamXML()) | |
117 self.assertParamExists() | |
118 previous = self.host.memory.params.dom.cloneNode(True) | |
119 # now check if it is really updated and not duplicated | |
120 self.host.memory.updateParams(self._getParamXML()) | |
121 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8")) | |
122 | |
123 self.host.memory.init() | |
124 # check successive updates (without intersection) | |
125 self.host.memory.updateParams(self._getParamXML('1')) | |
126 self.assertParamExists("1") | |
127 self.assertParamNotExists("2") | |
128 self.host.memory.updateParams(self._getParamXML('2')) | |
129 self.assertParamExists("1") | |
130 self.assertParamExists("2") | |
131 | |
132 previous = self.host.memory.params.dom.cloneNode(True) # save for later | |
133 | |
134 self.host.memory.init() | |
135 # check successive updates (with intersection) | |
136 self.host.memory.updateParams(self._getParamXML('1')) | |
137 self.assertParamExists("1") | |
138 self.assertParamNotExists("2") | |
139 self.host.memory.updateParams(self._getParamXML('both')) | |
140 self.assertParamExists("1") | |
141 self.assertParamExists("2") | |
142 | |
143 # successive updates with or without intersection should have the same result | |
144 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8")) | |
145 | |
146 def test_getParams(self): | |
147 # tests with no security level on the parameter (most secure) | |
148 params = self._getParamXML() | |
149 self.host.memory.init() | |
150 self.host.memory.updateParams(params) | |
151 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | |
152 self._getParams(0).addCallback(self.assertParamNotExists_async) | |
153 self._getParams(1).addCallback(self.assertParamNotExists_async) | |
154 # tests with security level 0 on the parameter (not secure) | |
155 params = self._getParamXML(security_level=0) | |
156 self.host.memory.init() | |
157 self.host.memory.updateParams(params) | |
158 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | |
159 self._getParams(0).addCallback(self.assertParamExists_async) | |
160 self._getParams(1).addCallback(self.assertParamExists_async) | |
161 # tests with security level 1 on the parameter (more secure) | |
162 params = self._getParamXML(security_level=1) | |
163 self.host.memory.init() | |
164 self.host.memory.updateParams(params) | |
165 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | |
166 self._getParams(0).addCallback(self.assertParamNotExists_async) | |
167 self._getParams(1).addCallback(self.assertParamExists_async) | |
168 | |
169 def test_paramsRegisterApp(self): | |
170 | |
171 def register(*args): | |
172 logger = logging.getLogger() | |
173 level = logger.getEffectiveLevel() | |
174 logger.setLevel(logging.INFO) | |
175 self.host.memory.paramsRegisterApp(*args) | |
176 logger.setLevel(level) | |
177 | |
178 # tests with no security level on the parameter (most secure) | |
179 params = self._getParamXML() | |
180 self.host.memory.init() | |
181 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | |
182 self.assertParamExists() | |
183 self.host.memory.init() | |
184 register(params, 0, Const.APP_NAME) | |
185 self.assertParamNotExists() | |
186 self.host.memory.init() | |
187 register(params, 1, Const.APP_NAME) | |
188 self.assertParamNotExists() | |
189 | |
190 # tests with security level 0 on the parameter (not secure) | |
191 params = self._getParamXML(security_level=0) | |
192 self.host.memory.init() | |
193 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | |
194 self.assertParamExists() | |
195 self.host.memory.init() | |
196 register(params, 0, Const.APP_NAME) | |
197 self.assertParamExists() | |
198 self.host.memory.init() | |
199 register(params, 1, Const.APP_NAME) | |
200 self.assertParamExists() | |
201 | |
202 # tests with security level 1 on the parameter (more secure) | |
203 params = self._getParamXML(security_level=1) | |
204 self.host.memory.init() | |
205 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | |
206 self.assertParamExists() | |
207 self.host.memory.init() | |
208 register(params, 0, Const.APP_NAME) | |
209 self.assertParamNotExists() | |
210 self.host.memory.init() | |
211 register(params, 1, Const.APP_NAME) | |
212 self.assertParamExists() | |
213 | |
214 def test_paramsRegisterApp_getParams(self): | |
215 # test retrieving the parameter for a specific frontend | |
216 self.host.memory.init() | |
217 params = self._getParamXML(security_level=1) | |
218 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) | |
219 self._getParams(1, '').addCallback(self.assertParamExists_async) | |
220 self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async) | |
221 self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async) |