From bf936a75eff609e305a6856d9dbe39cd994988c3 Mon Sep 17 00:00:00 2001 From: Salvatore Laiso <32564922+salvatorelaiso@users.noreply.github.com> Date: Wed, 26 Jul 2023 12:54:35 +0200 Subject: [PATCH] Add utils tests --- pyeudiw/tests/tools/test_utils.py | 84 +++++++++++++++++++++++++++++++ pyeudiw/tools/utils.py | 5 +- 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 pyeudiw/tests/tools/test_utils.py diff --git a/pyeudiw/tests/tools/test_utils.py b/pyeudiw/tests/tools/test_utils.py new file mode 100644 index 00000000..debc5214 --- /dev/null +++ b/pyeudiw/tests/tools/test_utils.py @@ -0,0 +1,84 @@ + +import datetime +import sys +import freezegun + +import pytest + +from pyeudiw.tools.utils import exp_from_now, iat_now, random_token + + +def test_make_timezone_aware(): + # TODO: test the function after it is implemented + pass + + +def frozen_time(fake_now, function, *args): + with freezegun.freeze_time(fake_now): + return function(*args) + + +@pytest.mark.parametrize("fake_now, timestamp", [ + ("2020-12-31 12:00:00", 1609416000), + ("2000-10-02 12:23:14", 970489394), + ("1992-09-03 22:00:00", 715557600), +]) +def test_iat_now(fake_now, timestamp): + iat = frozen_time(fake_now=fake_now, function=iat_now) + assert iat == timestamp + + +@pytest.mark.parametrize("fake_now, delta_mins, timestamp", [ + ("2020-12-31 12:00:00", 0, 1609416000), + ("2000-10-02 12:23:14", 1, 970489454), + ("1992-09-03 22:00:00", 2, 715557720), +]) +def test_exp_from_now(fake_now, delta_mins, timestamp): + exp = frozen_time(fake_now, exp_from_now, delta_mins) + assert exp == timestamp + + +def test_datetime_from_timestamp(): + # TODO: test the function after it is implemented + pass + + +def test_get_http_url(): + # TODO: test the function after it is implemented + pass + + + +@pytest.mark.parametrize("n", [ + -1, 0, 1, 2, 3, 10, 999, 10**1000, 2., + sys.maxsize, sys.maxsize - 1, + #sys.maxsize // 2 -1, + "1"]) +def test_random_token(n): + if type(n) != int: + with pytest.raises(TypeError): + random_token(n) + return + + if n < 0: + with pytest.raises(ValueError): + random_token(n) + return + + if n >= sys.maxsize - 32: + with pytest.raises(OverflowError): + random_token(n) + return + + rand = random_token(n) + + if (n == 0): + assert rand == '' + return + + assert rand + assert len(rand) == n * 2 + hex = int(rand, 16) + assert hex + assert type(hex) == int + \ No newline at end of file diff --git a/pyeudiw/tools/utils.py b/pyeudiw/tools/utils.py index 5758ea08..2d6c004f 100644 --- a/pyeudiw/tools/utils.py +++ b/pyeudiw/tools/utils.py @@ -1,4 +1,3 @@ -from datetime import timezone # from django.utils.timezone import make_aware from secrets import token_hex @@ -20,8 +19,8 @@ def iat_now() -> int: def exp_from_now(minutes: int = 33) -> int: - _now = timezone.localtime() - return int((_now + datetime.timedelta(minutes=minutes)).timestamp()) + now = datetime.datetime.now() + return int((now + datetime.timedelta(minutes=minutes)).timestamp()) def datetime_from_timestamp(value) -> datetime.datetime: