API - test refacto (response errors assertion)

This commit is contained in:
Sam 2022-03-13 08:36:49 +01:00
parent 1245a64e18
commit 2de9cb1857
12 changed files with 281 additions and 552 deletions

View File

@ -1,8 +1,11 @@
import json import json
from typing import Any, Tuple from typing import Any, Dict, Optional, Tuple
from flask import Flask from flask import Flask
from flask.testing import FlaskClient from flask.testing import FlaskClient
from werkzeug.test import TestResponse
from .custom_asserts import assert_errored_response
class ApiTestCaseMixin: class ApiTestCaseMixin:
@ -29,6 +32,74 @@ class ApiTestCaseMixin:
auth_token = json.loads(resp_login.data.decode())['auth_token'] auth_token = json.loads(resp_login.data.decode())['auth_token']
return client, auth_token return client, auth_token
@staticmethod
def assert_400(
response: TestResponse,
error_message: Optional[str] = 'invalid payload',
status: Optional[str] = 'error',
) -> Dict:
return assert_errored_response(
response, 400, error_message=error_message, status=status
)
@staticmethod
def assert_401(response: TestResponse, error_message: str) -> Dict:
return assert_errored_response(
response, 401, error_message=error_message
)
@staticmethod
def assert_403(
response: TestResponse,
error_message: Optional[str] = 'you do not have permissions',
) -> Dict:
return assert_errored_response(response, 403, error_message)
@staticmethod
def assert_404(response: TestResponse) -> Dict:
return assert_errored_response(response, 404, status='not found')
@staticmethod
def assert_404_with_entity(response: TestResponse, entity: str) -> Dict:
error_message = f'{entity} does not exist'
return assert_errored_response(
response, 404, error_message=error_message, status='not found'
)
@staticmethod
def assert_404_with_message(
response: TestResponse, error_message: str
) -> Dict:
return assert_errored_response(
response, 404, error_message=error_message, status='not found'
)
@staticmethod
def assert_413(
response: TestResponse,
error_message: Optional[str] = None,
match: Optional[str] = None,
) -> Dict:
return assert_errored_response(
response,
413,
error_message=error_message,
status='fail',
match=match,
)
@staticmethod
def assert_500(
response: TestResponse,
error_message: Optional[str] = (
'error, please try again or contact the administrator'
),
status: Optional[str] = 'error',
) -> Dict:
return assert_errored_response(
response, 500, error_message=error_message, status=status
)
class CallArgsMixin: class CallArgsMixin:
@staticmethod @staticmethod

View File

@ -49,10 +49,7 @@ class TestGetConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response, 'error on getting configuration')
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( def test_it_returns_error_if_application_has_several_config(
self, app: Flask, app_config: Flask, user_1_admin: User self, app: Flask, app_config: Flask, user_1_admin: User
@ -67,10 +64,7 @@ class TestGetConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response, 'error on getting configuration')
assert response.status_code == 500
assert 'error' in data['status']
assert 'error on getting configuration' in data['message']
class TestUpdateConfig(ApiTestCaseMixin): class TestUpdateConfig(ApiTestCaseMixin):
@ -140,11 +134,7 @@ class TestUpdateConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
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( def test_it_returns_400_if_invalid_is_payload(
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
@ -160,10 +150,7 @@ class TestUpdateConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
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( def test_it_returns_error_on_update_if_application_has_no_config(
self, app_no_config: Flask, user_1_admin: User self, app_no_config: Flask, user_1_admin: User
@ -179,10 +166,7 @@ class TestUpdateConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response, 'error when updating configuration')
assert response.status_code == 500
assert 'error' in data['status']
assert 'error when updating configuration' in data['message']
def test_it_raises_error_if_archive_max_size_is_below_files_max_size( def test_it_raises_error_if_archive_max_size_is_below_files_max_size(
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
@ -205,13 +189,13 @@ class TestUpdateConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(
assert response.status_code == 400 response,
assert 'error' in data['status'] (
assert ( 'Max. size of zip archive must be equal or greater than max.'
'Max. size of zip archive must be equal or greater than max. size ' ' size of uploaded files'
'of uploaded files' ),
) in data['message'] )
def test_it_raises_error_if_archive_max_size_equals_0( def test_it_raises_error_if_archive_max_size_equals_0(
self, app_with_max_file_size_equals_0: Flask, user_1_admin: User self, app_with_max_file_size_equals_0: Flask, user_1_admin: User
@ -231,12 +215,8 @@ class TestUpdateConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(
assert response.status_code == 400 response, 'Max. size of zip archive must be greater than 0'
assert 'error' in data['status']
assert (
'Max. size of zip archive must be greater than 0'
in data['message']
) )
def test_it_raises_error_if_files_max_size_equals_0( def test_it_raises_error_if_files_max_size_equals_0(
@ -257,12 +237,8 @@ class TestUpdateConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(
assert response.status_code == 400 response, 'Max. size of uploaded files must be greater than 0'
assert 'error' in data['status']
assert (
'Max. size of uploaded files must be greater than 0'
in data['message']
) )
def test_it_raises_error_if_gpx_limit_import_equals_0( def test_it_raises_error_if_gpx_limit_import_equals_0(
@ -283,10 +259,6 @@ class TestUpdateConfig(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(
assert response.status_code == 400 response, 'Max. files in a zip archive must be greater than 0'
assert 'error' in data['status']
assert (
'Max. files in a zip archive must be greater than 0'
in data['message']
) )

View File

@ -1,5 +1,8 @@
import os import os
import pytest
from werkzeug.test import TestResponse
os.environ['FLASK_ENV'] = 'testing' os.environ['FLASK_ENV'] = 'testing'
os.environ['APP_SETTINGS'] = 'fittrackee.config.TestingConfig' os.environ['APP_SETTINGS'] = 'fittrackee.config.TestingConfig'
# to avoid resetting dev database during tests # to avoid resetting dev database during tests
@ -10,3 +13,8 @@ pytest_plugins = [
'fittrackee.tests.fixtures.fixtures_workouts', 'fittrackee.tests.fixtures.fixtures_workouts',
'fittrackee.tests.fixtures.fixtures_users', 'fittrackee.tests.fixtures.fixtures_users',
] ]
pytest.register_assert_rewrite('fittrackee.tests.custom_asserts')
# Prevent pytest from collecting TestResponse as test
TestResponse.__test__ = False # type: ignore

View File

@ -0,0 +1,24 @@
import json
import re
from typing import Dict, Optional
from werkzeug.test import TestResponse
def assert_errored_response(
response: TestResponse,
status_code: int,
error_message: Optional[str] = None,
status: Optional[str] = 'error',
match: Optional[str] = None,
) -> Dict:
assert response.content_type == 'application/json'
assert response.status_code == status_code
data = json.loads(response.data.decode())
assert status in data['status']
if error_message is not None:
assert error_message in data['message']
if match is not None:
assert re.match(match, data['message'])
return data

View File

@ -14,7 +14,7 @@ from fittrackee.workouts.models import Sport, Workout
from ..api_test_case import ApiTestCaseMixin from ..api_test_case import ApiTestCaseMixin
class TestUserRegistration: class TestUserRegistration(ApiTestCaseMixin):
def test_user_can_register(self, app: Flask) -> None: def test_user_can_register(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -58,11 +58,8 @@ class TestUserRegistration:
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode())
assert data['status'] == 'error' self.assert_400(response, 'sorry, that user already exists')
assert data['message'] == 'sorry, that user already exists'
assert response.content_type == 'application/json'
assert response.status_code == 400
@pytest.mark.parametrize( @pytest.mark.parametrize(
'input_email', 'input_email',
@ -84,11 +81,8 @@ class TestUserRegistration:
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode())
assert data['status'] == 'error' self.assert_400(response, 'sorry, that user already exists')
assert data['message'] == 'sorry, that user already exists'
assert response.content_type == 'application/json'
assert response.status_code == 400
def test_it_returns_error_if_username_is_too_short( def test_it_returns_error_if_username_is_too_short(
self, app: Flask self, app: Flask
@ -108,11 +102,7 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_400(response, "username: 3 to 12 characters required\n")
assert data['status'] == 'error'
assert data['message'] == "username: 3 to 12 characters required\n"
assert response.content_type == 'application/json'
assert response.status_code == 400
def test_it_returns_error_if_username_is_too_long( def test_it_returns_error_if_username_is_too_long(
self, app: Flask self, app: Flask
@ -130,11 +120,8 @@ class TestUserRegistration:
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode())
assert data['status'] == 'error' self.assert_400(response, "username: 3 to 12 characters required\n")
assert data['message'] == "username: 3 to 12 characters required\n"
assert response.content_type == 'application/json'
assert response.status_code == 400
def test_it_returns_error_if_email_is_invalid(self, app: Flask) -> None: def test_it_returns_error_if_email_is_invalid(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -152,11 +139,7 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_400(response, "email: valid email must be provided\n")
assert data['status'] == 'error'
assert data['message'] == "email: valid email must be provided\n"
assert response.content_type == 'application/json'
assert response.status_code == 400
def test_it_returns_error_if_password_is_too_short( def test_it_returns_error_if_password_is_too_short(
self, app: Flask self, app: Flask
@ -176,11 +159,7 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_400(response, "password: 8 characters required\n")
assert data['status'] == 'error'
assert data['message'] == "password: 8 characters required\n"
assert response.content_type == 'application/json'
assert response.status_code == 400
def test_it_returns_error_if_passwords_mismatch(self, app: Flask) -> None: def test_it_returns_error_if_passwords_mismatch(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -198,14 +177,10 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_400(
assert data['status'] == 'error' response,
assert ( "password: password and password confirmation do not match\n",
data['message']
== "password: password and password confirmation do not match\n"
) )
assert response.content_type == 'application/json'
assert response.status_code == 400
def test_it_returns_error_if_payload_is_invalid(self, app: Flask) -> None: def test_it_returns_error_if_payload_is_invalid(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -234,10 +209,7 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'invalid payload' in data['message']
assert 'error' in data['status']
def test_it_returns_error_if_email_is_missing(self, app: Flask) -> None: def test_it_returns_error_if_email_is_missing(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -254,10 +226,7 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'invalid payload' in data['message']
assert 'error' in data['status']
def test_it_returns_error_if_password_is_missing(self, app: Flask) -> None: def test_it_returns_error_if_password_is_missing(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -274,10 +243,7 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'invalid payload', data['message']
assert 'error', data['status']
def test_it_returns_error_if_password_confirmation_is_missing( def test_it_returns_error_if_password_confirmation_is_missing(
self, app: Flask self, app: Flask
@ -292,10 +258,8 @@ class TestUserRegistration:
), ),
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode())
assert response.status_code == 400 self.assert_400(response)
assert 'invalid payload' in data['message']
assert 'error' in data['status']
def test_it_returns_error_if_username_is_invalid(self, app: Flask) -> None: def test_it_returns_error_if_username_is_invalid(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -313,16 +277,10 @@ class TestUserRegistration:
content_type='application/json', content_type='application/json',
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert (
'error, please try again or contact the administrator'
in data['message']
)
assert 'error' in data['status']
class TestUserLogin: class TestUserLogin(ApiTestCaseMixin):
@pytest.mark.parametrize( @pytest.mark.parametrize(
'input_email', 'input_email',
['test@test.com', 'TEST@TEST.COM'], ['test@test.com', 'TEST@TEST.COM'],
@ -378,11 +336,7 @@ class TestUserLogin:
content_type='application/json', content_type='application/json',
) )
assert response.content_type == 'application/json' self.assert_401(response, 'invalid credentials')
assert response.status_code == 401
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid credentials'
def test_it_returns_error_on_invalid_payload(self, app: Flask) -> None: def test_it_returns_error_on_invalid_payload(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -393,11 +347,7 @@ class TestUserLogin:
content_type='application/json', content_type='application/json',
) )
assert response.content_type == 'application/json' self.assert_400(response)
assert response.status_code == 400
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
def test_it_returns_error_if_password_is_invalid( def test_it_returns_error_if_password_is_invalid(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -410,11 +360,7 @@ class TestUserLogin:
content_type='application/json', content_type='application/json',
) )
assert response.content_type == 'application/json' self.assert_401(response, 'invalid credentials')
assert response.status_code == 401
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid credentials'
class TestUserLogout(ApiTestCaseMixin): class TestUserLogout(ApiTestCaseMixin):
@ -441,34 +387,30 @@ class TestUserLogout(ApiTestCaseMixin):
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email app, user_1.email
) )
with freeze_time(now + timedelta(seconds=4)): with freeze_time(now + timedelta(seconds=4)):
response = client.get( response = client.get(
'/api/auth/logout', '/api/auth/logout',
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode())
assert data['status'] == 'error' self.assert_401(response, 'signature expired, please log in again')
assert data['message'] == 'signature expired, please log in again'
assert response.status_code == 401
def test_it_returns_error_with_invalid_token(self, app: Flask) -> None: def test_it_returns_error_with_invalid_token(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
response = client.get( response = client.get(
'/api/auth/logout', headers=dict(Authorization='Bearer invalid') '/api/auth/logout', headers=dict(Authorization='Bearer invalid')
) )
data = json.loads(response.data.decode())
assert data['status'] == 'error' self.assert_401(response, 'invalid token, please log in again')
assert data['message'] == 'invalid token, please log in again'
assert response.status_code == 401
def test_it_returns_error_with_invalid_headers(self, app: Flask) -> None: def test_it_returns_error_with_invalid_headers(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
response = client.get('/api/auth/logout', headers=dict()) response = client.get('/api/auth/logout', headers=dict())
data = json.loads(response.data.decode())
assert data['status'] == 'error' self.assert_401(response, 'provide a valid auth token')
assert data['message'] == 'provide a valid auth token'
assert response.status_code == 401
class TestUserProfile(ApiTestCaseMixin): class TestUserProfile(ApiTestCaseMixin):
@ -576,13 +518,12 @@ class TestUserProfile(ApiTestCaseMixin):
def test_it_returns_error_if_headers_are_invalid(self, app: Flask) -> None: def test_it_returns_error_if_headers_are_invalid(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
response = client.get( response = client.get(
'/api/auth/profile', headers=dict(Authorization='Bearer invalid') '/api/auth/profile', headers=dict(Authorization='Bearer invalid')
) )
data = json.loads(response.data.decode())
assert data['status'] == 'error' self.assert_401(response, 'invalid token, please log in again')
assert data['message'] == 'invalid token, please log in again'
assert response.status_code == 401
class TestUserProfileUpdate(ApiTestCaseMixin): class TestUserProfileUpdate(ApiTestCaseMixin):
@ -692,10 +633,7 @@ class TestUserProfileUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
assert response.status_code == 400
def test_it_returns_error_if_payload_is_empty( def test_it_returns_error_if_payload_is_empty(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -711,10 +649,7 @@ class TestUserProfileUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'invalid payload' in data['message']
assert 'error' in data['status']
def test_it_returns_error_if_passwords_mismatch( def test_it_returns_error_if_passwords_mismatch(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -740,13 +675,10 @@ class TestUserProfileUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(
assert data['status'] == 'error' response,
assert ( 'password: password and password confirmation do not match\n',
data['message']
== 'password: password and password confirmation do not match\n'
) )
assert response.status_code == 400
def test_it_returns_error_if_password_confirmation_is_missing( def test_it_returns_error_if_password_confirmation_is_missing(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -771,13 +703,10 @@ class TestUserProfileUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(
assert data['status'] == 'error' response,
assert ( 'password: password and password confirmation do not match\n',
data['message']
== 'password: password and password confirmation do not match\n'
) )
assert response.status_code == 400
class TestUserPreferencesUpdate(ApiTestCaseMixin): class TestUserPreferencesUpdate(ApiTestCaseMixin):
@ -840,10 +769,7 @@ class TestUserPreferencesUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
assert response.status_code == 400
def test_it_returns_error_if_payload_is_empty( def test_it_returns_error_if_payload_is_empty(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -859,10 +785,7 @@ class TestUserPreferencesUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'invalid payload' in data['message']
assert 'error' in data['status']
class TestUserSportPreferencesUpdate(ApiTestCaseMixin): class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
@ -880,10 +803,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'invalid payload' in data['message']
assert 'error' in data['status']
def test_it_returns_error_if_sport_id_is_missing( def test_it_returns_error_if_sport_id_is_missing(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -899,10 +819,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
assert response.status_code == 400
def test_it_returns_error_if_sport_not_found( def test_it_returns_error_if_sport_not_found(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -918,10 +835,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_entity(response, 'sport')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'sport does not exist' in data['message']
def test_it_returns_error_if_payload_contains_only_sport_id( def test_it_returns_error_if_payload_contains_only_sport_id(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -937,10 +851,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
assert response.status_code == 400
def test_it_returns_error_if_color_is_invalid( def test_it_returns_error_if_color_is_invalid(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -961,10 +872,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response, 'invalid hexadecimal color')
assert data['status'] == 'error'
assert data['message'] == 'invalid hexadecimal color'
assert response.status_code == 400
@pytest.mark.parametrize( @pytest.mark.parametrize(
'input_color', 'input_color',
@ -1075,10 +983,7 @@ class TestUserSportPreferencesReset(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
assert response.status_code == 404 self.assert_404_with_entity(response, 'sport')
data = json.loads(response.data.decode())
assert 'not found' in data['status']
assert 'sport does not exist' in data['message']
def test_it_resets_sport_preferences( def test_it_resets_sport_preferences(
self, self,
@ -1172,10 +1077,7 @@ class TestUserPicture(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_400(response, 'no file part', 'fail')
assert data['status'] == 'fail'
assert data['message'] == 'no file part'
assert response.status_code == 400
def test_it_returns_error_if_file_is_invalid( def test_it_returns_error_if_file_is_invalid(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -1193,10 +1095,7 @@ class TestUserPicture(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_400(response, 'file extension not allowed', 'fail')
assert data['status'] == 'fail'
assert data['message'] == 'file extension not allowed'
assert response.status_code == 400
def test_it_returns_error_if_image_size_exceeds_file_limit( def test_it_returns_error_if_image_size_exceeds_file_limit(
self, self,
@ -1220,12 +1119,9 @@ class TestUserPicture(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) data = self.assert_413(
assert response.status_code == 413 response,
assert 'fail' in data['status'] 'Error during picture upload, file size (1.2KB) exceeds 1.0KB.',
assert (
'Error during picture upload, file size (1.2KB) exceeds 1.0KB.'
in data['message']
) )
assert 'data' not in data assert 'data' not in data
@ -1251,17 +1147,14 @@ class TestUserPicture(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) data = self.assert_413(
assert response.status_code == 413 response,
assert 'fail' in data['status'] 'Error during picture upload, file size (1.2KB) exceeds 1.0KB.',
assert (
'Error during picture upload, file size (1.2KB) exceeds 1.0KB.'
in data['message']
) )
assert 'data' not in data assert 'data' not in data
class TestRegistrationConfiguration: class TestRegistrationConfiguration(ApiTestCaseMixin):
def test_it_returns_error_if_it_exceeds_max_users( def test_it_returns_error_if_it_exceeds_max_users(
self, self,
app_with_3_users_max: Flask, app_with_3_users_max: Flask,
@ -1284,11 +1177,7 @@ class TestRegistrationConfiguration:
content_type='application/json', content_type='application/json',
) )
assert response.content_type == 'application/json' self.assert_403(response, 'error, registration is disabled')
assert response.status_code == 403
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'error, registration is disabled'
def test_it_disables_registration_on_user_registration( def test_it_disables_registration_on_user_registration(
self, self,
@ -1323,10 +1212,7 @@ class TestRegistrationConfiguration:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 403 self.assert_403(response, 'error, registration is disabled')
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'error, registration is disabled'
def test_it_does_not_disable_registration_on_user_registration( def test_it_does_not_disable_registration_on_user_registration(
self, self,
@ -1361,7 +1247,7 @@ class TestRegistrationConfiguration:
assert response.status_code == 201 assert response.status_code == 201
class TestPasswordResetRequest: class TestPasswordResetRequest(ApiTestCaseMixin):
@patch('smtplib.SMTP_SSL') @patch('smtplib.SMTP_SSL')
@patch('smtplib.SMTP') @patch('smtplib.SMTP')
def test_it_requests_password_reset_when_user_exists( def test_it_requests_password_reset_when_user_exists(
@ -1404,10 +1290,7 @@ class TestPasswordResetRequest:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 400 self.assert_400(response)
data = json.loads(response.data.decode())
assert data['message'] == 'invalid payload'
assert data['status'] == 'error'
def test_it_returns_error_on_empty_payload(self, app: Flask) -> None: def test_it_returns_error_on_empty_payload(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -1418,13 +1301,10 @@ class TestPasswordResetRequest:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 400 self.assert_400(response)
data = json.loads(response.data.decode())
assert data['message'] == 'invalid payload'
assert data['status'] == 'error'
class TestPasswordUpdate: class TestPasswordUpdate(ApiTestCaseMixin):
def test_it_returns_error_if_payload_is_empty(self, app: Flask) -> None: def test_it_returns_error_if_payload_is_empty(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -1439,10 +1319,7 @@ class TestPasswordUpdate:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 400 self.assert_400(response)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
def test_it_returns_error_if_token_is_missing(self, app: Flask) -> None: def test_it_returns_error_if_token_is_missing(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -1458,10 +1335,7 @@ class TestPasswordUpdate:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 400 self.assert_400(response)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
def test_it_returns_error_if_password_is_missing(self, app: Flask) -> None: def test_it_returns_error_if_password_is_missing(self, app: Flask) -> None:
client = app.test_client() client = app.test_client()
@ -1477,10 +1351,7 @@ class TestPasswordUpdate:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 400 self.assert_400(response)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
def test_it_returns_error_if_password_confirmation_is_missing( def test_it_returns_error_if_password_confirmation_is_missing(
self, app: Flask self, app: Flask
@ -1498,10 +1369,7 @@ class TestPasswordUpdate:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 400 self.assert_400(response)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
def test_it_returns_error_if_token_is_invalid(self, app: Flask) -> None: def test_it_returns_error_if_token_is_invalid(self, app: Flask) -> None:
token = get_user_token(1) token = get_user_token(1)
@ -1519,10 +1387,7 @@ class TestPasswordUpdate:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 401 self.assert_401(response, 'invalid token, please request a new token')
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'invalid token, please request a new token'
def test_it_returns_error_if_token_is_expired( def test_it_returns_error_if_token_is_expired(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -1544,11 +1409,8 @@ class TestPasswordUpdate:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 401 self.assert_401(
data = json.loads(response.data.decode()) response, 'invalid token, please request a new token'
assert data['status'] == 'error'
assert (
data['message'] == 'invalid token, please request a new token'
) )
def test_it_returns_error_if_password_is_invalid( def test_it_returns_error_if_password_is_invalid(
@ -1569,10 +1431,7 @@ class TestPasswordUpdate:
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 400 self.assert_400(response, 'password: 8 characters required\n')
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'password: 8 characters required\n'
def test_it_update_password(self, app: Flask, user_1: User) -> None: def test_it_update_password(self, app: Flask, user_1: User) -> None:
token = get_user_token(user_1.id, password_reset=True) token = get_user_token(user_1.id, password_reset=True)

View File

@ -106,11 +106,8 @@ class TestGetUser(ApiTestCaseMixin):
content_type='application/json', content_type='application/json',
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode())
assert response.status_code == 404 self.assert_404_with_entity(response, 'user')
assert 'not found' in data['status']
assert 'user does not exist' in data['message']
class TestGetUsers(ApiTestCaseMixin): class TestGetUsers(ApiTestCaseMixin):
@ -823,7 +820,7 @@ class TestGetUsers(ApiTestCaseMixin):
} }
class TestGetUserPicture: class TestGetUserPicture(ApiTestCaseMixin):
def test_it_return_error_if_user_has_no_picture( def test_it_return_error_if_user_has_no_picture(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
@ -831,10 +828,7 @@ class TestGetUserPicture:
response = client.get(f'/api/users/{user_1.username}/picture') response = client.get(f'/api/users/{user_1.username}/picture')
data = json.loads(response.data.decode()) self.assert_404_with_message(response, 'No picture.')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'No picture.' in data['message']
def test_it_returns_error_if_user_does_not_exist( def test_it_returns_error_if_user_does_not_exist(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -843,10 +837,7 @@ class TestGetUserPicture:
response = client.get('/api/users/not_existing/picture') response = client.get('/api/users/not_existing/picture')
data = json.loads(response.data.decode()) self.assert_404_with_entity(response, 'user')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'user does not exist' in data['message']
class TestUpdateUser(ApiTestCaseMixin): class TestUpdateUser(ApiTestCaseMixin):
@ -909,10 +900,7 @@ class TestUpdateUser(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
def test_it_returns_error_if_payload_for_admin_rights_is_invalid( def test_it_returns_error_if_payload_for_admin_rights_is_invalid(
self, app: Flask, user_1_admin: User, user_2: User self, app: Flask, user_1_admin: User, user_2: User
@ -928,13 +916,7 @@ class TestUpdateUser(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
def test_it_returns_error_if_user_can_not_change_admin_rights( def test_it_returns_error_if_user_can_not_change_admin_rights(
self, app: Flask, user_1: User, user_2: User self, app: Flask, user_1: User, user_2: User
@ -950,10 +932,7 @@ class TestUpdateUser(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
assert response.status_code == 403
assert 'error' in data['status']
assert 'you do not have permissions' in data['message']
class TestDeleteUser(ApiTestCaseMixin): class TestDeleteUser(ApiTestCaseMixin):
@ -1048,10 +1027,7 @@ class TestDeleteUser(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
assert response.status_code == 403
assert 'error' in data['status']
assert 'you do not have permissions' in data['message']
def test_it_returns_error_when_deleting_non_existing_user( def test_it_returns_error_when_deleting_non_existing_user(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -1065,10 +1041,7 @@ class TestDeleteUser(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_entity(response, 'user')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'user does not exist' in data['message']
def test_admin_can_delete_another_user_account( def test_admin_can_delete_another_user_account(
self, app: Flask, user_1_admin: User, user_2: User self, app: Flask, user_1_admin: User, user_2: User
@ -1110,12 +1083,9 @@ class TestDeleteUser(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(
assert response.status_code == 403 response,
assert 'error' in data['status'] 'you can not delete your account, no other user has admin rights',
assert (
'you can not delete your account, no other user has admin rights'
in data['message']
) )
def test_it_enables_registration_on_user_delete( def test_it_enables_registration_on_user_delete(
@ -1176,7 +1146,4 @@ class TestDeleteUser(ApiTestCaseMixin):
content_type='application/json', content_type='application/json',
) )
assert response.status_code == 403 self.assert_403(response, 'error, registration is disabled')
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'error, registration is disabled'

View File

@ -209,9 +209,7 @@ class TestGetSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_404(response)
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['sports']) == 0 assert len(data['data']['sports']) == 0
def test_it_gets_a_inactive_sport( def test_it_gets_a_inactive_sport(
@ -424,11 +422,7 @@ class TestUpdateSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
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( def test_returns_error_if_payload_is_invalid(
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
@ -444,10 +438,7 @@ class TestUpdateSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
def test_it_returns_error_if_sport_does_not_exist( def test_it_returns_error_if_sport_does_not_exist(
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
@ -463,7 +454,5 @@ class TestUpdateSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_404(response)
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['sports']) == 0 assert len(data['data']['sports']) == 0

View File

@ -38,10 +38,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_entity(response, 'user')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'user does not exist' in data['message']
def test_it_returns_error_if_date_format_is_invalid( def test_it_returns_error_if_date_format_is_invalid(
self, self,
@ -64,13 +61,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
def test_it_returns_error_if_period_is_invalid( def test_it_returns_error_if_period_is_invalid(
self, self,
@ -90,10 +81,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response, 'Invalid time period.', 'fail')
assert response.status_code == 400
assert 'fail' in data['status']
assert 'Invalid time period.' in data['message']
def test_it_gets_stats_by_time_all_workouts( def test_it_gets_stats_by_time_all_workouts(
self, self,
@ -955,10 +943,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_entity(response, 'user')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'user does not exist' in data['message']
def test_it_returns_error_if_sport_does_not_exist( def test_it_returns_error_if_sport_does_not_exist(
self, self,
@ -978,10 +963,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_entity(response, 'sport')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'sport does not exist' in data['message']
def test_it_returns_error_if_sport_id_is_invalid( def test_it_returns_error_if_sport_id_is_invalid(
self, self,
@ -1001,13 +983,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
class TestGetAllStats(ApiTestCaseMixin): class TestGetAllStats(ApiTestCaseMixin):
@ -1081,8 +1057,4 @@ class TestGetAllStats(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
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']

View File

@ -107,10 +107,7 @@ class TestGetWorkouts(ApiTestCaseMixin):
response = client.get('/api/workouts') response = client.get('/api/workouts')
data = json.loads(response.data.decode()) self.assert_401(response, 'provide a valid auth token')
assert response.status_code == 401
assert 'error' in data['status']
assert 'provide a valid auth token' in data['message']
class TestGetWorkoutsWithPagination(ApiTestCaseMixin): class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
@ -278,13 +275,7 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
@patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6) @patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6)
def test_it_gets_max_workouts_per_page_if_per_page_exceeds_max( def test_it_gets_max_workouts_per_page_if_per_page_exceeds_max(
@ -1034,10 +1025,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
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_workout_does_not_exist( def test_it_returns_404_if_workout_does_not_exist(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -1051,9 +1039,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_404(response)
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['workouts']) == 0 assert len(data['data']['workouts']) == 0
def test_it_returns_404_on_getting_gpx_if_workout_does_not_exist( def test_it_returns_404_on_getting_gpx_if_workout_does_not_exist(
@ -1069,10 +1055,9 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_404_with_message(
assert response.status_code == 404 response, f'workout not found (id: {random_short_id})'
assert 'not found' in data['status'] )
assert f'workout not found (id: {random_short_id})' in data['message']
assert data['data']['gpx'] == '' assert data['data']['gpx'] == ''
def test_it_returns_404_on_getting_chart_data_if_workout_does_not_exist( def test_it_returns_404_on_getting_chart_data_if_workout_does_not_exist(
@ -1088,10 +1073,9 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_404_with_message(
assert response.status_code == 404 response, f'workout not found (id: {random_short_id})'
assert 'not found' in data['status'] )
assert f'workout not found (id: {random_short_id})' in data['message']
assert data['data']['chart_data'] == '' assert data['data']['chart_data'] == ''
def test_it_returns_404_on_getting_gpx_if_workout_have_no_gpx( def test_it_returns_404_on_getting_gpx_if_workout_have_no_gpx(
@ -1111,12 +1095,8 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_message(
assert response.status_code == 404 response, f'no gpx file for this workout (id: {workout_short_id})'
assert 'not found' in data['status']
assert (
f'no gpx file for this workout (id: {workout_short_id})'
in data['message']
) )
def test_it_returns_404_if_workout_have_no_chart_data( def test_it_returns_404_if_workout_have_no_chart_data(
@ -1136,12 +1116,8 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_message(
assert response.status_code == 404 response, f'no gpx file for this workout (id: {workout_short_id})'
assert 'not found' in data['status']
assert (
f'no gpx file for this workout (id: {workout_short_id})'
in data['message']
) )
def test_it_returns_500_on_getting_gpx_if_an_workout_has_invalid_gpx_pathname( # noqa def test_it_returns_500_on_getting_gpx_if_an_workout_has_invalid_gpx_pathname( # noqa
@ -1161,13 +1137,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
assert 'data' not in data assert 'data' not in data
def test_it_returns_500_on_getting_chart_data_if_an_workout_has_invalid_gpx_pathname( # noqa def test_it_returns_500_on_getting_chart_data_if_an_workout_has_invalid_gpx_pathname( # noqa
@ -1187,13 +1157,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
assert 'data' not in data assert 'data' not in data
def test_it_returns_404_if_workout_has_no_map( def test_it_returns_404_if_workout_has_no_map(
@ -1206,11 +1170,8 @@ class TestGetWorkout(ApiTestCaseMixin):
f'/api/workouts/map/{uuid4().hex}', f'/api/workouts/map/{uuid4().hex}',
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode())
assert response.status_code == 404 self.assert_404_with_message(response, 'Map does not exist')
assert 'not found' in data['status']
assert 'Map does not exist' in data['message']
class TestDownloadWorkoutGpx(ApiTestCaseMixin): class TestDownloadWorkoutGpx(ApiTestCaseMixin):
@ -1228,10 +1189,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_message(response, 'workout not found')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'workout not found' in data['message']
def test_it_returns_404_if_workout_does_not_have_gpx( def test_it_returns_404_if_workout_does_not_have_gpx(
self, self,
@ -1249,10 +1207,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_message(response, 'no gpx file for workout')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'no gpx file for workout' in data['message']
def test_it_returns_404_if_workout_belongs_to_a_different_user( def test_it_returns_404_if_workout_belongs_to_a_different_user(
self, self,
@ -1271,10 +1226,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_404_with_message(response, 'workout not found')
assert response.status_code == 404
assert 'not found' in data['status']
assert 'workout not found' in data['message']
def test_it_calls_send_from_directory_if_workout_has_gpx( def test_it_calls_send_from_directory_if_workout_has_gpx(
self, self,

View File

@ -1,6 +1,5 @@
import json import json
import os import os
import re
from datetime import datetime from datetime import datetime
from io import BytesIO from io import BytesIO
from typing import Dict from typing import Dict
@ -424,10 +423,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
), ),
) )
data = json.loads(response.data.decode()) data = self.assert_500(response, 'Error during gpx processing.')
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error during gpx processing.' in data['message']
assert 'data' not in data assert 'data' not in data
def test_it_returns_500_if_gpx_has_invalid_xml( def test_it_returns_500_if_gpx_has_invalid_xml(
@ -456,10 +452,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
), ),
) )
data = json.loads(response.data.decode()) data = self.assert_500(response, 'Error during gpx file parsing.')
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error during gpx file parsing.' in data['message']
assert 'data' not in data assert 'data' not in data
def test_it_returns_400_if_workout_gpx_has_invalid_extension( def test_it_returns_400_if_workout_gpx_has_invalid_extension(
@ -481,10 +474,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_400(response, 'file extension not allowed', 'fail')
assert response.status_code == 400
assert data['status'] == 'fail'
assert data['message'] == 'file extension not allowed'
def test_it_returns_400_if_sport_id_is_not_provided( def test_it_returns_400_if_sport_id_is_not_provided(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -504,10 +494,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
def test_it_returns_500_if_sport_id_does_not_exists( def test_it_returns_500_if_sport_id_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -528,10 +515,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_500(response, 'Sport id: 2 does not exist')
assert response.status_code == 500
assert data['status'] == 'error'
assert data['message'] == 'Sport id: 2 does not exist'
def test_returns_400_if_no_gpx_file_is_provided( def test_returns_400_if_no_gpx_file_is_provided(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -549,10 +533,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_400(response, 'no file part', 'fail')
assert response.status_code == 400
assert data['status'] == 'fail'
assert data['message'] == 'no file part'
def test_it_returns_error_if_file_size_exceeds_limit( def test_it_returns_error_if_file_size_exceeds_limit(
self, self,
@ -576,12 +557,13 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
Authorization=f'Bearer {auth_token}', Authorization=f'Bearer {auth_token}',
), ),
) )
data = json.loads(response.data.decode())
assert response.status_code == 413 data = self.assert_413(
assert 'fail' in data['status'] response,
assert re.match( match=(
r'Error during workout upload, file size \((.*)\) exceeds 1.0KB.', r'Error during workout upload, '
data['message'], r'file size \((.*)\) exceeds 1.0KB.'
),
) )
assert 'data' not in data assert 'data' not in data
@ -628,10 +610,7 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
def test_it_returns_500_if_workout_format_is_invalid( def test_it_returns_500_if_workout_format_is_invalid(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -654,10 +633,7 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response, 'Error during workout save.', status='fail')
assert response.status_code == 500
assert 'fail' in data['status']
assert 'Error during workout save.' in data['message']
def test_it_adds_workout_with_zero_value( def test_it_adds_workout_with_zero_value(
self, self,
@ -767,9 +743,7 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) data = self.assert_400(response, error_message=None, status='fail')
assert response.status_code == 400
assert 'fail' in data['status']
assert len(data['data']['workouts']) == 0 assert len(data['data']['workouts']) == 0
def test_it_returns_500_if_one_file_in_zip_archive_is_invalid( def test_it_returns_500_if_one_file_in_zip_archive_is_invalid(
@ -796,10 +770,7 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) data = self.assert_500(response, 'Error during gpx processing.')
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error during gpx processing.' in data['message']
assert 'data' not in data assert 'data' not in data
def test_it_imports_only_max_number_of_files( def test_it_imports_only_max_number_of_files(
@ -860,12 +831,11 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
Authorization=f'Bearer {auth_token}', Authorization=f'Bearer {auth_token}',
), ),
) )
data = json.loads(response.data.decode())
assert response.status_code == 413 data = self.assert_413(
assert 'fail' in data['status'] response,
assert ( 'Error during workout upload, '
'Error during workout upload, file size (2.5KB) exceeds 1.0KB.' 'file size (2.5KB) exceeds 1.0KB.',
in data['message']
) )
assert 'data' not in data assert 'data' not in data
@ -943,14 +913,8 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
f'/api/workouts/map/{map_id}', f'/api/workouts/map/{map_id}',
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode())
assert response.status_code == 500 self.assert_500(response)
assert data['status'] == 'error'
assert (
data['message']
== 'error, please try again or contact the administrator'
)
def test_it_gets_an_workout_created_with_gpx( def test_it_gets_an_workout_created_with_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -1068,10 +1032,7 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
assert response.status_code == 403
assert 'error' in data['status']
assert data['message'] == 'you do not have permissions'
def test_it_returns_500_on_invalid_segment_id( def test_it_returns_500_on_invalid_segment_id(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -1098,11 +1059,7 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response, 'Incorrect segment id')
assert response.status_code == 500
assert 'error' in data['status']
assert data['message'] == 'Incorrect segment id'
assert 'data' not in data
def test_it_returns_404_if_segment_id_does_not_exist( def test_it_returns_404_if_segment_id_does_not_exist(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -1129,10 +1086,9 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_404_with_message(
assert response.status_code == 404 response, 'No segment with id \'999999\''
assert 'not found' in data['status'] )
assert data['message'] == 'No segment with id \'999999\''
assert 'data' not in data assert 'data' not in data

View File

@ -169,10 +169,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
assert response.status_code == 403
assert 'error' in data['status']
assert 'you do not have permissions' in data['message']
def test_it_updates_sport( def test_it_updates_sport(
self, self,
@ -213,10 +210,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {token}'), headers=dict(Authorization=f'Bearer {token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
def test_it_raises_500_if_sport_does_not_exists( def test_it_raises_500_if_sport_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -231,13 +225,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {token}'), headers=dict(Authorization=f'Bearer {token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
@ -409,10 +397,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
assert response.status_code == 403
assert 'error' in data['status']
assert 'you do not have permissions' in data['message']
def test_it_updates_an_workout_wo_gpx_with_timezone( def test_it_updates_an_workout_wo_gpx_with_timezone(
self, self,
@ -574,10 +559,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_400(response)
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
def test_it_returns_500_if_date_format_is_invalid( def test_it_returns_500_if_date_format_is_invalid(
self, self,
@ -603,14 +585,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
def test_it_returns_404_if_edited_workout_does_not_exists( def test_it_returns_404_if_edited_workout_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -632,7 +607,5 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode()) data = self.assert_404(response)
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['workouts']) == 0 assert len(data['data']['workouts']) == 0

View File

@ -54,11 +54,7 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
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_workout_does_not_exist( def test_it_returns_404_if_workout_does_not_exist(
self, app: Flask, user_1: User self, app: Flask, user_1: User
@ -66,12 +62,13 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email app, user_1.email
) )
response = client.delete( response = client.delete(
f'/api/workouts/{get_random_short_id()}', f'/api/workouts/{get_random_short_id()}',
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
) )
data = json.loads(response.data.decode())
assert response.status_code == 404 data = self.assert_404(response)
assert 'not found' in data['status'] assert 'not found' in data['status']
def test_it_returns_500_when_deleting_an_workout_with_gpx_invalid_file( def test_it_returns_500_when_deleting_an_workout_with_gpx_invalid_file(
@ -88,14 +85,7 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {token}'), headers=dict(Authorization=f'Bearer {token}'),
) )
data = json.loads(response.data.decode()) self.assert_500(response)
assert response.status_code == 500
assert 'error' in data['status']
assert (
'error, please try again or contact the administrator'
in data['message']
)
class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin): class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin):
@ -137,8 +127,4 @@ class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin):
), ),
) )
data = json.loads(response.data.decode()) self.assert_403(response)
assert response.status_code == 403
assert 'error' in data['status']
assert 'you do not have permissions' in data['message']