From b73153ba21f9162ea3ca8bb1eb545ad1af4366a6 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 13 May 2018 18:36:31 +0200 Subject: [PATCH] API: tests refactor (fixtures) + fix --- mpwo_api/mpwo_api/activities/sports.py | 7 +- mpwo_api/mpwo_api/tests/conftest.py | 476 ++++++++++++++++++ .../tests/test_activities_api_0_get.py | 181 +++---- .../tests/test_activities_api_1_post.py | 163 +++--- .../tests/test_activities_api_2_patch.py | 150 ++---- .../tests/test_activities_api_3_delete.py | 41 +- .../mpwo_api/tests/test_activities_model.py | 34 +- mpwo_api/mpwo_api/tests/test_auth_api.py | 190 +++++-- mpwo_api/mpwo_api/tests/test_records_api.py | 45 +- mpwo_api/mpwo_api/tests/test_records_model.py | 182 +++---- mpwo_api/mpwo_api/tests/test_sports_api.py | 207 ++++++-- mpwo_api/mpwo_api/tests/test_sports_model.py | 26 +- mpwo_api/mpwo_api/tests/test_users_api.py | 22 +- mpwo_api/mpwo_api/tests/test_users_model.py | 18 +- mpwo_api/mpwo_api/tests/utils.py | 71 --- mpwo_api/mpwo_api/tests/utils_gpx.py | 231 --------- mpwo_api/mpwo_api/users/auth.py | 16 +- mpwo_api/mpwo_api/users/utils.py | 3 +- 18 files changed, 1116 insertions(+), 947 deletions(-) delete mode 100644 mpwo_api/mpwo_api/tests/utils.py delete mode 100644 mpwo_api/mpwo_api/tests/utils_gpx.py diff --git a/mpwo_api/mpwo_api/activities/sports.py b/mpwo_api/mpwo_api/activities/sports.py index 52cbacfd..8df42c59 100644 --- a/mpwo_api/mpwo_api/activities/sports.py +++ b/mpwo_api/mpwo_api/activities/sports.py @@ -61,19 +61,14 @@ def post_sport(auth_user_id): } return jsonify(response_object), 400 - sports_list = [] try: new_sport = Sport(label=sport_data.get('label')) db.session.add(new_sport) db.session.commit() - sports_list.append({ - 'id': new_sport.id, - 'label': new_sport.label - }) response_object = { 'status': 'created', 'data': { - 'sports': sports_list + 'sports': [new_sport.serialize()] } } code = 201 diff --git a/mpwo_api/mpwo_api/tests/conftest.py b/mpwo_api/mpwo_api/tests/conftest.py index 2f4f3506..fa0d50a6 100644 --- a/mpwo_api/mpwo_api/tests/conftest.py +++ b/mpwo_api/mpwo_api/tests/conftest.py @@ -1,7 +1,10 @@ +import datetime import os import pytest from mpwo_api import create_app, db +from mpwo_api.activities.models import Activity, Record, Sport +from mpwo_api.users.models import User os.environ["FLASK_ENV"] = 'testing' os.environ["APP_SETTINGS"] = 'mpwo_api.config.TestingConfig' @@ -16,3 +19,476 @@ def app(): db.session.remove() db.drop_all() return app + + +@pytest.fixture() +def user_1(): + user = User(username='test', email='test@test.com', password='12345678') + db.session.add(user) + db.session.commit() + return user + + +@pytest.fixture() +def user_1_admin(): + admin = User( + username='admin', + email='admin@example.com', + password='12345678' + ) + admin.admin = True + db.session.add(admin) + db.session.commit() + return admin + + +@pytest.fixture() +def user_1_full(): + user = User(username='test', email='test@test.com', password='12345678') + user.first_name = 'John' + user.last_name = 'Doe' + user.bio = 'just a random guy' + user.location = 'somewhere' + user.birth_date = datetime.datetime.strptime('01/01/1980', '%d/%m/%Y') + db.session.add(user) + db.session.commit() + return user + + +@pytest.fixture() +def user_2(): + user = User(username='toto', email='toto@toto.com', password='87654321') + db.session.add(user) + db.session.commit() + return user + + +@pytest.fixture() +def sport_1_cycling(): + sport = Sport(label='Cycling') + db.session.add(sport) + db.session.commit() + return sport + + +@pytest.fixture() +def sport_2_running(): + sport = Sport(label='Running') + db.session.add(sport) + db.session.commit() + return sport + + +@pytest.fixture() +def activity_cycling_user_1(): + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), + distance=10, + duration=datetime.timedelta(seconds=1024) + ) + db.session.add(activity) + db.session.commit() + return activity + + +@pytest.fixture() +def activity_running_user_1(): + activity = Activity( + user_id=1, + sport_id=2, + activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'), + distance=12, + duration=datetime.timedelta(seconds=6000) + ) + db.session.add(activity) + db.session.commit() + return activity + + +@pytest.fixture() +def seven_activities_user_1(): + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), + distance=10, + duration=datetime.timedelta(seconds=1024) + ) + db.session.add(activity) + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'), + distance=8, + duration=datetime.timedelta(seconds=6000) + ) + db.session.add(activity) + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('20/03/2017', '%d/%m/%Y'), + distance=5, + duration=datetime.timedelta(seconds=1024) + ) + db.session.add(activity) + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('09/05/2018', '%d/%m/%Y'), + distance=10, + duration=datetime.timedelta(seconds=3000) + ) + db.session.add(activity) + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('01/06/2017', '%d/%m/%Y'), + distance=10, + duration=datetime.timedelta(seconds=3456) + ) + db.session.add(activity) + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('23/02/2018', '%d/%m/%Y'), + distance=1, + duration=datetime.timedelta(seconds=600) + ) + db.session.add(activity) + activity = Activity( + user_id=1, + sport_id=1, + activity_date=datetime.datetime.strptime('23/02/2018', '%d/%m/%Y'), + distance=10, + duration=datetime.timedelta(seconds=1000) + ) + db.session.add(activity) + db.session.commit() + return activity + + +@pytest.fixture() +def activity_cycling_user_2(): + activity = Activity( + user_id=2, + sport_id=1, + activity_date=datetime.datetime.strptime('23/01/2018', '%d/%m/%Y'), + distance=15, + duration=datetime.timedelta(seconds=3600) + ) + db.session.add(activity) + db.session.commit() + return activity + + +@pytest.fixture() +def record_as(activity_cycling_user_1): + record = Record( + user_id=1, + sport_id=1, + activity=activity_cycling_user_1, + record_type='AS') + db.session.add(record) + db.session.commit() + return record + + +@pytest.fixture() +def record_fd(activity_cycling_user_1): + record = Record( + user_id=1, + sport_id=1, + activity=activity_cycling_user_1, + record_type='FD') + db.session.add(record) + db.session.commit() + return record + + +@pytest.fixture() +def record_fd_running(activity_running_user_1): + record = Record( + user_id=1, + sport_id=2, + activity=activity_running_user_1, + record_type='FD') + db.session.add(record) + db.session.commit() + return record + + +@pytest.fixture() +def record_ld(activity_cycling_user_1): + record = Record( + user_id=1, + sport_id=1, + activity=activity_cycling_user_1, + record_type='LD') + db.session.add(record) + db.session.commit() + return record + + +@pytest.fixture() +def record_ms(activity_cycling_user_1): + record = Record( + user_id=1, + sport_id=1, + activity=activity_cycling_user_1, + record_type='MS') + db.session.add(record) + db.session.commit() + return record + + +@pytest.fixture() +def record_ms_user_2_cycling(activity_cycling_user_2): + record = Record( + user_id=2, + sport_id=1, + activity=activity_cycling_user_2, + record_type='MS') + db.session.add(record) + db.session.commit() + return record + + +@pytest.fixture() +def gpx_file(): + return ( + '' + '' # noqa + ' ' + ' ' + ' just an activity' + ' ' + ' ' + ' 998' + ' ' + ' ' + ' ' + ' 998' + ' ' + ' ' + ' ' + ' 994' + ' ' + ' ' + ' ' + ' 994' + ' ' + ' ' + ' ' + ' 994' + ' ' + ' ' + ' ' + ' 993' + ' ' + ' ' + ' ' + ' 992' + ' ' + ' ' + ' ' + ' 992' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 986' + ' ' + ' ' + ' ' + ' 986' + ' ' + ' ' + ' ' + ' 986' + ' ' + ' ' + ' ' + ' 985' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 979' + ' ' + ' ' + ' ' + ' 981' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 979' + ' ' + ' ' + ' ' + ' 979' + ' ' + ' ' + ' ' + ' 975' + ' ' + ' ' + ' ' + ' ' + '' + ) + + +@pytest.fixture() +def gpx_file_wo_name(): + return ( + '' + '' # noqa + ' ' + ' ' + ' ' + ' ' + ' 998' + ' ' + ' ' + ' ' + ' 998' + ' ' + ' ' + ' ' + ' 994' + ' ' + ' ' + ' ' + ' 994' + ' ' + ' ' + ' ' + ' 994' + ' ' + ' ' + ' ' + ' 993' + ' ' + ' ' + ' ' + ' 992' + ' ' + ' ' + ' ' + ' 992' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 987' + ' ' + ' ' + ' ' + ' 986' + ' ' + ' ' + ' ' + ' 986' + ' ' + ' ' + ' ' + ' 986' + ' ' + ' ' + ' ' + ' 985' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 979' + ' ' + ' ' + ' ' + ' 981' + ' ' + ' ' + ' ' + ' 980' + ' ' + ' ' + ' ' + ' 979' + ' ' + ' ' + ' ' + ' 979' + ' ' + ' ' + ' ' + ' 975' + ' ' + ' ' + ' ' + ' ' + '' + ) + + +@pytest.fixture() +def gpx_file_wo_track(): + return ( + '' + '' # noqa + ' ' + '' + ) diff --git a/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py b/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py index 22eda9f5..938ee0b5 100644 --- a/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py +++ b/mpwo_api/mpwo_api/tests/test_activities_api_0_get.py @@ -1,35 +1,10 @@ -import datetime import json -from mpwo_api.tests.utils import add_activity, add_sport, add_user - -def test_get_all_activities_for_authenticated_user(app): - add_user('test', 'test@test.com', '12345678') - add_user('toto', 'toto@toto.com', '12345678') - add_sport('cycling') - add_sport('running') - add_activity( - user_id=1, - sport_id=2, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - add_activity( - user_id=2, - sport_id=1, - activity_date=datetime.datetime.strptime('23/01/2018', '%d/%m/%Y'), - distance=15, - duration=datetime.timedelta(seconds=3600), - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'), - distance=12, - duration=datetime.timedelta(seconds=6000) - ) +def test_get_all_activities_for_authenticated_user( + app, user_1, user_2, sport_1_cycling, sport_2_running, + activity_cycling_user_1, activity_cycling_user_2, activity_running_user_1, +): client = app.test_client() resp_login = client.post( @@ -57,44 +32,28 @@ def test_get_all_activities_for_authenticated_user(app): assert 'creation_date' in data['data']['activities'][0] assert 'Sun, 01 Apr 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa assert 1 == data['data']['activities'][0]['user_id'] - assert 1 == data['data']['activities'][0]['sport_id'] + assert 2 == data['data']['activities'][0]['sport_id'] assert 12.0 == data['data']['activities'][0]['distance'] assert '1:40:00' == data['data']['activities'][0]['duration'] assert 'creation_date' in data['data']['activities'][1] assert 'Mon, 01 Jan 2018 00:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa assert 1 == data['data']['activities'][1]['user_id'] - assert 2 == data['data']['activities'][1]['sport_id'] + assert 1 == data['data']['activities'][1]['sport_id'] assert 10.0 == data['data']['activities'][1]['distance'] assert '0:17:04' == data['data']['activities'][1]['duration'] -def test_get_activities_for_authenticated_user_no_activity(app): - add_user('test', 'test@test.com', '12345678') - add_user('toto', 'toto@toto.com', '12345678') - add_sport('cycling') - add_sport('running') - add_activity( - user_id=1, - sport_id=2, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'), - distance=12, - duration=datetime.timedelta(seconds=6000) - ) - +def test_get_activities_for_authenticated_user_no_activity( + app, user_1, user_2, sport_1_cycling, sport_2_running, + activity_cycling_user_1, activity_running_user_1, +): client = app.test_client() resp_login = client.post( '/api/auth/login', data=json.dumps(dict( email='toto@toto.com', - password='12345678' + password='87654321' )), content_type='application/json' ) @@ -118,64 +77,14 @@ def test_get_activities_for_authenticated_user_no_authentication(app): response = client.get('/api/activities') data = json.loads(response.data.decode()) - assert response.status_code == 403 + assert response.status_code == 401 assert 'error' in data['status'] assert 'Provide a valid auth token.' in data['message'] -def test_get_activities_pagination(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'), - distance=8, - duration=datetime.timedelta(seconds=6000) - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('20/03/2017', '%d/%m/%Y'), - distance=5, - duration=datetime.timedelta(seconds=1024) - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('09/05/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=3000) - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/06/2017', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=3456) - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('23/02/2018', '%d/%m/%Y'), - distance=1, - duration=datetime.timedelta(seconds=600) - ) - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('23/02/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1000) - ) - +def test_get_activities_pagination( + app, user_1, sport_1_cycling, seven_activities_user_1 +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -258,3 +167,63 @@ def test_get_activities_pagination(app): assert response.status_code == 200 assert 'success' in data['status'] assert len(data['data']['activities']) == 0 + + +def test_get_an_activity( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): + + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='test@test.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.get( + '/api/activities/1', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 200 + assert 'success' in data['status'] + assert len(data['data']['activities']) == 1 + + assert 'creation_date' in data['data']['activities'][0] + assert 'Mon, 01 Jan 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa + assert 1 == data['data']['activities'][0]['user_id'] + assert 1 == data['data']['activities'][0]['sport_id'] + assert 10.0 == data['data']['activities'][0]['distance'] + assert '0:17:04' == data['data']['activities'][0]['duration'] + + +def test_get_an_activity_invalid_id(app, user_1): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='test@test.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.get( + '/api/activities/11', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 404 + assert 'not found' in data['status'] + assert len(data['data']['activities']) == 0 diff --git a/mpwo_api/mpwo_api/tests/test_activities_api_1_post.py b/mpwo_api/mpwo_api/tests/test_activities_api_1_post.py index 67198c18..4fb69a3c 100644 --- a/mpwo_api/mpwo_api/tests/test_activities_api_1_post.py +++ b/mpwo_api/mpwo_api/tests/test_activities_api_1_post.py @@ -1,14 +1,46 @@ import json from io import BytesIO -from mpwo_api.tests.utils import add_sport, add_user -from mpwo_api.tests.utils_gpx import gpx_file + +def assert_activity_data_with_gpx(data): + assert 'creation_date' in data['data']['activities'][0] + assert 'Tue, 13 Mar 2018 12:44:45 GMT' == data['data']['activities'][0]['activity_date'] # noqa + assert 1 == data['data']['activities'][0]['user_id'] + assert 1 == data['data']['activities'][0]['sport_id'] + assert '0:04:10' == data['data']['activities'][0]['duration'] + assert 'just an activity' == data['data']['activities'][0]['title'] + assert data['data']['activities'][0]['ascent'] == 0.4 + assert data['data']['activities'][0]['ave_speed'] == 4.6 + assert data['data']['activities'][0]['descent'] == 23.4 + assert data['data']['activities'][0]['distance'] == 0.32 + assert data['data']['activities'][0]['max_alt'] == 998.0 + assert data['data']['activities'][0]['max_speed'] == 5.09 + assert data['data']['activities'][0]['min_alt'] == 975.0 + assert data['data']['activities'][0]['moving'] == '0:04:10' + assert data['data']['activities'][0]['pauses'] is None + assert data['data']['activities'][0]['with_gpx'] is True -def test_add_an_activity_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') +def assert_activity_data_wo_gpx(data): + assert 'creation_date' in data['data']['activities'][0] + assert data['data']['activities'][0]['activity_date'] == 'Tue, 15 May 2018 14:05:00 GMT' # noqa + assert data['data']['activities'][0]['user_id'] == 1 + assert data['data']['activities'][0]['sport_id'] == 1 + assert data['data']['activities'][0]['duration'] == '1:00:00' + assert data['data']['activities'][0]['title'] == 'Cycling - 2018-05-15 14:05:00' # noqa + assert data['data']['activities'][0]['ascent'] is None + assert data['data']['activities'][0]['ave_speed'] == 10.0 + assert data['data']['activities'][0]['descent'] is None + assert data['data']['activities'][0]['distance'] == 10.0 + assert data['data']['activities'][0]['max_alt'] is None + assert data['data']['activities'][0]['max_speed'] == 10.0 + assert data['data']['activities'][0]['min_alt'] is None + assert data['data']['activities'][0]['moving'] == '1:00:00' + assert data['data']['activities'][0]['pauses'] is None + assert data['data']['activities'][0]['with_gpx'] is False + +def test_add_an_activity_gpx(app, user_1, sport_1_cycling, gpx_file): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -34,30 +66,11 @@ def test_add_an_activity_gpx(app): data = json.loads(response.data.decode()) assert response.status_code == 201 - assert 'created' in data['status'] assert len(data['data']['activities']) == 1 - assert 'creation_date' in data['data']['activities'][0] - assert 'Tue, 13 Mar 2018 12:44:45 GMT' == data['data']['activities'][0]['activity_date'] # noqa - assert 1 == data['data']['activities'][0]['user_id'] - assert 1 == data['data']['activities'][0]['sport_id'] - assert '0:04:10' == data['data']['activities'][0]['duration'] - assert 'just an activity' == data['data']['activities'][0]['title'] - assert data['data']['activities'][0]['ascent'] == 0.4 - assert data['data']['activities'][0]['ave_speed'] == 4.6 - assert data['data']['activities'][0]['descent'] == 23.4 - assert data['data']['activities'][0]['distance'] == 0.32 - assert data['data']['activities'][0]['max_alt'] == 998.0 - assert data['data']['activities'][0]['max_speed'] == 5.09 - assert data['data']['activities'][0]['min_alt'] == 975.0 - assert data['data']['activities'][0]['moving'] == '0:04:10' - assert data['data']['activities'][0]['pauses'] is None - assert data['data']['activities'][0]['with_gpx'] is True + assert_activity_data_with_gpx(data) -def test_get_an_activity_with_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_get_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -88,34 +101,17 @@ def test_get_an_activity_with_gpx(app): )['auth_token'] ) ) - data = json.loads(response.data.decode()) assert response.status_code == 200 assert 'success' in data['status'] assert len(data['data']['activities']) == 1 - assert 'creation_date' in data['data']['activities'][0] - assert 'Tue, 13 Mar 2018 12:44:45 GMT' == data['data']['activities'][0]['activity_date'] # noqa - assert 1 == data['data']['activities'][0]['user_id'] - assert 1 == data['data']['activities'][0]['sport_id'] - assert '0:04:10' == data['data']['activities'][0]['duration'] - assert 'just an activity' == data['data']['activities'][0]['title'] - assert data['data']['activities'][0]['ascent'] == 0.4 - assert data['data']['activities'][0]['ave_speed'] == 4.6 - assert data['data']['activities'][0]['descent'] == 23.4 - assert data['data']['activities'][0]['distance'] == 0.32 - assert data['data']['activities'][0]['max_alt'] == 998.0 - assert data['data']['activities'][0]['max_speed'] == 5.09 - assert data['data']['activities'][0]['min_alt'] == 975.0 - assert data['data']['activities'][0]['moving'] == '0:04:10' - assert data['data']['activities'][0]['pauses'] is None - assert data['data']['activities'][0]['with_gpx'] is True + assert_activity_data_with_gpx(data) -def test_add_an_activity_gpx_invalid_file(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_add_an_activity_gpx_invalid_file( + app, user_1, sport_1_cycling, gpx_file +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -145,10 +141,9 @@ def test_add_an_activity_gpx_invalid_file(app): assert data['message'] == 'File extension not allowed.' -def test_add_an_activity_gpx_no_sport_id(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_add_an_activity_gpx_no_sport_id( + app, user_1, sport_1_cycling, gpx_file +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -178,10 +173,9 @@ def test_add_an_activity_gpx_no_sport_id(app): assert data['message'] == 'Invalid payload.' -def test_add_an_activity_gpx_incorrect_sport_id(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_add_an_activity_gpx_incorrect_sport_id( + app, user_1, sport_1_cycling, gpx_file +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -212,10 +206,7 @@ def test_add_an_activity_gpx_incorrect_sport_id(app): 'Error during activity file save.' -def test_add_an_activity_gpx_no_file(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_add_an_activity_gpx_no_file(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -244,10 +235,7 @@ def test_add_an_activity_gpx_no_file(app): assert data['message'] == 'No file part.' -def test_add_an_activity_no_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_add_an_activity_no_gpx(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -277,28 +265,10 @@ def test_add_an_activity_no_gpx(app): assert response.status_code == 201 assert 'created' in data['status'] assert len(data['data']['activities']) == 1 - assert 'creation_date' in data['data']['activities'][0] - assert data['data']['activities'][0]['activity_date'] == 'Tue, 15 May 2018 14:05:00 GMT' # noqa - assert data['data']['activities'][0]['user_id'] == 1 - assert data['data']['activities'][0]['sport_id'] == 1 - assert data['data']['activities'][0]['duration'] == '1:00:00' - assert data['data']['activities'][0]['title'] == 'cycling - 2018-05-15 14:05:00' # noqa - assert data['data']['activities'][0]['ascent'] is None - assert data['data']['activities'][0]['ave_speed'] == 10.0 - assert data['data']['activities'][0]['descent'] is None - assert data['data']['activities'][0]['distance'] == 10.0 - assert data['data']['activities'][0]['max_alt'] is None - assert data['data']['activities'][0]['max_speed'] == 10.0 - assert data['data']['activities'][0]['min_alt'] is None - assert data['data']['activities'][0]['moving'] == '1:00:00' - assert data['data']['activities'][0]['pauses'] is None - assert data['data']['activities'][0]['with_gpx'] is False + assert_activity_data_wo_gpx(data) -def test_get_an_activity_wo_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_get_an_activity_wo_gpx(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -336,28 +306,10 @@ def test_get_an_activity_wo_gpx(app): assert response.status_code == 200 assert 'success' in data['status'] assert len(data['data']['activities']) == 1 - assert 'creation_date' in data['data']['activities'][0] - assert data['data']['activities'][0]['activity_date'] == 'Tue, 15 May 2018 14:05:00 GMT' # noqa - assert data['data']['activities'][0]['user_id'] == 1 - assert data['data']['activities'][0]['sport_id'] == 1 - assert data['data']['activities'][0]['duration'] == '1:00:00' - assert data['data']['activities'][0]['title'] == 'cycling - 2018-05-15 14:05:00' # noqa - assert data['data']['activities'][0]['ascent'] is None - assert data['data']['activities'][0]['ave_speed'] == 10.0 - assert data['data']['activities'][0]['descent'] is None - assert data['data']['activities'][0]['distance'] == 10.0 - assert data['data']['activities'][0]['max_alt'] is None - assert data['data']['activities'][0]['max_speed'] == 10.0 - assert data['data']['activities'][0]['min_alt'] is None - assert data['data']['activities'][0]['moving'] == '1:00:00' - assert data['data']['activities'][0]['pauses'] is None - assert data['data']['activities'][0]['with_gpx'] is False + assert_activity_data_wo_gpx(data) -def test_add_an_activity_no_gpx_invalid_payload(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_add_an_activity_no_gpx_invalid_payload(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -388,10 +340,7 @@ def test_add_an_activity_no_gpx_invalid_payload(app): assert 'Invalid payload.' in data['message'] -def test_add_an_activity_no_gpx_error(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_add_an_activity_no_gpx_error(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', diff --git a/mpwo_api/mpwo_api/tests/test_activities_api_2_patch.py b/mpwo_api/mpwo_api/tests/test_activities_api_2_patch.py index 3e0be5b5..b02d56ba 100644 --- a/mpwo_api/mpwo_api/tests/test_activities_api_2_patch.py +++ b/mpwo_api/mpwo_api/tests/test_activities_api_2_patch.py @@ -1,16 +1,27 @@ -import datetime import json from io import BytesIO -from mpwo_api.tests.utils import add_activity, add_sport, add_user -from mpwo_api.tests.utils_gpx import gpx_file + +def assert_activity_data_with_gpx(data): + assert 'creation_date' in data['data']['activities'][0] + assert 'Tue, 13 Mar 2018 12:44:45 GMT' == data['data']['activities'][0]['activity_date'] # noqa + assert 1 == data['data']['activities'][0]['user_id'] + assert '0:04:10' == data['data']['activities'][0]['duration'] + assert data['data']['activities'][0]['ascent'] == 0.4 + assert data['data']['activities'][0]['ave_speed'] == 4.6 + assert data['data']['activities'][0]['descent'] == 23.4 + assert data['data']['activities'][0]['distance'] == 0.32 + assert data['data']['activities'][0]['max_alt'] == 998.0 + assert data['data']['activities'][0]['max_speed'] == 5.09 + assert data['data']['activities'][0]['min_alt'] == 975.0 + assert data['data']['activities'][0]['moving'] == '0:04:10' + assert data['data']['activities'][0]['pauses'] is None + assert data['data']['activities'][0]['with_gpx'] is True -def test_edit_an_activity_with_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_sport('running') - +def test_edit_an_activity_with_gpx( + app, user_1, sport_1_cycling, sport_2_running, gpx_file +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -46,36 +57,19 @@ def test_edit_an_activity_with_gpx(app): )['auth_token'] ) ) - data = json.loads(response.data.decode()) assert response.status_code == 200 assert 'success' in data['status'] assert len(data['data']['activities']) == 1 - - assert 'creation_date' in data['data']['activities'][0] - assert 'Tue, 13 Mar 2018 12:44:45 GMT' == data['data']['activities'][0]['activity_date'] # noqa - assert 1 == data['data']['activities'][0]['user_id'] assert 2 == data['data']['activities'][0]['sport_id'] - assert '0:04:10' == data['data']['activities'][0]['duration'] assert data['data']['activities'][0]['title'] == 'Activity test' - assert data['data']['activities'][0]['ascent'] == 0.4 - assert data['data']['activities'][0]['ave_speed'] == 4.6 - assert data['data']['activities'][0]['descent'] == 23.4 - assert data['data']['activities'][0]['distance'] == 0.32 - assert data['data']['activities'][0]['max_alt'] == 998.0 - assert data['data']['activities'][0]['max_speed'] == 5.09 - assert data['data']['activities'][0]['min_alt'] == 975.0 - assert data['data']['activities'][0]['moving'] == '0:04:10' - assert data['data']['activities'][0]['pauses'] is None - assert data['data']['activities'][0]['with_gpx'] is True + assert_activity_data_with_gpx(data) -def test_edit_an_activity_with_gpx_partial(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_sport('running') - +def test_edit_an_activity_with_gpx_partial( + app, user_1, sport_1_cycling, sport_2_running, gpx_file +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -110,36 +104,19 @@ def test_edit_an_activity_with_gpx_partial(app): )['auth_token'] ) ) - data = json.loads(response.data.decode()) assert response.status_code == 200 assert 'success' in data['status'] assert len(data['data']['activities']) == 1 - - assert 'creation_date' in data['data']['activities'][0] - assert 'Tue, 13 Mar 2018 12:44:45 GMT' == data['data']['activities'][0]['activity_date'] # noqa - assert 1 == data['data']['activities'][0]['user_id'] assert 2 == data['data']['activities'][0]['sport_id'] - assert '0:04:10' == data['data']['activities'][0]['duration'] assert data['data']['activities'][0]['title'] == 'just an activity' - assert data['data']['activities'][0]['ascent'] == 0.4 - assert data['data']['activities'][0]['ave_speed'] == 4.6 - assert data['data']['activities'][0]['descent'] == 23.4 - assert data['data']['activities'][0]['distance'] == 0.32 - assert data['data']['activities'][0]['max_alt'] == 998.0 - assert data['data']['activities'][0]['max_speed'] == 5.09 - assert data['data']['activities'][0]['min_alt'] == 975.0 - assert data['data']['activities'][0]['moving'] == '0:04:10' - assert data['data']['activities'][0]['pauses'] is None - assert data['data']['activities'][0]['with_gpx'] is True + assert_activity_data_with_gpx(data) -def test_edit_an_activity_with_gpx_invalid_payload(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_sport('running') - +def test_edit_an_activity_with_gpx_invalid_payload( + app, user_1, sport_1_cycling, gpx_file +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -180,10 +157,9 @@ def test_edit_an_activity_with_gpx_invalid_payload(app): assert 'Invalid payload.' in data['message'] -def test_edit_an_activity_with_gpx_incorrect_data(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_edit_an_activity_with_gpx_incorrect_data( + app, user_1, sport_1_cycling, gpx_file +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -226,17 +202,10 @@ def test_edit_an_activity_with_gpx_incorrect_data(app): assert 'Error. Please try again or contact the administrator.' in data['message'] # noqa -def test_edit_an_activity_wo_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - activity.title = 'cycling - 2018-01-01 00:00:00' +def test_edit_an_activity_wo_gpx( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): + activity_cycling_user_1.title = 'cycling - 2018-01-01 00:00:00' client = app.test_client() resp_login = client.post( @@ -263,7 +232,6 @@ def test_edit_an_activity_wo_gpx(app): )['auth_token'] ) ) - data = json.loads(response.data.decode()) assert response.status_code == 200 @@ -287,17 +255,10 @@ def test_edit_an_activity_wo_gpx(app): assert data['data']['activities'][0]['with_gpx'] is False -def test_edit_an_activity_wo_gpx_partial(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - activity.title = 'cycling - 2018-01-01 00:00:00' +def test_edit_an_activity_wo_gpx_partial( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): + activity_cycling_user_1.title = 'cycling - 2018-01-01 00:00:00' client = app.test_client() resp_login = client.post( @@ -345,17 +306,9 @@ def test_edit_an_activity_wo_gpx_partial(app): assert data['data']['activities'][0]['with_gpx'] is False -def test_edit_an_activity_wo_gpx_invalid_payload(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - +def test_edit_an_activity_wo_gpx_invalid_payload( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -383,17 +336,9 @@ def test_edit_an_activity_wo_gpx_invalid_payload(app): assert 'Invalid payload.' in data['message'] -def test_edit_an_activity_wo_gpx_incorrect_data(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - +def test_edit_an_activity_wo_gpx_incorrect_data( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -427,10 +372,9 @@ def test_edit_an_activity_wo_gpx_incorrect_data(app): in data['message'] -def test_edit_an_activity_no_activity(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_edit_an_activity_no_activity( + app, user_1, sport_1_cycling +): client = app.test_client() resp_login = client.post( '/api/auth/login', diff --git a/mpwo_api/mpwo_api/tests/test_activities_api_3_delete.py b/mpwo_api/mpwo_api/tests/test_activities_api_3_delete.py index 86dd432f..2c59f151 100644 --- a/mpwo_api/mpwo_api/tests/test_activities_api_3_delete.py +++ b/mpwo_api/mpwo_api/tests/test_activities_api_3_delete.py @@ -1,18 +1,16 @@ -import datetime import json import os from io import BytesIO -from mpwo_api.tests.utils import ( - add_activity, add_sport, add_user, get_gpx_filepath -) -from mpwo_api.tests.utils_gpx import gpx_file +from mpwo_api.activities.models import Activity -def test_delete_an_activity_with_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') +def get_gpx_filepath(activity_id): + activity = Activity.query.filter_by(id=activity_id).first() + return activity.gpx + +def test_delete_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -47,17 +45,9 @@ def test_delete_an_activity_with_gpx(app): assert response.status_code == 204 -def test_delete_an_activity_wo_gpx(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - +def test_delete_an_activity_wo_gpx( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -75,13 +65,10 @@ def test_delete_an_activity_wo_gpx(app): )['auth_token'] ) ) - assert response.status_code == 204 -def test_delete_an_activity_no_activityy(app): - add_user('test', 'test@test.com', '12345678') - +def test_delete_an_activity_no_activityy(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -99,17 +86,13 @@ def test_delete_an_activity_no_activityy(app): )['auth_token'] ) ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 assert 'not found' in data['status'] -def test_delete_an_activity_with_gpx_invalid_file(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_delete_an_activity_with_gpx_invalid_file( + app, user_1, sport_1_cycling, gpx_file): client = app.test_client() resp_login = client.post( '/api/auth/login', diff --git a/mpwo_api/mpwo_api/tests/test_activities_model.py b/mpwo_api/mpwo_api/tests/test_activities_model.py index b4def9a4..3835ac0d 100644 --- a/mpwo_api/mpwo_api/tests/test_activities_model.py +++ b/mpwo_api/mpwo_api/tests/test_activities_model.py @@ -1,25 +1,15 @@ -import datetime - -from mpwo_api.tests.utils import add_activity, add_sport, add_user -def test_add_activity(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - activity.title = 'Test' +def test_add_activity( + app, sport_1_cycling, user_1, activity_cycling_user_1 +): + activity_cycling_user_1.title = 'Test' - assert 1 == activity.id - assert 1 == activity.user_id - assert 1 == activity.sport_id - assert '2018-01-01 13:36:00' == str(activity.activity_date) - assert 10.0 == float(activity.distance) - assert '0:17:04' == str(activity.duration) - assert 'Test' == activity.title - assert '' == str(activity) + assert 1 == activity_cycling_user_1.id + assert 1 == activity_cycling_user_1.user_id + assert 1 == activity_cycling_user_1.sport_id + assert '2018-01-01 00:00:00' == str(activity_cycling_user_1.activity_date) + assert 10.0 == float(activity_cycling_user_1.distance) + assert '0:17:04' == str(activity_cycling_user_1.duration) + assert 'Test' == activity_cycling_user_1.title + assert '' == str(activity_cycling_user_1) # noqa diff --git a/mpwo_api/mpwo_api/tests/test_auth_api.py b/mpwo_api/mpwo_api/tests/test_auth_api.py index 1d3c9d2b..46f192f2 100644 --- a/mpwo_api/mpwo_api/tests/test_auth_api.py +++ b/mpwo_api/mpwo_api/tests/test_auth_api.py @@ -2,8 +2,6 @@ import json import time from io import BytesIO -from mpwo_api.tests.utils import add_user, add_user_full - def test_user_registration(app): client = app.test_client() @@ -25,8 +23,7 @@ def test_user_registration(app): assert response.status_code == 201 -def test_user_registration_user_already_exists(app): - add_user('test', 'test@test.com', '12345678') +def test_user_registration_user_already_exists(app, user_1): client = app.test_client() response = client.post( '/api/auth/register', @@ -217,9 +214,26 @@ def test_user_registration_invalid_json_keys_no_password_conf(app): assert 'error' in data['status'] -def test_registered_user_login(app): +def test_user_registration_invalid_data(app): + client = app.test_client() + response = client.post( + '/api/auth/register', + data=json.dumps(dict( + username=1, + email='test@test.com', + password='12345678', + password_conf='12345678' + )), + content_type='application/json', + ) + data = json.loads(response.data.decode()) + assert response.status_code == 500 + assert 'Error. Please try again or contact the administrator.' in data['message'] # noqa + assert 'error' in data['status'] + + +def test_login_registered_user(app, user_1): client = app.test_client() - add_user('test', 'test@test.com', '12345678') response = client.post( '/api/auth/login', data=json.dumps(dict( @@ -236,7 +250,7 @@ def test_registered_user_login(app): assert response.status_code == 200 -def test_no_registered_user_login(app): +def test_login_no_registered_user(app): client = app.test_client() response = client.post( '/api/auth/login', @@ -253,8 +267,21 @@ def test_no_registered_user_login(app): assert response.status_code == 404 -def test_registered_user_login_invalid_password(app): - add_user('test', 'test@test.com', '12345678') +def test_login_invalid_payload(app): + client = app.test_client() + response = client.post( + '/api/auth/login', + data=json.dumps(dict()), + content_type='application/json' + ) + data = json.loads(response.data.decode()) + assert data['status'] == 'error' + assert data['message'] == 'Invalid payload.' + assert response.content_type == 'application/json' + assert response.status_code == 400 + + +def test_login_registered_user_invalid_password(app, user_1): client = app.test_client() response = client.post( '/api/auth/login', @@ -271,8 +298,7 @@ def test_registered_user_login_invalid_password(app): assert response.status_code == 404 -def test_valid_logout(app): - add_user('test', 'test@test.com', '12345678') +def test_logout(app, user_1): client = app.test_client() # user login resp_login = client.post( @@ -298,8 +324,7 @@ def test_valid_logout(app): assert response.status_code == 200 -def test_invalid_logout_expired_token(app): - add_user('test', 'test@test.com', '12345678') +def test_logout_expired_token(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -325,7 +350,7 @@ def test_invalid_logout_expired_token(app): assert response.status_code == 401 -def test_invalid_logout(app): +def test_logout_invalid(app): client = app.test_client() response = client.get( '/api/auth/logout', @@ -336,8 +361,18 @@ def test_invalid_logout(app): assert response.status_code == 401 -def test_user_profile_minimal(app): - add_user('test', 'test@test.com', '12345678') +def test_logout_invalid_headers(app): + client = app.test_client() + response = client.get( + '/api/auth/logout', + headers=dict()) + data = json.loads(response.data.decode()) + assert data['status'] == 'error' + assert data['message'] == 'Provide a valid auth token.' + assert response.status_code == 401 + + +def test_user_profile_minimal(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -365,8 +400,7 @@ def test_user_profile_minimal(app): assert response.status_code == 200 -def test_user_profile_full(app): - add_user_full('test', 'test@test.com', '12345678') +def test_user_profile_full(app, user_1_full): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -410,8 +444,41 @@ def test_invalid_profile(app): assert response.status_code == 401 -def test_user_profile_valid_update(app): - add_user('test', 'test@test.com', '12345678') +def test_user_profile_valid_update(app, user_1): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='test@test.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.post( + '/api/auth/profile/edit', + content_type='application/json', + data=json.dumps(dict( + first_name='John', + last_name='Doe', + location='Somewhere', + bio='just a random guy', + birth_date='1980-01-01', + password='87654321', + password_conf='87654321' + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert data['status'] == 'success' + assert data['message'] == 'User profile updated.' + assert response.status_code == 200 + + +def test_user_profile_valid_update_without_password(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -443,8 +510,7 @@ def test_user_profile_valid_update(app): assert response.status_code == 200 -def test_user_profile_valid_update_with_one_field(app): - add_user('test', 'test@test.com', '12345678') +def test_user_profile_valid_update_with_one_field(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -472,8 +538,7 @@ def test_user_profile_valid_update_with_one_field(app): assert response.status_code == 200 -def test_user_profile_update_invalid_json(app): - add_user('test', 'test@test.com', '12345678') +def test_user_profile_update_invalid_json(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -499,9 +564,74 @@ def test_user_profile_update_invalid_json(app): assert 'error' in data['status'] -def test_update_user_picture(app): - add_user('test', 'test@test.com', '12345678') +def test_user_profile_invalid_password(app, user_1): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='test@test.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.post( + '/api/auth/profile/edit', + content_type='application/json', + data=json.dumps(dict( + first_name='John', + last_name='Doe', + location='Somewhere', + bio='just a random guy', + birth_date='1980-01-01', + password='87654321', + password_conf='876543210' + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert data['status'] == 'error' + assert data['message'] == 'Password and password confirmation don\'t match.\n' # noqa + assert response.status_code == 400 + +def test_user_profile_missing_password_conf(app, user_1): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='test@test.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.post( + '/api/auth/profile/edit', + content_type='application/json', + data=json.dumps(dict( + first_name='John', + last_name='Doe', + location='Somewhere', + bio='just a random guy', + birth_date='1980-01-01', + password='87654321', + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert data['status'] == 'error' + assert data['message'] == 'Password and password confirmation don\'t match.\n' # noqa + assert response.status_code == 400 + + +def test_update_user_picture(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -528,9 +658,7 @@ def test_update_user_picture(app): assert response.status_code == 200 -def test_update_user_no_picture(app): - add_user('test', 'test@test.com', '12345678') - +def test_update_user_no_picture(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -554,9 +682,7 @@ def test_update_user_no_picture(app): assert response.status_code == 400 -def test_update_user_invalid_picture(app): - add_user('test', 'test@test.com', '12345678') - +def test_update_user_invalid_picture(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', diff --git a/mpwo_api/mpwo_api/tests/test_records_api.py b/mpwo_api/mpwo_api/tests/test_records_api.py index bc2a006e..831516b5 100644 --- a/mpwo_api/mpwo_api/tests/test_records_api.py +++ b/mpwo_api/mpwo_api/tests/test_records_api.py @@ -1,42 +1,11 @@ -import datetime import json -from mpwo_api.tests.utils import add_activity, add_record, add_sport, add_user - - -def test_get_all_activities_for_authenticated_user(app): - add_user('test', 'test@test.com', '12345678') - add_user('toto', 'toto@toto.com', '12345678') - add_sport('cycling') - add_sport('running') - - activity = add_activity( - user_id=1, - sport_id=2, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - add_record(1, 2, activity, 'LD') - - activity = add_activity( - user_id=2, - sport_id=1, - activity_date=datetime.datetime.strptime('23/01/2018', '%d/%m/%Y'), - distance=15, - duration=datetime.timedelta(seconds=3600), - ) - add_record(2, 1, activity, 'MS') - - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/04/2018', '%d/%m/%Y'), - distance=12, - duration=datetime.timedelta(seconds=6000) - ) - add_record(1, 1, activity, 'FD') +def test_get_all_records_for_authenticated_user( + app, user_1, user_2, sport_1_cycling, sport_2_running, + activity_cycling_user_1, activity_cycling_user_2, activity_running_user_1, + record_ld, record_ms_user_2_cycling, record_fd_running +): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -62,14 +31,14 @@ def test_get_all_activities_for_authenticated_user(app): assert 'Sun, 01 Apr 2018 00:00:00 GMT' == data['data']['records'][0]['activity_date'] # noqa assert 1 == data['data']['records'][0]['user_id'] - assert 1 == data['data']['records'][0]['sport_id'] + assert 2 == data['data']['records'][0]['sport_id'] assert 3 == data['data']['records'][0]['activity_id'] assert 'FD' == data['data']['records'][0]['record_type'] assert 'value' in data['data']['records'][0] assert 'Mon, 01 Jan 2018 00:00:00 GMT' == data['data']['records'][1]['activity_date'] # noqa assert 1 == data['data']['records'][1]['user_id'] - assert 2 == data['data']['records'][1]['sport_id'] + assert 1 == data['data']['records'][1]['sport_id'] assert 1 == data['data']['records'][1]['activity_id'] assert 'LD' == data['data']['records'][1]['record_type'] assert 'value' in data['data']['records'][1] diff --git a/mpwo_api/mpwo_api/tests/test_records_model.py b/mpwo_api/mpwo_api/tests/test_records_model.py index 295e8199..26f7bf5a 100644 --- a/mpwo_api/mpwo_api/tests/test_records_model.py +++ b/mpwo_api/mpwo_api/tests/test_records_model.py @@ -1,30 +1,18 @@ import datetime -from mpwo_api.tests.utils import add_activity, add_record, add_sport, add_user +def test_record_model( + app, user_1, sport_1_cycling, activity_cycling_user_1, record_ld +): + assert 1 == record_ld.id + assert 1 == record_ld.user_id + assert 1 == record_ld.sport_id + assert 1 == record_ld.activity_id + assert 'LD' == record_ld.record_type + assert '2018-01-01 00:00:00' == str(record_ld.activity_date) + assert '' == str(record_ld) -def test_add_record(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - record = add_record(1, 1, activity, 'LD') - - assert 1 == record.id - assert 1 == record.user_id - assert 1 == record.sport_id - assert 1 == record.activity_id - assert 'LD' == record.record_type - assert '2018-01-01 13:36:00' == str(record.activity_date) - assert '' == str(record) - - record_serialize = record.serialize() + record_serialize = record_ld.serialize() assert 'id' in record_serialize assert 'user_id' in record_serialize assert 'sport_id' in record_serialize @@ -34,137 +22,83 @@ def test_add_record(app): assert 'value' in record_serialize -def test_add_records_no_value(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') +def test_add_record_no_value( + app, user_1, sport_1_cycling, activity_cycling_user_1, record_as +): + assert record_as.value is None + assert record_as._value is None - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - record = add_record(1, 1, activity, 'AS') - - assert record.value is None - assert record._value is None - - record_serialize = record.serialize() + record_serialize = record_as.serialize() assert record_serialize.get('value') is None -def test_add_as_records(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - record = add_record(1, 1, activity, 'AS') +def test_add_as_records( + app, user_1, sport_1_cycling, activity_cycling_user_1, record_as +): # record.value = 4.6 - record.value = 4.61 + record_as.value = 4.61 - assert isinstance(record.value, float) - assert record.value == 4.61 - assert record._value == 461 + assert isinstance(record_as.value, float) + assert record_as.value == 4.61 + assert record_as._value == 461 - record_serialize = record.serialize() + record_serialize = record_as.serialize() assert record_serialize.get('value') == 4.61 assert isinstance(record_serialize.get('value'), float) -def test_add_fd_records(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') +def test_add_fd_records( + app, user_1, sport_1_cycling, activity_cycling_user_1, record_fd +): + record_fd.value = 0.322 - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - record = add_record(1, 1, activity, 'FD') - record.value = 0.322 + assert isinstance(record_fd.value, float) + assert record_fd.value == 0.322 + assert record_fd._value == 322 - assert isinstance(record.value, float) - assert record.value == 0.322 - assert record._value == 322 - - record_serialize = record.serialize() + record_serialize = record_fd.serialize() assert record_serialize.get('value') == 0.322 assert isinstance(record_serialize.get('value'), float) -def test_add_ld_records(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') +def test_add_ld_records( + app, user_1, sport_1_cycling, activity_cycling_user_1, record_ld +): + record_ld.value = activity_cycling_user_1.duration - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - record = add_record(1, 1, activity, 'LD') - record.value = activity.duration + assert isinstance(record_ld.value, datetime.timedelta) + assert str(record_ld.value) == '0:17:04' + assert record_ld._value == 1024 - assert isinstance(record.value, datetime.timedelta) - assert str(record.value) == '0:17:04' - assert record._value == 1024 - - record_serialize = record.serialize() + record_serialize = record_ld.serialize() assert record_serialize.get('value') == '0:17:04' assert isinstance(record_serialize.get('value'), str) -def test_add_ld_records_zero(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') +def test_add_ld_records_zero( + app, user_1, sport_1_cycling, activity_cycling_user_1, record_ld +): + activity_cycling_user_1.duration = datetime.timedelta(seconds=0) + record_ld.value = activity_cycling_user_1.duration - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=0) - ) - record = add_record(1, 1, activity, 'LD') - record.value = activity.duration + assert isinstance(record_ld.value, datetime.timedelta) + assert str(record_ld.value) == '0:00:00' + assert record_ld._value == 0 - assert isinstance(record.value, datetime.timedelta) - assert str(record.value) == '0:00:00' - assert record._value == 0 - - record_serialize = record.serialize() + record_serialize = record_ld.serialize() assert record_serialize.get('value') == '0:00:00' assert isinstance(record_serialize.get('value'), str) -def test_add_ms_records_no_value(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') +def test_add_ms_records_no_value( + app, user_1, sport_1_cycling, activity_cycling_user_1, record_ms +): + record_ms.value = 23.5 - activity = add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018 13:36', '%d/%m/%Y %H:%M'), # noqa - distance=10, - duration=datetime.timedelta(seconds=1024) - ) - record = add_record(1, 1, activity, 'MS') - record.value = 23.5 + assert isinstance(record_ms.value, float) + assert record_ms.value == 23.5 + assert record_ms._value == 2350 - assert isinstance(record.value, float) - assert record.value == 23.5 - assert record._value == 2350 - - record_serialize = record.serialize() + record_serialize = record_ms.serialize() assert record_serialize.get('value') == 23.5 assert isinstance(record_serialize.get('value'), float) diff --git a/mpwo_api/mpwo_api/tests/test_sports_api.py b/mpwo_api/mpwo_api/tests/test_sports_api.py index b70d0483..5514c97c 100644 --- a/mpwo_api/mpwo_api/tests/test_sports_api.py +++ b/mpwo_api/mpwo_api/tests/test_sports_api.py @@ -1,14 +1,19 @@ -import datetime import json -from mpwo_api.tests.utils import add_activity, add_admin, add_sport, add_user +expected_sport_1_cycling_result = { + 'id': 1, + 'label': 'Cycling', + '_can_be_deleted': True +} + +expected_sport_2_running_result = { + 'id': 2, + 'label': 'Running', + '_can_be_deleted': True +} -def test_get_all_sports(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_sport('running') - +def test_get_all_sports(app, user_1, sport_1_cycling, sport_2_running): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -32,14 +37,11 @@ def test_get_all_sports(app): assert 'success' in data['status'] assert len(data['data']['sports']) == 2 - assert 'cycling' in data['data']['sports'][0]['label'] - assert 'running' in data['data']['sports'][1]['label'] + assert data['data']['sports'][0] == expected_sport_1_cycling_result + assert data['data']['sports'][1] == expected_sport_2_running_result -def test_get_a_sport(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_get_a_sport(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -63,12 +65,35 @@ def test_get_a_sport(app): assert 'success' in data['status'] assert len(data['data']['sports']) == 1 - assert 'cycling' in data['data']['sports'][0]['label'] + assert data['data']['sports'][0] == expected_sport_1_cycling_result -def test_add_a_sport(app): - add_admin() +def test_get_a_sport_invalid(app, user_1): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='test@test.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.get( + '/api/sports/1', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert response.status_code == 404 + assert 'not found' in data['status'] + assert len(data['data']['sports']) == 0 + + +def test_add_a_sport(app, user_1_admin): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -82,7 +107,7 @@ def test_add_a_sport(app): '/api/sports', content_type='application/json', data=json.dumps(dict( - label='surfing' + label='Cycling' )), headers=dict( Authorization='Bearer ' + json.loads( @@ -96,12 +121,10 @@ def test_add_a_sport(app): assert 'created' in data['status'] assert len(data['data']['sports']) == 1 - assert 'surfing' in data['data']['sports'][0]['label'] + assert data['data']['sports'][0] == expected_sport_1_cycling_result -def test_add_a_sport_not_admin(app): - add_user('test', 'test@test.com', '12345678') - +def test_add_a_sport_not_admin(app, user_1): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -125,16 +148,40 @@ def test_add_a_sport_not_admin(app): ) data = json.loads(response.data.decode()) - assert response.status_code == 401 + assert response.status_code == 403 assert 'created' not in data['status'] assert 'error' in data['status'] assert 'You do not have permissions.' in data['message'] -def test_update_a_sport(app): - add_admin() - add_sport('cycling') +def test_add_a_sport_invalid_payload(app, user_1_admin): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='admin@example.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.post( + '/api/sports', + content_type='application/json', + data=json.dumps(dict()), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert response.status_code == 400 + assert 'error' in data['status'] + assert 'Invalid payload.' in data['message'] + + +def test_update_a_sport(app, user_1_admin, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -165,10 +212,7 @@ def test_update_a_sport(app): assert 'cycling updated' in data['data']['sports'][0]['label'] -def test_update_a_sport_not_admin(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_update_a_sport_not_admin(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -192,16 +236,69 @@ def test_update_a_sport_not_admin(app): ) data = json.loads(response.data.decode()) - assert response.status_code == 401 + assert response.status_code == 403 assert 'success' not in data['status'] assert 'error' in data['status'] assert 'You do not have permissions.' in data['message'] -def test_delete_a_sport(app): - add_admin() - add_sport('cycling') +def test_update_a_sport_invalid_payload(app, user_1_admin): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='admin@example.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.patch( + '/api/sports/1', + content_type='application/json', + data=json.dumps(dict()), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert response.status_code == 400 + assert 'error' in data['status'] + assert 'Invalid payload.' in data['message'] + + +def test_update_a_sport_invalid_id(app, user_1_admin): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='admin@example.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.patch( + '/api/sports/1', + content_type='application/json', + data=json.dumps(dict( + label='cycling updated' + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 404 + assert 'not found' in data['status'] + assert len(data['data']['sports']) == 0 + + +def test_delete_a_sport(app, user_1_admin, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -223,10 +320,7 @@ def test_delete_a_sport(app): assert response.status_code == 204 -def test_delete_a_sport_not_admin(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - +def test_delete_a_sport_not_admin(app, user_1, sport_1_cycling): client = app.test_client() resp_login = client.post( '/api/auth/login', @@ -247,22 +341,41 @@ def test_delete_a_sport_not_admin(app): ) data = json.loads(response.data.decode()) - assert response.status_code == 401 + assert response.status_code == 403 assert 'error' in data['status'] assert 'You do not have permissions.' in data['message'] -def test_delete_a_sport_with_an_activity(app): - add_admin() - add_sport('cycling') - add_activity( - user_id=1, - sport_id=1, - activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), - distance=10, - duration=datetime.timedelta(seconds=1024) +def test_delete_a_sport_invalid_id(app, user_1_admin): + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='admin@example.com', + password='12345678' + )), + content_type='application/json' ) + response = client.delete( + '/api/sports/1', + content_type='application/json', + data=json.dumps(dict()), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert response.status_code == 404 + assert 'not found' in data['status'] + assert len(data['data']['sports']) == 0 + + +def test_delete_a_sport_with_an_activity( + app, user_1_admin, sport_1_cycling, activity_cycling_user_1 +): client = app.test_client() resp_login = client.post( '/api/auth/login', diff --git a/mpwo_api/mpwo_api/tests/test_sports_model.py b/mpwo_api/mpwo_api/tests/test_sports_model.py index a304a7a7..f25de22c 100644 --- a/mpwo_api/mpwo_api/tests/test_sports_model.py +++ b/mpwo_api/mpwo_api/tests/test_sports_model.py @@ -1,10 +1,22 @@ -from mpwo_api.tests.utils import add_sport, add_user +def test_sport_model(app, sport_1_cycling): + assert 1 == sport_1_cycling.id + assert 'Cycling' == sport_1_cycling.label + assert '' == str(sport_1_cycling) + + serialized_sport = sport_1_cycling.serialize() + assert 1 == serialized_sport['id'] + assert 'Cycling' == serialized_sport['label'] + assert serialized_sport['_can_be_deleted'] is True -def test_add_sport(app): - add_user('test', 'test@test.com', '12345678') - sport = add_sport('cycling') +def test_sport_model_with_activity( + app, sport_1_cycling, user_1, activity_cycling_user_1 +): + assert 1 == sport_1_cycling.id + assert 'Cycling' == sport_1_cycling.label + assert '' == str(sport_1_cycling) - assert 1 == sport.id - assert 'cycling' == sport.label - assert '' == str(sport) + serialized_sport = sport_1_cycling.serialize() + assert 1 == serialized_sport['id'] + assert 'Cycling' == serialized_sport['label'] + assert serialized_sport['_can_be_deleted'] is False diff --git a/mpwo_api/mpwo_api/tests/test_users_api.py b/mpwo_api/mpwo_api/tests/test_users_api.py index 5caacbca..5ff51293 100644 --- a/mpwo_api/mpwo_api/tests/test_users_api.py +++ b/mpwo_api/mpwo_api/tests/test_users_api.py @@ -1,6 +1,5 @@ import json -from mpwo_api.tests.utils import add_user from mpwo_api.users.models import User @@ -14,12 +13,11 @@ def test_ping(app): assert 'success' in data['status'] -def test_single_user(app): +def test_single_user(app, user_1): """=> Get single user details""" - user = add_user('test', 'test@test.com', 'test') client = app.test_client() - response = client.get(f'/api/users/{user.id}') + response = client.get(f'/api/users/{user_1.id}') data = json.loads(response.data.decode()) assert response.status_code == 200 @@ -52,10 +50,8 @@ def test_single_user_wrong_id(app): assert 'User does not exist' in data['message'] -def test_users_list(app): +def test_users_list(app, user_1, user_2): """=> Ensure get single user behaves correctly.""" - add_user('test', 'test@test.com', 'test') - add_user('toto', 'toto@toto.com', 'toto') client = app.test_client() response = client.get('/api/users') @@ -73,15 +69,13 @@ def test_users_list(app): assert 'toto@toto.com' in data['data']['users'][1]['email'] -def test_encode_auth_token(app): +def test_encode_auth_token(app, user_1): """=> Ensure correct auth token generation""" - user = add_user('test', 'test@test.com', 'test') - auth_token = user.encode_auth_token(user.id) + auth_token = user_1.encode_auth_token(user_1.id) assert isinstance(auth_token, bytes) -def test_decode_auth_token(app): - user = add_user('test', 'test@test.com', 'test') - auth_token = user.encode_auth_token(user.id) +def test_decode_auth_token(app, user_1): + auth_token = user_1.encode_auth_token(user_1.id) assert isinstance(auth_token, bytes) - assert User.decode_auth_token(auth_token) == user.id + assert User.decode_auth_token(auth_token) == user_1.id diff --git a/mpwo_api/mpwo_api/tests/test_users_model.py b/mpwo_api/mpwo_api/tests/test_users_model.py index fee6118e..8df4caef 100644 --- a/mpwo_api/mpwo_api/tests/test_users_model.py +++ b/mpwo_api/mpwo_api/tests/test_users_model.py @@ -1,6 +1,14 @@ -from mpwo_api.tests.utils import add_user +def test_user_model(app, user_1): + assert '' == str(user_1) - -def test_add_user(app): - user = add_user('test', 'test@test.com', '12345678') - assert '' == str(user) + serialized_user = user_1.serialize() + assert 1 == serialized_user['id'] + assert 'test' == serialized_user['username'] + assert 'created_at' in serialized_user + assert serialized_user['admin'] is False + assert serialized_user['first_name'] is None + assert serialized_user['last_name'] is None + assert serialized_user['bio'] is None + assert serialized_user['location'] is None + assert serialized_user['birth_date'] is None + assert serialized_user['picture'] is False diff --git a/mpwo_api/mpwo_api/tests/utils.py b/mpwo_api/mpwo_api/tests/utils.py deleted file mode 100644 index b7c43379..00000000 --- a/mpwo_api/mpwo_api/tests/utils.py +++ /dev/null @@ -1,71 +0,0 @@ -import datetime - -from mpwo_api import db -from mpwo_api.activities.models import Activity, Record, Sport -from mpwo_api.users.models import User - - -def add_admin(): - admin = User( - username="admin", - email="admin@example.com", - password="12345678" - ) - admin.admin = True - db.session.add(admin) - db.session.commit() - return admin - - -def add_user(username, email, password): - user = User(username=username, email=email, password=password) - db.session.add(user) - db.session.commit() - return user - - -def add_user_full(username, email, password): - user = User(username=username, email=email, password=password) - user.first_name = 'John' - user.last_name = 'Doe' - user.bio = 'just a random guy' - user.location = 'somewhere' - user.birth_date = datetime.datetime.strptime('01/01/1980', '%d/%m/%Y') - db.session.add(user) - db.session.commit() - return user - - -def add_sport(label): - sport = Sport(label=label) - db.session.add(sport) - db.session.commit() - return sport - - -def add_activity(user_id, sport_id, activity_date, distance, duration): - activity = Activity( - user_id=user_id, - sport_id=sport_id, - activity_date=activity_date, - distance=distance, - duration=duration) - db.session.add(activity) - db.session.commit() - return activity - - -def get_gpx_filepath(activity_id): - activity = Activity.query.filter_by(id=activity_id).first() - return activity.gpx - - -def add_record(user_id, sport_id, activity, record_type): - record = Record( - user_id=user_id, - sport_id=sport_id, - activity=activity, - record_type=record_type) - db.session.add(record) - db.session.commit() - return record diff --git a/mpwo_api/mpwo_api/tests/utils_gpx.py b/mpwo_api/mpwo_api/tests/utils_gpx.py deleted file mode 100644 index 0e75f3c2..00000000 --- a/mpwo_api/mpwo_api/tests/utils_gpx.py +++ /dev/null @@ -1,231 +0,0 @@ -gpx_file = ( - '' - '' # noqa - ' ' - ' ' - ' just an activity' - ' ' - ' ' - ' 998' - ' ' - ' ' - ' ' - ' 998' - ' ' - ' ' - ' ' - ' 994' - ' ' - ' ' - ' ' - ' 994' - ' ' - ' ' - ' ' - ' 994' - ' ' - ' ' - ' ' - ' 993' - ' ' - ' ' - ' ' - ' 992' - ' ' - ' ' - ' ' - ' 992' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 986' - ' ' - ' ' - ' ' - ' 986' - ' ' - ' ' - ' ' - ' 986' - ' ' - ' ' - ' ' - ' 985' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 979' - ' ' - ' ' - ' ' - ' 981' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 979' - ' ' - ' ' - ' ' - ' 979' - ' ' - ' ' - ' ' - ' 975' - ' ' - ' ' - ' ' - ' ' - '' -) - -gpx_file_wo_name = ( - '' - '' # noqa - ' ' - ' ' - ' ' - ' ' - ' 998' - ' ' - ' ' - ' ' - ' 998' - ' ' - ' ' - ' ' - ' 994' - ' ' - ' ' - ' ' - ' 994' - ' ' - ' ' - ' ' - ' 994' - ' ' - ' ' - ' ' - ' 993' - ' ' - ' ' - ' ' - ' 992' - ' ' - ' ' - ' ' - ' 992' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 987' - ' ' - ' ' - ' ' - ' 986' - ' ' - ' ' - ' ' - ' 986' - ' ' - ' ' - ' ' - ' 986' - ' ' - ' ' - ' ' - ' 985' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 979' - ' ' - ' ' - ' ' - ' 981' - ' ' - ' ' - ' ' - ' 980' - ' ' - ' ' - ' ' - ' 979' - ' ' - ' ' - ' ' - ' 979' - ' ' - ' ' - ' ' - ' 975' - ' ' - ' ' - ' ' - ' ' - '' -) - -gpx_file_wo_track = ( - '' - '' # noqa - ' ' - '' -) - -gpx_file_empty = '' diff --git a/mpwo_api/mpwo_api/users/auth.py b/mpwo_api/mpwo_api/users/auth.py index 1eb93bc1..2cc112cf 100644 --- a/mpwo_api/mpwo_api/users/auth.py +++ b/mpwo_api/mpwo_api/users/auth.py @@ -30,7 +30,17 @@ def register_user(): password = post_data.get('password') password_conf = post_data.get('password_conf') - ret = register_controls(username, email, password, password_conf) + try: + ret = register_controls(username, email, password, password_conf) + except TypeError as e: + db.session.rollback() + appLog.error(e) + + response_object = { + 'status': 'error', + 'message': 'Error. Please try again or contact the administrator.' + } + return jsonify(response_object), 500 if ret != '': response_object = { 'status': 'error', @@ -74,7 +84,7 @@ def register_user(): 'status': 'error', 'message': 'Error. Please try again or contact the administrator.' } - return jsonify(response_object), 400 + return jsonify(response_object), 500 @auth_blueprint.route('/auth/login', methods=['POST']) @@ -143,7 +153,7 @@ def logout_user(user_id): 'status': 'error', 'message': 'Provide a valid auth token.' } - return jsonify(response_object), 403 + return jsonify(response_object), 401 @auth_blueprint.route('/auth/profile', methods=['GET']) diff --git a/mpwo_api/mpwo_api/users/utils.py b/mpwo_api/mpwo_api/users/utils.py index 7b5ac00b..22879b5d 100644 --- a/mpwo_api/mpwo_api/users/utils.py +++ b/mpwo_api/mpwo_api/users/utils.py @@ -67,7 +67,6 @@ def verify_user(current_request, verify_admin): auth_header = current_request.headers.get('Authorization') if not auth_header: response_object['message'] = 'Provide a valid auth token.' - code = 403 return response_object, code, None auth_token = auth_header.split(" ")[1] resp = User.decode_auth_token(auth_token) @@ -79,7 +78,7 @@ def verify_user(current_request, verify_admin): return response_object, code, None if verify_admin and not is_admin(resp): response_object['message'] = 'You do not have permissions.' - return response_object, code, None + return response_object, 403, None return None, None, resp