def setUp(self) -> None:
        self.client = nio.AsyncClient("test.matrix.org",
                                      user="******",
                                      device_id="test_device")
        self.owner = "an_owner"
        self.room_id = "test_room"
        self.matrix_room1 = nio.MatrixRoom(self.owner, self.room_id)
        self.client.rooms = {
            "test_room": self.matrix_room1,
            "other_test_room": "also_empty_room"
        }

        self.subject = "test_room"
        self.title = "A title"
        self.topic = "a_topic"
        self.display_name = "a_display_name"
        self.room1 = matrix_nio.MatrixNioRoom(self.room_id,
                                              client=self.client,
                                              title=self.title,
                                              subject=self.subject)
        self.users = [
            MatrixUser("12345", display_name="Charles de Gaulle"),
            MatrixUser("54321", display_name="Georges Pompidou")
        ]
        self.occupants = [
            matrix_nio.MatrixNioRoomOccupant("12345", "Charles de Gaulle",
                                             self.client),
            matrix_nio.MatrixNioRoomOccupant("54321", "Georges Pompidou",
                                             self.client)
        ]
        self.room1.matrix_room.users = self.users
        self.room1.matrix_room.own_user_id = self.owner
        self.room1.matrix_room.topic = self.topic
        self.room1.matrix_room.name = self.display_name
 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()
 def test_matrix_nio_room_exists(self):
     matrix_client = nio.AsyncClient("test.matrix.org",
                                     user="******",
                                     device_id="test_device")
     nio_room1 = nio.MatrixRoom("nio_room1", "room1_owner")
     nio_room2 = nio.MatrixRoom("nio_room2", "room2_owner")
     matrix_rooms = {"nio_room1": nio_room1, "nio_room2": nio_room2}
     matrix_client.rooms = matrix_rooms
     errbot_nio_room1 = matrix_nio.MatrixNioRoom(
         "nio_room1",
         client=matrix_client,
         title="nio_room1 title",
         subject="nio_room1 subject")
     errbot_nio_room2 = matrix_nio.MatrixNioRoom(
         "nio_room2",
         client=matrix_client,
         title="nio_room2 title",
         subject="nio_room2 subject")
     del matrix_client.rooms[errbot_nio_room2.id]
     result1 = errbot_nio_room1.exists
     result2 = errbot_nio_room2.exists
     self.assertTrue(result1)
     self.assertFalse(result2)
 def test_matrix_nio_room_joined(self):
     matrix_client = nio.AsyncClient("test.matrix.org",
                                     user="******",
                                     device_id="test_device")
     joined_nio_room1 = nio.MatrixRoom("nio_room1", "room1_owner")
     joined_nio_room2 = nio.MatrixRoom("nio_room2", "room2_owner")
     joined_matrix_rooms = {
         "nio_room1": joined_nio_room1,
         "nio_room2": joined_nio_room2
     }
     matrix_client.rooms = joined_matrix_rooms
     errbot_nio_room1 = matrix_nio.MatrixNioRoom(
         "nio_room1",
         client=matrix_client,
         title="nio_room1 title",
         subject="nio_room1 subject")
     errbot_nio_room2 = matrix_nio.MatrixNioRoom(
         "nio_room2",
         client=matrix_client,
         title="nio_room2 title",
         subject="nio_room2 subject")
     matrix_client.joined_rooms = mock.Mock(
         return_value=aiounittest.futurized(
             JoinedRoomsResponse.from_dict(
                 {"joined_rooms": ["nio_room1", "nio_room2"]})))
     # joined = True
     result1 = errbot_nio_room1.joined
     self.assertTrue(result1)
     matrix_client.joined_rooms.assert_called_once()
     matrix_client.joined_rooms = mock.Mock(
         return_value=aiounittest.futurized(
             JoinedRoomsResponse.from_dict({"joined_rooms": ["nio_room1"]
                                            })))
     # joined = false
     result2 = errbot_nio_room2.joined
     self.assertFalse(result2)
     matrix_client.joined_rooms.assert_called_once()
 def test_matrix_nio_room_destroy(self):
     matrix_client = nio.AsyncClient("test.matrix.org",
                                     user="******",
                                     device_id="test_device")
     room1 = nio.MatrixRoom("nio_room1", "room1_owner")
     rooms = {"nio_room1": room1}
     matrix_client.rooms = rooms
     nio_room1 = matrix_nio.MatrixNioRoom("nio_room1",
                                          client=matrix_client,
                                          title="nio_room1 title",
                                          subject="nio_room1 subject")
     matrix_client.room_forget = mock.Mock(
         return_value=aiounittest.futurized(
             RoomForgetResponse.from_dict({}, "nio_room1")))
     nio_room1.destroy()
     matrix_client.room_forget.assert_called_once_with("nio_room1")
 def test_matrix_nio_room_destroy_error(self):
     matrix_client = nio.AsyncClient("test.matrix.org",
                                     user="******",
                                     device_id="test_device")
     room1 = nio.MatrixRoom("nio_room1", "room1_owner")
     rooms = {"nio_room1": room1}
     matrix_client.rooms = rooms
     nio_room1 = matrix_nio.MatrixNioRoom("nio_room1",
                                          client=matrix_client,
                                          title="nio_room1 title",
                                          subject="nio_room1 subject")
     matrix_client.room_forget = mock.Mock(
         return_value=aiounittest.futurized(
             RoomForgetError.from_dict(
                 {
                     "errcode": "ERROR_DESTROYING_ROOM",
                     "error": "Error destroying room",
                     "retry_after_ms": 10000,
                     "soft_logout": "false"
                 }, "nio_room1")))
     with self.assertRaises(ValueError):
         nio_room1.destroy()
     matrix_client.room_forget.assert_called_once_with("nio_room1")
 def test_matrix_nio_joined_room_error(self):
     matrix_client = nio.AsyncClient("test.matrix.org",
                                     user="******",
                                     device_id="test_device")
     joined_nio_room1 = nio.MatrixRoom("nio_room1", "room1_owner")
     joined_matrix_rooms = {"nio_room1": joined_nio_room1}
     matrix_client.rooms = joined_matrix_rooms
     errbot_nio_room1 = matrix_nio.MatrixNioRoom(
         "nio_room1",
         client=matrix_client,
         title="nio_room1 title",
         subject="nio_room1 subject")
     matrix_client.joined_rooms = mock.Mock(
         return_value=aiounittest.futurized(
             JoinedRoomsError.from_dict({
                 "errcode": "ERROR_FETCHING_JOINED_ROOMS",
                 "error": "Error fetching joined rooms",
                 "retry_after_ms": 10000
             })))
     result = None
     with self.assertRaises(ValueError):
         result = errbot_nio_room1.joined
     self.assertIsNone(result)
     matrix_client.joined_rooms.assert_called_once()