-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add database model for JWT refresh tokens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE manage.JWTRefreshToken ( | ||
id SERIAL PRIMARY KEY, | ||
data JSON NOT NULL, | ||
name VARCHAR NOT NULL UNIQUE, | ||
description VARCHAR, | ||
hash VARCHAR NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
from datetime import datetime, timedelta, timezone | ||
|
||
from nav.models.api import JWTRefreshToken | ||
|
||
|
||
class TestIsActive: | ||
def test_should_return_false_if_nbf_is_in_the_future(self, token): | ||
now = datetime.now(tz=timezone.utc) | ||
token.data['nbf'] = (now + timedelta(hours=1)).timestamp() | ||
token.data['exp'] = (now + timedelta(hours=1)).timestamp() | ||
assert not token.is_active() | ||
|
||
def test_should_return_false_if_exp_is_in_the_past(self, token): | ||
now = datetime.now(tz=timezone.utc) | ||
token.data['nbf'] = (now - timedelta(hours=1)).timestamp() | ||
token.data['exp'] = (now - timedelta(hours=1)).timestamp() | ||
assert not token.is_active() | ||
|
||
def test_should_return_true_if_nbf_is_in_the_past_and_exp_is_in_the_future( | ||
self, token | ||
): | ||
now = datetime.now(tz=timezone.utc) | ||
token.data['nbf'] = (now - timedelta(hours=1)).timestamp() | ||
token.data['exp'] = (now + timedelta(hours=1)).timestamp() | ||
assert token.is_active() | ||
|
||
|
||
def test_string_representation_should_match_token(token): | ||
assert str(token) == token.name | ||
|
||
|
||
@pytest.fixture() | ||
def token(data) -> JWTRefreshToken: | ||
return JWTRefreshToken( | ||
name="testtoken", | ||
description="this is a test token", | ||
data=data, | ||
hash="dummyhash", | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def data() -> dict: | ||
data = { | ||
"exp": 1516339022, | ||
"nbf": 1516239022, | ||
"iat": 1516239022, | ||
"aud": "nav", | ||
"iss": "nav", | ||
"token_type": "refresh_token", | ||
} | ||
return data |