Commit f68ad01e authored by hynek's avatar hynek 🤤
Browse files

test(dhrep/token): cover 100% of token model

parent 4c7172ae
......@@ -2,7 +2,7 @@ import pytest
from django.core.exceptions import ImproperlyConfigured
from discuss_data.dhrep.token import Token, TokenHelper
from discuss_data.dhrep.token import Token, TokenHelper, InvalidTokenException
class TestTokenHelper:
......@@ -44,6 +44,13 @@ class TestTokenHelper:
assert t.expires_in == "86400"
assert t.scope == "read,write"
@staticmethod
def test_create_invalid_token_object():
token = "aksess_token=0d0#344e5-96c0-441d-bc2c-8230f0cea2a9&token_type=bearer&expires_in=86400&scope=read,write"
th: TokenHelper = TokenHelper()
with pytest.raises(InvalidTokenException):
th.create_token_object(token)
@staticmethod
def test_invalid_token():
t = Token(
......@@ -51,3 +58,42 @@ class TestTokenHelper:
)
th = TokenHelper()
assert not th.check(t)
@staticmethod
def test_create_token_object_from_dict():
token = {
"access_token": "0d0#344e5-96c0-441d-bc2c-8230f0cea2a9",
"token_type": "bearer",
"expires_in": "86400",
"scope": "read,write",
}
th = TokenHelper()
t = th.create_token_object_from_dict(token)
assert t.access_token == "0d0#344e5-96c0-441d-bc2c-8230f0cea2a9"
assert t.token_type == "bearer"
assert t.expires_in == "86400"
assert t.scope == "read,write"
@staticmethod
def test_create_invalid_key_token_object_from_dict():
token = {
"aksess_token": "0d0#344e5-96c0-441d-bc2c-8230f0cea2a9",
"token_type": "bearer",
"expires_in": "86400",
"scope": "read,write",
}
th = TokenHelper()
with pytest.raises(InvalidTokenException):
th.create_token_object_from_dict(token)
@staticmethod
def test_to_dict():
token = Token(
"0d0#344e5-96c0-441d-bc2c-8230f0cea2a9", "bearer", "86400", "read,write"
)
th = TokenHelper()
t = th.to_dict(token)
assert t["access_token"] == "0d0#344e5-96c0-441d-bc2c-8230f0cea2a9"
assert t["token_type"] == "bearer"
assert t["expires_in"] == "86400"
assert t["scope"] == "read,write"
import pytest
from django.urls import reverse, resolve
class TestUrls:
def test_get_token(self):
@staticmethod
def test_get_token():
assert reverse("dhrep:get_token") == "/dhrep/token/"
assert resolve("/dhrep/token/").view_name == "dhrep:get_token"
def test_show_token(self):
@staticmethod
def test_show_token():
token: str = "access_token=0d03344e5-96c0-441d-bc2c-8230f0cea2a9&token_type=bearer&expires_in=86400&scope=read,write"
assert (
reverse("dhrep:show_token", kwargs={"token": token},)
reverse("dhrep:show_token", kwargs={"token": token})
== "/dhrep/token/" + token
)
assert resolve("/dhrep/token/" + token).view_name == "dhrep:show_token"
......
import pytest
from django.core.exceptions import ValidationError, ImproperlyConfigured
from django.test import Client
from django.test import Client, TestCase
from django.urls import reverse
from discuss_data.dhrep.views import get_token, show_token
from django.contrib.auth import get_user_model
class TestViews:
client = Client()
User = get_user_model()
@pytest.mark.django_db
def test_get_token(self):
class TestViews(TestCase):
def setUp(self):
troi = User(
first_name="Deanna",
last_name="Troi",
username="dtroi",
name_prefix="Commander",
)
troi.save()
def test_get_token_no_login(self):
response = self.client.get(reverse("dhrep:get_token"))
assert response.status_code == 200
assert "get a token" in str(response.content)
assert response.status_code == 302
assert response.url == "/shib/login/?next=" + reverse("dhrep:get_token")
@pytest.mark.django_db
def test_show_unautorized_token(self):
def test_show_token_no_login(self):
token: str = "access_token=0d03344e5-96c0-441d-bc2c-8230f0cea2a9&token_type=bearer&expires_in=86400&scope=read,write"
response = self.client.get(reverse("dhrep:show_token", kwargs={"token": token}))
assert response.status_code == 302
def test_get_token(self):
self.client.force_login(User.objects.get_or_create(username="dtroi")[0])
response = self.client.get(reverse("dhrep:get_token"))
assert response.status_code == 200
assert "Unauthorized" in str(response.content)
assert "get a token" in str(response.content)
@pytest.mark.django_db
def test_show_gone_token(self):
token: str = "access_token=6c192873-768d-4c43-81e2-5e388969020c&token_type=bearer&expires_in=86400&scope=read,write"
def test_show_invalid_token(self):
self.client.force_login(User.objects.get_or_create(username="dtroi")[0])
token: str = "access_token=0d03344e5-96c0-441d-bc2c-8230f0cea2a9&token_type=bearer&expires_in=86400&scope=read,write"
response = self.client.get(reverse("dhrep:show_token", kwargs={"token": token}))
assert response.status_code == 200
assert "Gone" in str(response.content)
assert "get a token" in str(response.content)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment