Ejemplo n.º 1
0
    def test_login(self, enter_room):
        """ Test login in a room """

        with self.assertRaises(exceptions.Forbidden):
            self.resource.login("name", "pass")

        enter_room.assert_not_called()

        room = RoomFactory(name="Room 1")
        enter_room.return_value = room

        self.assertEqual(self.resource.login("Room 1"), room)
        enter_room.assert_called_with(room)

        enter_room.reset_mock()

        password = hashlib.sha256("pass".encode('utf-8')).hexdigest()
        room = RoomFactory(name="Room 2", password=password)
        enter_room.return_value = room

        with self.assertRaises(exceptions.Forbidden):
            self.resource.login("Room 2", password="******")

        self.assertEqual(self.resource.login("Room 2", password="******"), room)
        enter_room.assert_called_with(room)
Ejemplo n.º 2
0
    def test_available_rooms(self):
        """ Test getting the available rooms for a given user """

        self.assertEqual(models.Room.available_rooms(self.session).count(), 0)

        room = RoomFactory(hidden=False, name="Room classic")
        room_hidden = RoomFactory(hidden=True, name="Room hidden")
        user = UserFactory(rank=5)

        rooms = list(models.Room.available_rooms(self.session, user))

        self.assertEqual(len(rooms), 2)
        self.assertIn(room, rooms)
        self.assertIn(room_hidden, rooms)

        user = UserFactory()
        rooms = list(models.Room.available_rooms(self.session, user))
        self.assertEqual(len(rooms), 1)
        self.assertIn(room, rooms)
        self.assertNotIn(room_hidden, rooms)

        user2 = user_with_room_privilege(room=room_hidden, level=2)
        rooms = list(models.Room.available_rooms(self.session, user2))
        self.assertEqual(len(rooms), 2)
        self.assertIn(room, rooms)
        self.assertIn(room_hidden, rooms)
Ejemplo n.º 3
0
    def test_enter_room(self, del_from_room, add_to_room):
        """ Test enter in a room """

        room = RoomFactory()
        user = UserFactory(connection=self.connection, online=True)

        self.resource.enter(room)

        self.assertEqual(self.connection.room, room)
        self.assertEqual(user.room, room)
        del_from_room.assert_not_called()
        add_to_room.assert_called_with(self.token, room.id)

        add_to_room.reset_mock()

        # Change of room
        room2 = RoomFactory()
        self.resource.enter(room2)

        self.assertEqual(self.connection.room, room2)
        self.assertEqual(user.room, room2)
        del_from_room.assert_called_with(self.token, room.id)
        add_to_room.assert_called_with(self.token, room2.id)

        # User reenter in the same room
        self.resource.enter(room2)

        self.assertEqual(self.connection.room, room2)
        self.assertEqual(user.room, room2)
        del_from_room.assert_called_with(self.token, room2.id)
        add_to_room.assert_called_with(self.token, room2.id)
Ejemplo n.º 4
0
    def test_enter_room_unauthorized(self, del_from_room, add_to_room):
        """ Test enter in a room """

        room = RoomFactory()
        # No user unauthorized
        with self.assertRaises(exceptions.Forbidden):
            self.resource.enter(room)

        user_with_room_privilege(room=room,
                                 level=0,
                                 connection=self.connection,
                                 online=True)

        # User with insufficient right
        with self.assertRaises(exceptions.Forbidden):
            self.resource.enter(room)

        #Room full
        room = RoomFactory()
        room.max_users = 2
        UserFactory(room=room, online=True)
        UserFactory(room=room, online=True)
        with self.assertRaises(exceptions.Unauthorized):
            self.resource.enter(room)

        del_from_room.assert_not_called()
        add_to_room.assert_not_called()
Ejemplo n.º 5
0
    def test_login(self):
        """ Test login in a room """

        self.assertIsNone(
            models.Room.login("name", "pass", self.session)
        )

        # Room without password
        room = RoomFactory(name="Room 1")

        self.assertEqual(
            models.Room.login("Room 1", None, self.session),
            room
        )
        self.assertEqual(
            models.Room.login("Room 1", "password", self.session),
            room
        )

        # Room with password
        password = hashlib.sha256("pass".encode('utf-8')).hexdigest()
        room = RoomFactory(name="Room 2", password=password)

        self.assertIsNone(
            models.Room.login("Room 2", "Wrong password", self.session)
        )

        self.assertEqual(
            models.Room.login("Room 2", "pass", self.session),
            room
        )
Ejemplo n.º 6
0
    def test_is_full(self):
        """ Test checking if the room is full """

        room = RoomFactory(max_users=2)
        self.assertEqual(room.is_full(), False)

        UserFactory(room=room, online=True)
        UserFactory(room=room, online=True)
        self.session.commit()
        self.assertEqual(room.is_full(), True)
Ejemplo n.º 7
0
    def test_set_op(self):
        """ Test to OP a user in a room """

        room = RoomFactory()

        self.connection.room = room
        user_with_room_privilege(level=7,
                                 connection=self.connection,
                                 online=True,
                                 room=room)

        target_user_admin = user_with_room_privilege(level=10,
                                                     online=True,
                                                     room=room)
        target_user = user_with_room_privilege(level=1, online=True, room=room)
        target_user2 = user_with_room_privilege(level=6,
                                                online=True,
                                                room=room)

        ret = self.chat_command(self.resource, "invalid_user")
        self.assertEqual(len(ret), 1)
        self.assertRegex(ret[0], "Unknown user")

        ret = self.chat_command(self.resource, target_user_admin.name)
        self.assertEqual(len(ret), 1)
        self.assertRegex(ret[0], "Not authorize")
        self.assertEqual(target_user_admin.level(room.id), 10)

        ret = self.chat_command(self.resource, target_user.name)
        self.assertIsNone(ret)
        self.assertEqual(target_user.level(room.id), 5)

        ret = self.chat_command(self.resource, target_user2.name)
        self.assertIsNone(ret)
        self.assertEqual(target_user2.level(room.id), 5)
Ejemplo n.º 8
0
    def test_handle_room_message(self, sendconnection, sendroom):
        """ Test handling an incomming message """

        room = RoomFactory(name="room_name")
        connection = ConnectionFactory()
        user = UserFactory(connection=connection, online=True, room=room)

        self.worker.handle(
            {
                "target": {
                    "type": "room",
                    "value": room.id
                },
                "message": "message",
                "source": connection.token
            },
            token=connection.token,
            session=self.session,
        )

        sendconnection.assert_not_called()
        sendroom.assert_called_once()
        self.assertEqual(sendroom.call_args[0][0], room.id)
        packet = sendroom.call_args[0][1]

        self.assertIsInstance(packet, smpacket.SMPacketServerNSCCM)
        self.assertRegex(packet["message"], "room_name")
        self.assertRegex(packet["message"], "message")
        self.assertRegex(packet["message"], user.name)
Ejemplo n.º 9
0
    def test_kick_with_room(self, disconnect_user):
        """ Test banning a user from a room """

        room = RoomFactory()
        self.connection.room = room

        user_with_room_privilege(
            connection=self.connection,
            online=True,
            room=room,
            level=self.chat_command.permission.value,
        )
        target_connection = ConnectionFactory(room=room)
        target_user = user_with_room_privilege(
            room=room,
            online=True,
            connection=target_connection,
        )

        self.chat_command(self.resource, target_user.name)
        self.assertFalse(
            models.Ban.is_ban(self.session, user_id=target_user.id))
        self.assertFalse(
            models.Ban.is_ban(self.session,
                              user_id=target_user.id,
                              room_id=room.id))

        self.assertIsNone(target_user.room)
        self.assertIsNone(target_connection.room)

        disconnect_user.assert_not_called()
Ejemplo n.º 10
0
    def test_by_name(self):
        """ Test getting a room by it's name"""

        room = RoomFactory()

        self.assertEqual(models.Room.by_name(room.name, self.session), room)

        self.assertIsNone(models.Room.by_name("invalid room", self.session))
Ejemplo n.º 11
0
    def test_by_id(self):
        """ Test getting a room by it's id"""

        room = RoomFactory()

        self.assertEqual(models.Room.by_id(room.id, self.session), room)

        self.assertIsNone(models.Room.by_id("invalid id", self.session))
Ejemplo n.º 12
0
    def test_get_room(self):
        """ Test getting a room given his ID """

        with self.assertRaises(exceptions.NotFound):
            self.resource.get(4)

        room = RoomFactory()
        self.assertEqual(room, self.resource.get(room.id))
Ejemplo n.º 13
0
    def test_room(self):
        """ Test getting the room associated with a connection """

        self.assertEqual(self.controller.room, None)
        room = RoomFactory()
        self.connection.room = room
        self.session.commit()
        self.assertEqual(self.controller.room, room)
Ejemplo n.º 14
0
    def test_nb_players(self):
        """ Test getting the number of user in the room """

        room = RoomFactory()

        self.assertEqual(room.nb_players, 0)
        UserFactory(room=room, online=True)
        self.session.commit()

        self.assertEqual(room.nb_players, 1)
Ejemplo n.º 15
0
    def test_change_description(self):
        """ Test to change the description in a room """

        room = RoomFactory()

        self.connection.room = room
        user_with_room_privilege(level=5,
                                 connection=self.connection,
                                 online=True,
                                 room=room)

        ret = self.chat_command(self.resource, "New description")
        self.assertIsNone(ret)
        self.assertEqual(room.description, "New description")
Ejemplo n.º 16
0
    def test_leave_room(self, del_from_room):
        """ Test enter in a room """

        room = self.resource.leave()
        self.assertIsNone(room)
        del_from_room.assert_not_called()

        room = RoomFactory()
        user = UserFactory(connection=self.connection, online=True, room=room)
        self.connection.room = room

        self.assertEqual(self.resource.leave(), room)
        self.assertIsNone(user.room)
        self.assertIsNone(self.connection.room)
Ejemplo n.º 17
0
    def test_enter_room_unauthorized(self, del_from_room, add_to_room):
        """ Test enter in a room """

        room = RoomFactory()
        # No user unauthorized
        with self.assertRaises(exceptions.Forbidden):
            self.resource.enter(room)

        user_with_room_privilege(room=room, level=0, connection=self.connection, online=True)

        # User with insufficient right
        with self.assertRaises(exceptions.Forbidden):
            self.resource.enter(room)

        #Room full
        room = RoomFactory()
        room.max_users = 2
        UserFactory(room=room, online=True)
        UserFactory(room=room, online=True)
        with self.assertRaises(exceptions.Unauthorized):
            self.resource.enter(room)

        del_from_room.assert_not_called()
        add_to_room.assert_not_called()
Ejemplo n.º 18
0
    def test_list(self, available_rooms):
        """ Test listing the rooms """

        self.assertEqual(list(self.resource.list()), [])
        available_rooms.assert_not_called()

        room = RoomFactory()
        user = UserFactory(connection=self.connection, online=True)

        available_rooms.return_value = [room]

        rooms = self.resource.list()
        self.assertEqual(rooms, [room])

        available_rooms.assert_called_with(session=self.session, users=[user])
Ejemplo n.º 19
0
    def test_moderators(self):
        """ Test getting the list of moderators in the room """

        room = RoomFactory()
        user_with_room_privilege(level=1, room=room)
        self.assertEqual(list(room.moderators), [])

        mod1 = user_with_room_privilege(level=6, room=room)
        mod2 = user_with_room_privilege(level=10, room=room)

        moderators = list(room.moderators)

        self.assertEqual(len(moderators), 2)
        self.assertIn(mod1, moderators)
        self.assertIn(mod2, moderators)
Ejemplo n.º 20
0
    def test_online_users(self):
        """ Test getting the online users in the room """

        room = RoomFactory()

        self.assertEqual(list(room.online_users), [])
        user1 = UserFactory(room=room, online=True)
        user2 = UserFactory(room=room, online=True)
        self.session.commit()

        online_users = list(room.online_users)

        self.assertEqual(len(online_users), 2)
        self.assertIn(user1, online_users)
        self.assertIn(user2, online_users)
Ejemplo n.º 21
0
    def test_leave(self):
        """ Test to leave a room """

        room = RoomFactory()
        self.connection.room = room

        user_with_room_privilege(
            level=1,
            connection=self.connection,
            online=True,
            room=room,
        )

        ret = self.chat_command(self.resource, None)
        self.assertIsNone(ret)
        self.assertEqual(self.connection.room, None)
Ejemplo n.º 22
0
    def test_delete(self):
        """ Test to delete of a room """

        room = RoomFactory()
        self.connection.room = room

        user_with_room_privilege(
            level=10,
            connection=self.connection,
            online=True,
            room=room,
        )

        ret = self.chat_command(self.resource, None)
        self.assertIsNone(ret)
        self.assertEqual(self.connection.room, None)
        self.assertIsNone(self.session.query(models.Room).get(room.id))
Ejemplo n.º 23
0
    def test_delete(self):
        """ Test delete a room """

        room = RoomFactory()

        with self.assertRaises(exceptions.Forbidden):
            self.resource.delete(room.id)

        user_with_room_privilege(room=room,
                                 level=10,
                                 online=True,
                                 connection=self.connection)
        self.connection.room = room

        self.assertEqual(self.session.query(models.Room).get(room.id), room)
        self.assertTrue(self.resource.delete(room.id))
        self.assertIsNone(self.session.query(models.Room).get(room.id))
        self.assertIsNone(self.connection.room)
Ejemplo n.º 24
0
    def test_send_message_room(self, sendroom):
        """ Test sending a message to a specific room """

        self.worker.send_message_room("bla", None)

        self.assertLog("WARNING")
        sendroom.assert_not_called()

        room = RoomFactory(name="room_name")
        self.worker.send_message_room("bla", room)

        sendroom.assert_called_once()
        self.assertEqual(sendroom.call_args[0][0], room.id)
        packet = sendroom.call_args[0][1]

        self.assertIsInstance(packet, smpacket.SMPacketServerNSCCM)
        self.assertRegex(packet["message"], "room_name")
        self.assertRegex(packet["message"], "bla")
    def test_send_room_message(self, send_message):
        """ Test sending a message """

        room = RoomFactory(name="machin")
        UserFactory(online=True, connection=self.connection, room=room)
        self.connection.room = room

        self.resource.send("bla")
        send_message.assert_called_once()

        msg = send_message.call_args[0][0]

        self.assertEqual(msg.kind, event.EventKind.chat_message)
        self.assertEqual(msg.token, self.token)
        self.assertEqual(msg.room_id, room.id)

        self.assertEqual(msg.data["message"], "bla")
        self.assertEqual(msg.data["target"]["value"], room.id)
        self.assertEqual(msg.data["target"]["type"], "room")
Ejemplo n.º 26
0
    def test_change_max_users(self):
        """ Test to change the nb max of users in a room """

        room = RoomFactory()

        self.connection.room = room
        user_with_room_privilege(
            level=5,
            connection=self.connection,
            online=True,
            room=room
        )

        ret = self.chat_command(self.resource, "invalid_number")
        self.assertEqual(len(ret), 1)
        self.assertRegex(ret[0], "Invalid number")

        ret = self.chat_command(self.resource, "54")
        self.assertIsNone(ret)
        self.assertEqual(room.max_users, 54)
Ejemplo n.º 27
0
    def test_change_free(self):
        """ Test to change the free of a room """

        room = RoomFactory(free=False)

        self.connection.room = room
        user_with_room_privilege(
            level=5,
            connection=self.connection,
            online=True,
            room=room,
        )

        ret = self.chat_command(self.resource, None)
        self.assertIsNone(ret)
        self.assertEqual(room.free, True)

        ret = self.chat_command(self.resource, None)
        self.assertIsNone(ret)
        self.assertEqual(room.free, False)
Ejemplo n.º 28
0
    def test_list_user_with_room(self):
        """ Test displaying the list of users """

        room = RoomFactory(max_users=42)
        self.connection.room = room

        user1 = UserFactory(online=True, room=room)
        UserFactory(online=False)
        UserFactory(online=True)
        user4 = UserFactory(online=True, room=room)

        res = self.chat_command(self.resource, "bla")
        self.assertEqual(len(res), 3)
        self.assertEqual(res[0], "2/42 players online")
        self.assertRegex(res[1], user1.name)
        self.assertRegex(res[2], user4.name)

        res = self.chat_command(self.resource, "bla", limit=1)
        self.assertEqual(len(res), 2)
        self.assertEqual(res[0], "2/42 players online")
        self.assertRegex(res[1], user1.name)
Ejemplo n.º 29
0
    def test_unban_with_room(self):
        """ Test banning a user from a room """

        room = RoomFactory()
        self.connection.room = room

        UserFactory(online=True, connection=self.connection, room=room)

        target_user = UserFactory(online=True)

        models.Ban.ban(self.session, user_id=target_user.id, room_id=room.id)
        self.assertTrue(
            models.Ban.is_ban(self.session,
                              user_id=target_user.id,
                              room_id=room.id))

        res = self.chat_command(self.resource, target_user.name)
        self.assertFalse(
            models.Ban.is_ban(self.session,
                              user_id=target_user.id,
                              room_id=room.id))
        self.assertIsNone(res)
Ejemplo n.º 30
0
    def test_find_ban(self):
        """ Test to find a ban """

        ban = BanFactory(ip="8.8.8.8")
        self.assertEqual(
            models.Ban.find_ban(session=self.session, ip="8.8.8.8"), ban)

        ban.end_at = datetime.datetime.utcnow() + datetime.timedelta(days=1)
        self.assertEqual(
            models.Ban.find_ban(session=self.session, ip="8.8.8.8"), ban)

        ban.end_at = datetime.datetime.utcnow()
        self.assertIsNone(
            models.Ban.find_ban(session=self.session, ip="8.8.8.8"))

        room = RoomFactory()
        ban.room = room
        ban.end_at = None

        self.assertIsNone(
            models.Ban.find_ban(session=self.session, ip="8.8.8.8"))
        self.assertEqual(
            models.Ban.find_ban(session=self.session,
                                ip="8.8.8.8",
                                room_id=room.id), ban)

        user = UserFactory()

        ban = BanFactory(user=user)
        self.assertEqual(
            models.Ban.find_ban(session=self.session, user_id=user.id), ban)
        ban.room = room

        self.assertIsNone(
            models.Ban.find_ban(session=self.session, user_id=user.id))
        self.assertEqual(
            models.Ban.find_ban(session=self.session,
                                user_id=user.id,
                                room_id=room.id), ban)