API - refactor tests with classes

This commit is contained in:
Sam 2020-05-10 15:55:56 +02:00
parent 0c07ce172a
commit f53213b4e9
20 changed files with 7990 additions and 7874 deletions

View File

@ -415,14 +415,17 @@
<dt class="field-odd">Status Codes</dt> <dt class="field-odd">Status Codes</dt>
<dd class="field-odd"><ul class="simple"> <dd class="field-odd"><ul class="simple">
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1">200 OK</a> success</p></li> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1">200 OK</a> success</p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1">400 Bad Request</a> no gpx file for this activity</p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">401 Unauthorized</a> <ul> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">401 Unauthorized</a> <ul>
<li><p>Provide a valid auth token.</p></li> <li><p>Provide a valid auth token.</p></li>
<li><p>Signature expired. Please log in again.</p></li> <li><p>Signature expired. Please log in again.</p></li>
<li><p>Invalid token. Please log in again.</p></li> <li><p>Invalid token. Please log in again.</p></li>
</ul> </ul>
</p></li> </p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">404 Not Found</a> activity not found</p></li> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">404 Not Found</a> <ul>
<li><p>activity not found</p></li>
<li><p>no gpx file for this activity</p></li>
</ul>
</p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">500 Internal Server Error</a> </p></li> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">500 Internal Server Error</a> </p></li>
</ul> </ul>
</dd> </dd>
@ -485,14 +488,17 @@
<dt class="field-odd">Status Codes</dt> <dt class="field-odd">Status Codes</dt>
<dd class="field-odd"><ul class="simple"> <dd class="field-odd"><ul class="simple">
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1">200 OK</a> success</p></li> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1">200 OK</a> success</p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1">400 Bad Request</a> no gpx file for this activity</p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">401 Unauthorized</a> <ul> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">401 Unauthorized</a> <ul>
<li><p>Provide a valid auth token.</p></li> <li><p>Provide a valid auth token.</p></li>
<li><p>Signature expired. Please log in again.</p></li> <li><p>Signature expired. Please log in again.</p></li>
<li><p>Invalid token. Please log in again.</p></li> <li><p>Invalid token. Please log in again.</p></li>
</ul> </ul>
</p></li> </p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">404 Not Found</a> activity not found</p></li> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">404 Not Found</a> <ul>
<li><p>activity not found</p></li>
<li><p>no gpx file for this activity</p></li>
</ul>
</p></li>
<li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">500 Internal Server Error</a> </p></li> <li><p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">500 Internal Server Error</a> </p></li>
</ul> </ul>
</dd> </dd>

View File

@ -390,8 +390,8 @@ def get_activity_data(auth_user_id, activity_id, data_type, segment_id=None):
return jsonify(response_object), code return jsonify(response_object), code
if not activity.gpx or activity.gpx == '': if not activity.gpx or activity.gpx == '':
message = f'No gpx file for this activity (id: {activity_id})' message = f'No gpx file for this activity (id: {activity_id})'
response_object = {'status': 'fail', 'message': message} response_object = {'status': 'error', 'message': message}
return jsonify(response_object), 400 return jsonify(response_object), 404
try: try:
absolute_gpx_filepath = get_absolute_file_path(activity.gpx) absolute_gpx_filepath = get_absolute_file_path(activity.gpx)
@ -470,12 +470,13 @@ def get_activity_gpx(auth_user_id, activity_id):
:reqheader Authorization: OAuth 2.0 Bearer Token :reqheader Authorization: OAuth 2.0 Bearer Token
:statuscode 200: success :statuscode 200: success
:statuscode 400: no gpx file for this activity
:statuscode 401: :statuscode 401:
- Provide a valid auth token. - Provide a valid auth token.
- Signature expired. Please log in again. - Signature expired. Please log in again.
- Invalid token. Please log in again. - Invalid token. Please log in again.
:statuscode 404: activity not found :statuscode 404:
- activity not found
- no gpx file for this activity
:statuscode 500: :statuscode 500:
""" """
@ -537,12 +538,13 @@ def get_activity_chart_data(auth_user_id, activity_id):
:reqheader Authorization: OAuth 2.0 Bearer Token :reqheader Authorization: OAuth 2.0 Bearer Token
:statuscode 200: success :statuscode 200: success
:statuscode 400: no gpx file for this activity
:statuscode 401: :statuscode 401:
- Provide a valid auth token. - Provide a valid auth token.
- Signature expired. Please log in again. - Signature expired. Please log in again.
- Invalid token. Please log in again. - Invalid token. Please log in again.
:statuscode 404: activity not found :statuscode 404:
- activity not found
- no gpx file for this activity
:statuscode 500: :statuscode 500:
""" """
@ -703,7 +705,7 @@ def get_map(map_id):
activity = Activity.query.filter_by(map_id=map_id).first() activity = Activity.query.filter_by(map_id=map_id).first()
if not activity: if not activity:
response_object = { response_object = {
'status': 'fail', 'status': 'error',
'message': 'Map does not exist', 'message': 'Map does not exist',
} }
return jsonify(response_object), 404 return jsonify(response_object), 404

View File

@ -168,7 +168,7 @@ def activity_cycling_user_1():
sport_id=1, sport_id=1,
activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'), activity_date=datetime.datetime.strptime('01/01/2018', '%d/%m/%Y'),
distance=10, distance=10,
duration=datetime.timedelta(seconds=1024), duration=datetime.timedelta(seconds=3600),
) )
activity.max_speed = 10 activity.max_speed = 10
activity.ave_speed = 10 activity.ave_speed = 10

View File

@ -9,7 +9,7 @@ def assert_activity_data_with_gpx(data):
assert ( assert (
'Tue, 13 Mar 2018 12:44:45 GMT' 'Tue, 13 Mar 2018 12:44:45 GMT'
== data['data']['activities'][0]['activity_date'] == data['data']['activities'][0]['activity_date']
) # noqa )
assert 'test' == data['data']['activities'][0]['user'] assert 'test' == data['data']['activities'][0]['user']
assert '0:04:10' == data['data']['activities'][0]['duration'] assert '0:04:10' == data['data']['activities'][0]['duration']
assert data['data']['activities'][0]['ascent'] == 0.4 assert data['data']['activities'][0]['ascent'] == 0.4
@ -47,9 +47,10 @@ def assert_activity_data_with_gpx(data):
assert records[3]['value'] == 4.61 assert records[3]['value'] == 4.61
def test_edit_an_activity_with_gpx( class TestEditActivityWithGpx:
app, user_1, sport_1_cycling, sport_2_running, gpx_file def test_it_updates_title_for_an_activity_with_gpx(
): self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -68,6 +69,7 @@ def test_edit_an_activity_with_gpx(
+ json.loads(resp_login.data.decode())['auth_token'], + json.loads(resp_login.data.decode())['auth_token'],
), ),
) )
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -77,8 +79,8 @@ def test_edit_an_activity_with_gpx(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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
@ -86,6 +88,28 @@ def test_edit_an_activity_with_gpx(
assert data['data']['activities'][0]['title'] == 'Activity test' assert data['data']['activities'][0]['title'] == 'Activity test'
assert_activity_data_with_gpx(data) assert_activity_data_with_gpx(data)
def test_it_adds_notes_for_an_activity_with_gpx(
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
):
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',
)
client.post(
'/api/activities',
data=dict(
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
data='{"sport_id": 1}',
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
),
)
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -95,18 +119,17 @@ def test_edit_an_activity_with_gpx(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['activities'][0]['title'] == 'Activity test' assert data['data']['activities'][0]['title'] == 'just an activity'
assert data['data']['activities'][0]['notes'] == 'test notes' assert data['data']['activities'][0]['notes'] == 'test notes'
def test_it_raises_403_when_editing_an_activity_from_different_user(
def test_edit_an_activity_with_gpx_different_user( self, app, user_1, user_2, sport_1_cycling, sport_2_running, gpx_file
app, user_1, user_2, sport_1_cycling, sport_2_running, gpx_file ):
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -130,6 +153,7 @@ def test_edit_an_activity_with_gpx_different_user(
data=json.dumps(dict(email='toto@toto.com', password='87654321')), data=json.dumps(dict(email='toto@toto.com', password='87654321')),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -139,16 +163,15 @@ def test_edit_an_activity_with_gpx_different_user(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 403 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_it_updates_sport(
def test_edit_an_activity_with_gpx_partial( self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
app, user_1, sport_1_cycling, sport_2_running, gpx_file ):
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -167,6 +190,7 @@ def test_edit_an_activity_with_gpx_partial(
+ json.loads(resp_login.data.decode())['auth_token'], + json.loads(resp_login.data.decode())['auth_token'],
), ),
) )
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -176,8 +200,8 @@ def test_edit_an_activity_with_gpx_partial(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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
@ -185,10 +209,9 @@ def test_edit_an_activity_with_gpx_partial(
assert data['data']['activities'][0]['title'] == 'just an activity' assert data['data']['activities'][0]['title'] == 'just an activity'
assert_activity_data_with_gpx(data) assert_activity_data_with_gpx(data)
def test_it_returns_400_if_payload_is_empty(
def test_edit_an_activity_with_gpx_invalid_payload( self, app, user_1, sport_1_cycling, gpx_file
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',
@ -207,6 +230,7 @@ def test_edit_an_activity_with_gpx_invalid_payload(
+ json.loads(resp_login.data.decode())['auth_token'], + json.loads(resp_login.data.decode())['auth_token'],
), ),
) )
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -218,15 +242,13 @@ def test_edit_an_activity_with_gpx_invalid_payload(
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_it_raises_500_if_sport_does_not_exists(
def test_edit_an_activity_with_gpx_incorrect_data( self, app, user_1, sport_1_cycling, gpx_file
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',
@ -245,6 +267,7 @@ def test_edit_an_activity_with_gpx_incorrect_data(
+ json.loads(resp_login.data.decode())['auth_token'], + json.loads(resp_login.data.decode())['auth_token'],
), ),
) )
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -256,18 +279,23 @@ def test_edit_an_activity_with_gpx_incorrect_data(
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert ( assert (
'Error. Please try again or contact the administrator.' 'Error. Please try again or contact the administrator.'
in data['message'] in data['message']
) # noqa )
def test_edit_an_activity_wo_gpx( class TestEditActivityWithoutGpx:
app, user_1, sport_1_cycling, sport_2_running def test_it_updates_an_activity_wo_gpx(
): self,
app,
user_1,
sport_1_cycling,
sport_2_running,
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',
@ -275,76 +303,6 @@ def test_edit_an_activity_wo_gpx(
content_type='application/json', content_type='application/json',
) )
response = client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(
dict(
sport_id=1,
duration=3600,
activity_date='2018-05-14 14:05',
distance=7,
title='Activity test',
)
),
headers=dict(
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 'creation_date' in data['data']['activities'][0]
assert (
data['data']['activities'][0]['activity_date']
== 'Mon, 14 May 2018 14:05:00 GMT'
) # noqa
assert data['data']['activities'][0]['user'] == 'test'
assert data['data']['activities'][0]['sport_id'] == 1
assert data['data']['activities'][0]['duration'] == '1:00:00'
assert data['data']['activities'][0]['title'] == 'Activity test' # noqa
assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] == 7.0
assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] == 7.0
assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] == 7.0
assert data['data']['activities'][0]['min_alt'] is None
assert data['data']['activities'][0]['moving'] == '1:00:00'
assert data['data']['activities'][0]['pauses'] is None
assert data['data']['activities'][0]['with_gpx'] is False
assert data['data']['activities'][0]['map'] is None
assert data['data']['activities'][0]['weather_start'] is None
assert data['data']['activities'][0]['weather_end'] is None
assert data['data']['activities'][0]['notes'] is None
records = data['data']['activities'][0]['records']
assert len(records) == 4
assert records[0]['sport_id'] == 1
assert records[0]['activity_id'] == 1
assert records[0]['record_type'] == 'MS'
assert records[0]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[0]['value'] == 7.0
assert records[1]['sport_id'] == 1
assert records[1]['activity_id'] == 1
assert records[1]['record_type'] == 'LD'
assert records[1]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[1]['value'] == '1:00:00'
assert records[2]['sport_id'] == 1
assert records[2]['activity_id'] == 1
assert records[2]['record_type'] == 'FD'
assert records[2]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[2]['value'] == 7.0
assert records[3]['sport_id'] == 1
assert records[3]['activity_id'] == 1
assert records[3]['record_type'] == 'AS'
assert records[3]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[3]['value'] == 7.0
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -362,6 +320,7 @@ def test_edit_an_activity_wo_gpx(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 200 assert response.status_code == 200
@ -371,7 +330,7 @@ def test_edit_an_activity_wo_gpx(
assert ( assert (
data['data']['activities'][0]['activity_date'] data['data']['activities'][0]['activity_date']
== 'Tue, 15 May 2018 15:05:00 GMT' == 'Tue, 15 May 2018 15:05:00 GMT'
) # noqa )
assert data['data']['activities'][0]['user'] == 'test' assert data['data']['activities'][0]['user'] == 'test'
assert data['data']['activities'][0]['sport_id'] == 2 assert data['data']['activities'][0]['sport_id'] == 2
assert data['data']['activities'][0]['duration'] == '1:00:00' assert data['data']['activities'][0]['duration'] == '1:00:00'
@ -414,6 +373,16 @@ def test_edit_an_activity_wo_gpx(
assert records[3]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT' assert records[3]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
assert records[3]['value'] == 8.0 assert records[3]['value'] == 8.0
def test_it_adds_notes_to_an_activity_wo_gpx(
self, 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.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -423,26 +392,26 @@ def test_edit_an_activity_wo_gpx(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 'creation_date' in data['data']['activities'][0]
assert ( assert (
data['data']['activities'][0]['activity_date'] data['data']['activities'][0]['activity_date']
== 'Tue, 15 May 2018 15:05:00 GMT' == 'Mon, 01 Jan 2018 00:00:00 GMT'
) # noqa )
assert data['data']['activities'][0]['user'] == 'test' assert data['data']['activities'][0]['user'] == 'test'
assert data['data']['activities'][0]['sport_id'] == 2 assert data['data']['activities'][0]['sport_id'] == 1
assert data['data']['activities'][0]['duration'] == '1:00:00' assert data['data']['activities'][0]['duration'] == '1:00:00'
assert data['data']['activities'][0]['title'] == 'Activity test' assert data['data']['activities'][0]['title'] is None
assert data['data']['activities'][0]['ascent'] is None assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] == 8.0 assert data['data']['activities'][0]['ave_speed'] == 10.0
assert data['data']['activities'][0]['descent'] is None assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] == 8.0 assert data['data']['activities'][0]['distance'] == 10.0
assert data['data']['activities'][0]['max_alt'] is None assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] == 8.0 assert data['data']['activities'][0]['max_speed'] == 10.0
assert data['data']['activities'][0]['min_alt'] is None assert data['data']['activities'][0]['min_alt'] is None
assert data['data']['activities'][0]['moving'] == '1:00:00' assert data['data']['activities'][0]['moving'] == '1:00:00'
assert data['data']['activities'][0]['pauses'] is None assert data['data']['activities'][0]['pauses'] is None
@ -454,60 +423,37 @@ def test_edit_an_activity_wo_gpx(
records = data['data']['activities'][0]['records'] records = data['data']['activities'][0]['records']
assert len(records) == 4 assert len(records) == 4
assert records[0]['sport_id'] == 2 assert records[0]['sport_id'] == 1
assert records[0]['activity_id'] == 1 assert records[0]['activity_id'] == 1
assert records[0]['record_type'] == 'MS' assert records[0]['record_type'] == 'MS'
assert records[0]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT' assert records[0]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[0]['value'] == 8.0 assert records[0]['value'] == 10.0
assert records[1]['sport_id'] == 2 assert records[1]['sport_id'] == 1
assert records[1]['activity_id'] == 1 assert records[1]['activity_id'] == 1
assert records[1]['record_type'] == 'LD' assert records[1]['record_type'] == 'LD'
assert records[1]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT' assert records[1]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[1]['value'] == '1:00:00' assert records[1]['value'] == '1:00:00'
assert records[2]['sport_id'] == 2 assert records[2]['sport_id'] == 1
assert records[2]['activity_id'] == 1 assert records[2]['activity_id'] == 1
assert records[2]['record_type'] == 'FD' assert records[2]['record_type'] == 'FD'
assert records[2]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT' assert records[2]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[2]['value'] == 8.0 assert records[2]['value'] == 10.0
assert records[3]['sport_id'] == 2 assert records[3]['sport_id'] == 1
assert records[3]['activity_id'] == 1 assert records[3]['activity_id'] == 1
assert records[3]['record_type'] == 'AS' assert records[3]['record_type'] == 'AS'
assert records[3]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT' assert records[3]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[3]['value'] == 8.0 assert records[3]['value'] == 10.0
def test_returns_403_when_editing_an_activity_wo_gpx_from_different_user(
def test_edit_an_activity_wo_gpx_different_user( self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_2
app, user_1, user_2, 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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(
dict(
sport_id=1,
duration=3600,
activity_date='2018-05-14 14:05',
distance=7,
title='Activity test',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
content_type='application/json',
)
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -525,16 +471,20 @@ def test_edit_an_activity_wo_gpx_different_user(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 403 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_it_updates_an_activity_wo_gpx_with_timezone(
def test_edit_an_activity_wo_gpx_timezone( self,
app, user_1_paris, sport_1_cycling, sport_2_running app,
): user_1_paris,
sport_1_cycling,
sport_2_running,
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',
@ -542,74 +492,6 @@ def test_edit_an_activity_wo_gpx_timezone(
content_type='application/json', content_type='application/json',
) )
response = client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(
dict(
sport_id=1,
duration=3600,
activity_date='2018-05-14 14:05',
distance=7,
)
),
headers=dict(
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 'creation_date' in data['data']['activities'][0]
assert (
data['data']['activities'][0]['activity_date']
== 'Mon, 14 May 2018 12:05:00 GMT'
) # noqa
assert data['data']['activities'][0]['user'] == 'test'
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-14 14:05:00'
) # noqa
assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] == 7.0
assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] == 7.0
assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] == 7.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
records = data['data']['activities'][0]['records']
assert len(records) == 4
assert records[0]['sport_id'] == 1
assert records[0]['activity_id'] == 1
assert records[0]['record_type'] == 'MS'
assert records[0]['activity_date'] == 'Mon, 14 May 2018 12:05:00 GMT'
assert records[0]['value'] == 7.0
assert records[1]['sport_id'] == 1
assert records[1]['activity_id'] == 1
assert records[1]['record_type'] == 'LD'
assert records[1]['activity_date'] == 'Mon, 14 May 2018 12:05:00 GMT'
assert records[1]['value'] == '1:00:00'
assert records[2]['sport_id'] == 1
assert records[2]['activity_id'] == 1
assert records[2]['record_type'] == 'FD'
assert records[2]['activity_date'] == 'Mon, 14 May 2018 12:05:00 GMT'
assert records[2]['value'] == 7.0
assert records[3]['sport_id'] == 1
assert records[3]['activity_id'] == 1
assert records[3]['record_type'] == 'AS'
assert records[3]['activity_date'] == 'Mon, 14 May 2018 12:05:00 GMT'
assert records[3]['value'] == 7.0
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -636,11 +518,11 @@ def test_edit_an_activity_wo_gpx_timezone(
assert ( assert (
data['data']['activities'][0]['activity_date'] data['data']['activities'][0]['activity_date']
== 'Tue, 15 May 2018 13:05:00 GMT' == 'Tue, 15 May 2018 13:05:00 GMT'
) # noqa )
assert data['data']['activities'][0]['user'] == 'test' assert data['data']['activities'][0]['user'] == 'test'
assert data['data']['activities'][0]['sport_id'] == 2 assert data['data']['activities'][0]['sport_id'] == 2
assert data['data']['activities'][0]['duration'] == '1:00:00' assert data['data']['activities'][0]['duration'] == '1:00:00'
assert data['data']['activities'][0]['title'] == 'Activity test' # noqa assert data['data']['activities'][0]['title'] == 'Activity test'
assert data['data']['activities'][0]['ascent'] is None assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] == 8.0 assert data['data']['activities'][0]['ave_speed'] == 8.0
assert data['data']['activities'][0]['descent'] is None assert data['data']['activities'][0]['descent'] is None
@ -675,86 +557,25 @@ def test_edit_an_activity_wo_gpx_timezone(
assert records[3]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT' assert records[3]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
assert records[3]['value'] == 8.0 assert records[3]['value'] == 8.0
def test_it_updates_only_sport_and_distance_an_activity_wo_gpx(
def test_edit_an_activity_wo_gpx_partial(app, user_1, sport_1_cycling): self,
app,
user_1,
sport_1_cycling,
sport_2_running,
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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.post(
'/api/activities/no_gpx',
content_type='application/json',
data=json.dumps(
dict(
sport_id=1,
duration=3600,
activity_date='2018-05-14 14:05',
distance=7,
)
),
headers=dict(
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 'creation_date' in data['data']['activities'][0]
assert (
data['data']['activities'][0]['activity_date']
== 'Mon, 14 May 2018 14:05:00 GMT'
) # noqa
assert data['data']['activities'][0]['user'] == 'test'
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-14 14:05:00'
) # noqa
assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] == 7.0
assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] == 7.0
assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] == 7.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
records = data['data']['activities'][0]['records']
assert len(records) == 4
assert records[0]['sport_id'] == 1
assert records[0]['activity_id'] == 1
assert records[0]['record_type'] == 'MS'
assert records[0]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[0]['value'] == 7.0
assert records[1]['sport_id'] == 1
assert records[1]['activity_id'] == 1
assert records[1]['record_type'] == 'LD'
assert records[1]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[1]['value'] == '1:00:00'
assert records[2]['sport_id'] == 1
assert records[2]['activity_id'] == 1
assert records[2]['record_type'] == 'FD'
assert records[2]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[2]['value'] == 7.0
assert records[3]['sport_id'] == 1
assert records[3]['activity_id'] == 1
assert records[3]['record_type'] == 'AS'
assert records[3]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT'
assert records[3]['value'] == 7.0
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
data=json.dumps(dict(sport_id=1, distance=10)), data=json.dumps(dict(sport_id=2, distance=20)),
headers=dict( headers=dict(
Authorization='Bearer ' Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
@ -762,28 +583,24 @@ def test_edit_an_activity_wo_gpx_partial(app, user_1, sport_1_cycling):
) )
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 'creation_date' in data['data']['activities'][0]
assert ( assert (
data['data']['activities'][0]['activity_date'] data['data']['activities'][0]['activity_date']
== 'Mon, 14 May 2018 14:05:00 GMT' == 'Mon, 01 Jan 2018 00:00:00 GMT'
) # noqa )
assert data['data']['activities'][0]['user'] == 'test' assert data['data']['activities'][0]['user'] == 'test'
assert data['data']['activities'][0]['sport_id'] == 1 assert data['data']['activities'][0]['sport_id'] == 2
assert data['data']['activities'][0]['duration'] == '1:00:00' assert data['data']['activities'][0]['duration'] == '1:00:00'
assert ( assert data['data']['activities'][0]['title'] is None
data['data']['activities'][0]['title']
== 'Cycling - 2018-05-14 14:05:00'
) # noqa
assert data['data']['activities'][0]['ascent'] is None assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] == 10.0 assert data['data']['activities'][0]['ave_speed'] == 20.0
assert data['data']['activities'][0]['descent'] is None assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] == 10.0 assert data['data']['activities'][0]['distance'] == 20.0
assert data['data']['activities'][0]['max_alt'] is None assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] == 10.0 assert data['data']['activities'][0]['max_speed'] == 20.0
assert data['data']['activities'][0]['min_alt'] is None assert data['data']['activities'][0]['min_alt'] is None
assert data['data']['activities'][0]['moving'] == '1:00:00' assert data['data']['activities'][0]['moving'] == '1:00:00'
assert data['data']['activities'][0]['pauses'] is None assert data['data']['activities'][0]['pauses'] is None
@ -791,37 +608,37 @@ def test_edit_an_activity_wo_gpx_partial(app, user_1, sport_1_cycling):
records = data['data']['activities'][0]['records'] records = data['data']['activities'][0]['records']
assert len(records) == 4 assert len(records) == 4
assert records[0]['sport_id'] == 1 assert records[0]['sport_id'] == 2
assert records[0]['activity_id'] == 1 assert records[0]['activity_id'] == 1
assert records[0]['record_type'] == 'MS' assert records[0]['record_type'] == 'MS'
assert records[0]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT' assert records[0]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[0]['value'] == 10.0 assert records[0]['value'] == 20.0
assert records[1]['sport_id'] == 1 assert records[1]['sport_id'] == 2
assert records[1]['activity_id'] == 1 assert records[1]['activity_id'] == 1
assert records[1]['record_type'] == 'LD' assert records[1]['record_type'] == 'LD'
assert records[1]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT' assert records[1]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[1]['value'] == '1:00:00' assert records[1]['value'] == '1:00:00'
assert records[2]['sport_id'] == 1 assert records[2]['sport_id'] == 2
assert records[2]['activity_id'] == 1 assert records[2]['activity_id'] == 1
assert records[2]['record_type'] == 'FD' assert records[2]['record_type'] == 'FD'
assert records[2]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT' assert records[2]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[2]['value'] == 10.0 assert records[2]['value'] == 20.0
assert records[3]['sport_id'] == 1 assert records[3]['sport_id'] == 2
assert records[3]['activity_id'] == 1 assert records[3]['activity_id'] == 1
assert records[3]['record_type'] == 'AS' assert records[3]['record_type'] == 'AS'
assert records[3]['activity_date'] == 'Mon, 14 May 2018 14:05:00 GMT' assert records[3]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[3]['value'] == 10.0 assert records[3]['value'] == 20.0
def test_it_returns_400_if_payload_is_empty(
def test_edit_an_activity_wo_gpx_invalid_payload( self, app, user_1, sport_1_cycling, activity_cycling_user_1
app, user_1, 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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/activities/1', '/api/activities/1',
content_type='application/json', content_type='application/json',
@ -833,15 +650,13 @@ def test_edit_an_activity_wo_gpx_invalid_payload(
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_it_returns_500_if_date_format_is_invalid(
def test_edit_an_activity_wo_gpx_incorrect_data( self, app, user_1, sport_1_cycling, activity_cycling_user_1
app, user_1, 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',
@ -853,7 +668,10 @@ def test_edit_an_activity_wo_gpx_incorrect_data(
content_type='application/json', content_type='application/json',
data=json.dumps( data=json.dumps(
dict( dict(
sport_id=1, duration=3600, activity_date='15/2018', distance=10 sport_id=1,
duration=3600,
activity_date='15/2018',
distance=10,
) )
), ),
headers=dict( headers=dict(
@ -871,8 +689,9 @@ def test_edit_an_activity_wo_gpx_incorrect_data(
in data['message'] in data['message']
) )
def test_it_returns_404_if_edited_activity_doens_not_exists(
def test_edit_an_activity_no_activity(app, user_1, sport_1_cycling): self, app, user_1, 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',
@ -897,15 +716,15 @@ def test_edit_an_activity_no_activity(app, user_1, sport_1_cycling):
) )
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']
assert len(data['data']['activities']) == 0 assert len(data['data']['activities']) == 0
def test_refresh_an_activity_with_gpx( class TestRefreshActivityWithGpx:
app, user_1, sport_1_cycling, sport_2_running, gpx_file def test_refresh_an_activity_with_gpx(
): self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',

View File

@ -11,7 +11,10 @@ def get_gpx_filepath(activity_id):
return activity.gpx return activity.gpx
def test_delete_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file): class TestDeleteActivityWithGpx:
def test_it_deletes_an_activity_with_gpx(
self, 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',
@ -40,10 +43,9 @@ def test_delete_an_activity_with_gpx(app, user_1, sport_1_cycling, gpx_file):
assert response.status_code == 204 assert response.status_code == 204
def test_it_returns_403_when_deleting_an_activity_from_different_user(
def test_delete_an_activity_with_gpx_different_user( self, app, user_1, user_2, sport_1_cycling, gpx_file
app, user_1, user_2, 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',
@ -81,51 +83,7 @@ def test_delete_an_activity_with_gpx_different_user(
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_it_returns_404_if_activity_does_not_exist(self, app, user_1):
def test_delete_an_activity_wo_gpx(
app, user_1, sport_1_cycling, activity_cycling_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.delete(
'/api/activities/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert response.status_code == 204
def test_delete_an_activity_wo_gpx_different_user(
app, user_1, user_2, sport_1_cycling, activity_cycling_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
content_type='application/json',
)
response = client.delete(
'/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 == 403
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']
def test_delete_an_activity_no_activity(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',
@ -143,10 +101,9 @@ def test_delete_an_activity_no_activity(app, user_1):
assert response.status_code == 404 assert response.status_code == 404
assert 'not found' in data['status'] assert 'not found' in data['status']
def test_it_returns_500_when_deleting_an_activity_with_gpx_invalid_file(
def test_delete_an_activity_with_gpx_invalid_file( self, app, user_1, sport_1_cycling, gpx_file
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',
@ -186,3 +143,46 @@ def test_delete_an_activity_with_gpx_invalid_file(
'Error. Please try again or contact the administrator.' 'Error. Please try again or contact the administrator.'
in data['message'] in data['message']
) )
class TestDeleteActivityWithoutGpx:
def test_it_deletes_an_activity_wo_gpx(
self, 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.delete(
'/api/activities/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert response.status_code == 204
def test_it_returns_403_when_deleting_an_activity_from_different_user(
self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
content_type='application/json',
)
response = client.delete(
'/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 == 403
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']

View File

@ -1,16 +1,21 @@
def test_add_activity(app, sport_1_cycling, user_1, activity_cycling_user_1): class TestActivityModel:
def test_activity_model(
self, app, sport_1_cycling, user_1, activity_cycling_user_1
):
activity_cycling_user_1.title = 'Test' activity_cycling_user_1.title = 'Test'
assert 1 == activity_cycling_user_1.id assert 1 == activity_cycling_user_1.id
assert 1 == activity_cycling_user_1.user_id assert 1 == activity_cycling_user_1.user_id
assert 1 == activity_cycling_user_1.sport_id assert 1 == activity_cycling_user_1.sport_id
assert '2018-01-01 00:00:00' == str(activity_cycling_user_1.activity_date) assert '2018-01-01 00:00:00' == str(
activity_cycling_user_1.activity_date
)
assert 10.0 == float(activity_cycling_user_1.distance) assert 10.0 == float(activity_cycling_user_1.distance)
assert '0:17:04' == str(activity_cycling_user_1.duration) assert '1:00:00' == str(activity_cycling_user_1.duration)
assert 'Test' == activity_cycling_user_1.title assert 'Test' == activity_cycling_user_1.title
assert '<Activity \'Cycling\' - 2018-01-01 00:00:00>' == str( assert '<Activity \'Cycling\' - 2018-01-01 00:00:00>' == str(
activity_cycling_user_1 activity_cycling_user_1
) # noqa )
serialized_activity = activity_cycling_user_1.serialize() serialized_activity = activity_cycling_user_1.serialize()
assert 1 == serialized_activity['id'] assert 1 == serialized_activity['id']
@ -19,10 +24,12 @@ def test_add_activity(app, sport_1_cycling, user_1, activity_cycling_user_1):
assert serialized_activity['title'] == 'Test' assert serialized_activity['title'] == 'Test'
assert 'creation_date' in serialized_activity assert 'creation_date' in serialized_activity
assert serialized_activity['modification_date'] is not None assert serialized_activity['modification_date'] is not None
assert str(serialized_activity['activity_date']) == '2018-01-01 00:00:00' assert (
assert serialized_activity['duration'] == '0:17:04' str(serialized_activity['activity_date']) == '2018-01-01 00:00:00'
)
assert serialized_activity['duration'] == '1:00:00'
assert serialized_activity['pauses'] is None assert serialized_activity['pauses'] is None
assert serialized_activity['moving'] == '0:17:04' assert serialized_activity['moving'] == '1:00:00'
assert serialized_activity['distance'] == 10.0 assert serialized_activity['distance'] == 10.0
assert serialized_activity['max_alt'] is None assert serialized_activity['max_alt'] is None
assert serialized_activity['descent'] is None assert serialized_activity['descent'] is None
@ -40,14 +47,14 @@ def test_add_activity(app, sport_1_cycling, user_1, activity_cycling_user_1):
assert serialized_activity['weather_end'] is None assert serialized_activity['weather_end'] is None
assert serialized_activity['notes'] is None assert serialized_activity['notes'] is None
def test_activity_segment_model(
def test_add_segment( self,
app, app,
sport_1_cycling, sport_1_cycling,
user_1, user_1,
activity_cycling_user_1, activity_cycling_user_1,
activity_cycling_user_1_segment, activity_cycling_user_1_segment,
): ):
assert '<Segment \'0\' for activity \'1\'>' == str( assert '<Segment \'0\' for activity \'1\'>' == str(
activity_cycling_user_1_segment activity_cycling_user_1_segment
) # noqa )

View File

@ -1,13 +1,15 @@
import json import json
def test_get_config(app, user_1): class TestGetConfig:
def test_it_gets_application_config(self, 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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/config', '/api/config',
headers=dict( headers=dict(
@ -15,25 +17,28 @@ def test_get_config(app, user_1):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['gpx_limit_import'] == 10 assert data['data']['gpx_limit_import'] == 10
assert data['data']['is_registration_enabled'] is True assert data['data']['is_registration_enabled'] is True
assert data['data']['max_single_file_size'] == 1048576 assert data['data']['max_single_file_size'] == 1048576
assert data['data']['max_zip_file_size'] == 10485760 assert data['data']['max_zip_file_size'] == 10485760
assert data['data']['max_users'] == 100 assert data['data']['max_users'] == 100
def test_it_returns_error_if_application_has_no_config(
def test_get_config_no_config(app_no_config, user_1_admin): self, app_no_config, user_1_admin
):
client = app_no_config.test_client() client = app_no_config.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/config', '/api/config',
content_type='application/json', content_type='application/json',
@ -42,20 +47,24 @@ def test_get_config_no_config(app_no_config, user_1_admin):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Error on getting configuration.' in data['message'] assert 'Error on getting configuration.' in data['message']
def test_it_returns_error_if_application_has_several_config(
def test_get_config_several_config(app, app_config, user_1_admin): self, app, app_config, 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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/config', '/api/config',
content_type='application/json', content_type='application/json',
@ -64,18 +73,21 @@ def test_get_config_several_config(app, app_config, user_1_admin):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Error on getting configuration.' in data['message'] assert 'Error on getting configuration.' in data['message']
def test_update_config_as_admin(app, user_1_admin): class TestUpdateConfig:
def test_it_updates_config_when_user_is_admin(self, 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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
@ -97,14 +109,16 @@ def test_update_config_as_admin(app, user_1_admin):
assert data['data']['max_zip_file_size'] == 10485760 assert data['data']['max_zip_file_size'] == 10485760
assert data['data']['max_users'] == 10 assert data['data']['max_users'] == 10
def test_it_updates_all_config(self, app, user_1_admin):
def test_update_full_config_as_admin(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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/config', '/api/config',
content_type='application/json', content_type='application/json',
@ -121,8 +135,8 @@ def test_update_full_config_as_admin(app, user_1_admin):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['gpx_limit_import'] == 20 assert data['data']['gpx_limit_import'] == 20
@ -131,14 +145,14 @@ def test_update_full_config_as_admin(app, user_1_admin):
assert data['data']['max_zip_file_size'] == 25000 assert data['data']['max_zip_file_size'] == 25000
assert data['data']['max_users'] == 50 assert data['data']['max_users'] == 50
def test_it_returns_403_when_user_is_not_an_admin(self, app, user_1):
def test_update_config_not_admin(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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/config', '/api/config',
content_type='application/json', content_type='application/json',
@ -148,21 +162,23 @@ def test_update_config_not_admin(app, user_1):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 403 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_it_returns_400_if_invalid_is_payload(self, app, user_1_admin):
def test_update_config_invalid_payload(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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/config', '/api/config',
content_type='application/json', content_type='application/json',
@ -172,20 +188,24 @@ def test_update_config_invalid_payload(app, user_1_admin):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_it_returns_error_on_update_if_application_has_no_config(
def test_update_config_no_config(app_no_config, user_1_admin): self, app_no_config, user_1_admin
):
client = app_no_config.test_client() client = app_no_config.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/config', '/api/config',
content_type='application/json', content_type='application/json',
@ -195,18 +215,8 @@ def test_update_config_no_config(app_no_config, user_1_admin):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Error on updating configuration.' in data['message'] assert 'Error on updating configuration.' in data['message']
def test_ping(app):
""" => Ensure the /ping route behaves correctly."""
client = app.test_client()
response = client.get('/api/ping')
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'pong' in data['message']
assert 'success' in data['status']

View File

@ -1,7 +1,8 @@
from fittrackee_api.application.models import AppConfig from fittrackee_api.application.models import AppConfig
def test_application_config(app): class TestConfigModel:
def test_application_config(self, app):
app_config = AppConfig.query.first() app_config = AppConfig.query.first()
assert 1 == app_config.id assert 1 == app_config.id

View File

@ -3,8 +3,10 @@ import time
from io import BytesIO from io import BytesIO
def test_user_registration(app): class TestUserRegistration:
def test_user_can_register(self, app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -17,6 +19,7 @@ def test_user_registration(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'success' assert data['status'] == 'success'
assert data['message'] == 'Successfully registered.' assert data['message'] == 'Successfully registered.'
@ -24,8 +27,7 @@ def test_user_registration(app):
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 201 assert response.status_code == 201
def test_it_returns_error_if_user_already_exists(self, app, user_1):
def test_user_registration_user_already_exists(app, user_1):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
@ -45,9 +47,9 @@ def test_user_registration_user_already_exists(app, user_1):
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_username_is_too_short(self, app):
def test_user_registration_invalid_short_username(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -60,14 +62,14 @@ def test_user_registration_invalid_short_username(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'
assert data['message'] == "Username: 3 to 12 characters required.\n" assert data['message'] == "Username: 3 to 12 characters required.\n"
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_username_is_too_long(self, app):
def test_user_registration_invalid_long_username(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
@ -87,9 +89,9 @@ def test_user_registration_invalid_long_username(app):
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_email_is_invalid(self, app):
def test_user_registration_invalid_email(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -102,15 +104,16 @@ def test_user_registration_invalid_email(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'
assert data['message'] == "Valid email must be provided.\n" assert data['message'] == "Valid email must be provided.\n"
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_password_is_too_short(self, app):
def test_user_registration_invalid_short_password(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -123,15 +126,16 @@ def test_user_registration_invalid_short_password(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'
assert data['message'] == "Password: 8 characters required.\n" assert data['message'] == "Password: 8 characters required.\n"
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_passwords_mismatch(self, app):
def test_user_registration_mismatched_password(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -144,16 +148,17 @@ def test_user_registration_mismatched_password(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'
assert ( assert (
data['message'] == "Password and password confirmation don\'t match.\n" data['message']
== "Password and password confirmation don\'t match.\n"
) )
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_paylaod_is_invalid(self, app):
def test_user_registration_invalid_json(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
@ -165,9 +170,9 @@ def test_user_registration_invalid_json(app):
assert 'Invalid payload.', data['message'] assert 'Invalid payload.', data['message']
assert 'error', data['status'] assert 'error', data['status']
def test_it_returns_error_if_username_is_missing(self, app):
def test_user_registration_invalid_json_keys_no_username(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -179,31 +184,35 @@ def test_user_registration_invalid_json_keys_no_username(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
assert 'error' in data['status'] assert 'error' in data['status']
def test_it_returns_error_if_email_is_missing(self, app):
def test_user_registration_invalid_json_keys_no_email(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
dict( dict(
username='test', password='12345678', password_conf='12345678' username='test',
password='12345678',
password_conf='12345678',
) )
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
assert 'error' in data['status'] assert 'error' in data['status']
def test_it_returns_error_if_password_is_missing(self, app):
def test_user_registration_invalid_json_keys_no_password(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -215,18 +224,20 @@ def test_user_registration_invalid_json_keys_no_password(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'Invalid payload.', data['message'] assert 'Invalid payload.', data['message']
assert 'error', data['status'] assert 'error', data['status']
def test_it_returns_error_if_password_confirmation_is_missing(self, app):
def test_user_registration_invalid_json_keys_no_password_conf(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
dict(username='test', email='test@test.com', password='12345678') dict(
username='test', email='test@test.com', password='12345678'
)
), ),
content_type='application/json', content_type='application/json',
) )
@ -235,9 +246,9 @@ def test_user_registration_invalid_json_keys_no_password_conf(app):
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
assert 'error' in data['status'] assert 'error' in data['status']
def test_it_returns_error_if_username_is_invalid(self, app):
def test_user_registration_invalid_data(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/register', '/api/auth/register',
data=json.dumps( data=json.dumps(
@ -250,6 +261,7 @@ def test_user_registration_invalid_data(app):
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert ( assert (
@ -259,47 +271,8 @@ def test_user_registration_invalid_data(app):
assert 'error' in data['status'] assert 'error' in data['status']
def test_user_registration_max_users_exceeded( class TestUserLogin:
app, user_1_admin, user_2, user_3 def test_user_can_register(self, app, user_1):
):
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',
)
client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(max_users=3, registration=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
response = client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='user4',
email='user4@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
assert response.content_type == 'application/json'
assert response.status_code == 403
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'Error. Registration is disabled.'
def test_login_registered_user(app, user_1):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/login', '/api/auth/login',
@ -313,8 +286,7 @@ def test_login_registered_user(app, user_1):
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 200 assert response.status_code == 200
def test_it_returns_error_if_user_does_not_exists(self, 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',
@ -327,8 +299,7 @@ def test_login_no_registered_user(app):
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 404 assert response.status_code == 404
def test_it_returns_error_on_invalid_payload(self, app):
def test_login_invalid_payload(app):
client = app.test_client() client = app.test_client()
response = client.post( response = client.post(
'/api/auth/login', '/api/auth/login',
@ -341,14 +312,15 @@ def test_login_invalid_payload(app):
assert response.content_type == 'application/json' assert response.content_type == 'application/json'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_password_is_invalid(self, app, user_1):
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',
data=json.dumps(dict(email='test@test.com', password='123456789')), data=json.dumps(dict(email='test@test.com', password='123456789')),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'
assert data['message'] == 'Invalid credentials.' assert data['message'] == 'Invalid credentials.'
@ -356,15 +328,15 @@ def test_login_registered_user_invalid_password(app, user_1):
assert response.status_code == 404 assert response.status_code == 404
def test_logout(app, user_1): class TestUserLogout:
def test_user_can_logout(self, app, user_1):
client = app.test_client() client = app.test_client()
# user login
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
# valid token logout
response = client.get( response = client.get(
'/api/auth/logout', '/api/auth/logout',
headers=dict( headers=dict(
@ -372,13 +344,13 @@ def test_logout(app, user_1):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'success' assert data['status'] == 'success'
assert data['message'] == 'Successfully logged out.' assert data['message'] == 'Successfully logged out.'
assert response.status_code == 200 assert response.status_code == 200
def test_it_returns_error_with_expired_token(self, app, user_1):
def test_logout_expired_token(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',
@ -399,8 +371,7 @@ def test_logout_expired_token(app, user_1):
assert data['message'] == 'Signature expired. Please log in again.' assert data['message'] == 'Signature expired. Please log in again.'
assert response.status_code == 401 assert response.status_code == 401
def test_it_returns_error_with_invalid_token(self, app):
def test_logout_invalid(app):
client = app.test_client() client = app.test_client()
response = client.get( response = client.get(
'/api/auth/logout', headers=dict(Authorization='Bearer invalid') '/api/auth/logout', headers=dict(Authorization='Bearer invalid')
@ -410,8 +381,7 @@ def test_logout_invalid(app):
assert data['message'] == 'Invalid token. Please log in again.' assert data['message'] == 'Invalid token. Please log in again.'
assert response.status_code == 401 assert response.status_code == 401
def test_it_returns_error_with_invalid_headers(self, app):
def test_logout_invalid_headers(app):
client = app.test_client() client = app.test_client()
response = client.get('/api/auth/logout', headers=dict()) response = client.get('/api/auth/logout', headers=dict())
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
@ -420,7 +390,8 @@ def test_logout_invalid_headers(app):
assert response.status_code == 401 assert response.status_code == 401
def test_user_profile_minimal(app, user_1): class TestUserProfile:
def test_it_returns_user_minimal_profile(self, 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',
@ -451,8 +422,7 @@ def test_user_profile_minimal(app, user_1):
assert data['data']['total_duration'] == '0:00:00' assert data['data']['total_duration'] == '0:00:00'
assert response.status_code == 200 assert response.status_code == 200
def test_it_returns_user_full_profile(self, app, user_1_full):
def test_user_profile_full(app, user_1_full):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -488,15 +458,15 @@ def test_user_profile_full(app, user_1_full):
assert data['data']['total_duration'] == '0:00:00' assert data['data']['total_duration'] == '0:00:00'
assert response.status_code == 200 assert response.status_code == 200
def test_it_returns_user_profile_with_activities(
def test_user_profile_with_activities( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
activity_cycling_user_1, activity_cycling_user_1,
activity_running_user_1, activity_running_user_1,
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -522,11 +492,10 @@ def test_user_profile_with_activities(
assert data['data']['nb_sports'] == 2 assert data['data']['nb_sports'] == 2
assert data['data']['sports_list'] == [1, 2] assert data['data']['sports_list'] == [1, 2]
assert data['data']['total_distance'] == 22 assert data['data']['total_distance'] == 22
assert data['data']['total_duration'] == '1:57:04' assert data['data']['total_duration'] == '2:40:00'
assert response.status_code == 200 assert response.status_code == 200
def test_it_returns_error_if_headers_are_invalid(self, app):
def test_invalid_profile(app):
client = app.test_client() client = app.test_client()
response = client.get( response = client.get(
'/api/auth/profile', headers=dict(Authorization='Bearer invalid') '/api/auth/profile', headers=dict(Authorization='Bearer invalid')
@ -537,7 +506,8 @@ def test_invalid_profile(app):
assert response.status_code == 401 assert response.status_code == 401
def test_user_profile_valid_update(app, user_1): class TestUserProfileUpdate:
def test_it_updates_user_profile(self, 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',
@ -588,8 +558,7 @@ def test_user_profile_valid_update(app, user_1):
assert data['data']['total_distance'] == 0 assert data['data']['total_distance'] == 0
assert data['data']['total_duration'] == '0:00:00' assert data['data']['total_duration'] == '0:00:00'
def test_it_updates_user_profile_without_password(self, app, user_1):
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',
@ -638,8 +607,7 @@ def test_user_profile_valid_update_without_password(app, user_1):
assert data['data']['total_distance'] == 0 assert data['data']['total_distance'] == 0
assert data['data']['total_duration'] == '0:00:00' assert data['data']['total_duration'] == '0:00:00'
def test_it_returns_error_if_fields_are_missing(self, app, user_1):
def test_user_profile_invalid_update_with_missing_fields(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',
@ -660,8 +628,7 @@ def test_user_profile_invalid_update_with_missing_fields(app, user_1):
assert data['message'] == 'Invalid payload.' assert data['message'] == 'Invalid payload.'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_payload_is_empty(self, app, user_1):
def test_user_profile_update_invalid_json(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',
@ -682,8 +649,7 @@ def test_user_profile_update_invalid_json(app, user_1):
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
assert 'error' in data['status'] assert 'error' in data['status']
def test_it_returns_error_if_passwords_mismatch(self, app, user_1):
def test_user_profile_invalid_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',
@ -715,12 +681,14 @@ def test_user_profile_invalid_password(app, user_1):
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'
assert ( assert (
data['message'] == 'Password and password confirmation don\'t match.\n' data['message']
== 'Password and password confirmation don\'t match.\n'
) )
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_password_confirmation_is_missing(
def test_user_profile_missing_password_conf(app, user_1): self, 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',
@ -751,12 +719,14 @@ def test_user_profile_missing_password_conf(app, user_1):
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'
assert ( assert (
data['message'] == 'Password and password confirmation don\'t match.\n' data['message']
== 'Password and password confirmation don\'t match.\n'
) )
assert response.status_code == 400 assert response.status_code == 400
def test_update_user_picture(app, user_1): class TestUserPicture:
def test_it_updates_user_picture(self, 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',
@ -794,8 +764,7 @@ def test_update_user_picture(app, user_1):
assert 'avatar.png' not in user_1.picture assert 'avatar.png' not in user_1.picture
assert 'avatar2.png' in user_1.picture assert 'avatar2.png' in user_1.picture
def test_it_returns_error_if_file_is_missing(self, app, user_1):
def test_update_user_no_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',
@ -815,8 +784,7 @@ def test_update_user_no_picture(app, user_1):
assert data['message'] == 'No file part.' assert data['message'] == 'No file part.'
assert response.status_code == 400 assert response.status_code == 400
def test_it_returns_error_if_file_is_invalid(self, app, user_1):
def test_update_user_invalid_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',
@ -838,9 +806,51 @@ def test_update_user_invalid_picture(app, user_1):
assert response.status_code == 400 assert response.status_code == 400
def test_it_disables_registration_on_user_registration( class TestRegistrationConfiguration:
app_no_config, app_config, user_1_admin, user_2 def test_it_returns_error_if_it_exceeds_max_users(
): self, app, user_1_admin, user_2, user_3
):
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',
)
client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(max_users=3, registration=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
response = client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='user4',
email='user4@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
assert response.content_type == 'application/json'
assert response.status_code == 403
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'Error. Registration is disabled.'
def test_it_disables_registration_on_user_registration(
self, app_no_config, app_config, user_1_admin, user_2
):
app_config.max_users = 3 app_config.max_users = 3
client = app_no_config.test_client() client = app_no_config.test_client()
client.post( client.post(
@ -872,10 +882,9 @@ def test_it_disables_registration_on_user_registration(
assert data['status'] == 'error' assert data['status'] == 'error'
assert data['message'] == 'Error. Registration is disabled.' assert data['message'] == 'Error. Registration is disabled.'
def test_it_does_not_disable_registration_on_user_registration(
def test_it_does_not_disable_registration_on_user_registration( self, app_no_config, app_config, user_1_admin, user_2,
app_no_config, app_config, user_1_admin, user_2, ):
):
app_config.max_users = 4 app_config.max_users = 4
client = app_no_config.test_client() client = app_no_config.test_client()
client.post( client.post(

View File

@ -1,7 +1,8 @@
import os import os
def test_development_config(app): class TestConfig:
def test_development_config(self, app):
app.config.from_object('fittrackee_api.config.DevelopmentConfig') app.config.from_object('fittrackee_api.config.DevelopmentConfig')
assert app.config['DEBUG'] assert app.config['DEBUG']
assert not app.config['TESTING'] assert not app.config['TESTING']
@ -9,8 +10,7 @@ def test_development_config(app):
'DATABASE_URL' 'DATABASE_URL'
) )
def test_testing_config(self, app):
def test_testing_config(app):
app.config.from_object('fittrackee_api.config.TestingConfig') app.config.from_object('fittrackee_api.config.TestingConfig')
assert app.config['DEBUG'] assert app.config['DEBUG']
assert app.config['TESTING'] assert app.config['TESTING']

View File

@ -0,0 +1,12 @@
import json
class TestHealthCheck:
def test_it_returns_pong_on_health_check(self, app):
""" => Ensure the /health_check route behaves correctly."""
client = app.test_client()
response = client.get('/api/ping')
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'pong' in data['message']
assert 'success' in data['status']

View File

@ -1,7 +1,9 @@
import json import json
def test_get_records_for_authenticated_user( class TestGetRecords:
def test_it_gets_records_for_authenticated_user(
self,
app, app,
user_1, user_1,
user_2, user_2,
@ -9,7 +11,7 @@ def test_get_records_for_authenticated_user(
sport_2_running, sport_2_running,
activity_cycling_user_1, activity_cycling_user_1,
activity_cycling_user_2, activity_cycling_user_2,
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -69,15 +71,15 @@ def test_get_records_for_authenticated_user(
assert 'MS' == data['data']['records'][3]['record_type'] assert 'MS' == data['data']['records'][3]['record_type']
assert 'value' in data['data']['records'][3] assert 'value' in data['data']['records'][3]
def test_it_gets_no_records_if_user_has_no_activity(
def test_get_records_no_activities_user_1( self,
app, app,
user_1, user_1,
user_2, user_2,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
activity_cycling_user_2, activity_cycling_user_2,
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -97,10 +99,9 @@ def test_get_records_no_activities_user_1(
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['records']) == 0 assert len(data['data']['records']) == 0
def test_it_gets_no_records_if_activity_has_zero_value(
def test_add_activity_zero_value( self, app, user_1, sport_1_cycling, sport_2_running
app, user_1, sport_1_cycling, sport_2_running ):
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -137,10 +138,9 @@ def test_add_activity_zero_value(
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['records']) == 0 assert len(data['data']['records']) == 0
def test_it_gets_updated_records_after_activities_post_and_patch(
def test_get_records_after_activities_post_and_patch( self, app, user_1, sport_1_cycling
app, user_1, 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',
@ -485,8 +485,8 @@ def test_get_records_after_activities_post_and_patch(
assert 'MS' == data['data']['records'][3]['record_type'] assert 'MS' == data['data']['records'][3]['record_type']
assert 7.0 == data['data']['records'][3]['value'] assert 7.0 == data['data']['records'][3]['value']
# add an activity with the same data as activity 1 except with a later date # add an activity with the same data as activity 1 except with a
# => no change in record # later date => no change in record
client.post( client.post(
'/api/activities/no_gpx', '/api/activities/no_gpx',
content_type='application/json', content_type='application/json',
@ -673,10 +673,9 @@ def test_get_records_after_activities_post_and_patch(
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['records']) == 0 assert len(data['data']['records']) == 0
def test_it_gets_updated_records_after_sport_change(
def test_get_records_after_sport_change( self, app, user_1, sport_1_cycling, sport_2_running
app, user_1, sport_1_cycling, sport_2_running ):
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',

View File

@ -3,7 +3,10 @@ import datetime
from fittrackee_api.activities.models import Record from fittrackee_api.activities.models import Record
def test_record_model(app, user_1, sport_1_cycling, activity_cycling_user_1): class TestRecordModel:
def test_record_model(
self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_ld = Record.query.filter_by( record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id, user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id, sport_id=activity_cycling_user_1.sport_id,
@ -25,10 +28,9 @@ def test_record_model(app, user_1, sport_1_cycling, activity_cycling_user_1):
assert 'activity_date' in record_serialize assert 'activity_date' in record_serialize
assert 'value' in record_serialize assert 'value' in record_serialize
def test_record_model_with_none_value(
def test_record_model_none_value( self, app, user_1, sport_1_cycling, activity_cycling_user_1
app, user_1, sport_1_cycling, activity_cycling_user_1 ):
):
record_ld = Record.query.filter_by( record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id, user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id, sport_id=activity_cycling_user_1.sport_id,
@ -46,8 +48,9 @@ def test_record_model_none_value(
record_serialize = record_ld.serialize() record_serialize = record_ld.serialize()
assert record_serialize['value'] is None assert record_serialize['value'] is None
def test_average_speed_records(
def test_add_as_records(app, user_1, sport_1_cycling, activity_cycling_user_1): self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_as = Record.query.filter_by( record_as = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id, user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id, sport_id=activity_cycling_user_1.sport_id,
@ -62,8 +65,9 @@ def test_add_as_records(app, user_1, sport_1_cycling, activity_cycling_user_1):
assert record_serialize.get('value') == 10.0 assert record_serialize.get('value') == 10.0
assert isinstance(record_serialize.get('value'), float) assert isinstance(record_serialize.get('value'), float)
def test_add_farest_distance_records(
def test_add_fd_records(app, user_1, sport_1_cycling, activity_cycling_user_1): self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_fd = Record.query.filter_by( record_fd = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id, user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id, sport_id=activity_cycling_user_1.sport_id,
@ -78,8 +82,9 @@ def test_add_fd_records(app, user_1, sport_1_cycling, activity_cycling_user_1):
assert record_serialize.get('value') == 10.0 assert record_serialize.get('value') == 10.0
assert isinstance(record_serialize.get('value'), float) assert isinstance(record_serialize.get('value'), float)
def test_add_longest_duration_records(
def test_add_ld_records(app, user_1, sport_1_cycling, activity_cycling_user_1): self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_ld = Record.query.filter_by( record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id, user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id, sport_id=activity_cycling_user_1.sport_id,
@ -87,17 +92,16 @@ def test_add_ld_records(app, user_1, sport_1_cycling, activity_cycling_user_1):
).first() ).first()
assert isinstance(record_ld.value, datetime.timedelta) assert isinstance(record_ld.value, datetime.timedelta)
assert str(record_ld.value) == '0:17:04' assert str(record_ld.value) == '1:00:00'
assert record_ld._value == 1024 assert record_ld._value == 3600
record_serialize = record_ld.serialize() record_serialize = record_ld.serialize()
assert record_serialize.get('value') == '0:17:04' assert record_serialize.get('value') == '1:00:00'
assert isinstance(record_serialize.get('value'), str) assert isinstance(record_serialize.get('value'), str)
def test_add_longest_duration_records_with_zero(
def test_add_ld_records_zero( self, app, user_1, sport_1_cycling, activity_cycling_user_1
app, user_1, sport_1_cycling, activity_cycling_user_1 ):
):
record_ld = Record.query.filter_by( record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id, user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id, sport_id=activity_cycling_user_1.sport_id,
@ -113,10 +117,9 @@ def test_add_ld_records_zero(
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_max_speed_records_no_value(
def test_add_ms_records_no_value( self, app, user_1, sport_1_cycling, activity_cycling_user_1
app, user_1, sport_1_cycling, activity_cycling_user_1 ):
):
record_ms = Record.query.filter_by( record_ms = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id, user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id, sport_id=activity_cycling_user_1.sport_id,

View File

@ -30,13 +30,17 @@ expected_sport_1_cycling_inactive_admin_result = (
expected_sport_1_cycling_inactive_admin_result['has_activities'] = False expected_sport_1_cycling_inactive_admin_result['has_activities'] = False
def test_get_all_sports(app, user_1, sport_1_cycling, sport_2_running): class TestGetSports:
def test_it_gets_all_sports(
self, app, user_1, sport_1_cycling, sport_2_running
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/sports', '/api/sports',
headers=dict( headers=dict(
@ -44,25 +48,24 @@ def test_get_all_sports(app, user_1, sport_1_cycling, sport_2_running):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 2 assert len(data['data']['sports']) == 2
assert data['data']['sports'][0] == expected_sport_1_cycling_result assert data['data']['sports'][0] == expected_sport_1_cycling_result
assert data['data']['sports'][1] == expected_sport_2_running_result assert data['data']['sports'][1] == expected_sport_2_running_result
def test_it_gets_all_sports_with_inactive_one(
def test_get_all_sports_with_inactive_one( self, app, user_1, sport_1_cycling_inactive, sport_2_running
app, user_1, sport_1_cycling_inactive, sport_2_running ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/sports', '/api/sports',
headers=dict( headers=dict(
@ -70,27 +73,29 @@ def test_get_all_sports_with_inactive_one(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 2 assert len(data['data']['sports']) == 2
assert ( assert (
data['data']['sports'][0] == expected_sport_1_cycling_inactive_result data['data']['sports'][0]
== expected_sport_1_cycling_inactive_result
) )
assert data['data']['sports'][1] == expected_sport_2_running_result assert data['data']['sports'][1] == expected_sport_2_running_result
def test_it_gets_all_sports_with_admin_rights(
def test_get_all_sports_admin( self, app, user_1_admin, sport_1_cycling_inactive, sport_2_running
app, user_1_admin, sport_1_cycling_inactive, sport_2_running ):
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/sports', '/api/sports',
headers=dict( headers=dict(
@ -98,26 +103,29 @@ def test_get_all_sports_admin(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 2 assert len(data['data']['sports']) == 2
assert ( assert (
data['data']['sports'][0] data['data']['sports'][0]
== expected_sport_1_cycling_inactive_admin_result == expected_sport_1_cycling_inactive_admin_result
) )
assert data['data']['sports'][1] == expected_sport_2_running_admin_result assert (
data['data']['sports'][1] == expected_sport_2_running_admin_result
)
def test_get_a_sport(app, user_1, sport_1_cycling): class TestGetSport:
def test_it_gets_a_sport(self, app, user_1, 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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/sports/1', '/api/sports/1',
headers=dict( headers=dict(
@ -125,22 +133,21 @@ def test_get_a_sport(app, user_1, sport_1_cycling):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 1 assert len(data['data']['sports']) == 1
assert data['data']['sports'][0] == expected_sport_1_cycling_result assert data['data']['sports'][0] == expected_sport_1_cycling_result
def test_it_returns_404_if_sport_does_not_exist(self, app, user_1):
def test_get_a_sport_invalid(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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/sports/1', '/api/sports/1',
headers=dict( headers=dict(
@ -148,14 +155,15 @@ def test_get_a_sport_invalid(app, user_1):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']
assert len(data['data']['sports']) == 0 assert len(data['data']['sports']) == 0
def test_it_gets_a_inactive_sport(
def test_get_a_inactive_sport(app, user_1, sport_1_cycling_inactive): self, app, user_1, sport_1_cycling_inactive
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -176,17 +184,19 @@ def test_get_a_inactive_sport(app, user_1, sport_1_cycling_inactive):
assert len(data['data']['sports']) == 1 assert len(data['data']['sports']) == 1
assert ( assert (
data['data']['sports'][0] == expected_sport_1_cycling_inactive_result data['data']['sports'][0]
== expected_sport_1_cycling_inactive_result
) )
def test_it_get_an_inactive_sport_with_admin_rights(
def test_get_a_inactive_sport_as_admin( self, app, user_1_admin, sport_1_cycling_inactive
app, user_1_admin, sport_1_cycling_inactive ):
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
@ -208,13 +218,17 @@ def test_get_a_inactive_sport_as_admin(
) )
def test_update_a_sport(app, user_1_admin, sport_1_cycling): class TestUpdateSport:
def test_it_disables_a_sport(self, 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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/sports/1', '/api/sports/1',
content_type='application/json', content_type='application/json',
@ -224,15 +238,25 @@ def test_update_a_sport(app, user_1_admin, sport_1_cycling):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 1 assert len(data['data']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is False assert data['data']['sports'][0]['is_active'] is False
assert data['data']['sports'][0]['has_activities'] is False assert data['data']['sports'][0]['has_activities'] is False
def test_it_enables_a_sport(self, app, user_1_admin, sport_1_cycling):
sport_1_cycling.is_active = False
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( response = client.patch(
'/api/sports/1', '/api/sports/1',
content_type='application/json', content_type='application/json',
@ -242,25 +266,26 @@ def test_update_a_sport(app, user_1_admin, sport_1_cycling):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 1 assert len(data['data']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is True assert data['data']['sports'][0]['is_active'] is True
assert data['data']['sports'][0]['has_activities'] is False assert data['data']['sports'][0]['has_activities'] is False
def test_it_disables_a_sport_with_activities(
def test_update_a_sport_with_activities( self, app, user_1_admin, sport_1_cycling, activity_cycling_user_1
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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/sports/1', '/api/sports/1',
content_type='application/json', content_type='application/json',
@ -270,15 +295,27 @@ def test_update_a_sport_with_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 1 assert len(data['data']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is False assert data['data']['sports'][0]['is_active'] is False
assert data['data']['sports'][0]['has_activities'] is True assert data['data']['sports'][0]['has_activities'] is True
def test_it_enables_a_sport_with_activities(
self, app, user_1_admin, sport_1_cycling, activity_cycling_user_1
):
sport_1_cycling.is_active = False
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( response = client.patch(
'/api/sports/1', '/api/sports/1',
content_type='application/json', content_type='application/json',
@ -288,17 +325,17 @@ def test_update_a_sport_with_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['sports']) == 1 assert len(data['data']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is True assert data['data']['sports'][0]['is_active'] is True
assert data['data']['sports'][0]['has_activities'] is True assert data['data']['sports'][0]['has_activities'] is True
def test_returns_error_if_user_has_no_admin_rights(
def test_update_a_sport_not_admin(app, user_1, sport_1_cycling): self, app, user_1, 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',
@ -321,14 +358,16 @@ def test_update_a_sport_not_admin(app, user_1, sport_1_cycling):
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_returns_error_if_payload_is_invalid(self, app, user_1_admin):
def test_update_a_sport_invalid_payload(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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/sports/1', '/api/sports/1',
content_type='application/json', content_type='application/json',
@ -338,18 +377,19 @@ def test_update_a_sport_invalid_payload(app, user_1_admin):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_it_returns_error_if_sport_does_not_exist(self, app, user_1_admin):
def test_update_a_sport_invalid_id(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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(

View File

@ -1,4 +1,6 @@
def assert_sport_model(sport, is_admin=False): class TestSportModel:
@staticmethod
def assert_sport_model(sport, is_admin=False):
assert 1 == sport.id assert 1 == sport.id
assert 'Cycling' == sport.label assert 'Cycling' == sport.label
assert '<Sport \'Cycling\'>' == str(sport) assert '<Sport \'Cycling\'>' == str(sport)
@ -9,21 +11,18 @@ def assert_sport_model(sport, is_admin=False):
assert serialized_sport['is_active'] is True assert serialized_sport['is_active'] is True
return serialized_sport return serialized_sport
def test_sport_model(self, app, sport_1_cycling):
def test_sport_model(app, sport_1_cycling): serialized_sport = self.assert_sport_model(sport_1_cycling)
serialized_sport = assert_sport_model(sport_1_cycling)
assert 'has_activities' not in serialized_sport assert 'has_activities' not in serialized_sport
def test_sport_model_with_activity(
def test_sport_model_with_activity( self, app, sport_1_cycling, user_1, activity_cycling_user_1
app, sport_1_cycling, user_1, activity_cycling_user_1 ):
): serialized_sport = self.assert_sport_model(sport_1_cycling)
serialized_sport = assert_sport_model(sport_1_cycling)
assert 'has_activities' not in serialized_sport assert 'has_activities' not in serialized_sport
def test_sport_model_with_activity_as_admin(
def test_sport_model_with_activity_admin( self, app, sport_1_cycling, user_1, activity_cycling_user_1
app, sport_1_cycling, user_1, activity_cycling_user_1 ):
): serialized_sport = self.assert_sport_model(sport_1_cycling, True)
serialized_sport = assert_sport_model(sport_1_cycling, True)
assert serialized_sport['has_activities'] is True assert serialized_sport['has_activities'] is True

View File

@ -1,13 +1,15 @@
import json import json
def test_get_stats_by_time_no_activities(app, user_1): class TestGetStatsByTime:
def test_it_gets_no_stats_when_user_has_no_activities(self, 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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time', f'/api/stats/{user_1.username}/by_time',
headers=dict( headers=dict(
@ -15,20 +17,20 @@ def test_get_stats_by_time_no_activities(app, user_1):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == {} assert data['data']['statistics'] == {}
def test_it_returns_error_when_user_does_not_exists(self, app, user_1):
def test_get_stats_by_time_no_user(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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/1000/by_time', f'/api/stats/1000/by_time',
headers=dict( headers=dict(
@ -36,36 +38,37 @@ def test_get_stats_by_time_no_user(app, user_1):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']
assert 'User does not exist.' in data['message'] assert 'User does not exist.' in data['message']
def test_it_returns_error_if_date_format_is_invalid(
def test_get_stats_by_time_all_activities_error( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from="2018-04-01&to=2018-04-30', f'/api/stats/{user_1.username}/by_time?from="2018-04-01&to=2018-04-30', # noqa
headers=dict( headers=dict(
Authorization='Bearer ' Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert ( assert (
@ -73,21 +76,22 @@ def test_get_stats_by_time_all_activities_error(
in data['message'] in data['message']
) )
def test_it_returns_error_if_period_is_invalid(
def test_get_stats_by_time_all_activities_invalid_period( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=day', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=day', # noqa
headers=dict( headers=dict(
@ -95,27 +99,28 @@ def test_get_stats_by_time_all_activities_invalid_period(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'fail' in data['status'] assert 'fail' in data['status']
assert 'Invalid time period.' in data['message'] assert 'Invalid time period.' in data['message']
def test_it_gets_stats_by_time_all_activities(
def test_get_stats_by_time_all_activities( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time', f'/api/stats/{user_1.username}/by_time',
headers=dict( headers=dict(
@ -123,8 +128,8 @@ def test_get_stats_by_time_all_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -149,30 +154,31 @@ def test_get_stats_by_time_all_activities(
}, },
} }
def test_it_gets_stats_for_april_2018(
def test_get_stats_by_time_all_activities_april_2018( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30', f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30', # noqa
headers=dict( headers=dict(
Authorization='Bearer ' Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -190,21 +196,22 @@ def test_get_stats_by_time_all_activities_april_2018(
} }
} }
def test_it_gets_stats_for_april_2018_with_paris_timezone(
def test_get_stats_by_time_all_activities_april_2018_paris( self,
app, app,
user_1_paris, user_1_paris,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1_paris.username}/by_time?' f'/api/stats/{user_1_paris.username}/by_time?'
f'from=2018-04-01&to=2018-04-30', f'from=2018-04-01&to=2018-04-30',
@ -213,8 +220,8 @@ def test_get_stats_by_time_all_activities_april_2018_paris(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -232,21 +239,22 @@ def test_get_stats_by_time_all_activities_april_2018_paris(
} }
} }
def test_it_gets_stats_by_year(
def test_get_stats_by_year_all_activities( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?time=year', f'/api/stats/{user_1.username}/by_time?time=year',
headers=dict( headers=dict(
@ -254,8 +262,8 @@ def test_get_stats_by_year_all_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -280,21 +288,22 @@ def test_get_stats_by_year_all_activities(
}, },
} }
def test_it_gets_stats_by_year_for_april_2018(
def test_get_stats_by_year_all_activities_april_2018( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa
headers=dict( headers=dict(
@ -302,8 +311,8 @@ def test_get_stats_by_year_all_activities_april_2018(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -321,21 +330,22 @@ def test_get_stats_by_year_all_activities_april_2018(
} }
} }
def test_it_gets_stats_by_year_for_april_2018_with_paris_timezone(
def test_get_stats_by_year_all_activities_april_2018_paris( self,
app, app,
user_1_paris, user_1_paris,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1_paris.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa f'/api/stats/{user_1_paris.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa
headers=dict( headers=dict(
@ -343,8 +353,8 @@ def test_get_stats_by_year_all_activities_april_2018_paris(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -362,21 +372,22 @@ def test_get_stats_by_year_all_activities_april_2018_paris(
} }
} }
def test_it_gets_stats_by_month(
def test_get_stats_by_month_all_activities( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?time=month', f'/api/stats/{user_1.username}/by_time?time=month',
headers=dict( headers=dict(
@ -384,8 +395,8 @@ def test_get_stats_by_month_all_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -438,21 +449,22 @@ def test_get_stats_by_month_all_activities(
}, },
} }
def test_it_gets_stats_by_month_with_new_york_timezone(
def test_get_stats_by_month_all_activities_new_york( self,
app, app,
user_1_full, user_1_full,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1_full.username}/by_time?time=month', f'/api/stats/{user_1_full.username}/by_time?time=month',
headers=dict( headers=dict(
@ -460,8 +472,8 @@ def test_get_stats_by_month_all_activities_new_york(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -514,21 +526,22 @@ def test_get_stats_by_month_all_activities_new_york(
}, },
} }
def test_it_gets_stats_by_month_for_april_2018(
def test_get_stats_by_month_all_activities_april_2018( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=month', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=month', # noqa
headers=dict( headers=dict(
@ -536,8 +549,8 @@ def test_get_stats_by_month_all_activities_april_2018(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -555,21 +568,22 @@ def test_get_stats_by_month_all_activities_april_2018(
} }
} }
def test_it_gets_stats_by_week(
def test_get_stats_by_week_all_activities( self,
app, app,
user_1_full, user_1_full,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1_full.username}/by_time?time=week', f'/api/stats/{user_1_full.username}/by_time?time=week',
headers=dict( headers=dict(
@ -577,8 +591,8 @@ def test_get_stats_by_week_all_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -631,21 +645,22 @@ def test_get_stats_by_week_all_activities(
}, },
} }
def test_it_gets_stats_by_week_for_week_13(
def test_get_stats_by_week_all_activities_week_13( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=week', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=week', # noqa
headers=dict( headers=dict(
@ -653,8 +668,8 @@ def test_get_stats_by_week_all_activities_week_13(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -672,21 +687,22 @@ def test_get_stats_by_week_all_activities_week_13(
} }
} }
def test_if_get_stats_by_week_starting_with_monday(
def test_get_stats_by_weekm_all_activities( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?time=weekm', f'/api/stats/{user_1.username}/by_time?time=weekm',
headers=dict( headers=dict(
@ -694,8 +710,8 @@ def test_get_stats_by_weekm_all_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -748,21 +764,22 @@ def test_get_stats_by_weekm_all_activities(
}, },
} }
def test_it_gets_stats_by_week_starting_with_monday_for_week_13(
def test_get_stats_by_weekm_all_activities_week_13( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=weekm', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=weekm', # noqa
headers=dict( headers=dict(
@ -770,8 +787,8 @@ def test_get_stats_by_weekm_all_activities_week_13(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -790,20 +807,23 @@ def test_get_stats_by_weekm_all_activities_week_13(
} }
def test_get_stats_by_sport_all_activities( class TestGetStatsBySport:
def test_it_gets_stats_by_sport(
self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport', f'/api/stats/{user_1.username}/by_sport',
headers=dict( headers=dict(
@ -811,8 +831,8 @@ def test_get_stats_by_sport_all_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -828,21 +848,22 @@ def test_get_stats_by_sport_all_activities(
}, },
} }
def test_it_get_stats_for_sport_1(
def test_get_stats_by_sport_all_activities_sport_1( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id=1', f'/api/stats/{user_1.username}/by_sport?sport_id=1',
headers=dict( headers=dict(
@ -850,8 +871,8 @@ def test_get_stats_by_sport_all_activities_sport_1(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['statistics'] == { assert data['data']['statistics'] == {
@ -862,21 +883,22 @@ def test_get_stats_by_sport_all_activities_sport_1(
} }
} }
def test_it_returns_errors_if_user_does_not_exist(
def test_get_stats_by_sport_all_activities_invalid_user( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/1000/by_sport?sport_id=1', f'/api/stats/1000/by_sport?sport_id=1',
headers=dict( headers=dict(
@ -884,27 +906,28 @@ def test_get_stats_by_sport_all_activities_invalid_user(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']
assert 'User does not exist.' in data['message'] assert 'User does not exist.' in data['message']
def test_it_returns_error_if_sport_does_not_exist(
def test_get_stats_by_sport_all_activities_invalid_sport( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id=999', f'/api/stats/{user_1.username}/by_sport?sport_id=999',
headers=dict( headers=dict(
@ -912,27 +935,28 @@ def test_get_stats_by_sport_all_activities_invalid_sport(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']
assert 'Sport does not exist.' in data['message'] assert 'Sport does not exist.' in data['message']
def test_it_returns_error_if_sport_id_is_invalid(
def test_get_stats_by_sport_all_activities_error( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
seven_activities_user_1, seven_activities_user_1,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id="999', f'/api/stats/{user_1.username}/by_sport?sport_id="999',
headers=dict( headers=dict(
@ -940,8 +964,8 @@ def test_get_stats_by_sport_all_activities_error(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert ( assert (
@ -950,13 +974,19 @@ def test_get_stats_by_sport_all_activities_error(
) )
def test_get_app_stats_without_activities(app, user_1_admin): class TestGetAllStats:
def test_it_returns_all_stats_when_users_have_no_activities(
self, app, user_1_admin, user_2
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/stats/all', '/api/stats/all',
headers=dict( headers=dict(
@ -964,17 +994,17 @@ def test_get_app_stats_without_activities(app, user_1_admin):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['activities'] == 0 assert data['data']['activities'] == 0
assert data['data']['sports'] == 0 assert data['data']['sports'] == 0
assert data['data']['users'] == 1 assert data['data']['users'] == 2
assert 'uploads_dir_size' in data['data'] assert 'uploads_dir_size' in data['data']
def test_it_gets_app_all_stats_with_activities(
def test_get_app_stats_with_activities( self,
app, app,
user_1_admin, user_1_admin,
user_2, user_2,
@ -984,13 +1014,16 @@ def test_get_app_stats_with_activities(
activity_cycling_user_1, activity_cycling_user_1,
activity_cycling_user_2, activity_cycling_user_2,
activity_running_user_1, activity_running_user_1,
): ):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/stats/all', '/api/stats/all',
headers=dict( headers=dict(
@ -998,8 +1031,8 @@ def test_get_app_stats_with_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['data']['activities'] == 3 assert data['data']['activities'] == 3
@ -1007,8 +1040,8 @@ def test_get_app_stats_with_activities(
assert data['data']['users'] == 3 assert data['data']['users'] == 3
assert 'uploads_dir_size' in data['data'] assert 'uploads_dir_size' in data['data']
def test_it_returns_error_if_user_has_no_admin_rights(
def test_get_app_stats_no_admin( self,
app, app,
user_1, user_1,
user_2, user_2,
@ -1018,13 +1051,14 @@ def test_get_app_stats_no_admin(
activity_cycling_user_1, activity_cycling_user_1,
activity_cycling_user_2, activity_cycling_user_2,
activity_running_user_1, activity_running_user_1,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/stats/all', '/api/stats/all',
headers=dict( headers=dict(
@ -1032,8 +1066,8 @@ def test_get_app_stats_no_admin(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 403 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']

View File

@ -3,17 +3,16 @@ from datetime import datetime, timedelta
from io import BytesIO from io import BytesIO
from unittest.mock import patch from unittest.mock import patch
from fittrackee_api.users.models import User
class TestGetUser:
def test_single_user(app, user_1, user_2): def test_it_gets_single_user_without_activities(self, app, user_1, user_2):
"""=> Get single user details"""
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/users/{user_2.username}', f'/api/users/{user_2.username}',
content_type='application/json', content_type='application/json',
@ -22,12 +21,11 @@ def test_single_user(app, user_1, user_2):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['status'] == 'success' assert data['status'] == 'success'
assert len(data['data']['users']) == 1 assert len(data['data']['users']) == 1
user = data['data']['users'][0] user = data['data']['users'][0]
assert user['username'] == 'toto' assert user['username'] == 'toto'
assert user['email'] == 'toto@toto.com' assert user['email'] == 'toto@toto.com'
@ -47,22 +45,22 @@ def test_single_user(app, user_1, user_2):
assert user['total_distance'] == 0 assert user['total_distance'] == 0
assert user['total_duration'] == '0:00:00' assert user['total_duration'] == '0:00:00'
def test_it_gets_single_user_with_activities(
def test_single_user_with_activities( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
sport_2_running, sport_2_running,
activity_cycling_user_1, activity_cycling_user_1,
activity_running_user_1, activity_running_user_1,
): ):
"""=> Get single user details"""
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
f'/api/users/{user_1.username}', f'/api/users/{user_1.username}',
content_type='application/json', content_type='application/json',
@ -71,12 +69,11 @@ def test_single_user_with_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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 data['status'] == 'success' assert data['status'] == 'success'
assert len(data['data']['users']) == 1 assert len(data['data']['users']) == 1
user = data['data']['users'][0] user = data['data']['users'][0]
assert user['username'] == 'test' assert user['username'] == 'test'
assert user['email'] == 'test@test.com' assert user['email'] == 'test@test.com'
@ -94,11 +91,9 @@ def test_single_user_with_activities(
assert user['nb_sports'] == 2 assert user['nb_sports'] == 2
assert user['sports_list'] == [1, 2] assert user['sports_list'] == [1, 2]
assert user['total_distance'] == 22 assert user['total_distance'] == 22
assert user['total_duration'] == '1:57:04' assert user['total_duration'] == '2:40:00'
def test_it_returns_error_if_user_does_not_exist(self, app, user_1):
def test_single_user_no_existing(app, user_1):
"""=> Ensure error is thrown if the id does not exist."""
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -120,15 +115,15 @@ def test_single_user_no_existing(app, user_1):
assert 'User does not exist.' in data['message'] assert 'User does not exist.' in data['message']
def test_users_list(app, user_1, user_2, user_3): class TestGetUsers:
"""=> Ensure get single user behaves correctly.""" def test_it_get_users_list(self, app, user_1, user_2, user_3):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users', '/api/users',
headers=dict( headers=dict(
@ -136,11 +131,10 @@ def test_users_list(app, user_1, user_2, user_3):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['users']) == 3 assert len(data['data']['users']) == 3
assert 'created_at' in data['data']['users'][0] assert 'created_at' in data['data']['users'][0]
assert 'created_at' in data['data']['users'][1] assert 'created_at' in data['data']['users'][1]
@ -183,8 +177,8 @@ def test_users_list(app, user_1, user_2, user_3):
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_with_activities(
def test_users_list_with_activities( self,
app, app,
user_1, user_1,
user_2, user_2,
@ -194,14 +188,14 @@ def test_users_list_with_activities(
sport_2_running, sport_2_running,
activity_running_user_1, activity_running_user_1,
activity_cycling_user_2, activity_cycling_user_2,
): ):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users', '/api/users',
headers=dict( headers=dict(
@ -209,11 +203,10 @@ def test_users_list_with_activities(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['users']) == 3 assert len(data['data']['users']) == 3
assert 'created_at' in data['data']['users'][0] assert 'created_at' in data['data']['users'][0]
assert 'created_at' in data['data']['users'][1] assert 'created_at' in data['data']['users'][1]
@ -230,7 +223,7 @@ def test_users_list_with_activities(
assert data['data']['users'][0]['nb_sports'] == 2 assert data['data']['users'][0]['nb_sports'] == 2
assert data['data']['users'][0]['sports_list'] == [1, 2] assert data['data']['users'][0]['sports_list'] == [1, 2]
assert data['data']['users'][0]['total_distance'] == 22.0 assert data['data']['users'][0]['total_distance'] == 22.0
assert data['data']['users'][0]['total_duration'] == '1:57:04' assert data['data']['users'][0]['total_duration'] == '2:40:00'
assert data['data']['users'][1]['timezone'] is None assert data['data']['users'][1]['timezone'] is None
assert data['data']['users'][1]['weekm'] is False assert data['data']['users'][1]['weekm'] is False
assert data['data']['users'][1]['nb_activities'] == 1 assert data['data']['users'][1]['nb_activities'] == 1
@ -253,17 +246,17 @@ def test_users_list_with_activities(
'total': 3, 'total': 3,
} }
@patch('fittrackee_api.users.users.USER_PER_PAGE', 2)
@patch('fittrackee_api.users.users.USER_PER_PAGE', 2) def test_it_gets_first_page_on_users_list(
def test_it_gets_first_page_on_users_list( self, app, user_1, user_2, user_3,
app, user_1, user_2, user_3, ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?page=1', '/api/users?page=1',
headers=dict( headers=dict(
@ -284,17 +277,17 @@ def test_it_gets_first_page_on_users_list(
'total': 3, 'total': 3,
} }
@patch('fittrackee_api.users.users.USER_PER_PAGE', 2)
@patch('fittrackee_api.users.users.USER_PER_PAGE', 2) def test_it_gets_next_page_on_users_list(
def test_it_gets_next_page_on_users_list( self, app, user_1, user_2, user_3,
app, user_1, user_2, user_3, ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?page=2', '/api/users?page=2',
headers=dict( headers=dict(
@ -315,16 +308,16 @@ def test_it_gets_next_page_on_users_list(
'total': 3, 'total': 3,
} }
def test_it_gets_empty_next_page_on_users_list(
def test_it_gets_empty_next_page_on_users_list( self, app, user_1, user_2, user_3,
app, user_1, user_2, user_3, ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?page=2', '/api/users?page=2',
headers=dict( headers=dict(
@ -345,16 +338,16 @@ def test_it_gets_empty_next_page_on_users_list(
'total': 3, 'total': 3,
} }
def test_it_gets_user_list_with_2_per_page(
def test_it_gets_user_list_with_2_per_page( self, app, user_1, user_2, user_3,
app, user_1, user_2, user_3, ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?per_page=2', '/api/users?per_page=2',
headers=dict( headers=dict(
@ -375,16 +368,16 @@ def test_it_gets_user_list_with_2_per_page(
'total': 3, 'total': 3,
} }
def test_it_gets_next_page_on_user_list_with_2_per_page(
def test_it_gets_next_page_on_user_list_with_2_per_page( self, app, user_1, user_2, user_3,
app, user_1, user_2, user_3, ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?page=2&per_page=2', '/api/users?page=2&per_page=2',
headers=dict( headers=dict(
@ -405,8 +398,9 @@ def test_it_gets_next_page_on_user_list_with_2_per_page(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_username(
def test_it_gets_users_list_ordered_by_username(app, user_1, user_2, user_3): self, app, user_1, user_2, user_3
):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
@ -436,16 +430,16 @@ def test_it_gets_users_list_ordered_by_username(app, user_1, user_2, user_3):
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_username_ascending(
def test_it_gets_users_list_ordered_by_username_ascending( self, app, user_1, user_2, user_3
app, user_1, user_2, user_3 ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=username&order=asc', '/api/users?order_by=username&order=asc',
headers=dict( headers=dict(
@ -469,16 +463,16 @@ def test_it_gets_users_list_ordered_by_username_ascending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_username_descending(
def test_it_gets_users_list_ordered_by_username_descending( self, app, user_1, user_2, user_3
app, user_1, user_2, user_3 ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=username&order=desc', '/api/users?order_by=username&order=desc',
headers=dict( headers=dict(
@ -502,19 +496,21 @@ def test_it_gets_users_list_ordered_by_username_descending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_creation_date(
def test_it_gets_users_list_ordered_by_creation_date( self, app, user_2, user_3, user_1_admin
app, user_2, user_3, user_1_admin ):
):
user_2.created_at = datetime.utcnow() - timedelta(days=1) user_2.created_at = datetime.utcnow() - timedelta(days=1)
user_3.created_at = datetime.utcnow() - timedelta(hours=1) user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow() user_1_admin.created_at = datetime.utcnow()
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=created_at', '/api/users?order_by=created_at',
headers=dict( headers=dict(
@ -538,19 +534,21 @@ def test_it_gets_users_list_ordered_by_creation_date(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_creation_date_ascending(
def test_it_gets_users_list_ordered_by_creation_date_ascending( self, app, user_2, user_3, user_1_admin
app, user_2, user_3, user_1_admin ):
):
user_2.created_at = datetime.utcnow() - timedelta(days=1) user_2.created_at = datetime.utcnow() - timedelta(days=1)
user_3.created_at = datetime.utcnow() - timedelta(hours=1) user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow() user_1_admin.created_at = datetime.utcnow()
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=created_at&order=asc', '/api/users?order_by=created_at&order=asc',
headers=dict( headers=dict(
@ -574,20 +572,21 @@ def test_it_gets_users_list_ordered_by_creation_date_ascending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_creation_date_descending(
def test_it_gets_users_list_ordered_by_creation_date_descending( self, app, user_2, user_3, user_1_admin
app, user_2, user_3, user_1_admin ):
):
user_2.created_at = datetime.utcnow() - timedelta(days=1) user_2.created_at = datetime.utcnow() - timedelta(days=1)
user_3.created_at = datetime.utcnow() - timedelta(hours=1) user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow() user_1_admin.created_at = datetime.utcnow()
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=created_at&order=desc', '/api/users?order_by=created_at&order=desc',
headers=dict( headers=dict(
@ -611,16 +610,18 @@ def test_it_gets_users_list_ordered_by_creation_date_descending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_admin_rights(
def test_it_gets_users_list_ordered_by_admin_rights( self, app, user_2, user_1_admin, user_3
app, user_2, user_1_admin, user_3 ):
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=admin', '/api/users?order_by=admin',
headers=dict( headers=dict(
@ -644,16 +645,18 @@ def test_it_gets_users_list_ordered_by_admin_rights(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_admin_rights_ascending(
def test_it_gets_users_list_ordered_by_admin_rights_ascending( self, app, user_2, user_1_admin, user_3
app, user_2, user_1_admin, user_3 ):
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=admin&order=asc', '/api/users?order_by=admin&order=asc',
headers=dict( headers=dict(
@ -677,16 +680,18 @@ def test_it_gets_users_list_ordered_by_admin_rights_ascending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_admin_rights_descending(
def test_it_gets_users_list_ordered_by_admin_rights_descending( self, app, user_2, user_3, user_1_admin
app, user_2, user_3, 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',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=admin&order=desc', '/api/users?order_by=admin&order=desc',
headers=dict( headers=dict(
@ -710,16 +715,22 @@ def test_it_gets_users_list_ordered_by_admin_rights_descending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_activities_count(
def test_it_gets_users_list_ordered_by_activities_count( self,
app, user_1, user_2, user_3, sport_1_cycling, activity_cycling_user_2, app,
): user_1,
user_2,
user_3,
sport_1_cycling,
activity_cycling_user_2,
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=activities_count', '/api/users?order_by=activities_count',
headers=dict( headers=dict(
@ -746,16 +757,22 @@ def test_it_gets_users_list_ordered_by_activities_count(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_activities_count_ascending(
def test_it_gets_users_list_ordered_by_activities_count_ascending( self,
app, user_1, user_2, user_3, sport_1_cycling, activity_cycling_user_2, app,
): user_1,
user_2,
user_3,
sport_1_cycling,
activity_cycling_user_2,
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=activities_count&order=asc', '/api/users?order_by=activities_count&order=asc',
headers=dict( headers=dict(
@ -782,16 +799,22 @@ def test_it_gets_users_list_ordered_by_activities_count_ascending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_ordered_by_activities_count_descending(
def test_it_gets_users_list_ordered_by_activities_count_descending( self,
app, user_1, user_2, user_3, sport_1_cycling, activity_cycling_user_2, app,
): user_1,
user_2,
user_3,
sport_1_cycling,
activity_cycling_user_2,
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=activities_count&order=desc', '/api/users?order_by=activities_count&order=desc',
headers=dict( headers=dict(
@ -818,14 +841,16 @@ def test_it_gets_users_list_ordered_by_activities_count_descending(
'total': 3, 'total': 3,
} }
def test_it_gets_users_list_filtering_on_username(
def test_it_gets_users_list_filtering_on_username(app, user_1, user_2, user_3): self, app, user_1, user_2, user_3
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?q=toto', '/api/users?q=toto',
headers=dict( headers=dict(
@ -847,16 +872,16 @@ def test_it_gets_users_list_filtering_on_username(app, user_1, user_2, user_3):
'total': 1, 'total': 1,
} }
def test_it_returns_empty_users_list_filtering_on_username(
def test_it_returns_empty_users_list_filtering_on_username( self, app, user_1, user_2, user_3
app, user_1, user_2, user_3 ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?q=not_existing', '/api/users?q=not_existing',
headers=dict( headers=dict(
@ -877,14 +902,16 @@ def test_it_returns_empty_users_list_filtering_on_username(
'total': 0, 'total': 0,
} }
def test_it_users_list_with_complex_query(
def test_it_users_list_with_complex_query(app, user_1, user_2, user_3): self, app, user_1, user_2, user_3
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.get( response = client.get(
'/api/users?order_by=username&order=desc&page=2&per_page=2', '/api/users?order_by=username&order=desc&page=2&per_page=2',
headers=dict( headers=dict(
@ -907,45 +934,39 @@ def test_it_users_list_with_complex_query(app, user_1, user_2, user_3):
} }
def test_encode_auth_token(app, user_1): class TestGetUserPicture:
"""=> Ensure correct auth token generation""" def test_it_return_error_if_user_has_no_picture(self, app, user_1):
auth_token = user_1.encode_auth_token(user_1.id)
assert isinstance(auth_token, bytes)
def test_decode_auth_token(app, user_1):
auth_token = user_1.encode_auth_token(user_1.id)
assert isinstance(auth_token, bytes)
assert User.decode_auth_token(auth_token) == user_1.id
def test_user_no_picture(app, user_1):
client = app.test_client() client = app.test_client()
response = client.get(f'/api/users/{user_1.username}/picture')
data = json.loads(response.data.decode())
response = client.get(f'/api/users/{user_1.username}/picture')
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']
assert 'No picture.' in data['message'] assert 'No picture.' in data['message']
def test_it_returns_error_if_user_does_not_exist(self, app, user_1):
def test_user_picture_no_user(app, user_1):
client = app.test_client() client = app.test_client()
response = client.get('/api/users/not_existing/picture')
data = json.loads(response.data.decode())
response = client.get('/api/users/not_existing/picture')
data = json.loads(response.data.decode())
assert response.status_code == 404 assert response.status_code == 404
assert 'fail' in data['status'] assert 'fail' in data['status']
assert 'User does not exist.' in data['message'] assert 'User does not exist.' in data['message']
def test_it_adds_admin_rights_to_a_user(app, user_1_admin, user_2): class TestUpdateUser:
def test_it_adds_admin_rights_to_a_user(self, app, user_1_admin, user_2):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/users/toto', '/api/users/toto',
content_type='application/json', content_type='application/json',
@ -955,24 +976,27 @@ def test_it_adds_admin_rights_to_a_user(app, user_1_admin, user_2):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['users']) == 1 assert len(data['data']['users']) == 1
user = data['data']['users'][0] user = data['data']['users'][0]
assert user['email'] == 'toto@toto.com' assert user['email'] == 'toto@toto.com'
assert user['admin'] is True assert user['admin'] is True
def test_it_removes_admin_rights_to_a_user(
def test_it_removes_admin_rights_to_a_user(app, user_1_admin, user_2): self, app, user_1_admin, user_2
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/users/toto', '/api/users/toto',
content_type='application/json', content_type='application/json',
@ -982,8 +1006,8 @@ def test_it_removes_admin_rights_to_a_user(app, user_1_admin, user_2):
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['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']['users']) == 1 assert len(data['data']['users']) == 1
@ -992,16 +1016,18 @@ def test_it_removes_admin_rights_to_a_user(app, user_1_admin, user_2):
assert user['email'] == 'toto@toto.com' assert user['email'] == 'toto@toto.com'
assert user['admin'] is False assert user['admin'] is False
def test_it_returns_error_if_payload_for_admin_rights_is_empty(
def test_it_returns_error_if_payload_for_admin_rights_is_empty( self, app, user_1_admin, user_2
app, user_1_admin, user_2 ):
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/users/toto', '/api/users/toto',
content_type='application/json', content_type='application/json',
@ -1011,22 +1037,24 @@ def test_it_returns_error_if_payload_for_admin_rights_is_empty(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 400 assert response.status_code == 400
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Invalid payload.' in data['message'] assert 'Invalid payload.' in data['message']
def test_it_returns_error_if_payload_for_admin_rights_is_invalid(
def test_it_returns_error_if_payload_for_admin_rights_is_invalid( self, app, user_1_admin, user_2
app, user_1_admin, user_2 ):
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/users/toto', '/api/users/toto',
content_type='application/json', content_type='application/json',
@ -1036,8 +1064,8 @@ def test_it_returns_error_if_payload_for_admin_rights_is_invalid(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert ( assert (
@ -1045,16 +1073,16 @@ def test_it_returns_error_if_payload_for_admin_rights_is_invalid(
in data['message'] in data['message']
) )
def test_it_returns_error_if_user_can_not_change_admin_rights(
def test_it_returns_error_if_user_can_not_change_admin_rights( self, app, user_1, user_2
app, user_1, user_2 ):
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.patch( response = client.patch(
'/api/users/toto', '/api/users/toto',
content_type='application/json', content_type='application/json',
@ -1064,20 +1092,22 @@ def test_it_returns_error_if_user_can_not_change_admin_rights(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 403 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_user_can_delete_its_own_account(app, user_1): class TestDeleteUser:
def test_user_can_delete_its_own_account(self, 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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.delete( response = client.delete(
'/api/users/test', '/api/users/test',
headers=dict( headers=dict(
@ -1088,10 +1118,9 @@ def test_user_can_delete_its_own_account(app, user_1):
assert response.status_code == 204 assert response.status_code == 204
def test_user_with_activity_can_delete_its_own_account(
def test_user_with_activity_can_delete_its_own_account( self, app, user_1, sport_1_cycling, gpx_file
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',
@ -1110,6 +1139,7 @@ def test_user_with_activity_can_delete_its_own_account(
+ json.loads(resp_login.data.decode())['auth_token'], + json.loads(resp_login.data.decode())['auth_token'],
), ),
) )
response = client.delete( response = client.delete(
'/api/users/test', '/api/users/test',
headers=dict( headers=dict(
@ -1120,10 +1150,9 @@ def test_user_with_activity_can_delete_its_own_account(
assert response.status_code == 204 assert response.status_code == 204
def test_user_with_picture_can_delete_its_own_account(
def test_user_with_picture_can_delete_its_own_account( self, app, user_1, sport_1_cycling, gpx_file
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',
@ -1139,6 +1168,7 @@ def test_user_with_picture_can_delete_its_own_account(
+ json.loads(resp_login.data.decode())['auth_token'], + json.loads(resp_login.data.decode())['auth_token'],
), ),
) )
response = client.delete( response = client.delete(
'/api/users/test', '/api/users/test',
headers=dict( headers=dict(
@ -1149,14 +1179,16 @@ def test_user_with_picture_can_delete_its_own_account(
assert response.status_code == 204 assert response.status_code == 204
def test_user_can_not_delete_another_user_account(
def test_user_can_not_delete_another_user_account(app, user_1, user_2): self, app, user_1, user_2
):
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(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.delete( response = client.delete(
'/api/users/toto', '/api/users/toto',
headers=dict( headers=dict(
@ -1170,14 +1202,16 @@ def test_user_can_not_delete_another_user_account(app, user_1, user_2):
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_it_returns_error_when_deleting_non_existing_user(
def test_it_returns_error_when_deleting_non_existing_user(app, user_1): self, 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',
data=json.dumps(dict(email='test@test.com', password='12345678')), data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json', content_type='application/json',
) )
response = client.delete( response = client.delete(
'/api/users/not_existing', '/api/users/not_existing',
headers=dict( headers=dict(
@ -1191,14 +1225,18 @@ def test_it_returns_error_when_deleting_non_existing_user(app, user_1):
assert 'not found' in data['status'] assert 'not found' in data['status']
assert 'User does not exist.' in data['message'] assert 'User does not exist.' in data['message']
def test_admin_can_delete_another_user_account(
def test_admin_can_delete_another_user_account(app, user_1_admin, user_2): self, app, user_1_admin, user_2
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.delete( response = client.delete(
'/api/users/toto', '/api/users/toto',
headers=dict( headers=dict(
@ -1209,14 +1247,18 @@ def test_admin_can_delete_another_user_account(app, user_1_admin, user_2):
assert response.status_code == 204 assert response.status_code == 204
def test_admin_can_delete_its_own_account(
def test_admin_can_delete_its_own_account(app, user_1_admin, user_2_admin): self, app, user_1_admin, user_2_admin
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.delete( response = client.delete(
'/api/users/admin', '/api/users/admin',
headers=dict( headers=dict(
@ -1227,14 +1269,15 @@ def test_admin_can_delete_its_own_account(app, user_1_admin, user_2_admin):
assert response.status_code == 204 assert response.status_code == 204
def test_admin_can_not_delete_its_own_account_if_no_other_admin(
def test_admin_can_not_delete_its_own_account_if_no_other_admin( self, app, user_1_admin, user_2
app, user_1_admin, user_2 ):
):
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(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
response = client.delete( response = client.delete(
@ -1253,17 +1296,19 @@ def test_admin_can_not_delete_its_own_account_if_no_other_admin(
in data['message'] in data['message']
) )
def test_it_enables_registration_on_user_delete(
def test_it_enables_registration_on_user_delete( self, app_no_config, app_config, user_1_admin, user_2, user_3
app_no_config, app_config, user_1_admin, user_2, user_3 ):
):
app_config.max_users = 3 app_config.max_users = 3
client = app_no_config.test_client() client = app_no_config.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
client.delete( client.delete(
'/api/users/toto', '/api/users/toto',
headers=dict( headers=dict(
@ -1285,17 +1330,19 @@ def test_it_enables_registration_on_user_delete(
) )
assert response.status_code == 201 assert response.status_code == 201
def test_it_does_not_enable_registration_on_user_delete(
def test_it_does_not_enable_registration_on_user_delete( self, app_no_config, app_config, user_1_admin, user_2, user_3
app_no_config, app_config, user_1_admin, user_2, user_3 ):
):
app_config.max_users = 2 app_config.max_users = 2
client = app_no_config.test_client() client = app_no_config.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')), data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json', content_type='application/json',
) )
client.delete( client.delete(
'/api/users/toto', '/api/users/toto',
headers=dict( headers=dict(
@ -1315,6 +1362,7 @@ def test_it_does_not_enable_registration_on_user_delete(
), ),
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 403 assert response.status_code == 403
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert data['status'] == 'error' assert data['status'] == 'error'

View File

@ -1,4 +1,8 @@
def test_user_model(app, user_1): from fittrackee_api.users.models import User
class TestUserModel:
def test_user_model(self, app, user_1):
assert '<User \'test\'>' == str(user_1) assert '<User \'test\'>' == str(user_1)
serialized_user = user_1.serialize() serialized_user = user_1.serialize()
@ -18,3 +22,12 @@ def test_user_model(app, user_1):
assert serialized_user['nb_sports'] == 0 assert serialized_user['nb_sports'] == 0
assert serialized_user['total_distance'] == 0 assert serialized_user['total_distance'] == 0
assert serialized_user['total_duration'] == '0:00:00' assert serialized_user['total_duration'] == '0:00:00'
def test_encode_auth_token(self, app, user_1):
auth_token = user_1.encode_auth_token(user_1.id)
assert isinstance(auth_token, bytes)
def test_decode_auth_token(self, app, user_1):
auth_token = user_1.encode_auth_token(user_1.id)
assert isinstance(auth_token, bytes)
assert User.decode_auth_token(auth_token) == user_1.id