authentik.enterprise.providers.ssf.tests.test_config

 1import json
 2
 3from django.urls import reverse
 4from rest_framework.test import APITestCase
 5
 6from authentik.core.models import Application
 7from authentik.core.tests.utils import create_test_cert
 8from authentik.enterprise.providers.ssf.models import (
 9    SSFProvider,
10)
11from authentik.lib.generators import generate_id
12
13
14class TestConfiguration(APITestCase):
15    def setUp(self):
16        self.application = Application.objects.create(name=generate_id(), slug=generate_id())
17        self.provider = SSFProvider.objects.create(
18            name=generate_id(),
19            signing_key=create_test_cert(),
20            backchannel_application=self.application,
21        )
22
23    def test_config_fetch(self):
24        """test SSF configuration (unauthenticated)"""
25        res = self.client.get(
26            reverse(
27                "authentik_providers_ssf:configuration",
28                kwargs={"application_slug": self.application.slug},
29            ),
30        )
31        self.assertEqual(res.status_code, 200)
32        content = json.loads(res.content)
33        self.assertEqual(content["spec_version"], "1_0-ID2")
34
35    def test_config_fetch_authenticated(self):
36        """test SSF configuration (authenticated)"""
37        res = self.client.get(
38            reverse(
39                "authentik_providers_ssf:configuration",
40                kwargs={"application_slug": self.application.slug},
41            ),
42            HTTP_AUTHORIZATION=f"Bearer {self.provider.token.key}",
43        )
44        self.assertEqual(res.status_code, 200)
45        content = json.loads(res.content)
46        self.assertEqual(content["spec_version"], "1_0-ID2")
47
48    def test_config_not_found(self):
49        """test SSF configuration (authenticated)"""
50        self.provider.delete()
51        res = self.client.get(
52            reverse(
53                "authentik_providers_ssf:configuration",
54                kwargs={"application_slug": self.application.slug},
55            ),
56            HTTP_AUTHORIZATION=f"Bearer {self.provider.token.key}",
57        )
58        self.assertEqual(res.status_code, 404)
class TestConfiguration(rest_framework.test.APITestCase):
15class TestConfiguration(APITestCase):
16    def setUp(self):
17        self.application = Application.objects.create(name=generate_id(), slug=generate_id())
18        self.provider = SSFProvider.objects.create(
19            name=generate_id(),
20            signing_key=create_test_cert(),
21            backchannel_application=self.application,
22        )
23
24    def test_config_fetch(self):
25        """test SSF configuration (unauthenticated)"""
26        res = self.client.get(
27            reverse(
28                "authentik_providers_ssf:configuration",
29                kwargs={"application_slug": self.application.slug},
30            ),
31        )
32        self.assertEqual(res.status_code, 200)
33        content = json.loads(res.content)
34        self.assertEqual(content["spec_version"], "1_0-ID2")
35
36    def test_config_fetch_authenticated(self):
37        """test SSF configuration (authenticated)"""
38        res = self.client.get(
39            reverse(
40                "authentik_providers_ssf:configuration",
41                kwargs={"application_slug": self.application.slug},
42            ),
43            HTTP_AUTHORIZATION=f"Bearer {self.provider.token.key}",
44        )
45        self.assertEqual(res.status_code, 200)
46        content = json.loads(res.content)
47        self.assertEqual(content["spec_version"], "1_0-ID2")
48
49    def test_config_not_found(self):
50        """test SSF configuration (authenticated)"""
51        self.provider.delete()
52        res = self.client.get(
53            reverse(
54                "authentik_providers_ssf:configuration",
55                kwargs={"application_slug": self.application.slug},
56            ),
57            HTTP_AUTHORIZATION=f"Bearer {self.provider.token.key}",
58        )
59        self.assertEqual(res.status_code, 404)

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):
16    def setUp(self):
17        self.application = Application.objects.create(name=generate_id(), slug=generate_id())
18        self.provider = SSFProvider.objects.create(
19            name=generate_id(),
20            signing_key=create_test_cert(),
21            backchannel_application=self.application,
22        )

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

def test_config_fetch(self):
24    def test_config_fetch(self):
25        """test SSF configuration (unauthenticated)"""
26        res = self.client.get(
27            reverse(
28                "authentik_providers_ssf:configuration",
29                kwargs={"application_slug": self.application.slug},
30            ),
31        )
32        self.assertEqual(res.status_code, 200)
33        content = json.loads(res.content)
34        self.assertEqual(content["spec_version"], "1_0-ID2")

test SSF configuration (unauthenticated)

def test_config_fetch_authenticated(self):
36    def test_config_fetch_authenticated(self):
37        """test SSF configuration (authenticated)"""
38        res = self.client.get(
39            reverse(
40                "authentik_providers_ssf:configuration",
41                kwargs={"application_slug": self.application.slug},
42            ),
43            HTTP_AUTHORIZATION=f"Bearer {self.provider.token.key}",
44        )
45        self.assertEqual(res.status_code, 200)
46        content = json.loads(res.content)
47        self.assertEqual(content["spec_version"], "1_0-ID2")

test SSF configuration (authenticated)

def test_config_not_found(self):
49    def test_config_not_found(self):
50        """test SSF configuration (authenticated)"""
51        self.provider.delete()
52        res = self.client.get(
53            reverse(
54                "authentik_providers_ssf:configuration",
55                kwargs={"application_slug": self.application.slug},
56            ),
57            HTTP_AUTHORIZATION=f"Bearer {self.provider.token.key}",
58        )
59        self.assertEqual(res.status_code, 404)

test SSF configuration (authenticated)