예제 #1
0
def test_set_report(client):
    tested_app, app = client

    reply = tested_app.get('/settingreport')
    assert reply.status_code == 401

    create_user(tested_app,email='*****@*****.**', firstname='mariacristina', lastname='uccheddu', password='******',
                                              age=23,
                                              weight=70,
                                              max_hr=120,
                                              rest_hr=60,
                                              vo2max=99)

    reply = tested_app.get('/settingreport')
    assert reply.status_code == 401

    assert login(tested_app, '*****@*****.**', 'ciao').status_code == 200

    # now [email protected] is logged in
    with app.app_context():
        user = db.session.query(User).filter(User.email == '*****@*****.**').first()

        reply = tested_app.get('/settingreport')
        assert reply.status_code == 200

        reply = tested_app.post('/settingreport', data=dict(setting_mail='6'), follow_redirects=True)
        assert reply.status_code == 200
        mail = db.session.query(Report).filter(Report.runner_id == user.id).first()
        assert mail is not None
        assert mail.runner_id == user.id
        assert mail.choice_time == 21600.0

        reply = tested_app.post('/settingreport', data=dict(setting_mail='12'), follow_redirects=True)
        assert reply.status_code == 200
        mail = db.session.query(Report).filter(Report.runner_id == user.id).first()
        assert mail is not None
        assert mail.runner_id == user.id
        assert mail.choice_time == 43200.0

        reply = tested_app.post('/settingreport', data=dict(setting_mail='24'), follow_redirects=True)
        assert reply.status_code == 200
        mail = db.session.query(Report).filter(Report.runner_id == user.id).first()
        assert mail is not None
        assert mail.runner_id == user.id
        assert mail.choice_time == 86400.0

        reply = tested_app.post('/settingreport', data=dict(setting_mail=None), follow_redirects=True)
        reply.status_code == 400
예제 #2
0
def test_delete_user(client):
    tested_app, app = client

    reply = create_user(
        tested_app)  # creates a user with '*****@*****.**' as email, default
    assert reply.status_code == 200

    reply = login(tested_app, '*****@*****.**', '123456')
    assert reply.status_code == 200

    reply = logout(tested_app)
    assert reply.status_code == 200

    # retrieve delete_user page without logging in before
    reply = tested_app.get('/delete_user')
    assert reply.status_code == 401

    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 200

    with app.app_context():
        user = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        new_predefined_run(user)
        assert db.session.query(Run).filter(
            Run.runner_id == user.id).first() is not None
        new_objective(user)
        assert db.session.query(Objective).filter(
            Objective.runner_id == user.id).first() is not None
        new_report(user, 1, 1)  # the 2nd and 3rd params doesn't matter
        assert db.session.query(Report).filter(
            Report.runner_id == user.id).first() is not None

    # retrieve delete_user page
    reply = tested_app.get('/delete_user')
    assert reply.status_code == 200

    # post bad request
    reply = tested_app.post('/delete_user', data=None)
    assert reply.status_code == 400

    # post incorrect password
    reply = tested_app.post('/delete_user', data=dict(password='******'))
    assert reply.status_code == 401

    # post correct password and checking that the user has been deleted
    reply = tested_app.post('/delete_user',
                            data=dict(password='******'),
                            follow_redirects=True)
    assert reply.status_code == 200

    with app.app_context():
        assert db.session.query(Run).filter(
            Run.runner_id == user.id).first() is None
        assert db.session.query(User).filter(
            User.email == '*****@*****.**').first() is None
        assert db.session.query(Objective).filter(
            Objective.runner_id == user.id).first() is None
        assert db.session.query(Report).filter(
            Report.runner_id == user.id).first() is None
def test_statistics(client):
    tested_app, app = client

    reply = create_user(tested_app)
    assert reply.status_code == 200

    with app.app_context():
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()
        run1 = new_predefined_run(user)  # run with id 1
        run2 = new_predefined_run(user)  # run with id 2
        run3 = new_predefined_run(user)  # run with id 3
        run4 = new_predefined_run(user)  # run with id 4
        run5 = new_predefined_run(user)  # run with id 5

        reply = tested_app.get('/statistics')
        assert reply.status_code == 401

        assert login(tested_app, email='*****@*****.**',
                     password='******').status_code == 200

        reply = tested_app.get('/statistics')
        assert reply.status_code == 200

        # check the correctness of the fields
        assert get_element_by_id(str(run1.id),
                                 str(reply.data)) == str(run1.name)
        assert get_element_by_id(str(run2.id),
                                 str(reply.data)) == str(run2.name)
        assert get_element_by_id(str(run3.id),
                                 str(reply.data)) == str(run3.name)
        assert get_element_by_id(str(run4.id),
                                 str(reply.data)) == str(run4.name)
        assert get_element_by_id(str(run5.id),
                                 str(reply.data)) == str(run5.name)
예제 #4
0
def test_login(client):
    tested_app, app = client

    # creates '*****@*****.**' with psw '123456'
    assert create_user(tested_app).status_code == 200

    # in the db exists '*****@*****.**' and '*****@*****.**' (the admin)
    # try to login with incorrect mail
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 401

    # try to login with incorrect mail and incorrect password
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 401

    # try to login with incorrect password only
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 401

    # logging in correctly
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 200

    # cannot login if already logged in
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 403
def test_average_speed(client):
    tested_app, app = client

    # prepare the database creating a new user
    reply = create_user(
        tested_app)  # creates a user with '*****@*****.**' as email, default
    assert reply.status_code == 200

    # login as new user
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 200

    # retrieve the user object and login
    with app.app_context():
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()
        user.strava_token = "fake_token"
        db.session.commit()

    # The average speed should be 0 if there are no runs
    reply = tested_app.get('/')
    assert reply.status_code == 200
    assert get_element_by_id('total_average_speed', str(reply.data)) == str(0)

    # add a run
    with app.app_context():
        new_run(user)

    # retrieve the run
    with app.app_context():
        q = db.session.query(Run).filter(Run.id == 1)  # should be the first
        run1 = q.first()

    # with only a run the average speed should be the average speed of the run
    reply = tested_app.get('/')
    assert reply.status_code == 200
    assert get_element_by_id('total_average_speed', str(reply.data)) == str(
        round(run1.average_speed, 2))

    # add another run
    with app.app_context():
        new_run(user)

    # retrieve the runs list
    with app.app_context():
        runs = db.session.query(
            Run).filter()  # should be all owned by our user

    # with multiples runs the average speed should be the average of the average speed of each run
    total_average_speed = 0
    for run in runs:
        total_average_speed += run.average_speed
    total_average_speed /= runs.count()

    reply = tested_app.get('/')
    assert reply.status_code == 200
    assert get_element_by_id('total_average_speed', str(reply.data)) == str(
        round(total_average_speed, 2))
예제 #6
0
def test_create_user(client):
    tested_app, app = client

    assert tested_app.get('/create_user').status_code == 200

    assert tested_app.post('/create_user', data=None).status_code == 400

    reply = tested_app.post('/create_user',
                            data=dict(email='*****@*****.**',
                                      firstname='andrea',
                                      lastname='bongiorno',
                                      password='******',
                                      age=23,
                                      weight=70,
                                      max_hr=120,
                                      rest_hr=60,
                                      vo2max=99),
                            follow_redirects=True)

    assert reply.status_code == 200  # create_user success (it also redirect to login)

    assert login(tested_app, '*****@*****.**', '123456').status_code == 200

    # cannot create a user when already logged in
    assert create_user(tested_app).status_code == 403

    with app.app_context():
        user = db.session.query(User).filter(
            User.email == '*****@*****.**').first()
        assert user is not None
        assert user.email == '*****@*****.**'
        assert user.firstname == 'andrea'
        assert user.lastname == 'bongiorno'
        assert check_password_hash(user.password, '123456') is True
        assert user.age == 23
        assert user.weight == 70
        assert user.max_hr == 120
        assert user.rest_hr == 60
        assert user.vo2max == 99

    logout(tested_app)

    # cannot create a user with the same email
    reply = tested_app.post('/create_user',
                            data=dict(email='*****@*****.**',
                                      firstname='andrea',
                                      lastname='bongiorno',
                                      password='******',
                                      age=23,
                                      weight=70,
                                      max_hr=120,
                                      rest_hr=60,
                                      vo2max=99),
                            follow_redirects=False)
    assert reply.status_code == 409
예제 #7
0
def test_run(client):
    tested_app, app = client

    # prepare the database creating a new user
    reply = create_user(
        tested_app)  # creates a user with '*****@*****.**' as email, default
    assert reply.status_code == 200

    # login as new user
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 200

    # retrieve the user object from db
    with app.app_context():
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()

    # add the run
    with app.app_context():
        new_run(user)

    # retrieve the run
    with app.app_context():
        q = db.session.query(Run).filter(Run.id == 1)  # should be the first
        run = q.first()

    # retrieve run page
    reply = tested_app.get(
        '/run/1')  # should be one, because the database is empty
    assert reply.status_code == 200

    # check the correctness of the fields
    assert get_element_by_id('start_date',
                             str(reply.data)) == str(run.start_date)
    assert get_element_by_id('distance', str(reply.data)) == str(run.distance)
    assert get_element_by_id('elapsed_time',
                             str(reply.data)) == str(run.elapsed_time // 60)
    assert get_element_by_id('average_speed',
                             str(reply.data)) == str(run.average_speed)
    assert get_element_by_id('average_heartrate',
                             str(reply.data)) == str(run.average_heartrate)
    assert get_element_by_id('total_elevation_gain',
                             str(reply.data)) == str(run.total_elevation_gain)

    # retrieving not existing run
    reply = tested_app.get('/run/45678')
    assert reply.status_code == 404
예제 #8
0
def test_prepare_body(client):
    tested_app, app = client

    reply = create_user(tested_app)  # creates a user with '*****@*****.**' as email, default
    assert reply.status_code == 200

    reply = login(tested_app, '*****@*****.**', '123456')
    assert reply.status_code == 200

    with app.app_context():
        user = db.session.query(User).filter(User.email == '*****@*****.**').first()
        run = new_predefined_run(user)
        assert db.session.query(Run).filter(Run.runner_id == user.id).first() is not None

    assert prepare_body(user, app) == "name: Run 10\ndistance: 50000.0\nstart_date: "+str(run.start_date)+"\n" \
                                      "average_speed: 12.820512820512821\nelapsed_time: 3900.0\naverage_heartrate: None" \
                                      "\ntotal_elevation_gain: 3.0\n\n\n"
def test_create_objective(client):
    tested_app, app = client

    # test for create_objective having not logged in
    reply = tested_app.post('/create_objective')
    assert reply.status_code == 401

    # create a new user
    reply = create_user(tested_app)
    assert reply.status_code == 200

    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 200

    # test for create_objective logged in but without data
    reply = tested_app.post('/create_objective')
    assert reply.status_code == 400

    # test for create_objective logged in with wrong data
    reply = tested_app.post('/create_objective',
                            data=dict(start_date="asd", name="Wrong test1"))
    assert reply.status_code == 400

    reply = tested_app.post('/create_objective',
                            data=dict(end_date="asd", name="Wrong test2"))
    assert reply.status_code == 400

    reply = tested_app.post('/create_objective',
                            data=dict(target_distance=-20, name="Wrong test3"))
    assert reply.status_code == 400

    # end date before start date
    reply = tested_app.post('/create_objective',
                            data=dict(start_date="2018-07-04",
                                      end_date="2017-07-04",
                                      name="Wrong test3"))
    assert reply.status_code == 400

    # test for create_objective logged in with wrong data
    reply = tested_app.post('/create_objective',
                            data=dict(start_date="2018-07-04",
                                      end_date="2018-07-05",
                                      target_distance=22,
                                      name="Test OK"))
    assert reply.status_code == 200
예제 #10
0
def test_users_list(client):
    tested_app, app = client

    assert login(tested_app, email='*****@*****.**',
                 password='******').status_code == 200

    # only admin can get this page
    assert tested_app.get('/users').status_code == 200

    reply = logout(tested_app)
    assert reply.status_code == 200

    assert create_user(tested_app).status_code == 200

    assert login(tested_app, email='*****@*****.**',
                 password='******').status_code == 200

    # [email protected] is not admin
    assert tested_app.get('/users').status_code == 401
def test_runs_data(client):
    tested_app, app = client

    assert create_user(tested_app).status_code == 200

    # trying to retrieve data without logging in
    reply = tested_app.post('run/statistics',
                            data=json.dumps({
                                'runs': [1, 2, 3, 4, 5],
                                'params': [True, True, True]
                            }),
                            content_type='application/json')
    assert reply.status_code == 401

    assert login(tested_app, email='*****@*****.**',
                 password='******').status_code == 200

    # creating some fake runs
    with app.app_context():
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()
        run1 = new_predefined_run(user)  # run with id 1
        run2 = new_predefined_run(user)  # run with id 2
        run3 = new_predefined_run(user)  # run with id 3
        run4 = new_predefined_run(user)  # run with id 4
        run5 = new_predefined_run(user)  # run with id 5

    reply = tested_app.post('run/statistics',
                            data=json.dumps({
                                'runs': [1, 2, 3, 4, 5],
                                'params': [True, True, True]
                            }),
                            content_type='application/json')

    assert reply.status_code == 200
    body = json.loads(str(reply.data, 'utf8'))
    assert body == {
        '1': [12.820512820512821, 50000.0, 3900.0, 'Run 10'],
        '2': [12.820512820512821, 50000.0, 3900.0, 'Run 10'],
        '3': [12.820512820512821, 50000.0, 3900.0, 'Run 10'],
        '4': [12.820512820512821, 50000.0, 3900.0, 'Run 10'],
        '5': [12.820512820512821, 50000.0, 3900.0, 'Run 10']
    }
예제 #12
0
def test_logout(client):
    tested_app, app = client

    # logout without log in
    assert logout(tested_app).status_code == 401

    # creates '*****@*****.**' with psw '123456'
    assert create_user(tested_app).status_code == 200

    # in the db exists '*****@*****.**' and '*****@*****.**' (the admin)

    # logging in correctly
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 200

    # logout correctly
    assert logout(tested_app).status_code == 200

    # trying to access a page the require login
    assert tested_app.get('/delete_user').status_code == 401
def test_view_objectives(client):
    tested_app, app = client

    with app.app_context():
        # create a new user
        reply = create_user(tested_app)
        assert reply.status_code == 200

        reply = login(tested_app, email='*****@*****.**', password='******')
        assert reply.status_code == 200

        # retrieve the user object
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()

        # add the objective
        new_objective(user, name="Test1")
        new_objective(user, name="Test2")
        new_objective(user, name="Test3")
        new_objective(user, name="Test4")

        # retrieve the objective table
        objectives = db.session.query(Objective)

        reply = tested_app.get('objectives')
        assert reply.status_code == 200

        for o in objectives.all():
            assert get_element_by_id("objective_%s_name" % (o.id),
                                     str(reply.data)) == str(o.name)
            assert get_element_by_id("objective_%s_start_date" % (o.id),
                                     str(reply.data)) == str(o.start_date)
            assert get_element_by_id("objective_%s_end_date" % (o.id),
                                     str(reply.data)) == str(o.end_date)
            assert get_element_by_id("objective_%s_target_distance" % (o.id),
                                     str(reply.data)) == str(o.target_distance)
            assert get_element_by_id("objective_%s_completion" % (o.id),
                                     str(reply.data)) == str(o.completion)
def test_check_objective_completion(client):
    tested_app, app = client

    # create a new user
    reply = create_user(tested_app)
    assert reply.status_code == 200

    # login as new user
    reply = login(tested_app, email='*****@*****.**', password='******')
    assert reply.status_code == 200

    with app.app_context():
        # retrieve the user object and login
        user = db.session.query(User).filter(
            User.email == '*****@*****.**').first()

        # check an objective with completion = 0%
        objective_1 = new_objective(user,
                                    start_date=datetime.now(),
                                    end_date=datetime.now() +
                                    timedelta(days=7),
                                    target_distance=3000)
        assert objective_1.completion == 0

        # create some test runs
        new_run(user,
                distance=500,
                elapsed_time=1024,
                start_date=datetime.now())
        new_run(user,
                distance=10,
                elapsed_time=660,
                start_date=datetime.now() + timedelta(days=2))
        new_run(user,
                distance=30,
                elapsed_time=2048,
                start_date=datetime.now() + timedelta(days=4))

        # check an objective with completion 50%
        objective_2 = new_objective(user,
                                    start_date=datetime.now(),
                                    end_date=datetime.now() +
                                    timedelta(days=1),
                                    target_distance=1000)
        assert objective_2.completion == 50

        # check an objective with completion of 100%
        objective_3 = new_objective(user,
                                    start_date=datetime.now(),
                                    end_date=datetime.now() +
                                    timedelta(days=1),
                                    target_distance=400)
        assert objective_3.completion == 100

        # check an objective with completion of 100% with exactly the km needed to complete it
        objective_4 = new_objective(user,
                                    start_date=datetime.now(),
                                    end_date=datetime.now() +
                                    timedelta(days=5),
                                    target_distance=540)
        assert objective_4.completion == 100
def test_create_challenge(client):
    tested_app, app = client

    # test for create_challenge having not logged in
    reply = tested_app.get('/create_challenge')
    assert reply.status_code == 401

    # test for /challenge , list of all user challenges having not logged in
    reply = tested_app.get('/challenge')
    assert reply.status_code == 401

    # test for comparison of runs in a challenge having not logged in
    reply = tested_app.get('/challenge/1')
    assert reply.status_code == 401

    reply = create_user(tested_app,
                        email='*****@*****.**',
                        firstname='mariacristina',
                        lastname='uccheddu',
                        password='******',
                        age=23,
                        weight=70,
                        max_hr=120,
                        rest_hr=60,
                        vo2max=99)

    assert login(tested_app, '*****@*****.**', 'ciao').status_code == 200

    # test for comparison of runs in a challenge logged in but the challenge doesn't exists
    reply = tested_app.get('/challenge/1')
    assert reply.status_code == 404

    # test for /challenge , list of all user challenges having logged in , return the vision of an empty list, you can see only the title of the page
    reply = tested_app.get('/challenge')
    assert reply.status_code == 200

    reply = tested_app.post('create_challenge',
                            data=dict(run_one="0", run_two="0"))
    assert reply.status_code == 400

    with app.app_context():
        query = db.session.query(User).filter(
            User.email == '*****@*****.**')
        user = query.first()
        new_run(user)
        new_run(user)
        new_run(user)

        var = 0
        reply = tested_app.post('create_challenge',
                                data=dict(run_one="1", run_two="1"))
        assert reply.status_code == 400

        reply = tested_app.post('create_challenge',
                                data=dict(run_one="1", run_two="2"))
        assert reply.status_code == 200
        var = var + 1

        challenge = db.session.query(Challenge).filter(
            Challenge.id == var).first()
        assert challenge.run_one == 1
        assert challenge.run_two == 2

        reply = tested_app.post('create_challenge',
                                data=dict(run_one="1", run_two="3"))
        assert reply.status_code == 200
        var = var + 1

        challenge = db.session.query(Challenge).filter(
            Challenge.id == var).first()
        assert challenge.run_one == 1
        assert challenge.run_two == 3

        reply = tested_app.post('create_challenge',
                                data=dict(run_one="1", run_two="40"))
        assert reply.status_code == 400

        reply = tested_app.post('create_challenge',
                                data=dict(run_one="40", run_two="0"))
        assert reply.status_code == 400

        reply = tested_app.post('create_challenge',
                                data=dict(run_one="90", run_two="90"))
        assert reply.status_code == 400

        # test for /challenge , list of all user challenges having logged in , return the vision of an empty list, you can see only the title of the page
        reply = tested_app.get('/challenge')
        assert reply.status_code == 200

        reply = tested_app.get('/challenge/1')
        assert reply.status_code == 200

        reply = tested_app.get('/challenge/40')
        assert reply.status_code == 404