Merge branch 'test-refacto' into dev

This commit is contained in:
Sam 2022-03-13 08:37:49 +01:00
commit ebd9811efa
13 changed files with 851 additions and 773 deletions

View File

@ -1,22 +1,30 @@
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:
@staticmethod @staticmethod
def get_test_client_and_auth_token( def get_test_client_and_auth_token(
app: Flask, as_admin: bool = False app: Flask, user_email: str
) -> Tuple[FlaskClient, str]: ) -> Tuple[FlaskClient, str]:
"""user_email must be user_1 or user_2 email"""
client = app.test_client() client = app.test_client()
resp_login = client.post( resp_login = client.post(
'/api/auth/login', '/api/auth/login',
data=json.dumps( data=json.dumps(
dict( dict(
email='admin@example.com' if as_admin else 'test@test.com', email=user_email,
password='12345678', password=(
'87654321'
if user_email == 'toto@toto.com'
else '12345678'
),
) )
), ),
content_type='application/json', content_type='application/json',
@ -24,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

@ -12,7 +12,9 @@ class TestGetConfig(ApiTestCaseMixin):
def test_it_gets_application_config( def test_it_gets_application_config(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/config', '/api/config',
@ -38,7 +40,7 @@ class TestGetConfig(ApiTestCaseMixin):
self, app_no_config: Flask, user_1_admin: User self, app_no_config: Flask, user_1_admin: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_no_config, as_admin=True app_no_config, user_1_admin.email
) )
response = client.get( response = client.get(
@ -47,16 +49,13 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -65,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):
@ -76,7 +72,7 @@ class TestUpdateConfig(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
'/api/config', '/api/config',
@ -98,7 +94,7 @@ class TestUpdateConfig(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -127,7 +123,9 @@ class TestUpdateConfig(ApiTestCaseMixin):
def test_it_returns_403_when_user_is_not_an_admin( def test_it_returns_403_when_user_is_not_an_admin(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
'/api/config', '/api/config',
@ -136,17 +134,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_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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -156,16 +150,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(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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_no_config, as_admin=True app_no_config, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -175,16 +166,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_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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -201,19 +189,19 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_max_file_size_equals_0, as_admin=True app_with_max_file_size_equals_0, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -227,19 +215,15 @@ 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(
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -253,19 +237,15 @@ 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(
self, app: Flask, user_1_admin: User self, app: Flask, user_1_admin: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -279,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,17 +360,15 @@ 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):
def test_user_can_logout(self, app: Flask, user_1: User) -> None: def test_user_can_logout(self, app: Flask, user_1: User) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/auth/logout', '/api/auth/logout',
@ -436,42 +384,42 @@ class TestUserLogout(ApiTestCaseMixin):
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
now = datetime.utcnow() now = datetime.utcnow()
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
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):
def test_it_returns_user_minimal_profile( def test_it_returns_user_minimal_profile(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/auth/profile', '/api/auth/profile',
@ -500,7 +448,9 @@ class TestUserProfile(ApiTestCaseMixin):
def test_it_returns_user_full_profile( def test_it_returns_user_full_profile(
self, app: Flask, user_1_full: User self, app: Flask, user_1_full: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_full.email
)
response = client.get( response = client.get(
'/api/auth/profile', '/api/auth/profile',
@ -540,7 +490,9 @@ class TestUserProfile(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/auth/profile', '/api/auth/profile',
@ -566,18 +518,19 @@ 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):
def test_it_updates_user_profile(self, app: Flask, user_1: User) -> None: def test_it_updates_user_profile(self, app: Flask, user_1: User) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit', '/api/auth/profile/edit',
@ -623,7 +576,9 @@ class TestUserProfileUpdate(ApiTestCaseMixin):
def test_it_updates_user_profile_without_password( def test_it_updates_user_profile_without_password(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit', '/api/auth/profile/edit',
@ -667,7 +622,9 @@ class TestUserProfileUpdate(ApiTestCaseMixin):
def test_it_returns_error_if_fields_are_missing( def test_it_returns_error_if_fields_are_missing(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit', '/api/auth/profile/edit',
@ -676,15 +633,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit', '/api/auth/profile/edit',
@ -693,15 +649,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit', '/api/auth/profile/edit',
@ -720,18 +675,17 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit', '/api/auth/profile/edit',
@ -749,20 +703,19 @@ 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):
def test_it_updates_user_preferences( def test_it_updates_user_preferences(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/preferences', '/api/auth/profile/edit/preferences',
@ -805,7 +758,9 @@ class TestUserPreferencesUpdate(ApiTestCaseMixin):
def test_it_returns_error_if_fields_are_missing( def test_it_returns_error_if_fields_are_missing(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/preferences', '/api/auth/profile/edit/preferences',
@ -814,15 +769,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/preferences', '/api/auth/profile/edit/preferences',
@ -831,17 +785,16 @@ 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):
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -850,15 +803,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -867,15 +819,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -884,15 +835,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -901,15 +851,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -923,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',
@ -939,7 +885,9 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
sport_2_running: Sport, sport_2_running: Sport,
input_color: str, input_color: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -966,7 +914,9 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
def test_it_disables_sport_for_auth_user( def test_it_disables_sport_for_auth_user(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -993,7 +943,9 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin):
def test_it_updates_stopped_speed_threshold_for_auth_user( def test_it_updates_stopped_speed_threshold_for_auth_user(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/profile/edit/sports', '/api/auth/profile/edit/sports',
@ -1022,17 +974,16 @@ class TestUserSportPreferencesReset(ApiTestCaseMixin):
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: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
'/api/auth/profile/reset/sports/1', '/api/auth/profile/reset/sports/1',
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,
@ -1041,7 +992,9 @@ class TestUserSportPreferencesReset(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference, user_sport_1_preference: UserSportPreference,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
f'/api/auth/profile/reset/sports/{sport_1_cycling.id}', f'/api/auth/profile/reset/sports/{sport_1_cycling.id}',
@ -1060,7 +1013,9 @@ class TestUserSportPreferencesReset(ApiTestCaseMixin):
def test_it_does_not_raise_error_if_sport_preferences_do_not_exist( def test_it_does_not_raise_error_if_sport_preferences_do_not_exist(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
f'/api/auth/profile/reset/sports/{sport_1_cycling.id}', f'/api/auth/profile/reset/sports/{sport_1_cycling.id}',
@ -1072,7 +1027,9 @@ class TestUserSportPreferencesReset(ApiTestCaseMixin):
class TestUserPicture(ApiTestCaseMixin): class TestUserPicture(ApiTestCaseMixin):
def test_it_updates_user_picture(self, app: Flask, user_1: User) -> None: def test_it_updates_user_picture(self, app: Flask, user_1: User) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/picture', '/api/auth/picture',
@ -1108,7 +1065,9 @@ class TestUserPicture(ApiTestCaseMixin):
def test_it_returns_error_if_file_is_missing( def test_it_returns_error_if_file_is_missing(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/picture', '/api/auth/picture',
@ -1118,15 +1077,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/auth/picture', '/api/auth/picture',
@ -1137,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,
@ -1150,7 +1105,7 @@ class TestUserPicture(ApiTestCaseMixin):
gpx_file: str, gpx_file: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_max_file_size app_with_max_file_size, user_1.email
) )
response = client.post( response = client.post(
@ -1164,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
@ -1181,7 +1133,7 @@ class TestUserPicture(ApiTestCaseMixin):
gpx_file: str, gpx_file: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_max_zip_file_size app_with_max_zip_file_size, user_1.email
) )
response = client.post( response = client.post(
@ -1195,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,
@ -1228,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,
@ -1267,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,
@ -1305,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(
@ -1348,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()
@ -1362,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()
@ -1383,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()
@ -1402,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()
@ -1421,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
@ -1442,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)
@ -1463,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
@ -1488,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(
@ -1513,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

@ -15,7 +15,9 @@ class TestGetUser(ApiTestCaseMixin):
def test_it_gets_single_user_without_workouts( def test_it_gets_single_user_without_workouts(
self, app: Flask, user_1: User, user_2: User self, app: Flask, user_1: User, user_2: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/users/{user_2.username}', f'/api/users/{user_2.username}',
@ -57,7 +59,9 @@ class TestGetUser(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/users/{user_1.username}', f'/api/users/{user_1.username}',
@ -93,25 +97,26 @@ class TestGetUser(ApiTestCaseMixin):
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users/not_existing', '/api/users/not_existing',
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):
def test_it_get_users_list( def test_it_get_users_list(
self, app: Flask, user_1: User, user_2: User, user_3: User self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users', '/api/users',
@ -181,7 +186,9 @@ class TestGetUsers(ApiTestCaseMixin):
workout_running_user_1: Workout, workout_running_user_1: Workout,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users', '/api/users',
@ -244,7 +251,9 @@ class TestGetUsers(ApiTestCaseMixin):
user_2: User, user_2: User,
user_3: User, user_3: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?page=1', '/api/users?page=1',
@ -271,7 +280,9 @@ class TestGetUsers(ApiTestCaseMixin):
user_2: User, user_2: User,
user_3: User, user_3: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?page=2', '/api/users?page=2',
@ -297,7 +308,9 @@ class TestGetUsers(ApiTestCaseMixin):
user_2: User, user_2: User,
user_3: User, user_3: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?page=2', '/api/users?page=2',
@ -323,7 +336,9 @@ class TestGetUsers(ApiTestCaseMixin):
user_2: User, user_2: User,
user_3: User, user_3: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?per_page=2', '/api/users?per_page=2',
@ -349,7 +364,9 @@ class TestGetUsers(ApiTestCaseMixin):
user_2: User, user_2: User,
user_3: User, user_3: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?page=2&per_page=2', '/api/users?page=2&per_page=2',
@ -371,7 +388,9 @@ class TestGetUsers(ApiTestCaseMixin):
def test_it_gets_users_list_ordered_by_username( def test_it_gets_users_list_ordered_by_username(
self, app: Flask, user_1: User, user_2: User, user_3: User self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?order_by=username', '/api/users?order_by=username',
@ -396,7 +415,9 @@ class TestGetUsers(ApiTestCaseMixin):
def test_it_gets_users_list_ordered_by_username_ascending( def test_it_gets_users_list_ordered_by_username_ascending(
self, app: Flask, user_1: User, user_2: User, user_3: User self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?order_by=username&order=asc', '/api/users?order_by=username&order=asc',
@ -421,7 +442,9 @@ class TestGetUsers(ApiTestCaseMixin):
def test_it_gets_users_list_ordered_by_username_descending( def test_it_gets_users_list_ordered_by_username_descending(
self, app: Flask, user_1: User, user_2: User, user_3: User self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?order_by=username&order=desc', '/api/users?order_by=username&order=desc',
@ -450,7 +473,7 @@ class TestGetUsers(ApiTestCaseMixin):
user_3.created_at = datetime.utcnow() - timedelta(hours=1) user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow() user_1_admin.created_at = datetime.utcnow()
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -480,7 +503,7 @@ class TestGetUsers(ApiTestCaseMixin):
user_3.created_at = datetime.utcnow() - timedelta(hours=1) user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow() user_1_admin.created_at = datetime.utcnow()
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -510,7 +533,7 @@ class TestGetUsers(ApiTestCaseMixin):
user_3.created_at = datetime.utcnow() - timedelta(hours=1) user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow() user_1_admin.created_at = datetime.utcnow()
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -537,7 +560,7 @@ class TestGetUsers(ApiTestCaseMixin):
self, app: Flask, user_2: User, user_1_admin: User, user_3: User self, app: Flask, user_2: User, user_1_admin: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -564,7 +587,7 @@ class TestGetUsers(ApiTestCaseMixin):
self, app: Flask, user_2: User, user_1_admin: User, user_3: User self, app: Flask, user_2: User, user_1_admin: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -591,7 +614,7 @@ class TestGetUsers(ApiTestCaseMixin):
self, app: Flask, user_2: User, user_3: User, user_1_admin: User self, app: Flask, user_2: User, user_3: User, user_1_admin: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -623,7 +646,9 @@ class TestGetUsers(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?order_by=workouts_count', '/api/users?order_by=workouts_count',
@ -657,7 +682,9 @@ class TestGetUsers(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?order_by=workouts_count&order=asc', '/api/users?order_by=workouts_count&order=asc',
@ -691,7 +718,9 @@ class TestGetUsers(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?order_by=workouts_count&order=desc', '/api/users?order_by=workouts_count&order=desc',
@ -719,7 +748,9 @@ class TestGetUsers(ApiTestCaseMixin):
def test_it_gets_users_list_filtering_on_username( def test_it_gets_users_list_filtering_on_username(
self, app: Flask, user_1: User, user_2: User, user_3: User self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?q=toto', '/api/users?q=toto',
@ -742,7 +773,9 @@ class TestGetUsers(ApiTestCaseMixin):
def test_it_returns_empty_users_list_filtering_on_username( def test_it_returns_empty_users_list_filtering_on_username(
self, app: Flask, user_1: User, user_2: User, user_3: User self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?q=not_existing', '/api/users?q=not_existing',
@ -764,7 +797,9 @@ class TestGetUsers(ApiTestCaseMixin):
def test_it_users_list_with_complex_query( def test_it_users_list_with_complex_query(
self, app: Flask, user_1: User, user_2: User, user_3: User self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/users?order_by=username&order=desc&page=2&per_page=2', '/api/users?order_by=username&order=desc&page=2&per_page=2',
@ -785,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:
@ -793,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
@ -805,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):
@ -816,7 +845,7 @@ class TestUpdateUser(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, user_2: User self, app: Flask, user_1_admin: User, user_2: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -838,7 +867,7 @@ class TestUpdateUser(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, user_2: User self, app: Flask, user_1_admin: User, user_2: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -861,7 +890,7 @@ class TestUpdateUser(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, user_2: User self, app: Flask, user_1_admin: User, user_2: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -871,16 +900,13 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -890,18 +916,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
'/api/users/toto', '/api/users/toto',
@ -910,17 +932,16 @@ 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):
def test_user_can_delete_its_own_account( def test_user_can_delete_its_own_account(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
'/api/users/test', '/api/users/test',
@ -932,7 +953,9 @@ class TestDeleteUser(ApiTestCaseMixin):
def test_user_with_workout_can_delete_its_own_account( def test_user_with_workout_can_delete_its_own_account(
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
client.post( client.post(
'/api/workouts', '/api/workouts',
data=dict( data=dict(
@ -959,7 +982,9 @@ class TestDeleteUser(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference, user_sport_1_preference: UserSportPreference,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
'/api/users/test', '/api/users/test',
@ -971,7 +996,9 @@ class TestDeleteUser(ApiTestCaseMixin):
def test_user_with_picture_can_delete_its_own_account( def test_user_with_picture_can_delete_its_own_account(
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
client.post( client.post(
'/api/auth/picture', '/api/auth/picture',
data=dict(file=(BytesIO(b'avatar'), 'avatar.png')), data=dict(file=(BytesIO(b'avatar'), 'avatar.png')),
@ -991,38 +1018,36 @@ class TestDeleteUser(ApiTestCaseMixin):
def test_user_can_not_delete_another_user_account( def test_user_can_not_delete_another_user_account(
self, app: Flask, user_1: User, user_2: User self, app: Flask, user_1: User, user_2: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
'/api/users/toto', '/api/users/toto',
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
'/api/users/not_existing', '/api/users/not_existing',
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.delete( response = client.delete(
@ -1036,7 +1061,7 @@ class TestDeleteUser(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, user_2_admin: User self, app: Flask, user_1_admin: User, user_2_admin: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.delete( response = client.delete(
@ -1050,7 +1075,7 @@ class TestDeleteUser(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, user_2: User self, app: Flask, user_1_admin: User, user_2: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.delete( response = client.delete(
@ -1058,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(
@ -1074,7 +1096,7 @@ class TestDeleteUser(ApiTestCaseMixin):
user_3: User, user_3: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_3_users_max, as_admin=True app_with_3_users_max, user_1_admin.email
) )
client.delete( client.delete(
'/api/users/toto', '/api/users/toto',
@ -1104,7 +1126,7 @@ class TestDeleteUser(ApiTestCaseMixin):
user_1_paris: User, user_1_paris: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_3_users_max, as_admin=True app_with_3_users_max, user_1_admin.email
) )
client.delete( client.delete(
@ -1124,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

@ -19,7 +19,9 @@ class TestGetRecords(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/records', '/api/records',
@ -92,7 +94,9 @@ class TestGetRecords(ApiTestCaseMixin):
sport_2_running: Sport, sport_2_running: Sport,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/records', '/api/records',
@ -111,7 +115,9 @@ class TestGetRecords(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
sport_2_running: Sport, sport_2_running: Sport,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
client.post( client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -141,7 +147,9 @@ class TestGetRecords(ApiTestCaseMixin):
def test_it_gets_updated_records_after_workouts_post_and_patch( def test_it_gets_updated_records_after_workouts_post_and_patch(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
content_type='application/json', content_type='application/json',
@ -628,7 +636,9 @@ class TestGetRecords(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
sport_2_running: Sport, sport_2_running: Sport,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',

View File

@ -52,7 +52,9 @@ class TestGetSports(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
sport_2_running: Sport, sport_2_running: Sport,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/sports', '/api/sports',
@ -73,7 +75,9 @@ class TestGetSports(ApiTestCaseMixin):
sport_1_cycling_inactive: Sport, sport_1_cycling_inactive: Sport,
sport_2_running: Sport, sport_2_running: Sport,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/sports', '/api/sports',
@ -98,7 +102,7 @@ class TestGetSports(ApiTestCaseMixin):
sport_2_running: Sport, sport_2_running: Sport,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -132,7 +136,7 @@ class TestGetSports(ApiTestCaseMixin):
db.session.commit() db.session.commit()
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -156,7 +160,9 @@ class TestGetSport(ApiTestCaseMixin):
def test_it_gets_a_sport( def test_it_gets_a_sport(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/sports/1', '/api/sports/1',
@ -176,7 +182,9 @@ class TestGetSport(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
user_sport_1_preference: UserSportPreference, user_sport_1_preference: UserSportPreference,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/sports/1', '/api/sports/1',
@ -192,22 +200,24 @@ class TestGetSport(ApiTestCaseMixin):
def test_it_returns_404_if_sport_does_not_exist( def test_it_returns_404_if_sport_does_not_exist(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/sports/1', '/api/sports/1',
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(
self, app: Flask, user_1: User, sport_1_cycling_inactive: Sport self, app: Flask, user_1: User, sport_1_cycling_inactive: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/sports/1', '/api/sports/1',
@ -227,7 +237,7 @@ class TestGetSport(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, sport_1_cycling_inactive: Sport self, app: Flask, user_1_admin: User, sport_1_cycling_inactive: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -250,7 +260,7 @@ class TestUpdateSport(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, sport_1_cycling: Sport self, app: Flask, user_1_admin: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -273,7 +283,7 @@ class TestUpdateSport(ApiTestCaseMixin):
) -> None: ) -> None:
sport_1_cycling.is_active = False sport_1_cycling.is_active = False
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -299,7 +309,7 @@ class TestUpdateSport(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -326,7 +336,7 @@ class TestUpdateSport(ApiTestCaseMixin):
) -> None: ) -> None:
sport_1_cycling.is_active = False sport_1_cycling.is_active = False
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -352,7 +362,7 @@ class TestUpdateSport(ApiTestCaseMixin):
user_admin_sport_1_preference: UserSportPreference, user_admin_sport_1_preference: UserSportPreference,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -380,7 +390,7 @@ class TestUpdateSport(ApiTestCaseMixin):
) -> None: ) -> None:
sport_1_cycling.is_active = False sport_1_cycling.is_active = False
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -401,7 +411,9 @@ class TestUpdateSport(ApiTestCaseMixin):
def test_returns_error_if_user_has_no_admin_rights( def test_returns_error_if_user_has_no_admin_rights(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
'/api/sports/1', '/api/sports/1',
@ -410,17 +422,13 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -430,16 +438,13 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.patch( response = client.patch(
@ -449,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

@ -12,7 +12,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
def test_it_gets_no_stats_when_user_has_no_workouts( def test_it_gets_no_stats_when_user_has_no_workouts(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time', f'/api/stats/{user_1.username}/by_time',
@ -27,17 +29,16 @@ class TestGetStatsByTime(ApiTestCaseMixin):
def test_it_returns_error_when_user_does_not_exists( def test_it_returns_error_when_user_does_not_exists(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/stats/1000/by_time', '/api/stats/1000/by_time',
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,
@ -48,7 +49,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
( (
@ -58,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,
@ -75,17 +72,16 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=day', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=day', # noqa
headers=dict(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,
@ -96,7 +92,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time', f'/api/stats/{user_1.username}/by_time',
@ -146,7 +144,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30', # noqa
@ -186,7 +186,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_paris.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1_paris.username}/by_time?' f'/api/stats/{user_1_paris.username}/by_time?'
@ -227,7 +229,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?time=year', f'/api/stats/{user_1.username}/by_time?time=year',
@ -277,7 +281,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa
@ -318,7 +324,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_paris.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1_paris.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa f'/api/stats/{user_1_paris.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa
@ -358,7 +366,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?time=month', f'/api/stats/{user_1.username}/by_time?time=month',
@ -448,7 +458,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_full.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1_full.username}/by_time?time=month', f'/api/stats/{user_1_full.username}/by_time?time=month',
@ -538,7 +550,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=month', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=month', # noqa
@ -578,7 +592,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_full.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1_full.username}/by_time?time=week', f'/api/stats/{user_1_full.username}/by_time?time=week',
@ -668,7 +684,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=week', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=week', # noqa
@ -708,7 +726,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?time=weekm', f'/api/stats/{user_1.username}/by_time?time=weekm',
@ -798,7 +818,9 @@ class TestGetStatsByTime(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=weekm', # noqa f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=weekm', # noqa
@ -840,7 +862,9 @@ class TestGetStatsBySport(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport', f'/api/stats/{user_1.username}/by_sport',
@ -878,7 +902,9 @@ class TestGetStatsBySport(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id=1', f'/api/stats/{user_1.username}/by_sport?sport_id=1',
@ -908,17 +934,16 @@ class TestGetStatsBySport(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/stats/1000/by_sport?sport_id=1', '/api/stats/1000/by_sport?sport_id=1',
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,
@ -929,17 +954,16 @@ class TestGetStatsBySport(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id=999', f'/api/stats/{user_1.username}/by_sport?sport_id=999',
headers=dict(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,
@ -950,20 +974,16 @@ class TestGetStatsBySport(ApiTestCaseMixin):
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id="999', f'/api/stats/{user_1.username}/by_sport?sport_id="999',
headers=dict(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):
@ -971,7 +991,7 @@ class TestGetAllStats(ApiTestCaseMixin):
self, app: Flask, user_1_admin: User, user_2: User self, app: Flask, user_1_admin: User, user_2: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -1000,7 +1020,7 @@ class TestGetAllStats(ApiTestCaseMixin):
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True app, user_1_admin.email
) )
response = client.get( response = client.get(
@ -1028,15 +1048,13 @@ class TestGetAllStats(ApiTestCaseMixin):
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/stats/all', '/api/stats/all',
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

@ -23,7 +23,9 @@ class TestGetWorkouts(ApiTestCaseMixin):
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts', '/api/workouts',
@ -105,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):
@ -119,7 +118,9 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts', '/api/workouts',
@ -157,7 +158,9 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?page=1', '/api/workouts?page=1',
@ -195,7 +198,9 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?page=2', '/api/workouts?page=2',
@ -233,7 +238,9 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?page=3', '/api/workouts?page=3',
@ -259,20 +266,16 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?page=A', '/api/workouts?page=A',
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(
@ -282,7 +285,9 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?per_page=10', '/api/workouts?per_page=10',
@ -317,7 +322,9 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?per_page=3', '/api/workouts?per_page=3',
@ -353,7 +360,9 @@ class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts', '/api/workouts',
@ -387,7 +396,9 @@ class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?order=asc', '/api/workouts?order=asc',
@ -421,7 +432,9 @@ class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?order=desc', '/api/workouts?order=desc',
@ -457,7 +470,9 @@ class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?order_by=workout_date', '/api/workouts?order_by=workout_date',
@ -491,7 +506,9 @@ class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?order_by=distance', '/api/workouts?order_by=distance',
@ -519,7 +536,9 @@ class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?order_by=duration', '/api/workouts?order_by=duration',
@ -547,7 +566,9 @@ class TestGetWorkoutsWithOrderBy(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?order_by=ave_speed', '/api/workouts?order_by=ave_speed',
@ -577,7 +598,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?from=2018-02-01&to=2018-02-28', '/api/workouts?from=2018-02-01&to=2018-02-28',
@ -615,7 +638,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?from=2018-03-01&to=2018-03-30', '/api/workouts?from=2018-03-01&to=2018-03-30',
@ -641,7 +666,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?from=2018-04-01', '/api/workouts?from=2018-04-01',
@ -676,7 +703,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?to=2017-12-31', '/api/workouts?to=2017-12-31',
@ -710,7 +739,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?distance_from=5&distance_to=8.1', '/api/workouts?distance_from=5&distance_to=8.1',
@ -744,7 +775,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?duration_from=00:52&duration_to=01:20', '/api/workouts?duration_from=00:52&duration_to=01:20',
@ -774,7 +807,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?ave_speed_from=5&ave_speed_to=10', '/api/workouts?ave_speed_from=5&ave_speed_to=10',
@ -808,7 +843,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
) -> None: ) -> None:
workout_cycling_user_1.max_speed = 25 workout_cycling_user_1.max_speed = 25
workout_running_user_1.max_speed = 11 workout_running_user_1.max_speed = 11
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?max_speed_from=10&max_speed_to=20', '/api/workouts?max_speed_from=10&max_speed_to=20',
@ -840,7 +877,9 @@ class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
sport_2_running: Sport, sport_2_running: Sport,
workout_running_user_1: Workout, workout_running_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?sport_id=2', '/api/workouts?sport_id=2',
@ -872,7 +911,9 @@ class TestGetWorkoutsWithFiltersAndPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?from=2017-01-01&page=2', '/api/workouts?from=2017-01-01&page=2',
@ -906,7 +947,9 @@ class TestGetWorkoutsWithFiltersAndPagination(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
seven_workouts_user_1: Workout, seven_workouts_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
'/api/workouts?from=2017-01-01&page=2&order=asc', '/api/workouts?from=2017-01-01&page=2&order=asc',
@ -942,7 +985,9 @@ class TestGetWorkout(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_cycling_user_1.short_id}', f'/api/workouts/{workout_cycling_user_1.short_id}',
@ -971,65 +1016,66 @@ class TestGetWorkout(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_cycling_user_2.short_id}', f'/api/workouts/{workout_cycling_user_2.short_id}',
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
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()) 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(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
random_short_id = get_random_short_id() random_short_id = get_random_short_id()
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{random_short_id}/gpx', f'/api/workouts/{random_short_id}/gpx',
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(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
random_short_id = get_random_short_id() random_short_id = get_random_short_id()
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{random_short_id}/chart_data', f'/api/workouts/{random_short_id}/chart_data',
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(
@ -1040,19 +1086,17 @@ class TestGetWorkout(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_short_id = workout_cycling_user_1.short_id workout_short_id = workout_cycling_user_1.short_id
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_short_id}/gpx', f'/api/workouts/{workout_short_id}/gpx',
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(
@ -1063,19 +1107,17 @@ class TestGetWorkout(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_short_id = workout_cycling_user_1.short_id workout_short_id = workout_cycling_user_1.short_id
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_short_id}/chart_data', f'/api/workouts/{workout_short_id}/chart_data',
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
@ -1086,20 +1128,16 @@ class TestGetWorkout(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_cycling_user_1.gpx = "some path" workout_cycling_user_1.gpx = "some path"
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_cycling_user_1.short_id}/gpx', f'/api/workouts/{workout_cycling_user_1.short_id}/gpx',
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
@ -1110,35 +1148,30 @@ class TestGetWorkout(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_cycling_user_1.gpx = 'some path' workout_cycling_user_1.gpx = 'some path'
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_cycling_user_1.short_id}/chart_data', f'/api/workouts/{workout_cycling_user_1.short_id}/chart_data',
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(
self, app: Flask, user_1: User self, app: Flask, user_1: User
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
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):
@ -1147,17 +1180,16 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
app: Flask, app: Flask,
user_1: User, user_1: User,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{get_random_short_id()}/gpx/download', f'/api/workouts/{get_random_short_id()}/gpx/download',
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,
@ -1166,17 +1198,16 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_cycling_user_1.short_id}/gpx/download', f'/api/workouts/{workout_cycling_user_1.short_id}/gpx/download',
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,
@ -1186,17 +1217,16 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.get( response = client.get(
f'/api/workouts/{workout_cycling_user_2.short_id}/gpx/download', f'/api/workouts/{workout_cycling_user_2.short_id}/gpx/download',
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,
@ -1209,7 +1239,9 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
workout_cycling_user_1.gpx = gpx_file_path workout_cycling_user_1.gpx = gpx_file_path
with patch('fittrackee.workouts.workouts.send_from_directory') as mock: with patch('fittrackee.workouts.workouts.send_from_directory') as mock:
mock.return_value = 'file' mock.return_value = 'file'
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
client.get( client.get(
( (

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
@ -210,7 +209,9 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
def test_it_adds_an_workout_with_gpx_file( def test_it_adds_an_workout_with_gpx_file(
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -238,7 +239,9 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
gpx_file_wo_name: str, gpx_file_wo_name: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -270,7 +273,9 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
gpx_file_wo_name: str, gpx_file_wo_name: str,
) -> None: ) -> None:
user_1.timezone = 'Europe/Paris' user_1.timezone = 'Europe/Paris'
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -311,7 +316,9 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
gpx_file: str, gpx_file: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -339,7 +346,9 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
gpx_file: str, gpx_file: str,
static_map_get_mock: Mock, static_map_get_mock: Mock,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
client.post( client.post(
'/api/workouts', '/api/workouts',
data=dict( data=dict(
@ -369,7 +378,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
static_map_get_mock: Mock, static_map_get_mock: Mock,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_default_static_map app_default_static_map, user_1.email
) )
client.post( client.post(
'/api/workouts', '/api/workouts',
@ -398,7 +407,9 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
gpx_file_wo_track: str, gpx_file_wo_track: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -412,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(
@ -425,7 +433,9 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
gpx_file_invalid_xml: str, gpx_file_invalid_xml: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -442,16 +452,15 @@ 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(
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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -465,15 +474,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -486,15 +494,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -508,15 +515,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -527,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,
@ -540,7 +543,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
gpx_file: str, gpx_file: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_max_file_size app_with_max_file_size, user_1.email
) )
response = client.post( response = client.post(
@ -554,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
@ -568,7 +572,9 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_adds_an_workout_without_gpx( def test_it_adds_an_workout_without_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -593,7 +599,9 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_returns_400_if_workout_date_is_missing( def test_it_returns_400_if_workout_date_is_missing(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -602,15 +610,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -626,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,
@ -638,7 +642,9 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
sport_2_running: Sport, sport_2_running: Sport,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -690,7 +696,9 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
file_path = os.path.join(app.root_path, 'tests/files/gpx_test.zip') file_path = os.path.join(app.root_path, 'tests/files/gpx_test.zip')
# 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file # 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file
with open(file_path, 'rb') as zip_file: with open(file_path, 'rb') as zip_file:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -719,7 +727,9 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
# 'gpx_test_folder.zip' contains 3 gpx files (same data) and 1 non-gpx # 'gpx_test_folder.zip' contains 3 gpx files (same data) and 1 non-gpx
# file in a folder # file in a folder
with open(file_path, 'rb') as zip_file: with open(file_path, 'rb') as zip_file:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -733,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(
@ -746,7 +754,9 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
) )
# 'gpx_test_incorrect.zip' contains 2 gpx files, one is incorrect # 'gpx_test_incorrect.zip' contains 2 gpx files, one is incorrect
with open(file_path, 'rb') as zip_file: with open(file_path, 'rb') as zip_file:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -760,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(
@ -778,7 +785,7 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
# 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file # 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file
with open(file_path, 'rb') as zip_file: with open(file_path, 'rb') as zip_file:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_max_workouts app_with_max_workouts, user_1.email
) )
client.post( client.post(
@ -811,7 +818,7 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
# 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file # 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file
with open(file_path, 'rb') as zip_file: with open(file_path, 'rb') as zip_file:
client, auth_token = self.get_test_client_and_auth_token( client, auth_token = self.get_test_client_and_auth_token(
app_with_max_zip_file_size app_with_max_zip_file_size, user_1.email
) )
response = client.post( response = client.post(
@ -824,21 +831,22 @@ 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
class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin): class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
def workout_assertion( def workout_assertion(
self, app: Flask, gpx_file: str, with_segments: bool self, app: Flask, user_1: User, gpx_file: str, with_segments: bool
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
data=dict( data=dict(
@ -905,19 +913,13 @@ 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
) -> None: ) -> None:
return self.workout_assertion(app, gpx_file, False) return self.workout_assertion(app, user_1, gpx_file, False)
def test_it_gets_an_workout_created_with_gpx_with_segments( def test_it_gets_an_workout_created_with_gpx_with_segments(
self, self,
@ -926,12 +928,16 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
gpx_file_with_segments: str, gpx_file_with_segments: str,
) -> None: ) -> None:
return self.workout_assertion(app, gpx_file_with_segments, True) return self.workout_assertion(
app, user_1, gpx_file_with_segments, True
)
def test_it_gets_chart_data_for_an_workout_created_with_gpx( def test_it_gets_chart_data_for_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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -960,7 +966,9 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
def test_it_gets_segment_chart_data_for_an_workout_created_with_gpx( def test_it_gets_segment_chart_data_for_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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -994,7 +1002,9 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
gpx_file: str, gpx_file: str,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
data=dict( data=dict(
@ -1022,15 +1032,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -1050,16 +1059,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts', '/api/workouts',
@ -1079,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
@ -1090,7 +1096,9 @@ class TestPostAndGetWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_add_and_gets_an_workout_wo_gpx( def test_it_add_and_gets_an_workout_wo_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -1121,7 +1129,9 @@ class TestPostAndGetWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_adds_and_gets_an_workout_wo_gpx_notes( def test_it_adds_and_gets_an_workout_wo_gpx_notes(
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -1156,7 +1166,9 @@ class TestPostAndGetWorkoutUsingTimezones(ApiTestCaseMixin):
self, app: Flask, user_1: User, sport_1_cycling: Sport self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None: ) -> None:
user_1.timezone = 'Europe/Paris' user_1.timezone = 'Europe/Paris'
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.post( response = client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -1194,7 +1206,9 @@ class TestPostAndGetWorkoutUsingTimezones(ApiTestCaseMixin):
def test_it_adds_and_gets_workouts_date_filter_with_timezone_new_york( def test_it_adds_and_gets_workouts_date_filter_with_timezone_new_york(
self, app: Flask, user_1_full: User, sport_1_cycling: Sport self, app: Flask, user_1_full: User, sport_1_cycling: Sport
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_full.email
)
client.post( client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',
@ -1234,7 +1248,9 @@ class TestPostAndGetWorkoutUsingTimezones(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_paris.email
)
client.post( client.post(
'/api/workouts/no_gpx', '/api/workouts/no_gpx',

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):
@ -250,7 +238,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_short_id = workout_cycling_user_1.short_id workout_short_id = workout_cycling_user_1.short_id
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_short_id}', f'/api/workouts/{workout_short_id}',
@ -337,7 +327,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_short_id = workout_cycling_user_1.short_id workout_short_id = workout_cycling_user_1.short_id
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_short_id}', f'/api/workouts/{workout_short_id}',
@ -361,7 +353,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
) -> None: ) -> None:
workout_short_id = workout_cycling_user_1.short_id workout_short_id = workout_cycling_user_1.short_id
workout_cycling_user_1.notes = uuid4().hex workout_cycling_user_1.notes = uuid4().hex
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_short_id}', f'/api/workouts/{workout_short_id}',
@ -384,7 +378,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_2: Workout, workout_cycling_user_2: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_cycling_user_2.short_id}', f'/api/workouts/{workout_cycling_user_2.short_id}',
@ -401,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,
@ -415,7 +408,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_short_id = workout_cycling_user_1.short_id workout_short_id = workout_cycling_user_1.short_id
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1_paris.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_short_id}', f'/api/workouts/{workout_short_id}',
@ -488,7 +483,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
workout_short_id = workout_cycling_user_1.short_id workout_short_id = workout_cycling_user_1.short_id
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_short_id}', f'/api/workouts/{workout_short_id}',
@ -551,7 +548,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_cycling_user_1.short_id}', f'/api/workouts/{workout_cycling_user_1.short_id}',
@ -560,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,
@ -572,7 +568,9 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{workout_cycling_user_1.short_id}', f'/api/workouts/{workout_cycling_user_1.short_id}',
content_type='application/json', content_type='application/json',
@ -587,19 +585,14 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.patch( response = client.patch(
f'/api/workouts/{get_random_short_id()}', f'/api/workouts/{get_random_short_id()}',
content_type='application/json', content_type='application/json',
@ -614,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,22 +54,21 @@ 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
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
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(
@ -86,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):
@ -104,7 +96,9 @@ class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin):
sport_1_cycling: Sport, sport_1_cycling: Sport,
workout_cycling_user_1: Workout, workout_cycling_user_1: Workout,
) -> None: ) -> None:
client, auth_token = self.get_test_client_and_auth_token(app) client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete( response = client.delete(
f'/api/workouts/{workout_cycling_user_1.short_id}', f'/api/workouts/{workout_cycling_user_1.short_id}',
headers=dict(Authorization=f'Bearer {auth_token}'), headers=dict(Authorization=f'Bearer {auth_token}'),
@ -133,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']