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 FlowTestCase
 11from authentik.lib.generators import generate_id
 12from authentik.providers.oauth2.models import OAuth2Provider
 13
 14
 15class TestApplicationsViews(FlowTestCase):
 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_redirect_application_auth_flow(self):
 64        """Test launching an application with a provider and an authentication flow set"""
 65        self.client.logout()
 66        auth_flow = create_test_flow()
 67        prov = OAuth2Provider.objects.create(
 68            name=generate_id(),
 69            authentication_flow=auth_flow,
 70        )
 71        self.app.provider = prov
 72        self.app.save()
 73        with self.assertFlowFinishes() as plan:
 74            response = self.client.get(
 75                reverse(
 76                    "authentik_core:application-launch",
 77                    kwargs={"application_slug": self.app.slug},
 78                ),
 79            )
 80            self.assertEqual(response.status_code, 302)
 81            self.assertEqual(
 82                response.url,
 83                reverse("authentik_core:if-flow", kwargs={"flow_slug": auth_flow.slug}),
 84            )
 85        plan = plan()
 86        self.assertEqual(len(plan.bindings), 1)
 87        self.assertTrue(plan.bindings[0].stage.is_in_memory)
 88
 89    def test_redirect_application_no_auth(self):
 90        """Test launching an application with a provider and an authentication flow set"""
 91        self.client.logout()
 92        empty_flow = create_test_flow()
 93        brand: Brand = create_test_brand()
 94        brand.flow_authentication = empty_flow
 95        brand.save()
 96
 97        prov = OAuth2Provider.objects.create(
 98            name=generate_id(),
 99        )
100        self.app.provider = prov
101        self.app.save()
102        with self.assertFlowFinishes() as plan:
103            response = self.client.get(
104                reverse(
105                    "authentik_core:application-launch",
106                    kwargs={"application_slug": self.app.slug},
107                ),
108            )
109            self.assertEqual(response.status_code, 302)
110            self.assertEqual(
111                response.url,
112                reverse("authentik_core:if-flow", kwargs={"flow_slug": empty_flow.slug}),
113            )
114        plan = plan()
115        self.assertEqual(len(plan.bindings), 1)
116        self.assertTrue(plan.bindings[0].stage.is_in_memory)
class TestApplicationsViews(authentik.flows.tests.FlowTestCase):
 16class TestApplicationsViews(FlowTestCase):
 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_redirect_application_auth_flow(self):
 65        """Test launching an application with a provider and an authentication flow set"""
 66        self.client.logout()
 67        auth_flow = create_test_flow()
 68        prov = OAuth2Provider.objects.create(
 69            name=generate_id(),
 70            authentication_flow=auth_flow,
 71        )
 72        self.app.provider = prov
 73        self.app.save()
 74        with self.assertFlowFinishes() as plan:
 75            response = self.client.get(
 76                reverse(
 77                    "authentik_core:application-launch",
 78                    kwargs={"application_slug": self.app.slug},
 79                ),
 80            )
 81            self.assertEqual(response.status_code, 302)
 82            self.assertEqual(
 83                response.url,
 84                reverse("authentik_core:if-flow", kwargs={"flow_slug": auth_flow.slug}),
 85            )
 86        plan = plan()
 87        self.assertEqual(len(plan.bindings), 1)
 88        self.assertTrue(plan.bindings[0].stage.is_in_memory)
 89
 90    def test_redirect_application_no_auth(self):
 91        """Test launching an application with a provider and an authentication flow set"""
 92        self.client.logout()
 93        empty_flow = create_test_flow()
 94        brand: Brand = create_test_brand()
 95        brand.flow_authentication = empty_flow
 96        brand.save()
 97
 98        prov = OAuth2Provider.objects.create(
 99            name=generate_id(),
100        )
101        self.app.provider = prov
102        self.app.save()
103        with self.assertFlowFinishes() as plan:
104            response = self.client.get(
105                reverse(
106                    "authentik_core:application-launch",
107                    kwargs={"application_slug": self.app.slug},
108                ),
109            )
110            self.assertEqual(response.status_code, 302)
111            self.assertEqual(
112                response.url,
113                reverse("authentik_core:if-flow", kwargs={"flow_slug": empty_flow.slug}),
114            )
115        plan = plan()
116        self.assertEqual(len(plan.bindings), 1)
117        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_redirect_application_auth_flow(self):
64    def test_redirect_application_auth_flow(self):
65        """Test launching an application with a provider and an authentication flow set"""
66        self.client.logout()
67        auth_flow = create_test_flow()
68        prov = OAuth2Provider.objects.create(
69            name=generate_id(),
70            authentication_flow=auth_flow,
71        )
72        self.app.provider = prov
73        self.app.save()
74        with self.assertFlowFinishes() as plan:
75            response = self.client.get(
76                reverse(
77                    "authentik_core:application-launch",
78                    kwargs={"application_slug": self.app.slug},
79                ),
80            )
81            self.assertEqual(response.status_code, 302)
82            self.assertEqual(
83                response.url,
84                reverse("authentik_core:if-flow", kwargs={"flow_slug": auth_flow.slug}),
85            )
86        plan = plan()
87        self.assertEqual(len(plan.bindings), 1)
88        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):
 90    def test_redirect_application_no_auth(self):
 91        """Test launching an application with a provider and an authentication flow set"""
 92        self.client.logout()
 93        empty_flow = create_test_flow()
 94        brand: Brand = create_test_brand()
 95        brand.flow_authentication = empty_flow
 96        brand.save()
 97
 98        prov = OAuth2Provider.objects.create(
 99            name=generate_id(),
100        )
101        self.app.provider = prov
102        self.app.save()
103        with self.assertFlowFinishes() as plan:
104            response = self.client.get(
105                reverse(
106                    "authentik_core:application-launch",
107                    kwargs={"application_slug": self.app.slug},
108                ),
109            )
110            self.assertEqual(response.status_code, 302)
111            self.assertEqual(
112                response.url,
113                reverse("authentik_core:if-flow", kwargs={"flow_slug": empty_flow.slug}),
114            )
115        plan = plan()
116        self.assertEqual(len(plan.bindings), 1)
117        self.assertTrue(plan.bindings[0].stage.is_in_memory)

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