diff --git a/fittrackee/tests/api_test_case.py b/fittrackee/tests/api_test_case.py index 8021f097..9683a933 100644 --- a/fittrackee/tests/api_test_case.py +++ b/fittrackee/tests/api_test_case.py @@ -1,8 +1,11 @@ import json -from typing import Any, Tuple +from typing import Any, Dict, Optional, Tuple from flask import Flask from flask.testing import FlaskClient +from werkzeug.test import TestResponse + +from .custom_asserts import assert_errored_response class ApiTestCaseMixin: @@ -29,6 +32,74 @@ class ApiTestCaseMixin: auth_token = json.loads(resp_login.data.decode())['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: @staticmethod diff --git a/fittrackee/tests/application/test_app_config_api.py b/fittrackee/tests/application/test_app_config_api.py index a52dc422..9cfddaa0 100644 --- a/fittrackee/tests/application/test_app_config_api.py +++ b/fittrackee/tests/application/test_app_config_api.py @@ -49,10 +49,7 @@ class TestGetConfig(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_500(response, 'error on getting configuration') def test_it_returns_error_if_application_has_several_config( self, app: Flask, app_config: Flask, user_1_admin: User @@ -67,10 +64,7 @@ class TestGetConfig(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_500(response, 'error on getting configuration') class TestUpdateConfig(ApiTestCaseMixin): @@ -140,11 +134,7 @@ class TestUpdateConfig(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_403(response) def test_it_returns_400_if_invalid_is_payload( self, app: Flask, user_1_admin: User @@ -160,10 +150,7 @@ class TestUpdateConfig(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert 'invalid payload' in data['message'] + self.assert_400(response) def test_it_returns_error_on_update_if_application_has_no_config( self, app_no_config: Flask, user_1_admin: User @@ -179,10 +166,7 @@ class TestUpdateConfig(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 500 - assert 'error' in data['status'] - assert 'error when updating configuration' in data['message'] + self.assert_500(response, 'error when updating configuration') def test_it_raises_error_if_archive_max_size_is_below_files_max_size( self, app: Flask, user_1_admin: User @@ -205,13 +189,13 @@ class TestUpdateConfig(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert ( - 'Max. size of zip archive must be equal or greater than max. size ' - 'of uploaded files' - ) in data['message'] + self.assert_400( + response, + ( + 'Max. size of zip archive must be equal or greater than max.' + ' size of uploaded files' + ), + ) def test_it_raises_error_if_archive_max_size_equals_0( 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert ( - 'Max. size of zip archive must be greater than 0' - in data['message'] + self.assert_400( + response, 'Max. size of zip archive must be greater than 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert ( - 'Max. size of uploaded files must be greater than 0' - in data['message'] + self.assert_400( + response, 'Max. size of uploaded files must be greater than 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert ( - 'Max. files in a zip archive must be greater than 0' - in data['message'] + self.assert_400( + response, 'Max. files in a zip archive must be greater than 0' ) diff --git a/fittrackee/tests/conftest.py b/fittrackee/tests/conftest.py index 46b610fd..2cc0ae74 100644 --- a/fittrackee/tests/conftest.py +++ b/fittrackee/tests/conftest.py @@ -1,5 +1,8 @@ import os +import pytest +from werkzeug.test import TestResponse + os.environ['FLASK_ENV'] = 'testing' os.environ['APP_SETTINGS'] = 'fittrackee.config.TestingConfig' # to avoid resetting dev database during tests @@ -10,3 +13,8 @@ pytest_plugins = [ 'fittrackee.tests.fixtures.fixtures_workouts', '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 diff --git a/fittrackee/tests/custom_asserts.py b/fittrackee/tests/custom_asserts.py new file mode 100644 index 00000000..168dd073 --- /dev/null +++ b/fittrackee/tests/custom_asserts.py @@ -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 diff --git a/fittrackee/tests/users/test_auth_api.py b/fittrackee/tests/users/test_auth_api.py index 010a696d..79c67e50 100644 --- a/fittrackee/tests/users/test_auth_api.py +++ b/fittrackee/tests/users/test_auth_api.py @@ -14,7 +14,7 @@ from fittrackee.workouts.models import Sport, Workout from ..api_test_case import ApiTestCaseMixin -class TestUserRegistration: +class TestUserRegistration(ApiTestCaseMixin): def test_user_can_register(self, app: Flask) -> None: client = app.test_client() @@ -58,11 +58,8 @@ class TestUserRegistration: ), content_type='application/json', ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'sorry, that user already exists' - assert response.content_type == 'application/json' - assert response.status_code == 400 + + self.assert_400(response, 'sorry, that user already exists') @pytest.mark.parametrize( 'input_email', @@ -84,11 +81,8 @@ class TestUserRegistration: ), content_type='application/json', ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'sorry, that user already exists' - assert response.content_type == 'application/json' - assert response.status_code == 400 + + self.assert_400(response, 'sorry, that user already exists') def test_it_returns_error_if_username_is_too_short( self, app: Flask @@ -108,11 +102,7 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - 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 + self.assert_400(response, "username: 3 to 12 characters required\n") def test_it_returns_error_if_username_is_too_long( self, app: Flask @@ -130,11 +120,8 @@ class TestUserRegistration: ), content_type='application/json', ) - data = json.loads(response.data.decode()) - 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 + + self.assert_400(response, "username: 3 to 12 characters required\n") def test_it_returns_error_if_email_is_invalid(self, app: Flask) -> None: client = app.test_client() @@ -152,11 +139,7 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - 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 + self.assert_400(response, "email: valid email must be provided\n") def test_it_returns_error_if_password_is_too_short( self, app: Flask @@ -176,11 +159,7 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == "password: 8 characters required\n" - assert response.content_type == 'application/json' - assert response.status_code == 400 + self.assert_400(response, "password: 8 characters required\n") def test_it_returns_error_if_passwords_mismatch(self, app: Flask) -> None: client = app.test_client() @@ -198,14 +177,10 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert ( - data['message'] - == "password: password and password confirmation do not match\n" + self.assert_400( + response, + "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: client = app.test_client() @@ -234,10 +209,7 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'invalid payload' in data['message'] - assert 'error' in data['status'] + self.assert_400(response) def test_it_returns_error_if_email_is_missing(self, app: Flask) -> None: client = app.test_client() @@ -254,10 +226,7 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'invalid payload' in data['message'] - assert 'error' in data['status'] + self.assert_400(response) def test_it_returns_error_if_password_is_missing(self, app: Flask) -> None: client = app.test_client() @@ -274,10 +243,7 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'invalid payload', data['message'] - assert 'error', data['status'] + self.assert_400(response) def test_it_returns_error_if_password_confirmation_is_missing( self, app: Flask @@ -292,10 +258,8 @@ class TestUserRegistration: ), content_type='application/json', ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'invalid payload' in data['message'] - assert 'error' in data['status'] + + self.assert_400(response) def test_it_returns_error_if_username_is_invalid(self, app: Flask) -> None: client = app.test_client() @@ -313,16 +277,10 @@ class TestUserRegistration: content_type='application/json', ) - data = json.loads(response.data.decode()) - assert response.status_code == 500 - assert ( - 'error, please try again or contact the administrator' - in data['message'] - ) - assert 'error' in data['status'] + self.assert_500(response) -class TestUserLogin: +class TestUserLogin(ApiTestCaseMixin): @pytest.mark.parametrize( 'input_email', ['test@test.com', 'TEST@TEST.COM'], @@ -378,11 +336,7 @@ class TestUserLogin: content_type='application/json', ) - assert response.content_type == 'application/json' - assert response.status_code == 401 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid credentials' + self.assert_401(response, 'invalid credentials') def test_it_returns_error_on_invalid_payload(self, app: Flask) -> None: client = app.test_client() @@ -393,11 +347,7 @@ class TestUserLogin: content_type='application/json', ) - assert response.content_type == 'application/json' - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' + self.assert_400(response) def test_it_returns_error_if_password_is_invalid( self, app: Flask, user_1: User @@ -410,11 +360,7 @@ class TestUserLogin: content_type='application/json', ) - assert response.content_type == 'application/json' - assert response.status_code == 401 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid credentials' + self.assert_401(response, 'invalid credentials') class TestUserLogout(ApiTestCaseMixin): @@ -441,34 +387,30 @@ class TestUserLogout(ApiTestCaseMixin): client, auth_token = self.get_test_client_and_auth_token( app, user_1.email ) - with freeze_time(now + timedelta(seconds=4)): + response = client.get( '/api/auth/logout', headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'signature expired, please log in again' - assert response.status_code == 401 + + self.assert_401(response, 'signature expired, please log in again') def test_it_returns_error_with_invalid_token(self, app: Flask) -> None: client = app.test_client() + response = client.get( '/api/auth/logout', headers=dict(Authorization='Bearer invalid') ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid token, please log in again' - assert response.status_code == 401 + + self.assert_401(response, 'invalid token, please log in again') def test_it_returns_error_with_invalid_headers(self, app: Flask) -> None: client = app.test_client() + response = client.get('/api/auth/logout', headers=dict()) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'provide a valid auth token' - assert response.status_code == 401 + + self.assert_401(response, 'provide a valid auth token') class TestUserProfile(ApiTestCaseMixin): @@ -576,13 +518,12 @@ class TestUserProfile(ApiTestCaseMixin): def test_it_returns_error_if_headers_are_invalid(self, app: Flask) -> None: client = app.test_client() + response = client.get( '/api/auth/profile', headers=dict(Authorization='Bearer invalid') ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid token, please log in again' - assert response.status_code == 401 + + self.assert_401(response, 'invalid token, please log in again') class TestUserProfileUpdate(ApiTestCaseMixin): @@ -692,10 +633,7 @@ class TestUserProfileUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' - assert response.status_code == 400 + self.assert_400(response) def test_it_returns_error_if_payload_is_empty( self, app: Flask, user_1: User @@ -711,10 +649,7 @@ class TestUserProfileUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'invalid payload' in data['message'] - assert 'error' in data['status'] + self.assert_400(response) def test_it_returns_error_if_passwords_mismatch( self, app: Flask, user_1: User @@ -740,13 +675,10 @@ class TestUserProfileUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert ( - data['message'] - == 'password: password and password confirmation do not match\n' + self.assert_400( + response, + 'password: password and password confirmation do not match\n', ) - assert response.status_code == 400 def test_it_returns_error_if_password_confirmation_is_missing( self, app: Flask, user_1: User @@ -771,13 +703,10 @@ class TestUserProfileUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert ( - data['message'] - == 'password: password and password confirmation do not match\n' + self.assert_400( + response, + 'password: password and password confirmation do not match\n', ) - assert response.status_code == 400 class TestUserPreferencesUpdate(ApiTestCaseMixin): @@ -840,10 +769,7 @@ class TestUserPreferencesUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' - assert response.status_code == 400 + self.assert_400(response) def test_it_returns_error_if_payload_is_empty( self, app: Flask, user_1: User @@ -859,10 +785,7 @@ class TestUserPreferencesUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'invalid payload' in data['message'] - assert 'error' in data['status'] + self.assert_400(response) class TestUserSportPreferencesUpdate(ApiTestCaseMixin): @@ -880,10 +803,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'invalid payload' in data['message'] - assert 'error' in data['status'] + self.assert_400(response) def test_it_returns_error_if_sport_id_is_missing( self, app: Flask, user_1: User @@ -899,10 +819,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' - assert response.status_code == 400 + self.assert_400(response) def test_it_returns_error_if_sport_not_found( self, app: Flask, user_1: User @@ -918,10 +835,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'sport does not exist' in data['message'] + self.assert_404_with_entity(response, 'sport') def test_it_returns_error_if_payload_contains_only_sport_id( self, app: Flask, user_1: User, sport_1_cycling: Sport @@ -937,10 +851,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' - assert response.status_code == 400 + self.assert_400(response) def test_it_returns_error_if_color_is_invalid( self, app: Flask, user_1: User, sport_1_cycling: Sport @@ -961,10 +872,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid hexadecimal color' - assert response.status_code == 400 + self.assert_400(response, 'invalid hexadecimal color') @pytest.mark.parametrize( 'input_color', @@ -1075,10 +983,7 @@ class TestUserSportPreferencesReset(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - assert response.status_code == 404 - data = json.loads(response.data.decode()) - assert 'not found' in data['status'] - assert 'sport does not exist' in data['message'] + self.assert_404_with_entity(response, 'sport') def test_it_resets_sport_preferences( self, @@ -1172,10 +1077,7 @@ class TestUserPicture(ApiTestCaseMixin): ), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'fail' - assert data['message'] == 'no file part' - assert response.status_code == 400 + self.assert_400(response, 'no file part', 'fail') def test_it_returns_error_if_file_is_invalid( self, app: Flask, user_1: User @@ -1193,10 +1095,7 @@ class TestUserPicture(ApiTestCaseMixin): ), ) - data = json.loads(response.data.decode()) - assert data['status'] == 'fail' - assert data['message'] == 'file extension not allowed' - assert response.status_code == 400 + self.assert_400(response, 'file extension not allowed', 'fail') def test_it_returns_error_if_image_size_exceeds_file_limit( self, @@ -1220,12 +1119,9 @@ class TestUserPicture(ApiTestCaseMixin): ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 413 - assert 'fail' in data['status'] - assert ( - 'Error during picture upload, file size (1.2KB) exceeds 1.0KB.' - in data['message'] + data = self.assert_413( + response, + 'Error during picture upload, file size (1.2KB) exceeds 1.0KB.', ) assert 'data' not in data @@ -1251,17 +1147,14 @@ class TestUserPicture(ApiTestCaseMixin): ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 413 - assert 'fail' in data['status'] - assert ( - 'Error during picture upload, file size (1.2KB) exceeds 1.0KB.' - in data['message'] + data = self.assert_413( + response, + 'Error during picture upload, file size (1.2KB) exceeds 1.0KB.', ) assert 'data' not in data -class TestRegistrationConfiguration: +class TestRegistrationConfiguration(ApiTestCaseMixin): def test_it_returns_error_if_it_exceeds_max_users( self, app_with_3_users_max: Flask, @@ -1284,11 +1177,7 @@ class TestRegistrationConfiguration: content_type='application/json', ) - assert response.content_type == 'application/json' - assert response.status_code == 403 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'error, registration is disabled' + self.assert_403(response, 'error, registration is disabled') def test_it_disables_registration_on_user_registration( self, @@ -1323,10 +1212,7 @@ class TestRegistrationConfiguration: content_type='application/json', ) - assert response.status_code == 403 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'error, registration is disabled' + self.assert_403(response, 'error, registration is disabled') def test_it_does_not_disable_registration_on_user_registration( self, @@ -1361,7 +1247,7 @@ class TestRegistrationConfiguration: assert response.status_code == 201 -class TestPasswordResetRequest: +class TestPasswordResetRequest(ApiTestCaseMixin): @patch('smtplib.SMTP_SSL') @patch('smtplib.SMTP') def test_it_requests_password_reset_when_user_exists( @@ -1404,10 +1290,7 @@ class TestPasswordResetRequest: content_type='application/json', ) - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['message'] == 'invalid payload' - assert data['status'] == 'error' + self.assert_400(response) def test_it_returns_error_on_empty_payload(self, app: Flask) -> None: client = app.test_client() @@ -1418,13 +1301,10 @@ class TestPasswordResetRequest: content_type='application/json', ) - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['message'] == 'invalid payload' - assert data['status'] == 'error' + self.assert_400(response) -class TestPasswordUpdate: +class TestPasswordUpdate(ApiTestCaseMixin): def test_it_returns_error_if_payload_is_empty(self, app: Flask) -> None: client = app.test_client() @@ -1439,10 +1319,7 @@ class TestPasswordUpdate: content_type='application/json', ) - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' + self.assert_400(response) def test_it_returns_error_if_token_is_missing(self, app: Flask) -> None: client = app.test_client() @@ -1458,10 +1335,7 @@ class TestPasswordUpdate: content_type='application/json', ) - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' + self.assert_400(response) def test_it_returns_error_if_password_is_missing(self, app: Flask) -> None: client = app.test_client() @@ -1477,10 +1351,7 @@ class TestPasswordUpdate: content_type='application/json', ) - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' + self.assert_400(response) def test_it_returns_error_if_password_confirmation_is_missing( self, app: Flask @@ -1498,10 +1369,7 @@ class TestPasswordUpdate: content_type='application/json', ) - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' + self.assert_400(response) def test_it_returns_error_if_token_is_invalid(self, app: Flask) -> None: token = get_user_token(1) @@ -1519,10 +1387,7 @@ class TestPasswordUpdate: content_type='application/json', ) - assert response.status_code == 401 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'invalid token, please request a new token' + self.assert_401(response, 'invalid token, please request a new token') def test_it_returns_error_if_token_is_expired( self, app: Flask, user_1: User @@ -1544,11 +1409,8 @@ class TestPasswordUpdate: content_type='application/json', ) - assert response.status_code == 401 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert ( - data['message'] == 'invalid token, please request a new token' + self.assert_401( + response, 'invalid token, please request a new token' ) def test_it_returns_error_if_password_is_invalid( @@ -1569,10 +1431,7 @@ class TestPasswordUpdate: content_type='application/json', ) - assert response.status_code == 400 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'password: 8 characters required\n' + self.assert_400(response, 'password: 8 characters required\n') def test_it_update_password(self, app: Flask, user_1: User) -> None: token = get_user_token(user_1.id, password_reset=True) diff --git a/fittrackee/tests/users/test_users_api.py b/fittrackee/tests/users/test_users_api.py index 6fc6432c..ee2cdcc3 100644 --- a/fittrackee/tests/users/test_users_api.py +++ b/fittrackee/tests/users/test_users_api.py @@ -106,11 +106,8 @@ class TestGetUser(ApiTestCaseMixin): content_type='application/json', headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'user does not exist' in data['message'] + self.assert_404_with_entity(response, 'user') 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( self, app: Flask, user_1: User ) -> None: @@ -831,10 +828,7 @@ class TestGetUserPicture: response = client.get(f'/api/users/{user_1.username}/picture') - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'No picture.' in data['message'] + self.assert_404_with_message(response, 'No picture.') def test_it_returns_error_if_user_does_not_exist( self, app: Flask, user_1: User @@ -843,10 +837,7 @@ class TestGetUserPicture: response = client.get('/api/users/not_existing/picture') - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'user does not exist' in data['message'] + self.assert_404_with_entity(response, 'user') class TestUpdateUser(ApiTestCaseMixin): @@ -909,10 +900,7 @@ class TestUpdateUser(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert 'invalid payload' in data['message'] + self.assert_400(response) def test_it_returns_error_if_payload_for_admin_rights_is_invalid( self, app: Flask, user_1_admin: User, user_2: User @@ -928,13 +916,7 @@ class TestUpdateUser(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] - ) + self.assert_500(response) def test_it_returns_error_if_user_can_not_change_admin_rights( self, app: Flask, user_1: User, user_2: User @@ -950,10 +932,7 @@ class TestUpdateUser(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_403(response) class TestDeleteUser(ApiTestCaseMixin): @@ -1048,10 +1027,7 @@ class TestDeleteUser(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_403(response) def test_it_returns_error_when_deleting_non_existing_user( self, app: Flask, user_1: User @@ -1065,10 +1041,7 @@ class TestDeleteUser(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'user does not exist' in data['message'] + self.assert_404_with_entity(response, 'user') def test_admin_can_delete_another_user_account( self, app: Flask, user_1_admin: User, user_2: User @@ -1110,12 +1083,9 @@ class TestDeleteUser(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 403 - assert 'error' in data['status'] - assert ( - 'you can not delete your account, no other user has admin rights' - in data['message'] + self.assert_403( + response, + 'you can not delete your account, no other user has admin rights', ) def test_it_enables_registration_on_user_delete( @@ -1176,7 +1146,4 @@ class TestDeleteUser(ApiTestCaseMixin): content_type='application/json', ) - assert response.status_code == 403 - data = json.loads(response.data.decode()) - assert data['status'] == 'error' - assert data['message'] == 'error, registration is disabled' + self.assert_403(response, 'error, registration is disabled') diff --git a/fittrackee/tests/workouts/test_sports_api.py b/fittrackee/tests/workouts/test_sports_api.py index cdce3e52..4cc76157 100644 --- a/fittrackee/tests/workouts/test_sports_api.py +++ b/fittrackee/tests/workouts/test_sports_api.py @@ -209,9 +209,7 @@ class TestGetSport(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] + data = self.assert_404(response) assert len(data['data']['sports']) == 0 def test_it_gets_a_inactive_sport( @@ -424,11 +422,7 @@ class TestUpdateSport(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_403(response) def test_returns_error_if_payload_is_invalid( self, app: Flask, user_1_admin: User @@ -444,10 +438,7 @@ class TestUpdateSport(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert 'invalid payload' in data['message'] + self.assert_400(response) def test_it_returns_error_if_sport_does_not_exist( self, app: Flask, user_1_admin: User @@ -463,7 +454,5 @@ class TestUpdateSport(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] + data = self.assert_404(response) assert len(data['data']['sports']) == 0 diff --git a/fittrackee/tests/workouts/test_stats_api.py b/fittrackee/tests/workouts/test_stats_api.py index 9421345c..b9ed5442 100644 --- a/fittrackee/tests/workouts/test_stats_api.py +++ b/fittrackee/tests/workouts/test_stats_api.py @@ -38,10 +38,7 @@ class TestGetStatsByTime(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'user does not exist' in data['message'] + self.assert_404_with_entity(response, 'user') def test_it_returns_error_if_date_format_is_invalid( self, @@ -64,13 +61,7 @@ class TestGetStatsByTime(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] - ) + self.assert_500(response) def test_it_returns_error_if_period_is_invalid( self, @@ -90,10 +81,7 @@ class TestGetStatsByTime(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'fail' in data['status'] - assert 'Invalid time period.' in data['message'] + self.assert_400(response, 'Invalid time period.', 'fail') def test_it_gets_stats_by_time_all_workouts( self, @@ -955,10 +943,7 @@ class TestGetStatsBySport(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'user does not exist' in data['message'] + self.assert_404_with_entity(response, 'user') def test_it_returns_error_if_sport_does_not_exist( self, @@ -978,10 +963,7 @@ class TestGetStatsBySport(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'sport does not exist' in data['message'] + self.assert_404_with_entity(response, 'sport') def test_it_returns_error_if_sport_id_is_invalid( self, @@ -1001,13 +983,7 @@ class TestGetStatsBySport(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] - ) + self.assert_500(response) class TestGetAllStats(ApiTestCaseMixin): @@ -1081,8 +1057,4 @@ class TestGetAllStats(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_403(response) diff --git a/fittrackee/tests/workouts/test_workouts_api_0_get.py b/fittrackee/tests/workouts/test_workouts_api_0_get.py index ba4c0ac9..d6d537ff 100644 --- a/fittrackee/tests/workouts/test_workouts_api_0_get.py +++ b/fittrackee/tests/workouts/test_workouts_api_0_get.py @@ -107,10 +107,7 @@ class TestGetWorkouts(ApiTestCaseMixin): response = client.get('/api/workouts') - data = json.loads(response.data.decode()) - assert response.status_code == 401 - assert 'error' in data['status'] - assert 'provide a valid auth token' in data['message'] + self.assert_401(response, 'provide a valid auth token') class TestGetWorkoutsWithPagination(ApiTestCaseMixin): @@ -278,13 +275,7 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] - ) + self.assert_500(response) @patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6) 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}'), ) - 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'] + self.assert_403(response) def test_it_returns_404_if_workout_does_not_exist( self, app: Flask, user_1: User @@ -1051,9 +1039,7 @@ class TestGetWorkout(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] + data = self.assert_404(response) assert len(data['data']['workouts']) == 0 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert f'workout not found (id: {random_short_id})' in data['message'] + data = self.assert_404_with_message( + response, f'workout not found (id: {random_short_id})' + ) assert data['data']['gpx'] == '' 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert f'workout not found (id: {random_short_id})' in data['message'] + data = self.assert_404_with_message( + response, f'workout not found (id: {random_short_id})' + ) assert data['data']['chart_data'] == '' 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert ( - f'no gpx file for this workout (id: {workout_short_id})' - in data['message'] + self.assert_404_with_message( + response, f'no gpx file for this workout (id: {workout_short_id})' ) 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert ( - f'no gpx file for this workout (id: {workout_short_id})' - in data['message'] + self.assert_404_with_message( + response, f'no gpx file for this workout (id: {workout_short_id})' ) 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}'), ) - 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'] - ) + data = self.assert_500(response) assert 'data' not in data 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}'), ) - 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'] - ) + data = self.assert_500(response) assert 'data' not in data def test_it_returns_404_if_workout_has_no_map( @@ -1206,11 +1170,8 @@ class TestGetWorkout(ApiTestCaseMixin): f'/api/workouts/map/{uuid4().hex}', headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'Map does not exist' in data['message'] + self.assert_404_with_message(response, 'Map does not exist') class TestDownloadWorkoutGpx(ApiTestCaseMixin): @@ -1228,10 +1189,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'workout not found' in data['message'] + self.assert_404_with_message(response, 'workout not found') def test_it_returns_404_if_workout_does_not_have_gpx( self, @@ -1249,10 +1207,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'no gpx file for workout' in data['message'] + self.assert_404_with_message(response, 'no gpx file for workout') def test_it_returns_404_if_workout_belongs_to_a_different_user( self, @@ -1271,10 +1226,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert 'workout not found' in data['message'] + self.assert_404_with_message(response, 'workout not found') def test_it_calls_send_from_directory_if_workout_has_gpx( self, diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index bce80688..a6e7693c 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -1,6 +1,5 @@ import json import os -import re from datetime import datetime from io import BytesIO from typing import Dict @@ -424,10 +423,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 500 - assert 'error' in data['status'] - assert 'Error during gpx processing.' in data['message'] + data = self.assert_500(response, 'Error during gpx processing.') assert 'data' not in data def test_it_returns_500_if_gpx_has_invalid_xml( @@ -456,10 +452,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 500 - assert 'error' in data['status'] - assert 'Error during gpx file parsing.' in data['message'] + data = self.assert_500(response, 'Error during gpx file parsing.') assert 'data' not in data 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()) - assert response.status_code == 400 - assert data['status'] == 'fail' - assert data['message'] == 'file extension not allowed' + self.assert_400(response, 'file extension not allowed', 'fail') def test_it_returns_400_if_sport_id_is_not_provided( 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()) - assert response.status_code == 400 - assert data['status'] == 'error' - assert data['message'] == 'invalid payload' + self.assert_400(response) def test_it_returns_500_if_sport_id_does_not_exists( 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()) - assert response.status_code == 500 - assert data['status'] == 'error' - assert data['message'] == 'Sport id: 2 does not exist' + self.assert_500(response, 'Sport id: 2 does not exist') def test_returns_400_if_no_gpx_file_is_provided( self, app: Flask, user_1: User, sport_1_cycling: Sport @@ -549,10 +533,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert data['status'] == 'fail' - assert data['message'] == 'no file part' + self.assert_400(response, 'no file part', 'fail') def test_it_returns_error_if_file_size_exceeds_limit( self, @@ -576,12 +557,13 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): Authorization=f'Bearer {auth_token}', ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 413 - assert 'fail' in data['status'] - assert re.match( - r'Error during workout upload, file size \((.*)\) exceeds 1.0KB.', - data['message'], + + data = self.assert_413( + response, + match=( + r'Error during workout upload, ' + r'file size \((.*)\) exceeds 1.0KB.' + ), ) assert 'data' not in data @@ -628,10 +610,7 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert 'invalid payload' in data['message'] + self.assert_400(response) def test_it_returns_500_if_workout_format_is_invalid( self, app: Flask, user_1: User, sport_1_cycling: Sport @@ -654,10 +633,7 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 500 - assert 'fail' in data['status'] - assert 'Error during workout save.' in data['message'] + self.assert_500(response, 'Error during workout save.', status='fail') def test_it_adds_workout_with_zero_value( self, @@ -767,9 +743,7 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin): ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'fail' in data['status'] + data = self.assert_400(response, error_message=None, status='fail') assert len(data['data']['workouts']) == 0 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()) - assert response.status_code == 500 - assert 'error' in data['status'] - assert 'Error during gpx processing.' in data['message'] + data = self.assert_500(response, 'Error during gpx processing.') assert 'data' not in data def test_it_imports_only_max_number_of_files( @@ -860,12 +831,11 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin): Authorization=f'Bearer {auth_token}', ), ) - data = json.loads(response.data.decode()) - assert response.status_code == 413 - assert 'fail' in data['status'] - assert ( - 'Error during workout upload, file size (2.5KB) exceeds 1.0KB.' - in data['message'] + + data = self.assert_413( + response, + 'Error during workout upload, ' + 'file size (2.5KB) exceeds 1.0KB.', ) assert 'data' not in data @@ -943,14 +913,8 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin): f'/api/workouts/map/{map_id}', headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 500 - assert data['status'] == 'error' - assert ( - data['message'] - == 'error, please try again or contact the administrator' - ) + self.assert_500(response) def test_it_gets_an_workout_created_with_gpx( 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()) - assert response.status_code == 403 - assert 'error' in data['status'] - assert data['message'] == 'you do not have permissions' + self.assert_403(response) def test_it_returns_500_on_invalid_segment_id( 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 500 - assert 'error' in data['status'] - assert data['message'] == 'Incorrect segment id' - assert 'data' not in data + self.assert_500(response, 'Incorrect segment id') def test_it_returns_404_if_segment_id_does_not_exist( 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}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] - assert data['message'] == 'No segment with id \'999999\'' + data = self.assert_404_with_message( + response, 'No segment with id \'999999\'' + ) assert 'data' not in data diff --git a/fittrackee/tests/workouts/test_workouts_api_2_patch.py b/fittrackee/tests/workouts/test_workouts_api_2_patch.py index d9055f00..df84d2e9 100644 --- a/fittrackee/tests/workouts/test_workouts_api_2_patch.py +++ b/fittrackee/tests/workouts/test_workouts_api_2_patch.py @@ -169,10 +169,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin): ), ) - 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'] + self.assert_403(response) def test_it_updates_sport( self, @@ -213,10 +210,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert 'invalid payload' in data['message'] + self.assert_400(response) def test_it_raises_500_if_sport_does_not_exists( 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}'), ) - 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'] - ) + self.assert_500(response) class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): @@ -409,10 +397,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] + self.assert_403(response) def test_it_updates_an_workout_wo_gpx_with_timezone( self, @@ -574,10 +559,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 400 - assert 'error' in data['status'] - assert 'invalid payload' in data['message'] + self.assert_400(response) def test_it_returns_500_if_date_format_is_invalid( self, @@ -603,14 +585,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {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'] - ) + self.assert_500(response) def test_it_returns_404_if_edited_workout_does_not_exists( self, app: Flask, user_1: User, sport_1_cycling: Sport @@ -632,7 +607,5 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin): headers=dict(Authorization=f'Bearer {auth_token}'), ) - data = json.loads(response.data.decode()) - assert response.status_code == 404 - assert 'not found' in data['status'] + data = self.assert_404(response) assert len(data['data']['workouts']) == 0 diff --git a/fittrackee/tests/workouts/test_workouts_api_3_delete.py b/fittrackee/tests/workouts/test_workouts_api_3_delete.py index b739ead4..fae8e6c5 100644 --- a/fittrackee/tests/workouts/test_workouts_api_3_delete.py +++ b/fittrackee/tests/workouts/test_workouts_api_3_delete.py @@ -54,11 +54,7 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin): ), ) - 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'] + self.assert_403(response) def test_it_returns_404_if_workout_does_not_exist( self, app: Flask, user_1: User @@ -66,12 +62,13 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin): client, auth_token = self.get_test_client_and_auth_token( app, user_1.email ) + response = client.delete( f'/api/workouts/{get_random_short_id()}', 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'] 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}'), ) - 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'] - ) + self.assert_500(response) class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin): @@ -137,8 +127,4 @@ class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin): ), ) - 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'] + self.assert_403(response)