authentik.core.tests.test_models

authentik core models tests

 1"""authentik core models tests"""
 2
 3from collections.abc import Callable
 4from datetime import timedelta
 5
 6from django.test import RequestFactory, TestCase
 7from django.utils.timezone import now
 8from freezegun import freeze_time
 9from guardian.shortcuts import get_anonymous_user
10
11from authentik.core.models import Provider, Source, Token
12from authentik.events.models import Event, EventAction
13from authentik.lib.generators import generate_id
14from authentik.lib.utils.reflection import all_subclasses
15
16
17class TestModels(TestCase):
18    """Test Models"""
19
20    def test_token_expire(self):
21        """Test token expiring"""
22        with freeze_time() as freeze:
23            token = Token.objects.create(expires=now(), user=get_anonymous_user())
24            freeze.tick(timedelta(seconds=1))
25            self.assertTrue(token.is_expired)
26
27    def test_token_expire_no_expire(self):
28        """Test token expiring with "expiring" set"""
29        with freeze_time() as freeze:
30            token = Token.objects.create(expires=now(), user=get_anonymous_user(), expiring=False)
31            freeze.tick(timedelta(seconds=1))
32            self.assertFalse(token.is_expired)
33
34    def test_filter_not_expired_warning(self):
35        """Test filter_not_expired's deprecation message"""
36        id = generate_id()
37        Token.objects.create(
38            expires=now() - timedelta(hours=1),
39            expiring=True,
40            user=get_anonymous_user(),
41            identifier=id,
42        )
43        self.assertFalse(Token.filter_not_expired(identifier=id).exists())
44        event = Event.objects.filter(action=EventAction.CONFIGURATION_WARNING).first()
45        self.assertIsNotNone(event)
46        self.assertEqual(
47            event.context["deprecation"], "authentik.core.models.Token.filter_not_expired"
48        )
49
50
51def source_tester_factory(test_model: type[Source]) -> Callable:
52    """Test source"""
53
54    factory = RequestFactory()
55    request = factory.get("/")
56
57    def tester(self: TestModels):
58        model_class = None
59        if test_model._meta.abstract:
60            return
61        else:
62            model_class = test_model()
63        model_class.slug = "test"
64        self.assertIsNotNone(model_class.component)
65        model_class.ui_login_button(request)
66        model_class.ui_user_settings()
67
68    return tester
69
70
71def provider_tester_factory(test_model: type[Provider]) -> Callable:
72    """Test provider"""
73
74    def tester(self: TestModels):
75        model_class = None
76        if test_model._meta.abstract:  # pragma: no cover
77            return
78        model_class = test_model()
79        self.assertIsNotNone(model_class.component)
80
81    return tester
82
83
84for model in all_subclasses(Source):
85    setattr(TestModels, f"test_source_{model.__name__}", source_tester_factory(model))
86for model in all_subclasses(Provider):
87    setattr(TestModels, f"test_provider_{model.__name__}", provider_tester_factory(model))
class TestModels(django.test.testcases.TestCase):
18class TestModels(TestCase):
19    """Test Models"""
20
21    def test_token_expire(self):
22        """Test token expiring"""
23        with freeze_time() as freeze:
24            token = Token.objects.create(expires=now(), user=get_anonymous_user())
25            freeze.tick(timedelta(seconds=1))
26            self.assertTrue(token.is_expired)
27
28    def test_token_expire_no_expire(self):
29        """Test token expiring with "expiring" set"""
30        with freeze_time() as freeze:
31            token = Token.objects.create(expires=now(), user=get_anonymous_user(), expiring=False)
32            freeze.tick(timedelta(seconds=1))
33            self.assertFalse(token.is_expired)
34
35    def test_filter_not_expired_warning(self):
36        """Test filter_not_expired's deprecation message"""
37        id = generate_id()
38        Token.objects.create(
39            expires=now() - timedelta(hours=1),
40            expiring=True,
41            user=get_anonymous_user(),
42            identifier=id,
43        )
44        self.assertFalse(Token.filter_not_expired(identifier=id).exists())
45        event = Event.objects.filter(action=EventAction.CONFIGURATION_WARNING).first()
46        self.assertIsNotNone(event)
47        self.assertEqual(
48            event.context["deprecation"], "authentik.core.models.Token.filter_not_expired"
49        )

Test Models

def test_token_expire(self):
21    def test_token_expire(self):
22        """Test token expiring"""
23        with freeze_time() as freeze:
24            token = Token.objects.create(expires=now(), user=get_anonymous_user())
25            freeze.tick(timedelta(seconds=1))
26            self.assertTrue(token.is_expired)

Test token expiring

def test_token_expire_no_expire(self):
28    def test_token_expire_no_expire(self):
29        """Test token expiring with "expiring" set"""
30        with freeze_time() as freeze:
31            token = Token.objects.create(expires=now(), user=get_anonymous_user(), expiring=False)
32            freeze.tick(timedelta(seconds=1))
33            self.assertFalse(token.is_expired)

Test token expiring with "expiring" set

def test_filter_not_expired_warning(self):
35    def test_filter_not_expired_warning(self):
36        """Test filter_not_expired's deprecation message"""
37        id = generate_id()
38        Token.objects.create(
39            expires=now() - timedelta(hours=1),
40            expiring=True,
41            user=get_anonymous_user(),
42            identifier=id,
43        )
44        self.assertFalse(Token.filter_not_expired(identifier=id).exists())
45        event = Event.objects.filter(action=EventAction.CONFIGURATION_WARNING).first()
46        self.assertIsNotNone(event)
47        self.assertEqual(
48            event.context["deprecation"], "authentik.core.models.Token.filter_not_expired"
49        )

Test filter_not_expired's deprecation message

def test_source_AppleOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_AzureADOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_DiscordOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_EntraIDOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_FacebookOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_GitHubOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_GitLabOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_GoogleOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_IncomingSyncSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_KerberosSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_LDAPSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_MailcowOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_OAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_OktaOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_OpenIDConnectOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_PatreonOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_PlexSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_RedditOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_SAMLSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_SCIMSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_SlackOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_TelegramSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_TwitchOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_TwitterOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_source_WeChatOAuthSource(self: TestModels):
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()

The type of the None singleton.

def test_provider_BackchannelProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_GoogleWorkspaceProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_LDAPProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_MicrosoftEntraProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_OAuth2Provider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_ProxyProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_RACProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_RadiusProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_SAMLProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_SAMLProviderImportModel(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_SCIMProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_SSFProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def test_provider_WSFederationProvider(self: TestModels):
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)

The type of the None singleton.

def source_tester_factory(test_model: type[authentik.core.models.Source]) -> Callable:
52def source_tester_factory(test_model: type[Source]) -> Callable:
53    """Test source"""
54
55    factory = RequestFactory()
56    request = factory.get("/")
57
58    def tester(self: TestModels):
59        model_class = None
60        if test_model._meta.abstract:
61            return
62        else:
63            model_class = test_model()
64        model_class.slug = "test"
65        self.assertIsNotNone(model_class.component)
66        model_class.ui_login_button(request)
67        model_class.ui_user_settings()
68
69    return tester

Test source

def provider_tester_factory(test_model: type[authentik.core.models.Provider]) -> Callable:
72def provider_tester_factory(test_model: type[Provider]) -> Callable:
73    """Test provider"""
74
75    def tester(self: TestModels):
76        model_class = None
77        if test_model._meta.abstract:  # pragma: no cover
78            return
79        model_class = test_model()
80        self.assertIsNotNone(model_class.component)
81
82    return tester

Test provider