def test_dict_to_json(): d = {"nested": {"a": "a", "b": 1}, "field": False} as_json = my_json.to_json(d) d_result = my_json.from_json(as_json) assert d == d_result
async def request_last_messages(self): json_message = { JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_VALUE: -1, JsonFields.MESSAGE_SENDER: 'sender1', JsonFields.MESSAGE_DESTINATION: 'room1' } await self.ws.send(to_json(json_message))
async def send_wrong_message(self, message): json_mess = { JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: message, JsonFields.MESSAGE_SENDER: self.user_name } self.sent_messages.append(message) await self.ws.send(to_json(json_mess))
async def send(self, message): # TODO hardcoded data, even if the function is used also for previous messages type json_mess = { JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: message, JsonFields.MESSAGE_DESTINATION: self.chat_name, JsonFields.MESSAGE_SENDER: self.user_name } self.sent_messages.append(message) json = to_json(json_mess) await self.ws.send(json)
async def send_new_user_notification(self, user_name_list: list, receivers_logins: list, chat_name: str): self.log.info('sending new user notification') json_message = { JsonFields.MESSAGE_TYPE: MessageTypes.USERS_UPDATE, JsonFields.MESSAGE_VALUE: user_name_list, JsonFields.MESSAGE_DESTINATION: receivers_logins } for participant_login, participant_ws in self.chat_participants.get( chat_name, {}).items(): self.log.info( f'sending notification to {participant_login}; json: {json_message}' ) self.log.info( f'participant_login: {participant_login}, receivers_logins: {receivers_logins}' ) if participant_login in receivers_logins: await participant_ws.send(my_json.to_json(json_message))
async def receive(self, websocket: websockets.WebSocketServerProtocol, path: str): data, chat_name, login = '', '', '' try: self.log.info(f'{websocket} - waiting for messages') self.log.info(f'{websocket} - PATH {path}') if not helper_functions.check_url(path): await websocket.close(code=4000, reason='wrong login or chat name') self.log.info(f'websocket closed, wrong login or chat name') return path_items = path.split('/') login, chat_name = path_items[-1], path_items[-2] await self.join_chat(login, chat_name, websocket) self.log.info(f'{websocket} - {login} joined chat {chat_name}') async for data in websocket: try: message = my_json.from_json(data) self.log.info(f'{websocket} - raw data received: {data}') self.log.info(f'{websocket} - message: {message}') if message[JsonFields.MESSAGE_TYPE] in [ MessageTypes.PREVIOUS_MESSAGES, MessageTypes.MORE_PREVIOUS_MESSAGES ]: self.log.info( f'{websocket} - received message type: {message[JsonFields.MESSAGE_TYPE]}' ) if helper_functions.check_previous_messages_json( data ) or helper_functions.check_more_previous_messages_json( data): self.log.info( f'{websocket} - previous messages json correct' ) chat = message[JsonFields.MESSAGE_DESTINATION] msg_id = message[JsonFields.MESSAGE_VALUE] participants = list( self.chat_participants.get(chat, {}).keys()) past_messages = self.chatbox_database.fetch_last_messages( chat, start_from_id=msg_id) self.log.info( f'PAST OR MORE PAST MESSAGES: {past_messages}') json_message = { JsonFields.MESSAGE_TYPE: message[JsonFields.MESSAGE_TYPE], JsonFields.MULTIPLE_MESSAGES: [m.__dict__ for m in past_messages] } await websocket.send(my_json.to_json(json_message)) self.log.info(f'{websocket} - past messages sent') # todo, probably to remove, users are notified before, just after joining await self.send_new_user_notification( participants, [login], chat) elif message[ JsonFields.MESSAGE_TYPE] == MessageTypes.MESSAGE: if helper_functions.check_message_json(data): self.log.info( f'{websocket} - messages json correct') message_details = self.update_and_save_message( message, chat_name, login, websocket) self.log.info( f'message after hyperlink control: {message_details}' ) json_message = { JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: message_details.__dict__ } destination_chat_participants = self.chat_participants.get( message[JsonFields.MESSAGE_DESTINATION], {}) for participant_sock in destination_chat_participants.values( ): self.log.info( f'{websocket} - sending message to {participant_sock} in {message_details.chat_name}' ) await participant_sock.send( my_json.to_json(json_message)) else: self.log.info( f'server received an incorrect json: {data}') # todo add test, when database connection is down, message is sent anyway # (that is why this must be after sending) except KeyError: self.log.error( f'{websocket} - KeyError; improper json: {data}') except Exception as e: self.log.exception(f"{websocket} - Unexpected error: {e}") # todo answer with an error json except Exception as e: self.log.exception(f"{websocket} - Unexpected error 2: {e}") if chat_name and login: self.remove_from_chat(chat_name, login) await self.send_new_user_notification( [login], list(self.chat_participants[chat_name].keys()), chat_name) self.log.info(f'{websocket} - {login} left chat {chat_name}') if self.get_num_of_chat_participants(chat_name) == 0: self.remove_chat(chat_name) self.log.info(f'{chat_name} removed from the dictionary') self.log.info( f'remaining chat rooms: {self.chat_participants.keys()}')
def test_check_previous_messages_json(): message1 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_VALUE: -1, JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message2 = {JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message3 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_DESTINATION: 'chat name'} message4 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_SENDER: 'user name'} message5 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_SENDER: '', JsonFields.MESSAGE_DESTINATION: 'chat name'} message6 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: ''} message7 = {'MESSAGE_TYPE': MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message8 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message9 = {JsonFields.MESSAGE_TYPE: '', JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message10 = {JsonFields.MESSAGE_TYPE: 'PREVIOUS_MESSAGES', JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message11 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, 'MESSAGE_SENDER': 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message12 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_SENDER: 'user name', 'MESSAGE_DESTINATION': 'chat name'} message13 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message14 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_VALUE: '10', JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} message15 = {JsonFields.MESSAGE_TYPE: MessageTypes.PREVIOUS_MESSAGES, JsonFields.MESSAGE_VALUE: '-1', JsonFields.MESSAGE_SENDER: 'user name', JsonFields.MESSAGE_DESTINATION: 'chat name'} assert check_previous_messages_json(to_json(message1)) assert not check_previous_messages_json(to_json(message2)) assert not check_previous_messages_json(to_json(message3)) assert not check_previous_messages_json(to_json(message4)) assert not check_previous_messages_json(to_json(message5)) assert not check_previous_messages_json(to_json(message6)) assert not check_previous_messages_json(to_json(message7)) assert not check_previous_messages_json(to_json(message8)) assert not check_previous_messages_json(to_json(message9)) assert not check_previous_messages_json(to_json(message10)) assert not check_previous_messages_json(to_json(message11)) assert not check_previous_messages_json(to_json(message12)) assert not check_previous_messages_json(to_json(message13)) assert not check_previous_messages_json(to_json(message14)) assert not check_previous_messages_json(to_json(message15))
def test_check_message_json(): message1 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name' } message2 = {JsonFields.MESSAGE_TYPE: 'MESSAGE', JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} message3 = {JsonFields.MESSAGE_TYPE: '', JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} message4 = {JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} message5 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: '', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} message6 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} message7 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: '', JsonFields.MESSAGE_SENDER: 'user name'} message8 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_SENDER: 'user name'} message9 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: ''} message10 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room'} message11 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name', 'another field': 'some value'} message12 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, 'MESSAGE_VALUE': 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} message13 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', 'MESSAGE_DESTINATION': 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} message14 = {JsonFields.MESSAGE_TYPE: MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', 'MESSAGE_SENDER': 'user name'} message15 = {'MESSAGE_TYPE': MessageTypes.MESSAGE, JsonFields.MESSAGE_VALUE: 'this is a message', JsonFields.MESSAGE_DESTINATION: 'chat room', JsonFields.MESSAGE_SENDER: 'user name'} assert check_message_json(to_json(message1)) assert check_message_json(to_json(message11)) assert not check_message_json(to_json(message2)) assert not check_message_json(to_json(message3)) assert not check_message_json(to_json(message4)) assert not check_message_json(to_json(message5)) assert not check_message_json(to_json(message6)) assert not check_message_json(to_json(message7)) assert not check_message_json(to_json(message8)) assert not check_message_json(to_json(message9)) assert not check_message_json(to_json(message10)) assert not check_message_json(to_json(message12)) assert not check_message_json(to_json(message13)) assert not check_message_json(to_json(message14)) assert not check_message_json(to_json(message15))