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
def test_json_to_dict(): # GIVEN json = '{"field1": "value1", "field2": 11}' expected_dict = {"field2": 11, "field1": "value1"} # WHEN result = my_json.from_json(json) # THEN assert expected_dict == result
def check_previous_messages_json(json_message: str) -> bool: dict_message = from_json(json_message) if (JsonFields.MESSAGE_TYPE in dict_message and JsonFields.MESSAGE_DESTINATION in dict_message and JsonFields.MESSAGE_VALUE in dict_message and JsonFields.MESSAGE_SENDER in dict_message): if (dict_message[JsonFields.MESSAGE_TYPE] == MessageTypes.PREVIOUS_MESSAGES and dict_message[JsonFields.MESSAGE_VALUE] == -1 and dict_message[JsonFields.MESSAGE_DESTINATION] != '' and dict_message[JsonFields.MESSAGE_SENDER] != ''): return True return False
async def start_receiving(self): async for data in self.ws: msg = from_json(data) if msg[JsonFields.MESSAGE_TYPE] == MessageTypes.MESSAGE: self.received_messages.append( msg[JsonFields.MESSAGE_VALUE]['message']) self.received_jsons.append(msg) elif msg[ JsonFields.MESSAGE_TYPE] == MessageTypes.PREVIOUS_MESSAGES: self.received_messages.extend( m['message'] for m in msg[JsonFields.MULTIPLE_MESSAGES]) self.received_jsons.append(msg)
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_wrong_json_to_dict(): json = '{"fiels"' expected_dict = {} result = my_json.from_json(json) assert expected_dict == result