-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dupe fixtures from the model tests. need to find good way to share the fixtures between integration/unittests or maybe not mock here and have an actual testing config file instead
- Loading branch information
Showing
1 changed file
with
153 additions
and
0 deletions.
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,153 @@ | ||
import pytest | ||
|
||
from unittest.mock import Mock, patch | ||
|
||
from django.urls import reverse | ||
from rest_framework.reverse import reverse_lazy | ||
from nav.models.api import JWTRefreshToken | ||
|
||
|
||
def test_token_not_in_database_should_be_rejected(db, api_client, url): | ||
token = JWTRefreshToken.generate_refresh_token() | ||
assert not JWTRefreshToken.objects.filter(token=token).exists() | ||
response = api_client.post( | ||
url, | ||
follow=True, | ||
data={ | ||
'refresh_token': token, | ||
}, | ||
) | ||
assert response.status_code == 403 | ||
|
||
|
||
def test_expired_token_should_be_rejected(db, api_client, url): | ||
token = JWTRefreshToken.generate_refresh_token() | ||
db_token = JWTRefreshToken(token=token) | ||
db_token.save() | ||
db_token.expire() | ||
response = api_client.post( | ||
url, | ||
follow=True, | ||
data={ | ||
'refresh_token': db_token.token, | ||
}, | ||
) | ||
assert response.status_code == 403 | ||
|
||
|
||
def test_valid_token_should_be_accepted(db, api_client, url): | ||
token = JWTRefreshToken.generate_refresh_token() | ||
db_token = JWTRefreshToken(token=token) | ||
db_token.save() | ||
response = api_client.post( | ||
url, | ||
follow=True, | ||
data={ | ||
'refresh_token': token, | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_valid_token_should_be_replaced_by_new_token_in_db(db, api_client, url): | ||
token = JWTRefreshToken.generate_refresh_token() | ||
db_token = JWTRefreshToken(token=token) | ||
db_token.save() | ||
response = api_client.post( | ||
url, | ||
follow=True, | ||
data={ | ||
'refresh_token': token, | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
assert not JWTRefreshToken.objects.filter(token=token).exists() | ||
new_token = response.data.get("refresh_token") | ||
assert JWTRefreshToken.objects.filter(token=new_token).exists() | ||
|
||
|
||
def test_should_include_access_and_refresh_token_in_response(db, api_client, url): | ||
token = JWTRefreshToken.generate_refresh_token() | ||
db_token = JWTRefreshToken(token=token) | ||
db_token.save() | ||
response = api_client.post( | ||
url, | ||
follow=True, | ||
data={ | ||
'refresh_token': token, | ||
}, | ||
) | ||
assert response.status_code == 200 | ||
assert "access_token" in response.data | ||
assert "refresh_token" in response.data | ||
|
||
|
||
@pytest.fixture() | ||
def url(): | ||
return reverse('api:1:jwt-refresh') | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def jwtconf_mock(private_key, nav_name) -> str: | ||
"""Mocks the get_nave_name and get_nav_private_key functions for | ||
the JWTConf class | ||
""" | ||
with patch("nav.models.api.JWTConf") as _jwtconf_mock: | ||
instance = _jwtconf_mock.return_value | ||
instance.get_nav_name = Mock(return_value=nav_name) | ||
instance.get_nav_private_key = Mock(return_value=private_key) | ||
yield _jwtconf_mock | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def private_key() -> str: | ||
"""Yields a private key in PEM format""" | ||
key = """-----BEGIN PRIVATE KEY----- | ||
MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCp+4AEZM4uYZKu | ||
/hrKzySMTFFx3/ncWo6XAFpADQHXLOwRB9Xh1/OwigHiqs/wHRAAmnrlkwCCQA8r | ||
xiHBAMjp5ApbkyggQz/DVijrpSba6Tiy1cyBTZC3cvOK2FpJzsakJLhIXD1HaULO | ||
ClyIJB/YrmHmQc8SL3Uzou5mMpdcBC2pzwmEW1cvQURpnvgrDF8V86GrQkjK6nIP | ||
IEeuW6kbD5lWFAPfLf1ohDWex3yxeSFyXNRApJhbF4HrKFemPkOi7acsky38UomQ | ||
jZgAMHPotJNkQvAHcnXHhg0F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VxQ2f8s/GajKwEz/BDwqievnVH0zJxmr/kyyqw5Ybh5HVvA1GfqaVRssJ | ||
DvTjZQDft0a9Lyy7ix1OS2XgkcMjTWj840LNPwKBgDPXMBgL5h41jd7jCsXzPhyr | ||
V96RzQkPcKsoVvrCoNi8eoEYgRd9jwfiU12rlXv+fgVXrrfMoJBoYT6YtrxEJVdM | ||
RAjRpnE8PMqCUA8Rd7RFK9Vp5Uo8RxTNvk9yPvDv1+lHHV7lEltIk5PXuKPHIrc1 | ||
nNUyhzvJs2Qba2L/huNC | ||
-----END PRIVATE KEY-----""" | ||
yield key | ||
|
||
|
||
@pytest.fixture() | ||
def public_key() -> str: | ||
"""Yields a public key in PEM format""" | ||
key = """-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqfuABGTOLmGSrv4ays8k | ||
jExRcd/53FqOlwBaQA0B1yzsEQfV4dfzsIoB4qrP8B0QAJp65ZMAgkAPK8YhwQDI | ||
6eQKW5MoIEM/w1Yo66Um2uk4stXMgU2Qt3LzithaSc7GpCS4SFw9R2lCzgpciCQf | ||
2K5h5kHPEi91M6LuZjKXXAQtqc8JhFtXL0FEaZ74KwxfFfOhq0JIyupyDyBHrlup | ||
Gw+ZVhQD3y39aIQ1nsd8sXkhclzUQKSYWxeB6yhXpj5Dou2nLJMt/FKJkI2YADBz | ||
6LSTZELwB3J1x4YNBXFhnaIb+W3PwrbfRsGTsyccMiSwQbEnsGxN9dE2Yt6C1NU5 | ||
rwIDAQAB | ||
-----END PUBLIC KEY-----""" | ||
yield key | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def nav_name() -> str: | ||
yield "nav" |