Skip to content

Commit

Permalink
Use naive datetimes
Browse files Browse the repository at this point in the history
Had issues with Django serving me naive datetimes from
the DateTimeFields which
could not be compared to the timezone aware datetimes objects
i was otherwise operating with
  • Loading branch information
stveit committed Feb 6, 2025
1 parent 1d622a1 commit 2889fe6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions python/nav/web/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# pylint: disable=R0903, R0901, R0904
"""Views for the NAV API"""

from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta
import logging

from IPy import IP
Expand Down Expand Up @@ -1186,8 +1186,8 @@ def post(self, request):
new_claims = decode_token(refresh_token)
new_hash = hash_token(refresh_token)
db_token.hash = new_hash
db_token.expires = datetime.fromtimestamp(new_claims['exp'], tz=timezone.utc)
db_token.activates = datetime.fromtimestamp(new_claims['nbf'], tz=timezone.utc)
db_token.expires = datetime.fromtimestamp(new_claims['exp'])
db_token.activates = datetime.fromtimestamp(new_claims['nbf'])
db_token.save()

response_data = {
Expand Down
16 changes: 8 additions & 8 deletions tests/integration/jwt_refresh_endpoint_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Generator
import pytest
from datetime import datetime, timezone, timedelta
from datetime import datetime, timedelta

from unittest.mock import Mock, patch

Expand All @@ -27,7 +27,7 @@ def test_token_not_in_database_should_be_rejected(db, api_client, url):
def test_inactive_token_should_be_rejected(db, api_client, url):
token = generate_refresh_token()
# Set expiry date in the past
now = datetime.now(tz=timezone.utc)
now = datetime.now()
db_token = JWTRefreshToken(
name="testtoken",
hash=hash_token(token),
Expand All @@ -53,8 +53,8 @@ def test_valid_token_should_be_accepted(db, api_client, url):
db_token = JWTRefreshToken(
name="testtoken",
hash=hash_token(token),
expires=datetime.fromtimestamp(data['exp'], tz=timezone.utc),
activates=datetime.fromtimestamp(data['nbf'], tz=timezone.utc),
expires=datetime.fromtimestamp(data['exp']),
activates=datetime.fromtimestamp(data['nbf']),
)
db_token.save()
response = api_client.post(
Expand All @@ -74,8 +74,8 @@ def test_valid_token_should_be_replaced_by_new_token_in_db(db, api_client, url):
db_token = JWTRefreshToken(
name="testtoken",
hash=token_hash,
expires=datetime.fromtimestamp(data['exp'], tz=timezone.utc),
activates=datetime.fromtimestamp(data['nbf'], tz=timezone.utc),
expires=datetime.fromtimestamp(data['exp']),
activates=datetime.fromtimestamp(data['nbf']),
)
db_token.save()
response = api_client.post(
Expand All @@ -98,8 +98,8 @@ def test_should_include_access_and_refresh_token_in_response(db, api_client, url
db_token = JWTRefreshToken(
name="testtoken",
hash=hash_token(token),
expires=datetime.fromtimestamp(data['exp'], tz=timezone.utc),
activates=datetime.fromtimestamp(data['nbf'], tz=timezone.utc),
expires=datetime.fromtimestamp(data['exp']),
activates=datetime.fromtimestamp(data['nbf']),
)
db_token.save()
response = api_client.post(
Expand Down

0 comments on commit 2889fe6

Please sign in to comment.