From 3851ee7798a9df89c160a796ecc705d070e91673 Mon Sep 17 00:00:00 2001 From: Nick Tarleton Date: Tue, 1 Oct 2024 14:24:08 -0700 Subject: [PATCH 1/4] add a test for the format of _get_expires_at --- tests/test_units/test_auth/test_utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/test_units/test_auth/test_utils.py diff --git a/tests/test_units/test_auth/test_utils.py b/tests/test_units/test_auth/test_utils.py new file mode 100644 index 0000000..a7c910c --- /dev/null +++ b/tests/test_units/test_auth/test_utils.py @@ -0,0 +1,11 @@ +import datetime + +from aiogoogle.auth.utils import _get_expires_at + + +def test_get_expires_at(): + exp_str = _get_expires_at(120) + exp_dt = datetime.datetime.fromisoformat(exp_str) + assert exp_dt.tzinfo is None + delta = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - exp_dt + assert -1 < delta.seconds < 1 From 9dcc95210858d5f7c9c0b977e0244685fa1c8f35 Mon Sep 17 00:00:00 2001 From: Nick Tarleton Date: Tue, 1 Oct 2024 14:24:22 -0700 Subject: [PATCH 2/4] remove use of utcnow(), deprecated in Python 3.12 --- aiogoogle/auth/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aiogoogle/auth/utils.py b/aiogoogle/auth/utils.py index 5f954d4..3f15c92 100644 --- a/aiogoogle/auth/utils.py +++ b/aiogoogle/auth/utils.py @@ -14,9 +14,10 @@ def create_secret(bytes_length=1024): # pragma: no cover def _get_expires_at(expires_in): - expires_at = datetime.datetime.utcnow() + datetime.timedelta(seconds=expires_in) + expires_at = datetime.datetime.now() + datetime.timedelta(seconds=expires_in) # account for clock skew expires_at -= datetime.timedelta(seconds=120) + expires_at = expires_at.astimezone(datetime.timezone.utc).replace(tzinfo=None) return expires_at.isoformat() @@ -30,7 +31,8 @@ def _is_expired(expires_at): expires_at = _parse_isoformat(expires_at) else: expires_at = datetime.datetime.fromisoformat(expires_at) - if datetime.datetime.utcnow() >= expires_at: + utc_now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) + if utc_now >= expires_at: return True else: return False From bf2b5c4133aeb5aeffde49e3a7b70e926828e90c Mon Sep 17 00:00:00 2001 From: Nick Tarleton Date: Tue, 1 Oct 2024 14:40:26 -0700 Subject: [PATCH 3/4] Fix a bug in timezone parsing in _is_expired Previously, _is_expired would raise TypeError: can't compare offset-naive and offset-aware datetimes if passed a (string representing a) datetime with a timezone. --- aiogoogle/auth/utils.py | 5 +++-- tests/test_units/test_auth/test_utils.py | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/aiogoogle/auth/utils.py b/aiogoogle/auth/utils.py index 3f15c92..be0f2cd 100644 --- a/aiogoogle/auth/utils.py +++ b/aiogoogle/auth/utils.py @@ -31,8 +31,9 @@ def _is_expired(expires_at): expires_at = _parse_isoformat(expires_at) else: expires_at = datetime.datetime.fromisoformat(expires_at) - utc_now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - if utc_now >= expires_at: + if expires_at.tzinfo is None: + expires_at = expires_at.replace(tzinfo=datetime.timezone.utc) + if datetime.datetime.now(datetime.timezone.utc) >= expires_at: return True else: return False diff --git a/tests/test_units/test_auth/test_utils.py b/tests/test_units/test_auth/test_utils.py index a7c910c..cd250c3 100644 --- a/tests/test_units/test_auth/test_utils.py +++ b/tests/test_units/test_auth/test_utils.py @@ -1,6 +1,6 @@ import datetime -from aiogoogle.auth.utils import _get_expires_at +from aiogoogle.auth.utils import _get_expires_at, _is_expired def test_get_expires_at(): @@ -9,3 +9,11 @@ def test_get_expires_at(): assert exp_dt.tzinfo is None delta = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - exp_dt assert -1 < delta.seconds < 1 + + +def test_is_expired(): + # correctness not tested, but none of these should raise an exception + _is_expired("2024-10-01T14:26:21") + _is_expired("2024-10-01T14:26:21Z") + _is_expired(datetime.datetime.now()) + _is_expired(datetime.datetime.now(datetime.timezone.utc)) From d0a4eb2195ed40b5e66686187d585e59f6059606 Mon Sep 17 00:00:00 2001 From: Nick Tarleton Date: Tue, 1 Oct 2024 14:55:46 -0700 Subject: [PATCH 4/4] in test, use an ISO format that Python <3.11 will accept --- tests/test_units/test_auth/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_units/test_auth/test_utils.py b/tests/test_units/test_auth/test_utils.py index cd250c3..8b938a7 100644 --- a/tests/test_units/test_auth/test_utils.py +++ b/tests/test_units/test_auth/test_utils.py @@ -14,6 +14,6 @@ def test_get_expires_at(): def test_is_expired(): # correctness not tested, but none of these should raise an exception _is_expired("2024-10-01T14:26:21") - _is_expired("2024-10-01T14:26:21Z") + _is_expired("2024-10-01T14:26:21+00:00") _is_expired(datetime.datetime.now()) _is_expired(datetime.datetime.now(datetime.timezone.utc))