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

@ -1,7 +1,9 @@
import json import json
def test_get_all_activities_for_authenticated_user( class TestGetActivities:
def test_it_gets_all_activities_for_authenticated_user(
self,
app, app,
user_1, user_1,
user_2, user_2,
@ -11,13 +13,13 @@ def test_get_all_activities_for_authenticated_user(
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/activities', '/api/activities',
headers=dict( headers=dict(
@ -25,12 +27,11 @@ def test_get_all_activities_for_authenticated_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 == 200 assert response.status_code == 200
assert 'success' in data['status'] assert 'success' in data['status']
assert len(data['data']['activities']) == 2 assert len(data['data']['activities']) == 2
assert 'creation_date' in data['data']['activities'][0] assert 'creation_date' in data['data']['activities'][0]
assert ( assert (
'Sun, 01 Apr 2018 00:00:00 GMT' 'Sun, 01 Apr 2018 00:00:00 GMT'
@ -49,10 +50,10 @@ def test_get_all_activities_for_authenticated_user(
assert 'test' == data['data']['activities'][1]['user'] assert 'test' == data['data']['activities'][1]['user']
assert 1 == data['data']['activities'][1]['sport_id'] assert 1 == data['data']['activities'][1]['sport_id']
assert 10.0 == data['data']['activities'][1]['distance'] assert 10.0 == data['data']['activities'][1]['distance']
assert '0:17:04' == data['data']['activities'][1]['duration'] assert '1:00:00' == data['data']['activities'][1]['duration']
def test_it_gets_no_activities_for_authenticated_user_with_no_activities(
def test_get_activities_for_authenticated_user_no_activity( self,
app, app,
user_1, user_1,
user_2, user_2,
@ -67,6 +68,7 @@ def test_get_activities_for_authenticated_user_no_activity(
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.get( response = client.get(
'/api/activities', '/api/activities',
headers=dict( headers=dict(
@ -74,25 +76,26 @@ def test_get_activities_for_authenticated_user_no_activity(
+ 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']) == 0 assert len(data['data']['activities']) == 0
def test_it_returns_401_if_user_is_not_authenticated(self, app):
def test_get_activities_for_authenticated_user_no_authentication(app):
client = app.test_client() client = app.test_client()
response = client.get('/api/activities')
data = json.loads(response.data.decode())
response = client.get('/api/activities')
data = json.loads(response.data.decode())
assert response.status_code == 401 assert response.status_code == 401
assert 'error' in data['status'] assert 'error' in data['status']
assert 'Provide a valid auth token.' in data['message'] assert 'Provide a valid auth token.' in data['message']
def test_get_activities_pagination( class TestGetActivitiesWithPagination:
app, user_1, sport_1_cycling, seven_activities_user_1 def test_it_gets_activities_with_default_pagination(
self, app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -100,6 +103,7 @@ def test_get_activities_pagination(
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/activities', '/api/activities',
headers=dict( headers=dict(
@ -107,8 +111,8 @@ def test_get_activities_pagination(
+ 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']) == 5 assert len(data['data']['activities']) == 5
@ -125,6 +129,16 @@ def test_get_activities_pagination(
) )
assert '0:17:04' == data['data']['activities'][4]['duration'] assert '0:17:04' == data['data']['activities'][4]['duration']
def test_it_gets_first_page(
self, app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities?page=1', '/api/activities?page=1',
headers=dict( headers=dict(
@ -132,8 +146,8 @@ def test_get_activities_pagination(
+ 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']) == 5 assert len(data['data']['activities']) == 5
@ -150,6 +164,16 @@ def test_get_activities_pagination(
) )
assert '0:17:04' == data['data']['activities'][4]['duration'] assert '0:17:04' == data['data']['activities'][4]['duration']
def test_it_gets_second_page(
self, app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities?page=2', '/api/activities?page=2',
headers=dict( headers=dict(
@ -157,8 +181,8 @@ def test_get_activities_pagination(
+ 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']) == 2 assert len(data['data']['activities']) == 2
@ -175,6 +199,16 @@ def test_get_activities_pagination(
) )
assert '0:17:04' == data['data']['activities'][1]['duration'] assert '0:17:04' == data['data']['activities'][1]['duration']
def test_it_gets_empty_third_page(
self, app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities?page=3', '/api/activities?page=3',
headers=dict( headers=dict(
@ -182,15 +216,14 @@ def test_get_activities_pagination(
+ 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']) == 0 assert len(data['data']['activities']) == 0
def test_it_returns_error_on_invalid_page_value(
def test_get_activities_pagination_error( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -198,6 +231,7 @@ def test_get_activities_pagination_error(
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/activities?page=A', '/api/activities?page=A',
headers=dict( headers=dict(
@ -205,8 +239,8 @@ def test_get_activities_pagination_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 (
@ -214,9 +248,8 @@ def test_get_activities_pagination_error(
in data['message'] in data['message']
) )
def test_it_gets_5_activities_per_page(
def test_get_activities_date_filter( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -224,6 +257,71 @@ def test_get_activities_date_filter(
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(
'/api/activities?per_page=10',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 7
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][6]['activity_date']
)
def test_it_gets_3_activities_per_page(
self, app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get(
'/api/activities?per_page=3',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 3
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['activities'][2]['activity_date']
)
class TestGetActivitiesWithFilters:
def test_it_gets_activities_with_date_filter(
self, app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities?from=2018-02-01&to=2018-02-28', '/api/activities?from=2018-02-01&to=2018-02-28',
headers=dict( headers=dict(
@ -231,8 +329,8 @@ def test_get_activities_date_filter(
+ 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']) == 2 assert len(data['data']['activities']) == 2
@ -249,9 +347,8 @@ def test_get_activities_date_filter(
) )
assert '0:16:40' == data['data']['activities'][1]['duration'] assert '0:16:40' == data['data']['activities'][1]['duration']
def test_it_gets_no_activities_with_date_filter(
def test_get_activities_date_filter_no_results( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -259,6 +356,7 @@ def test_get_activities_date_filter_no_results(
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/activities?from=2018-03-01&to=2018-03-30', '/api/activities?from=2018-03-01&to=2018-03-30',
headers=dict( headers=dict(
@ -266,15 +364,14 @@ def test_get_activities_date_filter_no_results(
+ 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']) == 0 assert len(data['data']['activities']) == 0
def test_if_gets_activities_with_date_filter_from(
def test_get_activities_date_filter_from( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -282,6 +379,7 @@ def test_get_activities_date_filter_from(
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/activities?from=2018-04-01', '/api/activities?from=2018-04-01',
headers=dict( headers=dict(
@ -289,8 +387,8 @@ def test_get_activities_date_filter_from(
+ 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']) == 2 assert len(data['data']['activities']) == 2
@ -304,9 +402,8 @@ def test_get_activities_date_filter_from(
== data['data']['activities'][1]['activity_date'] == data['data']['activities'][1]['activity_date']
) )
def test_it_gets_activities_with_date_filter_to(
def test_get_activities_date_filter_to( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -314,6 +411,7 @@ def test_get_activities_date_filter_to(
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/activities?to=2017-12-31', '/api/activities?to=2017-12-31',
headers=dict( headers=dict(
@ -321,8 +419,8 @@ def test_get_activities_date_filter_to(
+ 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']) == 2 assert len(data['data']['activities']) == 2
@ -335,9 +433,8 @@ def test_get_activities_date_filter_to(
== data['data']['activities'][1]['activity_date'] == data['data']['activities'][1]['activity_date']
) )
def test_it_gets_activities_with_ascending_order(
def test_get_activities_date_filter_paginate( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -345,37 +442,7 @@ def test_get_activities_date_filter_paginate(
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(
'/api/activities?from=2017-01-01&page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
def test_get_activities_order(
app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities?order=asc', '/api/activities?order=asc',
headers=dict( headers=dict(
@ -383,8 +450,8 @@ def test_get_activities_order(
+ 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']) == 5 assert len(data['data']['activities']) == 5
@ -397,9 +464,8 @@ def test_get_activities_order(
== data['data']['activities'][4]['activity_date'] == data['data']['activities'][4]['activity_date']
) )
def test_it_gets_activities_with_distance_filter(
def test_get_activities_date_filter_paginate_order( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -407,37 +473,7 @@ def test_get_activities_date_filter_paginate_order(
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(
'/api/activities?from=2017-01-01&page=2&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
def test_get_activities_distance_filter(
app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities?distance_from=5&distance_to=8', '/api/activities?distance_from=5&distance_to=8',
headers=dict( headers=dict(
@ -445,8 +481,8 @@ def test_get_activities_distance_filter(
+ 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']) == 2 assert len(data['data']['activities']) == 2
@ -459,9 +495,8 @@ def test_get_activities_distance_filter(
== data['data']['activities'][1]['activity_date'] == data['data']['activities'][1]['activity_date']
) )
def test_it_gets_activities_with_duration_filter(
def test_get_activities_duration_filter( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -469,6 +504,7 @@ def test_get_activities_duration_filter(
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/activities?duration_from=00:52&duration_to=01:20', '/api/activities?duration_from=00:52&duration_to=01:20',
headers=dict( headers=dict(
@ -476,8 +512,8 @@ def test_get_activities_duration_filter(
+ 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
@ -486,9 +522,8 @@ def test_get_activities_duration_filter(
== data['data']['activities'][0]['activity_date'] == data['data']['activities'][0]['activity_date']
) )
def test_it_gets_activities_with_average_speed_filter(
def test_get_activities_ave_speed_filter( self, app, user_1, sport_1_cycling, seven_activities_user_1
app, user_1, sport_1_cycling, seven_activities_user_1
): ):
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
@ -496,6 +531,7 @@ def test_get_activities_ave_speed_filter(
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/activities?ave_speed_from=5&ave_speed_to=10', '/api/activities?ave_speed_from=5&ave_speed_to=10',
headers=dict( headers=dict(
@ -503,8 +539,8 @@ def test_get_activities_ave_speed_filter(
+ 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
@ -513,8 +549,8 @@ def test_get_activities_ave_speed_filter(
== data['data']['activities'][0]['activity_date'] == data['data']['activities'][0]['activity_date']
) )
def test_it_gets_activities_with_max_speed_filter(
def test_get_activities_max_speed_filter( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
@ -530,6 +566,7 @@ def test_get_activities_max_speed_filter(
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/activities?max_speed_from=10&max_speed_to=20', '/api/activities?max_speed_from=10&max_speed_to=20',
headers=dict( headers=dict(
@ -537,8 +574,8 @@ def test_get_activities_max_speed_filter(
+ 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
@ -547,8 +584,8 @@ def test_get_activities_max_speed_filter(
== data['data']['activities'][0]['activity_date'] == data['data']['activities'][0]['activity_date']
) )
def test_it_gets_activities_with_sport_filter(
def test_get_activities_sport_filter( self,
app, app,
user_1, user_1,
sport_1_cycling, sport_1_cycling,
@ -562,6 +599,7 @@ def test_get_activities_sport_filter(
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/activities?sport_id=2', '/api/activities?sport_id=2',
headers=dict( headers=dict(
@ -569,8 +607,8 @@ def test_get_activities_sport_filter(
+ 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
@ -580,16 +618,81 @@ def test_get_activities_sport_filter(
) )
def test_get_an_activity( class TestGetActivitiesWithFiltersAndPagination:
app, user_1, sport_1_cycling, activity_cycling_user_1 def test_it_gets_page_2_with_date_filter(
self, app, user_1, sport_1_cycling, seven_activities_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(
'/api/activities?from=2017-01-01&page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
def test_it_get_page_2_with_date_filter_and_ascending_order(
self, app, user_1, sport_1_cycling, seven_activities_user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get(
'/api/activities?from=2017-01-01&page=2&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
class TestGetActivity:
def test_it_gets_an_activity(
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.get( response = client.get(
'/api/activities/1', '/api/activities/1',
headers=dict( headers=dict(
@ -597,12 +700,11 @@ def test_get_an_activity(
+ 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 (
'Mon, 01 Jan 2018 00:00:00 GMT' 'Mon, 01 Jan 2018 00:00:00 GMT'
@ -611,19 +713,18 @@ def test_get_an_activity(
assert 'test' == data['data']['activities'][0]['user'] assert 'test' == data['data']['activities'][0]['user']
assert 1 == data['data']['activities'][0]['sport_id'] assert 1 == data['data']['activities'][0]['sport_id']
assert 10.0 == data['data']['activities'][0]['distance'] assert 10.0 == data['data']['activities'][0]['distance']
assert '0:17:04' == data['data']['activities'][0]['duration'] assert '1:00:00' == data['data']['activities'][0]['duration']
def test_it_returns_403_if_activity_belongs_to_a_different_user(
def test_get_an_activity_different_user( self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_2
app, user_1, user_2, 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/activities/1', '/api/activities/1',
headers=dict( headers=dict(
@ -631,72 +732,20 @@ def test_get_an_activity_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_returns_404_if_activity_does_not_exist(self, app, user_1):
def test_get_activities_per_page(
app, user_1, sport_1_cycling, seven_activities_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(
'/api/activities?per_page=10',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 7
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][6]['activity_date']
)
response = client.get(
'/api/activities?per_page=3',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 3
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['activities'][2]['activity_date']
)
def test_get_an_activity_invalid_id(app, user_1):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities/11', '/api/activities/11',
headers=dict( headers=dict(
@ -704,20 +753,22 @@ def test_get_an_activity_invalid_id(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']['activities']) == 0 assert len(data['data']['activities']) == 0
def test_it_returns_404_on_getting_gpx_if_activity_does_not_exist(
def test_get_an_activity_no_activity_no_gpx(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.get( response = client.get(
'/api/activities/11/gpx', '/api/activities/11/gpx',
headers=dict( headers=dict(
@ -725,13 +776,23 @@ def test_get_an_activity_no_activity_no_gpx(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 'Activity not found (id: 11)' in data['message'] assert 'Activity not found (id: 11)' in data['message']
assert data['data']['gpx'] == '' assert data['data']['gpx'] == ''
def test_it_returns_404_on_getting_chart_data_if_activity_does_not_exist(
self, app, user_1
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities/11/chart_data', '/api/activities/11/chart_data',
headers=dict( headers=dict(
@ -739,16 +800,15 @@ def test_get_an_activity_no_activity_no_gpx(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 'Activity not found (id: 11)' in data['message'] assert 'Activity not found (id: 11)' in data['message']
assert data['data']['chart_data'] == '' assert data['data']['chart_data'] == ''
def test_it_returns_404_on_getting_gpx_if_activity_have_no_gpx(
def test_get_an_activity_activity_no_gpx( 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(
@ -756,6 +816,7 @@ def test_get_an_activity_activity_no_gpx(
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/activities/1/gpx', '/api/activities/1/gpx',
headers=dict( headers=dict(
@ -763,12 +824,22 @@ def test_get_an_activity_activity_no_gpx(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
assert response.status_code == 400 data = json.loads(response.data.decode())
assert 'fail' in data['status'] assert response.status_code == 404
assert 'error' in data['status']
assert 'No gpx file for this activity (id: 1)' in data['message'] assert 'No gpx file for this activity (id: 1)' in data['message']
def test_it_returns_404_if_activity_have_no_chart_data(
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.get( response = client.get(
'/api/activities/1/chart_data', '/api/activities/1/chart_data',
headers=dict( headers=dict(
@ -776,15 +847,14 @@ def test_get_an_activity_activity_no_gpx(
+ json.loads(resp_login.data.decode())['auth_token'] + json.loads(resp_login.data.decode())['auth_token']
), ),
) )
data = json.loads(response.data.decode())
assert response.status_code == 400 data = json.loads(response.data.decode())
assert 'fail' in data['status'] assert response.status_code == 404
assert 'error' in data['status']
assert 'No gpx file for this activity (id: 1)' in data['message'] assert 'No gpx file for this activity (id: 1)' in data['message']
def test_it_returns_500_on_getting_gpx_if_an_activity_has_invalid_gpx_pathname( # noqa
def test_get_an_activity_activity_invalid_gpx( self, app, user_1, sport_1_cycling, activity_cycling_user_1
app, user_1, sport_1_cycling, activity_cycling_user_1
): ):
activity_cycling_user_1.gpx = "some path" activity_cycling_user_1.gpx = "some path"
client = app.test_client() client = app.test_client()
@ -793,6 +863,7 @@ def test_get_an_activity_activity_invalid_gpx(
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/activities/1/gpx', '/api/activities/1/gpx',
headers=dict( headers=dict(
@ -800,13 +871,24 @@ def test_get_an_activity_activity_invalid_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 == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert 'internal error' in data['message'] assert 'internal error' in data['message']
assert 'data' not in data assert 'data' not in data
def test_it_returns_500_on_getting_chart_data_if_an_activity_has_invalid_gpx_pathname( # noqa
self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
activity_cycling_user_1.gpx = "some path"
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get( response = client.get(
'/api/activities/1/chart_data', '/api/activities/1/chart_data',
headers=dict( headers=dict(
@ -814,15 +896,14 @@ def test_get_an_activity_activity_invalid_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 == 500 assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert 'internal error' in data['message'] assert 'internal error' in data['message']
assert 'data' not in data assert 'data' not in data
def test_it_returns_404_if_activity_has_no_map(self, app, user_1):
def test_get_map_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',
@ -839,5 +920,5 @@ def test_get_map_no_activity(app, user_1):
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 404 assert response.status_code == 404
assert 'fail' in data['status'] assert 'error' in data['status']
assert 'Map does not exist' in data['message'] assert 'Map does not exist' in data['message']

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,8 +47,9 @@ 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(
@ -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,17 +119,16 @@ 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(
@ -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,15 +163,14 @@ 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(
@ -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,9 +209,8 @@ 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(
@ -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,14 +242,12 @@ 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(
@ -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,17 +279,22 @@ 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(
@ -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,30 +423,29 @@ 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(
@ -485,29 +453,7 @@ def test_edit_an_activity_wo_gpx_different_user(
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,15 +471,19 @@ 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(
@ -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,146 +557,13 @@ 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,
client = app.test_client() app,
resp_login = client.post( user_1,
'/api/auth/login', sport_1_cycling,
data=json.dumps(dict(email='test@test.com', password='12345678')), sport_2_running,
content_type='application/json', activity_cycling_user_1,
)
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(
'/api/activities/1',
content_type='application/json',
data=json.dumps(dict(sport_id=1, distance=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert 'creation_date' in data['data']['activities'][0]
assert (
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'] == 10.0
assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] == 10.0
assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] == 10.0
assert data['data']['activities'][0]['min_alt'] is None
assert data['data']['activities'][0]['moving'] == '1:00:00'
assert data['data']['activities'][0]['pauses'] is None
assert data['data']['activities'][0]['with_gpx'] is False
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'] == 10.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'] == 10.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'] == 10.0
def test_edit_an_activity_wo_gpx_invalid_payload(
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(
@ -822,6 +571,74 @@ def test_edit_an_activity_wo_gpx_invalid_payload(
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(
'/api/activities/1',
content_type='application/json',
data=json.dumps(dict(sport_id=2, distance=20)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert 'creation_date' in data['data']['activities'][0]
assert (
data['data']['activities'][0]['activity_date']
== 'Mon, 01 Jan 2018 00:00:00 GMT'
)
assert data['data']['activities'][0]['user'] == 'test'
assert data['data']['activities'][0]['sport_id'] == 2
assert data['data']['activities'][0]['duration'] == '1:00:00'
assert data['data']['activities'][0]['title'] is None
assert data['data']['activities'][0]['ascent'] is None
assert data['data']['activities'][0]['ave_speed'] == 20.0
assert data['data']['activities'][0]['descent'] is None
assert data['data']['activities'][0]['distance'] == 20.0
assert data['data']['activities'][0]['max_alt'] is None
assert data['data']['activities'][0]['max_speed'] == 20.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'] == 2
assert records[0]['activity_id'] == 1
assert records[0]['record_type'] == 'MS'
assert records[0]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[0]['value'] == 20.0
assert records[1]['sport_id'] == 2
assert records[1]['activity_id'] == 1
assert records[1]['record_type'] == 'LD'
assert records[1]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[1]['value'] == '1:00:00'
assert records[2]['sport_id'] == 2
assert records[2]['activity_id'] == 1
assert records[2]['record_type'] == 'FD'
assert records[2]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[2]['value'] == 20.0
assert records[3]['sport_id'] == 2
assert records[3]['activity_id'] == 1
assert records[3]['record_type'] == 'AS'
assert records[3]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
assert records[3]['value'] == 20.0
def test_it_returns_400_if_payload_is_empty(
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',
@ -833,14 +650,12 @@ 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(
@ -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,14 +716,14 @@ 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
class TestRefreshActivityWithGpx:
def test_refresh_an_activity_with_gpx( def test_refresh_an_activity_with_gpx(
app, user_1, sport_1_cycling, sport_2_running, gpx_file 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(

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,9 +43,8 @@ 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(
@ -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,9 +101,8 @@ 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(
@ -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,8 +47,8 @@ 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,
@ -50,4 +57,4 @@ def test_add_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,8 +458,8 @@ 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,
@ -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,8 +806,50 @@ def test_update_user_invalid_picture(app, user_1):
assert response.status_code == 400 assert response.status_code == 400
class TestRegistrationConfiguration:
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( def test_it_disables_registration_on_user_registration(
app_no_config, app_config, user_1_admin, user_2 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()
@ -872,9 +882,8 @@ 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(
app_no_config, app_config, user_1_admin, user_2, self, 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()

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,
@ -69,8 +71,8 @@ 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,
@ -97,9 +99,8 @@ 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(
@ -137,9 +138,8 @@ 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(
@ -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,9 +673,8 @@ 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(

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,9 +28,8 @@ 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,
@ -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,16 +92,15 @@ 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,
@ -113,9 +117,8 @@ 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,

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,18 +48,16 @@ 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(
@ -63,6 +65,7 @@ def test_get_all_sports_with_inactive_one(
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,3 +1,5 @@
class TestSportModel:
@staticmethod
def assert_sport_model(sport, is_admin=False): def assert_sport_model(sport, is_admin=False):
assert 1 == sport.id assert 1 == sport.id
assert 'Cycling' == sport.label assert 'Cycling' == sport.label
@ -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(
app, sport_1_cycling, user_1, activity_cycling_user_1 self, app, sport_1_cycling, user_1, activity_cycling_user_1
): ):
serialized_sport = assert_sport_model(sport_1_cycling) serialized_sport = self.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 = assert_sport_model(sport_1_cycling, True) serialized_sport = self.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,14 +38,14 @@ 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,
@ -57,15 +59,16 @@ def test_get_stats_by_time_all_activities_error(
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,8 +76,8 @@ 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,
@ -88,6 +91,7 @@ def test_get_stats_by_time_all_activities_invalid_period(
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,14 +99,14 @@ 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,
@ -116,6 +120,7 @@ def test_get_stats_by_time_all_activities(
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,8 +154,8 @@ 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,
@ -164,15 +169,16 @@ def test_get_stats_by_time_all_activities_april_2018(
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,8 +196,8 @@ 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,
@ -205,6 +211,7 @@ def test_get_stats_by_time_all_activities_april_2018_paris(
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,8 +239,8 @@ 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,
@ -247,6 +254,7 @@ def test_get_stats_by_year_all_activities(
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,8 +288,8 @@ 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,
@ -295,6 +303,7 @@ def test_get_stats_by_year_all_activities_april_2018(
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,8 +330,8 @@ 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,
@ -336,6 +345,7 @@ def test_get_stats_by_year_all_activities_april_2018_paris(
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,8 +372,8 @@ 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,
@ -377,6 +387,7 @@ def test_get_stats_by_month_all_activities(
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,8 +449,8 @@ 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,
@ -453,6 +464,7 @@ def test_get_stats_by_month_all_activities_new_york(
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,8 +526,8 @@ 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,
@ -529,6 +541,7 @@ def test_get_stats_by_month_all_activities_april_2018(
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,8 +568,8 @@ 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,
@ -570,6 +583,7 @@ def test_get_stats_by_week_all_activities(
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,8 +645,8 @@ 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,
@ -646,6 +660,7 @@ def test_get_stats_by_week_all_activities_week_13(
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,8 +687,8 @@ 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,
@ -687,6 +702,7 @@ def test_get_stats_by_weekm_all_activities(
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,8 +764,8 @@ 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,
@ -763,6 +779,7 @@ def test_get_stats_by_weekm_all_activities_week_13(
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,7 +807,9 @@ 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,
@ -804,6 +823,7 @@ def test_get_stats_by_sport_all_activities(
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,8 +848,8 @@ 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,
@ -843,6 +863,7 @@ def test_get_stats_by_sport_all_activities_sport_1(
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,8 +883,8 @@ 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,
@ -877,6 +898,7 @@ def test_get_stats_by_sport_all_activities_invalid_user(
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,14 +906,14 @@ 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,
@ -905,6 +927,7 @@ def test_get_stats_by_sport_all_activities_invalid_sport(
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,14 +935,14 @@ 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,
@ -933,6 +956,7 @@ def test_get_stats_by_sport_all_activities_error(
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,
@ -988,9 +1018,12 @@ def test_get_app_stats_with_activities(
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,
@ -1025,6 +1058,7 @@ def test_get_app_stats_no_admin(
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,8 +45,8 @@ 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,
@ -56,13 +54,13 @@ def test_single_user_with_activities(
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,
@ -195,13 +189,13 @@ def test_users_list_with_activities(
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,10 +246,9 @@ 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(
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(
@ -264,6 +256,7 @@ def test_it_gets_first_page_on_users_list(
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,10 +277,9 @@ 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(
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(
@ -295,6 +287,7 @@ def test_it_gets_next_page_on_users_list(
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,9 +308,8 @@ 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(
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(
@ -325,6 +317,7 @@ def test_it_gets_empty_next_page_on_users_list(
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,9 +338,8 @@ 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(
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(
@ -355,6 +347,7 @@ def test_it_gets_user_list_with_2_per_page(
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,9 +368,8 @@ 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(
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(
@ -385,6 +377,7 @@ def test_it_gets_next_page_on_user_list_with_2_per_page(
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,9 +430,8 @@ 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(
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(
@ -446,6 +439,7 @@ def test_it_gets_users_list_ordered_by_username_ascending(
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,9 +463,8 @@ 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(
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(
@ -479,6 +472,7 @@ def test_it_gets_users_list_ordered_by_username_descending(
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,9 +496,8 @@ 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(
app, user_2, user_3, user_1_admin self, 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)
@ -512,9 +505,12 @@ def test_it_gets_users_list_ordered_by_creation_date(
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,9 +534,8 @@ 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(
app, user_2, user_3, user_1_admin self, 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)
@ -548,9 +543,12 @@ def test_it_gets_users_list_ordered_by_creation_date_ascending(
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(
app, user_2, user_3, user_1_admin self, 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(
app, user_2, user_1_admin, user_3 self, 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(
app, user_2, user_1_admin, user_3 self, 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(
app, user_2, user_3, user_1_admin self, 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,9 +715,14 @@ 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(
app, user_1, user_2, user_3, sport_1_cycling, activity_cycling_user_2, self,
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(
@ -720,6 +730,7 @@ def test_it_gets_users_list_ordered_by_activities_count(
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,9 +757,14 @@ 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(
app, user_1, user_2, user_3, sport_1_cycling, activity_cycling_user_2, self,
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(
@ -756,6 +772,7 @@ def test_it_gets_users_list_ordered_by_activities_count_ascending(
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,9 +799,14 @@ 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(
app, user_1, user_2, user_3, sport_1_cycling, activity_cycling_user_2, self,
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(
@ -792,6 +814,7 @@ def test_it_gets_users_list_ordered_by_activities_count_descending(
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,9 +872,8 @@ 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(
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(
@ -857,6 +881,7 @@ def test_it_returns_empty_users_list_filtering_on_username(
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(
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',
@ -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(
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',
@ -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,9 +1073,8 @@ 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(
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(
@ -1055,6 +1082,7 @@ def test_it_returns_error_if_user_can_not_change_admin_rights(
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,9 +1118,8 @@ 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(
app, user_1, sport_1_cycling, gpx_file 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(
@ -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,9 +1150,8 @@ 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(
app, user_1, sport_1_cycling, gpx_file 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(
@ -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(
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(
@ -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(
app_no_config, app_config, user_1_admin, user_2, user_3 self, 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(
app_no_config, app_config, user_1_admin, user_2, user_3 self, 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