Skip to content

Commit c35d5b6

Browse files
committed
Use naive datetimes
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
1 parent cc0a03c commit c35d5b6

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

python/nav/models/sql/changes/sc.05.13.0001.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CREATE TABLE manage.JWTRefreshToken (
22
id SERIAL PRIMARY KEY,
33
name VARCHAR NOT NULL UNIQUE,
44
description VARCHAR,
5-
expires TIMESTAMP NOT NULL,
6-
activates TIMESTAMP NOT NULL,
5+
expires TIMESTAMPTZ NOT NULL,
6+
activates TIMESTAMPTZ NOT NULL,
77
hash VARCHAR NOT NULL
88
);

python/nav/web/api/v1/views.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# pylint: disable=R0903, R0901, R0904
1717
"""Views for the NAV API"""
1818

19-
from datetime import datetime, timedelta, timezone
19+
from datetime import datetime, timedelta
2020
import logging
2121

2222
from IPy import IP
@@ -1186,8 +1186,8 @@ def post(self, request):
11861186
new_claims = decode_token(refresh_token)
11871187
new_hash = hash_token(refresh_token)
11881188
db_token.hash = new_hash
1189-
db_token.expires = datetime.fromtimestamp(new_claims['exp'], tz=timezone.utc)
1190-
db_token.activates = datetime.fromtimestamp(new_claims['nbf'], tz=timezone.utc)
1189+
db_token.expires = datetime.fromtimestamp(new_claims['exp'])
1190+
db_token.activates = datetime.fromtimestamp(new_claims['nbf'])
11911191
db_token.save()
11921192

11931193
response_data = {

tests/integration/jwt_refresh_endpoint_test.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Generator
22
import pytest
3-
from datetime import datetime, timezone, timedelta
3+
from datetime import datetime, timedelta
44

55
from unittest.mock import Mock, patch
66

@@ -27,7 +27,7 @@ def test_token_not_in_database_should_be_rejected(db, api_client, url):
2727
def test_inactive_token_should_be_rejected(db, api_client, url):
2828
token = generate_refresh_token()
2929
# Set expiry date in the past
30-
now = datetime.now(tz=timezone.utc)
30+
now = datetime.now()
3131
db_token = JWTRefreshToken(
3232
name="testtoken",
3333
hash=hash_token(token),
@@ -53,8 +53,8 @@ def test_valid_token_should_be_accepted(db, api_client, url):
5353
db_token = JWTRefreshToken(
5454
name="testtoken",
5555
hash=hash_token(token),
56-
expires=datetime.fromtimestamp(data['exp'], tz=timezone.utc),
57-
activates=datetime.fromtimestamp(data['nbf'], tz=timezone.utc),
56+
expires=datetime.fromtimestamp(data['exp']),
57+
activates=datetime.fromtimestamp(data['nbf']),
5858
)
5959
db_token.save()
6060
response = api_client.post(
@@ -74,8 +74,8 @@ def test_valid_token_should_be_replaced_by_new_token_in_db(db, api_client, url):
7474
db_token = JWTRefreshToken(
7575
name="testtoken",
7676
hash=token_hash,
77-
expires=datetime.fromtimestamp(data['exp'], tz=timezone.utc),
78-
activates=datetime.fromtimestamp(data['nbf'], tz=timezone.utc),
77+
expires=datetime.fromtimestamp(data['exp']),
78+
activates=datetime.fromtimestamp(data['nbf']),
7979
)
8080
db_token.save()
8181
response = api_client.post(
@@ -98,8 +98,8 @@ def test_should_include_access_and_refresh_token_in_response(db, api_client, url
9898
db_token = JWTRefreshToken(
9999
name="testtoken",
100100
hash=hash_token(token),
101-
expires=datetime.fromtimestamp(data['exp'], tz=timezone.utc),
102-
activates=datetime.fromtimestamp(data['nbf'], tz=timezone.utc),
101+
expires=datetime.fromtimestamp(data['exp']),
102+
activates=datetime.fromtimestamp(data['nbf']),
103103
)
104104
db_token.save()
105105
response = api_client.post(

tests/unittests/models/jwtrefreshtoken_test.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from datetime import datetime, timedelta, timezone
1+
from datetime import datetime, timedelta
22

33
from nav.models.api import JWTRefreshToken
44

55

66
class TestIsActive:
77
def test_should_return_false_if_token_activates_in_the_future(self):
8-
now = datetime.now(tz=timezone.utc)
8+
now = datetime.now()
99
token = JWTRefreshToken(
1010
name="testtoken",
1111
hash="dummyhash",
@@ -15,7 +15,7 @@ def test_should_return_false_if_token_activates_in_the_future(self):
1515
assert not token.is_active()
1616

1717
def test_should_return_false_if_token_expires_in_the_past(self):
18-
now = datetime.now(tz=timezone.utc)
18+
now = datetime.now()
1919
token = JWTRefreshToken(
2020
name="testtoken",
2121
hash="dummyhash",
@@ -27,7 +27,7 @@ def test_should_return_false_if_token_expires_in_the_past(self):
2727
def test_should_return_true_if_token_activates_in_the_past_and_expires_in_the_future(
2828
self,
2929
):
30-
now = datetime.now(tz=timezone.utc)
30+
now = datetime.now()
3131
token = JWTRefreshToken(
3232
name="testtoken",
3333
hash="dummyhash",
@@ -37,7 +37,7 @@ def test_should_return_true_if_token_activates_in_the_past_and_expires_in_the_fu
3737
assert token.is_active()
3838

3939
def test_should_return_true_if_token_activates_now_and_expires_in_the_future(self):
40-
now = datetime.now(tz=timezone.utc)
40+
now = datetime.now()
4141
token = JWTRefreshToken(
4242
name="testtoken",
4343
hash="dummyhash",
@@ -48,7 +48,7 @@ def test_should_return_true_if_token_activates_now_and_expires_in_the_future(sel
4848

4949

5050
def test_string_representation_should_match_name():
51-
now = datetime.now(tz=timezone.utc)
51+
now = datetime.now()
5252
token = JWTRefreshToken(
5353
name="testtoken",
5454
hash="dummyhash",

0 commit comments

Comments
 (0)