policies: improve api test coverage
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
9f736a9d99
commit
d1c4818724
|
@ -158,7 +158,7 @@ class PolicyViewSet(
|
||||||
pk=test_params.validated_data["user"].pk
|
pk=test_params.validated_data["user"].pk
|
||||||
)
|
)
|
||||||
if not users.exists():
|
if not users.exists():
|
||||||
raise PermissionDenied()
|
return Response(status=400)
|
||||||
|
|
||||||
p_request = PolicyRequest(users.first())
|
p_request = PolicyRequest(users.first())
|
||||||
p_request.debug = True
|
p_request.debug = True
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
"""Test policies API"""
|
"""Test policies API"""
|
||||||
from json import loads
|
from json import loads
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
from authentik.core.tests.utils import create_test_admin_user
|
from authentik.core.tests.utils import create_test_admin_user
|
||||||
from authentik.policies.dummy.models import DummyPolicy
|
from authentik.policies.dummy.models import DummyPolicy
|
||||||
|
from authentik.policies.types import PolicyResult
|
||||||
|
|
||||||
|
|
||||||
class TestPoliciesAPI(APITestCase):
|
class TestPoliciesAPI(APITestCase):
|
||||||
|
@ -17,8 +19,10 @@ class TestPoliciesAPI(APITestCase):
|
||||||
self.user = create_test_admin_user()
|
self.user = create_test_admin_user()
|
||||||
self.client.force_login(self.user)
|
self.client.force_login(self.user)
|
||||||
|
|
||||||
def test_test_call(self):
|
@patch("authentik.policies.dummy.models.DummyPolicy.passes")
|
||||||
|
def test_test_call(self, passes_mock: MagicMock):
|
||||||
"""Test Policy's test endpoint"""
|
"""Test Policy's test endpoint"""
|
||||||
|
passes_mock.return_value = PolicyResult(True, "dummy")
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
reverse("authentik_api:policy-test", kwargs={"pk": self.policy.pk}),
|
reverse("authentik_api:policy-test", kwargs={"pk": self.policy.pk}),
|
||||||
data={
|
data={
|
||||||
|
@ -28,6 +32,22 @@ class TestPoliciesAPI(APITestCase):
|
||||||
body = loads(response.content.decode())
|
body = loads(response.content.decode())
|
||||||
self.assertEqual(body["passing"], True)
|
self.assertEqual(body["passing"], True)
|
||||||
self.assertEqual(body["messages"], ["dummy"])
|
self.assertEqual(body["messages"], ["dummy"])
|
||||||
|
self.assertEqual(body["log_messages"], [])
|
||||||
|
|
||||||
|
def test_test_call_invalid(self):
|
||||||
|
"""Test invalid policy test"""
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:policy-test", kwargs={"pk": self.policy.pk}),
|
||||||
|
data={},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:policy-test", kwargs={"pk": self.policy.pk}),
|
||||||
|
data={
|
||||||
|
"user": self.user.pk + 1,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
|
||||||
def test_types(self):
|
def test_types(self):
|
||||||
"""Test Policy's types endpoint"""
|
"""Test Policy's types endpoint"""
|
||||||
|
@ -35,3 +55,17 @@ class TestPoliciesAPI(APITestCase):
|
||||||
reverse("authentik_api:policy-types"),
|
reverse("authentik_api:policy-types"),
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
def test_cache_info(self):
|
||||||
|
"""Test Policy's cache_info endpoint"""
|
||||||
|
response = self.client.get(
|
||||||
|
reverse("authentik_api:policy-cache-info"),
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
def test_cache_clear(self):
|
||||||
|
"""Test Policy's cache_clear endpoint"""
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:policy-cache-clear"),
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 204)
|
||||||
|
|
Reference in New Issue