# HG changeset patch # User souliane # Date 1389374430 -3600 # Node ID 6625558371dbe36ce2cfda2b15e9f1a6f41cae44 # Parent 52c4b755aba65e8ee401537a90b3a8f0243c323d test: added tests for the plugin "room game" + rename other test files diff -r 52c4b755aba6 -r 6625558371db src/test/test_memory.py --- a/src/test/test_memory.py Fri Jan 10 18:15:02 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,188 +0,0 @@ -#!/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 . - -from sat.core.i18n import _ -from sat.test import helpers -from twisted.trial import unittest -import traceback -import logging -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 """ - - - - - - - - """ % { - '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 assertParam_generic(self, src=None, exists=True, deferred=False): - msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n" - if deferred == False: - # in this stack we can see the line where the error came from, - # if limit=5, 6 is not enough you can increase the value - msg += "\n".join(traceback.format_stack(limit=5 if exists else 6)) - assertion = self._paramExists(src=src) - getattr(self, "assert%s" % exists)(assertion, msg) - - def assertParamExists(self, src=None): - self.assertParam_generic(src, True) - - def assertParamNotExists(self, src=None): - self.assertParam_generic(src, False) - - def assertParamExists_async(self, src): - """@param src: a deferred result from Memory.getParams""" - self.assertParam_generic(minidom.parseString(src.encode("utf-8")), True, True) - - def assertParamNotExists_async(self, src): - """@param src: a deferred result from Memory.getParams""" - self.assertParam_generic(minidom.parseString(src.encode("utf-8")), False, True) - - def _getParams(self, security_limit, app='', profile_key='@NONE@'): - if profile_key == '@NONE@': - profile_key = '@DEFAULT@' - return self.host.memory.getParams(security_limit, app, profile_key) - - def test_updateParams(self): - self.host.memory.init() - # check if the update works - self.host.memory.updateParams(self._getParamXML()) - self.assertParamExists() - 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._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) - self._getParams(0).addCallback(self.assertParamNotExists_async) - self._getParams(1).addCallback(self.assertParamNotExists_async) - # 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._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) - self._getParams(0).addCallback(self.assertParamExists_async) - self._getParams(1).addCallback(self.assertParamExists_async) - # 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._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) - self._getParams(0).addCallback(self.assertParamNotExists_async) - self._getParams(1).addCallback(self.assertParamExists_async) - - def test_paramsRegisterApp(self): - - def register(*args): - logger = logging.getLogger() - level = logger.getEffectiveLevel() - logger.setLevel(logging.INFO) - self.host.memory.paramsRegisterApp(*args) - logger.setLevel(level) - - # tests with no security level on the parameter (most secure) - params = self._getParamXML() - self.host.memory.init() - register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) - self.assertParamExists() - self.host.memory.init() - register(params, 0, Const.APP_NAME) - self.assertParamNotExists() - self.host.memory.init() - register(params, 1, Const.APP_NAME) - self.assertParamNotExists() - # tests with security level 0 on the parameter (not secure) - params = self._getParamXML(security_level=0) - self.host.memory.init() - register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) - self.assertParamExists() - self.host.memory.init() - register(params, 0, Const.APP_NAME) - self.assertParamExists() - self.host.memory.init() - register(params, 1, Const.APP_NAME) - self.assertParamExists() - # tests with security level 1 on the parameter (more secure) - params = self._getParamXML(security_level=1) - self.host.memory.init() - register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) - self.assertParamExists() - self.host.memory.init() - register(params, 0, Const.APP_NAME) - self.assertParamNotExists() - self.host.memory.init() - register(params, 1, Const.APP_NAME) - self.assertParamExists() - - 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._getParams(1, '').addCallback(self.assertParamExists_async) - self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async) - self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async) diff -r 52c4b755aba6 -r 6625558371db src/test/test_memory_memory.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/test_memory_memory.py Fri Jan 10 18:20:30 2014 +0100 @@ -0,0 +1,188 @@ +#!/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 . + +from sat.core.i18n import _ +from sat.test import helpers +from twisted.trial import unittest +import traceback +import logging +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 """ + + + + + + + + """ % { + '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 assertParam_generic(self, src=None, exists=True, deferred=False): + msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n" + if deferred == False: + # in this stack we can see the line where the error came from, + # if limit=5, 6 is not enough you can increase the value + msg += "\n".join(traceback.format_stack(limit=5 if exists else 6)) + assertion = self._paramExists(src=src) + getattr(self, "assert%s" % exists)(assertion, msg) + + def assertParamExists(self, src=None): + self.assertParam_generic(src, True) + + def assertParamNotExists(self, src=None): + self.assertParam_generic(src, False) + + def assertParamExists_async(self, src): + """@param src: a deferred result from Memory.getParams""" + self.assertParam_generic(minidom.parseString(src.encode("utf-8")), True, True) + + def assertParamNotExists_async(self, src): + """@param src: a deferred result from Memory.getParams""" + self.assertParam_generic(minidom.parseString(src.encode("utf-8")), False, True) + + def _getParams(self, security_limit, app='', profile_key='@NONE@'): + if profile_key == '@NONE@': + profile_key = '@DEFAULT@' + return self.host.memory.getParams(security_limit, app, profile_key) + + def test_updateParams(self): + self.host.memory.init() + # check if the update works + self.host.memory.updateParams(self._getParamXML()) + self.assertParamExists() + 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._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) + self._getParams(0).addCallback(self.assertParamNotExists_async) + self._getParams(1).addCallback(self.assertParamNotExists_async) + # 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._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) + self._getParams(0).addCallback(self.assertParamExists_async) + self._getParams(1).addCallback(self.assertParamExists_async) + # 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._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) + self._getParams(0).addCallback(self.assertParamNotExists_async) + self._getParams(1).addCallback(self.assertParamExists_async) + + def test_paramsRegisterApp(self): + + def register(*args): + logger = logging.getLogger() + level = logger.getEffectiveLevel() + logger.setLevel(logging.INFO) + self.host.memory.paramsRegisterApp(*args) + logger.setLevel(level) + + # tests with no security level on the parameter (most secure) + params = self._getParamXML() + self.host.memory.init() + register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) + self.assertParamExists() + self.host.memory.init() + register(params, 0, Const.APP_NAME) + self.assertParamNotExists() + self.host.memory.init() + register(params, 1, Const.APP_NAME) + self.assertParamNotExists() + # tests with security level 0 on the parameter (not secure) + params = self._getParamXML(security_level=0) + self.host.memory.init() + register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) + self.assertParamExists() + self.host.memory.init() + register(params, 0, Const.APP_NAME) + self.assertParamExists() + self.host.memory.init() + register(params, 1, Const.APP_NAME) + self.assertParamExists() + # tests with security level 1 on the parameter (more secure) + params = self._getParamXML(security_level=1) + self.host.memory.init() + register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) + self.assertParamExists() + self.host.memory.init() + register(params, 0, Const.APP_NAME) + self.assertParamNotExists() + self.host.memory.init() + register(params, 1, Const.APP_NAME) + self.assertParamExists() + + 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._getParams(1, '').addCallback(self.assertParamExists_async) + self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async) + self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async) diff -r 52c4b755aba6 -r 6625558371db src/test/test_plugin_misc_room_game.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/test_plugin_misc_room_game.py Fri Jan 10 18:20:30 2014 +0100 @@ -0,0 +1,484 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# SAT: a jabber client +# Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) +# Copyright (C) 2013 Adrien Cossa (souliane@mailoo.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 . + +""" Tests for the plugin room game (base class for MUC games) """ + +from sat.core.i18n import _ +from constants import Const +from sat.test import helpers, helpers_plugins +from sat.plugins import plugin_misc_room_game as plugin +from twisted.words.protocols.jabber.jid import JID +from wokkel.muc import User +import traceback +import logging + +# Data used for test initialization +NAMESERVICE = 'http://www.goffi.org/protocol/dummy' +TAG = 'dummy' +PLUGIN_INFO = { + "name": "Dummy plugin", + "import_name": "DUMMY", + "type": "MISC", + "protocols": [], + "dependencies": [], + "main": "Dummy", + "handler": "no", # handler MUST be "no" (dynamic inheritance) + "description": _("""Dummy plugin to test room game""") +} + +ROOM_JID_S = Const.MUC_STR[0] +PROFILE = Const.PROFILE[0] +OTHER_PROFILE = Const.PROFILE[1] + + +class RoomGameTest(helpers.SatTestCase): + + def setUp(self): + self.host = helpers.FakeSAT() + + def init(self, game_init={}, player_init={}): + self.host.init() + self.plugin = plugin.RoomGame(self.host) + self.plugin._init_(self.host, PLUGIN_INFO, (NAMESERVICE, TAG), game_init, player_init) + self.plugin_0045 = self.host.plugins['XEP-0045'] = helpers_plugins.FakeXEP_0045(self.host) + self.plugin_0249 = self.host.plugins['XEP-0249'] = helpers_plugins.FakeXEP_0249(self.host) + logger = logging.getLogger() + level = logger.getEffectiveLevel() + logger.setLevel(logging.WARNING) # remove info pollution + for profile in Const.PROFILE: + self.host.getClient(profile) # init self.host.profiles[profile] + logger.setLevel(level) + + def initGame(self, muc_index, user_index): + self.plugin_0045.joinRoom(user_index, muc_index) + self.plugin._initGame(Const.MUC_STR[muc_index], Const.JID[user_index].user) + + def expectedMessage(self, to, type_, tag, players=[]): + content = "<%s" % tag + if not players: + content += "/>" + else: + content += ">" + for i in xrange(0, len(players)): + content += "%s" % (i, players[i]) + content += "" % tag + return "<%s xmlns='%s'>%s" % (to, type_, TAG, NAMESERVICE, content) + + def test_createOrInvite_solo(self): + self.init() + self.plugin_0045.joinRoom(0, 0) + self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), [], Const.PROFILE[0]) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) + + def test_createOrInvite_multi_not_waiting(self): + self.init() + self.plugin_0045.joinRoom(0, 0) + other_players = [Const.JID_STR[1], Const.JID_STR[2]] + self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), other_players, Const.PROFILE[0]) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) + + def test_createOrInvite_multi_waiting(self): + self.init(player_init={'score': 0}) + self.plugin_0045.joinRoom(0, 0) + other_players = [Const.JID_STR[1], Const.JID_STR[2]] + self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), other_players, Const.PROFILE[0]) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, False)) + self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) + + def test_initGame(self): + self.init() + self.initGame(0, 0) + self.assertTrue(self.plugin.isReferee(ROOM_JID_S, Const.JID[0].user)) + self.assertEqual([], self.plugin.games[ROOM_JID_S]['players']) + + def test_checkJoinAuth(self): + self.init() + check = lambda value: getattr(self, "assert%s" % value)(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[0], Const.JID[0].user)) + check(False) + # to test the "invited" mode, the referee must be different than the user to test + self.initGame(0, 1) + self.plugin.join_mode = self.plugin.ALL + check(True) + self.plugin.join_mode = self.plugin.INVITED + check(False) + self.plugin.invitations[ROOM_JID_S] = [(None, [Const.JID[0].userhost()])] + check(True) + self.plugin.join_mode = self.plugin.NONE + check(False) + self.plugin.games[ROOM_JID_S]['players'].append(Const.JID[0].user) + check(True) + + def test_updatePlayers(self): + self.init() + self.initGame(0, 0) + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], []) + self.plugin._updatePlayers(ROOM_JID_S, [], Const.PROFILE[0]) + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], []) + self.plugin._updatePlayers(ROOM_JID_S, ["user1"], Const.PROFILE[0]) + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], ["user1"]) + self.plugin._updatePlayers(ROOM_JID_S, ["user2", "user3"], Const.PROFILE[0]) + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], ["user1", "user2", "user3"]) + self.plugin._updatePlayers(ROOM_JID_S, ["user2", "user3"], Const.PROFILE[0]) # should not be stored twice + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], ["user1", "user2", "user3"]) + + def test_signalPlayers(self): + self.init() + self.initGame(0, 0) + self.plugin._signalPlayers(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) + self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", [])) + self.plugin.games[ROOM_JID_S]['players'].append("test1") + self.plugin._signalPlayers(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) + self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", ["test1"])) + self.plugin.games[ROOM_JID_S]['started'] = True + self.plugin.games[ROOM_JID_S]['players'].append("test2") + self.plugin._signalPlayers(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) + self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", ["test1", "test2"])) + self.plugin.games[ROOM_JID_S]['players'].append("test3") + self.plugin.games[ROOM_JID_S]['players'].append("test4") + user1 = JID(ROOM_JID_S + "/" + Const.JID[0].user) + user2 = JID(ROOM_JID_S + "/" + Const.JID[1].user) + self.plugin._signalPlayers(ROOM_JID_S, [user1, user2], Const.PROFILE[0]) + self.assertEqualXML(self.host.getSentMessage(3, 0), self.expectedMessage(user1.full(), "normal", "started", ["test1", "test2", "test3", "test4"])) + self.assertEqualXML(self.host.getSentMessage(4, 0), self.expectedMessage(user2.full(), "normal", "started", ["test1", "test2", "test3", "test4"])) + + def test_invitePlayers(self): + self.init() + self.initGame(0, 0) + self.plugin_0045.joinRoom(0, 1) + self.assertEqual(self.plugin.invitations[ROOM_JID_S], []) + room = self.plugin_0045.getRoom(0, 0) + nicks = self.plugin._invitePlayers(room, [Const.JID_STR[1], Const.JID_STR[2]], Const.JID[0].user, Const.PROFILE[0]) + self.assertEqual(self.plugin.invitations[ROOM_JID_S][0][1], [Const.JID[1].userhost(), Const.JID[2].userhost()]) + # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost + self.assertEqual(nicks, [Const.JID[1].user, Const.JID[2].user]) + + nicks = self.plugin._invitePlayers(room, [Const.JID_STR[1], Const.JID_STR[3]], Const.JID[0].user, Const.PROFILE[0]) + self.assertEqual(self.plugin.invitations[ROOM_JID_S][1][1], [Const.JID[1].userhost(), Const.JID[3].userhost()]) + # this time Const.JID[1] and Const.JID[3] have the same user but the host differs + self.assertEqual(nicks, [Const.JID[1].user]) + + def test_checkInviteAuth(self): + + def check(value, index): + nick = self.plugin_0045.getNick(0, index) + getattr(self, "assert%s" % value)(self.plugin._checkInviteAuth(ROOM_JID_S, nick)) + + self.init() + + for mode in [self.plugin.FROM_ALL, self.plugin.FROM_NONE, self.plugin.FROM_REFEREE, self.plugin.FROM_PLAYERS]: + self.plugin.invite_mode = mode + check(True, 0) + + self.initGame(0, 0) + self.plugin.invite_mode = self.plugin.FROM_ALL + check(True, 0) + check(True, 1) + self.plugin.invite_mode = self.plugin.FROM_NONE + check(True, 0) # game initialized but not started yet, referee can invite + check(False, 1) + self.plugin.invite_mode = self.plugin.FROM_REFEREE + check(True, 0) + check(False, 1) + user_nick = self.plugin_0045.joinRoom(0, 1) + self.plugin.games[ROOM_JID_S]['players'].append(user_nick) + self.plugin.invite_mode = self.plugin.FROM_PLAYERS + check(True, 0) + check(True, 1) + check(False, 2) + + def test_isReferee(self): + self.init() + self.initGame(0, 0) + self.assertTrue(self.plugin.isReferee(ROOM_JID_S, self.plugin_0045.getNick(0, 0))) + self.assertFalse(self.plugin.isReferee(ROOM_JID_S, self.plugin_0045.getNick(0, 1))) + + def test_isPlayer(self): + self.init() + self.initGame(0, 0) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNick(0, 0))) + user_nick = self.plugin_0045.joinRoom(0, 1) + self.plugin.games[ROOM_JID_S]['players'].append(user_nick) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, user_nick)) + self.assertFalse(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNick(0, 2))) + + def test_checkWaitAuth(self): + + def check(value, other_players, confirmed, rest): + room = self.plugin_0045.getRoom(0, 0) + self.assertEqual((value, confirmed, rest), self.plugin._checkWaitAuth(room, other_players)) + + self.init() + self.initGame(0, 0) + other_players = [Const.JID[1], Const.JID[3]] + self.plugin.wait_mode = self.plugin.FOR_NONE + check(True, [], [], []) + check(True, [Const.JID[0]], [], [Const.JID[0]]) # getRoomNickOfUser checks for the other users only + check(True, other_players, [], other_players) + self.plugin.wait_mode = self.plugin.FOR_ALL + check(True, [], [], []) + check(False, [Const.JID[0]], [], [Const.JID[0]]) + check(False, other_players, [], other_players) + self.plugin_0045.joinRoom(0, 1) + check(False, other_players, [], other_players) + self.plugin_0045.joinRoom(0, 4) + check(False, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0)], [Const.JID[3]]) + self.plugin_0045.joinRoom(0, 3) + check(True, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0), + self.plugin_0045.getNickOfUser(0, 3, 0)], []) + + other_players = [Const.JID[1], Const.JID[3], Const.JID[2]] + # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost + check(True, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0), + self.plugin_0045.getNickOfUser(0, 3, 0), + self.plugin_0045.getNickOfUser(0, 2, 0)], []) + + def test_prepareRoom_trivial(self): + self.init() + other_players = [] + self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) + self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[0], Const.JID[0].user)) + self.assertTrue(self.plugin._checkInviteAuth(ROOM_JID_S, Const.JID[0].user)) + self.assertEqual((True, [], []), self.plugin._checkWaitAuth(ROOM_JID_S, [])) + self.assertTrue(self.plugin.isReferee(ROOM_JID_S, Const.JID[0].user)) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, Const.JID[0].user)) + self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) + + def test_prepareRoom_invite(self): + self.init() + other_players = [Const.JID_STR[1], Const.JID_STR[2]] + self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) + room = self.plugin_0045.getRoom(0, 0) + + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) + self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[1], Const.JID[1].user)) + self.assertFalse(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[3], Const.JID[3].user)) + self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID_S, Const.JID[1].user)) + self.assertEqual((True, [], other_players), self.plugin._checkWaitAuth(room, other_players)) + + player2_nick = self.plugin_0045.joinRoom(0, 1) + self.plugin.userJoinedTrigger(room, room.roster[player2_nick], PROFILE) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, player2_nick)) + self.assertTrue(self.plugin._checkInviteAuth(ROOM_JID_S, player2_nick)) + self.assertFalse(self.plugin.isReferee(ROOM_JID_S, player2_nick)) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, player2_nick)) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNickOfUser(0, 2, 0))) + self.assertFalse(self.plugin.isPlayer(ROOM_JID_S, Const.JID_STR[3])) + self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, Const.PROFILE[1])) + + def test_prepareRoom_score1(self): + self.init(player_init={'score': 0}) + other_players = [Const.JID_STR[1], Const.JID_STR[2]] + self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) + room = self.plugin_0045.getRoom(0, 0) + + self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) + self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[1], Const.JID[1].user)) + self.assertFalse(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[3], Const.JID[3].user)) + self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID_S, Const.JID[1].user)) + self.assertEqual((False, [], other_players), self.plugin._checkWaitAuth(room, other_players)) + + user_nick = self.plugin_0045.joinRoom(0, 1) + self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, user_nick)) + self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID_S, user_nick)) + self.assertFalse(self.plugin.isReferee(ROOM_JID_S, user_nick)) + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, user_nick)) + # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNickOfUser(0, 2, 0))) + # the following assertion is True because Const.JID[1] nick in the room is equal to Const.JID[3].user + self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, Const.JID[3].user)) + # but Const.JID[3] is actually not in the room + self.assertEqual(self.plugin_0045.getNickOfUser(0, 3, 0), None) + self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, Const.PROFILE[0])) + + def test_prepareRoom_score2(self): + self.init(player_init={'score': 0}) + other_players = [Const.JID_STR[1], Const.JID_STR[4]] + self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) + room = self.plugin_0045.getRoom(0, 0) + + user_nick = self.plugin_0045.joinRoom(0, 1) + self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) + self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) + user_nick = self.plugin_0045.joinRoom(0, 4) + self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) + self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) + + def test_userJoinedTrigger(self): + self.init(player_init={"xxx": "xyz"}) + other_players = [Const.JID_STR[1], Const.JID_STR[3]] + self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) + nicks = [self.plugin_0045.getNick(0, 0)] + + self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages + self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks)) + self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 1) + + # wrong profile + user_nick = self.plugin_0045.joinRoom(0, 1) + room = self.plugin_0045.getRoom(0, 1) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), OTHER_PROFILE) + self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # no new message has been sent + self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started + + # referee profile, user is allowed, wait for one more + room = self.plugin_0045.getRoom(0, 0) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE) + nicks.append(user_nick) + self.assertEqual(self.host.countSentMessages(), [2, 0, 0, 0, 0]) + self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks)) + self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started + + # referee profile, user is not allowed + user_nick = self.plugin_0045.joinRoom(0, 4) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE) + self.assertEqual(self.host.countSentMessages(), [3, 0, 0, 0, 0]) + self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S + '/' + user_nick, "normal", "players", nicks)) + self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started + + # referee profile, user is allowed, everybody here + user_nick = self.plugin_0045.joinRoom(0, 3) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) + nicks.append(user_nick) + self.assertEqual(self.host.getSentMessage(3, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) # game started + self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0) + + # wait for none + self.init() + self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) + self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages + room = self.plugin_0045.getRoom(0, 0) + nicks = [self.plugin_0045.getNick(0, 0)] + user_nick = self.plugin_0045.joinRoom(0, 3) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) + nicks.append(user_nick) + self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) + + def test_userLeftTrigger(self): + self.init(player_init={"xxx": "xyz"}) + other_players = [Const.JID_STR[1], Const.JID_STR[3], Const.JID_STR[4]] + self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) + room = self.plugin_0045.getRoom(0, 0) + nicks = [self.plugin_0045.getNick(0, 0)] + self.assertTrue(self.plugin.invitations[ROOM_JID_S][0], [Const.JID[1].userhost(), Const.JID[3].userhost(), Const.JID[4].userhost()]) + + # one user joins + user_nick = self.plugin_0045.joinRoom(0, 1) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE) + nicks.append(user_nick) + + # the user leaves + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) + room = self.plugin_0045.getRoom(0, 1) + self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[1]), Const.PROFILE[1]) # wrong profile + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) + room = self.plugin_0045.getRoom(0, 0) + self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[1]), PROFILE) # referee profile + nicks.pop() + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) + + # all the users join + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE) + nicks.append(user_nick) + user_nick = self.plugin_0045.joinRoom(0, 3) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) + nicks.append(user_nick) + user_nick = self.plugin_0045.joinRoom(0, 4) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE) + nicks.append(user_nick) + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) + self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0) + + # one user leaves + self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[4]), PROFILE) + nicks.pop() + self.assertTrue(self.plugin.invitations[ROOM_JID_S][0], [Const.JID[4].userhost()]) + + # another leaves + self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[3]), PROFILE) + nicks.pop() + self.assertTrue(self.plugin.invitations[ROOM_JID_S][0], [Const.JID[4].userhost(), Const.JID[3].userhost()]) + + # they can join again + user_nick = self.plugin_0045.getNickOfUser(0, 3, 0) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) + nicks.append(user_nick) + user_nick = self.plugin_0045.getNickOfUser(0, 4, 0) + self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE) + nicks.append(user_nick) + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) + self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0) + + def test__checkCreateGameAndInit(self): + self.init() + self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) # print internal error + + nick = self.plugin_0045.joinRoom(0, 0) + self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, False)) + self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) + self.assertTrue(self.plugin.isReferee(ROOM_JID_S, nick)) + + self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, OTHER_PROFILE)) # print internal error + + self.plugin_0045.joinRoom(0, 1) + self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, OTHER_PROFILE)) + + self.plugin.createGame(ROOM_JID_S, [Const.JID_STR[1]], PROFILE) + self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) + self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, OTHER_PROFILE)) + + def test_createGame(self): + + self.init(player_init={"xxx": "xyz"}) + nicks = [] + for i in [0, 1, 3, 4]: + nicks.append(self.plugin_0045.joinRoom(0, i)) + + # game not exists + self.plugin.createGame(ROOM_JID_S, nicks, PROFILE) + self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) + self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) + self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) + for nick in nicks: + self.assertEqual('init', self.plugin.games[ROOM_JID_S]['status'][nick]) + self.assertEqual(self.plugin.player_init, self.plugin.games[ROOM_JID_S]['players_data'][nick]) + self.plugin.games[ROOM_JID_S]['players_data'][nick]["xxx"] = nick + for nick in nicks: + # checks that a copy of self.player_init has been done and not a reference + self.assertEqual(nick, self.plugin.games[ROOM_JID_S]['players_data'][nick]['xxx']) + + # game exists, current profile is referee + self.init(player_init={"xxx": "xyz"}) + self.initGame(0, 0) + self.plugin.games[ROOM_JID_S]['started'] = True + self.plugin.createGame(ROOM_JID_S, nicks, PROFILE) + self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) + + # game exists, current profile is not referee + self.init(player_init={"xxx": "xyz"}) + self.initGame(0, 0) + self.plugin.games[ROOM_JID_S]['started'] = True + self.plugin_0045.joinRoom(0, 1) + self.plugin.createGame(ROOM_JID_S, nicks, OTHER_PROFILE) + self.assertEqual(self.host.countSentMessages(), [0, 0, 0, 0, 0]) # no sync message has been sent by other_profile diff -r 52c4b755aba6 -r 6625558371db src/test/test_plugin_misc_text_syntaxes.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/test_plugin_misc_text_syntaxes.py Fri Jan 10 18:20:30 2014 +0100 @@ -0,0 +1,86 @@ +#!/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 . + +""" Plugin text syntaxes tests """ + +from sat.test import helpers +from sat.plugins import plugin_misc_text_syntaxes + + +class SanitisationTest(helpers.SatTestCase): + + def setUp(self): + self.host = helpers.FakeSAT() + self.text_syntaxes = plugin_misc_text_syntaxes.TextSyntaxes(self.host) + + def test_xhtml_sanitise(self): + evil_html = """ + + + + + + + + + a link + another link +

a paragraph

+
secret EVIL!
+ of EVIL! + +
+ Password: +
+ annoying EVIL! + spam spam SPAM! + + + """ # example from lxml: /usr/share/doc/python-lxml-doc/html/lxmlhtml.html#cleaning-up-html + + expected = """
+ + + a link + another link +

a paragraph

+
secret EVIL!
+ of EVIL! + Password: + annoying EVIL! + spam spam SPAM! + + +
""" + + d = self.text_syntaxes.clean_xhtml(evil_html) + d.addCallback(self.assertEqualXML, expected, ignore_blank=True) + return d + + def test_styles_sanitise(self): + evil_html = """

test retest
toto

""" + + expected = """

test retest
toto

""" + + d = self.text_syntaxes.clean_xhtml(evil_html) + d.addCallback(self.assertEqualXML, expected) + return d diff -r 52c4b755aba6 -r 6625558371db src/test/test_plugin_text_syntaxes.py --- a/src/test/test_plugin_text_syntaxes.py Fri Jan 10 18:15:02 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -#!/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 . - -""" Plugin text syntaxes tests """ - -from sat.test import helpers -from sat.plugins import plugin_misc_text_syntaxes - - -class SanitisationTest(helpers.SatTestCase): - - def setUp(self): - self.host = helpers.FakeSAT() - self.text_syntaxes = plugin_misc_text_syntaxes.TextSyntaxes(self.host) - - def test_xhtml_sanitise(self): - evil_html = """ - - - - - - - - - a link - another link -

a paragraph

-
secret EVIL!
- of EVIL! - -
- Password: -
- annoying EVIL! - spam spam SPAM! - - - """ # example from lxml: /usr/share/doc/python-lxml-doc/html/lxmlhtml.html#cleaning-up-html - - expected = """
- - - a link - another link -

a paragraph

-
secret EVIL!
- of EVIL! - Password: - annoying EVIL! - spam spam SPAM! - - -
""" - - d = self.text_syntaxes.clean_xhtml(evil_html) - d.addCallback(self.assertEqualXML, expected, ignore_blank=True) - return d - - def test_styles_sanitise(self): - evil_html = """

test retest
toto

""" - - expected = """

test retest
toto

""" - - d = self.text_syntaxes.clean_xhtml(evil_html) - d.addCallback(self.assertEqualXML, expected) - return d