authentik.admin.tests.test_base_url_backfill

Tests for backfilling base_url during the system settings reconcile

 1"""Tests for backfilling base_url during the system settings reconcile"""
 2
 3from django.apps import apps
 4from django.test import TestCase
 5
 6from authentik.admin.utils import get_system_settings
 7from authentik.blueprints.tests import reconcile_app
 8from authentik.events.logs import capture_logs
 9from authentik.lib.config import CONFIG
10from authentik.outposts.apps import MANAGED_OUTPOST
11from authentik.outposts.models import Outpost, OutpostConfig
12
13
14class TestBaseURLBackfill(TestCase):
15    """The system settings reconcile seeds base_url from the config value or the embedded
16    outpost host"""
17
18    def setUp(self):
19        super().setUp()
20        self.settings = get_system_settings()
21        self.settings.base_url = ""
22        self.settings.save()
23
24    @reconcile_app("authentik_outposts")
25    def test_backfill_from_outpost(self):
26        """base_url is backfilled from the embedded outpost host, without a trailing slash"""
27        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
28        outpost.config = OutpostConfig(authentik_host="https://outpost.example.com/")
29        outpost.save()
30        with CONFIG.patch("web.base_url", ""):
31            apps.get_app_config("authentik_admin").system_settings()
32        self.settings.refresh_from_db()
33        self.assertEqual(self.settings.base_url, "https://outpost.example.com")
34
35    @reconcile_app("authentik_outposts")
36    def test_backfill_from_config(self):
37        """The AUTHENTIK_WEB__BASE_URL config value takes precedence over the outpost host"""
38        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
39        outpost.config = OutpostConfig(authentik_host="https://outpost.example.com")
40        outpost.save()
41        with CONFIG.patch("web.base_url", "https://config.example.com"):
42            apps.get_app_config("authentik_admin").system_settings()
43        self.settings.refresh_from_db()
44        self.assertEqual(self.settings.base_url, "https://config.example.com")
45
46    def test_backfill_does_not_overwrite(self):
47        """A configured base_url is never overwritten by the backfill"""
48        self.settings.base_url = "https://set.example.com"
49        self.settings.save()
50        with CONFIG.patch("web.base_url", "https://config.example.com"):
51            apps.get_app_config("authentik_admin").system_settings()
52        self.settings.refresh_from_db()
53        self.assertEqual(self.settings.base_url, "https://set.example.com")
54
55    @reconcile_app("authentik_outposts")
56    def test_backfill_discards_invalid_outpost_host(self):
57        """An outpost host the settings API would reject is discarded rather than written,
58        since the backfill's `.update()` skips field validation"""
59        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
60        outpost.config = OutpostConfig(authentik_host="outpost.example.com")
61        outpost.save()
62        with CONFIG.patch("web.base_url", ""), capture_logs() as logs:
63            apps.get_app_config("authentik_admin").system_settings()
64        self.settings.refresh_from_db()
65        self.assertEqual(self.settings.base_url, "")
66        self.assertTrue(any("Discarding invalid base_url" in log.event for log in logs))
67
68    def test_backfill_no_outpost(self):
69        """With no embedded outpost and no config value, base_url stays empty"""
70        Outpost.objects.filter(managed=MANAGED_OUTPOST).delete()
71        with CONFIG.patch("web.base_url", ""):
72            apps.get_app_config("authentik_admin").system_settings()
73        self.settings.refresh_from_db()
74        self.assertEqual(self.settings.base_url, "")
class TestBaseURLBackfill(django.test.testcases.TestCase):
15class TestBaseURLBackfill(TestCase):
16    """The system settings reconcile seeds base_url from the config value or the embedded
17    outpost host"""
18
19    def setUp(self):
20        super().setUp()
21        self.settings = get_system_settings()
22        self.settings.base_url = ""
23        self.settings.save()
24
25    @reconcile_app("authentik_outposts")
26    def test_backfill_from_outpost(self):
27        """base_url is backfilled from the embedded outpost host, without a trailing slash"""
28        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
29        outpost.config = OutpostConfig(authentik_host="https://outpost.example.com/")
30        outpost.save()
31        with CONFIG.patch("web.base_url", ""):
32            apps.get_app_config("authentik_admin").system_settings()
33        self.settings.refresh_from_db()
34        self.assertEqual(self.settings.base_url, "https://outpost.example.com")
35
36    @reconcile_app("authentik_outposts")
37    def test_backfill_from_config(self):
38        """The AUTHENTIK_WEB__BASE_URL config value takes precedence over the outpost host"""
39        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
40        outpost.config = OutpostConfig(authentik_host="https://outpost.example.com")
41        outpost.save()
42        with CONFIG.patch("web.base_url", "https://config.example.com"):
43            apps.get_app_config("authentik_admin").system_settings()
44        self.settings.refresh_from_db()
45        self.assertEqual(self.settings.base_url, "https://config.example.com")
46
47    def test_backfill_does_not_overwrite(self):
48        """A configured base_url is never overwritten by the backfill"""
49        self.settings.base_url = "https://set.example.com"
50        self.settings.save()
51        with CONFIG.patch("web.base_url", "https://config.example.com"):
52            apps.get_app_config("authentik_admin").system_settings()
53        self.settings.refresh_from_db()
54        self.assertEqual(self.settings.base_url, "https://set.example.com")
55
56    @reconcile_app("authentik_outposts")
57    def test_backfill_discards_invalid_outpost_host(self):
58        """An outpost host the settings API would reject is discarded rather than written,
59        since the backfill's `.update()` skips field validation"""
60        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
61        outpost.config = OutpostConfig(authentik_host="outpost.example.com")
62        outpost.save()
63        with CONFIG.patch("web.base_url", ""), capture_logs() as logs:
64            apps.get_app_config("authentik_admin").system_settings()
65        self.settings.refresh_from_db()
66        self.assertEqual(self.settings.base_url, "")
67        self.assertTrue(any("Discarding invalid base_url" in log.event for log in logs))
68
69    def test_backfill_no_outpost(self):
70        """With no embedded outpost and no config value, base_url stays empty"""
71        Outpost.objects.filter(managed=MANAGED_OUTPOST).delete()
72        with CONFIG.patch("web.base_url", ""):
73            apps.get_app_config("authentik_admin").system_settings()
74        self.settings.refresh_from_db()
75        self.assertEqual(self.settings.base_url, "")

The system settings reconcile seeds base_url from the config value or the embedded outpost host

def setUp(self):
19    def setUp(self):
20        super().setUp()
21        self.settings = get_system_settings()
22        self.settings.base_url = ""
23        self.settings.save()

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

@reconcile_app('authentik_outposts')
def test_backfill_from_outpost(self):
25    @reconcile_app("authentik_outposts")
26    def test_backfill_from_outpost(self):
27        """base_url is backfilled from the embedded outpost host, without a trailing slash"""
28        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
29        outpost.config = OutpostConfig(authentik_host="https://outpost.example.com/")
30        outpost.save()
31        with CONFIG.patch("web.base_url", ""):
32            apps.get_app_config("authentik_admin").system_settings()
33        self.settings.refresh_from_db()
34        self.assertEqual(self.settings.base_url, "https://outpost.example.com")

base_url is backfilled from the embedded outpost host, without a trailing slash

@reconcile_app('authentik_outposts')
def test_backfill_from_config(self):
36    @reconcile_app("authentik_outposts")
37    def test_backfill_from_config(self):
38        """The AUTHENTIK_WEB__BASE_URL config value takes precedence over the outpost host"""
39        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
40        outpost.config = OutpostConfig(authentik_host="https://outpost.example.com")
41        outpost.save()
42        with CONFIG.patch("web.base_url", "https://config.example.com"):
43            apps.get_app_config("authentik_admin").system_settings()
44        self.settings.refresh_from_db()
45        self.assertEqual(self.settings.base_url, "https://config.example.com")

The AUTHENTIK_WEB__BASE_URL config value takes precedence over the outpost host

def test_backfill_does_not_overwrite(self):
47    def test_backfill_does_not_overwrite(self):
48        """A configured base_url is never overwritten by the backfill"""
49        self.settings.base_url = "https://set.example.com"
50        self.settings.save()
51        with CONFIG.patch("web.base_url", "https://config.example.com"):
52            apps.get_app_config("authentik_admin").system_settings()
53        self.settings.refresh_from_db()
54        self.assertEqual(self.settings.base_url, "https://set.example.com")

A configured base_url is never overwritten by the backfill

@reconcile_app('authentik_outposts')
def test_backfill_discards_invalid_outpost_host(self):
56    @reconcile_app("authentik_outposts")
57    def test_backfill_discards_invalid_outpost_host(self):
58        """An outpost host the settings API would reject is discarded rather than written,
59        since the backfill's `.update()` skips field validation"""
60        outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
61        outpost.config = OutpostConfig(authentik_host="outpost.example.com")
62        outpost.save()
63        with CONFIG.patch("web.base_url", ""), capture_logs() as logs:
64            apps.get_app_config("authentik_admin").system_settings()
65        self.settings.refresh_from_db()
66        self.assertEqual(self.settings.base_url, "")
67        self.assertTrue(any("Discarding invalid base_url" in log.event for log in logs))

An outpost host the settings API would reject is discarded rather than written, since the backfill's .update() skips field validation

def test_backfill_no_outpost(self):
69    def test_backfill_no_outpost(self):
70        """With no embedded outpost and no config value, base_url stays empty"""
71        Outpost.objects.filter(managed=MANAGED_OUTPOST).delete()
72        with CONFIG.patch("web.base_url", ""):
73            apps.get_app_config("authentik_admin").system_settings()
74        self.settings.refresh_from_db()
75        self.assertEqual(self.settings.base_url, "")

With no embedded outpost and no config value, base_url stays empty