API - refactor tests

This commit is contained in:
Sam 2021-02-20 23:20:20 +01:00
parent bb8491f84d
commit 33bbe8c736
12 changed files with 611 additions and 2089 deletions

View File

@ -0,0 +1,25 @@
import json
from typing import Tuple
from flask import Flask
from flask.testing import FlaskClient
class ApiTestCaseMixin:
@staticmethod
def get_test_client_and_auth_token(
app: Flask, as_admin: bool = False
) -> Tuple[FlaskClient, str]:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(
email='admin@example.com' if as_admin else 'test@test.com',
password='12345678',
)
),
content_type='application/json',
)
auth_token = json.loads(resp_login.data.decode())['auth_token']
return client, auth_token

View File

@ -4,24 +4,18 @@ from flask import Flask
from fittrackee.users.models import User
from ..api_test_case import ApiTestCaseMixin
class TestGetConfig:
class TestGetConfig(ApiTestCaseMixin):
def test_it_gets_application_config(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/config',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -41,22 +35,14 @@ class TestGetConfig:
def test_it_returns_error_if_application_has_no_config(
self, app_no_config: Flask, user_1_admin: User
) -> None:
client = app_no_config.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_no_config, as_admin=True
)
response = client.get(
'/api/config',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -67,22 +53,14 @@ class TestGetConfig:
def test_it_returns_error_if_application_has_several_config(
self, app: Flask, app_config: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/config',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -91,26 +69,18 @@ class TestGetConfig:
assert 'Error on getting configuration.' in data['message']
class TestUpdateConfig:
class TestUpdateConfig(ApiTestCaseMixin):
def test_it_updates_config_when_user_is_admin(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -125,13 +95,8 @@ class TestUpdateConfig:
def test_it_updates_all_config(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
@ -145,10 +110,7 @@ class TestUpdateConfig:
max_users=50,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -163,21 +125,13 @@ class TestUpdateConfig:
def test_it_returns_403_when_user_is_not_an_admin(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -189,23 +143,15 @@ class TestUpdateConfig:
def test_it_returns_400_if_invalid_is_payload(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict()),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -216,23 +162,15 @@ class TestUpdateConfig:
def test_it_returns_error_on_update_if_application_has_no_config(
self, app_no_config: Flask, user_1_admin: User
) -> None:
client = app_no_config.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_no_config, as_admin=True
)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -243,13 +181,8 @@ class TestUpdateConfig:
def test_it_raises_error_if_archive_max_size_is_below_files_max_size(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
@ -263,10 +196,7 @@ class TestUpdateConfig:
max_users=50,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -280,13 +210,8 @@ class TestUpdateConfig:
def test_it_raises_error_if_archive_max_size_equals_0(
self, app_with_max_file_size_equals_0: Flask, user_1_admin: User
) -> None:
client = app_with_max_file_size_equals_0.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_max_file_size_equals_0, as_admin=True
)
response = client.patch(
@ -297,10 +222,7 @@ class TestUpdateConfig:
max_zip_file_size=0,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -314,13 +236,8 @@ class TestUpdateConfig:
def test_it_raises_error_if_files_max_size_equals_0(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
@ -331,10 +248,7 @@ class TestUpdateConfig:
max_single_file_size=0,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -348,13 +262,8 @@ class TestUpdateConfig:
def test_it_raises_error_if_gpx_limit_import_equals_0(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
@ -365,10 +274,7 @@ class TestUpdateConfig:
gpx_limit_import=0,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())

View File

@ -13,6 +13,7 @@ def get_app_config(
max_workouts: Optional[int] = None,
max_single_file_size: Optional[Union[int, float]] = None,
max_zip_file_size: Optional[Union[int, float]] = None,
max_users: Optional[int] = None,
) -> Optional[AppConfig]:
if with_config:
config = AppConfig()
@ -27,7 +28,7 @@ def get_app_config(
* 1024
* 1024
)
config.max_users = 100
config.max_users = 100 if max_users is None else max_users
db.session.add(config)
db.session.commit()
return config
@ -39,6 +40,7 @@ def get_app(
max_workouts: Optional[int] = None,
max_single_file_size: Optional[Union[int, float]] = None,
max_zip_file_size: Optional[Union[int, float]] = None,
max_users: Optional[int] = None,
) -> Generator:
app = create_app()
with app.app_context():
@ -49,6 +51,7 @@ def get_app(
max_workouts,
max_single_file_size,
max_zip_file_size,
max_users,
)
if app_db_config:
update_app_config_from_database(app, app_db_config)
@ -101,6 +104,12 @@ def app_with_max_zip_file_size(monkeypatch: pytest.MonkeyPatch) -> Generator:
yield from get_app(with_config=True, max_zip_file_size=0.001)
@pytest.fixture
def app_with_3_users_max(monkeypatch: pytest.MonkeyPatch) -> Generator:
monkeypatch.setenv('EMAIL_URL', 'smtp://none:none@0.0.0.0:1025')
yield from get_app(with_config=True, max_users=3)
@pytest.fixture
def app_no_config() -> Generator:
yield from get_app(with_config=False)

View File

@ -10,6 +10,8 @@ from fittrackee.users.models import User
from fittrackee.users.utils_token import get_user_token
from fittrackee.workouts.models import Sport, Workout
from ..api_test_case import ApiTestCaseMixin
class TestUserRegistration:
def test_user_can_register(self, app: Flask) -> None:
@ -356,21 +358,14 @@ class TestUserLogin:
assert data['message'] == 'Invalid credentials.'
class TestUserLogout:
class TestUserLogout(ApiTestCaseMixin):
def test_user_can_logout(self, app: Flask, user_1: User) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/auth/logout',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -381,20 +376,13 @@ class TestUserLogout:
def test_it_returns_error_with_expired_token(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
now = datetime.utcnow()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
with freeze_time(now + timedelta(seconds=4)):
response = client.get(
'/api/auth/logout',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
@ -420,23 +408,17 @@ class TestUserLogout:
assert response.status_code == 401
class TestUserProfile:
class TestUserProfile(ApiTestCaseMixin):
def test_it_returns_user_minimal_profile(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/auth/profile',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['data'] is not None
@ -457,19 +439,13 @@ class TestUserProfile:
def test_it_returns_user_full_profile(
self, app: Flask, user_1_full: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/auth/profile',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['data'] is not None
@ -501,19 +477,13 @@ class TestUserProfile:
workout_cycling_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/auth/profile',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['data'] is not None
@ -540,14 +510,10 @@ class TestUserProfile:
assert response.status_code == 401
class TestUserProfileUpdate:
class TestUserProfileUpdate(ApiTestCaseMixin):
def test_it_updates_user_profile(self, app: Flask, user_1: User) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/profile/edit',
content_type='application/json',
@ -565,11 +531,9 @@ class TestUserProfileUpdate:
language='fr',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['message'] == 'User profile updated.'
@ -595,12 +559,8 @@ class TestUserProfileUpdate:
def test_it_updates_user_profile_without_password(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/profile/edit',
content_type='application/json',
@ -616,11 +576,9 @@ class TestUserProfileUpdate:
language='fr',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['message'] == 'User profile updated.'
@ -646,21 +604,15 @@ class TestUserProfileUpdate:
def test_it_returns_error_if_fields_are_missing(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/profile/edit',
content_type='application/json',
data=json.dumps(dict(first_name='John')),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'Invalid payload.'
@ -669,21 +621,15 @@ class TestUserProfileUpdate:
def test_it_returns_error_if_payload_is_empty(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/profile/edit',
content_type='application/json',
data=json.dumps(dict()),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'Invalid payload.' in data['message']
@ -692,12 +638,8 @@ class TestUserProfileUpdate:
def test_it_returns_error_if_passwords_mismatch(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/profile/edit',
content_type='application/json',
@ -715,11 +657,9 @@ class TestUserProfileUpdate:
language='en',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert (
@ -731,12 +671,8 @@ class TestUserProfileUpdate:
def test_it_returns_error_if_password_confirmation_is_missing(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/profile/edit',
content_type='application/json',
@ -753,11 +689,9 @@ class TestUserProfileUpdate:
language='en',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert (
@ -767,23 +701,19 @@ class TestUserProfileUpdate:
assert response.status_code == 400
class TestUserPicture:
class TestUserPicture(ApiTestCaseMixin):
def test_it_updates_user_picture(self, app: Flask, user_1: User) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/picture',
data=dict(file=(BytesIO(b'avatar'), 'avatar.png')),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['message'] == 'User picture updated.'
@ -795,10 +725,10 @@ class TestUserPicture:
data=dict(file=(BytesIO(b'avatar2'), 'avatar2.png')),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
assert data['status'] == 'success'
assert data['message'] == 'User picture updated.'
@ -809,20 +739,16 @@ class TestUserPicture:
def test_it_returns_error_if_file_is_missing(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/picture',
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
assert data['status'] == 'fail'
assert data['message'] == 'No file part.'
@ -831,21 +757,17 @@ class TestUserPicture:
def test_it_returns_error_if_file_is_invalid(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/auth/picture',
data=dict(file=(BytesIO(b'avatar'), 'avatar.bmp')),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
assert data['status'] == 'fail'
assert data['message'] == 'File extension not allowed.'
@ -858,11 +780,8 @@ class TestUserPicture:
sport_1_cycling: Sport,
gpx_file: str,
) -> None:
client = app_with_max_file_size.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_max_file_size
)
response = client.post(
@ -872,10 +791,10 @@ class TestUserPicture:
),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
print('data', data)
assert response.status_code == 413
@ -893,11 +812,8 @@ class TestUserPicture:
sport_1_cycling: Sport,
gpx_file: str,
) -> None:
client = app_with_max_zip_file_size.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_max_zip_file_size
)
response = client.post(
@ -907,10 +823,10 @@ class TestUserPicture:
),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
print('data', data)
assert response.status_code == 413
@ -924,26 +840,13 @@ class TestUserPicture:
class TestRegistrationConfiguration:
def test_it_returns_error_if_it_exceeds_max_users(
self, app: Flask, user_1_admin: User, user_2: User, user_3: User
self,
app_with_3_users_max: Flask,
user_1_admin: User,
user_2: User,
user_3: User,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
)
client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(max_users=3, registration=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
client = app_with_3_users_max.test_client()
response = client.post(
'/api/auth/register',
@ -966,13 +869,11 @@ class TestRegistrationConfiguration:
def test_it_disables_registration_on_user_registration(
self,
app_no_config: Flask,
app_config: Flask,
app_with_3_users_max: Flask,
user_1_admin: User,
user_2: User,
) -> None:
app_config.max_users = 3
client = app_no_config.test_client()
client = app_with_3_users_max.test_client()
client.post(
'/api/auth/register',
data=json.dumps(
@ -985,6 +886,7 @@ class TestRegistrationConfiguration:
),
content_type='application/json',
)
response = client.post(
'/api/auth/register',
data=json.dumps(
@ -997,6 +899,7 @@ class TestRegistrationConfiguration:
),
content_type='application/json',
)
assert response.status_code == 403
data = json.loads(response.data.decode())
assert data['status'] == 'error'
@ -1004,13 +907,10 @@ class TestRegistrationConfiguration:
def test_it_does_not_disable_registration_on_user_registration(
self,
app_no_config: Flask,
app_config: Flask,
user_1_admin: User,
user_2: User,
app_with_3_users_max: Flask,
user_1: User,
) -> None:
app_config.max_users = 4
client = app_no_config.test_client()
client = app_with_3_users_max.test_client()
client.post(
'/api/auth/register',
data=json.dumps(

View File

@ -8,25 +8,19 @@ from flask import Flask
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from ..api_test_case import ApiTestCaseMixin
class TestGetUser:
class TestGetUser(ApiTestCaseMixin):
def test_it_gets_single_user_without_workouts(
self, app: Flask, user_1: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/users/{user_2.username}',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -61,20 +55,12 @@ class TestGetUser:
workout_cycling_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/users/{user_1.username}',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -103,19 +89,12 @@ class TestGetUser:
def test_it_returns_error_if_user_does_not_exist(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users/not_existing',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -124,23 +103,15 @@ class TestGetUser:
assert 'User does not exist.' in data['message']
class TestGetUsers:
class TestGetUsers(ApiTestCaseMixin):
def test_it_get_users_list(
self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -200,19 +171,11 @@ class TestGetUsers:
workout_running_user_1: Workout,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -265,19 +228,11 @@ class TestGetUsers:
user_2: User,
user_3: User,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?page=1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -300,19 +255,11 @@ class TestGetUsers:
user_2: User,
user_3: User,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -334,19 +281,11 @@ class TestGetUsers:
user_2: User,
user_3: User,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -368,19 +307,11 @@ class TestGetUsers:
user_2: User,
user_3: User,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?per_page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -402,19 +333,11 @@ class TestGetUsers:
user_2: User,
user_3: User,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?page=2&per_page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -432,18 +355,11 @@ class TestGetUsers:
def test_it_gets_users_list_ordered_by_username(
self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?order_by=username',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -464,19 +380,11 @@ class TestGetUsers:
def test_it_gets_users_list_ordered_by_username_ascending(
self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?order_by=username&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -497,19 +405,11 @@ class TestGetUsers:
def test_it_gets_users_list_ordered_by_username_descending(
self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?order_by=username&order=desc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -533,21 +433,13 @@ class TestGetUsers:
user_2.created_at = datetime.utcnow() - timedelta(days=1)
user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow()
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/users?order_by=created_at',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -571,21 +463,13 @@ class TestGetUsers:
user_2.created_at = datetime.utcnow() - timedelta(days=1)
user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow()
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/users?order_by=created_at&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -609,21 +493,13 @@ class TestGetUsers:
user_2.created_at = datetime.utcnow() - timedelta(days=1)
user_3.created_at = datetime.utcnow() - timedelta(hours=1)
user_1_admin.created_at = datetime.utcnow()
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/users?order_by=created_at&order=desc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -644,21 +520,13 @@ class TestGetUsers:
def test_it_gets_users_list_ordered_by_admin_rights(
self, app: Flask, user_2: User, user_1_admin: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/users?order_by=admin',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -679,21 +547,13 @@ class TestGetUsers:
def test_it_gets_users_list_ordered_by_admin_rights_ascending(
self, app: Flask, user_2: User, user_1_admin: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/users?order_by=admin&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -714,21 +574,13 @@ class TestGetUsers:
def test_it_gets_users_list_ordered_by_admin_rights_descending(
self, app: Flask, user_2: User, user_3: User, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/users?order_by=admin&order=desc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -755,19 +607,11 @@ class TestGetUsers:
sport_1_cycling: Sport,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?order_by=workouts_count',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -797,19 +641,11 @@ class TestGetUsers:
sport_1_cycling: Sport,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?order_by=workouts_count&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -839,19 +675,11 @@ class TestGetUsers:
sport_1_cycling: Sport,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?order_by=workouts_count&order=desc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -875,19 +703,11 @@ class TestGetUsers:
def test_it_gets_users_list_filtering_on_username(
self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?q=toto',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -906,19 +726,11 @@ class TestGetUsers:
def test_it_returns_empty_users_list_filtering_on_username(
self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?q=not_existing',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -936,19 +748,11 @@ class TestGetUsers:
def test_it_users_list_with_complex_query(
self, app: Flask, user_1: User, user_2: User, user_3: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/users?order_by=username&order=desc&page=2&per_page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -991,27 +795,19 @@ class TestGetUserPicture:
assert 'User does not exist.' in data['message']
class TestUpdateUser:
class TestUpdateUser(ApiTestCaseMixin):
def test_it_adds_admin_rights_to_a_user(
self, app: Flask, user_1_admin: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/users/toto',
content_type='application/json',
data=json.dumps(dict(admin=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1025,23 +821,15 @@ class TestUpdateUser:
def test_it_removes_admin_rights_to_a_user(
self, app: Flask, user_1_admin: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/users/toto',
content_type='application/json',
data=json.dumps(dict(admin=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1056,23 +844,15 @@ class TestUpdateUser:
def test_it_returns_error_if_payload_for_admin_rights_is_empty(
self, app: Flask, user_1_admin: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/users/toto',
content_type='application/json',
data=json.dumps(dict()),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1083,23 +863,15 @@ class TestUpdateUser:
def test_it_returns_error_if_payload_for_admin_rights_is_invalid(
self, app: Flask, user_1_admin: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/users/toto',
content_type='application/json',
data=json.dumps(dict(admin="")),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1113,21 +885,13 @@ class TestUpdateUser:
def test_it_returns_error_if_user_can_not_change_admin_rights(
self, app: Flask, user_1: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
'/api/users/toto',
content_type='application/json',
data=json.dumps(dict(admin=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1136,23 +900,15 @@ class TestUpdateUser:
assert 'You do not have permissions.' in data['message']
class TestDeleteUser:
class TestDeleteUser(ApiTestCaseMixin):
def test_user_can_delete_its_own_account(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.delete(
'/api/users/test',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204
@ -1160,12 +916,7 @@ class TestDeleteUser:
def test_user_with_workout_can_delete_its_own_account(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
client.post(
'/api/workouts',
data=dict(
@ -1174,17 +925,13 @@ class TestDeleteUser:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
response = client.delete(
'/api/users/test',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204
@ -1192,28 +939,19 @@ class TestDeleteUser:
def test_user_with_picture_can_delete_its_own_account(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
client.post(
'/api/auth/picture',
data=dict(file=(BytesIO(b'avatar'), 'avatar.png')),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
response = client.delete(
'/api/users/test',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204
@ -1221,19 +959,11 @@ class TestDeleteUser:
def test_user_can_not_delete_another_user_account(
self, app: Flask, user_1: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.delete(
'/api/users/toto',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1244,19 +974,11 @@ class TestDeleteUser:
def test_it_returns_error_when_deleting_non_existing_user(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.delete(
'/api/users/not_existing',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1267,21 +989,13 @@ class TestDeleteUser:
def test_admin_can_delete_another_user_account(
self, app: Flask, user_1_admin: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.delete(
'/api/users/toto',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204
@ -1289,21 +1003,13 @@ class TestDeleteUser:
def test_admin_can_delete_its_own_account(
self, app: Flask, user_1_admin: User, user_2_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.delete(
'/api/users/admin',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204
@ -1311,20 +1017,13 @@ class TestDeleteUser:
def test_admin_can_not_delete_its_own_account_if_no_other_admin(
self, app: Flask, user_1_admin: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.delete(
'/api/users/admin',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1337,29 +1036,19 @@ class TestDeleteUser:
def test_it_enables_registration_on_user_delete(
self,
app_no_config: Flask,
app_config: Flask,
app_with_3_users_max: Flask,
user_1_admin: User,
user_2: User,
user_3: User,
) -> None:
app_config.max_users = 3
client = app_no_config.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_3_users_max, as_admin=True
)
client.delete(
'/api/users/toto',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.post(
'/api/auth/register',
data=json.dumps(
@ -1376,28 +1065,19 @@ class TestDeleteUser:
def test_it_does_not_enable_registration_on_user_delete(
self,
app_no_config: Flask,
app_config: Flask,
app_with_3_users_max: Flask,
user_1_admin: User,
user_2: User,
user_3: User,
user_1_paris: User,
) -> None:
app_config.max_users = 2
client = app_no_config.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_3_users_max, as_admin=True
)
client.delete(
'/api/users/toto',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.post(
'/api/auth/register',

View File

@ -5,8 +5,10 @@ from flask import Flask
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from ..api_test_case import ApiTestCaseMixin
class TestGetRecords:
class TestGetRecords(ApiTestCaseMixin):
def test_it_gets_records_for_authenticated_user(
self,
app: Flask,
@ -17,21 +19,14 @@ class TestGetRecords:
workout_cycling_user_1: Workout,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
@ -97,21 +92,14 @@ class TestGetRecords:
sport_2_running: Sport,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 0
@ -123,12 +111,8 @@ class TestGetRecords:
sport_1_cycling: Sport,
sport_2_running: Sport,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
client.post(
'/api/workouts/no_gpx',
content_type='application/json',
@ -141,20 +125,15 @@ class TestGetRecords:
title='Workout test',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 0
@ -162,12 +141,7 @@ class TestGetRecords:
def test_it_gets_updated_records_after_workouts_post_and_patch(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
content_type='application/json',
@ -180,26 +154,20 @@ class TestGetRecords:
title='Workout test 1',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_1_short_id = data['data']['workouts'][0]['id']
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['records']) == 4
assert (
'Mon, 14 May 2018 14:05:00 GMT'
== data['data']['records'][0]['workout_date']
@ -254,19 +222,13 @@ class TestGetRecords:
title='Workout test 2',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_2_short_id = data['data']['workouts'][0]['id']
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -327,19 +289,13 @@ class TestGetRecords:
title='Workout test 3',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_3_short_id = data['data']['workouts'][0]['id']
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -393,17 +349,11 @@ class TestGetRecords:
f'/api/workouts/{workout_3_short_id}',
content_type='application/json',
data=json.dumps(dict(duration=4000)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -454,17 +404,11 @@ class TestGetRecords:
# delete workout 2 => AS and MS record update
client.delete(
f'/api/workouts/{workout_2_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -526,19 +470,13 @@ class TestGetRecords:
title='Workout test 4',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_4_short_id = data['data']['workouts'][0]['id']
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -602,19 +540,13 @@ class TestGetRecords:
title='Workout test 5',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_5_short_id = data['data']['workouts'][0]['id']
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -665,38 +597,23 @@ class TestGetRecords:
# delete all workouts - no more records
client.delete(
f'/api/workouts/{workout_1_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
client.delete(
f'/api/workouts/{workout_3_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
client.delete(
f'/api/workouts/{workout_4_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
client.delete(
f'/api/workouts/{workout_5_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -711,12 +628,8 @@ class TestGetRecords:
sport_1_cycling: Sport,
sport_2_running: Sport,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
content_type='application/json',
@ -729,10 +642,7 @@ class TestGetRecords:
title='Workout test 1',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_1_short_id = data['data']['workouts'][0]['id']
@ -748,10 +658,7 @@ class TestGetRecords:
title='Workout test 2',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_2_short_id = data['data']['workouts'][0]['id']
@ -767,10 +674,7 @@ class TestGetRecords:
title='Workout test 3',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.post(
'/api/workouts/no_gpx',
@ -784,19 +688,13 @@ class TestGetRecords:
title='Workout test 4',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_4_short_id = data['data']['workouts'][0]['id']
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -888,17 +786,11 @@ class TestGetRecords:
f'/api/workouts/{workout_2_short_id}',
content_type='application/json',
data=json.dumps(dict(sport_id=1)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.get(
'/api/records',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())

View File

@ -5,6 +5,8 @@ from flask import Flask
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from ..api_test_case import ApiTestCaseMixin
expected_sport_1_cycling_result = {
'id': 1,
'label': 'Cycling',
@ -35,7 +37,7 @@ expected_sport_1_cycling_inactive_admin_result = (
expected_sport_1_cycling_inactive_admin_result['has_workouts'] = False
class TestGetSports:
class TestGetSports(ApiTestCaseMixin):
def test_it_gets_all_sports(
self,
app: Flask,
@ -43,19 +45,11 @@ class TestGetSports:
sport_1_cycling: Sport,
sport_2_running: Sport,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/sports',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -72,19 +66,11 @@ class TestGetSports:
sport_1_cycling_inactive: Sport,
sport_2_running: Sport,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/sports',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -104,21 +90,13 @@ class TestGetSports:
sport_1_cycling_inactive: Sport,
sport_2_running: Sport,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/sports',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -134,23 +112,15 @@ class TestGetSports:
)
class TestGetSport:
class TestGetSport(ApiTestCaseMixin):
def test_it_gets_a_sport(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -162,19 +132,11 @@ class TestGetSport:
def test_it_returns_404_if_sport_does_not_exist(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -185,24 +147,16 @@ class TestGetSport:
def test_it_gets_a_inactive_sport(
self, app: Flask, user_1: User, sport_1_cycling_inactive: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['sports']) == 1
assert (
data['data']['sports'][0]
@ -212,26 +166,18 @@ class TestGetSport:
def test_it_get_an_inactive_sport_with_admin_rights(
self, app: Flask, user_1_admin: User, sport_1_cycling_inactive: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/sports/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['sports']) == 1
assert (
data['data']['sports'][0]
@ -239,27 +185,19 @@ class TestGetSport:
)
class TestUpdateSport:
class TestUpdateSport(ApiTestCaseMixin):
def test_it_disables_a_sport(
self, app: Flask, user_1_admin: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -273,23 +211,15 @@ class TestUpdateSport:
self, app: Flask, user_1_admin: User, sport_1_cycling: Sport
) -> None:
sport_1_cycling.is_active = False
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -306,23 +236,15 @@ class TestUpdateSport:
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -340,23 +262,15 @@ class TestUpdateSport:
workout_cycling_user_1: Workout,
) -> None:
sport_1_cycling.is_active = False
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=True)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -369,23 +283,16 @@ class TestUpdateSport:
def test_returns_error_if_user_has_no_admin_rights(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 403
assert 'success' not in data['status']
assert 'error' in data['status']
@ -394,23 +301,15 @@ class TestUpdateSport:
def test_returns_error_if_payload_is_invalid(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict()),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -421,25 +320,18 @@ class TestUpdateSport:
def test_it_returns_error_if_sport_does_not_exist(
self, app: Flask, user_1_admin: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.patch(
'/api/sports/1',
content_type='application/json',
data=json.dumps(dict(is_active=False)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert len(data['data']['sports']) == 0

View File

@ -5,24 +5,18 @@ from flask import Flask
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from ..api_test_case import ApiTestCaseMixin
class TestGetStatsByTime:
class TestGetStatsByTime(ApiTestCaseMixin):
def test_it_gets_no_stats_when_user_has_no_workouts(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -33,19 +27,11 @@ class TestGetStatsByTime:
def test_it_returns_error_when_user_does_not_exists(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/stats/1000/by_time',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -62,19 +48,14 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?from="2018-04-01&to=2018-04-30', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
(
f'/api/stats/{user_1.username}/by_time'
f'?from="2018-04-01&to=2018-04-30'
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -94,19 +75,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=day', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -123,19 +96,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -172,19 +137,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -214,20 +171,12 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1_paris.username}/by_time?'
f'from=2018-04-01&to=2018-04-30',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -257,19 +206,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?time=year',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -306,19 +247,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -348,19 +281,12 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1_paris.username}/by_time?from=2018-04-01&to=2018-04-30&time=year', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -390,19 +316,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?time=month',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -467,19 +385,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1_full.username}/by_time?time=month',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -544,19 +454,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=month', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -586,19 +488,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1_full.username}/by_time?time=week',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -663,19 +557,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=week', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -705,19 +591,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?time=weekm',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -782,19 +660,11 @@ class TestGetStatsByTime:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_time?from=2018-04-01&to=2018-04-30&time=weekm', # noqa
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -816,7 +686,7 @@ class TestGetStatsByTime:
}
class TestGetStatsBySport:
class TestGetStatsBySport(ApiTestCaseMixin):
def test_it_gets_stats_by_sport(
self,
app: Flask,
@ -826,19 +696,11 @@ class TestGetStatsBySport:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_sport',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -866,19 +728,11 @@ class TestGetStatsBySport:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id=1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -901,19 +755,11 @@ class TestGetStatsBySport:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/stats/1000/by_sport?sport_id=1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -930,19 +776,11 @@ class TestGetStatsBySport:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id=999',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -959,19 +797,11 @@ class TestGetStatsBySport:
seven_workouts_user_1: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/stats/{user_1.username}/by_sport?sport_id="999',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -983,25 +813,17 @@ class TestGetStatsBySport:
)
class TestGetAllStats:
class TestGetAllStats(ApiTestCaseMixin):
def test_it_returns_all_stats_when_users_have_no_workouts(
self, app: Flask, user_1_admin: User, user_2: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/stats/all',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1024,21 +846,13 @@ class TestGetAllStats:
workout_cycling_user_2: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='admin@example.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app, as_admin=True
)
response = client.get(
'/api/stats/all',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1061,19 +875,11 @@ class TestGetAllStats:
workout_cycling_user_2: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/stats/all',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())

View File

@ -7,10 +7,11 @@ from flask import Flask
from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from ..api_test_case import ApiTestCaseMixin
from .utils import get_random_short_id
class TestGetWorkouts:
class TestGetWorkouts(ApiTestCaseMixin):
def test_it_gets_all_workouts_for_authenticated_user(
self,
app: Flask,
@ -22,19 +23,11 @@ class TestGetWorkouts:
workout_cycling_user_2: Workout,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -104,7 +97,7 @@ class TestGetWorkouts:
assert 'Provide a valid auth token.' in data['message']
class TestGetWorkoutsWithPagination:
class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
def test_it_gets_workouts_with_default_pagination(
self,
app: Flask,
@ -112,19 +105,11 @@ class TestGetWorkoutsWithPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -151,19 +136,11 @@ class TestGetWorkoutsWithPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?page=1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -190,19 +167,11 @@ class TestGetWorkoutsWithPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -229,19 +198,11 @@ class TestGetWorkoutsWithPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?page=3',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -256,19 +217,11 @@ class TestGetWorkoutsWithPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?page=A',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -287,19 +240,11 @@ class TestGetWorkoutsWithPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?per_page=10',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -323,19 +268,11 @@ class TestGetWorkoutsWithPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?per_page=3',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -352,7 +289,7 @@ class TestGetWorkoutsWithPagination:
)
class TestGetWorkoutsWithOrder:
class TestGetWorkoutsWithOrder(ApiTestCaseMixin):
def test_it_gets_workouts_with_default_order(
self,
app: Flask,
@ -360,19 +297,11 @@ class TestGetWorkoutsWithOrder:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -395,19 +324,11 @@ class TestGetWorkoutsWithOrder:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -430,19 +351,11 @@ class TestGetWorkoutsWithOrder:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?order=desc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -459,7 +372,7 @@ class TestGetWorkoutsWithOrder:
)
class TestGetWorkoutsWithFilters:
class TestGetWorkoutsWithFilters(ApiTestCaseMixin):
def test_it_gets_workouts_with_date_filter(
self,
app: Flask,
@ -467,19 +380,11 @@ class TestGetWorkoutsWithFilters:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?from=2018-02-01&to=2018-02-28',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -506,19 +411,11 @@ class TestGetWorkoutsWithFilters:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?from=2018-03-01&to=2018-03-30',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -533,19 +430,11 @@ class TestGetWorkoutsWithFilters:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?from=2018-04-01',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -569,19 +458,11 @@ class TestGetWorkoutsWithFilters:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?to=2017-12-31',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -604,19 +485,11 @@ class TestGetWorkoutsWithFilters:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?distance_from=5&distance_to=8',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -639,19 +512,11 @@ class TestGetWorkoutsWithFilters:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?duration_from=00:52&duration_to=01:20',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -670,19 +535,11 @@ class TestGetWorkoutsWithFilters:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?ave_speed_from=5&ave_speed_to=10',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -705,19 +562,11 @@ class TestGetWorkoutsWithFilters:
) -> None:
workout_cycling_user_1.max_speed = 25
workout_running_user_1.max_speed = 11
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?max_speed_from=10&max_speed_to=20',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -738,19 +587,11 @@ class TestGetWorkoutsWithFilters:
sport_2_running: Sport,
workout_running_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?sport_id=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -763,7 +604,7 @@ class TestGetWorkoutsWithFilters:
)
class TestGetWorkoutsWithFiltersAndPagination:
class TestGetWorkoutsWithFiltersAndPagination(ApiTestCaseMixin):
def test_it_gets_page_2_with_date_filter(
self,
app: Flask,
@ -771,19 +612,11 @@ class TestGetWorkoutsWithFiltersAndPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?from=2017-01-01&page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -806,19 +639,11 @@ class TestGetWorkoutsWithFiltersAndPagination:
sport_1_cycling: Sport,
seven_workouts_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
'/api/workouts?from=2017-01-01&page=2&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -835,7 +660,7 @@ class TestGetWorkoutsWithFiltersAndPagination:
)
class TestGetWorkout:
class TestGetWorkout(ApiTestCaseMixin):
def test_it_gets_an_workout(
self,
app: Flask,
@ -843,19 +668,11 @@ class TestGetWorkout:
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{workout_cycling_user_1.short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -880,19 +697,11 @@ class TestGetWorkout:
sport_1_cycling: Sport,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{workout_cycling_user_2.short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -903,19 +712,11 @@ class TestGetWorkout:
def test_it_returns_404_if_workout_does_not_exist(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{get_random_short_id()}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -927,19 +728,11 @@ class TestGetWorkout:
self, app: Flask, user_1: User
) -> None:
random_short_id = get_random_short_id()
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{random_short_id}/gpx',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -952,19 +745,11 @@ class TestGetWorkout:
self, app: Flask, user_1: User
) -> None:
random_short_id = get_random_short_id()
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{random_short_id}/chart_data',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -981,19 +766,11 @@ class TestGetWorkout:
workout_cycling_user_1: Workout,
) -> None:
workout_short_id = workout_cycling_user_1.short_id
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{workout_short_id}/gpx',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1012,19 +789,11 @@ class TestGetWorkout:
workout_cycling_user_1: Workout,
) -> None:
workout_short_id = workout_cycling_user_1.short_id
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{workout_short_id}/chart_data',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1043,19 +812,11 @@ class TestGetWorkout:
workout_cycling_user_1: Workout,
) -> None:
workout_cycling_user_1.gpx = "some path"
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{workout_cycling_user_1.short_id}/gpx',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1075,19 +836,11 @@ class TestGetWorkout:
workout_cycling_user_1: Workout,
) -> None:
workout_cycling_user_1.gpx = 'some path'
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/{workout_cycling_user_1.short_id}/chart_data',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1102,18 +855,10 @@ class TestGetWorkout:
def test_it_returns_404_if_workout_has_no_map(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.get(
f'/api/workouts/map/{uuid4().hex}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())

View File

@ -10,6 +10,8 @@ from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from fittrackee.workouts.utils_id import decode_short_id
from ..api_test_case import ApiTestCaseMixin
def assert_workout_data_with_gpx(data: Dict) -> None:
assert 'creation_date' in data['data']['workouts'][0]
@ -201,16 +203,11 @@ def assert_workout_data_wo_gpx(data: Dict) -> None:
assert records[3]['value'] == 10.0
class TestPostWorkoutWithGpx:
class TestPostWorkoutWithGpx(ApiTestCaseMixin):
def test_it_adds_an_workout_with_gpx_file(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -220,8 +217,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -239,12 +235,7 @@ class TestPostWorkoutWithGpx:
sport_1_cycling: Sport,
gpx_file_wo_name: str,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -254,8 +245,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -277,12 +267,7 @@ class TestPostWorkoutWithGpx:
gpx_file_wo_name: str,
) -> None:
user_1.timezone = 'Europe/Paris'
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -292,8 +277,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -310,12 +294,7 @@ class TestPostWorkoutWithGpx:
def test_it_adds_get_an_workout_with_gpx_notes(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -325,8 +304,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
@ -344,12 +322,7 @@ class TestPostWorkoutWithGpx:
sport_1_cycling: Sport,
gpx_file_wo_track: str,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -359,8 +332,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -377,12 +349,7 @@ class TestPostWorkoutWithGpx:
sport_1_cycling: Sport,
gpx_file_invalid_xml: str,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -395,8 +362,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -409,12 +375,7 @@ class TestPostWorkoutWithGpx:
def test_it_returns_400_if_workout_gpx_has_invalid_extension(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -424,8 +385,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -437,12 +397,7 @@ class TestPostWorkoutWithGpx:
def test_it_returns_400_if_sport_id_is_not_provided(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -451,8 +406,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -464,12 +418,7 @@ class TestPostWorkoutWithGpx:
def test_it_returns_500_if_sport_id_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -479,8 +428,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -492,20 +440,14 @@ class TestPostWorkoutWithGpx:
def test_returns_400_if_no_gpx_file_is_provided(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
data=dict(data='{}'),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -521,11 +463,8 @@ class TestPostWorkoutWithGpx:
sport_1_cycling: Sport,
gpx_file: str,
) -> None:
client = app_with_max_file_size.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_max_file_size
)
response = client.post(
@ -536,8 +475,7 @@ class TestPostWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
@ -550,16 +488,11 @@ class TestPostWorkoutWithGpx:
assert 'data' not in data
class TestPostWorkoutWithoutGpx:
class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_adds_an_workout_without_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
@ -572,10 +505,7 @@ class TestPostWorkoutWithoutGpx:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -587,21 +517,13 @@ class TestPostWorkoutWithoutGpx:
def test_it_returns_400_if_workout_date_is_missing(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
content_type='application/json',
data=json.dumps(dict(sport_id=1, duration=3600, distance=10)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -612,12 +534,7 @@ class TestPostWorkoutWithoutGpx:
def test_it_returns_500_if_workout_format_is_invalid(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
@ -630,10 +547,7 @@ class TestPostWorkoutWithoutGpx:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -648,12 +562,7 @@ class TestPostWorkoutWithoutGpx:
sport_1_cycling: Sport,
sport_2_running: Sport,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
@ -667,10 +576,7 @@ class TestPostWorkoutWithoutGpx:
title='Workout test',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -701,21 +607,14 @@ class TestPostWorkoutWithoutGpx:
assert len(data['data']['workouts'][0]['records']) == 0
class TestPostWorkoutWithZipArchive:
class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
def test_it_adds_workouts_with_zip_archive(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
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
with open(file_path, 'rb') as zip_file:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='test@test.com', password='12345678')
),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -724,8 +623,7 @@ class TestPostWorkoutWithZipArchive:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -745,14 +643,7 @@ class TestPostWorkoutWithZipArchive:
# 'gpx_test_folder.zip' contains 3 gpx files (same data) and 1 non-gpx
# file in a folder
with open(file_path, 'rb') as zip_file:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='test@test.com', password='12345678')
),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -762,8 +653,7 @@ class TestPostWorkoutWithZipArchive:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -780,14 +670,7 @@ class TestPostWorkoutWithZipArchive:
)
# 'gpx_test_incorrect.zip' contains 2 gpx files, one is incorrect
with open(file_path, 'rb') as zip_file:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='test@test.com', password='12345678')
),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -797,8 +680,7 @@ class TestPostWorkoutWithZipArchive:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -819,13 +701,8 @@ class TestPostWorkoutWithZipArchive:
)
# 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file
with open(file_path, 'rb') as zip_file:
client = app_with_max_workouts.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='test@test.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_max_workouts
)
client.post(
@ -835,17 +712,13 @@ class TestPostWorkoutWithZipArchive:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
response = client.get(
'/api/workouts',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert len(data['data']['workouts']) == 2
@ -861,13 +734,8 @@ class TestPostWorkoutWithZipArchive:
)
# 'gpx_test.zip' contains 3 gpx files (same data) and 1 non-gpx file
with open(file_path, 'rb') as zip_file:
client = app_with_max_zip_file_size.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(
dict(email='test@test.com', password='12345678')
),
content_type='application/json',
client, auth_token = self.get_test_client_and_auth_token(
app_with_max_zip_file_size
)
response = client.post(
@ -877,8 +745,7 @@ class TestPostWorkoutWithZipArchive:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
@ -891,17 +758,11 @@ class TestPostWorkoutWithZipArchive:
assert 'data' not in data
class TestPostAndGetWorkoutWithGpx:
@staticmethod
class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
def workout_assertion(
app: Flask, gpx_file: str, with_segments: bool
self, app: Flask, gpx_file: str, with_segments: bool
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
data=dict(
@ -910,8 +771,7 @@ class TestPostAndGetWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
@ -929,10 +789,7 @@ class TestPostAndGetWorkoutWithGpx:
response = client.get(
f'/api/workouts/{workout_short_id}/gpx',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -943,10 +800,7 @@ class TestPostAndGetWorkoutWithGpx:
response = client.get(
f'/api/workouts/{workout_short_id}/gpx/segment/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -957,10 +811,7 @@ class TestPostAndGetWorkoutWithGpx:
response = client.get(
f'/api/workouts/map/{map_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 200
@ -976,10 +827,7 @@ class TestPostAndGetWorkoutWithGpx:
response = client.get(
f'/api/workouts/map/{map_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1007,12 +855,7 @@ class TestPostAndGetWorkoutWithGpx:
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
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -1022,18 +865,14 @@ class TestPostAndGetWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
workout_short_id = data['data']['workouts'][0]['id']
response = client.get(
f'/api/workouts/{workout_short_id}/chart_data',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1045,12 +884,7 @@ class TestPostAndGetWorkoutWithGpx:
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
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -1060,18 +894,14 @@ class TestPostAndGetWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
workout_short_id = data['data']['workouts'][0]['id']
response = client.get(
f'/api/workouts/{workout_short_id}/chart_data/segment/1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1088,12 +918,7 @@ class TestPostAndGetWorkoutWithGpx:
sport_1_cycling: Sport,
gpx_file: str,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
data=dict(
@ -1102,8 +927,7 @@ class TestPostAndGetWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
@ -1130,12 +954,7 @@ class TestPostAndGetWorkoutWithGpx:
def test_it_returns_500_on_invalid_segment_id(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -1145,18 +964,14 @@ class TestPostAndGetWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
workout_short_id = data['data']['workouts'][0]['id']
response = client.get(
f'/api/workouts/{workout_short_id}/chart_data/segment/0',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1168,12 +983,7 @@ class TestPostAndGetWorkoutWithGpx:
def test_it_returns_404_if_segment_id_does_not_exist(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts',
@ -1183,18 +993,14 @@ class TestPostAndGetWorkoutWithGpx:
),
headers=dict(
content_type='multipart/form-data',
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
workout_short_id = data['data']['workouts'][0]['id']
response = client.get(
f'/api/workouts/{workout_short_id}/chart_data/segment/999999',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1204,16 +1010,11 @@ class TestPostAndGetWorkoutWithGpx:
assert 'data' not in data
class TestPostAndGetWorkoutWithoutGpx:
class TestPostAndGetWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_add_and_gets_an_workout_wo_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
@ -1226,19 +1027,13 @@ class TestPostAndGetWorkoutWithoutGpx:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_short_id = data['data']['workouts'][0]['id']
response = client.get(
f'/api/workouts/{workout_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1250,12 +1045,7 @@ class TestPostAndGetWorkoutWithoutGpx:
def test_it_adds_and_gets_an_workout_wo_gpx_notes(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
@ -1269,19 +1059,13 @@ class TestPostAndGetWorkoutWithoutGpx:
notes="new test with notes",
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_short_id = data['data']['workouts'][0]['id']
response = client.get(
f'/api/workouts/{workout_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1291,17 +1075,12 @@ class TestPostAndGetWorkoutWithoutGpx:
assert 'new test with notes' == data['data']['workouts'][0]['notes']
class TestPostAndGetWorkoutUsingTimezones:
class TestPostAndGetWorkoutUsingTimezones(ApiTestCaseMixin):
def test_it_add_and_gets_an_workout_wo_gpx_with_timezone(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
user_1.timezone = 'Europe/Paris'
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.post(
'/api/workouts/no_gpx',
@ -1314,19 +1093,13 @@ class TestPostAndGetWorkoutUsingTimezones:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
workout_short_id = data['data']['workouts'][0]['id']
response = client.get(
f'/api/workouts/{workout_short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1345,12 +1118,7 @@ class TestPostAndGetWorkoutUsingTimezones:
def test_it_adds_and_gets_workouts_date_filter_with_timezone_new_york(
self, app: Flask, user_1_full: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
client.post(
'/api/workouts/no_gpx',
@ -1363,17 +1131,11 @@ class TestPostAndGetWorkoutUsingTimezones:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.get(
'/api/workouts?from=2018-01-01&to=2018-01-31',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -1396,12 +1158,7 @@ class TestPostAndGetWorkoutUsingTimezones:
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
client.post(
'/api/workouts/no_gpx',
@ -1414,10 +1171,7 @@ class TestPostAndGetWorkoutUsingTimezones:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
client.post(
'/api/workouts/no_gpx',
@ -1430,10 +1184,7 @@ class TestPostAndGetWorkoutUsingTimezones:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
workout_cycling_user_1.workout_date = datetime.strptime(
@ -1452,17 +1203,11 @@ class TestPostAndGetWorkoutUsingTimezones:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
response = client.get(
'/api/workouts?from=2018-01-01&to=2018-01-31',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())

View File

@ -7,6 +7,7 @@ from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from fittrackee.workouts.utils_id import decode_short_id
from ..api_test_case import ApiTestCaseMixin
from .utils import get_random_short_id, post_an_workout
@ -53,7 +54,7 @@ def assert_workout_data_with_gpx(data: Dict, sport_id: int) -> None:
assert records[3]['value'] == 4.61
class TestEditWorkoutWithGpx:
class TestEditWorkoutWithGpx(ApiTestCaseMixin):
def test_it_updates_title_for_an_workout_with_gpx(
self,
app: Flask,
@ -203,7 +204,7 @@ class TestEditWorkoutWithGpx:
)
class TestEditWorkoutWithoutGpx:
class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_updates_an_workout_wo_gpx(
self,
app: Flask,
@ -213,12 +214,7 @@ class TestEditWorkoutWithoutGpx:
workout_cycling_user_1: Workout,
) -> None:
workout_short_id = workout_cycling_user_1.short_id
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{workout_short_id}',
@ -232,10 +228,7 @@ class TestEditWorkoutWithoutGpx:
title='Workout test',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -298,21 +291,13 @@ class TestEditWorkoutWithoutGpx:
workout_cycling_user_1: Workout,
) -> None:
workout_short_id = workout_cycling_user_1.short_id
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{workout_short_id}',
content_type='application/json',
data=json.dumps(dict(notes='test notes')),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -374,12 +359,7 @@ class TestEditWorkoutWithoutGpx:
sport_1_cycling: Sport,
workout_cycling_user_2: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{workout_cycling_user_2.short_id}',
@ -393,10 +373,7 @@ class TestEditWorkoutWithoutGpx:
title='Workout test',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -413,12 +390,7 @@ class TestEditWorkoutWithoutGpx:
workout_cycling_user_1: Workout,
) -> None:
workout_short_id = workout_cycling_user_1.short_id
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{workout_short_id}',
@ -432,10 +404,7 @@ class TestEditWorkoutWithoutGpx:
title='Workout test',
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -494,21 +463,13 @@ class TestEditWorkoutWithoutGpx:
workout_cycling_user_1: Workout,
) -> None:
workout_short_id = workout_cycling_user_1.short_id
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{workout_short_id}',
content_type='application/json',
data=json.dumps(dict(sport_id=2, distance=20)),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -565,21 +526,13 @@ class TestEditWorkoutWithoutGpx:
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{workout_cycling_user_1.short_id}',
content_type='application/json',
data=json.dumps(dict()),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -594,12 +547,7 @@ class TestEditWorkoutWithoutGpx:
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{workout_cycling_user_1.short_id}',
content_type='application/json',
@ -611,10 +559,7 @@ class TestEditWorkoutWithoutGpx:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
@ -629,12 +574,7 @@ class TestEditWorkoutWithoutGpx:
def test_it_returns_404_if_edited_workout_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.patch(
f'/api/workouts/{get_random_short_id()}',
content_type='application/json',
@ -646,10 +586,7 @@ class TestEditWorkoutWithoutGpx:
distance=10,
)
),
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())

View File

@ -7,6 +7,7 @@ from fittrackee.users.models import User
from fittrackee.workouts.models import Sport, Workout
from fittrackee.workouts.utils import get_absolute_file_path
from ..api_test_case import ApiTestCaseMixin
from .utils import get_random_short_id, post_an_workout
@ -15,7 +16,7 @@ def get_gpx_filepath(workout_id: int) -> str:
return workout.gpx
class TestDeleteWorkoutWithGpx:
class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
def test_it_deletes_an_workout_with_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
@ -62,18 +63,10 @@ class TestDeleteWorkoutWithGpx:
def test_it_returns_404_if_workout_does_not_exist(
self, app: Flask, user_1: User
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.delete(
f'/api/workouts/{get_random_short_id()}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
@ -103,7 +96,7 @@ class TestDeleteWorkoutWithGpx:
)
class TestDeleteWorkoutWithoutGpx:
class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin):
def test_it_deletes_an_workout_wo_gpx(
self,
app: Flask,
@ -111,18 +104,10 @@ class TestDeleteWorkoutWithoutGpx:
sport_1_cycling: Sport,
workout_cycling_user_1: Workout,
) -> None:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
client, auth_token = self.get_test_client_and_auth_token(app)
response = client.delete(
f'/api/workouts/{workout_cycling_user_1.short_id}',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 204