Mercurial > libervia-backend
comparison src/test/test_plugin_misc_radiocol.py @ 1412:979210da778a
test: fix the tests
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 17 Apr 2015 19:06:39 +0200 |
parents | 2308f8405ffb |
children | 0681d69cbe0a |
comparison
equal
deleted
inserted
replaced
1411:8767c0bb7d48 | 1412:979210da778a |
---|---|
40 import os | 40 import os |
41 import copy | 41 import copy |
42 import shutil | 42 import shutil |
43 from logging import WARNING | 43 from logging import WARNING |
44 | 44 |
45 ROOM_JID_S = Const.MUC_STR[0] | 45 ROOM_JID = JID(Const.MUC_STR[0]) |
46 PROFILE = Const.PROFILE[0] | 46 PROFILE = Const.PROFILE[0] |
47 REFEREE_FULL = ROOM_JID_S + '/' + Const.JID[0].user | 47 REFEREE_FULL = JID(ROOM_JID.userhost() + '/' + Const.JID[0].user) |
48 PLAYERS_INDICES = [0, 1, 3] # referee included | 48 PLAYERS_INDICES = [0, 1, 3] # referee included |
49 OTHER_PROFILES = [Const.PROFILE[1], Const.PROFILE[3]] | 49 OTHER_PROFILES = [Const.PROFILE[1], Const.PROFILE[3]] |
50 OTHER_PLAYERS = [Const.JID_STR[1], Const.JID_STR[3]] | 50 OTHER_PLAYERS = [Const.JID[1], Const.JID[3]] |
51 | 51 |
52 | 52 |
53 class RadiocolTest(helpers.SatTestCase): | 53 class RadiocolTest(helpers.SatTestCase): |
54 | 54 |
55 def setUp(self): | 55 def setUp(self): |
60 self.host.plugins['ROOM-GAME'] = plugin_room_game.RoomGame(self.host) | 60 self.host.plugins['ROOM-GAME'] = plugin_room_game.RoomGame(self.host) |
61 self.plugin = plugin.Radiocol(self.host) # must be init after ROOM-GAME | 61 self.plugin = plugin.Radiocol(self.host) # must be init after ROOM-GAME |
62 self.plugin.testing = True | 62 self.plugin.testing = True |
63 self.plugin_0045 = self.host.plugins['XEP-0045'] = helpers_plugins.FakeXEP_0045(self.host) | 63 self.plugin_0045 = self.host.plugins['XEP-0045'] = helpers_plugins.FakeXEP_0045(self.host) |
64 self.plugin_0249 = self.host.plugins['XEP-0249'] = helpers_plugins.FakeXEP_0249(self.host) | 64 self.plugin_0249 = self.host.plugins['XEP-0249'] = helpers_plugins.FakeXEP_0249(self.host) |
65 logger = getLogger() | |
66 level = logger.getEffectiveLevel() | |
67 logger.setLevel(WARNING) # remove log.info pollution | |
68 for profile in Const.PROFILE: | 65 for profile in Const.PROFILE: |
69 self.host.getClient(profile) # init self.host.profiles[profile] | 66 self.host.getClient(profile) # init self.host.profiles[profile] |
70 logger.setLevel(level) | |
71 self.songs = [] | 67 self.songs = [] |
72 self.playlist = [] | 68 self.playlist = [] |
73 self.sound_dir = self.host.memory.getConfig('', 'media_dir') + '/test/sound/' | 69 self.sound_dir = self.host.memory.getConfig('', 'media_dir') + '/test/sound/' |
74 try: | 70 try: |
75 for filename in os.listdir(self.sound_dir): | 71 for filename in os.listdir(self.sound_dir): |
88 for i in xrange(0, len(players)): | 84 for i in xrange(0, len(players)): |
89 content += "<player index='%s'>%s</player>" % (i, players[i]) | 85 content += "<player index='%s'>%s</player>" % (i, players[i]) |
90 content += "</started>" | 86 content += "</started>" |
91 return content | 87 return content |
92 | 88 |
93 def _expectedMessage(self, to_s, type_, content): | 89 def _expectedMessage(self, to_jid, type_, content): |
94 """ | 90 """ |
95 @param to_s: recipient full jid as unicode | 91 @param to_jid: recipient full jid |
96 @param type_: message type ('normal' or 'groupchat') | 92 @param type_: message type ('normal' or 'groupchat') |
97 @param content: content as unicode or list of domish elements | 93 @param content: content as unicode or list of domish elements |
98 @return: the message XML built from the given recipient, message type and content | 94 @return: the message XML built from the given recipient, message type and content |
99 """ | 95 """ |
100 if isinstance(content, list): | 96 if isinstance(content, list): |
101 new_content = copy.deepcopy(content) | 97 new_content = copy.deepcopy(content) |
102 for element in new_content: | 98 for element in new_content: |
103 if not element.hasAttribute('xmlns'): | 99 if not element.hasAttribute('xmlns'): |
104 element['xmlns'] = '' | 100 element['xmlns'] = '' |
105 content = "".join([element.toXml() for element in new_content]) | 101 content = "".join([element.toXml() for element in new_content]) |
106 return "<message to='%s' type='%s'><%s xmlns='%s'>%s</%s></message>" % (to_s, type_, plugin.RADIOC_TAG, plugin.NC_RADIOCOL, content, plugin.RADIOC_TAG) | 102 return "<message to='%s' type='%s'><%s xmlns='%s'>%s</%s></message>" % (to_jid.full(), type_, plugin.RADIOC_TAG, plugin.NC_RADIOCOL, content, plugin.RADIOC_TAG) |
107 | 103 |
108 def _rejectSongCb(self, profile_index): | 104 def _rejectSongCb(self, profile_index): |
109 """Check if the message "song_rejected" has been sent by the referee | 105 """Check if the message "song_rejected" has been sent by the referee |
110 and process the command with the profile of the uploader | 106 and process the command with the profile of the uploader |
111 @param profile_index: uploader's profile""" | 107 @param profile_index: uploader's profile""" |
112 sent = self.host.getSentMessage(0) | 108 sent = self.host.getSentMessage(0) |
113 content = "<song_rejected xmlns='' reason='Too many songs in queue'/>" | 109 content = "<song_rejected xmlns='' reason='Too many songs in queue'/>" |
114 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID_S + '/' + self.plugin_0045.getNick(0, profile_index), 'normal', content)) | 110 self.assertEqualXML(sent.toXml(), self._expectedMessage(JID(ROOM_JID.userhost() + '/' + self.plugin_0045.getNick(0, profile_index), 'normal', content))) |
115 self._roomGameCmd(sent, ['radiocolSongRejected', ROOM_JID_S, 'Too many songs in queue']) | 111 self._roomGameCmd(sent, ['radiocolSongRejected', ROOM_JID.full(), 'Too many songs in queue']) |
116 | 112 |
117 def _noUploadCb(self): | 113 def _noUploadCb(self): |
118 """Check if the message "no_upload" has been sent by the referee | 114 """Check if the message "no_upload" has been sent by the referee |
119 and process the command with the profiles of each room users""" | 115 and process the command with the profiles of each room users""" |
120 sent = self.host.getSentMessage(0) | 116 sent = self.host.getSentMessage(0) |
121 content = "<no_upload xmlns=''/>" | 117 content = "<no_upload xmlns=''/>" |
122 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID_S, 'groupchat', content)) | 118 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID, 'groupchat', content)) |
123 self._roomGameCmd(sent, ['radiocolNoUpload', ROOM_JID_S]) | 119 self._roomGameCmd(sent, ['radiocolNoUpload', ROOM_JID.full()]) |
124 | 120 |
125 def _uploadOkCb(self): | 121 def _uploadOkCb(self): |
126 """Check if the message "upload_ok" has been sent by the referee | 122 """Check if the message "upload_ok" has been sent by the referee |
127 and process the command with the profiles of each room users""" | 123 and process the command with the profiles of each room users""" |
128 sent = self.host.getSentMessage(0) | 124 sent = self.host.getSentMessage(0) |
129 content = "<upload_ok xmlns=''/>" | 125 content = "<upload_ok xmlns=''/>" |
130 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID_S, 'groupchat', content)) | 126 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID, 'groupchat', content)) |
131 self._roomGameCmd(sent, ['radiocolUploadOk', ROOM_JID_S]) | 127 self._roomGameCmd(sent, ['radiocolUploadOk', ROOM_JID.full()]) |
132 | 128 |
133 def _preloadCb(self, attrs, profile_index): | 129 def _preloadCb(self, attrs, profile_index): |
134 """Check if the message "preload" has been sent by the referee | 130 """Check if the message "preload" has been sent by the referee |
135 and process the command with the profiles of each room users | 131 and process the command with the profiles of each room users |
136 @param attrs: information dict about the song | 132 @param attrs: information dict about the song |
142 preload_elt = domish.generateElementsNamed(radiocol_elt.elements(), 'preload').next() | 138 preload_elt = domish.generateElementsNamed(radiocol_elt.elements(), 'preload').next() |
143 attrs['timestamp'] = preload_elt['timestamp'] # we could not guess it... | 139 attrs['timestamp'] = preload_elt['timestamp'] # we could not guess it... |
144 content = "<preload xmlns='' %s/>" % " ".join(["%s='%s'" % (attr, attrs[attr]) for attr in attrs]) | 140 content = "<preload xmlns='' %s/>" % " ".join(["%s='%s'" % (attr, attrs[attr]) for attr in attrs]) |
145 if sent.hasAttribute('from'): | 141 if sent.hasAttribute('from'): |
146 del sent['from'] | 142 del sent['from'] |
147 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID_S, 'groupchat', content)) | 143 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID, 'groupchat', content)) |
148 self._roomGameCmd(sent, ['radiocolPreload', ROOM_JID_S, attrs['timestamp'], attrs['filename'], attrs['title'], attrs['artist'], attrs['album'], attrs['sender']]) | 144 self._roomGameCmd(sent, ['radiocolPreload', ROOM_JID.full(), attrs['timestamp'], attrs['filename'], attrs['title'], attrs['artist'], attrs['album'], attrs['sender']]) |
149 | 145 |
150 def _playNextSongCb(self): | 146 def _playNextSongCb(self): |
151 """Check if the message "play" has been sent by the referee | 147 """Check if the message "play" has been sent by the referee |
152 and process the command with the profiles of each room users""" | 148 and process the command with the profiles of each room users""" |
153 sent = self.host.getSentMessage(0) | 149 sent = self.host.getSentMessage(0) |
154 filename = self.playlist.pop(0) | 150 filename = self.playlist.pop(0) |
155 content = "<play xmlns='' filename='%s' />" % filename | 151 content = "<play xmlns='' filename='%s' />" % filename |
156 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID_S, 'groupchat', content)) | 152 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID, 'groupchat', content)) |
157 self._roomGameCmd(sent, ['radiocolPlay', ROOM_JID_S, filename]) | 153 self._roomGameCmd(sent, ['radiocolPlay', ROOM_JID.full(), filename]) |
158 | 154 |
159 game_data = self.plugin.games[ROOM_JID_S] | 155 game_data = self.plugin.games[ROOM_JID] |
160 if len(game_data['queue']) == plugin.QUEUE_LIMIT - 1: | 156 if len(game_data['queue']) == plugin.QUEUE_LIMIT - 1: |
161 self._uploadOkCb() | 157 self._uploadOkCb() |
162 | 158 |
163 def _addSongCb(self, d, filepath, profile_index): | 159 def _addSongCb(self, d, filepath, profile_index): |
164 """Check if the message "song_added" has been sent by the uploader | 160 """Check if the message "song_added" has been sent by the uploader |
168 @param profile_index: the profile index of the uploader | 164 @param profile_index: the profile index of the uploader |
169 """ | 165 """ |
170 if isinstance(d, Failure): | 166 if isinstance(d, Failure): |
171 self.fail("OGG song could not be added!") | 167 self.fail("OGG song could not be added!") |
172 | 168 |
173 game_data = self.plugin.games[ROOM_JID_S] | 169 game_data = self.plugin.games[ROOM_JID] |
174 song = OggVorbis(filepath) | 170 song = OggVorbis(filepath) |
175 attrs = {'filename': os.path.basename(filepath), | 171 attrs = {'filename': os.path.basename(filepath), |
176 'title': song.get("title", ["Unknown"])[0], | 172 'title': song.get("title", ["Unknown"])[0], |
177 'artist': song.get("artist", ["Unknown"])[0], | 173 'artist': song.get("artist", ["Unknown"])[0], |
178 'album': song.get("album", ["Unknown"])[0], | 174 'album': song.get("album", ["Unknown"])[0], |
211 """ | 207 """ |
212 if isinstance(from_index, list): | 208 if isinstance(from_index, list): |
213 call = from_index | 209 call = from_index |
214 from_index = 0 | 210 from_index = 0 |
215 | 211 |
216 sent['from'] = ROOM_JID_S + '/' + self.plugin_0045.getNick(0, from_index) | 212 sent['from'] = ROOM_JID.full() + '/' + self.plugin_0045.getNick(0, from_index) |
217 recipient = JID(sent['to']).resource | 213 recipient = JID(sent['to']).resource |
218 | 214 |
219 # The message could have been sent to a room user (room_jid + '/' + nick), | 215 # The message could have been sent to a room user (room_jid + '/' + nick), |
220 # but when it is received, the 'to' attribute of the message has been | 216 # but when it is received, the 'to' attribute of the message has been |
221 # changed to the recipient own JID. We need to simulate that here. | 217 # changed to the recipient own JID. We need to simulate that here. |
225 | 221 |
226 for index in xrange(0, len(Const.PROFILE)): | 222 for index in xrange(0, len(Const.PROFILE)): |
227 nick = self.plugin_0045.getNick(0, index) | 223 nick = self.plugin_0045.getNick(0, index) |
228 if nick: | 224 if nick: |
229 if not recipient or nick == recipient: | 225 if not recipient or nick == recipient: |
230 if call and (self.plugin.isPlayer(ROOM_JID_S, nick) or call[0] == 'radiocolStarted'): | 226 if call and (self.plugin.isPlayer(ROOM_JID, nick) or call[0] == 'radiocolStarted'): |
231 args = copy.deepcopy(call) | 227 args = copy.deepcopy(call) |
232 args.append(Const.PROFILE[index]) | 228 args.append(Const.PROFILE[index]) |
233 self.host.bridge.expectCall(*args) | 229 self.host.bridge.expectCall(*args) |
234 self.plugin.room_game_cmd(sent, Const.PROFILE[index]) | 230 self.plugin.room_game_cmd(sent, Const.PROFILE[index]) |
235 | 231 |
237 """Synchronize one player when he joins a running game. | 233 """Synchronize one player when he joins a running game. |
238 @param sync_data: result from self.plugin.getSyncData | 234 @param sync_data: result from self.plugin.getSyncData |
239 @param profile_index: index of the profile to be synchronized | 235 @param profile_index: index of the profile to be synchronized |
240 """ | 236 """ |
241 for nick in sync_data: | 237 for nick in sync_data: |
242 expected = self._expectedMessage(ROOM_JID_S + '/' + nick, 'normal', sync_data[nick]) | 238 expected = self._expectedMessage(JID(ROOM_JID.userhost() + '/' + nick), 'normal', sync_data[nick]) |
243 sent = self.host.getSentMessage(0) | 239 sent = self.host.getSentMessage(0) |
244 self.assertEqualXML(sent.toXml(), expected) | 240 self.assertEqualXML(sent.toXml(), expected) |
245 for elt in sync_data[nick]: | 241 for elt in sync_data[nick]: |
246 if elt.name == 'preload': | 242 if elt.name == 'preload': |
247 self.host.bridge.expectCall('radiocolPreload', ROOM_JID_S, elt['timestamp'], elt['filename'], elt['title'], elt['artist'], elt['album'], elt['sender'], Const.PROFILE[profile_index]) | 243 self.host.bridge.expectCall('radiocolPreload', ROOM_JID.full(), elt['timestamp'], elt['filename'], elt['title'], elt['artist'], elt['album'], elt['sender'], Const.PROFILE[profile_index]) |
248 elif elt.name == 'play': | 244 elif elt.name == 'play': |
249 self.host.bridge.expectCall('radiocolPlay', ROOM_JID_S, elt['filename'], Const.PROFILE[profile_index]) | 245 self.host.bridge.expectCall('radiocolPlay', ROOM_JID.full(), elt['filename'], Const.PROFILE[profile_index]) |
250 elif elt.name == 'no_upload': | 246 elif elt.name == 'no_upload': |
251 self.host.bridge.expectCall('radiocolNoUpload', ROOM_JID_S, Const.PROFILE[profile_index]) | 247 self.host.bridge.expectCall('radiocolNoUpload', ROOM_JID.full(), Const.PROFILE[profile_index]) |
252 sync_data[nick] | 248 sync_data[nick] |
253 self._roomGameCmd(sent, []) | 249 self._roomGameCmd(sent, []) |
254 | 250 |
255 def _joinRoom(self, room, nicks, player_index, sync=True): | 251 def _joinRoom(self, room, nicks, player_index, sync=True): |
256 """Make a player join a room and update the list of nicks | 252 """Make a player join a room and update the list of nicks |
261 """ | 257 """ |
262 user_nick = self.plugin_0045.joinRoom(0, player_index) | 258 user_nick = self.plugin_0045.joinRoom(0, player_index) |
263 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) | 259 self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE) |
264 if player_index not in PLAYERS_INDICES: | 260 if player_index not in PLAYERS_INDICES: |
265 # this user is actually not a player | 261 # this user is actually not a player |
266 self.assertFalse(self.plugin.isPlayer(ROOM_JID_S, user_nick)) | 262 self.assertFalse(self.plugin.isPlayer(ROOM_JID, user_nick)) |
267 to_jid, type_ = (ROOM_JID_S + '/' + user_nick, 'normal') | 263 to_jid, type_ = (JID(ROOM_JID.userhost() + '/' + user_nick), 'normal') |
268 else: | 264 else: |
269 # this user is a player | 265 # this user is a player |
270 self.assertTrue(self.plugin.isPlayer(ROOM_JID_S, user_nick)) | 266 self.assertTrue(self.plugin.isPlayer(ROOM_JID, user_nick)) |
271 nicks.append(user_nick) | 267 nicks.append(user_nick) |
272 to_jid, type_ = (ROOM_JID_S, 'groupchat') | 268 to_jid, type_ = (ROOM_JID, 'groupchat') |
273 | 269 |
274 # Check that the message "players" has been sent by the referee | 270 # Check that the message "players" has been sent by the referee |
275 expected = self._expectedMessage(to_jid, type_, self._buildPlayers(nicks)) | 271 expected = self._expectedMessage(to_jid, type_, self._buildPlayers(nicks)) |
276 sent = self.host.getSentMessage(0) | 272 sent = self.host.getSentMessage(0) |
277 self.assertEqualXML(sent.toXml(), expected) | 273 self.assertEqualXML(sent.toXml(), expected) |
278 | 274 |
279 # Process the command with the profiles of each room users | 275 # Process the command with the profiles of each room users |
280 self._roomGameCmd(sent, ['radiocolStarted', ROOM_JID_S, REFEREE_FULL, nicks, [plugin.QUEUE_TO_START, plugin.QUEUE_LIMIT]]) | 276 self._roomGameCmd(sent, ['radiocolStarted', ROOM_JID.full(), REFEREE_FULL.full(), nicks, [plugin.QUEUE_TO_START, plugin.QUEUE_LIMIT]]) |
281 | 277 |
282 if sync: | 278 if sync: |
283 self._syncCb(self.plugin._getSyncData(ROOM_JID_S, [user_nick]), player_index) | 279 self._syncCb(self.plugin._getSyncData(ROOM_JID, [user_nick]), player_index) |
284 | 280 |
285 def _leaveRoom(self, room, nicks, player_index): | 281 def _leaveRoom(self, room, nicks, player_index): |
286 """Make a player leave a room and update the list of nicks | 282 """Make a player leave a room and update the list of nicks |
287 @param room: wokkel.muc.Room instance from the referee perspective | 283 @param room: wokkel.muc.Room instance from the referee perspective |
288 @param nicks: list of the players which will be updated | 284 @param nicks: list of the players which will be updated |
340 | 336 |
341 def test_game(self): | 337 def test_game(self): |
342 self.reinit() | 338 self.reinit() |
343 | 339 |
344 # create game | 340 # create game |
345 self.plugin.prepareRoom(OTHER_PLAYERS, ROOM_JID_S, PROFILE) | 341 self.plugin.prepareRoom(OTHER_PLAYERS, ROOM_JID, PROFILE) |
346 self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) | 342 self.assertTrue(self.plugin._gameExists(ROOM_JID, True)) |
347 room = self.plugin_0045.getRoom(0, 0) | 343 room = self.plugin_0045.getRoom(0, 0) |
348 nicks = [self.plugin_0045.getNick(0, 0)] | 344 nicks = [self.plugin_0045.getNick(0, 0)] |
349 | 345 |
350 sent = self.host.getSentMessage(0) | 346 sent = self.host.getSentMessage(0) |
351 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID_S, 'groupchat', self._buildPlayers(nicks))) | 347 self.assertEqualXML(sent.toXml(), self._expectedMessage(ROOM_JID, 'groupchat', self._buildPlayers(nicks))) |
352 self._roomGameCmd(sent, ['radiocolStarted', ROOM_JID_S, REFEREE_FULL, nicks, [plugin.QUEUE_TO_START, plugin.QUEUE_LIMIT]]) | 348 self._roomGameCmd(sent, ['radiocolStarted', ROOM_JID.full(), REFEREE_FULL.full(), nicks, [plugin.QUEUE_TO_START, plugin.QUEUE_LIMIT]]) |
353 | 349 |
354 self._joinRoom(room, nicks, 1) # player joins | 350 self._joinRoom(room, nicks, 1) # player joins |
355 self._joinRoom(room, nicks, 4) # user not playing joins | 351 self._joinRoom(room, nicks, 4) # user not playing joins |
356 | 352 |
357 song_index = 0 | 353 song_index = 0 |