API: tests refactor (fixtures) + fix

This commit is contained in:
Sam 2018-05-13 18:36:31 +02:00
parent c40147501d
commit b73153ba21
18 changed files with 1116 additions and 947 deletions

View File

@ -61,19 +61,14 @@ def post_sport(auth_user_id):
} }
return jsonify(response_object), 400 return jsonify(response_object), 400
sports_list = []
try: try:
new_sport = Sport(label=sport_data.get('label')) new_sport = Sport(label=sport_data.get('label'))
db.session.add(new_sport) db.session.add(new_sport)
db.session.commit() db.session.commit()
sports_list.append({
'id': new_sport.id,
'label': new_sport.label
})
response_object = { response_object = {
'status': 'created', 'status': 'created',
'data': { 'data': {
'sports': sports_list 'sports': [new_sport.serialize()]
} }
} }
code = 201 code = 201

View File

@ -1,7 +1,10 @@
import datetime
import os import os
import pytest import pytest
from mpwo_api import create_app, db 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["FLASK_ENV"] = 'testing'
os.environ["APP_SETTINGS"] = 'mpwo_api.config.TestingConfig' os.environ["APP_SETTINGS"] = 'mpwo_api.config.TestingConfig'
@ -16,3 +19,476 @@ def app():
db.session.remove() db.session.remove()
db.drop_all() db.drop_all()
return app 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 (
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
' <metadata/>'
' <trk>'
' <name>just an activity</name>'
' <trkseg>'
' <trkpt lat="44.68095" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:45Z</time>'
' </trkpt>'
' <trkpt lat="44.68091" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:50Z</time>'
' </trkpt>'
' <trkpt lat="44.6808" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:00Z</time>'
' </trkpt>'
' <trkpt lat="44.68075" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:05Z</time>'
' </trkpt>'
' <trkpt lat="44.68071" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:10Z</time>'
' </trkpt>'
' <trkpt lat="44.68049" lon="6.07361">'
' <ele>993</ele>'
' <time>2018-03-13T12:45:30Z</time>'
' </trkpt>'
' <trkpt lat="44.68019" lon="6.07356">'
' <ele>992</ele>'
' <time>2018-03-13T12:45:55Z</time>'
' </trkpt>'
' <trkpt lat="44.68014" lon="6.07355">'
' <ele>992</ele>'
' <time>2018-03-13T12:46:00Z</time>'
' </trkpt>'
' <trkpt lat="44.67995" lon="6.07358">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67977" lon="6.07364">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:30Z</time>'
' </trkpt>'
' <trkpt lat="44.67972" lon="6.07367">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67966" lon="6.07368">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67961" lon="6.0737">'
' <ele>986</ele>'
' <time>2018-03-13T12:46:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67938" lon="6.07377">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:05Z</time>'
' </trkpt>'
' <trkpt lat="44.67933" lon="6.07381">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:10Z</time>'
' </trkpt>'
' <trkpt lat="44.67922" lon="6.07385">'
' <ele>985</ele>'
' <time>2018-03-13T12:47:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67911" lon="6.0739">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:30Z</time>'
' </trkpt>'
' <trkpt lat="44.679" lon="6.07399">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67896" lon="6.07402">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67884" lon="6.07408">'
' <ele>979</ele>'
' <time>2018-03-13T12:47:55Z</time>'
' </trkpt>'
' <trkpt lat="44.67863" lon="6.07423">'
' <ele>981</ele>'
' <time>2018-03-13T12:48:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67858" lon="6.07425">'
' <ele>980</ele>'
' <time>2018-03-13T12:48:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67842" lon="6.07434">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67837" lon="6.07435">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67822" lon="6.07442">'
' <ele>975</ele>'
' <time>2018-03-13T12:48:55Z</time>'
' </trkpt>'
' </trkseg>'
' </trk>'
'</gpx>'
)
@pytest.fixture()
def gpx_file_wo_name():
return (
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
' <metadata/>'
' <trk>'
' <trkseg>'
' <trkpt lat="44.68095" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:45Z</time>'
' </trkpt>'
' <trkpt lat="44.68091" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:50Z</time>'
' </trkpt>'
' <trkpt lat="44.6808" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:00Z</time>'
' </trkpt>'
' <trkpt lat="44.68075" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:05Z</time>'
' </trkpt>'
' <trkpt lat="44.68071" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:10Z</time>'
' </trkpt>'
' <trkpt lat="44.68049" lon="6.07361">'
' <ele>993</ele>'
' <time>2018-03-13T12:45:30Z</time>'
' </trkpt>'
' <trkpt lat="44.68019" lon="6.07356">'
' <ele>992</ele>'
' <time>2018-03-13T12:45:55Z</time>'
' </trkpt>'
' <trkpt lat="44.68014" lon="6.07355">'
' <ele>992</ele>'
' <time>2018-03-13T12:46:00Z</time>'
' </trkpt>'
' <trkpt lat="44.67995" lon="6.07358">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67977" lon="6.07364">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:30Z</time>'
' </trkpt>'
' <trkpt lat="44.67972" lon="6.07367">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67966" lon="6.07368">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67961" lon="6.0737">'
' <ele>986</ele>'
' <time>2018-03-13T12:46:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67938" lon="6.07377">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:05Z</time>'
' </trkpt>'
' <trkpt lat="44.67933" lon="6.07381">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:10Z</time>'
' </trkpt>'
' <trkpt lat="44.67922" lon="6.07385">'
' <ele>985</ele>'
' <time>2018-03-13T12:47:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67911" lon="6.0739">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:30Z</time>'
' </trkpt>'
' <trkpt lat="44.679" lon="6.07399">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67896" lon="6.07402">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67884" lon="6.07408">'
' <ele>979</ele>'
' <time>2018-03-13T12:47:55Z</time>'
' </trkpt>'
' <trkpt lat="44.67863" lon="6.07423">'
' <ele>981</ele>'
' <time>2018-03-13T12:48:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67858" lon="6.07425">'
' <ele>980</ele>'
' <time>2018-03-13T12:48:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67842" lon="6.07434">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67837" lon="6.07435">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67822" lon="6.07442">'
' <ele>975</ele>'
' <time>2018-03-13T12:48:55Z</time>'
' </trkpt>'
' </trkseg>'
' </trk>'
'</gpx>'
)
@pytest.fixture()
def gpx_file_wo_track():
return (
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
' <metadata/>'
'</gpx>'
)

View File

@ -1,35 +1,10 @@
import datetime
import json import json
from mpwo_api.tests.utils import add_activity, add_sport, add_user
def test_get_all_activities_for_authenticated_user(
def test_get_all_activities_for_authenticated_user(app): app, user_1, user_2, sport_1_cycling, sport_2_running,
add_user('test', 'test@test.com', '12345678') activity_cycling_user_1, activity_cycling_user_2, activity_running_user_1,
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)
)
client = app.test_client() client = app.test_client()
resp_login = client.post( 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 'creation_date' in data['data']['activities'][0]
assert 'Sun, 01 Apr 2018 00:00:00 GMT' == data['data']['activities'][0]['activity_date'] # noqa 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]['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 12.0 == data['data']['activities'][0]['distance']
assert '1:40:00' == data['data']['activities'][0]['duration'] assert '1:40:00' == data['data']['activities'][0]['duration']
assert 'creation_date' in data['data']['activities'][1] 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 'Mon, 01 Jan 2018 00:00:00 GMT' == data['data']['activities'][1]['activity_date'] # noqa
assert 1 == data['data']['activities'][1]['user_id'] 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 10.0 == data['data']['activities'][1]['distance']
assert '0:17:04' == data['data']['activities'][1]['duration'] assert '0:17:04' == data['data']['activities'][1]['duration']
def test_get_activities_for_authenticated_user_no_activity(app): def test_get_activities_for_authenticated_user_no_activity(
add_user('test', 'test@test.com', '12345678') app, user_1, user_2, sport_1_cycling, sport_2_running,
add_user('toto', 'toto@toto.com', '12345678') activity_cycling_user_1, activity_running_user_1,
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)
)
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps(dict( data=json.dumps(dict(
email='toto@toto.com', email='toto@toto.com',
password='12345678' password='87654321'
)), )),
content_type='application/json' content_type='application/json'
) )
@ -118,64 +77,14 @@ def test_get_activities_for_authenticated_user_no_authentication(app):
response = client.get('/api/activities') response = client.get('/api/activities')
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 403 assert response.status_code == 401
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Provide a valid auth token.' in data['message'] assert 'Provide a valid auth token.' in data['message']
def test_get_activities_pagination(app): def test_get_activities_pagination(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, seven_activities_user_1
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)
)
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -258,3 +167,63 @@ def test_get_activities_pagination(app):
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']) == 0 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

View File

@ -1,14 +1,46 @@
import json import json
from io import BytesIO 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): def assert_activity_data_wo_gpx(data):
add_user('test', 'test@test.com', '12345678') assert 'creation_date' in data['data']['activities'][0]
add_sport('cycling') 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -34,30 +66,11 @@ def test_add_an_activity_gpx(app):
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 'creation_date' in data['data']['activities'][0] assert_activity_data_with_gpx(data)
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_get_an_activity_with_gpx(app): def test_get_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -88,34 +101,17 @@ def test_get_an_activity_with_gpx(app):
)['auth_token'] )['auth_token']
) )
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
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 'creation_date' in data['data']['activities'][0] assert_activity_data_with_gpx(data)
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_invalid_file(app): def test_add_an_activity_gpx_invalid_file(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, gpx_file
add_sport('cycling') ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -145,10 +141,9 @@ def test_add_an_activity_gpx_invalid_file(app):
assert data['message'] == 'File extension not allowed.' assert data['message'] == 'File extension not allowed.'
def test_add_an_activity_gpx_no_sport_id(app): def test_add_an_activity_gpx_no_sport_id(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, gpx_file
add_sport('cycling') ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -178,10 +173,9 @@ def test_add_an_activity_gpx_no_sport_id(app):
assert data['message'] == 'Invalid payload.' assert data['message'] == 'Invalid payload.'
def test_add_an_activity_gpx_incorrect_sport_id(app): def test_add_an_activity_gpx_incorrect_sport_id(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, gpx_file
add_sport('cycling') ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -212,10 +206,7 @@ def test_add_an_activity_gpx_incorrect_sport_id(app):
'Error during activity file save.' 'Error during activity file save.'
def test_add_an_activity_gpx_no_file(app): def test_add_an_activity_gpx_no_file(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -244,10 +235,7 @@ def test_add_an_activity_gpx_no_file(app):
assert data['message'] == 'No file part.' assert data['message'] == 'No file part.'
def test_add_an_activity_no_gpx(app): def test_add_an_activity_no_gpx(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -277,28 +265,10 @@ def test_add_an_activity_no_gpx(app):
assert response.status_code == 201 assert response.status_code == 201
assert 'created' in data['status'] assert 'created' in data['status']
assert len(data['data']['activities']) == 1 assert len(data['data']['activities']) == 1
assert 'creation_date' in data['data']['activities'][0] assert_activity_data_wo_gpx(data)
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_get_an_activity_wo_gpx(app): def test_get_an_activity_wo_gpx(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -336,28 +306,10 @@ def test_get_an_activity_wo_gpx(app):
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 'creation_date' in data['data']['activities'][0] assert_activity_data_wo_gpx(data)
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_no_gpx_invalid_payload(app): def test_add_an_activity_no_gpx_invalid_payload(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -388,10 +340,7 @@ def test_add_an_activity_no_gpx_invalid_payload(app):
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_add_an_activity_no_gpx_error(app): def test_add_an_activity_no_gpx_error(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',

View File

@ -1,16 +1,27 @@
import datetime
import json import json
from io import BytesIO 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): def test_edit_an_activity_with_gpx(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, sport_2_running, gpx_file
add_sport('cycling') ):
add_sport('running')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -46,36 +57,19 @@ def test_edit_an_activity_with_gpx(app):
)['auth_token'] )['auth_token']
) )
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
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 '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 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]['title'] == 'Activity test'
assert data['data']['activities'][0]['ascent'] == 0.4 assert_activity_data_with_gpx(data)
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_partial(app): def test_edit_an_activity_with_gpx_partial(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, sport_2_running, gpx_file
add_sport('cycling') ):
add_sport('running')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -110,36 +104,19 @@ def test_edit_an_activity_with_gpx_partial(app):
)['auth_token'] )['auth_token']
) )
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
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 '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 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]['title'] == 'just an activity'
assert data['data']['activities'][0]['ascent'] == 0.4 assert_activity_data_with_gpx(data)
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_invalid_payload(app): def test_edit_an_activity_with_gpx_invalid_payload(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, gpx_file
add_sport('cycling') ):
add_sport('running')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -180,10 +157,9 @@ def test_edit_an_activity_with_gpx_invalid_payload(app):
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_edit_an_activity_with_gpx_incorrect_data(app): def test_edit_an_activity_with_gpx_incorrect_data(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, gpx_file
add_sport('cycling') ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/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 assert 'Error. Please try again or contact the administrator.' in data['message'] # noqa
def test_edit_an_activity_wo_gpx(app): def test_edit_an_activity_wo_gpx(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1
add_sport('cycling') ):
activity = add_activity( activity_cycling_user_1.title = 'cycling - 2018-01-01 00:00:00'
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'
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -263,7 +232,6 @@ def test_edit_an_activity_wo_gpx(app):
)['auth_token'] )['auth_token']
) )
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 200 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 assert data['data']['activities'][0]['with_gpx'] is False
def test_edit_an_activity_wo_gpx_partial(app): def test_edit_an_activity_wo_gpx_partial(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1
add_sport('cycling') ):
activity = add_activity( activity_cycling_user_1.title = 'cycling - 2018-01-01 00:00:00'
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'
client = app.test_client() client = app.test_client()
resp_login = client.post( 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 assert data['data']['activities'][0]['with_gpx'] is False
def test_edit_an_activity_wo_gpx_invalid_payload(app): def test_edit_an_activity_wo_gpx_invalid_payload(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1
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)
)
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -383,17 +336,9 @@ def test_edit_an_activity_wo_gpx_invalid_payload(app):
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_edit_an_activity_wo_gpx_incorrect_data(app): def test_edit_an_activity_wo_gpx_incorrect_data(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1
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)
)
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -427,10 +372,9 @@ def test_edit_an_activity_wo_gpx_incorrect_data(app):
in data['message'] in data['message']
def test_edit_an_activity_no_activity(app): def test_edit_an_activity_no_activity(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling
add_sport('cycling') ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',

View File

@ -1,18 +1,16 @@
import datetime
import json import json
import os import os
from io import BytesIO from io import BytesIO
from mpwo_api.tests.utils import ( from mpwo_api.activities.models import Activity
add_activity, add_sport, add_user, get_gpx_filepath
)
from mpwo_api.tests.utils_gpx import gpx_file
def test_delete_an_activity_with_gpx(app): def get_gpx_filepath(activity_id):
add_user('test', 'test@test.com', '12345678') activity = Activity.query.filter_by(id=activity_id).first()
add_sport('cycling') return activity.gpx
def test_delete_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',
@ -47,17 +45,9 @@ def test_delete_an_activity_with_gpx(app):
assert response.status_code == 204 assert response.status_code == 204
def test_delete_an_activity_wo_gpx(app): def test_delete_an_activity_wo_gpx(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1
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)
)
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -75,13 +65,10 @@ def test_delete_an_activity_wo_gpx(app):
)['auth_token'] )['auth_token']
) )
) )
assert response.status_code == 204 assert response.status_code == 204
def test_delete_an_activity_no_activityy(app): def test_delete_an_activity_no_activityy(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -99,17 +86,13 @@ def test_delete_an_activity_no_activityy(app):
)['auth_token'] )['auth_token']
) )
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 404 assert response.status_code == 404
assert 'not found' in data['status'] assert 'not found' in data['status']
def test_delete_an_activity_with_gpx_invalid_file(app): def test_delete_an_activity_with_gpx_invalid_file(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, gpx_file):
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',

View File

@ -1,25 +1,15 @@
import datetime
from mpwo_api.tests.utils import add_activity, add_sport, add_user
def test_add_activity(app): def test_add_activity(
add_user('test', 'test@test.com', '12345678') app, sport_1_cycling, user_1, activity_cycling_user_1
add_sport('cycling') ):
activity = add_activity( activity_cycling_user_1.title = 'Test'
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'
assert 1 == activity.id assert 1 == activity_cycling_user_1.id
assert 1 == activity.user_id assert 1 == activity_cycling_user_1.user_id
assert 1 == activity.sport_id assert 1 == activity_cycling_user_1.sport_id
assert '2018-01-01 13:36:00' == str(activity.activity_date) assert '2018-01-01 00:00:00' == str(activity_cycling_user_1.activity_date)
assert 10.0 == float(activity.distance) assert 10.0 == float(activity_cycling_user_1.distance)
assert '0:17:04' == str(activity.duration) assert '0:17:04' == str(activity_cycling_user_1.duration)
assert 'Test' == activity.title assert 'Test' == activity_cycling_user_1.title
assert '<Activity \'cycling\' - 2018-01-01 13:36:00>' == str(activity) assert '<Activity \'Cycling\' - 2018-01-01 00:00:00>' == str(activity_cycling_user_1) # noqa

View File

@ -2,8 +2,6 @@ import json
import time import time
from io import BytesIO from io import BytesIO
from mpwo_api.tests.utils import add_user, add_user_full
def test_user_registration(app): def test_user_registration(app):
client = app.test_client() client = app.test_client()
@ -25,8 +23,7 @@ def test_user_registration(app):
assert response.status_code == 201 assert response.status_code == 201
def test_user_registration_user_already_exists(app): def test_user_registration_user_already_exists(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
@ -217,9 +214,26 @@ def test_user_registration_invalid_json_keys_no_password_conf(app):
assert 'error' in data['status'] 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() client = app.test_client()
add_user('test', 'test@test.com', '12345678')
response = client.post( response = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps(dict( data=json.dumps(dict(
@ -236,7 +250,7 @@ def test_registered_user_login(app):
assert response.status_code == 200 assert response.status_code == 200
def test_no_registered_user_login(app): def test_login_no_registered_user(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/login', '/api/auth/login',
@ -253,8 +267,21 @@ def test_no_registered_user_login(app):
assert response.status_code == 404 assert response.status_code == 404
def test_registered_user_login_invalid_password(app): def test_login_invalid_payload(app):
add_user('test', 'test@test.com', '12345678') 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() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/login', '/api/auth/login',
@ -271,8 +298,7 @@ def test_registered_user_login_invalid_password(app):
assert response.status_code == 404 assert response.status_code == 404
def test_valid_logout(app): def test_logout(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
# user login # user login
resp_login = client.post( resp_login = client.post(
@ -298,8 +324,7 @@ def test_valid_logout(app):
assert response.status_code == 200 assert response.status_code == 200
def test_invalid_logout_expired_token(app): def test_logout_expired_token(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -325,7 +350,7 @@ def test_invalid_logout_expired_token(app):
assert response.status_code == 401 assert response.status_code == 401
def test_invalid_logout(app): def test_logout_invalid(app):
client = app.test_client() client = app.test_client()
response = client.get( response = client.get(
'/api/auth/logout', '/api/auth/logout',
@ -336,8 +361,18 @@ def test_invalid_logout(app):
assert response.status_code == 401 assert response.status_code == 401
def test_user_profile_minimal(app): def test_logout_invalid_headers(app):
add_user('test', 'test@test.com', '12345678') 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -365,8 +400,7 @@ def test_user_profile_minimal(app):
assert response.status_code == 200 assert response.status_code == 200
def test_user_profile_full(app): def test_user_profile_full(app, user_1_full):
add_user_full('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -410,8 +444,41 @@ def test_invalid_profile(app):
assert response.status_code == 401 assert response.status_code == 401
def test_user_profile_valid_update(app): def test_user_profile_valid_update(app, user_1):
add_user('test', 'test@test.com', '12345678') 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -443,8 +510,7 @@ def test_user_profile_valid_update(app):
assert response.status_code == 200 assert response.status_code == 200
def test_user_profile_valid_update_with_one_field(app): def test_user_profile_valid_update_with_one_field(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -472,8 +538,7 @@ def test_user_profile_valid_update_with_one_field(app):
assert response.status_code == 200 assert response.status_code == 200
def test_user_profile_update_invalid_json(app): def test_user_profile_update_invalid_json(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -499,9 +564,74 @@ def test_user_profile_update_invalid_json(app):
assert 'error' in data['status'] assert 'error' in data['status']
def test_update_user_picture(app): def test_user_profile_invalid_password(app, user_1):
add_user('test', 'test@test.com', '12345678') 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -528,9 +658,7 @@ def test_update_user_picture(app):
assert response.status_code == 200 assert response.status_code == 200
def test_update_user_no_picture(app): def test_update_user_no_picture(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -554,9 +682,7 @@ def test_update_user_no_picture(app):
assert response.status_code == 400 assert response.status_code == 400
def test_update_user_invalid_picture(app): def test_update_user_invalid_picture(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',

View File

@ -1,42 +1,11 @@
import datetime
import json 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/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 '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]['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 3 == data['data']['records'][0]['activity_id']
assert 'FD' == data['data']['records'][0]['record_type'] assert 'FD' == data['data']['records'][0]['record_type']
assert 'value' in data['data']['records'][0] assert 'value' in data['data']['records'][0]
assert 'Mon, 01 Jan 2018 00:00:00 GMT' == data['data']['records'][1]['activity_date'] # noqa 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 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 1 == data['data']['records'][1]['activity_id']
assert 'LD' == data['data']['records'][1]['record_type'] assert 'LD' == data['data']['records'][1]['record_type']
assert 'value' in data['data']['records'][1] assert 'value' in data['data']['records'][1]

View File

@ -1,30 +1,18 @@
import datetime 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 '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
def test_add_record(app): record_serialize = record_ld.serialize()
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 '<Record cycling - LD - 2018-01-01>' == str(record)
record_serialize = record.serialize()
assert 'id' in record_serialize assert 'id' in record_serialize
assert 'user_id' in record_serialize assert 'user_id' in record_serialize
assert 'sport_id' in record_serialize assert 'sport_id' in record_serialize
@ -34,137 +22,83 @@ def test_add_record(app):
assert 'value' in record_serialize assert 'value' in record_serialize
def test_add_records_no_value(app): def test_add_record_no_value(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1, record_as
add_sport('cycling') ):
assert record_as.value is None
assert record_as._value is None
activity = add_activity( record_serialize = record_as.serialize()
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()
assert record_serialize.get('value') is None assert record_serialize.get('value') is None
def test_add_as_records(app): def test_add_as_records(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1, record_as
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')
# record.value = 4.6 # record.value = 4.6
record.value = 4.61 record_as.value = 4.61
assert isinstance(record.value, float) assert isinstance(record_as.value, float)
assert record.value == 4.61 assert record_as.value == 4.61
assert record._value == 461 assert record_as._value == 461
record_serialize = record.serialize() record_serialize = record_as.serialize()
assert record_serialize.get('value') == 4.61 assert record_serialize.get('value') == 4.61
assert isinstance(record_serialize.get('value'), float) assert isinstance(record_serialize.get('value'), float)
def test_add_fd_records(app): def test_add_fd_records(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1, record_fd
add_sport('cycling') ):
record_fd.value = 0.322
activity = add_activity( assert isinstance(record_fd.value, float)
user_id=1, assert record_fd.value == 0.322
sport_id=1, assert record_fd._value == 322
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.value, float) record_serialize = record_fd.serialize()
assert record.value == 0.322
assert record._value == 322
record_serialize = record.serialize()
assert record_serialize.get('value') == 0.322 assert record_serialize.get('value') == 0.322
assert isinstance(record_serialize.get('value'), float) assert isinstance(record_serialize.get('value'), float)
def test_add_ld_records(app): def test_add_ld_records(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1, record_ld
add_sport('cycling') ):
record_ld.value = activity_cycling_user_1.duration
activity = add_activity( assert isinstance(record_ld.value, datetime.timedelta)
user_id=1, assert str(record_ld.value) == '0:17:04'
sport_id=1, assert record_ld._value == 1024
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.value, datetime.timedelta) record_serialize = record_ld.serialize()
assert str(record.value) == '0:17:04'
assert record._value == 1024
record_serialize = record.serialize()
assert record_serialize.get('value') == '0:17:04' assert record_serialize.get('value') == '0:17:04'
assert isinstance(record_serialize.get('value'), str) assert isinstance(record_serialize.get('value'), str)
def test_add_ld_records_zero(app): def test_add_ld_records_zero(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1, record_ld
add_sport('cycling') ):
activity_cycling_user_1.duration = datetime.timedelta(seconds=0)
record_ld.value = activity_cycling_user_1.duration
activity = add_activity( assert isinstance(record_ld.value, datetime.timedelta)
user_id=1, assert str(record_ld.value) == '0:00:00'
sport_id=1, assert record_ld._value == 0
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.value, datetime.timedelta) record_serialize = record_ld.serialize()
assert str(record.value) == '0:00:00'
assert record._value == 0
record_serialize = record.serialize()
assert record_serialize.get('value') == '0:00:00' assert record_serialize.get('value') == '0:00:00'
assert isinstance(record_serialize.get('value'), str) assert isinstance(record_serialize.get('value'), str)
def test_add_ms_records_no_value(app): def test_add_ms_records_no_value(
add_user('test', 'test@test.com', '12345678') app, user_1, sport_1_cycling, activity_cycling_user_1, record_ms
add_sport('cycling') ):
record_ms.value = 23.5
activity = add_activity( assert isinstance(record_ms.value, float)
user_id=1, assert record_ms.value == 23.5
sport_id=1, assert record_ms._value == 2350
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.value, float) record_serialize = record_ms.serialize()
assert record.value == 23.5
assert record._value == 2350
record_serialize = record.serialize()
assert record_serialize.get('value') == 23.5 assert record_serialize.get('value') == 23.5
assert isinstance(record_serialize.get('value'), float) assert isinstance(record_serialize.get('value'), float)

View File

@ -1,14 +1,19 @@
import datetime
import json 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): def test_get_all_sports(app, user_1, sport_1_cycling, sport_2_running):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
add_sport('running')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -32,14 +37,11 @@ def test_get_all_sports(app):
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['sports']) == 2 assert len(data['data']['sports']) == 2
assert 'cycling' in data['data']['sports'][0]['label'] assert data['data']['sports'][0] == expected_sport_1_cycling_result
assert 'running' in data['data']['sports'][1]['label'] assert data['data']['sports'][1] == expected_sport_2_running_result
def test_get_a_sport(app): def test_get_a_sport(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -63,12 +65,35 @@ def test_get_a_sport(app):
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['sports']) == 1 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): def test_get_a_sport_invalid(app, user_1):
add_admin() 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -82,7 +107,7 @@ def test_add_a_sport(app):
'/api/sports', '/api/sports',
content_type='application/json', content_type='application/json',
data=json.dumps(dict( data=json.dumps(dict(
label='surfing' label='Cycling'
)), )),
headers=dict( headers=dict(
Authorization='Bearer ' + json.loads( Authorization='Bearer ' + json.loads(
@ -96,12 +121,10 @@ def test_add_a_sport(app):
assert 'created' in data['status'] assert 'created' in data['status']
assert len(data['data']['sports']) == 1 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): def test_add_a_sport_not_admin(app, user_1):
add_user('test', 'test@test.com', '12345678')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -125,16 +148,40 @@ def test_add_a_sport_not_admin(app):
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 401 assert response.status_code == 403
assert 'created' not in data['status'] assert 'created' not in data['status']
assert 'error' in data['status'] assert 'error' in data['status']
assert 'You do not have permissions.' in data['message'] assert 'You do not have permissions.' in data['message']
def test_update_a_sport(app): def test_add_a_sport_invalid_payload(app, user_1_admin):
add_admin() client = app.test_client()
add_sport('cycling') 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -165,10 +212,7 @@ def test_update_a_sport(app):
assert 'cycling updated' in data['data']['sports'][0]['label'] assert 'cycling updated' in data['data']['sports'][0]['label']
def test_update_a_sport_not_admin(app): def test_update_a_sport_not_admin(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -192,16 +236,69 @@ def test_update_a_sport_not_admin(app):
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 401 assert response.status_code == 403
assert 'success' not in data['status'] assert 'success' not in data['status']
assert 'error' in data['status'] assert 'error' in data['status']
assert 'You do not have permissions.' in data['message'] assert 'You do not have permissions.' in data['message']
def test_delete_a_sport(app): def test_update_a_sport_invalid_payload(app, user_1_admin):
add_admin() client = app.test_client()
add_sport('cycling') 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -223,10 +320,7 @@ def test_delete_a_sport(app):
assert response.status_code == 204 assert response.status_code == 204
def test_delete_a_sport_not_admin(app): def test_delete_a_sport_not_admin(app, user_1, sport_1_cycling):
add_user('test', 'test@test.com', '12345678')
add_sport('cycling')
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -247,22 +341,41 @@ def test_delete_a_sport_not_admin(app):
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 401 assert response.status_code == 403
assert 'error' in data['status'] assert 'error' in data['status']
assert 'You do not have permissions.' in data['message'] assert 'You do not have permissions.' in data['message']
def test_delete_a_sport_with_an_activity(app): def test_delete_a_sport_invalid_id(app, user_1_admin):
add_admin() client = app.test_client()
add_sport('cycling') resp_login = client.post(
add_activity( '/api/auth/login',
user_id=1, data=json.dumps(dict(
sport_id=1, email='admin@example.com',
activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), password='12345678'
distance=10, )),
duration=datetime.timedelta(seconds=1024) 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() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',

View File

@ -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 '<Sport \'Cycling\'>' == 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): def test_sport_model_with_activity(
add_user('test', 'test@test.com', '12345678') app, sport_1_cycling, user_1, activity_cycling_user_1
sport = add_sport('cycling') ):
assert 1 == sport_1_cycling.id
assert 'Cycling' == sport_1_cycling.label
assert '<Sport \'Cycling\'>' == str(sport_1_cycling)
assert 1 == sport.id serialized_sport = sport_1_cycling.serialize()
assert 'cycling' == sport.label assert 1 == serialized_sport['id']
assert '<Sport \'cycling\'>' == str(sport) assert 'Cycling' == serialized_sport['label']
assert serialized_sport['_can_be_deleted'] is False

View File

@ -1,6 +1,5 @@
import json import json
from mpwo_api.tests.utils import add_user
from mpwo_api.users.models import User from mpwo_api.users.models import User
@ -14,12 +13,11 @@ def test_ping(app):
assert 'success' in data['status'] assert 'success' in data['status']
def test_single_user(app): def test_single_user(app, user_1):
"""=> Get single user details""" """=> Get single user details"""
user = add_user('test', 'test@test.com', 'test')
client = app.test_client() 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()) data = json.loads(response.data.decode())
assert response.status_code == 200 assert response.status_code == 200
@ -52,10 +50,8 @@ def test_single_user_wrong_id(app):
assert 'User does not exist' in data['message'] 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.""" """=> Ensure get single user behaves correctly."""
add_user('test', 'test@test.com', 'test')
add_user('toto', 'toto@toto.com', 'toto')
client = app.test_client() client = app.test_client()
response = client.get('/api/users') response = client.get('/api/users')
@ -73,15 +69,13 @@ def test_users_list(app):
assert 'toto@toto.com' in data['data']['users'][1]['email'] 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""" """=> Ensure correct auth token generation"""
user = add_user('test', 'test@test.com', 'test') auth_token = user_1.encode_auth_token(user_1.id)
auth_token = user.encode_auth_token(user.id)
assert isinstance(auth_token, bytes) assert isinstance(auth_token, bytes)
def test_decode_auth_token(app): def test_decode_auth_token(app, user_1):
user = add_user('test', 'test@test.com', 'test') auth_token = user_1.encode_auth_token(user_1.id)
auth_token = user.encode_auth_token(user.id)
assert isinstance(auth_token, bytes) assert isinstance(auth_token, bytes)
assert User.decode_auth_token(auth_token) == user.id assert User.decode_auth_token(auth_token) == user_1.id

View File

@ -1,6 +1,14 @@
from mpwo_api.tests.utils import add_user def test_user_model(app, user_1):
assert '<User \'test\'>' == str(user_1)
serialized_user = user_1.serialize()
def test_add_user(app): assert 1 == serialized_user['id']
user = add_user('test', 'test@test.com', '12345678') assert 'test' == serialized_user['username']
assert '<User \'test\'>' == str(user) 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

View File

@ -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

View File

@ -1,231 +0,0 @@
gpx_file = (
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
' <metadata/>'
' <trk>'
' <name>just an activity</name>'
' <trkseg>'
' <trkpt lat="44.68095" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:45Z</time>'
' </trkpt>'
' <trkpt lat="44.68091" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:50Z</time>'
' </trkpt>'
' <trkpt lat="44.6808" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:00Z</time>'
' </trkpt>'
' <trkpt lat="44.68075" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:05Z</time>'
' </trkpt>'
' <trkpt lat="44.68071" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:10Z</time>'
' </trkpt>'
' <trkpt lat="44.68049" lon="6.07361">'
' <ele>993</ele>'
' <time>2018-03-13T12:45:30Z</time>'
' </trkpt>'
' <trkpt lat="44.68019" lon="6.07356">'
' <ele>992</ele>'
' <time>2018-03-13T12:45:55Z</time>'
' </trkpt>'
' <trkpt lat="44.68014" lon="6.07355">'
' <ele>992</ele>'
' <time>2018-03-13T12:46:00Z</time>'
' </trkpt>'
' <trkpt lat="44.67995" lon="6.07358">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67977" lon="6.07364">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:30Z</time>'
' </trkpt>'
' <trkpt lat="44.67972" lon="6.07367">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67966" lon="6.07368">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67961" lon="6.0737">'
' <ele>986</ele>'
' <time>2018-03-13T12:46:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67938" lon="6.07377">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:05Z</time>'
' </trkpt>'
' <trkpt lat="44.67933" lon="6.07381">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:10Z</time>'
' </trkpt>'
' <trkpt lat="44.67922" lon="6.07385">'
' <ele>985</ele>'
' <time>2018-03-13T12:47:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67911" lon="6.0739">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:30Z</time>'
' </trkpt>'
' <trkpt lat="44.679" lon="6.07399">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67896" lon="6.07402">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67884" lon="6.07408">'
' <ele>979</ele>'
' <time>2018-03-13T12:47:55Z</time>'
' </trkpt>'
' <trkpt lat="44.67863" lon="6.07423">'
' <ele>981</ele>'
' <time>2018-03-13T12:48:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67858" lon="6.07425">'
' <ele>980</ele>'
' <time>2018-03-13T12:48:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67842" lon="6.07434">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67837" lon="6.07435">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67822" lon="6.07442">'
' <ele>975</ele>'
' <time>2018-03-13T12:48:55Z</time>'
' </trkpt>'
' </trkseg>'
' </trk>'
'</gpx>'
)
gpx_file_wo_name = (
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
' <metadata/>'
' <trk>'
' <trkseg>'
' <trkpt lat="44.68095" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:45Z</time>'
' </trkpt>'
' <trkpt lat="44.68091" lon="6.07367">'
' <ele>998</ele>'
' <time>2018-03-13T12:44:50Z</time>'
' </trkpt>'
' <trkpt lat="44.6808" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:00Z</time>'
' </trkpt>'
' <trkpt lat="44.68075" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:05Z</time>'
' </trkpt>'
' <trkpt lat="44.68071" lon="6.07364">'
' <ele>994</ele>'
' <time>2018-03-13T12:45:10Z</time>'
' </trkpt>'
' <trkpt lat="44.68049" lon="6.07361">'
' <ele>993</ele>'
' <time>2018-03-13T12:45:30Z</time>'
' </trkpt>'
' <trkpt lat="44.68019" lon="6.07356">'
' <ele>992</ele>'
' <time>2018-03-13T12:45:55Z</time>'
' </trkpt>'
' <trkpt lat="44.68014" lon="6.07355">'
' <ele>992</ele>'
' <time>2018-03-13T12:46:00Z</time>'
' </trkpt>'
' <trkpt lat="44.67995" lon="6.07358">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67977" lon="6.07364">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:30Z</time>'
' </trkpt>'
' <trkpt lat="44.67972" lon="6.07367">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67966" lon="6.07368">'
' <ele>987</ele>'
' <time>2018-03-13T12:46:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67961" lon="6.0737">'
' <ele>986</ele>'
' <time>2018-03-13T12:46:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67938" lon="6.07377">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:05Z</time>'
' </trkpt>'
' <trkpt lat="44.67933" lon="6.07381">'
' <ele>986</ele>'
' <time>2018-03-13T12:47:10Z</time>'
' </trkpt>'
' <trkpt lat="44.67922" lon="6.07385">'
' <ele>985</ele>'
' <time>2018-03-13T12:47:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67911" lon="6.0739">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:30Z</time>'
' </trkpt>'
' <trkpt lat="44.679" lon="6.07399">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67896" lon="6.07402">'
' <ele>980</ele>'
' <time>2018-03-13T12:47:45Z</time>'
' </trkpt>'
' <trkpt lat="44.67884" lon="6.07408">'
' <ele>979</ele>'
' <time>2018-03-13T12:47:55Z</time>'
' </trkpt>'
' <trkpt lat="44.67863" lon="6.07423">'
' <ele>981</ele>'
' <time>2018-03-13T12:48:15Z</time>'
' </trkpt>'
' <trkpt lat="44.67858" lon="6.07425">'
' <ele>980</ele>'
' <time>2018-03-13T12:48:20Z</time>'
' </trkpt>'
' <trkpt lat="44.67842" lon="6.07434">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:35Z</time>'
' </trkpt>'
' <trkpt lat="44.67837" lon="6.07435">'
' <ele>979</ele>'
' <time>2018-03-13T12:48:40Z</time>'
' </trkpt>'
' <trkpt lat="44.67822" lon="6.07442">'
' <ele>975</ele>'
' <time>2018-03-13T12:48:55Z</time>'
' </trkpt>'
' </trkseg>'
' </trk>'
'</gpx>'
)
gpx_file_wo_track = (
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
'<gpx xmlns:gpxdata="http://www.cluetrust.com/XML/GPXDATA/1/0" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxext="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns="http://www.topografix.com/GPX/1/1">' # noqa
' <metadata/>'
'</gpx>'
)
gpx_file_empty = ''

View File

@ -30,7 +30,17 @@ def register_user():
password = post_data.get('password') password = post_data.get('password')
password_conf = post_data.get('password_conf') 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 != '': if ret != '':
response_object = { response_object = {
'status': 'error', 'status': 'error',
@ -74,7 +84,7 @@ def register_user():
'status': 'error', 'status': 'error',
'message': 'Error. Please try again or contact the administrator.' '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']) @auth_blueprint.route('/auth/login', methods=['POST'])
@ -143,7 +153,7 @@ def logout_user(user_id):
'status': 'error', 'status': 'error',
'message': 'Provide a valid auth token.' 'message': 'Provide a valid auth token.'
} }
return jsonify(response_object), 403 return jsonify(response_object), 401
@auth_blueprint.route('/auth/profile', methods=['GET']) @auth_blueprint.route('/auth/profile', methods=['GET'])

View File

@ -67,7 +67,6 @@ def verify_user(current_request, verify_admin):
auth_header = current_request.headers.get('Authorization') auth_header = current_request.headers.get('Authorization')
if not auth_header: if not auth_header:
response_object['message'] = 'Provide a valid auth token.' response_object['message'] = 'Provide a valid auth token.'
code = 403
return response_object, code, None return response_object, code, None
auth_token = auth_header.split(" ")[1] auth_token = auth_header.split(" ")[1]
resp = User.decode_auth_token(auth_token) resp = User.decode_auth_token(auth_token)
@ -79,7 +78,7 @@ def verify_user(current_request, verify_admin):
return response_object, code, None return response_object, code, None
if verify_admin and not is_admin(resp): if verify_admin and not is_admin(resp):
response_object['message'] = 'You do not have permissions.' response_object['message'] = 'You do not have permissions.'
return response_object, code, None return response_object, 403, None
return None, None, resp return None, None, resp