authentik.enterprise.requests.tests.test_request_rules_api

 1from django.urls import reverse
 2from rest_framework.test import APITestCase
 3
 4from authentik.brands.models import Brand
 5from authentik.core.tests.utils import create_test_admin_user, create_test_flow
 6from authentik.enterprise.requests.models import RequestRule
 7from authentik.enterprise.tests import enterprise_test
 8from authentik.flows.models import Flow
 9from authentik.lib.generators import generate_id
10
11
12class RequestRuleAPITests(APITestCase):
13    """`RequestRuleSerializer.validate()` must require a request_flow on either the
14    rule itself or the current brand, since a rule with neither can never resolve
15    a flow to send a requester through."""
16
17    def _set_brand_flow(self, flow: Flow | None):
18        brand, _ = Brand.objects.get_or_create(default=True, defaults={"domain": generate_id()})
19        brand.flow_request = flow
20        brand.save()
21        return brand
22
23    @enterprise_test()
24    def test_create_rejects_no_flow_anywhere(self):
25        """Rule has no request_flow and the brand has none either -> rejected."""
26        self._set_brand_flow(None)
27        self.client.force_login(create_test_admin_user())
28
29        res = self.client.post(
30            reverse("authentik_api:requestrule-list"),
31            data={"name": generate_id()},
32        )
33        self.assertEqual(res.status_code, 400, res.content)
34        self.assertFalse(RequestRule.objects.filter(name__isnull=False).exists())
35
36    @enterprise_test()
37    def test_create_allowed_with_brand_flow(self):
38        """Rule has no request_flow of its own, but the brand has one -> allowed."""
39        self._set_brand_flow(create_test_flow())
40        self.client.force_login(create_test_admin_user())
41
42        res = self.client.post(
43            reverse("authentik_api:requestrule-list"),
44            data={"name": generate_id()},
45        )
46        self.assertEqual(res.status_code, 201, res.content)
47
48    @enterprise_test()
49    def test_create_allowed_with_own_flow(self):
50        """Rule sets its own request_flow, regardless of the brand -> allowed."""
51        self._set_brand_flow(None)
52        self.client.force_login(create_test_admin_user())
53        rule_flow = create_test_flow()
54
55        res = self.client.post(
56            reverse("authentik_api:requestrule-list"),
57            data={"name": generate_id(), "request_flow": str(rule_flow.pk)},
58        )
59        self.assertEqual(res.status_code, 201, res.content)
60
61    @enterprise_test()
62    def test_update_rejects_clearing_flow_with_no_brand_flow(self):
63        """An existing rule's own request_flow can't be cleared if the brand has none."""
64        self._set_brand_flow(None)
65        self.client.force_login(create_test_admin_user())
66        rule = RequestRule.objects.create(name=generate_id(), request_flow=create_test_flow())
67
68        res = self.client.patch(
69            reverse("authentik_api:requestrule-detail", kwargs={"pk": rule.pk}),
70            data={"request_flow": None},
71        )
72        self.assertEqual(res.status_code, 400, res.content)
class RequestRuleAPITests(rest_framework.test.APITestCase):
13class RequestRuleAPITests(APITestCase):
14    """`RequestRuleSerializer.validate()` must require a request_flow on either the
15    rule itself or the current brand, since a rule with neither can never resolve
16    a flow to send a requester through."""
17
18    def _set_brand_flow(self, flow: Flow | None):
19        brand, _ = Brand.objects.get_or_create(default=True, defaults={"domain": generate_id()})
20        brand.flow_request = flow
21        brand.save()
22        return brand
23
24    @enterprise_test()
25    def test_create_rejects_no_flow_anywhere(self):
26        """Rule has no request_flow and the brand has none either -> rejected."""
27        self._set_brand_flow(None)
28        self.client.force_login(create_test_admin_user())
29
30        res = self.client.post(
31            reverse("authentik_api:requestrule-list"),
32            data={"name": generate_id()},
33        )
34        self.assertEqual(res.status_code, 400, res.content)
35        self.assertFalse(RequestRule.objects.filter(name__isnull=False).exists())
36
37    @enterprise_test()
38    def test_create_allowed_with_brand_flow(self):
39        """Rule has no request_flow of its own, but the brand has one -> allowed."""
40        self._set_brand_flow(create_test_flow())
41        self.client.force_login(create_test_admin_user())
42
43        res = self.client.post(
44            reverse("authentik_api:requestrule-list"),
45            data={"name": generate_id()},
46        )
47        self.assertEqual(res.status_code, 201, res.content)
48
49    @enterprise_test()
50    def test_create_allowed_with_own_flow(self):
51        """Rule sets its own request_flow, regardless of the brand -> allowed."""
52        self._set_brand_flow(None)
53        self.client.force_login(create_test_admin_user())
54        rule_flow = create_test_flow()
55
56        res = self.client.post(
57            reverse("authentik_api:requestrule-list"),
58            data={"name": generate_id(), "request_flow": str(rule_flow.pk)},
59        )
60        self.assertEqual(res.status_code, 201, res.content)
61
62    @enterprise_test()
63    def test_update_rejects_clearing_flow_with_no_brand_flow(self):
64        """An existing rule's own request_flow can't be cleared if the brand has none."""
65        self._set_brand_flow(None)
66        self.client.force_login(create_test_admin_user())
67        rule = RequestRule.objects.create(name=generate_id(), request_flow=create_test_flow())
68
69        res = self.client.patch(
70            reverse("authentik_api:requestrule-detail", kwargs={"pk": rule.pk}),
71            data={"request_flow": None},
72        )
73        self.assertEqual(res.status_code, 400, res.content)

RequestRuleSerializer.validate() must require a request_flow on either the rule itself or the current brand, since a rule with neither can never resolve a flow to send a requester through.

@enterprise_test()
def test_create_rejects_no_flow_anywhere(self):
24    @enterprise_test()
25    def test_create_rejects_no_flow_anywhere(self):
26        """Rule has no request_flow and the brand has none either -> rejected."""
27        self._set_brand_flow(None)
28        self.client.force_login(create_test_admin_user())
29
30        res = self.client.post(
31            reverse("authentik_api:requestrule-list"),
32            data={"name": generate_id()},
33        )
34        self.assertEqual(res.status_code, 400, res.content)
35        self.assertFalse(RequestRule.objects.filter(name__isnull=False).exists())

Rule has no request_flow and the brand has none either -> rejected.

@enterprise_test()
def test_create_allowed_with_brand_flow(self):
37    @enterprise_test()
38    def test_create_allowed_with_brand_flow(self):
39        """Rule has no request_flow of its own, but the brand has one -> allowed."""
40        self._set_brand_flow(create_test_flow())
41        self.client.force_login(create_test_admin_user())
42
43        res = self.client.post(
44            reverse("authentik_api:requestrule-list"),
45            data={"name": generate_id()},
46        )
47        self.assertEqual(res.status_code, 201, res.content)

Rule has no request_flow of its own, but the brand has one -> allowed.

@enterprise_test()
def test_create_allowed_with_own_flow(self):
49    @enterprise_test()
50    def test_create_allowed_with_own_flow(self):
51        """Rule sets its own request_flow, regardless of the brand -> allowed."""
52        self._set_brand_flow(None)
53        self.client.force_login(create_test_admin_user())
54        rule_flow = create_test_flow()
55
56        res = self.client.post(
57            reverse("authentik_api:requestrule-list"),
58            data={"name": generate_id(), "request_flow": str(rule_flow.pk)},
59        )
60        self.assertEqual(res.status_code, 201, res.content)

Rule sets its own request_flow, regardless of the brand -> allowed.

@enterprise_test()
def test_update_rejects_clearing_flow_with_no_brand_flow(self):
62    @enterprise_test()
63    def test_update_rejects_clearing_flow_with_no_brand_flow(self):
64        """An existing rule's own request_flow can't be cleared if the brand has none."""
65        self._set_brand_flow(None)
66        self.client.force_login(create_test_admin_user())
67        rule = RequestRule.objects.create(name=generate_id(), request_flow=create_test_flow())
68
69        res = self.client.patch(
70            reverse("authentik_api:requestrule-detail", kwargs={"pk": rule.pk}),
71            data={"request_flow": None},
72        )
73        self.assertEqual(res.status_code, 400, res.content)

An existing rule's own request_flow can't be cleared if the brand has none.