def test_kernel_first_login(self): mess = PackMessage.prepare_message('error', error_mode='first_login') expected_line = f"test_connection_1: {mess}" self.chat.engine('logout', 'test_connection_1', '-') self.chat.engine('msgall hello!', 'test_connection_1', '-') line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)
async def logout_engine(self, connection): username = self.sender.get_name_by_connection(connection) if username != 0: message = PackMessage.prepare_message(mode='logout', username=username, version=self.version) await self.sender.send_all(message) await self.sender.logout(connection)
async def login_engine(self, connection, username): if self.sender.login(connection, username) == 0: message = PackMessage.prepare_message(mode='login', username=username, version=self.version) await self.sender.send_all(message) else: await self.send_error(connection, 'user_exist')
def test_kernel_login_username_already_taken(self): mess = PackMessage.prepare_message('error', error_mode='user_exist', username='******') expected_line = f"test_connection_2: {mess}" self.chat.engine('login Dima', 'test_connection_2', '-') line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)
def test_kernel_logout_answer(self): mess = PackMessage.prepare_message('logout', username='******') expected_line = f"test_connection_2: {mess}" self.chat.engine('login Dima2', 'test_connection_2', '-') self.chat.engine('logout', 'test_connection_2', '-') line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)
def test_kernel_userlist(self): mess = PackMessage.prepare_message('info', info_mode='userlist', userlist="['Dima']") expected_line = f'test_connection_1: {mess}' self.chat.engine('userlist', 'test_connection_1', '-') line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)
def test_kernel_send_message_all(self): mess = PackMessage.prepare_message('send_message_all', message='Hello!', username='******') expected_line = f"test_connection_1: {mess}" self.chat.engine('msgall Hello!', 'test_connection_1', '-') line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)
def test_kernel_send_message_not_exist_user(self): mess = PackMessage.prepare_message('error', error_mode='not_found', username='******') expected_line = f"test_connection_1: {mess}" self.chat.engine('msg NoBody hello!', 'test_connection_1', '-') line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)
def test_kernel_bad_request(self): mess = PackMessage.prepare_message('error', error_mode='bad_request', message='empty message') expected_line = f"test_connection_1: {mess}" self.chat.engine('', 'test_connection_1', '-') self.chat.engine('msgall', 'test_connection_1', '-') line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)
async def send_error(self, connection, error_mode, mess=None, username=None): message = PackMessage.prepare_message('error', error_mode=error_mode, message=mess, username=username) await self.sender.send(connection, message)
async def send_message_engine(self, connection, username, message): user = self.sender.get_connection_by_name(username) if user is not None: message = PackMessage.prepare_message( mode='send_message', username=username, message=message, sender=self.sender.get_name_by_connection(connection)) await self.sender.send(user, message) await self.sender.send(connection, message) else: await self.send_error(connection, 'not_found', username=username)
async def send_info(self, connection, info_mode, clear_data, userlist=None, username=None): message = PackMessage.prepare_message('info', info_mode=info_mode, clear_data=clear_data, userlist=userlist, username=username) await self.sender.send(connection, message)
async def send_all_engine(self, connection, message): username = self.sender.get_name_by_connection(connection) message = PackMessage.prepare_message(mode='send_message_all', username=username, message=message) await self.sender.send_all(message)
def test_kernel_login_answer(self): mess = PackMessage.prepare_message('login', username='******') expected_line = f"test_connection_1: {mess}" line = TestServer.TEST_MESSAGE_STATE self.assertEqual(expected_line, line)