From cd9caa66235468ddad4922a4f9a3ee0b55d022b1 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 31 Jan 2021 10:21:40 +0100 Subject: [PATCH] API - fix readable duration - fix #62 + add some tests on user utils --- fittrackee/tests/users/test_users_utils.py | 46 ++++++++++++++++++++++ fittrackee/users/auth.py | 5 ++- fittrackee/users/utils.py | 13 ++++-- 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 fittrackee/tests/users/test_users_utils.py diff --git a/fittrackee/tests/users/test_users_utils.py b/fittrackee/tests/users/test_users_utils.py new file mode 100644 index 00000000..f2cf81ed --- /dev/null +++ b/fittrackee/tests/users/test_users_utils.py @@ -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 diff --git a/fittrackee/users/auth.py b/fittrackee/users/auth.py index 68aa877b..675fdb8f 100644 --- a/fittrackee/users/auth.py +++ b/fittrackee/users/auth.py @@ -647,10 +647,11 @@ def request_password_reset() -> Union[Dict, HttpResponse]: if user: password_reset_token = user.encode_password_reset_token(user.id) ui_url = current_app.config['UI_URL'] + user_language = 'en' if user.language is None else user.language email_data = { 'expiration_delay': get_readable_duration( current_app.config['PASSWORD_TOKEN_EXPIRATION_SECONDS'], - 'en' if user.language is None else user.language, + user_language, ), 'username': user.username, 'password_reset_url': ( @@ -660,7 +661,7 @@ def request_password_reset() -> Union[Dict, HttpResponse]: 'browser_name': request.user_agent.browser, # type: ignore } user_data = { - 'language': user.language if user.language else 'en', + 'language': user_language, 'email': user.email, } reset_password_email.send(user_data, email_data) diff --git a/fittrackee/users/utils.py b/fittrackee/users/utils.py index a996cbdc..fc202123 100644 --- a/fittrackee/users/utils.py +++ b/fittrackee/users/utils.py @@ -156,13 +156,18 @@ def display_readable_file_size(size_in_bytes: Union[float, int]) -> str: 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 """ - if locale is not None and locale != 'en': - _t = humanize.i18n.activate(locale) # noqa + if locale is None: + locale = 'en' + if locale != 'en': + try: + _t = humanize.i18n.activate(locale) # noqa + except FileNotFoundError: + locale = 'en' readable_duration = humanize.naturaldelta(timedelta(seconds=duration)) - if locale is not None and locale != 'en': + if locale != 'en': humanize.i18n.deactivate() return readable_duration