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

@ -11,178 +11,178 @@ 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:
client = app.test_client() def test_it_deletes_an_activity_with_gpx(
resp_login = client.post( self, app, user_1, sport_1_cycling, gpx_file
'/api/auth/login', ):
data=json.dumps(dict(email='test@test.com', password='12345678')), client = app.test_client()
content_type='application/json', resp_login = client.post(
) '/api/auth/login',
client.post( data=json.dumps(dict(email='test@test.com', password='12345678')),
'/api/activities', content_type='application/json',
data=dict( )
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'), client.post(
data='{"sport_id": 1}', '/api/activities',
), data=dict(
headers=dict( file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
content_type='multipart/form-data', data='{"sport_id": 1}',
Authorization='Bearer ' ),
+ json.loads(resp_login.data.decode())['auth_token'], headers=dict(
), content_type='multipart/form-data',
) Authorization='Bearer '
response = client.delete( + json.loads(resp_login.data.decode())['auth_token'],
'/api/activities/1', ),
headers=dict( )
Authorization='Bearer ' response = client.delete(
+ json.loads(resp_login.data.decode())['auth_token'] '/api/activities/1',
), headers=dict(
) Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert response.status_code == 204 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, 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'],
),
)
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_it_returns_404_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.delete(
'/api/activities/9999',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
def test_it_returns_500_when_deleting_an_activity_with_gpx_invalid_file(
self, app, user_1, sport_1_cycling, 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'],
),
)
gpx_filepath = get_gpx_filepath(1)
gpx_filepath = get_absolute_file_path(gpx_filepath)
os.remove(gpx_filepath)
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 == 500
assert 'error' in data['status']
assert (
'Error. Please try again or contact the administrator.'
in data['message']
)
def test_delete_an_activity_with_gpx_different_user( class TestDeleteActivityWithoutGpx:
app, user_1, user_2, sport_1_cycling, gpx_file 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( client = app.test_client()
'/api/auth/login', resp_login = client.post(
data=json.dumps(dict(email='test@test.com', password='12345678')), '/api/auth/login',
content_type='application/json', data=json.dumps(dict(email='test@test.com', password='12345678')),
) content_type='application/json',
client.post( )
'/api/activities', response = client.delete(
data=dict( '/api/activities/1',
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'), headers=dict(
data='{"sport_id": 1}', Authorization='Bearer '
), + json.loads(resp_login.data.decode())['auth_token']
headers=dict( ),
content_type='multipart/form-data', )
Authorization='Bearer ' assert response.status_code == 204
+ 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.delete(
'/api/activities/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode()) 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']
),
)
assert response.status_code == 403 data = json.loads(response.data.decode())
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']
assert response.status_code == 403
def test_delete_an_activity_wo_gpx( assert 'error' in data['status']
app, user_1, sport_1_cycling, activity_cycling_user_1 assert 'You do not have permissions.' in data['message']
):
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()
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/9999',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
def test_delete_an_activity_with_gpx_invalid_file(
app, user_1, sport_1_cycling, 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'],
),
)
gpx_filepath = get_gpx_filepath(1)
gpx_filepath = get_absolute_file_path(gpx_filepath)
os.remove(gpx_filepath)
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 == 500
assert 'error' in data['status']
assert (
'Error. Please try again or contact the administrator.'
in data['message']
)

View File

@ -1,53 +1,60 @@
def test_add_activity(app, sport_1_cycling, user_1, activity_cycling_user_1): class TestActivityModel:
activity_cycling_user_1.title = 'Test' def test_activity_model(
self, app, sport_1_cycling, user_1, activity_cycling_user_1
):
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(
assert 10.0 == float(activity_cycling_user_1.distance) activity_cycling_user_1.activity_date
assert '0:17:04' == str(activity_cycling_user_1.duration) )
assert 'Test' == activity_cycling_user_1.title assert 10.0 == float(activity_cycling_user_1.distance)
assert '<Activity \'Cycling\' - 2018-01-01 00:00:00>' == str( assert '1:00:00' == str(activity_cycling_user_1.duration)
activity_cycling_user_1 assert 'Test' == activity_cycling_user_1.title
) # noqa assert '<Activity \'Cycling\' - 2018-01-01 00:00:00>' == str(
activity_cycling_user_1
)
serialized_activity = activity_cycling_user_1.serialize() serialized_activity = activity_cycling_user_1.serialize()
assert 1 == serialized_activity['id'] assert 1 == serialized_activity['id']
assert 'test' == serialized_activity['user'] assert 'test' == serialized_activity['user']
assert 1 == serialized_activity['sport_id'] assert 1 == serialized_activity['sport_id']
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['pauses'] is None )
assert serialized_activity['moving'] == '0:17:04' assert serialized_activity['duration'] == '1:00:00'
assert serialized_activity['distance'] == 10.0 assert serialized_activity['pauses'] is None
assert serialized_activity['max_alt'] is None assert serialized_activity['moving'] == '1:00:00'
assert serialized_activity['descent'] is None assert serialized_activity['distance'] == 10.0
assert serialized_activity['ascent'] is None assert serialized_activity['max_alt'] is None
assert serialized_activity['max_speed'] == 10.0 assert serialized_activity['descent'] is None
assert serialized_activity['ave_speed'] == 10.0 assert serialized_activity['ascent'] is None
assert serialized_activity['with_gpx'] is False assert serialized_activity['max_speed'] == 10.0
assert serialized_activity['bounds'] == [] assert serialized_activity['ave_speed'] == 10.0
assert serialized_activity['previous_activity'] is None assert serialized_activity['with_gpx'] is False
assert serialized_activity['next_activity'] is None assert serialized_activity['bounds'] == []
assert serialized_activity['segments'] == [] assert serialized_activity['previous_activity'] is None
assert serialized_activity['records'] != [] assert serialized_activity['next_activity'] is None
assert serialized_activity['map'] is None assert serialized_activity['segments'] == []
assert serialized_activity['weather_start'] is None assert serialized_activity['records'] != []
assert serialized_activity['weather_end'] is None assert serialized_activity['map'] is None
assert serialized_activity['notes'] is None assert serialized_activity['weather_start'] is None
assert serialized_activity['weather_end'] is None
assert serialized_activity['notes'] is None
def test_activity_segment_model(
def test_add_segment( self,
app, app,
sport_1_cycling, sport_1_cycling,
user_1, user_1,
activity_cycling_user_1, activity_cycling_user_1,
activity_cycling_user_1_segment, activity_cycling_user_1_segment,
): ):
assert '<Segment \'0\' for activity \'1\'>' == str( assert '<Segment \'0\' for activity \'1\'>' == str(
activity_cycling_user_1_segment activity_cycling_user_1_segment
) # noqa )

View File

@ -1,212 +1,222 @@
import json import json
def test_get_config(app, user_1): class TestGetConfig:
client = app.test_client() def test_it_gets_application_config(self, app, user_1):
resp_login = client.post( client = app.test_client()
'/api/auth/login', resp_login = client.post(
data=json.dumps(dict(email='test@test.com', password='12345678')), '/api/auth/login',
content_type='application/json', data=json.dumps(dict(email='test@test.com', password='12345678')),
) content_type='application/json',
response = client.get( )
'/api/config',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 response = client.get(
assert 'success' in data['status'] '/api/config',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert data['data']['gpx_limit_import'] == 10 data = json.loads(response.data.decode())
assert data['data']['is_registration_enabled'] is True assert response.status_code == 200
assert data['data']['max_single_file_size'] == 1048576 assert 'success' in data['status']
assert data['data']['max_zip_file_size'] == 10485760 assert data['data']['gpx_limit_import'] == 10
assert data['data']['max_users'] == 100 assert data['data']['is_registration_enabled'] is True
assert data['data']['max_single_file_size'] == 1048576
assert data['data']['max_zip_file_size'] == 10485760
assert data['data']['max_users'] == 100
def test_it_returns_error_if_application_has_no_config(
self, app_no_config, user_1_admin
):
client = app_no_config.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.get(
'/api/config',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error on getting configuration.' in data['message']
def test_it_returns_error_if_application_has_several_config(
self, app, app_config, user_1_admin
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
)
response = client.get(
'/api/config',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error on getting configuration.' in data['message']
def test_get_config_no_config(app_no_config, user_1_admin): class TestUpdateConfig:
client = app_no_config.test_client() def test_it_updates_config_when_user_is_admin(self, app, user_1_admin):
resp_login = client.post( client = app.test_client()
'/api/auth/login', resp_login = client.post(
data=json.dumps(dict(email='admin@example.com', password='12345678')), '/api/auth/login',
content_type='application/json', data=json.dumps(
) dict(email='admin@example.com', password='12345678')
response = client.get( ),
'/api/config', content_type='application/json',
content_type='application/json', )
headers=dict( response = client.patch(
Authorization='Bearer ' '/api/config',
+ json.loads(resp_login.data.decode())['auth_token'] content_type='application/json',
), data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
) headers=dict(
data = json.loads(response.data.decode()) Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500 assert response.status_code == 200
assert 'error' in data['status'] assert 'success' in data['status']
assert 'Error on getting configuration.' in data['message'] assert data['data']['gpx_limit_import'] == 100
assert data['data']['is_registration_enabled'] is True
assert data['data']['max_single_file_size'] == 1048576
assert data['data']['max_zip_file_size'] == 10485760
assert data['data']['max_users'] == 10
def test_it_updates_all_config(self, app, user_1_admin):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
)
def test_get_config_several_config(app, app_config, user_1_admin): response = client.patch(
client = app.test_client() '/api/config',
resp_login = client.post( content_type='application/json',
'/api/auth/login', data=json.dumps(
data=json.dumps(dict(email='admin@example.com', password='12345678')), dict(
content_type='application/json', gpx_limit_import=20,
) max_single_file_size=10000,
response = client.get( max_zip_file_size=25000,
'/api/config', max_users=50,
content_type='application/json', )
headers=dict( ),
Authorization='Bearer ' headers=dict(
+ json.loads(resp_login.data.decode())['auth_token'] Authorization='Bearer '
), + json.loads(resp_login.data.decode())['auth_token']
) ),
data = json.loads(response.data.decode()) )
assert response.status_code == 500 data = json.loads(response.data.decode())
assert 'error' in data['status'] assert response.status_code == 200
assert 'Error on getting configuration.' in data['message'] assert 'success' in data['status']
assert data['data']['gpx_limit_import'] == 20
assert data['data']['is_registration_enabled'] is True
assert data['data']['max_single_file_size'] == 10000
assert data['data']['max_zip_file_size'] == 25000
assert data['data']['max_users'] == 50
def test_it_returns_403_when_user_is_not_an_admin(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',
)
def test_update_config_as_admin(app, user_1_admin): response = client.patch(
client = app.test_client() '/api/config',
resp_login = client.post( content_type='application/json',
'/api/auth/login', data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
data=json.dumps(dict(email='admin@example.com', password='12345678')), headers=dict(
content_type='application/json', Authorization='Bearer '
) + json.loads(resp_login.data.decode())['auth_token']
response = client.patch( ),
'/api/config', )
content_type='application/json',
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 data = json.loads(response.data.decode())
assert 'success' in data['status'] assert response.status_code == 403
assert data['data']['gpx_limit_import'] == 100 assert 'success' not in data['status']
assert data['data']['is_registration_enabled'] is True assert 'error' in data['status']
assert data['data']['max_single_file_size'] == 1048576 assert 'You do not have permissions.' in data['message']
assert data['data']['max_zip_file_size'] == 10485760
assert data['data']['max_users'] == 10
def test_it_returns_400_if_invalid_is_payload(self, app, user_1_admin):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
)
def test_update_full_config_as_admin(app, user_1_admin): response = client.patch(
client = app.test_client() '/api/config',
resp_login = client.post( content_type='application/json',
'/api/auth/login', data=json.dumps(dict()),
data=json.dumps(dict(email='admin@example.com', password='12345678')), headers=dict(
content_type='application/json', Authorization='Bearer '
) + json.loads(resp_login.data.decode())['auth_token']
response = client.patch( ),
'/api/config', )
content_type='application/json',
data=json.dumps(
dict(
gpx_limit_import=20,
max_single_file_size=10000,
max_zip_file_size=25000,
max_users=50,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 data = json.loads(response.data.decode())
assert 'success' in data['status'] assert response.status_code == 400
assert data['data']['gpx_limit_import'] == 20 assert 'error' in data['status']
assert data['data']['is_registration_enabled'] is True assert 'Invalid payload.' in data['message']
assert data['data']['max_single_file_size'] == 10000
assert data['data']['max_zip_file_size'] == 25000
assert data['data']['max_users'] == 50
def test_it_returns_error_on_update_if_application_has_no_config(
self, app_no_config, user_1_admin
):
client = app_no_config.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
)
def test_update_config_not_admin(app, user_1): response = client.patch(
client = app.test_client() '/api/config',
resp_login = client.post( content_type='application/json',
'/api/auth/login', data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
data=json.dumps(dict(email='test@test.com', password='12345678')), headers=dict(
content_type='application/json', Authorization='Bearer '
) + json.loads(resp_login.data.decode())['auth_token']
response = client.patch( ),
'/api/config', )
content_type='application/json',
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 403 data = json.loads(response.data.decode())
assert 'success' not in data['status'] assert response.status_code == 500
assert 'error' in data['status'] assert 'error' in data['status']
assert 'You do not have permissions.' in data['message'] assert 'Error on updating configuration.' in data['message']
def test_update_config_invalid_payload(app, user_1_admin):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')),
content_type='application/json',
)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict()),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'Invalid payload.' in data['message']
def test_update_config_no_config(app_no_config, user_1_admin):
client = app_no_config.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')),
content_type='application/json',
)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert '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,13 +1,14 @@
from fittrackee_api.application.models import AppConfig from fittrackee_api.application.models import AppConfig
def test_application_config(app): class TestConfigModel:
app_config = AppConfig.query.first() def test_application_config(self, app):
assert 1 == app_config.id app_config = AppConfig.query.first()
assert 1 == app_config.id
serialized_app_config = app_config.serialize() serialized_app_config = app_config.serialize()
assert serialized_app_config['gpx_limit_import'] == 10 assert serialized_app_config['gpx_limit_import'] == 10
assert serialized_app_config['is_registration_enabled'] is True assert serialized_app_config['is_registration_enabled'] is True
assert serialized_app_config['max_single_file_size'] == 1048576 assert serialized_app_config['max_single_file_size'] == 1048576
assert serialized_app_config['max_zip_file_size'] == 10485760 assert serialized_app_config['max_zip_file_size'] == 10485760
assert serialized_app_config['max_users'] == 100 assert serialized_app_config['max_users'] == 100

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,20 @@
import os import os
def test_development_config(app): class TestConfig:
app.config.from_object('fittrackee_api.config.DevelopmentConfig') def test_development_config(self, app):
assert app.config['DEBUG'] app.config.from_object('fittrackee_api.config.DevelopmentConfig')
assert not app.config['TESTING'] assert app.config['DEBUG']
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get( assert not app.config['TESTING']
'DATABASE_URL' assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
) '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'] assert not app.config['PRESERVE_CONTEXT_ON_EXCEPTION']
assert not app.config['PRESERVE_CONTEXT_ON_EXCEPTION'] assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get( 'DATABASE_TEST_URL'
'DATABASE_TEST_URL' )
)

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']

File diff suppressed because it is too large Load Diff

View File

@ -3,130 +3,133 @@ 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:
record_ld = Record.query.filter_by( def test_record_model(
user_id=activity_cycling_user_1.user_id, self, app, user_1, sport_1_cycling, activity_cycling_user_1
sport_id=activity_cycling_user_1.sport_id, ):
record_type='LD', record_ld = Record.query.filter_by(
).first() user_id=activity_cycling_user_1.user_id,
assert 'test' == record_ld.user.username sport_id=activity_cycling_user_1.sport_id,
assert 1 == record_ld.sport_id record_type='LD',
assert 1 == record_ld.activity_id ).first()
assert 'LD' == record_ld.record_type assert 'test' == record_ld.user.username
assert '2018-01-01 00:00:00' == str(record_ld.activity_date) assert 1 == record_ld.sport_id
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld) assert 1 == record_ld.activity_id
assert 'LD' == record_ld.record_type
assert '2018-01-01 00:00:00' == str(record_ld.activity_date)
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
record_serialize = record_ld.serialize() record_serialize = record_ld.serialize()
assert 'id' in record_serialize assert 'id' in record_serialize
assert 'user' in record_serialize assert 'user' in record_serialize
assert 'sport_id' in record_serialize assert 'sport_id' in record_serialize
assert 'activity_id' in record_serialize assert 'activity_id' in record_serialize
assert 'record_type' in record_serialize assert 'record_type' in record_serialize
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(
self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='LD',
).first()
record_ld.value = None
assert 'test' == record_ld.user.username
assert 1 == record_ld.sport_id
assert 1 == record_ld.activity_id
assert 'LD' == record_ld.record_type
assert '2018-01-01 00:00:00' == str(record_ld.activity_date)
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
assert record_ld.value is None
def test_record_model_none_value( record_serialize = record_ld.serialize()
app, user_1, sport_1_cycling, activity_cycling_user_1 assert record_serialize['value'] is None
):
record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='LD',
).first()
record_ld.value = None
assert 'test' == record_ld.user.username
assert 1 == record_ld.sport_id
assert 1 == record_ld.activity_id
assert 'LD' == record_ld.record_type
assert '2018-01-01 00:00:00' == str(record_ld.activity_date)
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
assert record_ld.value is None
record_serialize = record_ld.serialize() def test_average_speed_records(
assert record_serialize['value'] is None self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_as = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='AS',
).first()
assert isinstance(record_as.value, float)
assert record_as.value == 10.0
assert record_as._value == 1000
def test_add_as_records(app, user_1, sport_1_cycling, activity_cycling_user_1): record_serialize = record_as.serialize()
record_as = Record.query.filter_by( assert record_serialize.get('value') == 10.0
user_id=activity_cycling_user_1.user_id, assert isinstance(record_serialize.get('value'), float)
sport_id=activity_cycling_user_1.sport_id,
record_type='AS',
).first()
assert isinstance(record_as.value, float) def test_add_farest_distance_records(
assert record_as.value == 10.0 self, app, user_1, sport_1_cycling, activity_cycling_user_1
assert record_as._value == 1000 ):
record_fd = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='FD',
).first()
record_serialize = record_as.serialize() assert isinstance(record_fd.value, float)
assert record_serialize.get('value') == 10.0 assert record_fd.value == 10.0
assert isinstance(record_serialize.get('value'), float) assert record_fd._value == 10000
record_serialize = record_fd.serialize()
assert record_serialize.get('value') == 10.0
assert isinstance(record_serialize.get('value'), float)
def test_add_fd_records(app, user_1, sport_1_cycling, activity_cycling_user_1): def test_add_longest_duration_records(
record_fd = Record.query.filter_by( self, app, user_1, sport_1_cycling, activity_cycling_user_1
user_id=activity_cycling_user_1.user_id, ):
sport_id=activity_cycling_user_1.sport_id, record_ld = Record.query.filter_by(
record_type='FD', user_id=activity_cycling_user_1.user_id,
).first() sport_id=activity_cycling_user_1.sport_id,
record_type='LD',
).first()
assert isinstance(record_fd.value, float) assert isinstance(record_ld.value, datetime.timedelta)
assert record_fd.value == 10.0 assert str(record_ld.value) == '1:00:00'
assert record_fd._value == 10000 assert record_ld._value == 3600
record_serialize = record_fd.serialize() record_serialize = record_ld.serialize()
assert record_serialize.get('value') == 10.0 assert record_serialize.get('value') == '1:00:00'
assert isinstance(record_serialize.get('value'), float) assert isinstance(record_serialize.get('value'), str)
def test_add_longest_duration_records_with_zero(
self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='LD',
).first()
record_ld.value = datetime.timedelta(seconds=0)
def test_add_ld_records(app, user_1, sport_1_cycling, activity_cycling_user_1): assert isinstance(record_ld.value, datetime.timedelta)
record_ld = Record.query.filter_by( assert str(record_ld.value) == '0:00:00'
user_id=activity_cycling_user_1.user_id, assert record_ld._value == 0
sport_id=activity_cycling_user_1.sport_id,
record_type='LD',
).first()
assert isinstance(record_ld.value, datetime.timedelta) record_serialize = record_ld.serialize()
assert str(record_ld.value) == '0:17:04' assert record_serialize.get('value') == '0:00:00'
assert record_ld._value == 1024 assert isinstance(record_serialize.get('value'), str)
record_serialize = record_ld.serialize() def test_max_speed_records_no_value(
assert record_serialize.get('value') == '0:17:04' self, app, user_1, sport_1_cycling, activity_cycling_user_1
assert isinstance(record_serialize.get('value'), str) ):
record_ms = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='MS',
).first()
assert isinstance(record_ms.value, float)
assert record_ms.value == 10.0
assert record_ms._value == 1000
def test_add_ld_records_zero( record_serialize = record_ms.serialize()
app, user_1, sport_1_cycling, activity_cycling_user_1 assert record_serialize.get('value') == 10.0
): assert isinstance(record_serialize.get('value'), float)
record_ld = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='LD',
).first()
record_ld.value = datetime.timedelta(seconds=0)
assert isinstance(record_ld.value, datetime.timedelta)
assert str(record_ld.value) == '0:00:00'
assert record_ld._value == 0
record_serialize = record_ld.serialize()
assert record_serialize.get('value') == '0:00:00'
assert isinstance(record_serialize.get('value'), str)
def test_add_ms_records_no_value(
app, user_1, sport_1_cycling, activity_cycling_user_1
):
record_ms = Record.query.filter_by(
user_id=activity_cycling_user_1.user_id,
sport_id=activity_cycling_user_1.sport_id,
record_type='MS',
).first()
assert isinstance(record_ms.value, float)
assert record_ms.value == 10.0
assert record_ms._value == 1000
record_serialize = record_ms.serialize()
assert record_serialize.get('value') == 10.0
assert isinstance(record_serialize.get('value'), float)

View File

@ -30,339 +30,379 @@ 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:
client = app.test_client() def test_it_gets_all_sports(
resp_login = client.post( self, app, user_1, sport_1_cycling, sport_2_running
'/api/auth/login', ):
data=json.dumps(dict(email='test@test.com', password='12345678')), client = app.test_client()
content_type='application/json', resp_login = client.post(
) '/api/auth/login',
response = client.get( data=json.dumps(dict(email='test@test.com', password='12345678')),
'/api/sports', content_type='application/json',
headers=dict( )
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 response = client.get(
assert 'success' in data['status'] '/api/sports',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert len(data['data']['sports']) == 2 data = json.loads(response.data.decode())
assert data['data']['sports'][0] == expected_sport_1_cycling_result assert response.status_code == 200
assert data['data']['sports'][1] == expected_sport_2_running_result assert 'success' in data['status']
assert len(data['data']['sports']) == 2
assert data['data']['sports'][0] == expected_sport_1_cycling_result
assert data['data']['sports'][1] == expected_sport_2_running_result
def test_it_gets_all_sports_with_inactive_one(
self, app, user_1, sport_1_cycling_inactive, sport_2_running
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get(
'/api/sports',
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']['sports']) == 2
assert (
data['data']['sports'][0]
== expected_sport_1_cycling_inactive_result
)
assert data['data']['sports'][1] == expected_sport_2_running_result
def test_it_gets_all_sports_with_admin_rights(
self, app, user_1_admin, sport_1_cycling_inactive, sport_2_running
):
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.get(
'/api/sports',
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']['sports']) == 2
assert (
data['data']['sports'][0]
== expected_sport_1_cycling_inactive_admin_result
)
assert (
data['data']['sports'][1] == expected_sport_2_running_admin_result
)
def test_get_all_sports_with_inactive_one( class TestGetSport:
app, user_1, sport_1_cycling_inactive, sport_2_running 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(
'/api/sports',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 response = client.get(
assert 'success' in data['status'] '/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert len(data['data']['sports']) == 2 data = json.loads(response.data.decode())
assert ( assert response.status_code == 200
data['data']['sports'][0] == expected_sport_1_cycling_inactive_result assert 'success' in data['status']
) assert len(data['data']['sports']) == 1
assert data['data']['sports'][1] == expected_sport_2_running_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):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['sports']) == 0
def test_it_gets_a_inactive_sport(
self, app, user_1, sport_1_cycling_inactive
):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['sports']) == 1
assert (
data['data']['sports'][0]
== expected_sport_1_cycling_inactive_result
)
def test_it_get_an_inactive_sport_with_admin_rights(
self, app, user_1_admin, sport_1_cycling_inactive
):
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.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['sports']) == 1
assert (
data['data']['sports'][0]
== expected_sport_1_cycling_inactive_admin_result
)
def test_get_all_sports_admin( class TestUpdateSport:
app, user_1_admin, sport_1_cycling_inactive, sport_2_running 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(
data=json.dumps(dict(email='admin@example.com', password='12345678')), dict(email='admin@example.com', password='12345678')
content_type='application/json', ),
) content_type='application/json',
response = client.get( )
'/api/sports',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 response = client.patch(
assert 'success' in data['status'] '/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert len(data['data']['sports']) == 2 data = json.loads(response.data.decode())
assert ( assert response.status_code == 200
data['data']['sports'][0] assert 'success' in data['status']
== expected_sport_1_cycling_inactive_admin_result assert len(data['data']['sports']) == 1
) assert data['data']['sports'][0]['is_active'] is False
assert data['data']['sports'][1] == expected_sport_2_running_admin_result 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',
)
def test_get_a_sport(app, user_1, sport_1_cycling): response = client.patch(
client = app.test_client() '/api/sports/1',
resp_login = client.post( content_type='application/json',
'/api/auth/login', data=json.dumps(dict(is_active=True)),
data=json.dumps(dict(email='test@test.com', password='12345678')), headers=dict(
content_type='application/json', Authorization='Bearer '
) + json.loads(resp_login.data.decode())['auth_token']
response = client.get( ),
'/api/sports/1', )
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 data = json.loads(response.data.decode())
assert 'success' in data['status'] assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is True
assert data['data']['sports'][0]['has_activities'] is False
assert len(data['data']['sports']) == 1 def test_it_disables_a_sport_with_activities(
assert data['data']['sports'][0] == expected_sport_1_cycling_result self, app, user_1_admin, sport_1_cycling, activity_cycling_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',
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
def test_get_a_sport_invalid(app, user_1): data = json.loads(response.data.decode())
client = app.test_client() assert response.status_code == 200
resp_login = client.post( assert 'success' in data['status']
'/api/auth/login', assert len(data['data']['sports']) == 1
data=json.dumps(dict(email='test@test.com', password='12345678')), assert data['data']['sports'][0]['is_active'] is False
content_type='application/json', assert data['data']['sports'][0]['has_activities'] is True
)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404 def test_it_enables_a_sport_with_activities(
assert 'not found' in data['status'] self, app, user_1_admin, sport_1_cycling, activity_cycling_user_1
assert len(data['data']['sports']) == 0 ):
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(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
def test_get_a_inactive_sport(app, user_1, sport_1_cycling_inactive): data = json.loads(response.data.decode())
client = app.test_client() assert response.status_code == 200
resp_login = client.post( assert 'success' in data['status']
'/api/auth/login', assert len(data['data']['sports']) == 1
data=json.dumps(dict(email='test@test.com', password='12345678')), assert data['data']['sports'][0]['is_active'] is True
content_type='application/json', assert data['data']['sports'][0]['has_activities'] is True
)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 def test_returns_error_if_user_has_no_admin_rights(
assert 'success' in data['status'] self, app, user_1, sport_1_cycling
):
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(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert len(data['data']['sports']) == 1 assert response.status_code == 403
assert ( assert 'success' not in data['status']
data['data']['sports'][0] == expected_sport_1_cycling_inactive_result assert 'error' in data['status']
) assert 'You do not have permissions.' in data['message']
def test_returns_error_if_payload_is_invalid(self, app, user_1_admin):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
)
def test_get_a_inactive_sport_as_admin( response = client.patch(
app, user_1_admin, sport_1_cycling_inactive '/api/sports/1',
): content_type='application/json',
client = app.test_client() data=json.dumps(dict()),
resp_login = client.post( headers=dict(
'/api/auth/login', Authorization='Bearer '
data=json.dumps(dict(email='admin@example.com', password='12345678')), + json.loads(resp_login.data.decode())['auth_token']
content_type='application/json', ),
) )
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200 data = json.loads(response.data.decode())
assert 'success' in data['status'] assert response.status_code == 400
assert 'error' in data['status']
assert 'Invalid payload.' in data['message']
assert len(data['data']['sports']) == 1 def test_it_returns_error_if_sport_does_not_exist(self, app, user_1_admin):
assert ( client = app.test_client()
data['data']['sports'][0] resp_login = client.post(
== expected_sport_1_cycling_inactive_admin_result '/api/auth/login',
) data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
def test_update_a_sport(app, user_1_admin, sport_1_cycling): assert 'not found' in data['status']
client = app.test_client() assert len(data['data']['sports']) == 0
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')),
content_type='application/json',
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
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']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is False
assert data['data']['sports'][0]['has_activities'] is False
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=True)),
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']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is True
assert data['data']['sports'][0]['has_activities'] is False
def test_update_a_sport_with_activities(
app, user_1_admin, sport_1_cycling, activity_cycling_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',
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
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']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is False
assert data['data']['sports'][0]['has_activities'] is True
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=True)),
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']['sports']) == 1
assert data['data']['sports'][0]['is_active'] is True
assert data['data']['sports'][0]['has_activities'] is True
def test_update_a_sport_not_admin(app, user_1, sport_1_cycling):
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(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 403
assert 'success' not in data['status']
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']
def test_update_a_sport_invalid_payload(app, user_1_admin):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')),
content_type='application/json',
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict()),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'Invalid payload.' in data['message']
def test_update_a_sport_invalid_id(app, user_1_admin):
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='admin@example.com', password='12345678')),
content_type='application/json',
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['sports']) == 0

View File

@ -1,29 +1,28 @@
def assert_sport_model(sport, is_admin=False): class TestSportModel:
assert 1 == sport.id @staticmethod
assert 'Cycling' == sport.label def assert_sport_model(sport, is_admin=False):
assert '<Sport \'Cycling\'>' == str(sport) assert 1 == sport.id
assert 'Cycling' == sport.label
assert '<Sport \'Cycling\'>' == str(sport)
serialized_sport = sport.serialize(is_admin) serialized_sport = sport.serialize(is_admin)
assert 1 == serialized_sport['id'] assert 1 == serialized_sport['id']
assert 'Cycling' == serialized_sport['label'] assert 'Cycling' == serialized_sport['label']
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):
serialized_sport = self.assert_sport_model(sport_1_cycling)
assert 'has_activities' not in serialized_sport
def test_sport_model(app, sport_1_cycling): def test_sport_model_with_activity(
serialized_sport = assert_sport_model(sport_1_cycling) self, app, sport_1_cycling, user_1, activity_cycling_user_1
assert 'has_activities' not in serialized_sport ):
serialized_sport = self.assert_sport_model(sport_1_cycling)
assert 'has_activities' not in serialized_sport
def test_sport_model_with_activity_as_admin(
def test_sport_model_with_activity( self, app, sport_1_cycling, user_1, activity_cycling_user_1
app, sport_1_cycling, user_1, activity_cycling_user_1 ):
): serialized_sport = self.assert_sport_model(sport_1_cycling, True)
serialized_sport = assert_sport_model(sport_1_cycling) assert serialized_sport['has_activities'] is True
assert 'has_activities' not in serialized_sport
def test_sport_model_with_activity_admin(
app, sport_1_cycling, user_1, activity_cycling_user_1
):
serialized_sport = assert_sport_model(sport_1_cycling, True)
assert serialized_sport['has_activities'] is True

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,33 @@
def test_user_model(app, user_1): from fittrackee_api.users.models import User
assert '<User \'test\'>' == str(user_1)
serialized_user = user_1.serialize()
assert 'test' == serialized_user['username'] class TestUserModel:
assert 'created_at' in serialized_user def test_user_model(self, app, user_1):
assert serialized_user['admin'] is False assert '<User \'test\'>' == str(user_1)
assert serialized_user['first_name'] is None
assert serialized_user['last_name'] is None serialized_user = user_1.serialize()
assert serialized_user['bio'] is None assert 'test' == serialized_user['username']
assert serialized_user['location'] is None assert 'created_at' in serialized_user
assert serialized_user['birth_date'] is None assert serialized_user['admin'] is False
assert serialized_user['picture'] is False assert serialized_user['first_name'] is None
assert serialized_user['timezone'] is None assert serialized_user['last_name'] is None
assert serialized_user['weekm'] is False assert serialized_user['bio'] is None
assert serialized_user['language'] is None assert serialized_user['location'] is None
assert serialized_user['nb_activities'] == 0 assert serialized_user['birth_date'] is None
assert serialized_user['nb_sports'] == 0 assert serialized_user['picture'] is False
assert serialized_user['total_distance'] == 0 assert serialized_user['timezone'] is None
assert serialized_user['total_duration'] == '0:00:00' assert serialized_user['weekm'] is False
assert serialized_user['language'] is None
assert serialized_user['nb_activities'] == 0
assert serialized_user['nb_sports'] == 0
assert serialized_user['total_distance'] == 0
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