Mercurial > libervia-backend
changeset 782:0e5807193721
test: added some tests for Memory
author | souliane <souliane@mailoo.org> |
---|---|
date | Sat, 04 Jan 2014 17:16:40 +0100 |
parents | 80ab2b58e205 |
children | 27581cddb758 |
files | src/test/constants.py src/test/helpers.py src/test/test_memory.py |
diffstat | 3 files changed, 203 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/src/test/constants.py Sat Jan 04 15:46:30 2014 +0100 +++ b/src/test/constants.py Sat Jan 04 17:16:40 2014 +0100 @@ -31,8 +31,10 @@ TEST_PROFILE = 'test_profile' + NO_SECURITY_LIMIT = -1 + SECURITY_LIMIT = 0 + # To test frontend parameters APP_NAME = "dummy_frontend" - SECURITY_LIMIT = 0 ENABLE_UNIBOX_KEY = D_("Composition") ENABLE_UNIBOX_PARAM = D_("Enable unibox")
--- a/src/test/helpers.py Sat Jan 04 15:46:30 2014 +0100 +++ b/src/test/helpers.py Sat Jan 04 17:16:40 2014 +0100 @@ -24,7 +24,9 @@ from sat.memory.memory import Params from twisted.trial.unittest import FailTest from twisted.trial import unittest +from twisted.internet import defer from xml.etree import cElementTree as etree +from sat.core import exceptions import re @@ -96,14 +98,28 @@ def __init__(self, host, storage): Params.__init__(self, host, storage) - self.values = {} # naive simulation of values storage + self.params = {} # naive simulation of values storage def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'): - self.values.setdefault(category, {}) - self.values[category][name] = value + profile = self.getProfileName(profile_key) + self.params.setdefault(profile, {}) + self.params[profile_key][(category, name)] = value def getParamA(self, name, category, attr="value", profile_key='@NONE@'): - return self.values[category][name] + profile = self.getProfileName(profile_key) + return self.params[profile][(category, name)] + + def getProfileName(self, profile_key, return_profile_keys=False): + if profile_key == '@DEFAULT@': + return Const.TEST_PROFILE + elif profile_key == '@NONE@': + raise exceptions.ProfileNotSetError + else: + return profile_key + + def loadIndParams(self, profile, cache=None): + self.params[profile] = {} + return defer.succeed(None) class FakeMemory(object): @@ -118,11 +134,11 @@ """Tests that manipulate params and/or entities should re-initialise the memory first to not fake the result.""" self.params.load_default_params() - self.params.values.clear() + self.params.params.clear() self.entities_data = {} # naive simulation of entities - def getProfileName(self, profile_key): - return profile_key + def getProfileName(self, profile_key, return_profile_keys=False): + return self.params.getProfileName(profile_key, return_profile_keys) def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"): pass @@ -139,9 +155,16 @@ def delWaitingSub(self, contact_jid, profile_key): pass + def getParams(self, security_limit=Const.NO_SECURITY_LIMIT, app='', profile_key='@NONE@'): + """profile_key is set to @DEFAULT@ to avoid specifying it always""" + return self.params.getParams(security_limit, app, profile_key='@DEFAULT@') + def updateParams(self, xml, security_limit=Const.SECURITY_LIMIT, app=''): self.params.updateParams(xml, security_limit, app) + def paramsRegisterApp(self, xml, security_limit=Const.NO_SECURITY_LIMIT, app=''): + return self.params.paramsRegisterApp(xml, security_limit, app) + def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'): self.params.setParam(name, value, category, security_limit, profile_key)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/test_memory.py Sat Jan 04 17:16:40 2014 +0100 @@ -0,0 +1,170 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# SAT: a jabber client +# Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from sat.core.i18n import _ +from sat.test import helpers +from twisted.trial import unittest +import traceback +from constants import Const +from xml.dom import minidom + + +class MemoryTest(unittest.TestCase): + + def setUp(self): + self.host = helpers.FakeSAT() + + def _getParamXML(self, name=None, category=None, security_level=None): + if not name: + name = Const.ENABLE_UNIBOX_PARAM + if not category: + category = Const.ENABLE_UNIBOX_KEY + return """ + <params> + <individual> + <category name="%(category_name)s" label="%(category_label)s"> + <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/> + </category> + </individual> + </params> + """ % { + 'category_name': category, + 'category_label': _(category), + 'param_name': name, + 'param_label': _(name), + 'security': '' if security_level is None else ('security="%d"' % security_level) + } + + def _paramExists(self, name=None, category=None, src=None): + """@return: True is the param (category, name) exists""" + if not name: + name = Const.ENABLE_UNIBOX_PARAM + if not category: + category = Const.ENABLE_UNIBOX_KEY + if src is None: + src = self.host.memory.params.dom.documentElement + for type_node in src.childNodes: + # when src comes self.host.memory.params.dom, we have here + # some "individual" or "general" elements, when it comes + # from Memory.getParams we have here a "params" elements + if type_node.nodeName not in ("individual", "general", "params"): + continue + for cat_node in type_node.childNodes: + if cat_node.nodeName != "category" or cat_node.getAttribute("name") != category: + continue + for param in cat_node.childNodes: + if param.nodeName == "param" and param.getAttribute("name") == name: + return True + return False + + def _assert(self, src, true=True): + """@param src: a deferred result from Memory.getParams""" + assertion = self._paramExists(src=minidom.parseString(src.encode("utf-8"))) + if not true: + assertion = not assertion + try: + assert(assertion) + except AssertionError as e: + # in this stack we can see the line where the error came from, + # if limit=5 is not enough you can increase the value + print "---------------------- stack start ----------------------" + traceback.print_stack(limit=5 if true else 6) + print "----------------------- stack end -----------------------" + raise e + + def _assert_not(self, src): + """@param src: a deferred result from Memory.getParams""" + self._assert(src, False) + + def test_updateParams(self): + self.host.memory.init() + # check if the update works + self.host.memory.updateParams(self._getParamXML()) + assert(self._paramExists()) + previous = self.host.memory.params.dom.cloneNode(True) + # now check if it is really updated and not duplicated + self.host.memory.updateParams(self._getParamXML()) + self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8")) + + def test_getParams(self): + # tests with no security level on the parameter (most secure) + params = self._getParamXML() + self.host.memory.init() + self.host.memory.updateParams(params) + self.host.memory.getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert) + self.host.memory.getParams(0, app='').addCallback(self._assert_not) + self.host.memory.getParams(1, app='').addCallback(self._assert_not) + # tests with security level 0 on the parameter (not secure) + params = self._getParamXML(security_level=0) + self.host.memory.init() + self.host.memory.updateParams(params) + self.host.memory.getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert) + self.host.memory.getParams(0, app='').addCallback(self._assert) + self.host.memory.getParams(1, app='').addCallback(self._assert) + # tests with security level 1 on the parameter (more secure) + params = self._getParamXML(security_level=1) + self.host.memory.init() + self.host.memory.updateParams(params) + self.host.memory.getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert) + self.host.memory.getParams(0).addCallback(self._assert_not) + self.host.memory.getParams(1).addCallback(self._assert) + + def test_paramsRegisterApp(self): + # tests with no security level on the parameter (most secure) + params = self._getParamXML() + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) + assert(self._paramExists()) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME) + assert(not self._paramExists()) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) + assert(not self._paramExists()) + # tests with security level 0 on the parameter (not secure) + params = self._getParamXML(security_level=0) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) + assert(self._paramExists()) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME) + assert(self._paramExists()) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) + assert(self._paramExists()) + # tests with security level 1 on the parameter (more secure) + params = self._getParamXML(security_level=1) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) + assert(self._paramExists()) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME) + assert(not self._paramExists()) + self.host.memory.init() + self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) + assert(self._paramExists()) + + def test_paramsRegisterApp_getParams(self): + # test retrieving the parameter for a specific frontend + self.host.memory.init() + params = self._getParamXML(security_level=1) + self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) + self.host.memory.getParams(1, '').addCallback(self._assert) + self.host.memory.getParams(1, Const.APP_NAME).addCallback(self._assert) + self.host.memory.getParams(1, 'another_dummy_frontend').addCallback(self._assert_not)