API: activities fix

This commit is contained in:
Sam 2018-05-14 10:55:01 +02:00
parent b73153ba21
commit 6d4574332b
4 changed files with 83 additions and 6 deletions

View File

@ -4,7 +4,7 @@
[![Flask Version](https://img.shields.io/badge/flask-1.0-brightgreen.svg)](http://flask.pocoo.org/) [![Flask Version](https://img.shields.io/badge/flask-1.0-brightgreen.svg)](http://flask.pocoo.org/)
[![React Version](https://img.shields.io/badge/react-16.3-brightgreen.svg)](https://reactjs.org/) [![React Version](https://img.shields.io/badge/react-16.3-brightgreen.svg)](https://reactjs.org/)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/45d64b31e37e4890a239b8298e66a011)](https://www.codacy.com/app/SamR1/mpwo?utm_source=github.com&utm_medium=referral&utm_content=SamR1/mpwo&utm_campaign=badger) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/45d64b31e37e4890a239b8298e66a011)](https://www.codacy.com/app/SamR1/mpwo?utm_source=github.com&utm_medium=referral&utm_content=SamR1/mpwo&utm_campaign=badger)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/45d64b31e37e4890a239b8298e66a011)](https://www.codacy.com/app/SamR1/mpwo?utm_source=github.com&utm_medium=referral&utm_content=SamR1/mpwo&utm_campaign=Badge_Coverage)<sup>1</sup> [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/45d64b31e37e4890a239b8298e66a011)](https://www.codacy.com/app/SamR1/mpwo?utm_source=github.com&utm_medium=referral&utm_content=SamR1/mpwo&utm_campaign=Badge_Coverage)<sup><sup>1</sup></sup>
[![Build Status](https://travis-ci.org/SamR1/mpwo.svg?branch=master)](https://travis-ci.org/SamR1/mpwo) [![Build Status](https://travis-ci.org/SamR1/mpwo.svg?branch=master)](https://travis-ci.org/SamR1/mpwo)
Self hosted workout/activity tracker written with Flask and React (_work in progress_). Self hosted workout/activity tracker written with Flask and React (_work in progress_).

View File

@ -122,7 +122,7 @@ def get_activity_gpx(auth_user_id, activity_id):
@activities_blueprint.route('/activities', methods=['POST']) @activities_blueprint.route('/activities', methods=['POST'])
@authenticate @authenticate
def post_activity(auth_user_id): def post_activity(auth_user_id):
"""Post an activity""" """Post an activity (with gpx file)"""
response_object = verify_extension('activity', request) response_object = verify_extension('activity', request)
if response_object['status'] != 'success': if response_object['status'] != 'success':
return jsonify(response_object), 400 return jsonify(response_object), 400
@ -142,6 +142,13 @@ def post_activity(auth_user_id):
activity_file.save(file_path) activity_file.save(file_path)
gpx_data = get_gpx_info(file_path) gpx_data = get_gpx_info(file_path)
if gpx_data is None:
response_object = {
'status': 'error',
'message': 'Error during gpx file parsing.'
}
return jsonify(response_object), 500
sport = Sport.query.filter_by(id=activity_data.get('sport_id')).first() sport = Sport.query.filter_by(id=activity_data.get('sport_id')).first()
new_filepath = get_new_file_path( new_filepath = get_new_file_path(
auth_user_id=auth_user_id, auth_user_id=auth_user_id,

View File

@ -116,7 +116,8 @@ def get_gpx_info(gpx_file):
gpx_data = { gpx_data = {
'name': gpx.tracks[0].name, 'name': gpx.tracks[0].name,
'segments': []} 'segments': []
}
max_speed = 0 max_speed = 0
start = 0 start = 0

View File

@ -8,7 +8,6 @@ def assert_activity_data_with_gpx(data):
assert 1 == data['data']['activities'][0]['user_id'] assert 1 == data['data']['activities'][0]['user_id']
assert 1 == data['data']['activities'][0]['sport_id'] assert 1 == data['data']['activities'][0]['sport_id']
assert '0:04:10' == data['data']['activities'][0]['duration'] 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]['ascent'] == 0.4
assert data['data']['activities'][0]['ave_speed'] == 4.6 assert data['data']['activities'][0]['ave_speed'] == 4.6
assert data['data']['activities'][0]['descent'] == 23.4 assert data['data']['activities'][0]['descent'] == 23.4
@ -66,11 +65,13 @@ def test_add_an_activity_gpx(app, user_1, sport_1_cycling, gpx_file):
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 201 assert response.status_code == 201
assert 'created' in data['status']
assert len(data['data']['activities']) == 1 assert len(data['data']['activities']) == 1
assert 'just an activity' == data['data']['activities'][0]['title']
assert_activity_data_with_gpx(data) assert_activity_data_with_gpx(data)
def test_get_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file): def test_add_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -106,10 +107,78 @@ def test_get_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file):
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['activities']) == 1 assert len(data['data']['activities']) == 1
assert 'just an activity' == data['data']['activities'][0]['title']
assert_activity_data_with_gpx(data) assert_activity_data_with_gpx(data)
def test_add_an_activity_gpx_invalid_file( def test_add_an_activity_with_gpx_without_name(
app, user_1, sport_1_cycling, gpx_file_wo_name
):
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_wo_name)), '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 == 201
assert 'created' in data['status']
assert len(data['data']['activities']) == 1
assert 'Cycling - 2018-03-13 12:44:45' == data['data']['activities'][0]['title'] # noqa
assert_activity_data_with_gpx(data)
def test_add_an_activity_with_gpx_invalid_file(
app, user_1, sport_1_cycling, gpx_file_wo_track
):
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_wo_track)), '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 app, user_1, sport_1_cycling, gpx_file
): ):
client = app.test_client() client = app.test_client()