comparison src/test/test_memory.py @ 782:0e5807193721

test: added some tests for Memory
author souliane <souliane@mailoo.org>
date Sat, 04 Jan 2014 17:16:40 +0100
parents
children c3acc1298a2f
comparison
equal deleted inserted replaced
781:80ab2b58e205 782:0e5807193721
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 from constants import Const
25 from xml.dom import minidom
26
27
28 class MemoryTest(unittest.TestCase):
29
30 def setUp(self):
31 self.host = helpers.FakeSAT()
32
33 def _getParamXML(self, name=None, category=None, security_level=None):
34 if not name:
35 name = Const.ENABLE_UNIBOX_PARAM
36 if not category:
37 category = Const.ENABLE_UNIBOX_KEY
38 return """
39 <params>
40 <individual>
41 <category name="%(category_name)s" label="%(category_label)s">
42 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/>
43 </category>
44 </individual>
45 </params>
46 """ % {
47 'category_name': category,
48 'category_label': _(category),
49 'param_name': name,
50 'param_label': _(name),
51 'security': '' if security_level is None else ('security="%d"' % security_level)
52 }
53
54 def _paramExists(self, name=None, category=None, src=None):
55 """@return: True is the param (category, name) exists"""
56 if not name:
57 name = Const.ENABLE_UNIBOX_PARAM
58 if not category:
59 category = Const.ENABLE_UNIBOX_KEY
60 if src is None:
61 src = self.host.memory.params.dom.documentElement
62 for type_node in src.childNodes:
63 # when src comes self.host.memory.params.dom, we have here
64 # some "individual" or "general" elements, when it comes
65 # from Memory.getParams we have here a "params" elements
66 if type_node.nodeName not in ("individual", "general", "params"):
67 continue
68 for cat_node in type_node.childNodes:
69 if cat_node.nodeName != "category" or cat_node.getAttribute("name") != category:
70 continue
71 for param in cat_node.childNodes:
72 if param.nodeName == "param" and param.getAttribute("name") == name:
73 return True
74 return False
75
76 def _assert(self, src, true=True):
77 """@param src: a deferred result from Memory.getParams"""
78 assertion = self._paramExists(src=minidom.parseString(src.encode("utf-8")))
79 if not true:
80 assertion = not assertion
81 try:
82 assert(assertion)
83 except AssertionError as e:
84 # in this stack we can see the line where the error came from,
85 # if limit=5 is not enough you can increase the value
86 print "---------------------- stack start ----------------------"
87 traceback.print_stack(limit=5 if true else 6)
88 print "----------------------- stack end -----------------------"
89 raise e
90
91 def _assert_not(self, src):
92 """@param src: a deferred result from Memory.getParams"""
93 self._assert(src, False)
94
95 def test_updateParams(self):
96 self.host.memory.init()
97 # check if the update works
98 self.host.memory.updateParams(self._getParamXML())
99 assert(self._paramExists())
100 previous = self.host.memory.params.dom.cloneNode(True)
101 # now check if it is really updated and not duplicated
102 self.host.memory.updateParams(self._getParamXML())
103 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8"))
104
105 def test_getParams(self):
106 # tests with no security level on the parameter (most secure)
107 params = self._getParamXML()
108 self.host.memory.init()
109 self.host.memory.updateParams(params)
110 self.host.memory.getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert)
111 self.host.memory.getParams(0, app='').addCallback(self._assert_not)
112 self.host.memory.getParams(1, app='').addCallback(self._assert_not)
113 # tests with security level 0 on the parameter (not secure)
114 params = self._getParamXML(security_level=0)
115 self.host.memory.init()
116 self.host.memory.updateParams(params)
117 self.host.memory.getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert)
118 self.host.memory.getParams(0, app='').addCallback(self._assert)
119 self.host.memory.getParams(1, app='').addCallback(self._assert)
120 # tests with security level 1 on the parameter (more secure)
121 params = self._getParamXML(security_level=1)
122 self.host.memory.init()
123 self.host.memory.updateParams(params)
124 self.host.memory.getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert)
125 self.host.memory.getParams(0).addCallback(self._assert_not)
126 self.host.memory.getParams(1).addCallback(self._assert)
127
128 def test_paramsRegisterApp(self):
129 # tests with no security level on the parameter (most secure)
130 params = self._getParamXML()
131 self.host.memory.init()
132 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
133 assert(self._paramExists())
134 self.host.memory.init()
135 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME)
136 assert(not self._paramExists())
137 self.host.memory.init()
138 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
139 assert(not self._paramExists())
140 # tests with security level 0 on the parameter (not secure)
141 params = self._getParamXML(security_level=0)
142 self.host.memory.init()
143 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
144 assert(self._paramExists())
145 self.host.memory.init()
146 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME)
147 assert(self._paramExists())
148 self.host.memory.init()
149 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
150 assert(self._paramExists())
151 # tests with security level 1 on the parameter (more secure)
152 params = self._getParamXML(security_level=1)
153 self.host.memory.init()
154 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
155 assert(self._paramExists())
156 self.host.memory.init()
157 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME)
158 assert(not self._paramExists())
159 self.host.memory.init()
160 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
161 assert(self._paramExists())
162
163 def test_paramsRegisterApp_getParams(self):
164 # test retrieving the parameter for a specific frontend
165 self.host.memory.init()
166 params = self._getParamXML(security_level=1)
167 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
168 self.host.memory.getParams(1, '').addCallback(self._assert)
169 self.host.memory.getParams(1, Const.APP_NAME).addCallback(self._assert)
170 self.host.memory.getParams(1, 'another_dummy_frontend').addCallback(self._assert_not)