parent
b385ccacbc
commit
cd9caa6623
46
fittrackee/tests/users/test_users_utils.py
Normal file
46
fittrackee/tests/users/test_users_utils.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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
|
@ -647,10 +647,11 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
|
|||||||
if user:
|
if user:
|
||||||
password_reset_token = user.encode_password_reset_token(user.id)
|
password_reset_token = user.encode_password_reset_token(user.id)
|
||||||
ui_url = current_app.config['UI_URL']
|
ui_url = current_app.config['UI_URL']
|
||||||
|
user_language = 'en' if user.language is None else user.language
|
||||||
email_data = {
|
email_data = {
|
||||||
'expiration_delay': get_readable_duration(
|
'expiration_delay': get_readable_duration(
|
||||||
current_app.config['PASSWORD_TOKEN_EXPIRATION_SECONDS'],
|
current_app.config['PASSWORD_TOKEN_EXPIRATION_SECONDS'],
|
||||||
'en' if user.language is None else user.language,
|
user_language,
|
||||||
),
|
),
|
||||||
'username': user.username,
|
'username': user.username,
|
||||||
'password_reset_url': (
|
'password_reset_url': (
|
||||||
@ -660,7 +661,7 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
|
|||||||
'browser_name': request.user_agent.browser, # type: ignore
|
'browser_name': request.user_agent.browser, # type: ignore
|
||||||
}
|
}
|
||||||
user_data = {
|
user_data = {
|
||||||
'language': user.language if user.language else 'en',
|
'language': user_language,
|
||||||
'email': user.email,
|
'email': user.email,
|
||||||
}
|
}
|
||||||
reset_password_email.send(user_data, email_data)
|
reset_password_email.send(user_data, email_data)
|
||||||
|
@ -156,13 +156,18 @@ def display_readable_file_size(size_in_bytes: Union[float, int]) -> str:
|
|||||||
return f'{size_in_bytes} bytes'
|
return f'{size_in_bytes} bytes'
|
||||||
|
|
||||||
|
|
||||||
def get_readable_duration(duration: int, locale: Optional[str] = 'en') -> str:
|
def get_readable_duration(duration: int, locale: Optional[str] = None) -> str:
|
||||||
"""
|
"""
|
||||||
Return readable and localized duration from duration in seconds
|
Return readable and localized duration from duration in seconds
|
||||||
"""
|
"""
|
||||||
if locale is not None and locale != 'en':
|
if locale is None:
|
||||||
|
locale = 'en'
|
||||||
|
if locale != 'en':
|
||||||
|
try:
|
||||||
_t = humanize.i18n.activate(locale) # noqa
|
_t = humanize.i18n.activate(locale) # noqa
|
||||||
|
except FileNotFoundError:
|
||||||
|
locale = 'en'
|
||||||
readable_duration = humanize.naturaldelta(timedelta(seconds=duration))
|
readable_duration = humanize.naturaldelta(timedelta(seconds=duration))
|
||||||
if locale is not None and locale != 'en':
|
if locale != 'en':
|
||||||
humanize.i18n.deactivate()
|
humanize.i18n.deactivate()
|
||||||
return readable_duration
|
return readable_duration
|
||||||
|
Loading…
Reference in New Issue
Block a user