authentik.core.tests.test_source_api

 1from json import loads
 2
 3from django.apps import apps
 4from django.urls import reverse
 5from rest_framework.test import APITestCase
 6
 7from authentik.core.tests.utils import create_test_admin_user
 8from authentik.lib.generators import generate_id
 9from authentik.sources.oauth.models import OAuthSource
10
11GITHUB_ICON_THEMED_URLS = {
12    "light": "/static/authentik/sources/github/light.svg",
13    "dark": "/static/authentik/sources/github/dark.svg",
14}
15
16
17class TestSourceAPI(APITestCase):
18    def setUp(self) -> None:
19        self.user = create_test_admin_user()
20        self.client.force_login(self.user)
21
22    def test_builtin_source_used_by(self):
23        """Test Providers's types endpoint"""
24        apps.get_app_config("authentik_core").source_inbuilt()
25        response = self.client.get(
26            reverse("authentik_api:source-used-by", kwargs={"slug": "authentik-built-in"}),
27        )
28        self.assertEqual(response.status_code, 200)
29
30    def test_user_settings_icon_themed_urls(self):
31        """Test themed icon URLs of user settings"""
32        github = OAuthSource.objects.create(
33            name=generate_id(), slug=generate_id(), provider_type="github"
34        )
35        custom = OAuthSource.objects.create(
36            name=generate_id(), slug=generate_id(), icon="https://example.com/logo.png"
37        )
38
39        response = self.client.get(reverse("authentik_api:source-user-settings"))
40
41        self.assertEqual(response.status_code, 200)
42        settings = {setting["object_uid"]: setting for setting in loads(response.content)}
43        self.assertEqual(settings[github.slug]["icon_themed_urls"], GITHUB_ICON_THEMED_URLS)
44        self.assertIsNone(settings[custom.slug]["icon_themed_urls"])
45
46    def test_types_icon_themed_urls(self):
47        """Test themed icon URLs of source types"""
48        response = self.client.get(reverse("authentik_api:source-types"))
49
50        self.assertEqual(response.status_code, 200)
51        types = {source_type["model_name"]: source_type for source_type in loads(response.content)}
52        self.assertEqual(types["githuboauthsource"]["icon_themed_urls"], GITHUB_ICON_THEMED_URLS)
GITHUB_ICON_THEMED_URLS = {'light': '/static/authentik/sources/github/light.svg', 'dark': '/static/authentik/sources/github/dark.svg'}
class TestSourceAPI(rest_framework.test.APITestCase):
18class TestSourceAPI(APITestCase):
19    def setUp(self) -> None:
20        self.user = create_test_admin_user()
21        self.client.force_login(self.user)
22
23    def test_builtin_source_used_by(self):
24        """Test Providers's types endpoint"""
25        apps.get_app_config("authentik_core").source_inbuilt()
26        response = self.client.get(
27            reverse("authentik_api:source-used-by", kwargs={"slug": "authentik-built-in"}),
28        )
29        self.assertEqual(response.status_code, 200)
30
31    def test_user_settings_icon_themed_urls(self):
32        """Test themed icon URLs of user settings"""
33        github = OAuthSource.objects.create(
34            name=generate_id(), slug=generate_id(), provider_type="github"
35        )
36        custom = OAuthSource.objects.create(
37            name=generate_id(), slug=generate_id(), icon="https://example.com/logo.png"
38        )
39
40        response = self.client.get(reverse("authentik_api:source-user-settings"))
41
42        self.assertEqual(response.status_code, 200)
43        settings = {setting["object_uid"]: setting for setting in loads(response.content)}
44        self.assertEqual(settings[github.slug]["icon_themed_urls"], GITHUB_ICON_THEMED_URLS)
45        self.assertIsNone(settings[custom.slug]["icon_themed_urls"])
46
47    def test_types_icon_themed_urls(self):
48        """Test themed icon URLs of source types"""
49        response = self.client.get(reverse("authentik_api:source-types"))
50
51        self.assertEqual(response.status_code, 200)
52        types = {source_type["model_name"]: source_type for source_type in loads(response.content)}
53        self.assertEqual(types["githuboauthsource"]["icon_themed_urls"], GITHUB_ICON_THEMED_URLS)

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) -> None:
19    def setUp(self) -> None:
20        self.user = create_test_admin_user()
21        self.client.force_login(self.user)

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

def test_builtin_source_used_by(self):
23    def test_builtin_source_used_by(self):
24        """Test Providers's types endpoint"""
25        apps.get_app_config("authentik_core").source_inbuilt()
26        response = self.client.get(
27            reverse("authentik_api:source-used-by", kwargs={"slug": "authentik-built-in"}),
28        )
29        self.assertEqual(response.status_code, 200)

Test Providers's types endpoint

def test_user_settings_icon_themed_urls(self):
31    def test_user_settings_icon_themed_urls(self):
32        """Test themed icon URLs of user settings"""
33        github = OAuthSource.objects.create(
34            name=generate_id(), slug=generate_id(), provider_type="github"
35        )
36        custom = OAuthSource.objects.create(
37            name=generate_id(), slug=generate_id(), icon="https://example.com/logo.png"
38        )
39
40        response = self.client.get(reverse("authentik_api:source-user-settings"))
41
42        self.assertEqual(response.status_code, 200)
43        settings = {setting["object_uid"]: setting for setting in loads(response.content)}
44        self.assertEqual(settings[github.slug]["icon_themed_urls"], GITHUB_ICON_THEMED_URLS)
45        self.assertIsNone(settings[custom.slug]["icon_themed_urls"])

Test themed icon URLs of user settings

def test_types_icon_themed_urls(self):
47    def test_types_icon_themed_urls(self):
48        """Test themed icon URLs of source types"""
49        response = self.client.get(reverse("authentik_api:source-types"))
50
51        self.assertEqual(response.status_code, 200)
52        types = {source_type["model_name"]: source_type for source_type in loads(response.content)}
53        self.assertEqual(types["githuboauthsource"]["icon_themed_urls"], GITHUB_ICON_THEMED_URLS)

Test themed icon URLs of source types