From 63f64d8acb10e33d83347743dcb5b3b36d0aadaa Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 28 May 2018 11:44:42 +0200 Subject: [PATCH] API: tests update --- mpwo_api/mpwo_api/tests/conftest.py | 9 ++ .../tests/test_activities_api_0_get.py | 82 +++++++++++++++++++ .../tests/test_activities_api_1_post.py | 50 ++++++++++- mpwo_api/mpwo_api/tests/test_records_model.py | 21 +++++ 4 files changed, 161 insertions(+), 1 deletion(-) diff --git a/mpwo_api/mpwo_api/tests/conftest.py b/mpwo_api/mpwo_api/tests/conftest.py index 1ac7b0a1..3e2503ac 100644 --- a/mpwo_api/mpwo_api/tests/conftest.py +++ b/mpwo_api/mpwo_api/tests/conftest.py @@ -441,3 +441,12 @@ def gpx_file_wo_track(): ' ' '' ) + + +@pytest.fixture() +def gpx_file_invalid_xml(): + 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 938ee0b5..5d2d7fe9 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 @@ -227,3 +227,85 @@ def test_get_an_activity_invalid_id(app, user_1): assert response.status_code == 404 assert 'not found' in data['status'] assert len(data['data']['activities']) == 0 + + +def test_get_an_activity_no_actvity_no_gpx(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/gpx', + 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 'Activity not found (id: 11)' in data['message'] + assert data['data']['gpx'] == '' + + +def test_get_an_activity_actvity_no_gpx( + 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/gpx', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 400 + assert 'fail' in data['status'] + assert 'No gpx file for this activity (id: 1)' in data['message'] + + +def test_get_an_activity_actvity_invalid_gpx( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): + activity_cycling_user_1.gpx = "some path" + 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/gpx', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 500 + assert 'error' in data['status'] + assert 'internal error' in data['message'] + assert data['data']['gpx'] == '' 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 2cb00beb..b1133ff0 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 @@ -134,7 +134,7 @@ def test_add_an_activity_gpx(app, user_1, sport_1_cycling, gpx_file): assert_activity_data_with_gpx(data) -def test_add_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file): +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', @@ -173,6 +173,21 @@ def test_add_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file): assert 'just an activity' == data['data']['activities'][0]['title'] assert_activity_data_with_gpx(data) + response = client.get( + '/api/activities/1/gpx', + 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 '' in data['message'] + assert len(data['data']['gpx']) != '' + def test_add_an_activity_with_gpx_without_name( app, user_1, sport_1_cycling, gpx_file_wo_name @@ -241,6 +256,39 @@ def test_add_an_activity_with_gpx_invalid_file( assert 'data' not in data +def test_add_an_activity_with_gpx_invalid_xml( + app, user_1, sport_1_cycling, gpx_file_invalid_xml +): + 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/activities', + data=dict( + file=(BytesIO(str.encode(gpx_file_invalid_xml)), 'example.gpx'), + data='{"sport_id": 1}' + ), + headers=dict( + content_type='multipart/form-data', + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 500 + assert 'error' in data['status'] + assert 'Error during gpx file parsing.' in data['message'] + assert 'data' not in data + + def test_add_an_activity_gpx_invalid_extension( app, user_1, sport_1_cycling, gpx_file ): diff --git a/mpwo_api/mpwo_api/tests/test_records_model.py b/mpwo_api/mpwo_api/tests/test_records_model.py index eec4b0d8..4b1fc7e7 100644 --- a/mpwo_api/mpwo_api/tests/test_records_model.py +++ b/mpwo_api/mpwo_api/tests/test_records_model.py @@ -28,6 +28,27 @@ def test_record_model( assert 'value' in record_serialize +def test_record_model_none_value( + app, user_1, sport_1_cycling, activity_cycling_user_1 +): + record_ld = Record.query.filter_by( + user_id=activity_cycling_user_1.user_id, + sport_id=activity_cycling_user_1.sport_id, + record_type='LD', + ).first() + record_ld.value = None + 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) + assert record_ld.value is None + + record_serialize = record_ld.serialize() + assert record_serialize['value'] is None + + def test_add_as_records( app, user_1, sport_1_cycling, activity_cycling_user_1 ):