authentik.enterprise.required_actions.tests.test_api

Required action user API tests

 1"""Required action user API tests"""
 2
 3from django.urls import reverse
 4from rest_framework.test import APITestCase
 5
 6from authentik.core.tests.utils import create_test_admin_user, create_test_flow, create_test_user
 7from authentik.enterprise.required_actions import USER_ATTRIBUTE_REQUIRED_ACTIONS
 8from authentik.flows.models import FlowAuthenticationRequirement, FlowDesignation
 9
10
11class TestRequiredActionsAPI(APITestCase):
12    def setUp(self):
13        self.client.force_login(create_test_admin_user())
14        self.user = create_test_user()
15        self.url = reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk})
16
17    def patch_attributes(self, attributes: dict):
18        return self.client.patch(self.url, {"attributes": attributes}, format="json")
19
20    def test_set(self):
21        """A flow slug or a list of flow slugs is accepted"""
22        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
23        for value in [flow.slug, [flow.slug]]:
24            with self.subTest(value=value):
25                response = self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: value})
26                self.assertEqual(response.status_code, 200)
27                self.user.refresh_from_db()
28                self.assertEqual(self.user.attributes[USER_ATTRIBUTE_REQUIRED_ACTIONS], value)
29
30    def test_set_invalid(self):
31        """Flows that are missing or cannot run, and values that aren't slugs, are rejected"""
32        authentication = create_test_flow(FlowDesignation.AUTHENTICATION)
33        invalidation = create_test_flow(FlowDesignation.INVALIDATION)
34        recovery = create_test_flow(
35            FlowDesignation.RECOVERY,
36            authentication=FlowAuthenticationRequirement.REQUIRE_UNAUTHENTICATED,
37        )
38        for value in [
39            "does-not-exist",
40            [authentication.slug],
41            [invalidation.slug],
42            [recovery.slug],
43            [42],
44            None,
45        ]:
46            with self.subTest(value=value):
47                response = self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: value})
48                self.assertEqual(response.status_code, 400)
49                self.assertEqual(
50                    response.json(),
51                    {
52                        "attributes": [
53                            "Required actions must reference existing flows that a logged-in "
54                            "user can run, other than authentication or invalidation flows."
55                        ]
56                    },
57                )
58
59    def test_unchanged_invalid_actions_allow_other_edits(self):
60        """Deleting a required flow doesn't block unrelated edits to the user"""
61        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
62        self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: [flow.slug]})
63        flow.delete()
64        response = self.patch_attributes(
65            {USER_ATTRIBUTE_REQUIRED_ACTIONS: [flow.slug], "department": "Support"}
66        )
67        self.assertEqual(response.status_code, 200)
class TestRequiredActionsAPI(rest_framework.test.APITestCase):
12class TestRequiredActionsAPI(APITestCase):
13    def setUp(self):
14        self.client.force_login(create_test_admin_user())
15        self.user = create_test_user()
16        self.url = reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk})
17
18    def patch_attributes(self, attributes: dict):
19        return self.client.patch(self.url, {"attributes": attributes}, format="json")
20
21    def test_set(self):
22        """A flow slug or a list of flow slugs is accepted"""
23        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
24        for value in [flow.slug, [flow.slug]]:
25            with self.subTest(value=value):
26                response = self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: value})
27                self.assertEqual(response.status_code, 200)
28                self.user.refresh_from_db()
29                self.assertEqual(self.user.attributes[USER_ATTRIBUTE_REQUIRED_ACTIONS], value)
30
31    def test_set_invalid(self):
32        """Flows that are missing or cannot run, and values that aren't slugs, are rejected"""
33        authentication = create_test_flow(FlowDesignation.AUTHENTICATION)
34        invalidation = create_test_flow(FlowDesignation.INVALIDATION)
35        recovery = create_test_flow(
36            FlowDesignation.RECOVERY,
37            authentication=FlowAuthenticationRequirement.REQUIRE_UNAUTHENTICATED,
38        )
39        for value in [
40            "does-not-exist",
41            [authentication.slug],
42            [invalidation.slug],
43            [recovery.slug],
44            [42],
45            None,
46        ]:
47            with self.subTest(value=value):
48                response = self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: value})
49                self.assertEqual(response.status_code, 400)
50                self.assertEqual(
51                    response.json(),
52                    {
53                        "attributes": [
54                            "Required actions must reference existing flows that a logged-in "
55                            "user can run, other than authentication or invalidation flows."
56                        ]
57                    },
58                )
59
60    def test_unchanged_invalid_actions_allow_other_edits(self):
61        """Deleting a required flow doesn't block unrelated edits to the user"""
62        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
63        self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: [flow.slug]})
64        flow.delete()
65        response = self.patch_attributes(
66            {USER_ATTRIBUTE_REQUIRED_ACTIONS: [flow.slug], "department": "Support"}
67        )
68        self.assertEqual(response.status_code, 200)

Similar to TransactionTestCase, but use transaction.atomic() to achieve test isolation.

In most situations, TestCase should be preferred to TransactionTestCase as it allows faster execution. However, there are some situations where using TransactionTestCase might be necessary (e.g. testing some transactional behavior).

On database backends with no transaction support, TestCase behaves as TransactionTestCase.

def setUp(self):
13    def setUp(self):
14        self.client.force_login(create_test_admin_user())
15        self.user = create_test_user()
16        self.url = reverse("authentik_api:user-detail", kwargs={"pk": self.user.pk})

Hook method for setting up the test fixture before exercising it.

def patch_attributes(self, attributes: dict):
18    def patch_attributes(self, attributes: dict):
19        return self.client.patch(self.url, {"attributes": attributes}, format="json")
def test_set(self):
21    def test_set(self):
22        """A flow slug or a list of flow slugs is accepted"""
23        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
24        for value in [flow.slug, [flow.slug]]:
25            with self.subTest(value=value):
26                response = self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: value})
27                self.assertEqual(response.status_code, 200)
28                self.user.refresh_from_db()
29                self.assertEqual(self.user.attributes[USER_ATTRIBUTE_REQUIRED_ACTIONS], value)

A flow slug or a list of flow slugs is accepted

def test_set_invalid(self):
31    def test_set_invalid(self):
32        """Flows that are missing or cannot run, and values that aren't slugs, are rejected"""
33        authentication = create_test_flow(FlowDesignation.AUTHENTICATION)
34        invalidation = create_test_flow(FlowDesignation.INVALIDATION)
35        recovery = create_test_flow(
36            FlowDesignation.RECOVERY,
37            authentication=FlowAuthenticationRequirement.REQUIRE_UNAUTHENTICATED,
38        )
39        for value in [
40            "does-not-exist",
41            [authentication.slug],
42            [invalidation.slug],
43            [recovery.slug],
44            [42],
45            None,
46        ]:
47            with self.subTest(value=value):
48                response = self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: value})
49                self.assertEqual(response.status_code, 400)
50                self.assertEqual(
51                    response.json(),
52                    {
53                        "attributes": [
54                            "Required actions must reference existing flows that a logged-in "
55                            "user can run, other than authentication or invalidation flows."
56                        ]
57                    },
58                )

Flows that are missing or cannot run, and values that aren't slugs, are rejected

def test_unchanged_invalid_actions_allow_other_edits(self):
60    def test_unchanged_invalid_actions_allow_other_edits(self):
61        """Deleting a required flow doesn't block unrelated edits to the user"""
62        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
63        self.patch_attributes({USER_ATTRIBUTE_REQUIRED_ACTIONS: [flow.slug]})
64        flow.delete()
65        response = self.patch_attributes(
66            {USER_ATTRIBUTE_REQUIRED_ACTIONS: [flow.slug], "department": "Support"}
67        )
68        self.assertEqual(response.status_code, 200)

Deleting a required flow doesn't block unrelated edits to the user