Пример #1
0
 def test_get_user_duplicate(self, test_app):
     with test_app.app_context():
         user_dict = api.get_user(TEST_USER['s_number'])
         user = UserDB.create(**user_dict)
         user_dup_dict = api.get_user(TEST_USER['s_number'])
         user_dup = UserDB.create(**user_dup_dict)
         assert user is user_dup
Пример #2
0
 def test_update_user(self, test_app):
     with test_app.app_context():
         user = UserDB.create(**TEST_USER_1)
         updated_user = UserDB.update(**TEST_USER_UPDATED)
         for (key, value) in TEST_USER_UPDATED.items(
         ):  # Make sure all values are updated as expected
             assert updated_user.__getattribute__(key) == value
             assert user.__getattribute__(
                 key) == updated_user.__getattribute__(key)
Пример #3
0
 def test_list_all_users(self, test_app):
     with test_app.app_context():
         user2 = UserDB.create(**TEST_USER_2)
         user1 = UserDB.create(**TEST_USER_1)
         user_list = UserDB.list_all()
         assert len(
             user_list) == 2  # Make sure exactly two users are returned
         assert user1 == user_list[0]  # Make sure each user is in the list
         assert user2 == user_list[
             1]  # Also make sure the order is correct (ascending id)
Пример #4
0
    def test_delete_user(self, test_app):
        with test_app.app_context():
            user = UserDB.create(**TEST_USER_1)
            get_user = UserDB.get(TEST_USER_1['id'])
            assert get_user is not None  # Make sure the user is retrieved

            deleted_user = UserDB.delete(TEST_USER_1['id'])
            assert user is deleted_user  # Make sure the same object is referenced

            get_user = UserDB.get(TEST_USER_1['id'])
            assert get_user is None  # Make sure the user is not retrieved
Пример #5
0
    def create_quick(cls, quantity: int, item_id: int, user_id: int):
        """
        Instantiate a Sale object with parameters from the database and store it in the database.

        :param quantity: Amount of the item to buy.
        :param item_id: Item ID.
        :param user_id: User ID.
        :return: The sale.
        """
        item = ItemDB.get(item_id)
        user = UserDB.get(user_id)

        # Check if it is possible to create the sale correctly
        if item is None:
            raise NotInDatabaseException(
                "Item with id %d does not exist in database." % item_id)
        elif user is None:
            raise NotInDatabaseException(
                "User with id %d does not exist in database." % user_id)

        # Calculate total price and create and return the sale
        total_price = quantity * item.price
        return cls.create(quantity=quantity,
                          total_price=total_price,
                          item_id=item_id,
                          item_name=item.name,
                          user_id=user_id,
                          user_s_number=user.s_number)
Пример #6
0
 def test_get_user(self, test_app):
     with test_app.app_context():
         user_dict = api.get_user(TEST_USER['s_number'])
         user = UserDB.create(**user_dict)
         for (key, value) in TEST_USER.items(
         ):  # Make sure all fields are stored correctly
             assert user.__getattribute__(key) == value
Пример #7
0
def login():
    if request.method == 'GET':  # Load the login page to let users enter their s-number
        return render_template('login.jinja2')

    elif request.method == 'POST':  # Attempt to login the user
        s_number = request.form[
            's-number']  # Load the student number from the push form
        try:  # Attempt to find the user from Congressus
            user_dict = api.get_user(s_number=s_number)  # Create a User
        except api.UserNotFoundException as err:  # The user was not found
            flash('User ' + s_number + ' not found, try again.', 'error')
            return render_template('login.jinja2')
        except HTTPError as err:  # There was a connection error
            flash(str(err), 'error')
            return render_template('login.jinja2')

        # Add user to the database
        user = UserDB.create(**user_dict)

        # Add session variables to identify the user
        session['user_id'] = user.id
        session['user_first_name'] = user.first_name
        session['user_s_number'] = user.s_number

        return redirect(
            url_for('streeplijst.folder'))  # Redirect to the streeplijst
Пример #8
0
    def test_list_all_users(self, test_app):
        with test_app.app_context():
            user1_dict = api.get_user(TEST_USER['s_number'])
            user2_dict = api.get_user(TEST_USER_NO_SDD['s_number'])
            user1 = UserDB.create(**user1_dict)
            user2 = UserDB.create(**user2_dict)

            user_list = UserDB.list_all()

            assert len(
                user_list) == 2  # Make sure exactly two users are returned

            # Make sure all users are in the list and it is sorted correctly
            if user1.id < user2.id:  # id of user1 is lower, so user1 must appear first in the list
                assert user1 == user_list[0]
                assert user2 == user_list[1]
            else:  # id of user2 is lower so it must appear first in the list
                assert user2 == user_list[0]
                assert user1 == user_list[1]
Пример #9
0
 def test_create_quick_sale(self, test_app):
     with test_app.app_context():
         item = ItemDB.create(**TEST_ITEM)
         user = UserDB.create(**TEST_USER)
         sale = SaleDB.create_quick(quantity=1,
                                    item_id=item.id,
                                    user_id=user.id)
         for (key, value) in TEST_SALE.items(
         ):  # Make sure all fields are stored correctly
             assert sale.__getattribute__(key) == value
         assert sale.id == 1  # The ID autoincrements, starting at 1
         assert sale.status == Sale.STATUS_NOT_POSTED  # Test the status
         assert sale.api_id is None and sale.api_created is None and sale.error_msg is None  # Test None fields
Пример #10
0
 def test_create_user_extra_fields(self, test_app):
     with test_app.app_context():
         user = UserDB.create(
             **TEST_USER_EXTRA_FIELDS
         )  # Create a user with fields which are not needed
         required_fields = ((key, value)
                            for (key,
                                 value) in TEST_USER_EXTRA_FIELDS.items()
                            if key != 'email')
         for (
                 key, value
         ) in required_fields:  # Make sure all fields are stored correctly
             assert user.__getattribute__(key) == value
         assert hasattr(user, 'email') is False
Пример #11
0
 def test_get_user_by_s_number(self, test_app):
     with test_app.app_context():
         user = UserDB.create(**TEST_USER_1)
         other_user = UserDB.get_by_s_number(TEST_USER_1['s_number'])
         assert user is other_user  # Make sure both objects are the same instance
Пример #12
0
 def test_create_user_duplicate(self, test_app):
     with test_app.app_context():
         user = UserDB.create(**TEST_USER_1)
         duplicate_user = UserDB.create(**TEST_USER_DUP)
         assert user is duplicate_user  # Make sure the same user is referenced in case of a duplicate
Пример #13
0
 def test_create_user(self, test_app):
     with test_app.app_context():
         user = UserDB.create(**TEST_USER_1)
         for (key, value) in TEST_USER_1.items(
         ):  # Make sure all fields are stored correctly
             assert user.__getattribute__(key) == value