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>
<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.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>Provide a valid auth token.</p></li>
<li><p>Signature expired. Please log in again.</p></li>
<li><p>Invalid token. Please log in again.</p></li>
</ul>
</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>
</ul>
</dd>
@ -485,14 +488,17 @@
<dt class="field-odd">Status Codes</dt>
<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.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>Provide a valid auth token.</p></li>
<li><p>Signature expired. Please log in again.</p></li>
<li><p>Invalid token. Please log in again.</p></li>
</ul>
</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>
</ul>
</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
if not activity.gpx or activity.gpx == '':
message = f'No gpx file for this activity (id: {activity_id})'
response_object = {'status': 'fail', 'message': message}
return jsonify(response_object), 400
response_object = {'status': 'error', 'message': message}
return jsonify(response_object), 404
try:
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
:statuscode 200: success
:statuscode 400: no gpx file for this activity
:statuscode 401:
- Provide a valid auth token.
- Signature expired. 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:
"""
@ -537,12 +538,13 @@ def get_activity_chart_data(auth_user_id, activity_id):
:reqheader Authorization: OAuth 2.0 Bearer Token
:statuscode 200: success
:statuscode 400: no gpx file for this activity
:statuscode 401:
- Provide a valid auth token.
- Signature expired. 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:
"""
@ -703,7 +705,7 @@ def get_map(map_id):
activity = Activity.query.filter_by(map_id=map_id).first()
if not activity:
response_object = {
'status': 'fail',
'status': 'error',
'message': 'Map does not exist',
}
return jsonify(response_object), 404

View File

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

View File

@ -11,178 +11,178 @@ def get_gpx_filepath(activity_id):
return activity.gpx
def test_delete_an_activity_with_gpx(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'],
),
)
response = client.delete(
'/api/activities/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
class TestDeleteActivityWithGpx:
def test_it_deletes_an_activity_with_gpx(
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'],
),
)
response = client.delete(
'/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(
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']
),
)
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
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
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']
data = json.loads(response.data.decode())
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()
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']
)
assert response.status_code == 403
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']

View File

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

View File

@ -1,212 +1,222 @@
import json
def test_get_config(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/config',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
class TestGetConfig:
def test_it_gets_application_config(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',
)
assert response.status_code == 200
assert 'success' in data['status']
response = client.get(
'/api/config',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert data['data']['gpx_limit_import'] == 10
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
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert data['data']['gpx_limit_import'] == 10
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):
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())
class TestUpdateConfig:
def test_it_updates_config_when_user_is_admin(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',
)
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 getting configuration.' in data['message']
assert response.status_code == 200
assert 'success' in data['status']
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):
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())
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']
),
)
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error on getting configuration.' in data['message']
data = json.loads(response.data.decode())
assert response.status_code == 200
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):
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(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())
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']
),
)
assert response.status_code == 200
assert 'success' in data['status']
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
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_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):
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(
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())
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']
),
)
assert response.status_code == 200
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
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'Invalid payload.' in data['message']
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):
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/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())
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']
),
)
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_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']
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error on updating configuration.' in data['message']

View File

@ -1,13 +1,14 @@
from fittrackee_api.application.models import AppConfig
def test_application_config(app):
app_config = AppConfig.query.first()
assert 1 == app_config.id
class TestConfigModel:
def test_application_config(self, app):
app_config = AppConfig.query.first()
assert 1 == app_config.id
serialized_app_config = app_config.serialize()
assert serialized_app_config['gpx_limit_import'] == 10
assert serialized_app_config['is_registration_enabled'] is True
assert serialized_app_config['max_single_file_size'] == 1048576
assert serialized_app_config['max_zip_file_size'] == 10485760
assert serialized_app_config['max_users'] == 100
serialized_app_config = app_config.serialize()
assert serialized_app_config['gpx_limit_import'] == 10
assert serialized_app_config['is_registration_enabled'] is True
assert serialized_app_config['max_single_file_size'] == 1048576
assert serialized_app_config['max_zip_file_size'] == 10485760
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
def test_development_config(app):
app.config.from_object('fittrackee_api.config.DevelopmentConfig')
assert app.config['DEBUG']
assert not app.config['TESTING']
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
'DATABASE_URL'
)
class TestConfig:
def test_development_config(self, app):
app.config.from_object('fittrackee_api.config.DevelopmentConfig')
assert app.config['DEBUG']
assert not app.config['TESTING']
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
'DATABASE_URL'
)
def test_testing_config(app):
app.config.from_object('fittrackee_api.config.TestingConfig')
assert app.config['DEBUG']
assert app.config['TESTING']
assert not app.config['PRESERVE_CONTEXT_ON_EXCEPTION']
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
'DATABASE_TEST_URL'
)
def test_testing_config(self, app):
app.config.from_object('fittrackee_api.config.TestingConfig')
assert app.config['DEBUG']
assert app.config['TESTING']
assert not app.config['PRESERVE_CONTEXT_ON_EXCEPTION']
assert app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get(
'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
def test_record_model(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()
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)
class TestRecordModel:
def test_record_model(
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()
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)
record_serialize = record_ld.serialize()
assert 'id' in record_serialize
assert 'user' in record_serialize
assert 'sport_id' in record_serialize
assert 'activity_id' in record_serialize
assert 'record_type' in record_serialize
assert 'activity_date' in record_serialize
assert 'value' in record_serialize
record_serialize = record_ld.serialize()
assert 'id' in record_serialize
assert 'user' in record_serialize
assert 'sport_id' in record_serialize
assert 'activity_id' in record_serialize
assert 'record_type' in record_serialize
assert 'activity_date' 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(
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
record_serialize = record_ld.serialize()
assert record_serialize['value'] is None
record_serialize = record_ld.serialize()
assert record_serialize['value'] is None
def test_average_speed_records(
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_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()
record_serialize = record_as.serialize()
assert record_serialize.get('value') == 10.0
assert isinstance(record_serialize.get('value'), float)
assert isinstance(record_as.value, float)
assert record_as.value == 10.0
assert record_as._value == 1000
def test_add_farest_distance_records(
self, app, user_1, sport_1_cycling, activity_cycling_user_1
):
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 record_serialize.get('value') == 10.0
assert isinstance(record_serialize.get('value'), float)
assert isinstance(record_fd.value, float)
assert record_fd.value == 10.0
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):
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()
def test_add_longest_duration_records(
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()
assert isinstance(record_fd.value, float)
assert record_fd.value == 10.0
assert record_fd._value == 10000
assert isinstance(record_ld.value, datetime.timedelta)
assert str(record_ld.value) == '1:00:00'
assert record_ld._value == 3600
record_serialize = record_fd.serialize()
assert record_serialize.get('value') == 10.0
assert isinstance(record_serialize.get('value'), float)
record_serialize = record_ld.serialize()
assert record_serialize.get('value') == '1:00:00'
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):
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()
assert isinstance(record_ld.value, datetime.timedelta)
assert str(record_ld.value) == '0:00:00'
assert record_ld._value == 0
assert isinstance(record_ld.value, datetime.timedelta)
assert str(record_ld.value) == '0:17:04'
assert record_ld._value == 1024
record_serialize = record_ld.serialize()
assert record_serialize.get('value') == '0:00:00'
assert isinstance(record_serialize.get('value'), str)
record_serialize = record_ld.serialize()
assert record_serialize.get('value') == '0:17:04'
assert isinstance(record_serialize.get('value'), str)
def test_max_speed_records_no_value(
self, 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
def test_add_ld_records_zero(
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)
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)
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
def test_get_all_sports(app, user_1, sport_1_cycling, 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())
class TestGetSports:
def test_it_gets_all_sports(
self, app, user_1, sport_1_cycling, 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',
)
assert response.status_code == 200
assert 'success' in data['status']
response = client.get(
'/api/sports',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
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
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_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(
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())
class TestGetSport:
def test_it_gets_a_sport(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',
)
assert response.status_code == 200
assert 'success' in data['status']
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
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
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_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(
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())
class TestUpdateSport:
def test_it_disables_a_sport(self, app, user_1_admin, sport_1_cycling):
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',
)
assert response.status_code == 200
assert 'success' in data['status']
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']
),
)
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
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
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):
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())
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']
),
)
assert response.status_code == 200
assert 'success' in data['status']
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
assert len(data['data']['sports']) == 1
assert data['data']['sports'][0] == expected_sport_1_cycling_result
def test_it_disables_a_sport_with_activities(
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):
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())
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
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['sports']) == 0
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(
'/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):
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())
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
assert response.status_code == 200
assert 'success' in data['status']
def test_returns_error_if_user_has_no_admin_rights(
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 (
data['data']['sports'][0] == expected_sport_1_cycling_inactive_result
)
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_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(
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())
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']
),
)
assert response.status_code == 200
assert 'success' in data['status']
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'Invalid payload.' in data['message']
assert len(data['data']['sports']) == 1
assert (
data['data']['sports'][0]
== expected_sport_1_cycling_inactive_admin_result
)
def test_it_returns_error_if_sport_does_not_exist(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',
)
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())
def test_update_a_sport(app, user_1_admin, sport_1_cycling):
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 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
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):
assert 1 == sport.id
assert 'Cycling' == sport.label
assert '<Sport \'Cycling\'>' == str(sport)
class TestSportModel:
@staticmethod
def assert_sport_model(sport, is_admin=False):
assert 1 == sport.id
assert 'Cycling' == sport.label
assert '<Sport \'Cycling\'>' == str(sport)
serialized_sport = sport.serialize(is_admin)
assert 1 == serialized_sport['id']
assert 'Cycling' == serialized_sport['label']
assert serialized_sport['is_active'] is True
return serialized_sport
serialized_sport = sport.serialize(is_admin)
assert 1 == serialized_sport['id']
assert 'Cycling' == serialized_sport['label']
assert serialized_sport['is_active'] is True
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):
serialized_sport = assert_sport_model(sport_1_cycling)
assert 'has_activities' not in serialized_sport
def test_sport_model_with_activity(
self, app, sport_1_cycling, user_1, activity_cycling_user_1
):
serialized_sport = self.assert_sport_model(sport_1_cycling)
assert 'has_activities' not in serialized_sport
def test_sport_model_with_activity(
app, sport_1_cycling, user_1, activity_cycling_user_1
):
serialized_sport = assert_sport_model(sport_1_cycling)
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
def test_sport_model_with_activity_as_admin(
self, app, sport_1_cycling, user_1, activity_cycling_user_1
):
serialized_sport = self.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):
assert '<User \'test\'>' == str(user_1)
from fittrackee_api.users.models import User
serialized_user = user_1.serialize()
assert 'test' == serialized_user['username']
assert 'created_at' in serialized_user
assert serialized_user['admin'] is False
assert serialized_user['first_name'] is None
assert serialized_user['last_name'] is None
assert serialized_user['bio'] is None
assert serialized_user['location'] is None
assert serialized_user['birth_date'] is None
assert serialized_user['picture'] is False
assert serialized_user['timezone'] is None
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'
class TestUserModel:
def test_user_model(self, app, user_1):
assert '<User \'test\'>' == str(user_1)
serialized_user = user_1.serialize()
assert 'test' == serialized_user['username']
assert 'created_at' in serialized_user
assert serialized_user['admin'] is False
assert serialized_user['first_name'] is None
assert serialized_user['last_name'] is None
assert serialized_user['bio'] is None
assert serialized_user['location'] is None
assert serialized_user['birth_date'] is None
assert serialized_user['picture'] is False
assert serialized_user['timezone'] is None
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