comparison sat/test/test_plugin_misc_room_game.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/test/test_plugin_misc_room_game.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 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
30 from logging import WARNING
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 = JID(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 reinit(self, game_init={}, player_init={}):
57 self.host.reinit()
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 for profile in Const.PROFILE:
63 self.host.getClient(profile) # init self.host.profiles[profile]
64
65 def initGame(self, muc_index, user_index):
66 self.plugin_0045.joinRoom(user_index, muc_index)
67 self.plugin._initGame(JID(Const.MUC_STR[muc_index]), Const.JID[user_index].user)
68
69 def _expectedMessage(self, to, type_, tag, players=[]):
70 content = "<%s" % tag
71 if not players:
72 content += "/>"
73 else:
74 content += ">"
75 for i in xrange(0, len(players)):
76 content += "<player index='%s'>%s</player>" % (i, players[i])
77 content += "</%s>" % tag
78 return "<message to='%s' type='%s'><%s xmlns='%s'>%s</dummy></message>" % (to.full(), type_, TAG, NAMESERVICE, content)
79
80 def test_createOrInvite_solo(self):
81 self.reinit()
82 self.plugin_0045.joinRoom(0, 0)
83 self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), [], Const.PROFILE[0])
84 self.assertTrue(self.plugin._gameExists(ROOM_JID, True))
85
86 def test_createOrInvite_multi_not_waiting(self):
87 self.reinit()
88 self.plugin_0045.joinRoom(0, 0)
89 other_players = [Const.JID[1], Const.JID[2]]
90 self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), other_players, Const.PROFILE[0])
91 self.assertTrue(self.plugin._gameExists(ROOM_JID, True))
92
93 def test_createOrInvite_multi_waiting(self):
94 self.reinit(player_init={'score': 0})
95 self.plugin_0045.joinRoom(0, 0)
96 other_players = [Const.JID[1], Const.JID[2]]
97 self.plugin._createOrInvite(self.plugin_0045.getRoom(0, 0), other_players, Const.PROFILE[0])
98 self.assertTrue(self.plugin._gameExists(ROOM_JID, False))
99 self.assertFalse(self.plugin._gameExists(ROOM_JID, True))
100
101 def test_initGame(self):
102 self.reinit()
103 self.initGame(0, 0)
104 self.assertTrue(self.plugin.isReferee(ROOM_JID, Const.JID[0].user))
105 self.assertEqual([], self.plugin.games[ROOM_JID]['players'])
106
107 def test_checkJoinAuth(self):
108 self.reinit()
109 check = lambda value: getattr(self, "assert%s" % value)(self.plugin._checkJoinAuth(ROOM_JID, Const.JID[0], Const.JID[0].user))
110 check(False)
111 # to test the "invited" mode, the referee must be different than the user to test
112 self.initGame(0, 1)
113 self.plugin.join_mode = self.plugin.ALL
114 check(True)
115 self.plugin.join_mode = self.plugin.INVITED
116 check(False)
117 self.plugin.invitations[ROOM_JID] = [(None, [Const.JID[0].userhostJID()])]
118 check(True)
119 self.plugin.join_mode = self.plugin.NONE
120 check(False)
121 self.plugin.games[ROOM_JID]['players'].append(Const.JID[0].user)
122 check(True)
123
124 def test_updatePlayers(self):
125 self.reinit()
126 self.initGame(0, 0)
127 self.assertEqual(self.plugin.games[ROOM_JID]['players'], [])
128 self.plugin._updatePlayers(ROOM_JID, [], True, Const.PROFILE[0])
129 self.assertEqual(self.plugin.games[ROOM_JID]['players'], [])
130 self.plugin._updatePlayers(ROOM_JID, ["user1"], True, Const.PROFILE[0])
131 self.assertEqual(self.plugin.games[ROOM_JID]['players'], ["user1"])
132 self.plugin._updatePlayers(ROOM_JID, ["user2", "user3"], True, Const.PROFILE[0])
133 self.assertEqual(self.plugin.games[ROOM_JID]['players'], ["user1", "user2", "user3"])
134 self.plugin._updatePlayers(ROOM_JID, ["user2", "user3"], True, Const.PROFILE[0]) # should not be stored twice
135 self.assertEqual(self.plugin.games[ROOM_JID]['players'], ["user1", "user2", "user3"])
136
137 def test_synchronizeRoom(self):
138 self.reinit()
139 self.initGame(0, 0)
140 self.plugin._synchronizeRoom(ROOM_JID, [Const.MUC[0]], Const.PROFILE[0])
141 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "players", []))
142 self.plugin.games[ROOM_JID]['players'].append("test1")
143 self.plugin._synchronizeRoom(ROOM_JID, [Const.MUC[0]], Const.PROFILE[0])
144 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "players", ["test1"]))
145 self.plugin.games[ROOM_JID]['started'] = True
146 self.plugin.games[ROOM_JID]['players'].append("test2")
147 self.plugin._synchronizeRoom(ROOM_JID, [Const.MUC[0]], Const.PROFILE[0])
148 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "started", ["test1", "test2"]))
149 self.plugin.games[ROOM_JID]['players'].append("test3")
150 self.plugin.games[ROOM_JID]['players'].append("test4")
151 user1 = JID(ROOM_JID.userhost() + "/" + Const.JID[0].user)
152 user2 = JID(ROOM_JID.userhost() + "/" + Const.JID[1].user)
153 self.plugin._synchronizeRoom(ROOM_JID, [user1, user2], Const.PROFILE[0])
154 self.assertEqualXML(self.host.getSentMessageXml(0), self._expectedMessage(user1, "normal", "started", ["test1", "test2", "test3", "test4"]))
155 self.assertEqualXML(self.host.getSentMessageXml(0), self._expectedMessage(user2, "normal", "started", ["test1", "test2", "test3", "test4"]))
156
157 def test_invitePlayers(self):
158 self.reinit()
159 self.initGame(0, 0)
160 self.plugin_0045.joinRoom(0, 1)
161 self.assertEqual(self.plugin.invitations[ROOM_JID], [])
162 room = self.plugin_0045.getRoom(0, 0)
163 nicks = self.plugin._invitePlayers(room, [Const.JID[1], Const.JID[2]], Const.JID[0].user, Const.PROFILE[0])
164 self.assertEqual(self.plugin.invitations[ROOM_JID][0][1], [Const.JID[1].userhostJID(), Const.JID[2].userhostJID()])
165 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost
166 self.assertEqual(nicks, [Const.JID[1].user, Const.JID[2].user])
167
168 nicks = self.plugin._invitePlayers(room, [Const.JID[1], Const.JID[3]], Const.JID[0].user, Const.PROFILE[0])
169 self.assertEqual(self.plugin.invitations[ROOM_JID][1][1], [Const.JID[1].userhostJID(), Const.JID[3].userhostJID()])
170 # this time Const.JID[1] and Const.JID[3] have the same user but the host differs
171 self.assertEqual(nicks, [Const.JID[1].user])
172
173 def test_checkInviteAuth(self):
174
175 def check(value, index):
176 nick = self.plugin_0045.getNick(0, index)
177 getattr(self, "assert%s" % value)(self.plugin._checkInviteAuth(ROOM_JID, nick))
178
179 self.reinit()
180
181 for mode in [self.plugin.FROM_ALL, self.plugin.FROM_NONE, self.plugin.FROM_REFEREE, self.plugin.FROM_PLAYERS]:
182 self.plugin.invite_mode = mode
183 check(True, 0)
184
185 self.initGame(0, 0)
186 self.plugin.invite_mode = self.plugin.FROM_ALL
187 check(True, 0)
188 check(True, 1)
189 self.plugin.invite_mode = self.plugin.FROM_NONE
190 check(True, 0) # game initialized but not started yet, referee can invite
191 check(False, 1)
192 self.plugin.invite_mode = self.plugin.FROM_REFEREE
193 check(True, 0)
194 check(False, 1)
195 user_nick = self.plugin_0045.joinRoom(0, 1)
196 self.plugin.games[ROOM_JID]['players'].append(user_nick)
197 self.plugin.invite_mode = self.plugin.FROM_PLAYERS
198 check(True, 0)
199 check(True, 1)
200 check(False, 2)
201
202 def test_isReferee(self):
203 self.reinit()
204 self.initGame(0, 0)
205 self.assertTrue(self.plugin.isReferee(ROOM_JID, self.plugin_0045.getNick(0, 0)))
206 self.assertFalse(self.plugin.isReferee(ROOM_JID, self.plugin_0045.getNick(0, 1)))
207
208 def test_isPlayer(self):
209 self.reinit()
210 self.initGame(0, 0)
211 self.assertTrue(self.plugin.isPlayer(ROOM_JID, self.plugin_0045.getNick(0, 0)))
212 user_nick = self.plugin_0045.joinRoom(0, 1)
213 self.plugin.games[ROOM_JID]['players'].append(user_nick)
214 self.assertTrue(self.plugin.isPlayer(ROOM_JID, user_nick))
215 self.assertFalse(self.plugin.isPlayer(ROOM_JID, self.plugin_0045.getNick(0, 2)))
216
217 def test_checkWaitAuth(self):
218
219 def check(value, other_players, confirmed, rest):
220 room = self.plugin_0045.getRoom(0, 0)
221 self.assertEqual((value, confirmed, rest), self.plugin._checkWaitAuth(room, other_players))
222
223 self.reinit()
224 self.initGame(0, 0)
225 other_players = [Const.JID[1], Const.JID[3]]
226 self.plugin.wait_mode = self.plugin.FOR_NONE
227 check(True, [], [], [])
228 check(True, [Const.JID[0]], [], [Const.JID[0]]) # getRoomNickOfUser checks for the other users only
229 check(True, other_players, [], other_players)
230 self.plugin.wait_mode = self.plugin.FOR_ALL
231 check(True, [], [], [])
232 check(False, [Const.JID[0]], [], [Const.JID[0]])
233 check(False, other_players, [], other_players)
234 self.plugin_0045.joinRoom(0, 1)
235 check(False, other_players, [], other_players)
236 self.plugin_0045.joinRoom(0, 4)
237 check(False, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0)], [Const.JID[3]])
238 self.plugin_0045.joinRoom(0, 3)
239 check(True, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0),
240 self.plugin_0045.getNickOfUser(0, 3, 0)], [])
241
242 other_players = [Const.JID[1], Const.JID[3], Const.JID[2]]
243 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost
244 check(True, other_players, [self.plugin_0045.getNickOfUser(0, 1, 0),
245 self.plugin_0045.getNickOfUser(0, 3, 0),
246 self.plugin_0045.getNickOfUser(0, 2, 0)], [])
247
248 def test_prepareRoom_trivial(self):
249 self.reinit()
250 other_players = []
251 self.plugin.prepareRoom(other_players, ROOM_JID, PROFILE)
252 self.assertTrue(self.plugin._gameExists(ROOM_JID, True))
253 self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID, Const.JID[0], Const.JID[0].user))
254 self.assertTrue(self.plugin._checkInviteAuth(ROOM_JID, Const.JID[0].user))
255 self.assertEqual((True, [], []), self.plugin._checkWaitAuth(ROOM_JID, []))
256 self.assertTrue(self.plugin.isReferee(ROOM_JID, Const.JID[0].user))
257 self.assertTrue(self.plugin.isPlayer(ROOM_JID, Const.JID[0].user))
258 self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE))
259
260 def test_prepareRoom_invite(self):
261 self.reinit()
262 other_players = [Const.JID[1], Const.JID[2]]
263 self.plugin.prepareRoom(other_players, ROOM_JID, PROFILE)
264 room = self.plugin_0045.getRoom(0, 0)
265
266 self.assertTrue(self.plugin._gameExists(ROOM_JID, True))
267 self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID, Const.JID[1], Const.JID[1].user))
268 self.assertFalse(self.plugin._checkJoinAuth(ROOM_JID, Const.JID[3], Const.JID[3].user))
269 self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID, Const.JID[1].user))
270 self.assertEqual((True, [], other_players), self.plugin._checkWaitAuth(room, other_players))
271
272 player2_nick = self.plugin_0045.joinRoom(0, 1)
273 self.plugin.userJoinedTrigger(room, room.roster[player2_nick], PROFILE)
274 self.assertTrue(self.plugin.isPlayer(ROOM_JID, player2_nick))
275 self.assertTrue(self.plugin._checkInviteAuth(ROOM_JID, player2_nick))
276 self.assertFalse(self.plugin.isReferee(ROOM_JID, player2_nick))
277 self.assertTrue(self.plugin.isPlayer(ROOM_JID, player2_nick))
278 self.assertTrue(self.plugin.isPlayer(ROOM_JID, self.plugin_0045.getNickOfUser(0, 2, 0)))
279 self.assertFalse(self.plugin.isPlayer(ROOM_JID, "xxx"))
280 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, Const.PROFILE[1]))
281
282 def test_prepareRoom_score1(self):
283 self.reinit(player_init={'score': 0})
284 other_players = [Const.JID[1], Const.JID[2]]
285 self.plugin.prepareRoom(other_players, ROOM_JID, PROFILE)
286 room = self.plugin_0045.getRoom(0, 0)
287
288 self.assertFalse(self.plugin._gameExists(ROOM_JID, True))
289 self.assertTrue(self.plugin._checkJoinAuth(ROOM_JID, Const.JID[1], Const.JID[1].user))
290 self.assertFalse(self.plugin._checkJoinAuth(ROOM_JID, Const.JID[3], Const.JID[3].user))
291 self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID, Const.JID[1].user))
292 self.assertEqual((False, [], other_players), self.plugin._checkWaitAuth(room, other_players))
293
294 user_nick = self.plugin_0045.joinRoom(0, 1)
295 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE)
296 self.assertTrue(self.plugin.isPlayer(ROOM_JID, user_nick))
297 self.assertFalse(self.plugin._checkInviteAuth(ROOM_JID, user_nick))
298 self.assertFalse(self.plugin.isReferee(ROOM_JID, user_nick))
299 self.assertTrue(self.plugin.isPlayer(ROOM_JID, user_nick))
300 # the following assertion is True because Const.JID[1] and Const.JID[2] have the same userhost
301 self.assertTrue(self.plugin.isPlayer(ROOM_JID, self.plugin_0045.getNickOfUser(0, 2, 0)))
302 # the following assertion is True because Const.JID[1] nick in the room is equal to Const.JID[3].user
303 self.assertTrue(self.plugin.isPlayer(ROOM_JID, Const.JID[3].user))
304 # but Const.JID[3] is actually not in the room
305 self.assertEqual(self.plugin_0045.getNickOfUser(0, 3, 0), None)
306 self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID, Const.PROFILE[0]))
307
308 def test_prepareRoom_score2(self):
309 self.reinit(player_init={'score': 0})
310 other_players = [Const.JID[1], Const.JID[4]]
311 self.plugin.prepareRoom(other_players, ROOM_JID, PROFILE)
312 room = self.plugin_0045.getRoom(0, 0)
313
314 user_nick = self.plugin_0045.joinRoom(0, 1)
315 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE)
316 self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE))
317 user_nick = self.plugin_0045.joinRoom(0, 4)
318 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE)
319 self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE))
320
321 def test_userJoinedTrigger(self):
322 self.reinit(player_init={"xxx": "xyz"})
323 other_players = [Const.JID[1], Const.JID[3]]
324 self.plugin.prepareRoom(other_players, ROOM_JID, PROFILE)
325 nicks = [self.plugin_0045.getNick(0, 0)]
326
327 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "players", nicks))
328 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 1)
329
330 # wrong profile
331 user_nick = self.plugin_0045.joinRoom(0, 1)
332 room = self.plugin_0045.getRoom(0, 1)
333 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), OTHER_PROFILE)
334 self.assertEqual(self.host.getSentMessage(0), None) # no new message has been sent
335 self.assertFalse(self.plugin._gameExists(ROOM_JID, True)) # game not started
336
337 # referee profile, user is allowed, wait for one more
338 room = self.plugin_0045.getRoom(0, 0)
339 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE)
340 nicks.append(user_nick)
341 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "players", nicks))
342 self.assertFalse(self.plugin._gameExists(ROOM_JID, True)) # game not started
343
344 # referee profile, user is not allowed
345 user_nick = self.plugin_0045.joinRoom(0, 4)
346 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE)
347 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(JID(ROOM_JID.userhost() + '/' + user_nick), "normal", "players", nicks))
348 self.assertFalse(self.plugin._gameExists(ROOM_JID, True)) # game not started
349
350 # referee profile, user is allowed, everybody here
351 user_nick = self.plugin_0045.joinRoom(0, 3)
352 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
353 nicks.append(user_nick)
354 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "started", nicks))
355 self.assertTrue(self.plugin._gameExists(ROOM_JID, True)) # game started
356 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 0)
357
358 # wait for none
359 self.reinit()
360 self.plugin.prepareRoom(other_players, ROOM_JID, PROFILE)
361 self.assertNotEqual(self.host.getSentMessage(0), None) # init messages
362 room = self.plugin_0045.getRoom(0, 0)
363 nicks = [self.plugin_0045.getNick(0, 0)]
364 user_nick = self.plugin_0045.joinRoom(0, 3)
365 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
366 nicks.append(user_nick)
367 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "started", nicks))
368 self.assertTrue(self.plugin._gameExists(ROOM_JID, True))
369
370 def test_userLeftTrigger(self):
371 self.reinit(player_init={"xxx": "xyz"})
372 other_players = [Const.JID[1], Const.JID[3], Const.JID[4]]
373 self.plugin.prepareRoom(other_players, ROOM_JID, PROFILE)
374 room = self.plugin_0045.getRoom(0, 0)
375 nicks = [self.plugin_0045.getNick(0, 0)]
376 self.assertEqual(self.plugin.invitations[ROOM_JID][0][1], [Const.JID[1].userhostJID(), Const.JID[3].userhostJID(), Const.JID[4].userhostJID()])
377
378 # one user joins
379 user_nick = self.plugin_0045.joinRoom(0, 1)
380 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE)
381 nicks.append(user_nick)
382
383 # the user leaves
384 self.assertEqual(self.plugin.games[ROOM_JID]['players'], nicks)
385 room = self.plugin_0045.getRoom(0, 1)
386 # to not call self.plugin_0045.leaveRoom(0, 1) here, we are testing the trigger with a wrong profile
387 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[1]), Const.PROFILE[1]) # not the referee
388 self.assertEqual(self.plugin.games[ROOM_JID]['players'], nicks)
389 room = self.plugin_0045.getRoom(0, 0)
390 user_nick = self.plugin_0045.leaveRoom(0, 1)
391 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[1]), PROFILE) # referee
392 nicks.pop()
393 self.assertEqual(self.plugin.games[ROOM_JID]['players'], nicks)
394
395 # all the users join
396 user_nick = self.plugin_0045.joinRoom(0, 1)
397 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE)
398 nicks.append(user_nick)
399 user_nick = self.plugin_0045.joinRoom(0, 3)
400 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
401 nicks.append(user_nick)
402 user_nick = self.plugin_0045.joinRoom(0, 4)
403 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE)
404 nicks.append(user_nick)
405 self.assertEqual(self.plugin.games[ROOM_JID]['players'], nicks)
406 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 0)
407
408 # one user leaves
409 user_nick = self.plugin_0045.leaveRoom(0, 4)
410 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[4]), PROFILE)
411 nicks.pop()
412 self.assertEqual(self.plugin.invitations[ROOM_JID][0][1], [Const.JID[4].userhostJID()])
413
414 # another leaves
415 user_nick = self.plugin_0045.leaveRoom(0, 3)
416 self.plugin.userLeftTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
417 nicks.pop()
418 self.assertEqual(self.plugin.invitations[ROOM_JID][0][1], [Const.JID[4].userhostJID(), Const.JID[3].userhostJID()])
419
420 # they can join again
421 user_nick = self.plugin_0045.joinRoom(0, 3)
422 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
423 nicks.append(user_nick)
424 user_nick = self.plugin_0045.joinRoom(0, 4)
425 self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE)
426 nicks.append(user_nick)
427 self.assertEqual(self.plugin.games[ROOM_JID]['players'], nicks)
428 self.assertTrue(len(self.plugin.invitations[ROOM_JID]) == 0)
429
430 def test__checkCreateGameAndInit(self):
431 self.reinit()
432 helpers.muteLogging()
433 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE))
434 helpers.unmuteLogging()
435
436 nick = self.plugin_0045.joinRoom(0, 0)
437 self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE))
438 self.assertTrue(self.plugin._gameExists(ROOM_JID, False))
439 self.assertFalse(self.plugin._gameExists(ROOM_JID, True))
440 self.assertTrue(self.plugin.isReferee(ROOM_JID, nick))
441
442 helpers.muteLogging()
443 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, OTHER_PROFILE))
444 helpers.unmuteLogging()
445
446 self.plugin_0045.joinRoom(0, 1)
447 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, OTHER_PROFILE))
448
449 self.plugin.createGame(ROOM_JID, [Const.JID[1]], PROFILE)
450 self.assertEqual((False, True), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE))
451 self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, OTHER_PROFILE))
452
453 def test_createGame(self):
454
455 self.reinit(player_init={"xxx": "xyz"})
456 nicks = []
457 for i in [0, 1, 3, 4]:
458 nicks.append(self.plugin_0045.joinRoom(0, i))
459
460 # game not exists
461 self.plugin.createGame(ROOM_JID, nicks, PROFILE)
462 self.assertTrue(self.plugin._gameExists(ROOM_JID, True))
463 self.assertEqual(self.plugin.games[ROOM_JID]['players'], nicks)
464 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "started", nicks))
465 for nick in nicks:
466 self.assertEqual('init', self.plugin.games[ROOM_JID]['status'][nick])
467 self.assertEqual(self.plugin.player_init, self.plugin.games[ROOM_JID]['players_data'][nick])
468 self.plugin.games[ROOM_JID]['players_data'][nick]["xxx"] = nick
469 for nick in nicks:
470 # checks that a copy of self.player_init has been done and not a reference
471 self.assertEqual(nick, self.plugin.games[ROOM_JID]['players_data'][nick]['xxx'])
472
473 # game exists, current profile is referee
474 self.reinit(player_init={"xxx": "xyz"})
475 self.initGame(0, 0)
476 self.plugin.games[ROOM_JID]['started'] = True
477 self.plugin.createGame(ROOM_JID, nicks, PROFILE)
478 self.assertEqual(self.host.getSentMessageXml(0), self._expectedMessage(ROOM_JID, "groupchat", "started", nicks))
479
480 # game exists, current profile is not referee
481 self.reinit(player_init={"xxx": "xyz"})
482 self.initGame(0, 0)
483 self.plugin.games[ROOM_JID]['started'] = True
484 self.plugin_0045.joinRoom(0, 1)
485 self.plugin.createGame(ROOM_JID, nicks, OTHER_PROFILE)
486 self.assertEqual(self.host.getSentMessage(0), None) # no sync message has been sent by other_profile