From d1c4818724e71a936a598cf30fafd5c161d8f169 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 5 Jul 2022 23:20:45 +0200 Subject: [PATCH] policies: improve api test coverage Signed-off-by: Jens Langhammer --- authentik/policies/api/policies.py | 2 +- authentik/policies/tests/test_policies_api.py | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/authentik/policies/api/policies.py b/authentik/policies/api/policies.py index 3f7d7b6dc..0ef0d9775 100644 --- a/authentik/policies/api/policies.py +++ b/authentik/policies/api/policies.py @@ -158,7 +158,7 @@ class PolicyViewSet( pk=test_params.validated_data["user"].pk ) if not users.exists(): - raise PermissionDenied() + return Response(status=400) p_request = PolicyRequest(users.first()) p_request.debug = True diff --git a/authentik/policies/tests/test_policies_api.py b/authentik/policies/tests/test_policies_api.py index 1eb9cf5d3..ecb83925b 100644 --- a/authentik/policies/tests/test_policies_api.py +++ b/authentik/policies/tests/test_policies_api.py @@ -1,11 +1,13 @@ """Test policies API""" from json import loads +from unittest.mock import MagicMock, patch from django.urls import reverse from rest_framework.test import APITestCase from authentik.core.tests.utils import create_test_admin_user from authentik.policies.dummy.models import DummyPolicy +from authentik.policies.types import PolicyResult class TestPoliciesAPI(APITestCase): @@ -17,8 +19,10 @@ class TestPoliciesAPI(APITestCase): self.user = create_test_admin_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""" + passes_mock.return_value = PolicyResult(True, "dummy") response = self.client.post( reverse("authentik_api:policy-test", kwargs={"pk": self.policy.pk}), data={ @@ -28,6 +32,22 @@ class TestPoliciesAPI(APITestCase): body = loads(response.content.decode()) self.assertEqual(body["passing"], True) 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): """Test Policy's types endpoint""" @@ -35,3 +55,17 @@ class TestPoliciesAPI(APITestCase): reverse("authentik_api:policy-types"), ) 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)