Example #1
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
Example #2
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
Example #3
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
Example #4
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
Example #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
Example #6
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
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
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'
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
Example #10
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
Example #11
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
Example #12
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
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
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
Example #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