authentik.core.tests.test_interface_views

Test interface view redirect behavior by user type

  1"""Test interface view redirect behavior by user type"""
  2
  3from django.test import TestCase
  4from django.urls import resolve, reverse
  5
  6from authentik.brands.api import Themes
  7from authentik.brands.models import Brand
  8from authentik.core.apps import Setup
  9from authentik.core.models import Application, UserTypes
 10from authentik.core.tests.utils import create_test_brand, create_test_user
 11from authentik.lib.config import CONFIG
 12
 13
 14class TestInterfaceRedirects(TestCase):
 15    """Test RootRedirectView and BrandDefaultRedirectView redirect logic by user type"""
 16
 17    def setUp(self):
 18        Setup.set(True)
 19        self.app = Application.objects.create(name="test-app", slug="test-app")
 20        self.brand: Brand = create_test_brand(default_application=self.app)
 21
 22    def _assert_redirects_to_app(self, url_name: str, user_type: UserTypes):
 23        user = create_test_user(type=user_type)
 24        self.client.force_login(user)
 25        response = self.client.get(reverse(f"authentik_core:{url_name}"))
 26        self.assertRedirects(
 27            response,
 28            reverse(
 29                "authentik_core:application-launch", kwargs={"application_slug": self.app.slug}
 30            ),
 31            fetch_redirect_response=False,
 32        )
 33
 34    def _assert_no_redirect(self, url_name: str, user_type: UserTypes):
 35        """Internal users should not be redirected away."""
 36        user = create_test_user(type=user_type)
 37        self.client.force_login(user)
 38        response = self.client.get(reverse(f"authentik_core:{url_name}"))
 39        # Internal users get a 200 (rendered template) or redirect to if-user, not to the app
 40        app_url = reverse(
 41            "authentik_core:application-launch", kwargs={"application_slug": self.app.slug}
 42        )
 43        self.assertNotEqual(response.get("Location"), app_url)
 44
 45    # --- RootRedirectView ---
 46
 47    def test_root_redirect_external_user(self):
 48        """External users are redirected to the default app from root"""
 49        self._assert_redirects_to_app("root-redirect", UserTypes.EXTERNAL)
 50
 51    def test_root_redirect_service_account(self):
 52        """Service accounts are redirected to the default app from root"""
 53        self._assert_redirects_to_app("root-redirect", UserTypes.SERVICE_ACCOUNT)
 54
 55    def test_root_redirect_internal_service_account(self):
 56        """Internal service accounts are redirected to the default app from root"""
 57        self._assert_redirects_to_app("root-redirect", UserTypes.INTERNAL_SERVICE_ACCOUNT)
 58
 59    def test_root_redirect_internal_user(self):
 60        """Internal users are NOT redirected to the app from root"""
 61        self._assert_no_redirect("root-redirect", UserTypes.INTERNAL)
 62
 63    # --- BrandDefaultRedirectView (if/user/) ---
 64
 65    def test_if_user_external_user(self):
 66        """External users are redirected to the default app from if/user/"""
 67        self._assert_redirects_to_app("if-user", UserTypes.EXTERNAL)
 68
 69    def test_if_user_service_account(self):
 70        """Service accounts are redirected to the default app from if/user/"""
 71        self._assert_redirects_to_app("if-user", UserTypes.SERVICE_ACCOUNT)
 72
 73    def test_if_user_internal_service_account(self):
 74        """Internal service accounts are redirected to the default app from if/user/"""
 75        self._assert_redirects_to_app("if-user", UserTypes.INTERNAL_SERVICE_ACCOUNT)
 76
 77    def test_if_user_internal_user(self):
 78        """Internal users are NOT redirected to the app from if/user/"""
 79        self._assert_no_redirect("if-user", UserTypes.INTERNAL)
 80
 81    # --- BrandDefaultRedirectView (if/admin/) ---
 82
 83    def test_if_admin_service_account(self):
 84        """Service accounts are redirected to the default app from if/admin/"""
 85        self._assert_redirects_to_app("if-admin", UserTypes.SERVICE_ACCOUNT)
 86
 87    def test_if_admin_internal_service_account(self):
 88        """Internal service accounts are redirected to the default app from if/admin/"""
 89        self._assert_redirects_to_app("if-admin", UserTypes.INTERNAL_SERVICE_ACCOUNT)
 90
 91    def test_if_admin_internal_user(self):
 92        """Internal users are NOT redirected to the app from if/admin/"""
 93        self._assert_no_redirect("if-admin", UserTypes.INTERNAL)
 94
 95    # --- No default app set ---
 96
 97    def test_service_account_no_default_app_access_denied(self):
 98        """Service accounts get access denied when no default app is configured"""
 99        self.brand.default_application = None
100        self.brand.save()
101        user = create_test_user(type=UserTypes.SERVICE_ACCOUNT)
102        self.client.force_login(user)
103        response = self.client.get(reverse("authentik_core:if-user"))
104        self.assertEqual(response.status_code, 200)
105        self.assertIn(b"Interface can only be accessed by internal users", response.content)
106
107
108class TestInterfaceCatchAll(TestCase):
109    """Path-based routing: any subpath under if/admin/ and if/user/ renders the SPA shell."""
110
111    def setUp(self):
112        Setup.set(True)
113        self.user = create_test_user(type=UserTypes.INTERNAL)
114        create_test_brand()
115        self.client.force_login(self.user)
116
117    def test_admin_exact_prefix_renders_shell(self):
118        """The exact /if/admin/ prefix still renders the admin interface."""
119        response = self.client.get(reverse("authentik_core:if-admin"))
120        self.assertEqual(response.status_code, 200)
121        self.assertTemplateUsed(response, "if/admin.html")
122        self.assertIn(b"ak-interface-admin", response.content)
123
124    def test_user_exact_prefix_renders_shell(self):
125        """The exact /if/user/ prefix still renders the user interface."""
126        response = self.client.get(reverse("authentik_core:if-user"))
127        self.assertEqual(response.status_code, 200)
128        self.assertTemplateUsed(response, "if/user.html")
129        self.assertIn(b"ak-interface-user", response.content)
130
131    def test_admin_deep_subpath_renders_shell(self):
132        """An arbitrary nested admin path returns the admin shell, not a 404."""
133        response = self.client.get("/if/admin/identity/users/42")
134        self.assertEqual(response.status_code, 200)
135        self.assertTemplateUsed(response, "if/admin.html")
136        self.assertIn(b"ak-interface-admin", response.content)
137
138    def test_user_deep_subpath_renders_shell(self):
139        """An arbitrary nested user path returns the user shell, not a 404."""
140        response = self.client.get("/if/user/settings/sources")
141        self.assertEqual(response.status_code, 200)
142        self.assertTemplateUsed(response, "if/user.html")
143        self.assertIn(b"ak-interface-user", response.content)
144
145    def test_admin_subpath_is_reversible(self):
146        """The admin catch-all route reverses with a path kwarg."""
147        self.assertEqual(
148            reverse("authentik_core:if-admin-path", kwargs={"path": "flow/inspector"}),
149            "/if/admin/flow/inspector",
150        )
151
152    def test_user_subpath_is_reversible(self):
153        """The user catch-all route reverses with a path kwarg."""
154        self.assertEqual(
155            reverse("authentik_core:if-user-path", kwargs={"path": "settings"}),
156            "/if/user/settings",
157        )
158
159    def test_deep_subpath_carries_interface_context(self):
160        """The deep-path shell carries the same injected config as the root."""
161        response = self.client.get("/if/admin/core/applications")
162        self.assertEqual(response.status_code, 200)
163        # base/header_js.html injects the interface context as data, not script.
164        self.assertIn(b'id="ak-config"', response.content)
165        self.assertIn(b'<meta name="ak-base-url-rel" content="/">', response.content)
166
167    def test_exact_admin_prefix_not_shadowed_by_catchall(self):
168        """Ordering: the exact route wins over the catch-all for /if/admin/."""
169        self.assertEqual(resolve("/if/admin/").url_name, "if-admin")
170
171    def test_exact_user_prefix_not_shadowed_by_catchall(self):
172        """Ordering: the exact route wins over the catch-all for /if/user/."""
173        self.assertEqual(resolve("/if/user/").url_name, "if-user")
174
175    def test_flow_route_not_shadowed(self):
176        """The catch-alls must not swallow flow URLs."""
177        self.assertEqual(resolve("/if/flow/some-slug/").url_name, "if-flow")
178
179    def test_ws_client_route_not_shadowed(self):
180        """The catch-alls must not swallow the websocket fallback URL."""
181        self.assertNotIn(resolve("/ws/client/").url_name, ("if-admin-path", "if-user-path"))
182
183    def test_api_route_not_shadowed(self):
184        """The catch-alls live under core's urlconf and must not affect the API."""
185        self.assertNotIn(resolve("/api/v3/core/users/").url_name, ("if-admin-path", "if-user-path"))
186
187    def test_web_path_reflected_in_shell_context(self):
188        """When web.path is non-root, the shell advertises it as ak-base-url-rel.
189
190        Routing under web.path is resolved at import time in the root urlconf, so
191        only the request-time context value is exercised here; deployment-prefix
192        routing is covered by the Playwright web.path smoke test in a later PR.
193        """
194        with CONFIG.patch("web.path", "/auth/"):
195            response = self.client.get("/if/user/settings")
196        self.assertEqual(response.status_code, 200)
197        self.assertIn(b'<meta name="ak-base-url-rel" content="/auth/">', response.content)
198
199
200class TestInterfaceFavicon(TestCase):
201    """The favicon links follow the brand's configured theme, and fall back to the
202    system color scheme only when the brand expresses none."""
203
204    def setUp(self):
205        Setup.set(True)
206        self.user = create_test_user(type=UserTypes.INTERNAL)
207        self.brand = create_test_brand(
208            branding_favicon="https://example.com/icon-%(theme)s.png",
209        )
210        self.client.force_login(self.user)
211
212    def _render(self, ui_theme: str | None = None) -> str:
213        if ui_theme:
214            self.brand.attributes = {"settings": {"theme": {"base": ui_theme}}}
215            self.brand.save()
216        response = self.client.get(reverse("authentik_core:if-user"))
217        self.assertEqual(response.status_code, 200)
218        return response.content.decode()
219
220    def test_automatic_theme_uses_media_queries(self):
221        """Without a brand theme, the browser picks the favicon by system color scheme."""
222        content = self._render()
223        self.assertIn(
224            '<link rel="icon" href="https://example.com/icon-light.png" '
225            'media="(prefers-color-scheme: light)">',
226            content,
227        )
228        self.assertIn(
229            '<link rel="icon" href="https://example.com/icon-dark.png" '
230            'media="(prefers-color-scheme: dark)">',
231            content,
232        )
233
234    def test_dark_theme_pins_favicon(self):
235        """A brand pinned to dark gets the dark favicon regardless of the system scheme."""
236        content = self._render(Themes.DARK)
237        self.assertIn('<link rel="icon" href="https://example.com/icon-dark.png">', content)
238        self.assertNotIn('href="https://example.com/icon-light.png"', content)
239
240    def test_light_theme_pins_favicon(self):
241        """A brand pinned to light gets the light favicon regardless of the system scheme."""
242        content = self._render(Themes.LIGHT)
243        self.assertIn('<link rel="icon" href="https://example.com/icon-light.png">', content)
244        self.assertNotIn('href="https://example.com/icon-dark.png"', content)
245
246    def test_untemplated_favicon_unchanged(self):
247        """A favicon without a %(theme)s variable renders a single plain link."""
248        self.brand.branding_favicon = "https://example.com/icon.png"
249        self.brand.save()
250        content = self._render()
251        self.assertIn('<link rel="icon" href="https://example.com/icon.png">', content)
252        self.assertIn('<link rel="shortcut icon" href="https://example.com/icon.png">', content)
class TestInterfaceRedirects(django.test.testcases.TestCase):
 15class TestInterfaceRedirects(TestCase):
 16    """Test RootRedirectView and BrandDefaultRedirectView redirect logic by user type"""
 17
 18    def setUp(self):
 19        Setup.set(True)
 20        self.app = Application.objects.create(name="test-app", slug="test-app")
 21        self.brand: Brand = create_test_brand(default_application=self.app)
 22
 23    def _assert_redirects_to_app(self, url_name: str, user_type: UserTypes):
 24        user = create_test_user(type=user_type)
 25        self.client.force_login(user)
 26        response = self.client.get(reverse(f"authentik_core:{url_name}"))
 27        self.assertRedirects(
 28            response,
 29            reverse(
 30                "authentik_core:application-launch", kwargs={"application_slug": self.app.slug}
 31            ),
 32            fetch_redirect_response=False,
 33        )
 34
 35    def _assert_no_redirect(self, url_name: str, user_type: UserTypes):
 36        """Internal users should not be redirected away."""
 37        user = create_test_user(type=user_type)
 38        self.client.force_login(user)
 39        response = self.client.get(reverse(f"authentik_core:{url_name}"))
 40        # Internal users get a 200 (rendered template) or redirect to if-user, not to the app
 41        app_url = reverse(
 42            "authentik_core:application-launch", kwargs={"application_slug": self.app.slug}
 43        )
 44        self.assertNotEqual(response.get("Location"), app_url)
 45
 46    # --- RootRedirectView ---
 47
 48    def test_root_redirect_external_user(self):
 49        """External users are redirected to the default app from root"""
 50        self._assert_redirects_to_app("root-redirect", UserTypes.EXTERNAL)
 51
 52    def test_root_redirect_service_account(self):
 53        """Service accounts are redirected to the default app from root"""
 54        self._assert_redirects_to_app("root-redirect", UserTypes.SERVICE_ACCOUNT)
 55
 56    def test_root_redirect_internal_service_account(self):
 57        """Internal service accounts are redirected to the default app from root"""
 58        self._assert_redirects_to_app("root-redirect", UserTypes.INTERNAL_SERVICE_ACCOUNT)
 59
 60    def test_root_redirect_internal_user(self):
 61        """Internal users are NOT redirected to the app from root"""
 62        self._assert_no_redirect("root-redirect", UserTypes.INTERNAL)
 63
 64    # --- BrandDefaultRedirectView (if/user/) ---
 65
 66    def test_if_user_external_user(self):
 67        """External users are redirected to the default app from if/user/"""
 68        self._assert_redirects_to_app("if-user", UserTypes.EXTERNAL)
 69
 70    def test_if_user_service_account(self):
 71        """Service accounts are redirected to the default app from if/user/"""
 72        self._assert_redirects_to_app("if-user", UserTypes.SERVICE_ACCOUNT)
 73
 74    def test_if_user_internal_service_account(self):
 75        """Internal service accounts are redirected to the default app from if/user/"""
 76        self._assert_redirects_to_app("if-user", UserTypes.INTERNAL_SERVICE_ACCOUNT)
 77
 78    def test_if_user_internal_user(self):
 79        """Internal users are NOT redirected to the app from if/user/"""
 80        self._assert_no_redirect("if-user", UserTypes.INTERNAL)
 81
 82    # --- BrandDefaultRedirectView (if/admin/) ---
 83
 84    def test_if_admin_service_account(self):
 85        """Service accounts are redirected to the default app from if/admin/"""
 86        self._assert_redirects_to_app("if-admin", UserTypes.SERVICE_ACCOUNT)
 87
 88    def test_if_admin_internal_service_account(self):
 89        """Internal service accounts are redirected to the default app from if/admin/"""
 90        self._assert_redirects_to_app("if-admin", UserTypes.INTERNAL_SERVICE_ACCOUNT)
 91
 92    def test_if_admin_internal_user(self):
 93        """Internal users are NOT redirected to the app from if/admin/"""
 94        self._assert_no_redirect("if-admin", UserTypes.INTERNAL)
 95
 96    # --- No default app set ---
 97
 98    def test_service_account_no_default_app_access_denied(self):
 99        """Service accounts get access denied when no default app is configured"""
100        self.brand.default_application = None
101        self.brand.save()
102        user = create_test_user(type=UserTypes.SERVICE_ACCOUNT)
103        self.client.force_login(user)
104        response = self.client.get(reverse("authentik_core:if-user"))
105        self.assertEqual(response.status_code, 200)
106        self.assertIn(b"Interface can only be accessed by internal users", response.content)

Test RootRedirectView and BrandDefaultRedirectView redirect logic by user type

def setUp(self):
18    def setUp(self):
19        Setup.set(True)
20        self.app = Application.objects.create(name="test-app", slug="test-app")
21        self.brand: Brand = create_test_brand(default_application=self.app)

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

def test_root_redirect_external_user(self):
48    def test_root_redirect_external_user(self):
49        """External users are redirected to the default app from root"""
50        self._assert_redirects_to_app("root-redirect", UserTypes.EXTERNAL)

External users are redirected to the default app from root

def test_root_redirect_service_account(self):
52    def test_root_redirect_service_account(self):
53        """Service accounts are redirected to the default app from root"""
54        self._assert_redirects_to_app("root-redirect", UserTypes.SERVICE_ACCOUNT)

Service accounts are redirected to the default app from root

def test_root_redirect_internal_service_account(self):
56    def test_root_redirect_internal_service_account(self):
57        """Internal service accounts are redirected to the default app from root"""
58        self._assert_redirects_to_app("root-redirect", UserTypes.INTERNAL_SERVICE_ACCOUNT)

Internal service accounts are redirected to the default app from root

def test_root_redirect_internal_user(self):
60    def test_root_redirect_internal_user(self):
61        """Internal users are NOT redirected to the app from root"""
62        self._assert_no_redirect("root-redirect", UserTypes.INTERNAL)

Internal users are NOT redirected to the app from root

def test_if_user_external_user(self):
66    def test_if_user_external_user(self):
67        """External users are redirected to the default app from if/user/"""
68        self._assert_redirects_to_app("if-user", UserTypes.EXTERNAL)

External users are redirected to the default app from if/user/

def test_if_user_service_account(self):
70    def test_if_user_service_account(self):
71        """Service accounts are redirected to the default app from if/user/"""
72        self._assert_redirects_to_app("if-user", UserTypes.SERVICE_ACCOUNT)

Service accounts are redirected to the default app from if/user/

def test_if_user_internal_service_account(self):
74    def test_if_user_internal_service_account(self):
75        """Internal service accounts are redirected to the default app from if/user/"""
76        self._assert_redirects_to_app("if-user", UserTypes.INTERNAL_SERVICE_ACCOUNT)

Internal service accounts are redirected to the default app from if/user/

def test_if_user_internal_user(self):
78    def test_if_user_internal_user(self):
79        """Internal users are NOT redirected to the app from if/user/"""
80        self._assert_no_redirect("if-user", UserTypes.INTERNAL)

Internal users are NOT redirected to the app from if/user/

def test_if_admin_service_account(self):
84    def test_if_admin_service_account(self):
85        """Service accounts are redirected to the default app from if/admin/"""
86        self._assert_redirects_to_app("if-admin", UserTypes.SERVICE_ACCOUNT)

Service accounts are redirected to the default app from if/admin/

def test_if_admin_internal_service_account(self):
88    def test_if_admin_internal_service_account(self):
89        """Internal service accounts are redirected to the default app from if/admin/"""
90        self._assert_redirects_to_app("if-admin", UserTypes.INTERNAL_SERVICE_ACCOUNT)

Internal service accounts are redirected to the default app from if/admin/

def test_if_admin_internal_user(self):
92    def test_if_admin_internal_user(self):
93        """Internal users are NOT redirected to the app from if/admin/"""
94        self._assert_no_redirect("if-admin", UserTypes.INTERNAL)

Internal users are NOT redirected to the app from if/admin/

def test_service_account_no_default_app_access_denied(self):
 98    def test_service_account_no_default_app_access_denied(self):
 99        """Service accounts get access denied when no default app is configured"""
100        self.brand.default_application = None
101        self.brand.save()
102        user = create_test_user(type=UserTypes.SERVICE_ACCOUNT)
103        self.client.force_login(user)
104        response = self.client.get(reverse("authentik_core:if-user"))
105        self.assertEqual(response.status_code, 200)
106        self.assertIn(b"Interface can only be accessed by internal users", response.content)

Service accounts get access denied when no default app is configured

class TestInterfaceCatchAll(django.test.testcases.TestCase):
109class TestInterfaceCatchAll(TestCase):
110    """Path-based routing: any subpath under if/admin/ and if/user/ renders the SPA shell."""
111
112    def setUp(self):
113        Setup.set(True)
114        self.user = create_test_user(type=UserTypes.INTERNAL)
115        create_test_brand()
116        self.client.force_login(self.user)
117
118    def test_admin_exact_prefix_renders_shell(self):
119        """The exact /if/admin/ prefix still renders the admin interface."""
120        response = self.client.get(reverse("authentik_core:if-admin"))
121        self.assertEqual(response.status_code, 200)
122        self.assertTemplateUsed(response, "if/admin.html")
123        self.assertIn(b"ak-interface-admin", response.content)
124
125    def test_user_exact_prefix_renders_shell(self):
126        """The exact /if/user/ prefix still renders the user interface."""
127        response = self.client.get(reverse("authentik_core:if-user"))
128        self.assertEqual(response.status_code, 200)
129        self.assertTemplateUsed(response, "if/user.html")
130        self.assertIn(b"ak-interface-user", response.content)
131
132    def test_admin_deep_subpath_renders_shell(self):
133        """An arbitrary nested admin path returns the admin shell, not a 404."""
134        response = self.client.get("/if/admin/identity/users/42")
135        self.assertEqual(response.status_code, 200)
136        self.assertTemplateUsed(response, "if/admin.html")
137        self.assertIn(b"ak-interface-admin", response.content)
138
139    def test_user_deep_subpath_renders_shell(self):
140        """An arbitrary nested user path returns the user shell, not a 404."""
141        response = self.client.get("/if/user/settings/sources")
142        self.assertEqual(response.status_code, 200)
143        self.assertTemplateUsed(response, "if/user.html")
144        self.assertIn(b"ak-interface-user", response.content)
145
146    def test_admin_subpath_is_reversible(self):
147        """The admin catch-all route reverses with a path kwarg."""
148        self.assertEqual(
149            reverse("authentik_core:if-admin-path", kwargs={"path": "flow/inspector"}),
150            "/if/admin/flow/inspector",
151        )
152
153    def test_user_subpath_is_reversible(self):
154        """The user catch-all route reverses with a path kwarg."""
155        self.assertEqual(
156            reverse("authentik_core:if-user-path", kwargs={"path": "settings"}),
157            "/if/user/settings",
158        )
159
160    def test_deep_subpath_carries_interface_context(self):
161        """The deep-path shell carries the same injected config as the root."""
162        response = self.client.get("/if/admin/core/applications")
163        self.assertEqual(response.status_code, 200)
164        # base/header_js.html injects the interface context as data, not script.
165        self.assertIn(b'id="ak-config"', response.content)
166        self.assertIn(b'<meta name="ak-base-url-rel" content="/">', response.content)
167
168    def test_exact_admin_prefix_not_shadowed_by_catchall(self):
169        """Ordering: the exact route wins over the catch-all for /if/admin/."""
170        self.assertEqual(resolve("/if/admin/").url_name, "if-admin")
171
172    def test_exact_user_prefix_not_shadowed_by_catchall(self):
173        """Ordering: the exact route wins over the catch-all for /if/user/."""
174        self.assertEqual(resolve("/if/user/").url_name, "if-user")
175
176    def test_flow_route_not_shadowed(self):
177        """The catch-alls must not swallow flow URLs."""
178        self.assertEqual(resolve("/if/flow/some-slug/").url_name, "if-flow")
179
180    def test_ws_client_route_not_shadowed(self):
181        """The catch-alls must not swallow the websocket fallback URL."""
182        self.assertNotIn(resolve("/ws/client/").url_name, ("if-admin-path", "if-user-path"))
183
184    def test_api_route_not_shadowed(self):
185        """The catch-alls live under core's urlconf and must not affect the API."""
186        self.assertNotIn(resolve("/api/v3/core/users/").url_name, ("if-admin-path", "if-user-path"))
187
188    def test_web_path_reflected_in_shell_context(self):
189        """When web.path is non-root, the shell advertises it as ak-base-url-rel.
190
191        Routing under web.path is resolved at import time in the root urlconf, so
192        only the request-time context value is exercised here; deployment-prefix
193        routing is covered by the Playwright web.path smoke test in a later PR.
194        """
195        with CONFIG.patch("web.path", "/auth/"):
196            response = self.client.get("/if/user/settings")
197        self.assertEqual(response.status_code, 200)
198        self.assertIn(b'<meta name="ak-base-url-rel" content="/auth/">', response.content)

Path-based routing: any subpath under if/admin/ and if/user/ renders the SPA shell.

def setUp(self):
112    def setUp(self):
113        Setup.set(True)
114        self.user = create_test_user(type=UserTypes.INTERNAL)
115        create_test_brand()
116        self.client.force_login(self.user)

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

def test_admin_exact_prefix_renders_shell(self):
118    def test_admin_exact_prefix_renders_shell(self):
119        """The exact /if/admin/ prefix still renders the admin interface."""
120        response = self.client.get(reverse("authentik_core:if-admin"))
121        self.assertEqual(response.status_code, 200)
122        self.assertTemplateUsed(response, "if/admin.html")
123        self.assertIn(b"ak-interface-admin", response.content)

The exact /if/admin/ prefix still renders the admin interface.

def test_user_exact_prefix_renders_shell(self):
125    def test_user_exact_prefix_renders_shell(self):
126        """The exact /if/user/ prefix still renders the user interface."""
127        response = self.client.get(reverse("authentik_core:if-user"))
128        self.assertEqual(response.status_code, 200)
129        self.assertTemplateUsed(response, "if/user.html")
130        self.assertIn(b"ak-interface-user", response.content)

The exact /if/user/ prefix still renders the user interface.

def test_admin_deep_subpath_renders_shell(self):
132    def test_admin_deep_subpath_renders_shell(self):
133        """An arbitrary nested admin path returns the admin shell, not a 404."""
134        response = self.client.get("/if/admin/identity/users/42")
135        self.assertEqual(response.status_code, 200)
136        self.assertTemplateUsed(response, "if/admin.html")
137        self.assertIn(b"ak-interface-admin", response.content)

An arbitrary nested admin path returns the admin shell, not a 404.

def test_user_deep_subpath_renders_shell(self):
139    def test_user_deep_subpath_renders_shell(self):
140        """An arbitrary nested user path returns the user shell, not a 404."""
141        response = self.client.get("/if/user/settings/sources")
142        self.assertEqual(response.status_code, 200)
143        self.assertTemplateUsed(response, "if/user.html")
144        self.assertIn(b"ak-interface-user", response.content)

An arbitrary nested user path returns the user shell, not a 404.

def test_admin_subpath_is_reversible(self):
146    def test_admin_subpath_is_reversible(self):
147        """The admin catch-all route reverses with a path kwarg."""
148        self.assertEqual(
149            reverse("authentik_core:if-admin-path", kwargs={"path": "flow/inspector"}),
150            "/if/admin/flow/inspector",
151        )

The admin catch-all route reverses with a path kwarg.

def test_user_subpath_is_reversible(self):
153    def test_user_subpath_is_reversible(self):
154        """The user catch-all route reverses with a path kwarg."""
155        self.assertEqual(
156            reverse("authentik_core:if-user-path", kwargs={"path": "settings"}),
157            "/if/user/settings",
158        )

The user catch-all route reverses with a path kwarg.

def test_deep_subpath_carries_interface_context(self):
160    def test_deep_subpath_carries_interface_context(self):
161        """The deep-path shell carries the same injected config as the root."""
162        response = self.client.get("/if/admin/core/applications")
163        self.assertEqual(response.status_code, 200)
164        # base/header_js.html injects the interface context as data, not script.
165        self.assertIn(b'id="ak-config"', response.content)
166        self.assertIn(b'<meta name="ak-base-url-rel" content="/">', response.content)

The deep-path shell carries the same injected config as the root.

def test_exact_admin_prefix_not_shadowed_by_catchall(self):
168    def test_exact_admin_prefix_not_shadowed_by_catchall(self):
169        """Ordering: the exact route wins over the catch-all for /if/admin/."""
170        self.assertEqual(resolve("/if/admin/").url_name, "if-admin")

Ordering: the exact route wins over the catch-all for /if/admin/.

def test_exact_user_prefix_not_shadowed_by_catchall(self):
172    def test_exact_user_prefix_not_shadowed_by_catchall(self):
173        """Ordering: the exact route wins over the catch-all for /if/user/."""
174        self.assertEqual(resolve("/if/user/").url_name, "if-user")

Ordering: the exact route wins over the catch-all for /if/user/.

def test_flow_route_not_shadowed(self):
176    def test_flow_route_not_shadowed(self):
177        """The catch-alls must not swallow flow URLs."""
178        self.assertEqual(resolve("/if/flow/some-slug/").url_name, "if-flow")

The catch-alls must not swallow flow URLs.

def test_ws_client_route_not_shadowed(self):
180    def test_ws_client_route_not_shadowed(self):
181        """The catch-alls must not swallow the websocket fallback URL."""
182        self.assertNotIn(resolve("/ws/client/").url_name, ("if-admin-path", "if-user-path"))

The catch-alls must not swallow the websocket fallback URL.

def test_api_route_not_shadowed(self):
184    def test_api_route_not_shadowed(self):
185        """The catch-alls live under core's urlconf and must not affect the API."""
186        self.assertNotIn(resolve("/api/v3/core/users/").url_name, ("if-admin-path", "if-user-path"))

The catch-alls live under core's urlconf and must not affect the API.

def test_web_path_reflected_in_shell_context(self):
188    def test_web_path_reflected_in_shell_context(self):
189        """When web.path is non-root, the shell advertises it as ak-base-url-rel.
190
191        Routing under web.path is resolved at import time in the root urlconf, so
192        only the request-time context value is exercised here; deployment-prefix
193        routing is covered by the Playwright web.path smoke test in a later PR.
194        """
195        with CONFIG.patch("web.path", "/auth/"):
196            response = self.client.get("/if/user/settings")
197        self.assertEqual(response.status_code, 200)
198        self.assertIn(b'<meta name="ak-base-url-rel" content="/auth/">', response.content)

When web.path is non-root, the shell advertises it as ak-base-url-rel.

Routing under web.path is resolved at import time in the root urlconf, so only the request-time context value is exercised here; deployment-prefix routing is covered by the Playwright web.path smoke test in a later PR.

class TestInterfaceFavicon(django.test.testcases.TestCase):
201class TestInterfaceFavicon(TestCase):
202    """The favicon links follow the brand's configured theme, and fall back to the
203    system color scheme only when the brand expresses none."""
204
205    def setUp(self):
206        Setup.set(True)
207        self.user = create_test_user(type=UserTypes.INTERNAL)
208        self.brand = create_test_brand(
209            branding_favicon="https://example.com/icon-%(theme)s.png",
210        )
211        self.client.force_login(self.user)
212
213    def _render(self, ui_theme: str | None = None) -> str:
214        if ui_theme:
215            self.brand.attributes = {"settings": {"theme": {"base": ui_theme}}}
216            self.brand.save()
217        response = self.client.get(reverse("authentik_core:if-user"))
218        self.assertEqual(response.status_code, 200)
219        return response.content.decode()
220
221    def test_automatic_theme_uses_media_queries(self):
222        """Without a brand theme, the browser picks the favicon by system color scheme."""
223        content = self._render()
224        self.assertIn(
225            '<link rel="icon" href="https://example.com/icon-light.png" '
226            'media="(prefers-color-scheme: light)">',
227            content,
228        )
229        self.assertIn(
230            '<link rel="icon" href="https://example.com/icon-dark.png" '
231            'media="(prefers-color-scheme: dark)">',
232            content,
233        )
234
235    def test_dark_theme_pins_favicon(self):
236        """A brand pinned to dark gets the dark favicon regardless of the system scheme."""
237        content = self._render(Themes.DARK)
238        self.assertIn('<link rel="icon" href="https://example.com/icon-dark.png">', content)
239        self.assertNotIn('href="https://example.com/icon-light.png"', content)
240
241    def test_light_theme_pins_favicon(self):
242        """A brand pinned to light gets the light favicon regardless of the system scheme."""
243        content = self._render(Themes.LIGHT)
244        self.assertIn('<link rel="icon" href="https://example.com/icon-light.png">', content)
245        self.assertNotIn('href="https://example.com/icon-dark.png"', content)
246
247    def test_untemplated_favicon_unchanged(self):
248        """A favicon without a %(theme)s variable renders a single plain link."""
249        self.brand.branding_favicon = "https://example.com/icon.png"
250        self.brand.save()
251        content = self._render()
252        self.assertIn('<link rel="icon" href="https://example.com/icon.png">', content)
253        self.assertIn('<link rel="shortcut icon" href="https://example.com/icon.png">', content)

The favicon links follow the brand's configured theme, and fall back to the system color scheme only when the brand expresses none.

def setUp(self):
205    def setUp(self):
206        Setup.set(True)
207        self.user = create_test_user(type=UserTypes.INTERNAL)
208        self.brand = create_test_brand(
209            branding_favicon="https://example.com/icon-%(theme)s.png",
210        )
211        self.client.force_login(self.user)

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

def test_automatic_theme_uses_media_queries(self):
221    def test_automatic_theme_uses_media_queries(self):
222        """Without a brand theme, the browser picks the favicon by system color scheme."""
223        content = self._render()
224        self.assertIn(
225            '<link rel="icon" href="https://example.com/icon-light.png" '
226            'media="(prefers-color-scheme: light)">',
227            content,
228        )
229        self.assertIn(
230            '<link rel="icon" href="https://example.com/icon-dark.png" '
231            'media="(prefers-color-scheme: dark)">',
232            content,
233        )

Without a brand theme, the browser picks the favicon by system color scheme.

def test_dark_theme_pins_favicon(self):
235    def test_dark_theme_pins_favicon(self):
236        """A brand pinned to dark gets the dark favicon regardless of the system scheme."""
237        content = self._render(Themes.DARK)
238        self.assertIn('<link rel="icon" href="https://example.com/icon-dark.png">', content)
239        self.assertNotIn('href="https://example.com/icon-light.png"', content)

A brand pinned to dark gets the dark favicon regardless of the system scheme.

def test_light_theme_pins_favicon(self):
241    def test_light_theme_pins_favicon(self):
242        """A brand pinned to light gets the light favicon regardless of the system scheme."""
243        content = self._render(Themes.LIGHT)
244        self.assertIn('<link rel="icon" href="https://example.com/icon-light.png">', content)
245        self.assertNotIn('href="https://example.com/icon-dark.png"', content)

A brand pinned to light gets the light favicon regardless of the system scheme.

def test_untemplated_favicon_unchanged(self):
247    def test_untemplated_favicon_unchanged(self):
248        """A favicon without a %(theme)s variable renders a single plain link."""
249        self.brand.branding_favicon = "https://example.com/icon.png"
250        self.brand.save()
251        content = self._render()
252        self.assertIn('<link rel="icon" href="https://example.com/icon.png">', content)
253        self.assertIn('<link rel="shortcut icon" href="https://example.com/icon.png">', content)

A favicon without a %(theme)s variable renders a single plain link.