Skip to content

Commit

Permalink
Merge pull request #151 from npt/master
Browse files Browse the repository at this point in the history
Fix another DeprecationWarning, and a bug in _is_expired
  • Loading branch information
omarryhan authored Oct 2, 2024
2 parents 32f27b1 + d0a4eb2 commit e04cd1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions aiogoogle/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -30,7 +31,9 @@ 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:
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
19 changes: 19 additions & 0 deletions tests/test_units/test_auth/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import datetime

from aiogoogle.auth.utils import _get_expires_at, _is_expired


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


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:21+00:00")
_is_expired(datetime.datetime.now())
_is_expired(datetime.datetime.now(datetime.timezone.utc))

0 comments on commit e04cd1d

Please sign in to comment.