authentik.brands.tests.test_brands

Test brands

  1"""Test brands"""
  2
  3from json import loads
  4
  5from django.conf import settings
  6from django.urls import reverse
  7from rest_framework.test import APITestCase
  8
  9from authentik.admin.flags import Flag
 10from authentik.blueprints.tests import apply_blueprint
 11from authentik.brands.models import SESSION_KEY_BRAND_SAFE_MODE, Brand
 12from authentik.core.models import Application
 13from authentik.core.tests.utils import (
 14    create_test_admin_user,
 15    create_test_brand,
 16    create_test_flow,
 17)
 18from authentik.lib.generators import generate_id
 19from authentik.providers.oauth2.models import OAuth2Provider
 20from authentik.providers.saml.models import SAMLProvider
 21
 22
 23class TestBrands(APITestCase):
 24    """Test brands"""
 25
 26    def setUp(self):
 27        super().setUp()
 28        Brand.objects.all().delete()
 29
 30    @property
 31    def default_flags(self) -> dict[str, object]:
 32        """Get current public flags.
 33
 34        Some tests define temporary Flag subclasses, so this can't be cached in setUp.
 35        """
 36        return {flag().key: flag.get() for flag in Flag.available(visibility="public")}
 37
 38    def test_current_brand(self):
 39        """Test Current brand API"""
 40        brand = create_test_brand()
 41        self.assertJSONEqual(
 42            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
 43            {
 44                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
 45                "branding_logo_themed_urls": None,
 46                "branding_favicon": "/static/dist/assets/icons/icon.png",
 47                "branding_favicon_themed_urls": None,
 48                "branding_title": "authentik",
 49                "branding_custom_css": "",
 50                "branding_map_tiles": "",
 51                "matched_domain": brand.domain,
 52                "ui_footer_links": [],
 53                "ui_theme": "automatic",
 54                "default_locale": "",
 55                "flags": self.default_flags,
 56            },
 57        )
 58
 59    def test_current_brand_authenticated_flags(self):
 60        """Authenticated-visibility flags are only exposed to authenticated requests"""
 61
 62        class _AuthedFlag(Flag[bool], key="brands_test_authed_flag"):
 63            default = True
 64            visibility = "authenticated"
 65
 66        create_test_brand()
 67
 68        # Anonymous requests only see public flags
 69        anon = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
 70        self.assertNotIn("brands_test_authed_flag", anon["flags"])
 71
 72        # Authenticated requests additionally see authenticated flags
 73        self.client.force_login(create_test_admin_user())
 74        authed = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
 75        self.assertTrue(authed["flags"]["brands_test_authed_flag"])
 76
 77    def test_brand_subdomain(self):
 78        """Test Current brand API"""
 79        Brand.objects.create(domain="bar.baz", branding_title="custom")
 80        self.assertJSONEqual(
 81            self.client.get(
 82                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
 83            ).content.decode(),
 84            {
 85                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
 86                "branding_logo_themed_urls": None,
 87                "branding_favicon": "/static/dist/assets/icons/icon.png",
 88                "branding_favicon_themed_urls": None,
 89                "branding_title": "custom",
 90                "branding_custom_css": "",
 91                "branding_map_tiles": "",
 92                "matched_domain": "bar.baz",
 93                "ui_footer_links": [],
 94                "ui_theme": "automatic",
 95                "default_locale": "",
 96                "flags": self.default_flags,
 97            },
 98        )
 99
100    def test_fallback(self):
101        """Test fallback brand"""
102        self.assertJSONEqual(
103            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
104            {
105                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
106                "branding_logo_themed_urls": None,
107                "branding_favicon": "/static/dist/assets/icons/icon.png",
108                "branding_favicon_themed_urls": None,
109                "branding_title": "authentik",
110                "branding_custom_css": "",
111                "branding_map_tiles": "",
112                "matched_domain": "fallback",
113                "ui_footer_links": [],
114                "ui_theme": "automatic",
115                "default_locale": "",
116                "flags": self.default_flags,
117            },
118        )
119
120    @apply_blueprint("default/default-brand.yaml")
121    def test_blueprint(self):
122        """Test Current brand API"""
123        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
124        response.pop("flow_authentication", None)
125        response.pop("flow_invalidation", None)
126        response.pop("flow_user_settings", None)
127        response.pop("flow_request", None)
128        self.assertEqual(
129            response,
130            {
131                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
132                "branding_logo_themed_urls": None,
133                "branding_favicon": "/static/dist/assets/icons/icon.png",
134                "branding_favicon_themed_urls": None,
135                "branding_title": "authentik",
136                "branding_custom_css": "",
137                "branding_map_tiles": "",
138                "matched_domain": "authentik-default",
139                "ui_footer_links": [],
140                "ui_theme": "automatic",
141                "default_locale": "",
142                "flags": self.default_flags,
143            },
144        )
145
146    @apply_blueprint("default/default-brand.yaml")
147    def test_blueprint_with_other_brand(self):
148        """Test Current brand API"""
149        Brand.objects.create(domain="bar.baz", branding_title="custom")
150        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
151        response.pop("flow_authentication", None)
152        response.pop("flow_invalidation", None)
153        response.pop("flow_user_settings", None)
154        response.pop("flow_request", None)
155        self.assertEqual(
156            response,
157            {
158                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
159                "branding_logo_themed_urls": None,
160                "branding_favicon": "/static/dist/assets/icons/icon.png",
161                "branding_favicon_themed_urls": None,
162                "branding_title": "authentik",
163                "branding_custom_css": "",
164                "branding_map_tiles": "",
165                "matched_domain": "authentik-default",
166                "ui_footer_links": [],
167                "ui_theme": "automatic",
168                "default_locale": "",
169                "flags": self.default_flags,
170            },
171        )
172        self.assertJSONEqual(
173            self.client.get(
174                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
175            ).content.decode(),
176            {
177                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
178                "branding_logo_themed_urls": None,
179                "branding_favicon": "/static/dist/assets/icons/icon.png",
180                "branding_favicon_themed_urls": None,
181                "branding_title": "custom",
182                "branding_custom_css": "",
183                "branding_map_tiles": "",
184                "matched_domain": "bar.baz",
185                "ui_footer_links": [],
186                "ui_theme": "automatic",
187                "default_locale": "",
188                "flags": self.default_flags,
189            },
190        )
191
192    def test_brand_subdomain_same_suffix(self):
193        """Test Current brand API"""
194        Brand.objects.create(domain="bar.baz", branding_title="custom-weak")
195        Brand.objects.create(domain="foo.bar.baz", branding_title="custom-strong")
196        self.assertJSONEqual(
197            self.client.get(
198                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
199            ).content.decode(),
200            {
201                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
202                "branding_logo_themed_urls": None,
203                "branding_favicon": "/static/dist/assets/icons/icon.png",
204                "branding_favicon_themed_urls": None,
205                "branding_title": "custom-strong",
206                "branding_custom_css": "",
207                "branding_map_tiles": "",
208                "matched_domain": "foo.bar.baz",
209                "ui_footer_links": [],
210                "ui_theme": "automatic",
211                "default_locale": "",
212                "flags": self.default_flags,
213            },
214        )
215
216    def test_brand_subdomain_other_suffix(self):
217        """Test Current brand API"""
218        Brand.objects.create(domain="bar.baz", branding_title="custom-weak")
219        Brand.objects.create(domain="foo.bar.baz", branding_title="custom-strong")
220        self.assertJSONEqual(
221            self.client.get(
222                reverse("authentik_api:brand-current"), HTTP_HOST="other.bar.baz"
223            ).content.decode(),
224            {
225                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
226                "branding_logo_themed_urls": None,
227                "branding_favicon": "/static/dist/assets/icons/icon.png",
228                "branding_favicon_themed_urls": None,
229                "branding_title": "custom-weak",
230                "branding_custom_css": "",
231                "branding_map_tiles": "",
232                "matched_domain": "bar.baz",
233                "ui_footer_links": [],
234                "ui_theme": "automatic",
235                "default_locale": "",
236                "flags": self.default_flags,
237            },
238        )
239
240    def test_create_default_multiple(self):
241        """Test attempted creation of multiple default brands"""
242        Brand.objects.create(
243            domain="foo",
244            default=True,
245            branding_title="custom",
246        )
247        user = create_test_admin_user()
248        self.client.force_login(user)
249        response = self.client.post(
250            reverse("authentik_api:brand-list"), data={"domain": "bar", "default": True}
251        )
252        self.assertEqual(response.status_code, 400)
253
254    def test_webfinger_no_app(self):
255        """Test Webfinger"""
256        create_test_brand()
257        self.assertJSONEqual(
258            self.client.get(reverse("authentik_brands:webfinger")).content.decode(), {}
259        )
260
261    def test_webfinger_not_supported(self):
262        """Test Webfinger"""
263        brand = create_test_brand()
264        provider = SAMLProvider.objects.create(
265            name=generate_id(),
266        )
267        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
268        brand.default_application = app
269        brand.save()
270        self.assertJSONEqual(
271            self.client.get(reverse("authentik_brands:webfinger")).content.decode(), {}
272        )
273
274    def test_webfinger_oidc(self):
275        """Test Webfinger"""
276        brand = create_test_brand()
277        provider = OAuth2Provider.objects.create(
278            name=generate_id(),
279        )
280        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
281        brand.default_application = app
282        brand.save()
283        self.assertJSONEqual(
284            self.client.get(reverse("authentik_brands:webfinger")).content.decode(),
285            {
286                "links": [
287                    {
288                        "href": f"http://testserver/application/o/{app.slug}/",
289                        "rel": "http://openid.net/specs/connect/1.0/issuer",
290                    }
291                ],
292                "subject": None,
293            },
294        )
295
296    def test_branding_url(self):
297        """Test branding attributes return correct values"""
298        brand = create_test_brand()
299        brand.branding_default_flow_background = "https://goauthentik.io/img/icon.png"
300        brand.branding_favicon = "https://goauthentik.io/img/icon.png"
301        brand.branding_logo = "https://goauthentik.io/img/icon.png"
302        brand.save()
303        self.assertEqual(
304            brand.branding_default_flow_background_url(), "https://goauthentik.io/img/icon.png"
305        )
306        self.assertJSONEqual(
307            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
308            {
309                "branding_logo": "https://goauthentik.io/img/icon.png",
310                "branding_logo_themed_urls": None,
311                "branding_favicon": "https://goauthentik.io/img/icon.png",
312                "branding_favicon_themed_urls": None,
313                "branding_title": "authentik",
314                "branding_custom_css": "",
315                "branding_map_tiles": "",
316                "matched_domain": brand.domain,
317                "ui_footer_links": [],
318                "ui_theme": "automatic",
319                "default_locale": "",
320                "flags": self.default_flags,
321            },
322        )
323
324    def test_custom_css(self):
325        """Test custom_css"""
326        brand = create_test_brand()
327        brand.branding_custom_css = """* {
328            font-family: "Foo bar";
329        }"""
330        brand.save()
331        res = self.client.get(reverse("authentik_core:if-user"))
332        self.assertEqual(res.status_code, 200)
333        self.assertIn(brand.branding_custom_css, res.content.decode())
334
335    def test_custom_css_safe_mode(self):
336        """Custom CSS is suppressed and the safe-mode class is set for safe-mode sessions"""
337        brand = create_test_brand()
338        brand.branding_custom_css = """* {
339            font-family: "Foo bar";
340        }"""
341        brand.save()
342        session = self.client.session
343        session[SESSION_KEY_BRAND_SAFE_MODE] = True
344        session.save()
345        res = self.client.get(reverse("authentik_core:if-user"))
346        self.assertEqual(res.status_code, 200)
347        body = res.content.decode()
348        self.assertNotIn(brand.branding_custom_css, body)
349        self.assertIn("ak-m-safe-mode", body)
350        # A banner is surfaced informing the user that branding is suppressed.
351        self.assertIn("ak-c-safe-mode", body)
352        self.assertIn("Recovery session is active. Custom branding is disabled.", body)
353
354    def test_current_brand_safe_mode(self):
355        """Current brand API suppresses custom CSS for safe-mode sessions"""
356        brand = create_test_brand()
357        brand.branding_custom_css = "* { color: red; }"
358        brand.save()
359        session = self.client.session
360        session[SESSION_KEY_BRAND_SAFE_MODE] = True
361        session.save()
362        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
363        self.assertEqual(response["branding_custom_css"], "")
364
365    def test_locale_cookie_wins_over_brand_locale(self):
366        """A language picked in the flow's language selector takes precedence over the
367        brand's locale"""
368        create_test_brand(attributes={"settings": {"locale": "de"}})
369        flow = create_test_flow()
370        self.client.cookies[settings.LANGUAGE_COOKIE_NAME] = "fr-FR"
371        res = self.client.get(reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug}))
372        self.assertEqual(res.status_code, 200)
373        self.assertIn(b'lang="fr"', res.content)
class TestBrands(rest_framework.test.APITestCase):
 24class TestBrands(APITestCase):
 25    """Test brands"""
 26
 27    def setUp(self):
 28        super().setUp()
 29        Brand.objects.all().delete()
 30
 31    @property
 32    def default_flags(self) -> dict[str, object]:
 33        """Get current public flags.
 34
 35        Some tests define temporary Flag subclasses, so this can't be cached in setUp.
 36        """
 37        return {flag().key: flag.get() for flag in Flag.available(visibility="public")}
 38
 39    def test_current_brand(self):
 40        """Test Current brand API"""
 41        brand = create_test_brand()
 42        self.assertJSONEqual(
 43            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
 44            {
 45                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
 46                "branding_logo_themed_urls": None,
 47                "branding_favicon": "/static/dist/assets/icons/icon.png",
 48                "branding_favicon_themed_urls": None,
 49                "branding_title": "authentik",
 50                "branding_custom_css": "",
 51                "branding_map_tiles": "",
 52                "matched_domain": brand.domain,
 53                "ui_footer_links": [],
 54                "ui_theme": "automatic",
 55                "default_locale": "",
 56                "flags": self.default_flags,
 57            },
 58        )
 59
 60    def test_current_brand_authenticated_flags(self):
 61        """Authenticated-visibility flags are only exposed to authenticated requests"""
 62
 63        class _AuthedFlag(Flag[bool], key="brands_test_authed_flag"):
 64            default = True
 65            visibility = "authenticated"
 66
 67        create_test_brand()
 68
 69        # Anonymous requests only see public flags
 70        anon = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
 71        self.assertNotIn("brands_test_authed_flag", anon["flags"])
 72
 73        # Authenticated requests additionally see authenticated flags
 74        self.client.force_login(create_test_admin_user())
 75        authed = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
 76        self.assertTrue(authed["flags"]["brands_test_authed_flag"])
 77
 78    def test_brand_subdomain(self):
 79        """Test Current brand API"""
 80        Brand.objects.create(domain="bar.baz", branding_title="custom")
 81        self.assertJSONEqual(
 82            self.client.get(
 83                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
 84            ).content.decode(),
 85            {
 86                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
 87                "branding_logo_themed_urls": None,
 88                "branding_favicon": "/static/dist/assets/icons/icon.png",
 89                "branding_favicon_themed_urls": None,
 90                "branding_title": "custom",
 91                "branding_custom_css": "",
 92                "branding_map_tiles": "",
 93                "matched_domain": "bar.baz",
 94                "ui_footer_links": [],
 95                "ui_theme": "automatic",
 96                "default_locale": "",
 97                "flags": self.default_flags,
 98            },
 99        )
100
101    def test_fallback(self):
102        """Test fallback brand"""
103        self.assertJSONEqual(
104            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
105            {
106                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
107                "branding_logo_themed_urls": None,
108                "branding_favicon": "/static/dist/assets/icons/icon.png",
109                "branding_favicon_themed_urls": None,
110                "branding_title": "authentik",
111                "branding_custom_css": "",
112                "branding_map_tiles": "",
113                "matched_domain": "fallback",
114                "ui_footer_links": [],
115                "ui_theme": "automatic",
116                "default_locale": "",
117                "flags": self.default_flags,
118            },
119        )
120
121    @apply_blueprint("default/default-brand.yaml")
122    def test_blueprint(self):
123        """Test Current brand API"""
124        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
125        response.pop("flow_authentication", None)
126        response.pop("flow_invalidation", None)
127        response.pop("flow_user_settings", None)
128        response.pop("flow_request", None)
129        self.assertEqual(
130            response,
131            {
132                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
133                "branding_logo_themed_urls": None,
134                "branding_favicon": "/static/dist/assets/icons/icon.png",
135                "branding_favicon_themed_urls": None,
136                "branding_title": "authentik",
137                "branding_custom_css": "",
138                "branding_map_tiles": "",
139                "matched_domain": "authentik-default",
140                "ui_footer_links": [],
141                "ui_theme": "automatic",
142                "default_locale": "",
143                "flags": self.default_flags,
144            },
145        )
146
147    @apply_blueprint("default/default-brand.yaml")
148    def test_blueprint_with_other_brand(self):
149        """Test Current brand API"""
150        Brand.objects.create(domain="bar.baz", branding_title="custom")
151        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
152        response.pop("flow_authentication", None)
153        response.pop("flow_invalidation", None)
154        response.pop("flow_user_settings", None)
155        response.pop("flow_request", None)
156        self.assertEqual(
157            response,
158            {
159                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
160                "branding_logo_themed_urls": None,
161                "branding_favicon": "/static/dist/assets/icons/icon.png",
162                "branding_favicon_themed_urls": None,
163                "branding_title": "authentik",
164                "branding_custom_css": "",
165                "branding_map_tiles": "",
166                "matched_domain": "authentik-default",
167                "ui_footer_links": [],
168                "ui_theme": "automatic",
169                "default_locale": "",
170                "flags": self.default_flags,
171            },
172        )
173        self.assertJSONEqual(
174            self.client.get(
175                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
176            ).content.decode(),
177            {
178                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
179                "branding_logo_themed_urls": None,
180                "branding_favicon": "/static/dist/assets/icons/icon.png",
181                "branding_favicon_themed_urls": None,
182                "branding_title": "custom",
183                "branding_custom_css": "",
184                "branding_map_tiles": "",
185                "matched_domain": "bar.baz",
186                "ui_footer_links": [],
187                "ui_theme": "automatic",
188                "default_locale": "",
189                "flags": self.default_flags,
190            },
191        )
192
193    def test_brand_subdomain_same_suffix(self):
194        """Test Current brand API"""
195        Brand.objects.create(domain="bar.baz", branding_title="custom-weak")
196        Brand.objects.create(domain="foo.bar.baz", branding_title="custom-strong")
197        self.assertJSONEqual(
198            self.client.get(
199                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
200            ).content.decode(),
201            {
202                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
203                "branding_logo_themed_urls": None,
204                "branding_favicon": "/static/dist/assets/icons/icon.png",
205                "branding_favicon_themed_urls": None,
206                "branding_title": "custom-strong",
207                "branding_custom_css": "",
208                "branding_map_tiles": "",
209                "matched_domain": "foo.bar.baz",
210                "ui_footer_links": [],
211                "ui_theme": "automatic",
212                "default_locale": "",
213                "flags": self.default_flags,
214            },
215        )
216
217    def test_brand_subdomain_other_suffix(self):
218        """Test Current brand API"""
219        Brand.objects.create(domain="bar.baz", branding_title="custom-weak")
220        Brand.objects.create(domain="foo.bar.baz", branding_title="custom-strong")
221        self.assertJSONEqual(
222            self.client.get(
223                reverse("authentik_api:brand-current"), HTTP_HOST="other.bar.baz"
224            ).content.decode(),
225            {
226                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
227                "branding_logo_themed_urls": None,
228                "branding_favicon": "/static/dist/assets/icons/icon.png",
229                "branding_favicon_themed_urls": None,
230                "branding_title": "custom-weak",
231                "branding_custom_css": "",
232                "branding_map_tiles": "",
233                "matched_domain": "bar.baz",
234                "ui_footer_links": [],
235                "ui_theme": "automatic",
236                "default_locale": "",
237                "flags": self.default_flags,
238            },
239        )
240
241    def test_create_default_multiple(self):
242        """Test attempted creation of multiple default brands"""
243        Brand.objects.create(
244            domain="foo",
245            default=True,
246            branding_title="custom",
247        )
248        user = create_test_admin_user()
249        self.client.force_login(user)
250        response = self.client.post(
251            reverse("authentik_api:brand-list"), data={"domain": "bar", "default": True}
252        )
253        self.assertEqual(response.status_code, 400)
254
255    def test_webfinger_no_app(self):
256        """Test Webfinger"""
257        create_test_brand()
258        self.assertJSONEqual(
259            self.client.get(reverse("authentik_brands:webfinger")).content.decode(), {}
260        )
261
262    def test_webfinger_not_supported(self):
263        """Test Webfinger"""
264        brand = create_test_brand()
265        provider = SAMLProvider.objects.create(
266            name=generate_id(),
267        )
268        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
269        brand.default_application = app
270        brand.save()
271        self.assertJSONEqual(
272            self.client.get(reverse("authentik_brands:webfinger")).content.decode(), {}
273        )
274
275    def test_webfinger_oidc(self):
276        """Test Webfinger"""
277        brand = create_test_brand()
278        provider = OAuth2Provider.objects.create(
279            name=generate_id(),
280        )
281        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
282        brand.default_application = app
283        brand.save()
284        self.assertJSONEqual(
285            self.client.get(reverse("authentik_brands:webfinger")).content.decode(),
286            {
287                "links": [
288                    {
289                        "href": f"http://testserver/application/o/{app.slug}/",
290                        "rel": "http://openid.net/specs/connect/1.0/issuer",
291                    }
292                ],
293                "subject": None,
294            },
295        )
296
297    def test_branding_url(self):
298        """Test branding attributes return correct values"""
299        brand = create_test_brand()
300        brand.branding_default_flow_background = "https://goauthentik.io/img/icon.png"
301        brand.branding_favicon = "https://goauthentik.io/img/icon.png"
302        brand.branding_logo = "https://goauthentik.io/img/icon.png"
303        brand.save()
304        self.assertEqual(
305            brand.branding_default_flow_background_url(), "https://goauthentik.io/img/icon.png"
306        )
307        self.assertJSONEqual(
308            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
309            {
310                "branding_logo": "https://goauthentik.io/img/icon.png",
311                "branding_logo_themed_urls": None,
312                "branding_favicon": "https://goauthentik.io/img/icon.png",
313                "branding_favicon_themed_urls": None,
314                "branding_title": "authentik",
315                "branding_custom_css": "",
316                "branding_map_tiles": "",
317                "matched_domain": brand.domain,
318                "ui_footer_links": [],
319                "ui_theme": "automatic",
320                "default_locale": "",
321                "flags": self.default_flags,
322            },
323        )
324
325    def test_custom_css(self):
326        """Test custom_css"""
327        brand = create_test_brand()
328        brand.branding_custom_css = """* {
329            font-family: "Foo bar";
330        }"""
331        brand.save()
332        res = self.client.get(reverse("authentik_core:if-user"))
333        self.assertEqual(res.status_code, 200)
334        self.assertIn(brand.branding_custom_css, res.content.decode())
335
336    def test_custom_css_safe_mode(self):
337        """Custom CSS is suppressed and the safe-mode class is set for safe-mode sessions"""
338        brand = create_test_brand()
339        brand.branding_custom_css = """* {
340            font-family: "Foo bar";
341        }"""
342        brand.save()
343        session = self.client.session
344        session[SESSION_KEY_BRAND_SAFE_MODE] = True
345        session.save()
346        res = self.client.get(reverse("authentik_core:if-user"))
347        self.assertEqual(res.status_code, 200)
348        body = res.content.decode()
349        self.assertNotIn(brand.branding_custom_css, body)
350        self.assertIn("ak-m-safe-mode", body)
351        # A banner is surfaced informing the user that branding is suppressed.
352        self.assertIn("ak-c-safe-mode", body)
353        self.assertIn("Recovery session is active. Custom branding is disabled.", body)
354
355    def test_current_brand_safe_mode(self):
356        """Current brand API suppresses custom CSS for safe-mode sessions"""
357        brand = create_test_brand()
358        brand.branding_custom_css = "* { color: red; }"
359        brand.save()
360        session = self.client.session
361        session[SESSION_KEY_BRAND_SAFE_MODE] = True
362        session.save()
363        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
364        self.assertEqual(response["branding_custom_css"], "")
365
366    def test_locale_cookie_wins_over_brand_locale(self):
367        """A language picked in the flow's language selector takes precedence over the
368        brand's locale"""
369        create_test_brand(attributes={"settings": {"locale": "de"}})
370        flow = create_test_flow()
371        self.client.cookies[settings.LANGUAGE_COOKIE_NAME] = "fr-FR"
372        res = self.client.get(reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug}))
373        self.assertEqual(res.status_code, 200)
374        self.assertIn(b'lang="fr"', res.content)

Test brands

def setUp(self):
27    def setUp(self):
28        super().setUp()
29        Brand.objects.all().delete()

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

default_flags: dict[str, object]
31    @property
32    def default_flags(self) -> dict[str, object]:
33        """Get current public flags.
34
35        Some tests define temporary Flag subclasses, so this can't be cached in setUp.
36        """
37        return {flag().key: flag.get() for flag in Flag.available(visibility="public")}

Get current public flags.

Some tests define temporary Flag subclasses, so this can't be cached in setUp.

def test_current_brand(self):
39    def test_current_brand(self):
40        """Test Current brand API"""
41        brand = create_test_brand()
42        self.assertJSONEqual(
43            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
44            {
45                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
46                "branding_logo_themed_urls": None,
47                "branding_favicon": "/static/dist/assets/icons/icon.png",
48                "branding_favicon_themed_urls": None,
49                "branding_title": "authentik",
50                "branding_custom_css": "",
51                "branding_map_tiles": "",
52                "matched_domain": brand.domain,
53                "ui_footer_links": [],
54                "ui_theme": "automatic",
55                "default_locale": "",
56                "flags": self.default_flags,
57            },
58        )

Test Current brand API

def test_current_brand_authenticated_flags(self):
60    def test_current_brand_authenticated_flags(self):
61        """Authenticated-visibility flags are only exposed to authenticated requests"""
62
63        class _AuthedFlag(Flag[bool], key="brands_test_authed_flag"):
64            default = True
65            visibility = "authenticated"
66
67        create_test_brand()
68
69        # Anonymous requests only see public flags
70        anon = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
71        self.assertNotIn("brands_test_authed_flag", anon["flags"])
72
73        # Authenticated requests additionally see authenticated flags
74        self.client.force_login(create_test_admin_user())
75        authed = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
76        self.assertTrue(authed["flags"]["brands_test_authed_flag"])

Authenticated-visibility flags are only exposed to authenticated requests

def test_brand_subdomain(self):
78    def test_brand_subdomain(self):
79        """Test Current brand API"""
80        Brand.objects.create(domain="bar.baz", branding_title="custom")
81        self.assertJSONEqual(
82            self.client.get(
83                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
84            ).content.decode(),
85            {
86                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
87                "branding_logo_themed_urls": None,
88                "branding_favicon": "/static/dist/assets/icons/icon.png",
89                "branding_favicon_themed_urls": None,
90                "branding_title": "custom",
91                "branding_custom_css": "",
92                "branding_map_tiles": "",
93                "matched_domain": "bar.baz",
94                "ui_footer_links": [],
95                "ui_theme": "automatic",
96                "default_locale": "",
97                "flags": self.default_flags,
98            },
99        )

Test Current brand API

def test_fallback(self):
101    def test_fallback(self):
102        """Test fallback brand"""
103        self.assertJSONEqual(
104            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
105            {
106                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
107                "branding_logo_themed_urls": None,
108                "branding_favicon": "/static/dist/assets/icons/icon.png",
109                "branding_favicon_themed_urls": None,
110                "branding_title": "authentik",
111                "branding_custom_css": "",
112                "branding_map_tiles": "",
113                "matched_domain": "fallback",
114                "ui_footer_links": [],
115                "ui_theme": "automatic",
116                "default_locale": "",
117                "flags": self.default_flags,
118            },
119        )

Test fallback brand

@apply_blueprint('default/default-brand.yaml')
def test_blueprint(self):
121    @apply_blueprint("default/default-brand.yaml")
122    def test_blueprint(self):
123        """Test Current brand API"""
124        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
125        response.pop("flow_authentication", None)
126        response.pop("flow_invalidation", None)
127        response.pop("flow_user_settings", None)
128        response.pop("flow_request", None)
129        self.assertEqual(
130            response,
131            {
132                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
133                "branding_logo_themed_urls": None,
134                "branding_favicon": "/static/dist/assets/icons/icon.png",
135                "branding_favicon_themed_urls": None,
136                "branding_title": "authentik",
137                "branding_custom_css": "",
138                "branding_map_tiles": "",
139                "matched_domain": "authentik-default",
140                "ui_footer_links": [],
141                "ui_theme": "automatic",
142                "default_locale": "",
143                "flags": self.default_flags,
144            },
145        )

Test Current brand API

@apply_blueprint('default/default-brand.yaml')
def test_blueprint_with_other_brand(self):
147    @apply_blueprint("default/default-brand.yaml")
148    def test_blueprint_with_other_brand(self):
149        """Test Current brand API"""
150        Brand.objects.create(domain="bar.baz", branding_title="custom")
151        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
152        response.pop("flow_authentication", None)
153        response.pop("flow_invalidation", None)
154        response.pop("flow_user_settings", None)
155        response.pop("flow_request", None)
156        self.assertEqual(
157            response,
158            {
159                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
160                "branding_logo_themed_urls": None,
161                "branding_favicon": "/static/dist/assets/icons/icon.png",
162                "branding_favicon_themed_urls": None,
163                "branding_title": "authentik",
164                "branding_custom_css": "",
165                "branding_map_tiles": "",
166                "matched_domain": "authentik-default",
167                "ui_footer_links": [],
168                "ui_theme": "automatic",
169                "default_locale": "",
170                "flags": self.default_flags,
171            },
172        )
173        self.assertJSONEqual(
174            self.client.get(
175                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
176            ).content.decode(),
177            {
178                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
179                "branding_logo_themed_urls": None,
180                "branding_favicon": "/static/dist/assets/icons/icon.png",
181                "branding_favicon_themed_urls": None,
182                "branding_title": "custom",
183                "branding_custom_css": "",
184                "branding_map_tiles": "",
185                "matched_domain": "bar.baz",
186                "ui_footer_links": [],
187                "ui_theme": "automatic",
188                "default_locale": "",
189                "flags": self.default_flags,
190            },
191        )

Test Current brand API

def test_brand_subdomain_same_suffix(self):
193    def test_brand_subdomain_same_suffix(self):
194        """Test Current brand API"""
195        Brand.objects.create(domain="bar.baz", branding_title="custom-weak")
196        Brand.objects.create(domain="foo.bar.baz", branding_title="custom-strong")
197        self.assertJSONEqual(
198            self.client.get(
199                reverse("authentik_api:brand-current"), HTTP_HOST="foo.bar.baz"
200            ).content.decode(),
201            {
202                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
203                "branding_logo_themed_urls": None,
204                "branding_favicon": "/static/dist/assets/icons/icon.png",
205                "branding_favicon_themed_urls": None,
206                "branding_title": "custom-strong",
207                "branding_custom_css": "",
208                "branding_map_tiles": "",
209                "matched_domain": "foo.bar.baz",
210                "ui_footer_links": [],
211                "ui_theme": "automatic",
212                "default_locale": "",
213                "flags": self.default_flags,
214            },
215        )

Test Current brand API

def test_brand_subdomain_other_suffix(self):
217    def test_brand_subdomain_other_suffix(self):
218        """Test Current brand API"""
219        Brand.objects.create(domain="bar.baz", branding_title="custom-weak")
220        Brand.objects.create(domain="foo.bar.baz", branding_title="custom-strong")
221        self.assertJSONEqual(
222            self.client.get(
223                reverse("authentik_api:brand-current"), HTTP_HOST="other.bar.baz"
224            ).content.decode(),
225            {
226                "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg",
227                "branding_logo_themed_urls": None,
228                "branding_favicon": "/static/dist/assets/icons/icon.png",
229                "branding_favicon_themed_urls": None,
230                "branding_title": "custom-weak",
231                "branding_custom_css": "",
232                "branding_map_tiles": "",
233                "matched_domain": "bar.baz",
234                "ui_footer_links": [],
235                "ui_theme": "automatic",
236                "default_locale": "",
237                "flags": self.default_flags,
238            },
239        )

Test Current brand API

def test_create_default_multiple(self):
241    def test_create_default_multiple(self):
242        """Test attempted creation of multiple default brands"""
243        Brand.objects.create(
244            domain="foo",
245            default=True,
246            branding_title="custom",
247        )
248        user = create_test_admin_user()
249        self.client.force_login(user)
250        response = self.client.post(
251            reverse("authentik_api:brand-list"), data={"domain": "bar", "default": True}
252        )
253        self.assertEqual(response.status_code, 400)

Test attempted creation of multiple default brands

def test_webfinger_no_app(self):
255    def test_webfinger_no_app(self):
256        """Test Webfinger"""
257        create_test_brand()
258        self.assertJSONEqual(
259            self.client.get(reverse("authentik_brands:webfinger")).content.decode(), {}
260        )

Test Webfinger

def test_webfinger_not_supported(self):
262    def test_webfinger_not_supported(self):
263        """Test Webfinger"""
264        brand = create_test_brand()
265        provider = SAMLProvider.objects.create(
266            name=generate_id(),
267        )
268        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
269        brand.default_application = app
270        brand.save()
271        self.assertJSONEqual(
272            self.client.get(reverse("authentik_brands:webfinger")).content.decode(), {}
273        )

Test Webfinger

def test_webfinger_oidc(self):
275    def test_webfinger_oidc(self):
276        """Test Webfinger"""
277        brand = create_test_brand()
278        provider = OAuth2Provider.objects.create(
279            name=generate_id(),
280        )
281        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
282        brand.default_application = app
283        brand.save()
284        self.assertJSONEqual(
285            self.client.get(reverse("authentik_brands:webfinger")).content.decode(),
286            {
287                "links": [
288                    {
289                        "href": f"http://testserver/application/o/{app.slug}/",
290                        "rel": "http://openid.net/specs/connect/1.0/issuer",
291                    }
292                ],
293                "subject": None,
294            },
295        )

Test Webfinger

def test_branding_url(self):
297    def test_branding_url(self):
298        """Test branding attributes return correct values"""
299        brand = create_test_brand()
300        brand.branding_default_flow_background = "https://goauthentik.io/img/icon.png"
301        brand.branding_favicon = "https://goauthentik.io/img/icon.png"
302        brand.branding_logo = "https://goauthentik.io/img/icon.png"
303        brand.save()
304        self.assertEqual(
305            brand.branding_default_flow_background_url(), "https://goauthentik.io/img/icon.png"
306        )
307        self.assertJSONEqual(
308            self.client.get(reverse("authentik_api:brand-current")).content.decode(),
309            {
310                "branding_logo": "https://goauthentik.io/img/icon.png",
311                "branding_logo_themed_urls": None,
312                "branding_favicon": "https://goauthentik.io/img/icon.png",
313                "branding_favicon_themed_urls": None,
314                "branding_title": "authentik",
315                "branding_custom_css": "",
316                "branding_map_tiles": "",
317                "matched_domain": brand.domain,
318                "ui_footer_links": [],
319                "ui_theme": "automatic",
320                "default_locale": "",
321                "flags": self.default_flags,
322            },
323        )

Test branding attributes return correct values

def test_custom_css(self):
325    def test_custom_css(self):
326        """Test custom_css"""
327        brand = create_test_brand()
328        brand.branding_custom_css = """* {
329            font-family: "Foo bar";
330        }"""
331        brand.save()
332        res = self.client.get(reverse("authentik_core:if-user"))
333        self.assertEqual(res.status_code, 200)
334        self.assertIn(brand.branding_custom_css, res.content.decode())

Test custom_css

def test_custom_css_safe_mode(self):
336    def test_custom_css_safe_mode(self):
337        """Custom CSS is suppressed and the safe-mode class is set for safe-mode sessions"""
338        brand = create_test_brand()
339        brand.branding_custom_css = """* {
340            font-family: "Foo bar";
341        }"""
342        brand.save()
343        session = self.client.session
344        session[SESSION_KEY_BRAND_SAFE_MODE] = True
345        session.save()
346        res = self.client.get(reverse("authentik_core:if-user"))
347        self.assertEqual(res.status_code, 200)
348        body = res.content.decode()
349        self.assertNotIn(brand.branding_custom_css, body)
350        self.assertIn("ak-m-safe-mode", body)
351        # A banner is surfaced informing the user that branding is suppressed.
352        self.assertIn("ak-c-safe-mode", body)
353        self.assertIn("Recovery session is active. Custom branding is disabled.", body)

Custom CSS is suppressed and the safe-mode class is set for safe-mode sessions

def test_current_brand_safe_mode(self):
355    def test_current_brand_safe_mode(self):
356        """Current brand API suppresses custom CSS for safe-mode sessions"""
357        brand = create_test_brand()
358        brand.branding_custom_css = "* { color: red; }"
359        brand.save()
360        session = self.client.session
361        session[SESSION_KEY_BRAND_SAFE_MODE] = True
362        session.save()
363        response = loads(self.client.get(reverse("authentik_api:brand-current")).content.decode())
364        self.assertEqual(response["branding_custom_css"], "")

Current brand API suppresses custom CSS for safe-mode sessions