Esempio n. 1
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. 2
0
    def test_ban_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.assertTrue(models.Ban.is_ban(self.session, user_id=target_user.id))
        disconnect_user.assert_called_with(target_user.id)

        self.assertIsNone(res)
Esempio n. 3
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. 4
0
    def test_list_users_booked_on_flights_endpoint(self):
        flight = FlightFactory.build()
        flight_seat1 = FlightSeatFactory.build(flight_id=flight.id,
                                               is_available=False)
        flight_seat2 = FlightSeatFactory.build(flight_id=flight.id,
                                               is_available=False)
        user1 = UserFactory.build()
        user2 = UserFactory.build()

        db.session.add(flight)
        db.session.add(flight_seat1)
        db.session.add(flight_seat2)
        db.session.add(user1)
        db.session.add(user2)
        db.session.commit()

        ticket1 = TicketFactory.build(flight_seat_id=flight_seat1.id,
                                      user_id=user1.id,
                                      status="PaidFor")
        ticket2 = TicketFactory.build(flight_seat_id=flight_seat2.id,
                                      user_id=user2.id,
                                      status="PaidFor")
        db.session.add(ticket1)
        db.session.add(ticket2)
        db.session.commit()

        response = self.client().get(self.make_url(
            '/flight/reserved/{}/'.format(flight.id)),
                                     headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))
        payload = response_json['payload']
        print(response_json)
        self.assertJSONKeyPresent(response_json, 'payload')
        self.assertEqual(len(payload['flightSeats']), 2)
Esempio n. 5
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. 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)
Esempio n. 7
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. 8
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)
Esempio n. 10
0
    def test_get_specific_card_enpoint(self):
        user = UserFactory.build()

        db.session.add(user)
        db.session.commit()

        card = CardFactory.build(user_id=user.id)
        db.session.add(card)

        db.session.commit()

        response = self.client().get(self.make_url('/card/{}/'.format(
            card.id)),
                                     headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))
        payload = response_json['payload']

        self.assert200(response)
        self.assertJSONKeyPresent(payload, 'cardDetail')
        self.assertJSONKeysPresent(payload['cardDetail'], 'cardNumber',
                                   'securiyNumber')
        self.assertEqual(int(payload['cardDetail']['id']), card.id)
        self.assertEqual(payload['cardDetail']['securiyNumber'],
                         int(card.securiy_number))
        self.assertEqual(payload['cardDetail']['cardNumber'], card.card_number)
Esempio n. 11
0
    def test_create_card_endpoint(self):
        user = UserFactory.build()
        print(user.first_name)
        db.session.add(user)
        db.session.commit()

        card = CardFactory.build(user_id=user.id)
        data = {
            'cardNumber': card.card_number,
            'expiryMonth': card.expiry_month,
            'expiryYear': card.expiry_year,
            'securityNumber': card.securiy_number,
            'userId': user.id
        }

        response = self.client().post(self.make_url('/card/'),
                                      data=self.encode_to_json_string(data),
                                      headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))
        print(response_json)
        payload = response_json['payload']

        self.assertJSONKeyPresent(response_json, 'payload')
        self.assertEqual(payload['cardDetail']['cardNumber'], card.card_number)
        self.assertEqual(payload['cardDetail']['securiyNumber'],
                         int(card.securiy_number))
Esempio n. 12
0
    def test_update_card_endpoint(self):
        user = UserFactory.build()

        db.session.add(user)
        db.session.commit()

        card = CardFactory.build(user_id=user.id)
        db.session.add(card)

        db.session.commit()

        data = data = {
            'cardNumber': '1234567890987665',
            'expiryMonth': '3',
            'expiryYear': '2026',
            'securityNumber': '123'
        }
        response = self.client().put(self.make_url('/card/{}/'.format(
            card.id)),
                                     data=self.encode_to_json_string(data),
                                     headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))
        payload = response_json['payload']

        self.assert200(response)
        self.assertEqual(payload['cardDetail']['securiyNumber'],
                         int(card.securiy_number))
        self.assertEqual(payload['cardDetail']['cardNumber'], card.card_number)
        '''Test invalid update request'''
        # User arbitrary value of 100 as the location ID
        response = self.client().put(self.make_url('/card/100/'),
                                     data=self.encode_to_json_string(data),
                                     headers=self.headers())
        self.assert400(response)
    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. 15
0
    def test_get_ticket_endpoint(self):
        flight = FlightFactory.build()
        flight_seat = FlightSeatFactory.build(flight_id=flight.id)
        user = UserFactory.build()

        db.session.add(flight)
        db.session.add(flight_seat)
        db.session.add(user)
        db.session.commit()

        ticket1 = TicketFactory.build(flight_seat_id=flight_seat.id,
                                      user_id=user.id,
                                      status="Booked")
        ticket2 = TicketFactory.build(flight_seat_id=flight_seat.id,
                                      user_id=user.id,
                                      status="PaidFor")
        db.session.add(ticket1)
        db.session.add(ticket2)
        db.session.commit()

        response = self.client().get(self.make_url('/ticket/{}/'.format(
            ticket1.id)),
                                     headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))
        payload = response_json['payload']
        print(payload['ticket'])
        self.assertJSONKeyPresent(response_json, 'payload')
        self.assertEqual(payload['ticket']['ticketNo'], ticket1.ticket_no)
Esempio n. 16
0
    def test_create_ticket_endpoint(self):
        flight = FlightFactory.build()
        flight_seat = FlightSeatFactory.build(flight_id=flight.id)
        user = UserFactory.build()
        ticket = TicketFactory.build()

        db.session.add(flight)
        db.session.add(flight_seat)
        db.session.add(user)
        db.session.commit()

        data = {
            'flightSeatId': flight_seat.id,
            'status': ticket.status,
            'userId': user.id
        }
        response = self.client().post(self.make_url('/ticket/'),
                                      data=self.encode_to_json_string(data),
                                      headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))

        payload = response_json['payload']

        self.assertJSONKeyPresent(response_json, 'payload')
        self.assertJSONKeyPresent(payload['ticket'], 'flightSeatId')
        self.assertJSONKeyPresent(payload['ticket'], 'ticketNo')
Esempio n. 17
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. 18
0
    def test_delete_ticket_endpoint(self):
        flight = FlightFactory.build()
        flight_seat = FlightSeatFactory.build(flight_id=flight.id)
        user = UserFactory.build()

        db.session.add(flight)
        db.session.add(flight_seat)
        db.session.add(user)
        db.session.commit()

        ticket1 = TicketFactory.build(flight_seat_id=flight_seat.id,
                                      user_id=user.id,
                                      status="Reserved")
        db.session.add(ticket1)
        db.session.commit()

        data = {
            'flightSeatId': flight_seat.id,
            'status': 'Booked',
            'userId': user.id
        }
        response = self.client().delete(self.make_url('/ticket/{}/'.format(
            ticket1.id)),
                                        headers=self.headers())
        response_json = self.decode_from_json_string(
            response.data.decode('utf-8'))
        payload = response_json['payload']
        self.assertJSONKeyPresent(response_json, 'payload')
        self.assertEqual(payload['status'], 'success')
Esempio n. 19
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)
    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_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")
Esempio n. 22
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)
Esempio n. 23
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))
Esempio n. 24
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))
Esempio n. 25
0
    def test_create_user_weak_password_endpoint(self):
        user = UserFactory.build()
        data = {'firstName': user.first_name, 'lastName': user.last_name, 'middleName': user.middle_name, 'emailAddress': user.email_address, 'password': '******'}
        response = self.client().post(self.make_url('/user/'), data=self.encode_to_json_string(data), headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        payload = response_json['msg']

        self.assertJSONKeyPresent(response_json, 'msg')
        self.assertEqual(payload, 'Bad Request - password not strong enough')
    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. 27
0
    def test_reset_with_strong_password_endpoint(self):
        user = UserFactory.create()
        password = '******'
        data = {'emailAddress': user.email_address, 'password': password}
        response = self.client().put(self.make_url('/user/reset'), data=self.encode_to_json_string(data), headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        payload = response_json['msg']

        self.assertJSONKeyPresent(response_json, 'msg')
        self.assertEqual(payload, 'OK')
Esempio n. 28
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. 29
0
    def test_reset_with_weak_password_endpoint(self):
        user = UserFactory.create()

        data = {'emailAddress': user.email_address, 'password': '******'}
        response = self.client().put(self.make_url('/user/reset'), data=self.encode_to_json_string(data), headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        payload = response_json['msg']

        self.assertJSONKeyPresent(response_json, 'msg')
        self.assertEqual(payload, 'Bad Request - password not strong enough')
Esempio n. 30
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. 31
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. 32
0
    def test_create_user_endpoint(self):
        user = UserFactory.build()
        data = {'firstName': user.first_name, 'lastName': user.last_name, 'middleName': user.middle_name, 'emailAddress': user.email_address, 'password': user.password}
        response = self.client().post(self.make_url('/user/'), data=self.encode_to_json_string(data), headers=self.headers())
        response_json = self.decode_from_json_string(response.data.decode('utf-8'))
        payload = response_json['payload']

        self.assertJSONKeyPresent(response_json, 'payload')
        self.assertEqual(payload['user']['firstName'], user.first_name)
        self.assertEqual(payload['user']['lastName'], user.last_name)
        self.assertEqual(payload['user']['middleName'], user.middle_name)
        self.assertEqual(payload['user']['emailAddress'], user.email_address)
    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")