예제 #1
0
def test_unit_edit_user(test_app):

    app, test_client = test_app

    temp_user_example_dict = customers_example[0]

    # create a user
    create_user_EP(test_client, **temp_user_example_dict)

    #--- UNIT TESTS ---

    with app.app_context():

        # modify a user and check if it is modified

        getuser = db.session.query(User).filter(
            User.email == temp_user_example_dict['email']).first()

        getuser.phone = '4444444444'
        getuser.password = '******'
        db.session.commit()

        getuser = db.session.query(User).filter(
            User.email == temp_user_example_dict['email']).first()

        assert getuser is not None
        assert getuser.email == temp_user_example_dict['email']
        assert getuser.phone == '4444444444'
        assert getuser.firstname == temp_user_example_dict['firstname']
        assert getuser.lastname == temp_user_example_dict['lastname']
        assert getuser.password == "newpassw"
        assert getuser.dateofbirth == datetime.date(2001, 10, 5)
예제 #2
0
def test_login_user(test_app):
    app, test_client = test_app

    temp_user_example_dict = customers_example[0]

    test_client.post('/create_user',
                     data=temp_user_example_dict,
                     follow_redirects=True)

    # --- UNIT TESTS ---
    with app.app_context():

        # authentication with correct credentials
        getuser = db.session.query(User).filter(
            User.email == temp_user_example_dict['email']).first()
        assert getuser is not None
        assert getuser.authenticate(temp_user_example_dict['password']) == True

        # authentication with wrong email
        getuser = db.session.query(User).filter(
            User.email == "*****@*****.**").first()

        assert getuser is None

        # authentication with correct email and wrong password
        getuser = db.session.query(User).filter(
            User.email == temp_user_example_dict['email']).first()

        assert getuser is not None
        assert getuser.authenticate("wrngpass") == False

    # --- COMPONENT TESTS ---

    # test get
    assert test_client.get('/login').status_code == 200

    # authentication with wrong email
    assert user_login_EP(test_client, "*****@*****.**",
                         temp_user_example_dict['password']).status_code == 401

    # authentication with correct email and wrong password
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         "wrngpass").status_code == 401

    # authentication with wrong email syntax
    assert user_login_EP(test_client, "wrongemailsyntax",
                         temp_user_example_dict['password']).status_code == 400

    # authentication with correct credentials
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         temp_user_example_dict['password']).status_code == 200

    # double login
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         temp_user_example_dict['password']).status_code == 200

    # creation of a new user when already logged in must fail
    temp_user_example_dict['email'] = '*****@*****.**'
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 403
예제 #3
0
def test_component_user_editing(test_app):

    app, test_client = test_app

    temp_user_example_dict = customers_example[0]

    # create a new user
    create_user_EP(test_client, **temp_user_example_dict)

    # test get without user logged
    assert test_client.get('/edit_user_informations').status_code == 401

    # test without user logged
    assert edit_user_EP(test_client, '4444444444',
                        temp_user_example_dict['password'],
                        'passw').status_code == 401

    # login with a user
    user_login_EP(test_client, temp_user_example_dict['email'],
                  temp_user_example_dict['password'])

    # test get with success
    assert test_client.get('/edit_user_informations').status_code == 200

    # try to edit the user with success
    assert edit_user_EP(test_client, '4444444444',
                        temp_user_example_dict['password'],
                        'newpassw').status_code == 200

    # login with old password
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         "passw").status_code == 401

    # login the user with the new password
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         "newpassw").status_code == 200

    # try to edit an user with wrong password
    assert edit_user_EP(test_client, '4444444444', 'wrongp',
                        'newpassw').status_code == 401

    # try to send an invalid form (password too long)
    assert edit_user_EP(test_client, '4444444444', 'passwtoolong',
                        'newpassw').status_code == 400
예제 #4
0
def test_mark_positive(test_app):
    app, test_client = test_app

    # create a health authority and an user for testing
    temp_ha_dict = dict(email='*****@*****.**',
                        phone='3333333333',
                        firstname='Ha',
                        lastname='Ha',
                        password='******',
                        dateofbirth='05/10/2000',
                        role='ha')
    temp_user_example_dict = customers_example[0]
    #assert create_user_EP(test_client, **temp_ha_dict).status_code == 200
    insert_ha(db, app)
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200

    # --- UNIT TESTS ---
    with app.app_context():
        # mark a wrong user user that doesn't exist
        getuser = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert getuser is None

        # get the user to be marked as positive
        getuser = db.session.query(User).filter(
            User.email == temp_user_example_dict['email']).first()

        assert getuser is not None

        new_quarantine = Quarantine()
        new_quarantine.user_id = getuser.id
        today = datetime.date.today()
        new_quarantine.start_date = today
        new_quarantine.end_date = today + datetime.timedelta(days=14)
        new_quarantine.in_observation = True

        db.session.add(new_quarantine)
        db.session.commit()

        getquarantine = db.session.query(Quarantine).filter(
            Quarantine.user_id == getuser.id).first()
        assert getquarantine is not None
        assert getquarantine.start_date == today
        assert getquarantine.end_date == today + datetime.timedelta(days=14)
        assert getquarantine.in_observation == True

        # change in_observation state, so the user can use the app again
        getquarantine.in_observation = False
        db.session.commit()

        getquarantinenewstatus = db.session.query(Quarantine).filter(
            Quarantine.user_id == getuser.id).first()
        assert getquarantinenewstatus.in_observation == False
예제 #5
0
def test_logout_user(test_app):
    app, test_client = test_app

    temp_user_example_dict = customers_example[0]

    create_user_EP(test_client, **temp_user_example_dict)

    # --- UNIT TESTS --- nothing to be tested as unit

    # --- COMPONENT TESTS ---
    # logout without user logged
    result = test_client.get('/logout', follow_redirects=True)

    assert result.status_code == 401

    result = user_login_EP(test_client, temp_user_example_dict['email'],
                           temp_user_example_dict['password'])

    assert result.status_code == 200

    # logout
    result = test_client.get('/logout', follow_redirects=True)

    assert result.status_code == 200
예제 #6
0
def test_unit_reviews(test_app):
    app, test_client = test_app

    assert create_user_EP(test_client,
                          **restaurant_owner_example[0]).status_code == 200
    assert user_login_EP(
        test_client, restaurant_owner_example[0]['email'],
        restaurant_owner_example[0]['password']).status_code == 200
    assert create_restaurant_EP(test_client).status_code == 200

    with app.app_context():

        # get a restaurant
        restaurant = db.session.query(Restaurant).filter_by(
            name=restaurant_example[0]['name']).first()
        #get a user
        user = db.session.query(User).filter_by(
            email=restaurant_owner_example[0]['email']).first()

        new_review = Review()
        new_review.marked = False
        new_review.comment = 'Good quality restaurant'
        new_review.rating = 3
        new_review.date = datetime.date.today()
        new_review.restaurant_id = restaurant.id
        new_review.reviewer_id = user.id
        db.session.add(new_review)
        db.session.commit()

        review = Review.query.filter_by(
            restaurant_id=int(restaurant.id)).first()

        assert review.marked == False
        assert review.comment == 'Good quality restaurant'
        assert review.rating == 3
        assert review.date == datetime.date.today()
        assert review.restaurant_id == restaurant.id
        assert review.reviewer_id == user.id
예제 #7
0
def test_users_list(test_app):

    app, test_client = test_app

    temp_user_example_dict = customers_example[0]
    insert_admin(db, app)

    #assert test_client.get('/users').status_code == 401

    # login with a user
    assert user_login_EP(test_client, '*****@*****.**',
                         'admin').status_code == 200

    assert test_client.get('/users').status_code == 200

    assert test_client.get('/logout', follow_redirects=True).status_code == 200

    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200

    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         temp_user_example_dict['password']).status_code == 200

    assert test_client.get('/users').status_code == 403
예제 #8
0
def test_unit_edit_restaurant(test_app):
    app, test_client = test_app

    # --- UNIT TESTS ---
    with app.app_context():

        #---------------------------------------------------------- building the starting conditions
        # create a user
        assert create_user_EP(test_client,
                              email='*****@*****.**',
                              password='******',
                              role='owner').status_code == 200
        user_test = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert user_test is not None
        user_test_id = user_test.id

        #create a restaurant
        body_restaurant = dict(owner_id=user_test.id,
                               name='Trial',
                               lat=22,
                               lon=22,
                               phone='3346734121',
                               cuisine_type=[Restaurant.CUISINE_TYPES(1)],
                               capacity=10,
                               prec_measures='leggeX',
                               avg_time_of_stay=30,
                               tot_reviews=5,
                               avg_rating=5,
                               likes=4)
        restaurant = Restaurant(**body_restaurant)
        db.session.add(restaurant)
        db.session.commit()

        #test if the restaurant was created
        restaurant_to_check = db.session.query(Restaurant).filter(
            Restaurant.id == restaurant.id).first()
        assert restaurant_to_check is not None
        check_restaurants(restaurant_to_check, restaurant)

        fields = {
            'phone': '345',
            'dishes-0-dish_name': 'pizza',
            'dishes-0-price': 4,
            'dishes-0-ingredients': 'pomodoro'
        }

        assert user_login_EP(test_client,
                             email='*****@*****.**',
                             password='******').status_code == 200
        assert test_client.post('/edit_restaurant_informations/' +
                                str(restaurant.id),
                                data=fields,
                                follow_redirects=True).status_code == 200

        assert restaurant.phone == '345'
        dish = db.session.query(Dish).filter_by(
            restaurant_id=restaurant.id).first()
        assert dish.dish_name == 'pizza'
        assert dish.price == 4
        assert dish.ingredients == 'pomodoro'
예제 #9
0
def test_insert_working_day(test_app):
    app, test_client = test_app

    # --- UNIT TESTS ---
    with app.app_context():
        # create a user and a restaurant to testing working_day insertions
        assert create_user_EP(
            test_client,
            email='*****@*****.**').status_code == 200
        user_test = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert user_test is not None
        restaurant_dict = dict(owner_id=user_test.id,
                               name='Trial Restaurant',
                               lat=22,
                               lon=22,
                               phone='3346734121',
                               cuisine_type=[Restaurant.CUISINE_TYPES(1)],
                               capacity=10,
                               prec_measures='leggeX',
                               avg_time_of_stay=30)
        restaurant = Restaurant(**restaurant_dict)
        db.session.add(restaurant)
        db.session.commit()
        restaurant = db.session.query(Restaurant).first()
        assert restaurant is not None

        # incorrect mandatory fields with validators
        incorrect_working_days = [
            dict(restaurant_id=None,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=0,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=-1,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=None,
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=0,
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day='ciao',
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=None),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[1, 2]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=['ei']),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=('12:00', '15:00')),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('12', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('ciao', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[(1, '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('12:00', '15:00', '17:00'),
                              ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('12:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('19:00', '18:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(2),
                 work_shifts=[('8:00', '10:00'), ('12:00', '15:00'),
                              ('19:00', '23:00')])
        ]
        count_assert = 0
        for w in incorrect_working_days:
            try:
                working_day = WorkingDay(**w)
            except ValueError:
                count_assert += 1
                assert True
        assert len(incorrect_working_days) == count_assert

        # missing fields
        incorrect_working_days = [
            dict(day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1))
        ]
        count_assert = 0
        for w in incorrect_working_days:
            working_day = WorkingDay(**w)
            try:
                db.session.add(working_day)
                db.session.commit()
            except (exc.IntegrityError, exc.InvalidRequestError):
                db.session.rollback()
                count_assert += 1
                assert True
        assert len(incorrect_working_days) == count_assert

        # correct working_days
        correct_working_days = [
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('19:00', '23:00')]),
            dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(2),
                 work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]),
        ]
        for w in correct_working_days:
            working_day = WorkingDay(**w)
            db.session.add(working_day)
            db.session.commit()
            working_day_to_check = db.session.query(WorkingDay).filter(
                WorkingDay.restaurant_id == working_day.restaurant_id).filter(
                    WorkingDay.day == working_day.day).first()
            assert working_day_to_check is not None
            check_working_days(working_day_to_check, working_day)

        # the insertion of the same day for the same restaurant must fail
        w = dict(restaurant_id=restaurant.id,
                 day=WorkingDay.WEEK_DAYS(1),
                 work_shifts=[('19:00', '23:00')])
        working_day = WorkingDay(**w)
        count_assert = 0
        try:
            db.session.add(working_day)
            db.session.commit()
        except (exc.IntegrityError, exc.InvalidRequestError):
            db.session.rollback()
            count_assert += 1
            assert True
        assert count_assert == 1

        # check total working_days
        working_days = db.session.query(WorkingDay).all()
        assert len(working_days) == len(correct_working_days)
예제 #10
0
def test_insert_dish(test_app):
    app, test_client = test_app

    # --- UNIT TESTS ---
    with app.app_context():
        # create a user and a restaurant to testing dish insertions
        assert create_user_EP(test_client, email='*****@*****.**').status_code == 200
        user_test = db.session.query(User).filter(User.email == '*****@*****.**').first()
        assert user_test is not None
        restaurant_dict = dict(
            owner_id = user_test.id,
            name = 'Trial Restaurant',
            lat = 22,
            lon = 22, 
            phone = '3346734121',
            cuisine_type = [Restaurant.CUISINE_TYPES(1)],
            capacity = 10,
            prec_measures = 'leggeX',
            avg_time_of_stay = 30
        )
        restaurant = Restaurant(**restaurant_dict)
        db.session.add(restaurant)
        db.session.commit()
        restaurant = db.session.query(Restaurant).first()
        assert restaurant is not None

        # incorrect fields with validators
        incorrect_dishes = [
            dict(restaurant_id = None, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = 0, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = -1, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = None, price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = '', price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = None, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = -1, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0, ingredients = None),
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0, ingredients = '')
        ]
        count_assert = 0
        for d in incorrect_dishes:
            try:
                dish = Dish(**d)
            except ValueError:
                count_assert += 1
                assert True
        assert len(incorrect_dishes) == count_assert


        # missing mandatory fields
        incorrect_dishes = [
            dict(dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0)
        ]
        count_assert = 0
        for d in incorrect_dishes:
            dish = Dish(**d)
            try:
                db.session.add(dish)
                db.session.commit()
            except (exc.IntegrityError, exc.InvalidRequestError):
                db.session.rollback()
                count_assert += 1
                assert True
        assert len(incorrect_dishes) == count_assert

        # correct dishes
        correct_dishes = [
            dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'),
            dict(restaurant_id = restaurant.id, dish_name = 'p', price = 0.1, ingredients = 'p')
        ]
        for d in correct_dishes:
            dish = Dish(**d)
            db.session.add(dish)
            db.session.commit()
            dish_to_check = db.session.query(Dish).filter(Dish.id == dish.id).first()
            assert dish_to_check is not None
            check_dishes(dish_to_check, dish)

        # check total dishes
        dishes = db.session.query(Dish).all()
        assert len(dishes) == len(correct_dishes)
예제 #11
0
def test_insert_table(test_app):
    app, test_client = test_app

    # --- UNIT TESTS ---
    with app.app_context():
        # create a user and a restaurant to testing table insertions
        assert create_user_EP(test_client, email='*****@*****.**').status_code == 200
        user_test = db.session.query(User).filter(User.email == '*****@*****.**').first()
        assert user_test is not None
        restaurant_dict = dict(
            owner_id = user_test.id,
            name = 'Trial Restaurant',
            lat = 22,
            lon = 22, 
            phone = '3346734121',
            cuisine_type = [Restaurant.CUISINE_TYPES(1)],
            capacity = 10,
            prec_measures = 'leggeX',
            avg_time_of_stay = 30
        )
        restaurant = Restaurant(**restaurant_dict)
        db.session.add(restaurant)
        db.session.commit()
        restaurant = db.session.query(Restaurant).first()
        assert restaurant is not None

        # incorrect mandatory fields with validators
        incorrect_tables = [
            dict(restaurant_id = None, capacity = 1, table_name = 'table'),
            dict(restaurant_id = 0, capacity = 1, table_name = 'table'),
            dict(restaurant_id = -1, capacity = 1, table_name = 'table'),
            dict(restaurant_id = restaurant.id, capacity = None, table_name = 'table'),
            dict(restaurant_id = restaurant.id, capacity = 0, table_name = 'table'),
            dict(restaurant_id = restaurant.id, capacity = -1, table_name = 'table'),
            dict(restaurant_id = restaurant.id, capacity = 1, table_name = None),
            dict(restaurant_id = restaurant.id, capacity = 1, table_name = '')
        ]
        count_assert = 0
        for t in incorrect_tables:
            try:
                table = Table(**t)
            except ValueError:
                count_assert += 1
                assert True
        assert len(incorrect_tables) == count_assert

        # missing fields
        incorrect_tables = [
            dict(capacity = 1, table_name = 'table'),
            dict(restaurant_id = restaurant.id, table_name = 'table'),
            dict(restaurant_id = restaurant.id, capacity = 1)
        ]
        count_assert = 0
        for t in incorrect_tables:
            table = Table(**t)
            try:
                db.session.add(table)
                db.session.commit()
            except (exc.IntegrityError, exc.InvalidRequestError):
                db.session.rollback()
                count_assert += 1
                assert True
        assert len(incorrect_tables) == count_assert

        # correct tables
        correct_tables = [
            dict(restaurant_id = restaurant.id, capacity = 1, table_name = 'c'),
            dict(restaurant_id = restaurant.id, capacity = 30, table_name = 'big table'),
        ]
        for t in correct_tables:
            table = Table(**t)
            db.session.add(table)
            db.session.commit()
            table_to_check = db.session.query(Table).filter(Table.id == table.id).first()
            assert table_to_check is not None
            check_tables(table_to_check, table)

        # check total tables
        tables = db.session.query(Table).all()
        assert len(tables) == len(correct_tables)
예제 #12
0
def test_delete_user(test_app):
    app, test_client = test_app

    # unregister without a previous log-in
    assert test_client.delete('/delete_user',
                              follow_redirects=True).status_code == 401

    # unregister HA
    insert_ha(db, app)
    assert user_login_EP(test_client, '*****@*****.**',
                         'ha').status_code == 200
    assert test_client.delete('/delete_user',
                              follow_redirects=True).status_code == 403
    assert user_logout_EP(test_client).status_code == 200

    # unregister a user without reservations
    with app.app_context():
        assert create_user_EP(test_client,
                              email='*****@*****.**',
                              password='******',
                              role='customer').status_code == 200
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 200
        assert test_client.delete('/delete_user',
                                  follow_redirects=True).status_code == 200
        del_inactive_users()

        # check customer absence in the db
        assert db.session.query(User).filter(
            User.email == '*****@*****.**').first() == None
        assert user_logout_EP(test_client).status_code == 200
        assert user_logout_EP(test_client).status_code == 401
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 401

    # register a owner
    assert create_user_EP(test_client,
                          email='*****@*****.**',
                          password='******',
                          role='owner').status_code == 200

    # register a restaurant
    assert user_login_EP(test_client, '*****@*****.**',
                         'passw').status_code == 200
    assert create_restaurant_EP(test_client,
                                correct_restaurant).status_code == 200
    assert user_logout_EP(test_client).status_code == 200

    # register two guests
    assert create_user_EP(test_client,
                          email='*****@*****.**',
                          password='******',
                          role='customer').status_code == 200
    assert create_user_EP(test_client,
                          email='*****@*****.**',
                          password='******',
                          role='customer').status_code == 200

    # unregister a user with only future reservations
    with app.app_context():
        assert create_user_EP(test_client,
                              email='*****@*****.**',
                              password='******',
                              role='customer').status_code == 200
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 200

        # register a reservation with 2 guests

        # check all them present in the db
        #user_test = db.session.query(User).filter(User.email == '*****@*****.**').first()
        #owner_test = db.session.query(User).filter(User.email == '*****@*****.**').first()
        #restaurant_test = db.session.query(Restaurant).filter(Restaurant.owner_id == owner_test.id).first()

        # look for a table in a correct date and time
        assert restaurant_reservation_EP(
            test_client,
            restaurant_id='1',  #restaurant_test.id, 
            date=correct_reservation['date'],
            time=correct_reservation['time'],
            guests=correct_reservation['guests'] + 1).status_code == 200
        # placing a reservation
        reservation_date_str = correct_reservation[
            'date'] + " " + correct_reservation['time']
        reservation_datetime = datetime.datetime.strptime(
            reservation_date_str, "%d/%m/%Y %H:%M")
        guests_email_dict = dict()
        for i in range(correct_reservation['guests']):
            key = 'guest' + str(i + 1)
            guests_email_dict[key] = correct_email[i]
        assert restaurant_reservation_POST_EP(
            test_client,
            restaurant_id='1',
            table_id_reservation=1,  #8,
            date=reservation_date_str,
            guests=correct_reservation['guests'] + 1,
            data=guests_email_dict).status_code == 666
        # checking via db if reservation has been added
        reservation_test = db.session.query(Reservation).filter(
            Reservation.restaurant_id == '1',
            Reservation.table_id == 1,  #8, 
            Reservation.date == reservation_datetime).first()
        assert reservation_test != None

        # unregister the customer, also all its future reservations
        assert test_client.delete('/delete_user',
                                  follow_redirects=True).status_code == 200
        del_inactive_users()

        # check the changes in db
        us = db.session.query(User).filter(
            User.email == '*****@*****.**').first()

        assert us == None
        assert user_logout_EP(test_client).status_code == 200
        assert user_logout_EP(test_client).status_code == 401
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 401

    # unregister a user with a computed reservation but are not passed 14 days from this last
    with app.app_context():
        assert create_user_EP(test_client,
                              email='*****@*****.**',
                              password='******',
                              role='customer').status_code == 200
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 200
        #user_test = db.session.query(User).filter(User.email == '*****@*****.**').first()

        # look for a table in a correct date and time
        startdate = datetime.date.today()
        enddate = startdate + datetime.timedelta(
            days=-1)  # placing for yesterday
        assert restaurant_reservation_EP(
            test_client,
            restaurant_id='1',  #restaurant_test.id, 
            date=enddate,
            time=correct_reservation['time'],
            guests=correct_reservation['guests'] + 1).status_code == 200
        # placing a reservation
        reservation_date_str = str(
            enddate.strftime("%d/%m/%Y")) + " " + correct_reservation['time']
        reservation_datetime = datetime.datetime.strptime(
            reservation_date_str, "%d/%m/%Y %H:%M")
        guests_email_dict = dict()
        for i in range(correct_reservation['guests']):
            key = 'guest' + str(i + 1)
            guests_email_dict[key] = correct_email[i]
        assert restaurant_reservation_POST_EP(
            test_client,
            restaurant_id='1',
            table_id_reservation=2,  #8,
            date=reservation_date_str,
            guests=correct_reservation['guests'] + 1,
            data=guests_email_dict).status_code == 666
        # checking via db if reservation has been added
        reservation_test = db.session.query(Reservation).filter(
            Reservation.restaurant_id == '1',
            Reservation.table_id == 2,  #8, 
            Reservation.date == reservation_datetime).first()
        assert reservation_test != None

        # unregister the customer, also all its future reservations
        #assert user_test.id == 1
        assert test_client.delete('/delete_user',
                                  follow_redirects=True).status_code == 200
        del_inactive_users()

        # check the changes in db not happened since are not passed the days
        us = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert us != None

        assert user_logout_EP(test_client).status_code == 200
        assert user_logout_EP(test_client).status_code == 401
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 401

    # unregister a user with a computed reservation but are passed exactly 14 days from this last
    # we don't care for those happened more than 14 days ago
    with app.app_context():
        assert create_user_EP(test_client,
                              email='*****@*****.**',
                              password='******',
                              role='customer').status_code == 200
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 200
        #user_test = db.session.query(User).filter(User.email == '*****@*****.**').first()

        # look for a table in a correct date and time
        startdate = datetime.date.today()
        enddate = startdate - datetime.timedelta(
            days=15)  # placing for 14 days ago
        assert restaurant_reservation_EP(
            test_client,
            restaurant_id='1',  #restaurant_test.id, 
            date=enddate,
            time=correct_reservation['time'],
            guests=correct_reservation['guests'] + 1).status_code == 200
        # placing a reservation
        reservation_date_str = str(
            enddate.strftime("%d/%m/%Y")) + " " + correct_reservation['time']
        reservation_datetime = datetime.datetime.strptime(
            reservation_date_str, "%d/%m/%Y %H:%M")
        guests_email_dict = dict()
        for i in range(correct_reservation['guests']):
            key = 'guest' + str(i + 1)
            guests_email_dict[key] = correct_email[i]
        assert restaurant_reservation_POST_EP(
            test_client,
            restaurant_id='1',
            table_id_reservation=2,  #8,
            date=reservation_date_str,
            guests=correct_reservation['guests'] + 1,
            data=guests_email_dict).status_code == 666
        # checking via db if reservation has been added
        reservation_test = db.session.query(Reservation).filter(
            Reservation.restaurant_id == '1',
            Reservation.table_id == 2,  #8, 
            Reservation.date == reservation_datetime).first()
        assert reservation_test != None

        # unregister the customer, also all its reservations
        assert test_client.delete('/delete_user',
                                  follow_redirects=True).status_code == 200
        del_inactive_users()

        # check the changes in db happened since now are passed excatly the days
        us = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert us == None
        assert user_logout_EP(test_client).status_code == 200
        assert user_logout_EP(test_client).status_code == 401
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 401

    # unregister a positive user
    with app.app_context():
        assert create_user_EP(test_client,
                              email='*****@*****.**',
                              password='******',
                              role='customer').status_code == 200
        #assert user_login_EP(test_client, '*****@*****.**', 'passw').status_code == 200
        #assert user_logout_EP(test_client).status_code == 200

        # HA mark as positive the previous customer
        assert user_login_EP(test_client, '*****@*****.**',
                             'ha').status_code == 200
        assert test_client.get('/patient_informations',
                               follow_redirects=True).status_code == 200
        assert test_client.post(
            '/patient_informations',
            data=dict(email="*****@*****.**"),
            follow_redirects=True).status_code == 200
        assert mark_patient_as_positive(
            test_client, '*****@*****.**').status_code == 555
        # check quarantine presence in the db
        #user_test = db.session.query(User).filter(User.email == '*****@*****.**').first()
        #assert db.session.query(Quarantine).filter(Quarantine.user_id == user_test.id).first() != None
        assert user_logout_EP(test_client).status_code == 200

        # unregister a positive customer
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 200
        assert test_client.delete('/delete_user',
                                  follow_redirects=True).status_code == 200
        del_inactive_users()

        #check quarantine and user presence in the db
        user_test = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert user_test.is_active == False
        user_quar = db.session.query(Quarantine).filter(
            Quarantine.user_id == user_test.id).first()
        assert user_quar.in_observation == True

        assert user_logout_EP(test_client).status_code == 200
        assert user_logout_EP(test_client).status_code == 401
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 401

    # unregister a owner user
    with app.app_context():
        # check the presence of its restaurant and itself in the db
        owner_test = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert owner_test != None
        restaurant_test = db.session.query(Restaurant).filter(
            Restaurant.owner_id == owner_test.id).first()
        assert restaurant_test != None

        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 200
        assert test_client.delete('/delete_user',
                                  follow_redirects=True).status_code == 200
        del_inactive_users()

        # check the absence of its restaurant and itself in the db
        restaurant_test = db.session.query(Restaurant).filter(
            Restaurant.owner_id == owner_test.id).first()
        assert restaurant_test == None
        owner_test = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert owner_test == None

        assert user_logout_EP(test_client).status_code == 200
        assert user_logout_EP(test_client).status_code == 401
        assert user_login_EP(test_client, '*****@*****.**',
                             'passw').status_code == 401
예제 #13
0
def test_users_reservation(test_app):
    app, test_client = test_app

    # create customers
    for user in customers_example:
        create_user_EP(test_client, **user)

    # create restaurant owners
    for ro in restaurant_owner_example:
        create_user_EP(test_client, **ro)

    for usr_idx, restaurant in enumerate(restaurant_example):
        user_login_EP(test_client, restaurant_owner_example[usr_idx]['email'],
                      restaurant_owner_example[usr_idx]['password'])

        create_restaurant_EP(test_client, restaurant)

        user_logout_EP(test_client)

    restaurant_id = ['1', '2', '3', '4']

    reservation_date_str_dict = [
        reservation_dates_example[1] + " " + reservation_times_example[0],
        reservation_dates_example[7] + " " + reservation_times_example[3]
    ]

    guests_email_dict = dict()
    for i in range(reservation_guests_number_example[1]):
        key = 'guest' + str(i + 1)
        guests_email_dict[key] = reservation_guests_email_example[i]

    # log as customer 1
    user_login_EP(test_client, customers_example[0]['email'],
                  customers_example[0]['password'])

    # Customer1 reservation 1 in the past
    assert restaurant_reservation_POST_EP(test_client, restaurant_id[0], 1,
                                          reservation_date_str_dict[0],
                                          reservation_guests_number_example[1],
                                          guests_email_dict).status_code == 666

    # Customer1 reservation 2 in the future
    assert restaurant_reservation_POST_EP(test_client, restaurant_id[0], 1,
                                          reservation_date_str_dict[1],
                                          reservation_guests_number_example[1],
                                          guests_email_dict).status_code == 666

    assert test_client.get('/users/reservation_list',
                           follow_redirects=True).status_code == 200

    assert test_client.get('/users/editreservation/1',
                           follow_redirects=True).status_code == 200

    guests_email_dict['guest-0-email'] = "*****@*****.**"

    assert test_client.post('/users/editreservation/1',
                            data=guests_email_dict,
                            follow_redirects=True).status_code == 222

    # failure, there is no possibility to delete a past reservation
    assert test_client.get('/users/deletereservation/1',
                           follow_redirects=True).status_code == 403

    # failure, the reservation id with this user doesn't exist
    assert test_client.get('/users/editreservation/100',
                           follow_redirects=True).status_code == 404

    # failure, there is no possibility to delete a past reservation
    assert test_client.get('/users/deletereservation/2',
                           follow_redirects=True).status_code == 200

    insert_ha(db, app)

    assert user_logout_EP(test_client).status_code == 200

    assert user_login_EP(test_client, '*****@*****.**',
                         'ha').status_code == 200

    assert test_client.get('/users/reservation_list',
                           follow_redirects=True).status_code == 403

    assert test_client.get('/users/editreservation/1',
                           follow_redirects=True).status_code == 403

    assert test_client.get('/users/deletereservation/1',
                           follow_redirects=True).status_code == 403
예제 #14
0
def test_contact_tracing_health_authority(test_app):
    app, test_client = test_app

    # create a health authority and an user for testing
    temp_user_example_dict = customers_example[0]
    #assert create_user_EP(test_client, **temp_ha_dict).status_code == 200
    insert_ha(db, app)
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200
    temp_user_example_dict = customers_example[1]
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200

    # create a owner and login
    temp_owner_example_dict = restaurant_owner_example[0]
    assert create_user_EP(test_client,
                          **temp_owner_example_dict).status_code == 200
    assert user_login_EP(
        test_client, temp_owner_example_dict['email'],
        temp_owner_example_dict['password']).status_code == 200

    # create a restaurant
    temp_restaurant_example = restaurant_h24_example
    assert create_restaurant_EP(test_client,
                                temp_restaurant_example).status_code == 200

    restaurant = None
    with app.app_context():
        restaurant = db.session.query(Restaurant).filter(
            Restaurant.name == temp_restaurant_example['name']).first()
    assert restaurant is not None

    # login user
    user_logout_EP(test_client)
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         temp_user_example_dict['password']).status_code == 200

    # make reservation 1
    date = datetime.datetime.now() - timedelta(days=2)
    timestamp = date.strftime("%d/%m/%Y")
    assert restaurant_reservation_EP(test_client, restaurant.id, timestamp,
                                     '20:00', '2').status_code == 200

    reservation_date_str = timestamp + ' 20:00'
    assert restaurant_reservation_POST_EP(test_client, str(restaurant.id), '1',
                                          reservation_date_str, '2', {
                                              'guest1': '*****@*****.**'
                                          }).status_code == 666

    # confirm the guests
    with app.app_context():
        seats = db.session.query(Seat).all()
        for s in seats:
            s.confirmed = True
        db.session.commit()

    # make reservation 2
    date = datetime.datetime.now() + timedelta(days=2)
    timestamp = date.strftime("%d/%m/%Y")
    assert restaurant_reservation_EP(test_client, restaurant.id, timestamp,
                                     '20:00', '2').status_code == 200

    reservation_date_str = timestamp + ' 20:00'
    assert restaurant_reservation_POST_EP(test_client, str(restaurant.id), '1',
                                          reservation_date_str, '2', {
                                              'guest1': '*****@*****.**'
                                          }).status_code == 666

    # login ha
    user_logout_EP(test_client)
    assert user_login_EP(test_client, "*****@*****.**",
                         "ha").status_code == 200

    # mark positive
    assert mark_patient_as_positive(
        test_client, temp_user_example_dict['email']).status_code == 555

    # test notification
    with app.app_context():
        notifications = db.session.query(Notification).all()
        for n in notifications:
            print(n.message)
        assert len(notifications) == 3
예제 #15
0
def test_component_reviews(test_app):
    app, test_client = test_app

    assert create_user_EP(test_client,
                          **restaurant_owner_example[0]).status_code == 200
    assert user_login_EP(
        test_client, restaurant_owner_example[0]['email'],
        restaurant_owner_example[0]['password']).status_code == 200
    assert create_restaurant_EP(test_client).status_code == 200

    review = dict(rating=4,
                  comment='Good quality restaurant',
                  date=datetime.date.today())

    uncorrect_review = dict(rating=10,
                            comment='Good quality restaurant',
                            date=datetime.date.today())

    with app.app_context():
        # get a restaurant
        restaurant = db.session.query(Restaurant).filter_by(
            name=restaurant_example[0]['name']).first()
        #get a user, the owner
        user = db.session.query(User).filter_by(
            email=restaurant_owner_example[0]['email']).first()

    # try to get as a owner (555)
    assert test_client.get('/restaurants/reviews/' + str(restaurant.id),
                           follow_redirects=True).status_code == 555

    # try to review a place when i'm a owner (403)
    assert create_review_EP(test_client, review,
                            restaurant.id).status_code == 403

    # logout with the owner (200)
    assert test_client.get('/logout', follow_redirects=True).status_code == 200

    # create a customer
    assert create_user_EP(test_client,
                          **customers_example[0]).status_code == 200

    # login with the customer (200)
    assert user_login_EP(test_client, customers_example[0]['email'],
                         customers_example[0]['password']).status_code == 200

    # try to get as a customer without a reservation (555)
    assert test_client.get('/restaurants/reviews/' +
                           str(restaurant.id)).status_code == 555
    assert create_review_EP(test_client, review,
                            restaurant.id).status_code == 403

    # create a reservation in the future (200)
    assert restaurant_reservation_EP(
        test_client, restaurant.id, '10/10/2030', reservation_times_example[0],
        reservation_guests_number_example[0]).status_code == 200

    reservation_date_str = '10/10/2030' + " " + reservation_times_example[14]
    assert restaurant_reservation_POST_EP(
        test_client, str(restaurant.id), '8', reservation_date_str, '1',
        customers_example[0]['email']).status_code == 666

    # try to review when i'm not been there yet
    assert test_client.get('/restaurants/reviews/' +
                           str(restaurant.id)).status_code == 555

    # create a reservation in the past(200)
    assert restaurant_reservation_EP(
        test_client, restaurant.id, '10/10/2020', reservation_times_example[0],
        reservation_guests_number_example[0]).status_code == 200

    reservation_date_str = '10/10/2020' + " " + reservation_times_example[14]
    assert restaurant_reservation_POST_EP(
        test_client, str(restaurant.id), '8', reservation_date_str, '1',
        customers_example[0]['email']).status_code == 666

    # try to send an invalid form (400)
    assert create_review_EP(test_client, uncorrect_review,
                            restaurant.id).status_code == 400

    # try to get as a customer who has a reservation (200)
    assert test_client.get('/restaurants/reviews/' + str(restaurant.id),
                           follow_redirects=True).status_code == 200
    assert create_review_EP(test_client, review,
                            restaurant.id).status_code == 200
    assert test_client.get('/restaurants/like/' +
                           str(restaurant.id)).status_code == 200
    assert test_client.get('/restaurants/like/' +
                           str(restaurant.id)).status_code == 200

    assert test_client.get('/restaurants',
                           follow_redirects=True).status_code == 200

    # try to double review the same restaurant (403)
    assert create_review_EP(test_client, review,
                            restaurant.id).status_code == 403

    # logout
    assert test_client.get('/logout', follow_redirects=True).status_code == 200

    insert_ha(db, app)

    # login as health authority
    assert user_login_EP(test_client, "*****@*****.**",
                         "ha").status_code == 200

    # try to get as health authority (555)
    assert test_client.get('/restaurants/reviews/' +
                           str(restaurant.id)).status_code == 403
    assert test_client.get('/restaurants/' +
                           str(restaurant.id)).status_code == 403
    assert test_client.get('/restaurants/' + str(restaurant.id) +
                           '/reservation').status_code == 403
    assert test_client.get('/restaurants/like/' +
                           str(restaurant.id)).status_code == 403

    # try to post as health authority (403)
    assert create_review_EP(test_client, review,
                            restaurant.id).status_code == 403
예제 #16
0
def test_component_edit_restaurant(test_app):
    app, test_client = test_app

    fields = {
        'phone': '345',
        'dishes-0-dish_name': 'pizza',
        'dishes-0-price': 4,
        'dishes-0-ingredients': 'pomodoro'
    }

    unc_fields = {
        'phone': '',
        'dishes-0-dish_name': 'pizza',
        'dishes-0-price': 4,
        'dishes-0-ingredients': 'pomodoro'
    }

    assert test_client.get('/edit_restaurant_informations',
                           follow_redirects=True).status_code == 403

    insert_ha(db, app)
    assert user_login_EP(test_client,
                         email='*****@*****.**',
                         password='******').status_code == 200

    assert test_client.get('/edit_restaurant_informations',
                           follow_redirects=True).status_code == 403

    assert test_client.get('/logout', follow_redirects=True).status_code == 200

    assert create_user_EP(test_client,
                          email='*****@*****.**',
                          password='******',
                          role='owner').status_code == 200
    # --- COMPONENTS TESTS ---
    #---------------------------------------------------------- building the starting conditions
    # log the user
    assert user_login_EP(test_client,
                         email='*****@*****.**',
                         password='******').status_code == 200

    # create a restaurant by the logged user
    correct_restaurant = {
        'name': 'Trial01-EP',
        'lat': 22,
        'lon': 22,
        'phone': '3346734121',
        'cuisine_type': [Restaurant.CUISINE_TYPES(1)],
        'prec_measures': 'leggeX',
        'avg_time_of_stay': 30,
        'tables-0-table_name': 'yellow',
        'tables-0-capacity': 5,
        'dishes-0-dish_name': 'pizza',
        'dishes-0-price': 4,
        'dishes-0-ingredients': 'pomodoro',
        'workingdays-0-day': WorkingDay.WEEK_DAYS(1),
        'workingdays-0-work_shifts': "('12:00','15:00'),('19:00','23:00')"
    }

    # try owner with no restaurants
    assert test_client.get('/edit_restaurant_informations',
                           follow_redirects=True).status_code == 403

    assert create_restaurant_EP(test_client,
                                correct_restaurant).status_code == 200
    with app.app_context():
        user = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert user is not None
        restaurant = db.session.query(Restaurant).filter_by(
            owner_id=user.id).first()
        assert restaurant is not None

    # try owner get with success
    assert test_client.get('/edit_restaurant_informations',
                           follow_redirects=True).status_code == 200

    # get with success
    assert test_client.get('/edit_restaurant_informations/' +
                           str(restaurant.id),
                           follow_redirects=True).status_code == 200

    # try with wrong id
    assert test_client.post('/edit_restaurant_informations/' + str(10),
                            data=fields,
                            follow_redirects=True).status_code == 404

    # post with invalid form
    assert test_client.post('/edit_restaurant_informations/' +
                            str(restaurant.id),
                            data=unc_fields,
                            follow_redirects=True).status_code == 400

    # post with success
    assert test_client.post('/edit_restaurant_informations/' +
                            str(restaurant.id),
                            data=fields,
                            follow_redirects=True).status_code == 200

    # try to post with ha
    assert test_client.get('/logout', follow_redirects=True).status_code == 200
    assert user_login_EP(test_client,
                         email='*****@*****.**',
                         password='******').status_code == 200
    assert test_client.post('/edit_restaurant_informations/' +
                            str(restaurant.id),
                            data=fields,
                            follow_redirects=True).status_code == 403

    assert test_client.get('/logout', follow_redirects=True).status_code == 200

    # try without logged user
    assert test_client.post('/edit_restaurant_informations/' +
                            str(restaurant.id),
                            data=fields,
                            follow_redirects=True).status_code == 403
예제 #17
0
def test_component_health_authority(test_app):

    app, test_client = test_app

    # create a health authority and an user for testing
    temp_user_example_dict = customers_example[0]
    #assert create_user_EP(test_client, **temp_ha_dict).status_code == 200
    insert_ha(db, app)
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200
    temp_user_example_dict = customers_example[1]
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200

    # access to patient information is forbidden for customers
    user_login_EP(test_client, temp_user_example_dict['email'],
                  temp_user_example_dict['password'])

    result = test_client.get('/patient_informations', follow_redirects=True)
    assert result.status_code == 403

    test_client.get('/logout', follow_redirects=True)

    # access to health authority is allowed
    user_login_EP(test_client, "*****@*****.**", "ha")

    result = test_client.get('/patient_informations', follow_redirects=True)
    assert result.status_code == 200

    # wrong email must return patient not found
    result = test_client.post('/patient_informations',
                              data=dict(email="*****@*****.**"),
                              follow_redirects=True)
    assert result.status_code == 404

    # try to mark the health authority itself
    result = test_client.post('/patient_informations',
                              data=dict(email="*****@*****.**"),
                              follow_redirects=True)
    assert result.status_code == 403

    # correct email must returns the patient informations
    result = test_client.post('/patient_informations',
                              data=dict(email=temp_user_example_dict['email']),
                              follow_redirects=True)
    assert result.status_code == 200

    # patient 1 is marked as positive
    assert mark_patient_as_positive(
        test_client, customers_example[0]['email']).status_code == 555
    #result = test_client.post('/patient_informations?email=userexample1%40test.com', data=dict(mark_positive_button='mark_positive'), follow_redirects=True)
    #assert result.status_code == 555

    # patient 2 is marked as positive
    assert mark_patient_as_positive(
        test_client, customers_example[1]['email']).status_code == 555

    # a patient already marked will return a different html
    result = test_client.post('/patient_informations',
                              data=dict(email=temp_user_example_dict['email']),
                              follow_redirects=True)
    assert result.status_code == 200

    # go to the previous page when patient is already marked as positive
    result = test_client.get(
        '/patient_informations?email=userexample1%40test.com',
        data=dict(go_back_button='go_back'),
        follow_redirects=True)
    assert result.status_code == 200
예제 #18
0
def test_insert_notification(test_app):
    app, test_client = test_app

    # --- UNIT TESTS ---
    with app.app_context():
        # create a user to test notification insertions
        assert create_user_EP(
            test_client,
            email='*****@*****.**').status_code == 200
        user_test = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert user_test is not None

        # incorrect fields with validators
        incorrect_notifications = [
            dict(user_id=0,
                 email='*****@*****.**',
                 message='hello',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=-1,
                 email='*****@*****.**',
                 message='hello',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email=None,
                 message='hello',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='',
                 message='hello',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='wrongemail',
                 message='hello',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message=None,
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='hello',
                 pending=None,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='hello',
                 pending=True,
                 type_=None,
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='hello',
                 pending=True,
                 type_=1,
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='hello',
                 pending=True,
                 type_='hei',
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='hello',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=None)
        ]
        count_assert = 0
        for n in incorrect_notifications:
            try:
                notification = Notification(**n)
            except ValueError:
                count_assert += 1
                assert True
        assert len(incorrect_notifications) == count_assert

        # missing mandatory fields
        incorrect_notifications = [
            dict(message='hello',
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(email='*****@*****.**',
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(email='*****@*****.**',
                 message='hello',
                 date=datetime.date(2020, 10, 5)),
            dict(email='*****@*****.**',
                 message='hello',
                 type_=Notification.TYPE(1))
        ]
        count_assert = 0
        for n in incorrect_notifications:
            notification = Notification(**n)
            try:
                db.session.add(notification)
                db.session.commit()
            except (exc.IntegrityError, exc.InvalidRequestError):
                db.session.rollback()
                count_assert += 1
                assert True
        assert len(incorrect_notifications) == count_assert

        # correct notifications
        correct_notifications = [
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='hello',
                 pending=True,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(email='*****@*****.**',
                 message='hello',
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
            dict(user_id=user_test.id,
                 email='*****@*****.**',
                 message='h',
                 pending=False,
                 type_=Notification.TYPE(1),
                 date=datetime.date(2020, 10, 5)),
        ]
        for n in correct_notifications:
            notification = Notification(**n)
            db.session.add(notification)
            db.session.commit()
            notification_to_check = db.session.query(Notification).filter(
                Notification.id == notification.id).first()
            assert notification_to_check is not None
            check_notifications(notification_to_check, notification)

        # check total notifications
        notifications = db.session.query(Notification).all()
        assert len(notifications) == len(correct_notifications)
예제 #19
0
def create_app():
    app = Flask(__name__)
    app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY'
    app.config['SECRET_KEY'] = 'ANOTHER ONE'
    #app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@postgres:5432/postgres'
    app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URI']
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    
    
    # Flask-Mail configuration
    app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
    app.config['MAIL_PORT'] = 587
    app.config['MAIL_USE_TLS'] = True
    app.config['MAIL_USERNAME'] = '******'
    app.config['MAIL_PASSWORD'] = '******'
    app.config['MAIL_DEFAULT_SENDER'] = '*****@*****.**'
    for bp in blueprints:
        app.register_blueprint(bp)
        bp.app = app

    db.init_app(app)
    login_manager.init_app(app)
    try:
        db.create_all(app=app)
    except Exception as e:
        print(e)


    # TODO THIS SECTION MUST BE REMOVED, ONLY FOR DEMO
    # already tested EndPoints are used to create examples
    app.config['WTF_CSRF_ENABLED'] = False

    with app.app_context():
        
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        adm = q.first()
        if adm is None:
            try: 
                # create a first admin user 
                # test for a user defined in database.db
                example = User()
                example.email = '*****@*****.**'
                example.phone = '3333333333'
                example.firstname = 'Admin'
                example.lastname = 'Admin'
                example.set_password('admin')
                example.dateofbirth = datetime.date(2020, 10, 5)
                example.role = 'admin'           
                example.is_admin = True
                db.session.add(example)
                db.session.commit()

        

                test_client = app.test_client()

                insert_ha(db, app)
                
                for user in customers_example:
                    create_user_EP(test_client,**user)

                for user in restaurant_owner_example:
                    create_user_EP(test_client,**user)

                for usr_idx,restaurant in enumerate(restaurant_example):
                    user_login_EP(test_client, restaurant_owner_example[usr_idx]['email'], 
                                                restaurant_owner_example[usr_idx]['password'])

                    create_restaurant_EP(test_client,restaurant)

                    user_logout_EP(test_client)

            except Exception as e:
                print(e)

        

    app.config['WTF_CSRF_ENABLED'] = True

    

    return app
예제 #20
0
def test_component_home(test_app):
    app, test_client = test_app

    assert test_client.get('/', follow_redirects=True).status_code == 200

    # normal user
    assert create_user_EP(test_client).status_code == 200
    assert user_login_EP(test_client).status_code == 200
    assert test_client.get('/', follow_redirects=True).status_code == 200

    # admin
    assert test_client.get('/logout', follow_redirects=True)
    #assert create_user_EP(test_client, email='*****@*****.**', password='******',role='admin').status_code == 200
    insert_admin(db, app)
    assert user_login_EP(test_client, '*****@*****.**',
                         'admin').status_code == 200
    assert test_client.get('/', follow_redirects=True).status_code == 200

    # owner
    assert test_client.get('/logout', follow_redirects=True).status_code == 200
    assert create_user_EP(test_client,
                          email='*****@*****.**',
                          password='******',
                          role='owner').status_code == 200
    assert user_login_EP(test_client, '*****@*****.**',
                         'owner').status_code == 200
    assert test_client.get('/', follow_redirects=True).status_code == 200
    assert create_restaurant_EP(test_client).status_code == 200
    assert test_client.get('/', follow_redirects=True).status_code == 200

    # ha -- to test the whole home I have to make reservations and mark positives
    assert test_client.get('/logout', follow_redirects=True)
    insert_ha(db, app)
    temp_user_example_dict = customers_example[1]
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200

    # create a owner and login
    temp_owner_example_dict = restaurant_owner_example[0]
    assert create_user_EP(test_client,
                          **temp_owner_example_dict).status_code == 200
    assert user_login_EP(
        test_client, temp_owner_example_dict['email'],
        temp_owner_example_dict['password']).status_code == 200

    # create a restaurant
    temp_restaurant_example = restaurant_h24_example
    assert create_restaurant_EP(test_client,
                                temp_restaurant_example).status_code == 200

    restaurant = None
    with app.app_context():
        restaurant = db.session.query(Restaurant).filter(
            Restaurant.name == temp_restaurant_example['name']).first()
    assert restaurant is not None

    # login user
    user_logout_EP(test_client)
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         temp_user_example_dict['password']).status_code == 200

    # make a reservation 1
    date = datetime.datetime.now() - timedelta(days=2)
    timestamp = date.strftime("%d/%m/%Y")
    assert restaurant_reservation_EP(test_client, restaurant.id, timestamp,
                                     '20:00', '2').status_code == 200

    reservation_date_str = timestamp + ' 20:00'
    assert restaurant_reservation_POST_EP(test_client, str(restaurant.id), '1',
                                          reservation_date_str, '2', {
                                              'guest1': '*****@*****.**'
                                          }).status_code == 666

    # make a reservation 2
    date = datetime.datetime.now() - timedelta(days=1)
    timestamp = date.strftime("%d/%m/%Y")
    assert restaurant_reservation_EP(test_client, restaurant.id, timestamp,
                                     '20:00', '3').status_code == 200

    reservation_date_str = timestamp + ' 20:00'
    assert restaurant_reservation_POST_EP(
        test_client, str(restaurant.id), '1', reservation_date_str, '3', {
            'guest1': '*****@*****.**',
            'guest2': customers_example[0]['email']
        }).status_code == 666

    # make a reservation 3
    date = datetime.datetime.now() - timedelta(days=4)
    timestamp = date.strftime("%d/%m/%Y")
    assert restaurant_reservation_EP(test_client, restaurant.id, timestamp,
                                     '20:00', '3').status_code == 200

    reservation_date_str = timestamp + ' 20:00'
    assert restaurant_reservation_POST_EP(
        test_client, str(restaurant.id), '1', reservation_date_str, '3', {
            'guest1': '*****@*****.**',
            'guest2': customers_example[0]['email']
        }).status_code == 666

    # a fake notification with user_id not associated with a real user
    with app.app_context():
        new_notification = Notification()
        new_notification.user_id = 20
        new_notification.message = 'message ' + timestamp + ' blabla'
        new_notification.email = '*****@*****.**'
        new_notification.pending = True
        new_notification.type_ = Notification.TYPE(1)
        new_notification.date = datetime.date.today()
        db.session.add(new_notification)
        db.session.commit()

    # confirm the guests
    with app.app_context():
        seats = db.session.query(Seat).all()
        for s in seats:
            s.confirmed = True
        db.session.commit()

    # login ha
    user_logout_EP(test_client)
    assert user_login_EP(test_client, "*****@*****.**",
                         "ha").status_code == 200

    # mark positive
    assert mark_patient_as_positive(
        test_client, temp_user_example_dict['email']).status_code == 555

    # test home ha
    assert test_client.get('/', follow_redirects=True).status_code == 200
예제 #21
0
def test_create_user(test_app):
    app, test_client = test_app

    temp_user_example_dict = customers_example[0]

    # --- UNIT TESTS ---
    with app.app_context():
        # checking user that doesn't exist
        getuser = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert getuser is None

        # create a new user and check if he has been added
        new_user = populate_user()

        db.session.add(new_user)
        db.session.commit()

        getuser = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert getuser is not None
        assert getuser.email == '*****@*****.**'
        assert getuser.firstname == "firstname_test"
        assert getuser.lastname == "lastname_test"
        assert getuser.password == "passw"
        assert getuser.dateofbirth == datetime.date(2020, 10, 5)
        assert getuser.role == "customer"

        # setting a wrong email syntax
        count_assert = 0
        try:
            new_user.email = "newuserwrongemail"
        except SyntaxError:
            count_assert = 1
            assert True
        assert count_assert == 1

        count_assert = 0
        try:
            new_user.role = "norole"
        except SyntaxError:
            count_assert = 1
            assert True
        assert count_assert == 1

        # creation of a user with an already existing email must fail
        new_user_2 = populate_user()
        count_assert = 0
        try:
            db.session.add(new_user_2)
            db.session.commit()
        except exc.IntegrityError:
            count_assert = 1
            assert True
        assert count_assert == 1

    # --- COMPONENTS TESTS ---
    # get with the success
    assert test_client.get('/create_user').status_code == 200

    #create an admin (403)
    assert create_user_EP(test_client, role="admin").status_code == 403

    #create an user with success
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 200

    # creation of a user with an already existing email must fail
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 403

    # creation of a user with wrong email syntax
    temp_user_example_dict['email'] = 'newuserwrongemail'
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 400

    #assert test_client.get('/logout', follow_redirects=True).status_code == 200

    # creation of a user with an already existing email must fail (in this case user was added via db.commit)
    temp_user_example_dict['email'] = "*****@*****.**"
    assert create_user_EP(test_client,
                          **temp_user_example_dict).status_code == 403

    #assert test_client.get('/logout', follow_redirects=True).status_code == 200

    temp_user_example_dict['email'] = "*****@*****.**"
    assert user_login_EP(test_client, temp_user_example_dict['email'],
                         temp_user_example_dict['password']).status_code == 200

    assert test_client.get('/create_user').status_code == 403