authentik.outposts.tests.test_set_outpost_host

Ensure when AuthentikOutpostConfig changes the embedded_outpost's host is correct afterward

 1"""Ensure when AuthentikOutpostConfig changes the embedded_outpost's host is correct afterward"""
 2
 3from django.apps import apps
 4from django.test import TestCase
 5
 6from authentik.admin.utils import get_system_settings
 7from authentik.outposts.apps import MANAGED_OUTPOST
 8from authentik.outposts.models import Outpost
 9
10
11class TestEmbeddedOutpostHost(TestCase):
12    """Set the outpost.config.authentik_host from base_url"""
13
14    def setUp(self):
15        super().setUp()
16        self.settings = get_system_settings()
17        self.settings.base_url = ""
18        self.settings.save()
19        self.reconcile()
20        self.outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
21
22    def reconcile(self):
23        apps.get_app_config("authentik_outposts").embedded_outpost()
24
25    def set_base_url(self, base_url: str):
26        self.settings.base_url = base_url
27        self.settings.save()
28
29    def set_host(self, host: str):
30        config = self.outpost.config
31        config.authentik_host = host
32        self.outpost.config = config
33        self.outpost.save()
34
35    def get_host(self) -> str:
36        self.outpost.refresh_from_db()
37        return self.outpost.config.authentik_host
38
39    def test_empty_host(self):
40        """An empty host is set to base_url"""
41        self.set_host("")
42        self.set_base_url("https://goauthentik.io")
43        self.reconcile()
44        self.assertEqual(self.get_host(), "https://goauthentik.io")
45
46    def test_host_without_https(self):
47        """An invalid host needs to be updated with the base_url"""
48        self.set_host("goauthentik.io")
49        self.set_base_url("https://goauthentik.io")
50        self.reconcile()
51        self.assertEqual(self.get_host(), "https://goauthentik.io")
52
53    # This is testing for the existing behavior. It may not be the *right* behavior.
54    def test_existing_url_unchanged_after_update(self):
55        """Never overwrite an existing valid host"""
56        self.set_host("http://goauthentik.internal:9000")
57        self.set_base_url("https://goauthentik.io")
58        self.reconcile()
59        self.assertEqual(self.get_host(), "http://goauthentik.internal:9000")
class TestEmbeddedOutpostHost(django.test.testcases.TestCase):
12class TestEmbeddedOutpostHost(TestCase):
13    """Set the outpost.config.authentik_host from base_url"""
14
15    def setUp(self):
16        super().setUp()
17        self.settings = get_system_settings()
18        self.settings.base_url = ""
19        self.settings.save()
20        self.reconcile()
21        self.outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)
22
23    def reconcile(self):
24        apps.get_app_config("authentik_outposts").embedded_outpost()
25
26    def set_base_url(self, base_url: str):
27        self.settings.base_url = base_url
28        self.settings.save()
29
30    def set_host(self, host: str):
31        config = self.outpost.config
32        config.authentik_host = host
33        self.outpost.config = config
34        self.outpost.save()
35
36    def get_host(self) -> str:
37        self.outpost.refresh_from_db()
38        return self.outpost.config.authentik_host
39
40    def test_empty_host(self):
41        """An empty host is set to base_url"""
42        self.set_host("")
43        self.set_base_url("https://goauthentik.io")
44        self.reconcile()
45        self.assertEqual(self.get_host(), "https://goauthentik.io")
46
47    def test_host_without_https(self):
48        """An invalid host needs to be updated with the base_url"""
49        self.set_host("goauthentik.io")
50        self.set_base_url("https://goauthentik.io")
51        self.reconcile()
52        self.assertEqual(self.get_host(), "https://goauthentik.io")
53
54    # This is testing for the existing behavior. It may not be the *right* behavior.
55    def test_existing_url_unchanged_after_update(self):
56        """Never overwrite an existing valid host"""
57        self.set_host("http://goauthentik.internal:9000")
58        self.set_base_url("https://goauthentik.io")
59        self.reconcile()
60        self.assertEqual(self.get_host(), "http://goauthentik.internal:9000")

Set the outpost.config.authentik_host from base_url

def setUp(self):
15    def setUp(self):
16        super().setUp()
17        self.settings = get_system_settings()
18        self.settings.base_url = ""
19        self.settings.save()
20        self.reconcile()
21        self.outpost = Outpost.objects.get(managed=MANAGED_OUTPOST)

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

def reconcile(self):
23    def reconcile(self):
24        apps.get_app_config("authentik_outposts").embedded_outpost()
def set_base_url(self, base_url: str):
26    def set_base_url(self, base_url: str):
27        self.settings.base_url = base_url
28        self.settings.save()
def set_host(self, host: str):
30    def set_host(self, host: str):
31        config = self.outpost.config
32        config.authentik_host = host
33        self.outpost.config = config
34        self.outpost.save()
def get_host(self) -> str:
36    def get_host(self) -> str:
37        self.outpost.refresh_from_db()
38        return self.outpost.config.authentik_host
def test_empty_host(self):
40    def test_empty_host(self):
41        """An empty host is set to base_url"""
42        self.set_host("")
43        self.set_base_url("https://goauthentik.io")
44        self.reconcile()
45        self.assertEqual(self.get_host(), "https://goauthentik.io")

An empty host is set to base_url

def test_host_without_https(self):
47    def test_host_without_https(self):
48        """An invalid host needs to be updated with the base_url"""
49        self.set_host("goauthentik.io")
50        self.set_base_url("https://goauthentik.io")
51        self.reconcile()
52        self.assertEqual(self.get_host(), "https://goauthentik.io")

An invalid host needs to be updated with the base_url

def test_existing_url_unchanged_after_update(self):
55    def test_existing_url_unchanged_after_update(self):
56        """Never overwrite an existing valid host"""
57        self.set_host("http://goauthentik.internal:9000")
58        self.set_base_url("https://goauthentik.io")
59        self.reconcile()
60        self.assertEqual(self.get_host(), "http://goauthentik.internal:9000")

Never overwrite an existing valid host