authentik.sources.oauth.tests.test_api

  1from django.urls import reverse
  2from requests_mock import Mocker
  3from rest_framework.test import APITestCase
  4
  5from authentik.core.tests.utils import create_test_admin_user
  6from authentik.lib.generators import generate_id
  7from authentik.sources.oauth.models import OAuthSource
  8
  9
 10class TestOAuthSourceAPI(APITestCase):
 11    def setUp(self):
 12        self.source = OAuthSource.objects.create(
 13            name=generate_id(),
 14            slug=generate_id(),
 15            provider_type="openidconnect",
 16            authorization_url="",
 17            profile_url="",
 18            consumer_key=generate_id(),
 19            consumer_secret=generate_id(),
 20        )
 21        self.user = create_test_admin_user()
 22
 23    def test_patch_no_type(self):
 24        self.client.force_login(self.user)
 25        res = self.client.patch(
 26            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
 27            {
 28                "authorization_url": f"https://{generate_id()}",
 29                "profile_url": f"https://{generate_id()}",
 30                "access_token_url": f"https://{generate_id()}",
 31            },
 32        )
 33        self.assertEqual(res.status_code, 200)
 34
 35    def test_patch_long_url(self):
 36        """URL fields are TextField, so URLs longer than 255 chars (e.g. an
 37        authorization URL carrying many static query parameters) must save."""
 38        self.client.force_login(self.user)
 39        long_url = "https://cilogon.org/authorize?idphint=" + ",".join(
 40            f"https://idp{i}.institution.example.org/idp/shibboleth" for i in range(10)
 41        )
 42        self.assertGreater(len(long_url), 255)
 43        res = self.client.patch(
 44            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
 45            {
 46                "authorization_url": long_url,
 47                "profile_url": f"https://{generate_id()}",
 48                "access_token_url": f"https://{generate_id()}",
 49            },
 50            content_type="application/json",
 51        )
 52        self.assertEqual(res.status_code, 200)
 53        self.source.refresh_from_db()
 54        self.assertEqual(self.source.authorization_url, long_url)
 55
 56    @Mocker()
 57    def test_disable_source_invalid_well_known_url(self, mock: Mocker):
 58        """Saving a disabled source should not make a request to the well-known url"""
 59        self.client.force_login(self.user)
 60
 61        well_known_url = f"https://{generate_id()}/.well-known/openid-configuration"
 62        mock.get(well_known_url, json={})
 63
 64        res = self.client.patch(
 65            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
 66            {
 67                "enabled": False,
 68                "oidc_well_known_url": well_known_url,
 69                "authorization_url": f"https://{generate_id()}",
 70                "profile_url": f"https://{generate_id()}",
 71                "access_token_url": f"https://{generate_id()}",
 72            },
 73            content_type="application/json",
 74        )
 75        self.assertEqual(res.status_code, 200)
 76        self.assertEqual(mock.call_count, 0)
 77
 78    def test_create_public_client(self):
 79        """Public clients (no secret) must use PKCE"""
 80        self.client.force_login(self.user)
 81        data = {
 82            "name": generate_id(),
 83            "slug": generate_id(),
 84            "provider_type": "openidconnect",
 85            "consumer_key": generate_id(),
 86            "authorization_url": f"https://{generate_id()}",
 87            "profile_url": f"https://{generate_id()}",
 88            "access_token_url": f"https://{generate_id()}",
 89        }
 90        res = self.client.post(
 91            reverse("authentik_api:oauthsource-list"), data, content_type="application/json"
 92        )
 93        self.assertEqual(res.status_code, 400)
 94        self.assertIn("pkce", res.json())
 95        res = self.client.post(
 96            reverse("authentik_api:oauthsource-list"),
 97            {**data, "pkce": "S256"},
 98            content_type="application/json",
 99        )
100        self.assertEqual(res.status_code, 201)
101
102    def test_create_secret_required(self):
103        """Source types that require a secret reject a missing one"""
104        self.client.force_login(self.user)
105        for provider_type in ["discord", "apple"]:
106            with self.subTest(provider_type):
107                res = self.client.post(
108                    reverse("authentik_api:oauthsource-list"),
109                    {
110                        "name": generate_id(),
111                        "slug": generate_id(),
112                        "provider_type": provider_type,
113                        "consumer_key": generate_id(),
114                        "pkce": "S256",
115                    },
116                    content_type="application/json",
117                )
118                self.assertEqual(res.status_code, 400)
119                self.assertIn("consumer_secret", res.json())
class TestOAuthSourceAPI(rest_framework.test.APITestCase):
 11class TestOAuthSourceAPI(APITestCase):
 12    def setUp(self):
 13        self.source = OAuthSource.objects.create(
 14            name=generate_id(),
 15            slug=generate_id(),
 16            provider_type="openidconnect",
 17            authorization_url="",
 18            profile_url="",
 19            consumer_key=generate_id(),
 20            consumer_secret=generate_id(),
 21        )
 22        self.user = create_test_admin_user()
 23
 24    def test_patch_no_type(self):
 25        self.client.force_login(self.user)
 26        res = self.client.patch(
 27            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
 28            {
 29                "authorization_url": f"https://{generate_id()}",
 30                "profile_url": f"https://{generate_id()}",
 31                "access_token_url": f"https://{generate_id()}",
 32            },
 33        )
 34        self.assertEqual(res.status_code, 200)
 35
 36    def test_patch_long_url(self):
 37        """URL fields are TextField, so URLs longer than 255 chars (e.g. an
 38        authorization URL carrying many static query parameters) must save."""
 39        self.client.force_login(self.user)
 40        long_url = "https://cilogon.org/authorize?idphint=" + ",".join(
 41            f"https://idp{i}.institution.example.org/idp/shibboleth" for i in range(10)
 42        )
 43        self.assertGreater(len(long_url), 255)
 44        res = self.client.patch(
 45            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
 46            {
 47                "authorization_url": long_url,
 48                "profile_url": f"https://{generate_id()}",
 49                "access_token_url": f"https://{generate_id()}",
 50            },
 51            content_type="application/json",
 52        )
 53        self.assertEqual(res.status_code, 200)
 54        self.source.refresh_from_db()
 55        self.assertEqual(self.source.authorization_url, long_url)
 56
 57    @Mocker()
 58    def test_disable_source_invalid_well_known_url(self, mock: Mocker):
 59        """Saving a disabled source should not make a request to the well-known url"""
 60        self.client.force_login(self.user)
 61
 62        well_known_url = f"https://{generate_id()}/.well-known/openid-configuration"
 63        mock.get(well_known_url, json={})
 64
 65        res = self.client.patch(
 66            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
 67            {
 68                "enabled": False,
 69                "oidc_well_known_url": well_known_url,
 70                "authorization_url": f"https://{generate_id()}",
 71                "profile_url": f"https://{generate_id()}",
 72                "access_token_url": f"https://{generate_id()}",
 73            },
 74            content_type="application/json",
 75        )
 76        self.assertEqual(res.status_code, 200)
 77        self.assertEqual(mock.call_count, 0)
 78
 79    def test_create_public_client(self):
 80        """Public clients (no secret) must use PKCE"""
 81        self.client.force_login(self.user)
 82        data = {
 83            "name": generate_id(),
 84            "slug": generate_id(),
 85            "provider_type": "openidconnect",
 86            "consumer_key": generate_id(),
 87            "authorization_url": f"https://{generate_id()}",
 88            "profile_url": f"https://{generate_id()}",
 89            "access_token_url": f"https://{generate_id()}",
 90        }
 91        res = self.client.post(
 92            reverse("authentik_api:oauthsource-list"), data, content_type="application/json"
 93        )
 94        self.assertEqual(res.status_code, 400)
 95        self.assertIn("pkce", res.json())
 96        res = self.client.post(
 97            reverse("authentik_api:oauthsource-list"),
 98            {**data, "pkce": "S256"},
 99            content_type="application/json",
100        )
101        self.assertEqual(res.status_code, 201)
102
103    def test_create_secret_required(self):
104        """Source types that require a secret reject a missing one"""
105        self.client.force_login(self.user)
106        for provider_type in ["discord", "apple"]:
107            with self.subTest(provider_type):
108                res = self.client.post(
109                    reverse("authentik_api:oauthsource-list"),
110                    {
111                        "name": generate_id(),
112                        "slug": generate_id(),
113                        "provider_type": provider_type,
114                        "consumer_key": generate_id(),
115                        "pkce": "S256",
116                    },
117                    content_type="application/json",
118                )
119                self.assertEqual(res.status_code, 400)
120                self.assertIn("consumer_secret", res.json())

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):
12    def setUp(self):
13        self.source = OAuthSource.objects.create(
14            name=generate_id(),
15            slug=generate_id(),
16            provider_type="openidconnect",
17            authorization_url="",
18            profile_url="",
19            consumer_key=generate_id(),
20            consumer_secret=generate_id(),
21        )
22        self.user = create_test_admin_user()

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

def test_patch_no_type(self):
24    def test_patch_no_type(self):
25        self.client.force_login(self.user)
26        res = self.client.patch(
27            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
28            {
29                "authorization_url": f"https://{generate_id()}",
30                "profile_url": f"https://{generate_id()}",
31                "access_token_url": f"https://{generate_id()}",
32            },
33        )
34        self.assertEqual(res.status_code, 200)
def test_patch_long_url(self):
36    def test_patch_long_url(self):
37        """URL fields are TextField, so URLs longer than 255 chars (e.g. an
38        authorization URL carrying many static query parameters) must save."""
39        self.client.force_login(self.user)
40        long_url = "https://cilogon.org/authorize?idphint=" + ",".join(
41            f"https://idp{i}.institution.example.org/idp/shibboleth" for i in range(10)
42        )
43        self.assertGreater(len(long_url), 255)
44        res = self.client.patch(
45            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
46            {
47                "authorization_url": long_url,
48                "profile_url": f"https://{generate_id()}",
49                "access_token_url": f"https://{generate_id()}",
50            },
51            content_type="application/json",
52        )
53        self.assertEqual(res.status_code, 200)
54        self.source.refresh_from_db()
55        self.assertEqual(self.source.authorization_url, long_url)

URL fields are TextField, so URLs longer than 255 chars (e.g. an authorization URL carrying many static query parameters) must save.

@Mocker()
def test_disable_source_invalid_well_known_url(self, mock: requests_mock.mocker.Mocker):
57    @Mocker()
58    def test_disable_source_invalid_well_known_url(self, mock: Mocker):
59        """Saving a disabled source should not make a request to the well-known url"""
60        self.client.force_login(self.user)
61
62        well_known_url = f"https://{generate_id()}/.well-known/openid-configuration"
63        mock.get(well_known_url, json={})
64
65        res = self.client.patch(
66            reverse("authentik_api:oauthsource-detail", kwargs={"slug": self.source.slug}),
67            {
68                "enabled": False,
69                "oidc_well_known_url": well_known_url,
70                "authorization_url": f"https://{generate_id()}",
71                "profile_url": f"https://{generate_id()}",
72                "access_token_url": f"https://{generate_id()}",
73            },
74            content_type="application/json",
75        )
76        self.assertEqual(res.status_code, 200)
77        self.assertEqual(mock.call_count, 0)

Saving a disabled source should not make a request to the well-known url

def test_create_public_client(self):
 79    def test_create_public_client(self):
 80        """Public clients (no secret) must use PKCE"""
 81        self.client.force_login(self.user)
 82        data = {
 83            "name": generate_id(),
 84            "slug": generate_id(),
 85            "provider_type": "openidconnect",
 86            "consumer_key": generate_id(),
 87            "authorization_url": f"https://{generate_id()}",
 88            "profile_url": f"https://{generate_id()}",
 89            "access_token_url": f"https://{generate_id()}",
 90        }
 91        res = self.client.post(
 92            reverse("authentik_api:oauthsource-list"), data, content_type="application/json"
 93        )
 94        self.assertEqual(res.status_code, 400)
 95        self.assertIn("pkce", res.json())
 96        res = self.client.post(
 97            reverse("authentik_api:oauthsource-list"),
 98            {**data, "pkce": "S256"},
 99            content_type="application/json",
100        )
101        self.assertEqual(res.status_code, 201)

Public clients (no secret) must use PKCE

def test_create_secret_required(self):
103    def test_create_secret_required(self):
104        """Source types that require a secret reject a missing one"""
105        self.client.force_login(self.user)
106        for provider_type in ["discord", "apple"]:
107            with self.subTest(provider_type):
108                res = self.client.post(
109                    reverse("authentik_api:oauthsource-list"),
110                    {
111                        "name": generate_id(),
112                        "slug": generate_id(),
113                        "provider_type": provider_type,
114                        "consumer_key": generate_id(),
115                        "pkce": "S256",
116                    },
117                    content_type="application/json",
118                )
119                self.assertEqual(res.status_code, 400)
120                self.assertIn("consumer_secret", res.json())

Source types that require a secret reject a missing one