Esempio n. 1
0
    def test_kick_without_room(self, disconnect_user):
        """ Test banning a user without room """

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

        disconnect_user.assert_not_called()

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

        target_user = UserFactory(online=True)

        res = self.chat_command(self.resource, target_user.name)
        self.assertEqual(len(res), 1)
        self.assertRegex(res[0], "Not authorize")
        disconnect_user.assert_not_called()

        user.rank = self.chat_command.permission.value
        res = self.chat_command(self.resource, target_user.name)

        self.assertFalse(
            models.Ban.is_ban(self.session, user_id=target_user.id))
        disconnect_user.assert_called_with(target_user.id)

        self.assertIsNone(res)
Esempio n. 2
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()
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
0
    def test_create_with_password(self):
        """ Test create a room with a password """

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

        room = self.resource.create("Room 1", password="******")

        password = hashlib.sha256("éàç".encode('utf-8')).hexdigest()

        self.assertIsNotNone(room)
        self.assertEqual(room.password, password)
        self.assertEqual(room.name, "Room 1")
Esempio n. 6
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)
    def test_connection(self):
        """ Test user connection """

        user = UserFactory(online=True)
        with self.assertRaises(exceptions.Unauthorized):
            self.resource.connect(user, pos=0)

        user = UserFactory()
        self.server.config.server["max_users"] = -1

        self.assertEqual(self.resource.connect(user, pos=1), user)
        self.assertEqual(user.pos, 1)
        self.assertTrue(user.online)
        self.assertEqual(user.connection, self.connection)
        self.assertIsNone(user.room)

        self.resource.connect(user, pos=0)
        self.assertEqual(user.pos, 0)
        self.assertTrue(user.online)
        self.assertEqual(user.connection, self.connection)
        self.assertIsNone(user.room)

        user2 = UserFactory()

        self.server.config.server["max_users"] = 1
        with self.assertRaises(exceptions.Unauthorized):
            self.resource.connect(user, pos=1)

        self.server.config.server["max_users"] = -1

        # User2 connect in a new position
        self.resource.connect(user2, pos=1)
        self.assertEqual(user2.pos, 1)
        self.assertTrue(user2.online)
        self.assertEqual(user2.connection, self.connection)
        self.assertIsNone(user2.room)
        self.assertEqual(len(self.connection.active_users), 2)

        # User2 connect at the user1 position
        self.resource.connect(user2, pos=0)
        self.assertEqual(user2.pos, 0)
        self.assertTrue(user2.online)
        self.assertFalse(user.online)
        self.assertEqual(user2.connection, self.connection)
        self.assertIsNone(user2.room)

        self.assertEqual(len(self.connection.users), 2)
        self.assertEqual(len(self.connection.active_users), 1)
    def test_login(self):
        """ Test login an user """

        user = UserFactory(name="User 1",
                           password=hashlib.sha256(
                               "Password".encode('utf-8')).hexdigest())

        # Short password
        with self.assertRaises(exceptions.Forbidden):
            self.resource.login("User 1", "pass")

        # Wrong password
        with self.assertRaises(exceptions.Forbidden):
            self.resource.login("User 1", "Wrong password")

        # Wrong user
        with self.assertRaises(exceptions.Forbidden):
            self.resource.login("User 2", "Password")

        self.assertEqual(user, self.resource.login("User 1", "Password"))

        models.Ban.ban(session=self.session, user_id=user.id)
        # User Ban
        with self.assertRaises(exceptions.Forbidden):
            self.resource.login("User 1", "Password")
    def test_login_or_create(self):
        """ Test login or create an User"""

        password = hashlib.sha256("Password".encode('utf-8')).hexdigest()

        user = UserFactory(name="User 1", password=password)

        # Short password
        with self.assertRaises(exceptions.Forbidden):
            self.resource.login_or_create("User 1", "pass")

        # Wrong password
        with self.assertRaises(exceptions.Forbidden):
            self.resource.login_or_create("User 1", "Wrong password")

        # Login
        self.assertEqual(user,
                         self.resource.login_or_create("User 1", "Password"))

        # Create
        user2 = self.resource.login_or_create("User 2", "Password")
        self.assertEqual(
            user2,
            self.session.query(models.User).filter_by(name="User 2").first())
        self.assertEqual(user2.password, password)
        self.assertEqual(user2.rank, 1)
    def test_active_users(self):
        """ Test getting the active users in a connection """
        conn = ConnectionFactory()
        self.assertEqual(conn.active_users, [])

        user1 = UserFactory(connection=conn, online=True)
        user2 = UserFactory(connection=conn, online=False)

        self.assertEqual(conn.active_users, [user1])

        user3 = UserFactory(connection=conn, online=True)

        self.assertEqual(len(conn.active_users), 2)
        self.assertIn(user1, conn.active_users)
        self.assertIn(user3, conn.active_users)
        self.assertNotIn(user2, conn.active_users)
    def test_send_message_without_target(self, send_message):
        """ Test sending a message without target """

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

        self.resource.send("bla")
        send_message.assert_not_called()
Esempio n. 12
0
    def test_handle_user_message(self, sendconnection, sendroom):
        """ Test handling an incomming message """

        connection = ConnectionFactory()
        user = UserFactory(connection=connection, online=True)

        connection_target = ConnectionFactory()

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

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

        self.assertIsInstance(packet, smpacket.SMPacketServerNSCCM)
        self.assertRegex(packet["message"], "message")
        self.assertRegex(packet["message"], user.name)
Esempio n. 13
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)
Esempio n. 14
0
    def test_list_user_without_room(self):
        """ Test displaying the list of users """

        user1 = UserFactory(online=True)
        UserFactory(online=False)
        user3 = UserFactory(online=True)

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

        res = self.chat_command(self.resource, "bla", limit=1)
        self.assertEqual(len(res), 2)
        self.assertEqual(res[0], "2/-1 players online")
        self.assertRegex(res[1], user1.name)
    def test_send_message_command(self, command):
        """ Test sending a command """

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

        self.resource.send("/coucou bla")
        command.assert_called_with("coucou", "bla")

        self.resource.send("/command ")
        command.assert_called_with("command", None)
Esempio n. 16
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)
Esempio n. 17
0
    def test_unban_without_room(self):
        """ Test banning a user without room """

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

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

        target_user = UserFactory(online=True)

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

        res = self.chat_command(self.resource, target_user.name)

        self.assertFalse(
            models.Ban.is_ban(self.session, user_id=target_user.id))
        self.assertIsNone(res)
Esempio n. 18
0
    def test_create(self):
        """ Test room creation """

        # No user unauthorized
        with self.assertRaises(exceptions.Forbidden):
            self.resource.create("aaa")

        user1 = UserFactory(connection=self.connection, online=True)
        user2 = UserFactory(connection=self.connection, online=True)

        room = self.resource.create("Room 1", description="description")
        self.assertIsNotNone(room)
        self.assertEqual(room.password, None)
        self.assertEqual(room.description, "description")
        self.assertEqual(room.name, "Room 1")

        self.assertEqual(user1.level(room.id), 10)
        self.assertEqual(user2.level(room.id), 10)

        with self.assertRaises(exceptions.ValidationError):
            self.resource.create("Room 1")
Esempio n. 19
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)
Esempio n. 20
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)
Esempio n. 21
0
    def test_pretty_result(self):
        """ Test pretty_result property """
        date = datetime.datetime(2017, 10, 13, 11, 42)

        song_stat = SongStatFactory(
            difficulty=3,  #HARD
            feet=9,
            grade=3,  #A
            user=UserFactory(name="José Prout"),
            percentage=78.327,
            created_at=date)

        self.assertEqual(song_stat.pretty_result(),
                         r"HARD (9): José Prout A (78.33%) on 13/10/17")
Esempio n. 22
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)
Esempio n. 23
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])
Esempio n. 24
0
    def test_route(self, handle_controller1, handle_controller2):
        """ Test routing packet """

        test_router = router.Router()

        serv = server.StepmaniaServer()
        conn = smconn.StepmaniaConn(server, "8.8.8.8", 42)
        connection = ConnectionFactory(token=conn.token)

        command = smcommand.SMClientCommand.NSCAttack
        packet = smpacket.SMPacket.new(command)

        # No route
        test_router.route(serv, conn, packet)
        handle_controller1.assert_not_called()
        handle_controller2.assert_not_called()

        # Route only controller 1
        test_router.add_route(command, Controller1)
        test_router.route(serv, conn, packet, session=self.session)
        handle_controller1.assert_called_with()
        handle_controller1.reset_mock()
        handle_controller2.assert_not_called()

        # Route with require login
        test_router.add_route(command, Controller2)
        test_router.route(serv, conn, packet, session=self.session)
        handle_controller1.assert_called_with()
        handle_controller1.reset_mock()
        handle_controller2.assert_not_called()

        # Test with login user
        UserFactory(connection_token=connection.token, online=True)

        test_router.route(serv, conn, packet, session=self.session)
        handle_controller1.assert_called_with()
        handle_controller2.assert_called_with()
        handle_controller1.reset_mock()
        handle_controller2.reset_mock()

        # With exceptions, no crash
        handle_controller1.side_effect = Exception()
        test_router.route(serv, conn, packet, session=self.session)
        handle_controller1.assert_called_with()
        handle_controller2.assert_called_with()
        handle_controller1.reset_mock()
        handle_controller2.reset_mock()
Esempio n. 25
0
    def test_can_without_room(self):
        """ Test can function without room """

        if not self.chat_command.permission:
            self.assertEqual(self.chat_command.can(self.connection),
                             not self.chat_command.room)
            return

        self.assertFalse(self.chat_command.can(self.connection))

        user = UserFactory(connection=self.connection, online=True)
        self.assertFalse(self.chat_command.can(self.connection))

        user.rank = self.chat_command.permission.value
        if self.chat_command.room:
            self.assertFalse(self.chat_command.can(self.connection))
        else:
            self.assertTrue(self.chat_command.can(self.connection))
    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")
    def test_send_message(self, send_message):
        """ Test sending a message """

        with self.assertRaises(exceptions.Unauthorized):
            self.resource.send("bla", target="target")

        send_message.assert_not_called()

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

        self.resource.send("bla", target="target")
        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.assertIsNone(msg.room_id)

        self.assertEqual(msg.data["message"], "bla")
        self.assertEqual(msg.data["target"]["value"], "target")
        self.assertEqual(msg.data["target"]["type"], "token")
Esempio n. 28
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)
Esempio n. 29
0
    def test_users(self):
        """ Test getting the users associated with a connection """

        self.assertEqual(self.controller.users, [])
        user = UserFactory(connection=self.connection)
        self.assertEqual(self.controller.users, [user])