authentik.core.tests.test_applications_views

Test Applications API

  1"""Test Applications API"""
  2
  3from unittest.mock import MagicMock, patch
  4
  5from django.urls import reverse
  6
  7from authentik.brands.models import Brand
  8from authentik.core.models import Application
  9from authentik.core.tests.utils import create_test_admin_user, create_test_brand, create_test_flow
 10from authentik.flows.tests import APIFlowTestCase
 11from authentik.lib.generators import generate_id
 12from authentik.providers.oauth2.models import OAuth2Provider
 13
 14
 15class TestApplicationsViews(APIFlowTestCase):
 16    """Test applications Views"""
 17
 18    def setUp(self) -> None:
 19        self.user = create_test_admin_user()
 20        self.app = Application.objects.create(
 21            name="allowed", slug="allowed", meta_launch_url="https://goauthentik.io/%(username)s"
 22        )
 23
 24    def test_check_redirect(self):
 25        """Test redirect"""
 26        empty_flow = create_test_flow()
 27        brand: Brand = create_test_brand()
 28        brand.flow_authentication = empty_flow
 29        brand.save()
 30        response = self.client.get(
 31            reverse(
 32                "authentik_core:application-launch",
 33                kwargs={"application_slug": self.app.slug},
 34            ),
 35            follow=True,
 36        )
 37        self.assertEqual(response.status_code, 200)
 38        with patch(
 39            "authentik.flows.stage.StageView.get_pending_user", MagicMock(return_value=self.user)
 40        ):
 41            response = self.client.post(
 42                reverse("authentik_api:flow-executor", kwargs={"flow_slug": empty_flow.slug})
 43            )
 44            self.assertEqual(response.status_code, 200)
 45            self.assertStageRedirects(response, f"https://goauthentik.io/{self.user.username}")
 46
 47    def test_check_redirect_auth(self):
 48        """Test redirect"""
 49        self.client.force_login(self.user)
 50        empty_flow = create_test_flow()
 51        brand: Brand = create_test_brand()
 52        brand.flow_authentication = empty_flow
 53        brand.save()
 54        response = self.client.get(
 55            reverse(
 56                "authentik_core:application-launch",
 57                kwargs={"application_slug": self.app.slug},
 58            ),
 59        )
 60        self.assertEqual(response.status_code, 302)
 61        self.assertEqual(response.url, f"https://goauthentik.io/{self.user.username}")
 62
 63    def test_check_redirect_auth_relative(self):
 64        """Test redirect to a launch URL relative to the current host"""
 65        self.client.force_login(self.user)
 66        self.app.meta_launch_url = "/app/%(username)s"
 67        self.app.save()
 68        response = self.client.get(
 69            reverse(
 70                "authentik_core:application-launch",
 71                kwargs={"application_slug": self.app.slug},
 72            ),
 73        )
 74        self.assertEqual(response.status_code, 302)
 75        self.assertEqual(response.url, f"/app/{self.user.username}")
 76
 77    def test_check_redirect_auth_ssh(self):
 78        """Test redirect to a launch URL with a non-HTTP scheme"""
 79        self.client.force_login(self.user)
 80        self.app.meta_launch_url = "ssh://%(username)s@goauthentik.io"
 81        self.app.save()
 82        response = self.client.get(
 83            reverse(
 84                "authentik_core:application-launch",
 85                kwargs={"application_slug": self.app.slug},
 86            ),
 87        )
 88        self.assertEqual(response.status_code, 302)
 89        self.assertEqual(response.url, f"ssh://{self.user.username}@goauthentik.io")
 90
 91    def test_check_redirect_auth_invalid_rendered_relative_url(self):
 92        """A formatted launch URL must still be root-relative."""
 93        self.user.name = "/"
 94        self.user.save(update_fields=["name"])
 95        self.client.force_login(self.user)
 96        self.app.meta_launch_url = "/%(name)s"
 97        self.app.save(update_fields=["meta_launch_url"])
 98
 99        response = self.client.get(
100            reverse(
101                "authentik_core:application-launch",
102                kwargs={"application_slug": self.app.slug},
103            ),
104        )
105        self.assertEqual(response.status_code, 404)
106
107    def test_redirect_application_auth_flow(self):
108        """Test launching an application with a provider and an authentication flow set"""
109        self.client.logout()
110        auth_flow = create_test_flow()
111        prov = OAuth2Provider.objects.create(
112            name=generate_id(),
113            authentication_flow=auth_flow,
114        )
115        self.app.provider = prov
116        self.app.save()
117        with self.assertFlowFinishes() as plan:
118            response = self.client.get(
119                reverse(
120                    "authentik_core:application-launch",
121                    kwargs={"application_slug": self.app.slug},
122                ),
123            )
124            self.assertEqual(response.status_code, 302)
125            self.assertEqual(
126                response.url,
127                reverse("authentik_core:if-flow", kwargs={"flow_slug": auth_flow.slug}),
128            )
129        plan = plan()
130        self.assertEqual(len(plan.bindings), 1)
131        self.assertTrue(plan.bindings[0].stage.is_in_memory)
132
133    def test_redirect_application_no_auth(self):
134        """Test launching an application with a provider and an authentication flow set"""
135        self.client.logout()
136        empty_flow = create_test_flow()
137        brand: Brand = create_test_brand()
138        brand.flow_authentication = empty_flow
139        brand.save()
140
141        prov = OAuth2Provider.objects.create(
142            name=generate_id(),
143        )
144        self.app.provider = prov
145        self.app.save()
146        with self.assertFlowFinishes() as plan:
147            response = self.client.get(
148                reverse(
149                    "authentik_core:application-launch",
150                    kwargs={"application_slug": self.app.slug},
151                ),
152            )
153            self.assertEqual(response.status_code, 302)
154            self.assertEqual(
155                response.url,
156                reverse("authentik_core:if-flow", kwargs={"flow_slug": empty_flow.slug}),
157            )
158        plan = plan()
159        self.assertEqual(len(plan.bindings), 1)
160        self.assertTrue(plan.bindings[0].stage.is_in_memory)
class TestApplicationsViews(authentik.flows.tests.APIFlowTestCase):
 16class TestApplicationsViews(APIFlowTestCase):
 17    """Test applications Views"""
 18
 19    def setUp(self) -> None:
 20        self.user = create_test_admin_user()
 21        self.app = Application.objects.create(
 22            name="allowed", slug="allowed", meta_launch_url="https://goauthentik.io/%(username)s"
 23        )
 24
 25    def test_check_redirect(self):
 26        """Test redirect"""
 27        empty_flow = create_test_flow()
 28        brand: Brand = create_test_brand()
 29        brand.flow_authentication = empty_flow
 30        brand.save()
 31        response = self.client.get(
 32            reverse(
 33                "authentik_core:application-launch",
 34                kwargs={"application_slug": self.app.slug},
 35            ),
 36            follow=True,
 37        )
 38        self.assertEqual(response.status_code, 200)
 39        with patch(
 40            "authentik.flows.stage.StageView.get_pending_user", MagicMock(return_value=self.user)
 41        ):
 42            response = self.client.post(
 43                reverse("authentik_api:flow-executor", kwargs={"flow_slug": empty_flow.slug})
 44            )
 45            self.assertEqual(response.status_code, 200)
 46            self.assertStageRedirects(response, f"https://goauthentik.io/{self.user.username}")
 47
 48    def test_check_redirect_auth(self):
 49        """Test redirect"""
 50        self.client.force_login(self.user)
 51        empty_flow = create_test_flow()
 52        brand: Brand = create_test_brand()
 53        brand.flow_authentication = empty_flow
 54        brand.save()
 55        response = self.client.get(
 56            reverse(
 57                "authentik_core:application-launch",
 58                kwargs={"application_slug": self.app.slug},
 59            ),
 60        )
 61        self.assertEqual(response.status_code, 302)
 62        self.assertEqual(response.url, f"https://goauthentik.io/{self.user.username}")
 63
 64    def test_check_redirect_auth_relative(self):
 65        """Test redirect to a launch URL relative to the current host"""
 66        self.client.force_login(self.user)
 67        self.app.meta_launch_url = "/app/%(username)s"
 68        self.app.save()
 69        response = self.client.get(
 70            reverse(
 71                "authentik_core:application-launch",
 72                kwargs={"application_slug": self.app.slug},
 73            ),
 74        )
 75        self.assertEqual(response.status_code, 302)
 76        self.assertEqual(response.url, f"/app/{self.user.username}")
 77
 78    def test_check_redirect_auth_ssh(self):
 79        """Test redirect to a launch URL with a non-HTTP scheme"""
 80        self.client.force_login(self.user)
 81        self.app.meta_launch_url = "ssh://%(username)s@goauthentik.io"
 82        self.app.save()
 83        response = self.client.get(
 84            reverse(
 85                "authentik_core:application-launch",
 86                kwargs={"application_slug": self.app.slug},
 87            ),
 88        )
 89        self.assertEqual(response.status_code, 302)
 90        self.assertEqual(response.url, f"ssh://{self.user.username}@goauthentik.io")
 91
 92    def test_check_redirect_auth_invalid_rendered_relative_url(self):
 93        """A formatted launch URL must still be root-relative."""
 94        self.user.name = "/"
 95        self.user.save(update_fields=["name"])
 96        self.client.force_login(self.user)
 97        self.app.meta_launch_url = "/%(name)s"
 98        self.app.save(update_fields=["meta_launch_url"])
 99
100        response = self.client.get(
101            reverse(
102                "authentik_core:application-launch",
103                kwargs={"application_slug": self.app.slug},
104            ),
105        )
106        self.assertEqual(response.status_code, 404)
107
108    def test_redirect_application_auth_flow(self):
109        """Test launching an application with a provider and an authentication flow set"""
110        self.client.logout()
111        auth_flow = create_test_flow()
112        prov = OAuth2Provider.objects.create(
113            name=generate_id(),
114            authentication_flow=auth_flow,
115        )
116        self.app.provider = prov
117        self.app.save()
118        with self.assertFlowFinishes() as plan:
119            response = self.client.get(
120                reverse(
121                    "authentik_core:application-launch",
122                    kwargs={"application_slug": self.app.slug},
123                ),
124            )
125            self.assertEqual(response.status_code, 302)
126            self.assertEqual(
127                response.url,
128                reverse("authentik_core:if-flow", kwargs={"flow_slug": auth_flow.slug}),
129            )
130        plan = plan()
131        self.assertEqual(len(plan.bindings), 1)
132        self.assertTrue(plan.bindings[0].stage.is_in_memory)
133
134    def test_redirect_application_no_auth(self):
135        """Test launching an application with a provider and an authentication flow set"""
136        self.client.logout()
137        empty_flow = create_test_flow()
138        brand: Brand = create_test_brand()
139        brand.flow_authentication = empty_flow
140        brand.save()
141
142        prov = OAuth2Provider.objects.create(
143            name=generate_id(),
144        )
145        self.app.provider = prov
146        self.app.save()
147        with self.assertFlowFinishes() as plan:
148            response = self.client.get(
149                reverse(
150                    "authentik_core:application-launch",
151                    kwargs={"application_slug": self.app.slug},
152                ),
153            )
154            self.assertEqual(response.status_code, 302)
155            self.assertEqual(
156                response.url,
157                reverse("authentik_core:if-flow", kwargs={"flow_slug": empty_flow.slug}),
158            )
159        plan = plan()
160        self.assertEqual(len(plan.bindings), 1)
161        self.assertTrue(plan.bindings[0].stage.is_in_memory)

Test applications Views

def setUp(self) -> None:
19    def setUp(self) -> None:
20        self.user = create_test_admin_user()
21        self.app = Application.objects.create(
22            name="allowed", slug="allowed", meta_launch_url="https://goauthentik.io/%(username)s"
23        )

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

def test_check_redirect(self):
25    def test_check_redirect(self):
26        """Test redirect"""
27        empty_flow = create_test_flow()
28        brand: Brand = create_test_brand()
29        brand.flow_authentication = empty_flow
30        brand.save()
31        response = self.client.get(
32            reverse(
33                "authentik_core:application-launch",
34                kwargs={"application_slug": self.app.slug},
35            ),
36            follow=True,
37        )
38        self.assertEqual(response.status_code, 200)
39        with patch(
40            "authentik.flows.stage.StageView.get_pending_user", MagicMock(return_value=self.user)
41        ):
42            response = self.client.post(
43                reverse("authentik_api:flow-executor", kwargs={"flow_slug": empty_flow.slug})
44            )
45            self.assertEqual(response.status_code, 200)
46            self.assertStageRedirects(response, f"https://goauthentik.io/{self.user.username}")

Test redirect

def test_check_redirect_auth(self):
48    def test_check_redirect_auth(self):
49        """Test redirect"""
50        self.client.force_login(self.user)
51        empty_flow = create_test_flow()
52        brand: Brand = create_test_brand()
53        brand.flow_authentication = empty_flow
54        brand.save()
55        response = self.client.get(
56            reverse(
57                "authentik_core:application-launch",
58                kwargs={"application_slug": self.app.slug},
59            ),
60        )
61        self.assertEqual(response.status_code, 302)
62        self.assertEqual(response.url, f"https://goauthentik.io/{self.user.username}")

Test redirect

def test_check_redirect_auth_relative(self):
64    def test_check_redirect_auth_relative(self):
65        """Test redirect to a launch URL relative to the current host"""
66        self.client.force_login(self.user)
67        self.app.meta_launch_url = "/app/%(username)s"
68        self.app.save()
69        response = self.client.get(
70            reverse(
71                "authentik_core:application-launch",
72                kwargs={"application_slug": self.app.slug},
73            ),
74        )
75        self.assertEqual(response.status_code, 302)
76        self.assertEqual(response.url, f"/app/{self.user.username}")

Test redirect to a launch URL relative to the current host

def test_check_redirect_auth_ssh(self):
78    def test_check_redirect_auth_ssh(self):
79        """Test redirect to a launch URL with a non-HTTP scheme"""
80        self.client.force_login(self.user)
81        self.app.meta_launch_url = "ssh://%(username)s@goauthentik.io"
82        self.app.save()
83        response = self.client.get(
84            reverse(
85                "authentik_core:application-launch",
86                kwargs={"application_slug": self.app.slug},
87            ),
88        )
89        self.assertEqual(response.status_code, 302)
90        self.assertEqual(response.url, f"ssh://{self.user.username}@goauthentik.io")

Test redirect to a launch URL with a non-HTTP scheme

def test_check_redirect_auth_invalid_rendered_relative_url(self):
 92    def test_check_redirect_auth_invalid_rendered_relative_url(self):
 93        """A formatted launch URL must still be root-relative."""
 94        self.user.name = "/"
 95        self.user.save(update_fields=["name"])
 96        self.client.force_login(self.user)
 97        self.app.meta_launch_url = "/%(name)s"
 98        self.app.save(update_fields=["meta_launch_url"])
 99
100        response = self.client.get(
101            reverse(
102                "authentik_core:application-launch",
103                kwargs={"application_slug": self.app.slug},
104            ),
105        )
106        self.assertEqual(response.status_code, 404)

A formatted launch URL must still be root-relative.

def test_redirect_application_auth_flow(self):
108    def test_redirect_application_auth_flow(self):
109        """Test launching an application with a provider and an authentication flow set"""
110        self.client.logout()
111        auth_flow = create_test_flow()
112        prov = OAuth2Provider.objects.create(
113            name=generate_id(),
114            authentication_flow=auth_flow,
115        )
116        self.app.provider = prov
117        self.app.save()
118        with self.assertFlowFinishes() as plan:
119            response = self.client.get(
120                reverse(
121                    "authentik_core:application-launch",
122                    kwargs={"application_slug": self.app.slug},
123                ),
124            )
125            self.assertEqual(response.status_code, 302)
126            self.assertEqual(
127                response.url,
128                reverse("authentik_core:if-flow", kwargs={"flow_slug": auth_flow.slug}),
129            )
130        plan = plan()
131        self.assertEqual(len(plan.bindings), 1)
132        self.assertTrue(plan.bindings[0].stage.is_in_memory)

Test launching an application with a provider and an authentication flow set

def test_redirect_application_no_auth(self):
134    def test_redirect_application_no_auth(self):
135        """Test launching an application with a provider and an authentication flow set"""
136        self.client.logout()
137        empty_flow = create_test_flow()
138        brand: Brand = create_test_brand()
139        brand.flow_authentication = empty_flow
140        brand.save()
141
142        prov = OAuth2Provider.objects.create(
143            name=generate_id(),
144        )
145        self.app.provider = prov
146        self.app.save()
147        with self.assertFlowFinishes() as plan:
148            response = self.client.get(
149                reverse(
150                    "authentik_core:application-launch",
151                    kwargs={"application_slug": self.app.slug},
152                ),
153            )
154            self.assertEqual(response.status_code, 302)
155            self.assertEqual(
156                response.url,
157                reverse("authentik_core:if-flow", kwargs={"flow_slug": empty_flow.slug}),
158            )
159        plan = plan()
160        self.assertEqual(len(plan.bindings), 1)
161        self.assertTrue(plan.bindings[0].stage.is_in_memory)

Test launching an application with a provider and an authentication flow set