Mercurial > libervia-backend
comparison src/test/test_plugin_misc_room_game.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 | |
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 # Copyright (C) 2013 Adrien Cossa (souliane@mailoo.org) | |
7 | |
8 # This program is free software: you can redistribute it and/or modify | |
9 # it under the terms of the GNU Affero General Public License as published by | |
10 # the Free Software Foundation, either version 3 of the License, or | |
11 # (at your option) any later version. | |
12 | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU Affero General Public License for more details. | |
17 | |
18 # You should have received a copy of the GNU Affero General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 """ Tests for the plugin room game (base class for MUC games) """ | |
22 | |
23 from sat.core.i18n import _ | |
24 from constants import Const | |
25 from sat.test import helpers, helpers_plugins | |
26 from sat.plugins import plugin_misc_room_game as plugin | |
27 from twisted.words.protocols.jabber.jid import JID | |
28 from wokkel.muc import User | |
29 import traceback | |
30 import logging | |
31 | |
32 # Data used for test initialization | |
33 NAMESERVICE = 'http://www.goffi.org/protocol/dummy' | |
34 TAG = 'dummy' | |
35 PLUGIN_INFO = { | |
36 "name": "Dummy plugin", | |
37 "import_name": "DUMMY", | |
38 "type": "MISC", | |
39 "protocols": [], | |
40 "dependencies": [], | |
41 "main": "Dummy", | |
42 "handler": "no", # handler MUST be "no" (dynamic inheritance) | |
43 "description": _("""Dummy plugin to test room game""") | |
44 } | |
45 | |
46 ROOM_JID_S = Const.MUC_STR[0] | |
47 PROFILE = Const.PROFILE[0] | |
48 OTHER_PROFILE = Const.PROFILE[1] | |
49 | |
50 | |
51 class RoomGameTest(helpers.SatTestCase): | |
52 | |
53 def setUp(self): | |
54 self.host = helpers.FakeSAT() | |
55 | |
56 def init(self, game_init={}, player_init={}): | |
57 self.host.init() | |
58 self.plugin = plugin.RoomGame(self.host) | |
59 self.plugin._init_(self.host, PLUGIN_INFO, (NAMESERVICE, TAG), game_init, player_init) | |
60 self.plugin_0045 = self.host.plugins['XEP-0045'] = helpers_plugins.FakeXEP_0045(self.host) | |
61 self.plugin_0249 = self.host.plugins['XEP-0249'] = helpers_plugins.FakeXEP_0249(self.host) | |
62 logger = logging.getLogger() | |
63 level = logger.getEffectiveLevel() | |
64 logger.setLevel(logging.WARNING) # remove info pollution | |
65 for profile in Const.PROFILE: | |
66 self.host.getClient(profile) # init self.host.profiles[profile] | |
67 logger.setLevel(level) | |
68 | |
69 def initGame(self, muc_index, user_index): | |
70 self.plugin_0045.joinRoom(user_index, muc_index) | |
71 self.plugin._initGame(Const.MUC_STR[muc_index], Const.JID[user_index].user) | |
72 | |
73 def expectedMessage(self, to, type_, tag, players=[]): | |
74 content = "<%s" % tag | |
75 if not players: | |
76 content += "/>" | |
77 else: | |
78 content += ">" | |
79 for i in xrange(0, len(players)): | |
80 content += "<player index='%s'>%s</player>" % (i, players[i]) | |
81 content += "</%s>" % tag | |
82 return "<message to='%s' type='%s'><%s xmlns='%s'>%s</dummy></message>" % (to, type_, TAG, NAMESERVICE, content) | |
83 | |
84 def test_createOrInvite_solo(self): | |
85 self.init() | |
86 self.plugin_0045.joinRoom(0, 0) | |
87 self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), [], Const.PROFILE[0]) | |
88 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) | |
89 | |
90 def test_createOrInvite_multi_not_waiting(self): | |
91 self.init() | |
92 self.plugin_0045.joinRoom(0, 0) | |
93 other_players = [Const.JID_STR[1], Const.JID_STR[2]] | |
94 self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), other_players, Const.PROFILE[0]) | |
95 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) | |
96 | |
97 def test_createOrInvite_multi_waiting(self): | |
98 self.init(player_init={'score': 0}) | |
99 self.plugin_0045.joinRoom(0, 0) | |
100 other_players = [Const.JID_STR[1], Const.JID_STR[2]] | |
101 self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), other_players, Const.PROFILE[0]) | |
102 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, False)) | |
103 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) | |
104 | |
105 def test_initGame(self): | |
106 self.init() | |
107 self.initGame(0, 0) | |
108 self.assertTrue(self.plugin.isReferee(ROOM_JID_S, Const.JID[0].user)) | |
109 self.assertEqual([], self.plugin.games[ROOM_JID_S]['players']) | |
110 | |
111 def test_checkJoinAuth(self): | |
112 self.init() | |
113 check = lambda value: getattr(self, "assert%s" % value)(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[0], Const.JID[0].user)) | |
114 check(False) | |
115 # to test the "invited" mode, the referee must be different than the user to test | |
116 self.initGame(0, 1) | |
117 self.plugin.join_mode = self.plugin.ALL | |
118 check(True) | |
119 self.plugin.join_mode = self.plugin.INVITED | |
120 check(False) | |
121 self.plugin.invitations[ROOM_JID_S] = [(None, [Const.JID[0].userhost()])] | |
122 check(True) | |
123 self.plugin.join_mode = self.plugin.NONE | |
124 check(False) | |
125 self.plugin.games[ROOM_JID_S]['players'].append(Const.JID[0].user) | |
126 check(True) | |
127 | |
128 def test_updatePlayers(self): | |
129 self.init() | |
130 self.initGame(0, 0) | |
131 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], []) | |
132 self.plugin._updatePlayers(ROOM_JID_S, [], Const.PROFILE[0]) | |
133 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], []) | |
134 self.plugin._updatePlayers(ROOM_JID_S, ["user1"], Const.PROFILE[0]) | |
135 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], ["user1"]) | |
136 self.plugin._updatePlayers(ROOM_JID_S, ["user2", "user3"], Const.PROFILE[0]) | |
137 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], ["user1", "user2", "user3"]) | |
138 self.plugin._updatePlayers(ROOM_JID_S, ["user2", "user3"], Const.PROFILE[0]) # should not be stored twice | |
139 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], ["user1", "user2", "user3"]) | |
140 | |
141 def test_signalPlayers(self): | |
142 self.init() | |
143 self.initGame(0, 0) | |
144 self.plugin._signalPlayers(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) | |
145 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", [])) | |
146 self.plugin.games[ROOM_JID_S]['players'].append("test1") | |
147 self.plugin._signalPlayers(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) | |
148 self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", ["test1"])) | |
149 self.plugin.games[ROOM_JID_S]['started'] = True | |
150 self.plugin.games[ROOM_JID_S]['players'].append("test2") | |
151 self.plugin._signalPlayers(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0]) | |
152 self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", ["test1", "test2"])) | |
153 self.plugin.games[ROOM_JID_S]['players'].append("test3") | |
154 self.plugin.games[ROOM_JID_S]['players'].append("test4") | |
155 user1 = JID(ROOM_JID_S + "/" + Const.JID[0].user) | |
156 user2 = JID(ROOM_JID_S + "/" + Const.JID[1].user) | |
157 self.plugin._signalPlayers(ROOM_JID_S, [user1, user2], Const.PROFILE[0]) | |
158 self.assertEqualXML(self.host.getSentMessage(3, 0), self.expectedMessage(user1.full(), "normal", "started", ["test1", "test2", "test3", "test4"])) | |
159 self.assertEqualXML(self.host.getSentMessage(4, 0), self.expectedMessage(user2.full(), "normal", "started", ["test1", "test2", "test3", "test4"])) | |
160 | |
161 def test_invitePlayers(self): | |
162 self.init() | |
163 self.initGame(0, 0) | |
164 self.plugin_0045.joinRoom(0, 1) | |
165 self.assertEqual(self.plugin.invitations[ROOM_JID_S], []) | |
166 room = self.plugin_0045.getRoom(0, 0) | |
167 nicks = self.plugin._invitePlayers(room, [Const.JID_STR[1], Const.JID_STR[2]], Const.JID[0].user, Const.PROFILE[0]) | |
168 self.assertEqual(self.plugin.invitations[ROOM_JID_S][0][1], [Const.JID[1].userhost(), Const.JID[2].userhost()]) | |
169 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost | |
170 self.assertEqual(nicks, [Const.JID[1].user, Const.JID[2].user]) | |
171 | |
172 nicks = self.plugin._invitePlayers(room, [Const.JID_STR[1], Const.JID_STR[3]], Const.JID[0].user, Const.PROFILE[0]) | |
173 self.assertEqual(self.plugin.invitations[ROOM_JID_S][1][1], [Const.JID[1].userhost(), Const.JID[3].userhost()]) | |
174 # this time Const.JID[1] and Const.JID[3] have the same user but the host differs | |
175 self.assertEqual(nicks, [Const.JID[1].user]) | |
176 | |
177 def test_checkInviteAuth(self): | |
178 | |
179 def check(value, index): | |
180 nick = self.plugin_0045.getNick(0, index) | |
181 getattr(self, "assert%s" % value)(self.plugin._checkInviteAuth(ROOM_JID_S, nick)) | |
182 | |
183 self.init() | |
184 | |
185 for mode in [self.plugin.FROM_ALL, self.plugin.FROM_NONE, self.plugin.FROM_REFEREE, self.plugin.FROM_PLAYERS]: | |
186 self.plugin.invite_mode = mode | |
187 check(True, 0) | |
188 | |
189 self.initGame(0, 0) | |
190 self.plugin.invite_mode = self.plugin.FROM_ALL | |
191 check(True, 0) | |
192 check(True, 1) | |
193 self.plugin.invite_mode = self.plugin.FROM_NONE | |
194 check(True, 0) # game initialized but not started yet, referee can invite | |
195 check(False, 1) | |
196 self.plugin.invite_mode = self.plugin.FROM_REFEREE | |
197 check(True, 0) | |
198 check(False, 1) | |
199 user_nick = self.plugin_0045.joinRoom(0, 1) | |
200 self.plugin.games[ROOM_JID_S]['players'].append(user_nick) | |
201 self.plugin.invite_mode = self.plugin.FROM_PLAYERS | |
202 check(True, 0) | |
203 check(True, 1) | |
204 check(False, 2) | |
205 | |
206 def test_isReferee(self): | |
207 self.init() | |
208 self.initGame(0, 0) | |
209 self.assertTrue(self.plugin.isReferee(ROOM_JID_S, self.plugin_0045.getNick(0, 0))) | |
210 self.assertFalse(self.plugin.isReferee(ROOM_JID_S, self.plugin_0045.getNick(0, 1))) | |
211 | |
212 def test_isPlayer(self): | |
213 self.init() | |
214 self.initGame(0, 0) | |
215 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNick(0, 0))) | |
216 user_nick = self.plugin_0045.joinRoom(0, 1) | |
217 self.plugin.games[ROOM_JID_S]['players'].append(user_nick) | |
218 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, user_nick)) | |
219 self.assertFalse(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNick(0, 2))) | |
220 | |
221 def test_checkWaitAuth(self): | |
222 | |
223 def check(value, other_players, confirmed, rest): | |
224 room = self.plugin_0045.getRoom(0, 0) | |
225 self.assertEqual((value, confirmed, rest), self.plugin._checkWaitAuth(room, other_players)) | |
226 | |
227 self.init() | |
228 self.initGame(0, 0) | |
229 other_players = [Const.JID[1], Const.JID[3]] | |
230 self.plugin.wait_mode = self.plugin.FOR_NONE | |
231 check(True, [], [], []) | |
232 check(True, [Const.JID[0]], [], [Const.JID[0]]) # getRoomNickOfUser checks for the other users only | |
233 check(True, other_players, [], other_players) | |
234 self.plugin.wait_mode = self.plugin.FOR_ALL | |
235 check(True, [], [], []) | |
236 check(False, [Const.JID[0]], [], [Const.JID[0]]) | |
237 check(False, other_players, [], other_players) | |
238 self.plugin_0045.joinRoom(0, 1) | |
239 check(False, other_players, [], other_players) | |
240 self.plugin_0045.joinRoom(0, 4) | |
241 check(False, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0)], [Const.JID[3]]) | |
242 self.plugin_0045.joinRoom(0, 3) | |
243 check(True, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0), | |
244 self.plugin_0045.getNickOfUser(0, 3, 0)], []) | |
245 | |
246 other_players = [Const.JID[1], Const.JID[3], Const.JID[2]] | |
247 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost | |
248 check(True, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0), | |
249 self.plugin_0045.getNickOfUser(0, 3, 0), | |
250 self.plugin_0045.getNickOfUser(0, 2, 0)], []) | |
251 | |
252 def test_prepareRoom_trivial(self): | |
253 self.init() | |
254 other_players = [] | |
255 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) | |
256 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) | |
257 self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[0], Const.JID[0].user)) | |
258 self.assertTrue(self.plugin._checkInviteAuth(ROOM_JID_S, Const.JID[0].user)) | |
259 self.assertEqual((True, [], []), self.plugin._checkWaitAuth(ROOM_JID_S, [])) | |
260 self.assertTrue(self.plugin.isReferee(ROOM_JID_S, Const.JID[0].user)) | |
261 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, Const.JID[0].user)) | |
262 self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) | |
263 | |
264 def test_prepareRoom_invite(self): | |
265 self.init() | |
266 other_players = [Const.JID_STR[1], Const.JID_STR[2]] | |
267 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) | |
268 room = self.plugin_0045.getRoom(0, 0) | |
269 | |
270 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) | |
271 self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[1], Const.JID[1].user)) | |
272 self.assertFalse(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[3], Const.JID[3].user)) | |
273 self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID_S, Const.JID[1].user)) | |
274 self.assertEqual((True, [], other_players), self.plugin._checkWaitAuth(room, other_players)) | |
275 | |
276 player2_nick = self.plugin_0045.joinRoom(0, 1) | |
277 self.plugin.userJoinedTrigger(room, room.roster[player2_nick], PROFILE) | |
278 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, player2_nick)) | |
279 self.assertTrue(self.plugin._checkInviteAuth(ROOM_JID_S, player2_nick)) | |
280 self.assertFalse(self.plugin.isReferee(ROOM_JID_S, player2_nick)) | |
281 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, player2_nick)) | |
282 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNickOfUser(0, 2, 0))) | |
283 self.assertFalse(self.plugin.isPlayer(ROOM_JID_S, Const.JID_STR[3])) | |
284 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, Const.PROFILE[1])) | |
285 | |
286 def test_prepareRoom_score1(self): | |
287 self.init(player_init={'score': 0}) | |
288 other_players = [Const.JID_STR[1], Const.JID_STR[2]] | |
289 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) | |
290 room = self.plugin_0045.getRoom(0, 0) | |
291 | |
292 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) | |
293 self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[1], Const.JID[1].user)) | |
294 self.assertFalse(self.plugin._checkJoinAuth(ROOM_JID_S, Const.JID_STR[3], Const.JID[3].user)) | |
295 self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID_S, Const.JID[1].user)) | |
296 self.assertEqual((False, [], other_players), self.plugin._checkWaitAuth(room, other_players)) | |
297 | |
298 user_nick = self.plugin_0045.joinRoom(0, 1) | |
299 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) | |
300 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, user_nick)) | |
301 self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID_S, user_nick)) | |
302 self.assertFalse(self.plugin.isReferee(ROOM_JID_S, user_nick)) | |
303 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, user_nick)) | |
304 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost | |
305 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, self.plugin_0045.getNickOfUser(0, 2, 0))) | |
306 # the following assertion is True because Const.JID[1] nick in the room is equal to Const.JID[3].user | |
307 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, Const.JID[3].user)) | |
308 # but Const.JID[3] is actually not in the room | |
309 self.assertEqual(self.plugin_0045.getNickOfUser(0, 3, 0), None) | |
310 self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, Const.PROFILE[0])) | |
311 | |
312 def test_prepareRoom_score2(self): | |
313 self.init(player_init={'score': 0}) | |
314 other_players = [Const.JID_STR[1], Const.JID_STR[4]] | |
315 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) | |
316 room = self.plugin_0045.getRoom(0, 0) | |
317 | |
318 user_nick = self.plugin_0045.joinRoom(0, 1) | |
319 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) | |
320 self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) | |
321 user_nick = self.plugin_0045.joinRoom(0, 4) | |
322 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) | |
323 self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) | |
324 | |
325 def test_userJoinedTrigger(self): | |
326 self.init(player_init={"xxx": "xyz"}) | |
327 other_players = [Const.JID_STR[1], Const.JID_STR[3]] | |
328 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) | |
329 nicks = [self.plugin_0045.getNick(0, 0)] | |
330 | |
331 self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages | |
332 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks)) | |
333 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 1) | |
334 | |
335 # wrong profile | |
336 user_nick = self.plugin_0045.joinRoom(0, 1) | |
337 room = self.plugin_0045.getRoom(0, 1) | |
338 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), OTHER_PROFILE) | |
339 self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # no new message has been sent | |
340 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started | |
341 | |
342 # referee profile, user is allowed, wait for one more | |
343 room = self.plugin_0045.getRoom(0, 0) | |
344 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE) | |
345 nicks.append(user_nick) | |
346 self.assertEqual(self.host.countSentMessages(), [2, 0, 0, 0, 0]) | |
347 self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks)) | |
348 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started | |
349 | |
350 # referee profile, user is not allowed | |
351 user_nick = self.plugin_0045.joinRoom(0, 4) | |
352 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE) | |
353 self.assertEqual(self.host.countSentMessages(), [3, 0, 0, 0, 0]) | |
354 self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S + '/' + user_nick, "normal", "players", nicks)) | |
355 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started | |
356 | |
357 # referee profile, user is allowed, everybody here | |
358 user_nick = self.plugin_0045.joinRoom(0, 3) | |
359 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) | |
360 nicks.append(user_nick) | |
361 self.assertEqual(self.host.getSentMessage(3, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) | |
362 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) # game started | |
363 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0) | |
364 | |
365 # wait for none | |
366 self.init() | |
367 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) | |
368 self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages | |
369 room = self.plugin_0045.getRoom(0, 0) | |
370 nicks = [self.plugin_0045.getNick(0, 0)] | |
371 user_nick = self.plugin_0045.joinRoom(0, 3) | |
372 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) | |
373 nicks.append(user_nick) | |
374 self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) | |
375 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) | |
376 | |
377 def test_userLeftTrigger(self): | |
378 self.init(player_init={"xxx": "xyz"}) | |
379 other_players = [Const.JID_STR[1], Const.JID_STR[3], Const.JID_STR[4]] | |
380 self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE) | |
381 room = self.plugin_0045.getRoom(0, 0) | |
382 nicks = [self.plugin_0045.getNick(0, 0)] | |
383 self.assertTrue(self.plugin.invitations[ROOM_JID_S][0], [Const.JID[1].userhost(), Const.JID[3].userhost(), Const.JID[4].userhost()]) | |
384 | |
385 # one user joins | |
386 user_nick = self.plugin_0045.joinRoom(0, 1) | |
387 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE) | |
388 nicks.append(user_nick) | |
389 | |
390 # the user leaves | |
391 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) | |
392 room = self.plugin_0045.getRoom(0, 1) | |
393 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[1]), Const.PROFILE[1]) # wrong profile | |
394 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) | |
395 room = self.plugin_0045.getRoom(0, 0) | |
396 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[1]), PROFILE) # referee profile | |
397 nicks.pop() | |
398 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) | |
399 | |
400 # all the users join | |
401 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE) | |
402 nicks.append(user_nick) | |
403 user_nick = self.plugin_0045.joinRoom(0, 3) | |
404 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) | |
405 nicks.append(user_nick) | |
406 user_nick = self.plugin_0045.joinRoom(0, 4) | |
407 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE) | |
408 nicks.append(user_nick) | |
409 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) | |
410 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0) | |
411 | |
412 # one user leaves | |
413 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[4]), PROFILE) | |
414 nicks.pop() | |
415 self.assertTrue(self.plugin.invitations[ROOM_JID_S][0], [Const.JID[4].userhost()]) | |
416 | |
417 # another leaves | |
418 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[3]), PROFILE) | |
419 nicks.pop() | |
420 self.assertTrue(self.plugin.invitations[ROOM_JID_S][0], [Const.JID[4].userhost(), Const.JID[3].userhost()]) | |
421 | |
422 # they can join again | |
423 user_nick = self.plugin_0045.getNickOfUser(0, 3, 0) | |
424 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE) | |
425 nicks.append(user_nick) | |
426 user_nick = self.plugin_0045.getNickOfUser(0, 4, 0) | |
427 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE) | |
428 nicks.append(user_nick) | |
429 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) | |
430 self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0) | |
431 | |
432 def test__checkCreateGameAndInit(self): | |
433 self.init() | |
434 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) # print internal error | |
435 | |
436 nick = self.plugin_0045.joinRoom(0, 0) | |
437 self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) | |
438 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, False)) | |
439 self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) | |
440 self.assertTrue(self.plugin.isReferee(ROOM_JID_S, nick)) | |
441 | |
442 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, OTHER_PROFILE)) # print internal error | |
443 | |
444 self.plugin_0045.joinRoom(0, 1) | |
445 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, OTHER_PROFILE)) | |
446 | |
447 self.plugin.createGame(ROOM_JID_S, [Const.JID_STR[1]], PROFILE) | |
448 self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID_S, PROFILE)) | |
449 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID_S, OTHER_PROFILE)) | |
450 | |
451 def test_createGame(self): | |
452 | |
453 self.init(player_init={"xxx": "xyz"}) | |
454 nicks = [] | |
455 for i in [0, 1, 3, 4]: | |
456 nicks.append(self.plugin_0045.joinRoom(0, i)) | |
457 | |
458 # game not exists | |
459 self.plugin.createGame(ROOM_JID_S, nicks, PROFILE) | |
460 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) | |
461 self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks) | |
462 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) | |
463 for nick in nicks: | |
464 self.assertEqual('init', self.plugin.games[ROOM_JID_S]['status'][nick]) | |
465 self.assertEqual(self.plugin.player_init, self.plugin.games[ROOM_JID_S]['players_data'][nick]) | |
466 self.plugin.games[ROOM_JID_S]['players_data'][nick]["xxx"] = nick | |
467 for nick in nicks: | |
468 # checks that a copy of self.player_init has been done and not a reference | |
469 self.assertEqual(nick, self.plugin.games[ROOM_JID_S]['players_data'][nick]['xxx']) | |
470 | |
471 # game exists, current profile is referee | |
472 self.init(player_init={"xxx": "xyz"}) | |
473 self.initGame(0, 0) | |
474 self.plugin.games[ROOM_JID_S]['started'] = True | |
475 self.plugin.createGame(ROOM_JID_S, nicks, PROFILE) | |
476 self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks)) | |
477 | |
478 # game exists, current profile is not referee | |
479 self.init(player_init={"xxx": "xyz"}) | |
480 self.initGame(0, 0) | |
481 self.plugin.games[ROOM_JID_S]['started'] = True | |
482 self.plugin_0045.joinRoom(0, 1) | |
483 self.plugin.createGame(ROOM_JID_S, nicks, OTHER_PROFILE) | |
484 self.assertEqual(self.host.countSentMessages(), [0, 0, 0, 0, 0]) # no sync message has been sent by other_profile |