def test_matrix_nio_backend_startup_error(self): configuration_copy = copy.deepcopy(self.bot_config) del (configuration_copy.BOT_IDENTITY["email"]) with self.assertRaises(SystemExit): matrix_nio.MatrixNioBackend(configuration_copy) configuration_copy = copy.deepcopy(self.bot_config) del (configuration_copy.BOT_IDENTITY["auth_dict"]) with self.assertRaises(SystemExit): matrix_nio.MatrixNioBackend(configuration_copy) configuration_copy = copy.deepcopy(self.bot_config) del (configuration_copy.BOT_IDENTITY["site"]) with self.assertRaises(SystemExit): matrix_nio.MatrixNioBackend(configuration_copy)
def test_matrix_nio_backend_serve_once_not_logged_in_has_not_synced_error_sync( self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") backend.client.access_token = True user_id = "@example:localhost" login_response = LoginResponse.from_dict({ "user_id": user_id, "device_id": "device_id", "access_token": "12345", }) login_response_mock = mock.Mock( return_value=aiounittest.futurized(login_response)) backend.client.login_raw = login_response_mock sync_mock = mock.Mock(return_value=aiounittest.futurized( ErrorResponse.from_dict({ "errcode": "ERROR_SYNCING", "error": "Error syncing", "retry_after_ms": 10000 }))) backend.client.sync = sync_mock with self.assertRaises(ValueError): backend.serve_once() sync_mock.assert_called_once_with(full_state=True)
async def test_matrix_nio_backend_send_message(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) test_server = "test.matrix.org" test_user = f"@test_user:{test_server}" event_id = "1234567890" room_id = "test_room" backend.client = nio.AsyncClient(test_server, user=test_user, device_id="test_device") backend.client.rooms = { "test_room": "Test Room", "other_test_room": "Test Room" } backend.client.room_send = mock.Mock( return_value=aiounittest.futurized( RoomSendResponse.from_dict({"event_id": event_id}, room_id))) message_text = "Test message" test_message = Message( message_text, matrix_nio.MatrixNioPerson("an_id", client=backend.client, emails=["*****@*****.**"], full_name="")) test_message.to = matrix_nio.MatrixNioRoom("test_room", client=backend.client, title="A title") test_message.to.room = "test_room" result = await backend._send_message(test_message) self.assertIsInstance(result, RoomSendResponse) self.assertEqual(result.room_id, room_id) self.assertEqual(result.event_id, event_id) # TODO: Add assert called once with backend.client.room_send.assert_called_once()
def test_matrix_nio_backend_serve_once_not_logged_in_has_synced(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") backend.has_synced = True user_id = "@example:localhost" login_response = LoginResponse.from_dict({ "user_id": user_id, "device_id": "device_id", "access_token": "12345", }) login_response_mock = mock.Mock( return_value=aiounittest.futurized(login_response)) backend.client.login_raw = login_response_mock test_name = "Test Name" backend.client.get_profile = mock.Mock( return_value=aiounittest.futurized( ProfileGetResponse.from_dict( { "displayname": test_name, "avatar_url": "http://test.org/avatar.png" }))) sync_forever_mock = mock.Mock(return_value=aiounittest.futurized(True)) backend.client.sync_forever = sync_forever_mock backend.serve_once() sync_forever_mock.assert_called_once_with(30000, full_state=True) backend.client.get_profile.assert_called_once_with(user_id) login_response_mock.assert_called_once_with( self.bot_config.BOT_IDENTITY["auth_dict"])
def test_matrix_nio_backend_handle_unsupported_message(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") backend.client.rooms = { "test_room": "Test Room", "other_test_room": "Test Room" } message_body = "Test message" test_message = RoomMessageEmote.from_dict({ "content": { "body": message_body, "msgtype": "m.emote" }, "event_id": "$15163623196QOZxj:localhost", "origin_server_ts": 1516362319505, "room_id": "!SVkFJHzfwvuaIEawgC:localhost", "sender": "@example:localhost", "type": "m.room.message", "unsigned": { "age": 43464955731 }, "user_id": "@example:localhost", "age": 43464955731 }) test_room = nio.MatrixRoom("test_room", "test_user") test_message.to = test_room callback = mock.Mock() ErrBot.callback_message = callback backend.handle_message(test_room, test_message) callback.assert_not_called()
async def test_matrix_nio_backend_send_message_error(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) test_server = "test.matrix.org" test_user = f"@test_user:{test_server}" event_id = "1234567890" room_id = "test_room" backend.client = nio.AsyncClient(test_server, user=test_user, device_id="test_device") backend.client.rooms = { "test_room": "Test Room", "other_test_room": "Test Room" } backend.client.room_send = mock.Mock( return_value=aiounittest.futurized( ErrorResponse.from_dict({ "errcode": "ERROR_SENDING_MESSAGE", "error": "Error sending message", "retry_after_ms": 10000 }))) message_text = "Test message" test_message = Message( message_text, matrix_nio.MatrixNioPerson("an_id", client=backend.client, emails=["*****@*****.**"], full_name="")) test_message.to = matrix_nio.MatrixNioRoom("test_room", client=backend.client, title="A title") test_message.to.room = "test_room" with self.assertRaises(ValueError): result = await backend._send_message(test_message) backend.client.room_send.assert_called_once()
def test_matrix_nio_backend_serve_once_logged_in_has_synced(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") backend.has_synced = True # Needed for ensuring that backend.client.logged_in = True backend.client.access_token = True sync_forever_mock = mock.Mock(return_value=aiounittest.futurized(True)) backend.client.sync_forever = sync_forever_mock backend.serve_once() sync_forever_mock.assert_called_once_with(30000, full_state=True)
def test_matrix_nio_backend_prefix_groupchat_reply(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") full_name = "Charles de Gaulle" person = matrix_nio.MatrixNioPerson("an_id", backend.client, full_name, ["*****@*****.**"]) message = Message("A message") message_body = f"@{person.fullname} {message.body}" backend.prefix_groupchat_reply(message, person) self.assertEqual(message.body, message_body)
def test_matrix_nio_backend_query_empty_room(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") room_id1 = "test_room" room_id2 = "another_test_room" backend.client.rooms = { room_id1: MatrixRoom(room_id1, "owner1"), room_id2: MatrixRoom(room_id2, "owner2") } self.assertEqual(backend.query_room("non_existent_room"), None)
def test_matrix_nio_backend_is_not_from_self(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) test_user_id = "test_user" backend.client = nio.AsyncClient("test.matrix.org", user=test_user_id, device_id="test_device") message_text = "Test message" response_text = "A response" test_message = Message( message_text, matrix_nio.MatrixNioPerson("another_test_user_id", client=backend.client, emails=["*****@*****.**"], full_name="")) self.assertFalse(backend.is_from_self(test_message))
def test_matrix_nio_backend_rooms(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") room_id1 = "test_room" room_id2 = "another_test_room" backend.client.rooms = { room_id1: MatrixRoom(room_id1, "owner1"), room_id2: MatrixRoom(room_id2, "owner2") } result = backend.rooms() result = list(result.keys()) self.assertIn(room_id1, result) self.assertIn(room_id2, result)
def test_matrix_nio_backend_query_room(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") room_id1 = "test_room" room_id2 = "another_test_room" backend.client.rooms = { room_id1: MatrixRoom(room_id1, "owner1"), room_id2: MatrixRoom(room_id2, "owner2") } result_room1 = backend.query_room(room_id1) result_room2 = backend.query_room(room_id2) self.assertEqual(result_room1.id, room_id1) self.assertEqual(result_room2.id, room_id2)
async def test_matrix_nio_backend_build_identifier_error(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") backend.client.get_profile = mock.Mock( return_value=aiounittest.futurized( ProfileGetError.from_dict({ "errcode": "ERROR_GETTING_PROFILE", "error": "Error fetching profile", "retry_after_ms": 10000 }))) test_id = "test_id" with self.assertRaises(ValueError): test_identifier = await asyncio.gather( backend.build_identifier(test_id))
def test_matrix_nio_backend_build_reply(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") message_text = "Test message" response_text = "A response" test_message = Message( message_text, matrix_nio.MatrixNioPerson("an_id", client=backend.client, emails=["*****@*****.**"], full_name="")) response = backend.build_reply(test_message, response_text) self.assertIsInstance(response, Message) self.assertEqual(response.to, test_message.frm) self.assertEqual(response.body, f"{message_text}\n{response_text}")
def test_matrix_nio_backend_serve_once_login_error(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") user_id = "@example:localhost" login_response = LoginError.from_dict({ "errcode": "ERROR_LOGGING_IN", "error": "Error logging in", "retry_after_ms": 10000 }) login_raw_mock = mock.Mock( return_value=aiounittest.futurized(login_response)) backend.client.login_raw = login_raw_mock with self.assertRaises(ValueError): backend.serve_once() login_raw_mock.assert_called_once_with( self.bot_config.BOT_IDENTITY["auth_dict"])
def test_matrix_nio_backend_serve_once_logged_in_has_not_synced(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") # Needed for ensuring that backend.client.logged_in = True backend.client.access_token = True # Needed since path may be tricky to get with open(os.path.join(os.path.dirname(__file__), "sync.json")) as json_file: data = json.loads(json_file.read()) sync_mock = mock.Mock( return_value=aiounittest.futurized(SyncResponse.from_dict(data))) backend.client.sync = sync_mock backend.serve_once() self.assertTrue(backend.has_synced) self.assertEqual(backend.client.next_batch, data["next_batch"]) sync_mock.assert_called_once_with(full_state=True)
async def test_matrix_nio_backend_build_identifier(self): backend = matrix_nio.MatrixNioBackend(self.bot_config) backend.client = nio.AsyncClient("test.matrix.org", user="******", device_id="test_device") test_name = "Test Name" backend.client.get_profile = mock.Mock( return_value=aiounittest.futurized( ProfileGetResponse.from_dict( { "displayname": test_name, "avatar_url": "http://test.org/avatar.png" }))) test_id = "test_id" test_identifier = await asyncio.gather( backend.build_identifier(test_id)) test_identifier = test_identifier[0] self.assertIsInstance(test_identifier, matrix_nio.MatrixNioPerson) self.assertEqual(test_identifier.id, test_id) self.assertEqual(test_identifier.fullname, test_name)
def test_matrix_nio_backend(self): test_backend = matrix_nio.MatrixNioBackend(self.bot_config) self.assertIsInstance(test_backend.client, nio.Client) self.assertIsInstance(test_backend.client.config, nio.AsyncClientConfig)
def test_matrix_nio_backend_mode(self): mode = matrix_nio.MatrixNioBackend(self.bot_config).mode self.assertEqual(mode, "matrix-nio")