Skip to content

Commit 721f869

Browse files
committed
fix(refresh): datetime handling in tests
1 parent af5766d commit 721f869

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
Changes
1111
=======
1212

13+
Version v5.4.0 (released 2025-XX-XX)
14+
15+
- feat: support for refresh tokens
16+
1317
Version v5.3.1 (released 2025-10-21)
1418

1519
- i18n: pulled translations

invenio_oauthclient/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .oauth import oauth_link_external_id, oauth_unlink_external_id
1515
from .proxies import current_oauthclient
1616

17-
__version__ = "5.3.1"
17+
__version__ = "5.4.0"
1818

1919
__all__ = (
2020
"__version__",

invenio_oauthclient/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,9 @@ class CustomOAuthRemoteApp(OAuthRemoteApp):
357357
login is disabled), the login view function will automatically redirect to
358358
that external authentication service.
359359
"""
360+
361+
OAUTHCLIENT_TOKEN_EXPIRES_LEEWAY = 10
362+
"""The number of seconds before the actual expiration of an access token from which it is considered expired.
363+
364+
This ensures that it won't cause issues if some time passes between the check for whether the token is expired and any requests that the token is then used in.
365+
"""

invenio_oauthclient/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ def is_expired(self):
148148
return False
149149

150150
leeway = current_app.config.get("OAUTHCLIENT_TOKEN_EXPIRES_LEEWAY", 10)
151-
expiration_with_leeway = self.expires_at - timedelta(seconds=leeway)
151+
expiration_with_leeway = (self.expires_at - timedelta(seconds=leeway)).replace(
152+
# see https://docs.sqlalchemy.org/en/13/core/type_basics.html#sqlalchemy.types.DateTime
153+
# We store datetimes in the DB as UTC but without timezone metadata. So to make comparison
154+
# possible, we need to mark this as UTC.
155+
tzinfo=timezone.utc
156+
)
152157
return expiration_with_leeway < datetime.now(tz=timezone.utc)
153158

154159
def __repr__(self):

tests/test_refresh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
"""Test handlers."""
1010
import json
11-
from datetime import datetime
11+
from datetime import datetime, timezone
1212

1313
from helpers import mock_remote_http_request
1414

@@ -27,7 +27,7 @@ def test_refresh(models_fixture, app):
2727
"mytoken",
2828
"mysecret",
2929
refresh_token="myrefreshtoken",
30-
expires_at=datetime.utcnow(),
30+
expires_at=datetime.now(tz=timezone.utc),
3131
)
3232
assert rt.is_expired is True
3333

0 commit comments

Comments
 (0)