API - fix error message on file uplaod + refactor - #72

This commit is contained in:
Sam
2021-02-20 21:37:31 +01:00
parent aa1170f1c7
commit bb8491f84d
10 changed files with 306 additions and 113 deletions

View File

@ -851,6 +851,76 @@ class TestUserPicture:
assert data['message'] == 'File extension not allowed.'
assert response.status_code == 400
def test_it_returns_error_if_image_size_exceeds_file_limit(
self,
app_with_max_file_size: Flask,
user_1: User,
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',
)
response = client.post(
'/api/auth/picture',
data=dict(
file=(BytesIO(b'test_file_for_avatar' * 50), 'avatar.jpg')
),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
),
)
data = json.loads(response.data.decode())
print('data', data)
assert response.status_code == 413
assert 'fail' in data['status']
assert (
'Error during picture upload, file size (1.2KB) exceeds 1.0KB.'
in data['message']
)
assert 'data' not in data
def test_it_returns_error_if_image_size_exceeds_archive_limit(
self,
app_with_max_zip_file_size: Flask,
user_1: User,
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',
)
response = client.post(
'/api/auth/picture',
data=dict(
file=(BytesIO(b'test_file_for_avatar' * 50), 'avatar.jpg')
),
headers=dict(
content_type='multipart/form-data',
authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token'],
),
)
data = json.loads(response.data.decode())
print('data', data)
assert response.status_code == 413
assert 'fail' in data['status']
assert (
'Error during picture upload, file size (1.2KB) exceeds 1.0KB.'
in data['message']
)
assert 'data' not in data
class TestRegistrationConfiguration:
def test_it_returns_error_if_it_exceeds_max_users(

View File

@ -1,46 +0,0 @@
from typing import Union
from uuid import uuid4
import pytest
from fittrackee.users.utils import (
display_readable_file_size,
get_readable_duration,
)
class TestDisplayReadableFileSize:
@pytest.mark.parametrize(
'size, expected_readable_size',
[
(0, '0 bytes'),
(1, '1 byte'),
(100, '100.0 bytes'),
(1024, '1.0KB'),
(286773663, '273.5MB'),
],
)
def test_it_returns_readable_file_size(
self, size: Union[float, int], expected_readable_size: str
) -> None:
readable_file_size = display_readable_file_size(size)
assert readable_file_size == expected_readable_size
class TestReadableDuration:
@pytest.mark.parametrize(
'locale, expected_duration',
[
('en', '30 seconds'),
('fr', '30 secondes'),
(None, '30 seconds'),
(uuid4().hex, '30 seconds'),
],
)
def test_it_returns_duration_in_locale(
self, locale: str, expected_duration: str
) -> None:
readable_duration = get_readable_duration(30, locale)
assert readable_duration == expected_duration