authentik.enterprise.providers.scim.tests.test_api

SCIM OAuth tests

 1"""SCIM OAuth tests"""
 2
 3from unittest.mock import MagicMock, PropertyMock, patch
 4
 5from django.urls import reverse
 6from rest_framework.test import APITestCase
 7
 8from authentik.core.tests.utils import create_test_admin_user
 9from authentik.enterprise.license import LicenseKey
10from authentik.enterprise.models import License
11from authentik.enterprise.tests.test_license import expiry_valid
12from authentik.lib.generators import generate_id
13from authentik.sources.oauth.models import OAuthSource
14
15
16class TestSCIMOAuthAPI(APITestCase):
17    """SCIM User tests"""
18
19    def setUp(self):
20        self.source = OAuthSource.objects.create(
21            name=generate_id(),
22            slug=generate_id(),
23            access_token_url="http://localhost/token",  # nosec
24            consumer_key=generate_id(),
25            consumer_secret=generate_id(),
26            provider_type="openidconnect",
27        )
28
29    @patch(
30        "authentik.enterprise.license.LicenseKey.validate",
31        MagicMock(
32            return_value=LicenseKey(
33                aud="",
34                exp=expiry_valid,
35                name=generate_id(),
36                internal_users=100,
37                external_users=100,
38            )
39        ),
40    )
41    def test_api_create(self):
42        License.objects.create(key=generate_id())
43        self.client.force_login(create_test_admin_user())
44        res = self.client.post(
45            reverse("authentik_api:scimprovider-list"),
46            {
47                "name": generate_id(),
48                "url": "http://localhost",
49                "auth_mode": "oauth",
50                "auth_oauth": str(self.source.pk),
51            },
52        )
53        self.assertEqual(res.status_code, 201)
54
55    @patch(
56        "authentik.enterprise.models.LicenseUsageStatus.is_valid",
57        PropertyMock(return_value=False),
58    )
59    def test_api_create_no_license(self):
60        self.client.force_login(create_test_admin_user())
61        res = self.client.post(
62            reverse("authentik_api:scimprovider-list"),
63            {
64                "name": generate_id(),
65                "url": "http://localhost",
66                "auth_mode": "oauth",
67                "auth_oauth": str(self.source.pk),
68            },
69        )
70        self.assertEqual(res.status_code, 400)
71        self.assertJSONEqual(
72            res.content, {"auth_mode": ["Enterprise is required to use the OAuth mode."]}
73        )
class TestSCIMOAuthAPI(rest_framework.test.APITestCase):
17class TestSCIMOAuthAPI(APITestCase):
18    """SCIM User tests"""
19
20    def setUp(self):
21        self.source = OAuthSource.objects.create(
22            name=generate_id(),
23            slug=generate_id(),
24            access_token_url="http://localhost/token",  # nosec
25            consumer_key=generate_id(),
26            consumer_secret=generate_id(),
27            provider_type="openidconnect",
28        )
29
30    @patch(
31        "authentik.enterprise.license.LicenseKey.validate",
32        MagicMock(
33            return_value=LicenseKey(
34                aud="",
35                exp=expiry_valid,
36                name=generate_id(),
37                internal_users=100,
38                external_users=100,
39            )
40        ),
41    )
42    def test_api_create(self):
43        License.objects.create(key=generate_id())
44        self.client.force_login(create_test_admin_user())
45        res = self.client.post(
46            reverse("authentik_api:scimprovider-list"),
47            {
48                "name": generate_id(),
49                "url": "http://localhost",
50                "auth_mode": "oauth",
51                "auth_oauth": str(self.source.pk),
52            },
53        )
54        self.assertEqual(res.status_code, 201)
55
56    @patch(
57        "authentik.enterprise.models.LicenseUsageStatus.is_valid",
58        PropertyMock(return_value=False),
59    )
60    def test_api_create_no_license(self):
61        self.client.force_login(create_test_admin_user())
62        res = self.client.post(
63            reverse("authentik_api:scimprovider-list"),
64            {
65                "name": generate_id(),
66                "url": "http://localhost",
67                "auth_mode": "oauth",
68                "auth_oauth": str(self.source.pk),
69            },
70        )
71        self.assertEqual(res.status_code, 400)
72        self.assertJSONEqual(
73            res.content, {"auth_mode": ["Enterprise is required to use the OAuth mode."]}
74        )

SCIM User tests

def setUp(self):
20    def setUp(self):
21        self.source = OAuthSource.objects.create(
22            name=generate_id(),
23            slug=generate_id(),
24            access_token_url="http://localhost/token",  # nosec
25            consumer_key=generate_id(),
26            consumer_secret=generate_id(),
27            provider_type="openidconnect",
28        )

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

@patch('authentik.enterprise.license.LicenseKey.validate', MagicMock(return_value=LicenseKey(aud='', exp=expiry_valid, name=generate_id(), internal_users=100, external_users=100)))
def test_api_create(self):
30    @patch(
31        "authentik.enterprise.license.LicenseKey.validate",
32        MagicMock(
33            return_value=LicenseKey(
34                aud="",
35                exp=expiry_valid,
36                name=generate_id(),
37                internal_users=100,
38                external_users=100,
39            )
40        ),
41    )
42    def test_api_create(self):
43        License.objects.create(key=generate_id())
44        self.client.force_login(create_test_admin_user())
45        res = self.client.post(
46            reverse("authentik_api:scimprovider-list"),
47            {
48                "name": generate_id(),
49                "url": "http://localhost",
50                "auth_mode": "oauth",
51                "auth_oauth": str(self.source.pk),
52            },
53        )
54        self.assertEqual(res.status_code, 201)
@patch('authentik.enterprise.models.LicenseUsageStatus.is_valid', PropertyMock(return_value=False))
def test_api_create_no_license(self):
56    @patch(
57        "authentik.enterprise.models.LicenseUsageStatus.is_valid",
58        PropertyMock(return_value=False),
59    )
60    def test_api_create_no_license(self):
61        self.client.force_login(create_test_admin_user())
62        res = self.client.post(
63            reverse("authentik_api:scimprovider-list"),
64            {
65                "name": generate_id(),
66                "url": "http://localhost",
67                "auth_mode": "oauth",
68                "auth_oauth": str(self.source.pk),
69            },
70        )
71        self.assertEqual(res.status_code, 400)
72        self.assertJSONEqual(
73            res.content, {"auth_mode": ["Enterprise is required to use the OAuth mode."]}
74        )