Mercurial > libervia-backend
comparison src/test/test_memory_memory.py @ 795:6625558371db
test: added tests for the plugin "room game" + rename other test files
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 10 Jan 2014 18:20:30 +0100 |
parents | src/test/test_memory.py@cb2db0d85029 |
children | 1fe00f0c9a91 |
comparison
equal
deleted
inserted
replaced
794:52c4b755aba6 | 795:6625558371db |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 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, name=None, category=None, security_level=None): | |
35 if not name: | |
36 name = Const.ENABLE_UNIBOX_PARAM | |
37 if not category: | |
38 category = Const.ENABLE_UNIBOX_KEY | |
39 return """ | |
40 <params> | |
41 <individual> | |
42 <category name="%(category_name)s" label="%(category_label)s"> | |
43 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/> | |
44 </category> | |
45 </individual> | |
46 </params> | |
47 """ % { | |
48 'category_name': category, | |
49 'category_label': _(category), | |
50 'param_name': name, | |
51 'param_label': _(name), | |
52 'security': '' if security_level is None else ('security="%d"' % security_level) | |
53 } | |
54 | |
55 def _paramExists(self, name=None, category=None, src=None): | |
56 """@return: True is the param (category, name) exists""" | |
57 if not name: | |
58 name = Const.ENABLE_UNIBOX_PARAM | |
59 if not category: | |
60 category = Const.ENABLE_UNIBOX_KEY | |
61 if src is None: | |
62 src = self.host.memory.params.dom.documentElement | |
63 for type_node in src.childNodes: | |
64 # when src comes self.host.memory.params.dom, we have here | |
65 # some "individual" or "general" elements, when it comes | |
66 # from Memory.getParams we have here a "params" elements | |
67 if type_node.nodeName not in ("individual", "general", "params"): | |
68 continue | |
69 for cat_node in type_node.childNodes: | |
70 if cat_node.nodeName != "category" or cat_node.getAttribute("name") != category: | |
71 continue | |
72 for param in cat_node.childNodes: | |
73 if param.nodeName == "param" and param.getAttribute("name") == name: | |
74 return True | |
75 return False | |
76 | |
77 def assertParam_generic(self, src=None, exists=True, deferred=False): | |
78 msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n" | |
79 if deferred == False: | |
80 # in this stack we can see the line where the error came from, | |
81 # if limit=5, 6 is not enough you can increase the value | |
82 msg += "\n".join(traceback.format_stack(limit=5 if exists else 6)) | |
83 assertion = self._paramExists(src=src) | |
84 getattr(self, "assert%s" % exists)(assertion, msg) | |
85 | |
86 def assertParamExists(self, src=None): | |
87 self.assertParam_generic(src, True) | |
88 | |
89 def assertParamNotExists(self, src=None): | |
90 self.assertParam_generic(src, False) | |
91 | |
92 def assertParamExists_async(self, src): | |
93 """@param src: a deferred result from Memory.getParams""" | |
94 self.assertParam_generic(minidom.parseString(src.encode("utf-8")), True, True) | |
95 | |
96 def assertParamNotExists_async(self, src): | |
97 """@param src: a deferred result from Memory.getParams""" | |
98 self.assertParam_generic(minidom.parseString(src.encode("utf-8")), False, True) | |
99 | |
100 def _getParams(self, security_limit, app='', profile_key='@NONE@'): | |
101 if profile_key == '@NONE@': | |
102 profile_key = '@DEFAULT@' | |
103 return self.host.memory.getParams(security_limit, app, profile_key) | |
104 | |
105 def test_updateParams(self): | |
106 self.host.memory.init() | |
107 # check if the update works | |
108 self.host.memory.updateParams(self._getParamXML()) | |
109 self.assertParamExists() | |
110 previous = self.host.memory.params.dom.cloneNode(True) | |
111 # now check if it is really updated and not duplicated | |
112 self.host.memory.updateParams(self._getParamXML()) | |
113 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8")) | |
114 | |
115 def test_getParams(self): | |
116 # tests with no security level on the parameter (most secure) | |
117 params = self._getParamXML() | |
118 self.host.memory.init() | |
119 self.host.memory.updateParams(params) | |
120 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | |
121 self._getParams(0).addCallback(self.assertParamNotExists_async) | |
122 self._getParams(1).addCallback(self.assertParamNotExists_async) | |
123 # tests with security level 0 on the parameter (not secure) | |
124 params = self._getParamXML(security_level=0) | |
125 self.host.memory.init() | |
126 self.host.memory.updateParams(params) | |
127 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | |
128 self._getParams(0).addCallback(self.assertParamExists_async) | |
129 self._getParams(1).addCallback(self.assertParamExists_async) | |
130 # tests with security level 1 on the parameter (more secure) | |
131 params = self._getParamXML(security_level=1) | |
132 self.host.memory.init() | |
133 self.host.memory.updateParams(params) | |
134 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | |
135 self._getParams(0).addCallback(self.assertParamNotExists_async) | |
136 self._getParams(1).addCallback(self.assertParamExists_async) | |
137 | |
138 def test_paramsRegisterApp(self): | |
139 | |
140 def register(*args): | |
141 logger = logging.getLogger() | |
142 level = logger.getEffectiveLevel() | |
143 logger.setLevel(logging.INFO) | |
144 self.host.memory.paramsRegisterApp(*args) | |
145 logger.setLevel(level) | |
146 | |
147 # tests with no security level on the parameter (most secure) | |
148 params = self._getParamXML() | |
149 self.host.memory.init() | |
150 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | |
151 self.assertParamExists() | |
152 self.host.memory.init() | |
153 register(params, 0, Const.APP_NAME) | |
154 self.assertParamNotExists() | |
155 self.host.memory.init() | |
156 register(params, 1, Const.APP_NAME) | |
157 self.assertParamNotExists() | |
158 # tests with security level 0 on the parameter (not secure) | |
159 params = self._getParamXML(security_level=0) | |
160 self.host.memory.init() | |
161 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | |
162 self.assertParamExists() | |
163 self.host.memory.init() | |
164 register(params, 0, Const.APP_NAME) | |
165 self.assertParamExists() | |
166 self.host.memory.init() | |
167 register(params, 1, Const.APP_NAME) | |
168 self.assertParamExists() | |
169 # tests with security level 1 on the parameter (more secure) | |
170 params = self._getParamXML(security_level=1) | |
171 self.host.memory.init() | |
172 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | |
173 self.assertParamExists() | |
174 self.host.memory.init() | |
175 register(params, 0, Const.APP_NAME) | |
176 self.assertParamNotExists() | |
177 self.host.memory.init() | |
178 register(params, 1, Const.APP_NAME) | |
179 self.assertParamExists() | |
180 | |
181 def test_paramsRegisterApp_getParams(self): | |
182 # test retrieving the parameter for a specific frontend | |
183 self.host.memory.init() | |
184 params = self._getParamXML(security_level=1) | |
185 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) | |
186 self._getParams(1, '').addCallback(self.assertParamExists_async) | |
187 self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async) | |
188 self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async) |