def test_room_title(self): self.assertIsNone(self.__room_state.room_title) self.__handler.handle( WebSocketMessage(label='update-room', payload={'title': 'foobar'})) self.assertEqual('foobar', self.__room_state.room_title) self.__handler.handle(WebSocketMessage(label='update-room', payload={})) self.assertEqual('foobar', self.__room_state.room_title)
def test_no_welcome_because_no_message(self): self.__handler.handle( WebSocketMessage(label='update-room', payload={'users': [self.__to_spotify_user('a')]})) self.__handler.handle( WebSocketMessage( label='update-room', payload={ 'users': [self.__to_spotify_user('a'), self.__to_spotify_user('b')] })) self.__dequeue_and_assert_whispers([])
def test_command_no_payload(self) -> None: self.__handler.handle(WebSocketMessage( label='push-message', payload=self.__create_push_message('joe', '/fake ') )) self.assertTrue(self.__command_processor.was_called) self.assertEqual(self.__command_processor.call_user_id, 'joe') self.assertIsNone(self.__command_processor.call_payload)
def __on_open() -> None: logger.info('Websocket connection OPENED') web_socket_client.send( WebSocketMessage(label='join', payload={ 'roomId': env.get_jqbx_room_id(), 'user': get_bot_user(env) }))
def __on_message(message: WebSocketMessage) -> None: logger.info('Incoming Message', message.as_dict()) try: handler = web_socket_message_handler_map.get(message.label, None) if handler: handler.handle(message) except Exception as e: logger.error(e)
def test_tracks(self): self.assertIsNone(self.__room_state.current_track) last_track = create_random_id_object() self.__handler.handle( WebSocketMessage( label='update-room', payload={'tracks': [last_track, create_random_id_object()]})) self.assertEqual(last_track, self.__room_state.current_track)
def __parse(raw_message: str) -> WebSocketMessage: stripped = raw_message.strip() parts = stripped.split('[', 1) label = None payload = None if len(parts) > 1: json_array = json.loads('[%s' % parts[1]) label = json_array[0] payload = None if len(json_array) == 1 else json_array[1] return WebSocketMessage(int(parts[0]), label, payload)
def test_mod_ids(self): self.assertEqual(len(self.__room_state.mod_ids), 0) room_update_message = WebSocketMessage( label='update-room', payload={ 'admin': [self.__to_spotify_uri('1'), self.__to_spotify_uri('2')], 'mods': [self.__to_spotify_uri('2'), self.__to_spotify_uri('3')] }) self.__handler.handle(room_update_message) self.assertEqual(sorted(['1', '2', '3']), sorted(self.__room_state.mod_ids)) self.__handler.handle(WebSocketMessage(label='update-room', payload={})) self.assertEqual(sorted(['1', '2', '3']), sorted(self.__room_state.mod_ids))
def dope(self) -> None: if self.__doped or self.__noped: return self.__web_socket_client.send( WebSocketMessage(label='thumbsUp', payload={ 'roomId': self.__env.get_jqbx_room_id(), 'user': get_bot_user(self.__env) })) self.__doped = True
def send(self, web_socket_message: WebSocketMessage) -> None: self.__logger.info('Outgoing Message', web_socket_message.as_dict()) serialized = str(web_socket_message.code) array_part = [ x for x in [web_socket_message.label, web_socket_message.payload] if x ] if array_part: serialized += json.dumps(array_part) self.__ws.send(serialized)
def test_no_welcome_because_no_initial_users(self): self.__data_service.set_welcome_message('what it do nephew') self.__handler.handle( WebSocketMessage( label='update-room', payload={ 'users': [self.__to_spotify_user('1'), self.__to_spotify_user('2')] })) self.__dequeue_and_assert_whispers([])
def test_keep_awake(self): date = 123 keep_awake_message = WebSocketMessage(label='keepAwake', payload={'date': date}) self.__handler.handle(keep_awake_message) client_messages = self.__client.dequeue_client_messages() stay_awake_message = next(x for x in client_messages if x.label == 'stayAwake') self.assertEqual(stay_awake_message.payload['date'], date) code_2_message = next(x for x in client_messages if x.code == 2) self.assertIsNone(code_2_message.label) self.assertIsNone(code_2_message.payload)
def chat(self, message: Union[str, List[str]]) -> None: lines = message if isinstance(message, list) else [message] payload = { 'roomId': self.__env.get_jqbx_room_id(), 'user': get_bot_user(self.__env), 'message': { 'message': '<br/>'.join(lines), 'user': get_bot_user(self.__env), 'selectingEmoji': False } } self.__web_socket_client.send( WebSocketMessage(label='chat', payload=payload))
def whisper(self, message: str, recipient: dict) -> None: bot_user = get_bot_user(self.__env) payload = { 'roomId': self.__env.get_jqbx_room_id(), 'user': bot_user, 'message': { 'message': '@%s %s' % (recipient['username'], message), 'user': get_bot_user(self.__env), 'recipients': [recipient, bot_user], 'selectingEmoji': False } } self.__web_socket_client.send( WebSocketMessage(label='chat', payload=payload))
def test_welcome(self): self.__data_service.set_welcome_message('what it do nephew') self.__handler.handle( WebSocketMessage( label='update-room', payload={ 'users': [self.__to_spotify_user('1'), self.__to_spotify_user('2')] })) self.__dequeue_and_assert_whispers([]) self.__handler.handle( WebSocketMessage(label='update-room', payload={ 'users': [ self.__to_spotify_user('1'), self.__to_spotify_user('2'), self.__to_spotify_user('3'), self.__to_spotify_user('4') ] })) self.__dequeue_and_assert_whispers( ['@User3 what it do nephew', '@User4 what it do nephew'])
def handle(self, message: WebSocketMessage) -> None: self.__web_socket_client.send( WebSocketMessage(label='stayAwake', payload={'date': message.payload['date']})) self.__web_socket_client.send(WebSocketMessage(2))
def test_no_command_because_malformed(self) -> None: self.__handler.handle(WebSocketMessage( label='push-message', payload=self.__create_push_message('joe', 'fake foo bar') )) self.assertFalse(self.__command_processor.was_called)
def test_no_command_because_sent_from_bot(self) -> None: self.__handler.handle(WebSocketMessage( label='push-message', payload=self.__create_push_message(self.__env.get_spotify_user_id(), '/fake foo bar') )) self.assertFalse(self.__command_processor.was_called)