authentik.policies.tests.test_views

  1from django.http import Http404, HttpResponse
  2from django.test import TestCase
  3
  4from authentik.blueprints.tests import apply_blueprint
  5from authentik.core.models import Application, Group, Provider
  6from authentik.core.tests.utils import (
  7    RequestFactory,
  8    create_test_brand,
  9    create_test_user,
 10)
 11from authentik.flows.models import Flow, FlowDesignation
 12from authentik.lib.generators import generate_id
 13from authentik.policies.models import PolicyBinding
 14from authentik.policies.views import (
 15    PolicyAccessView,
 16)
 17
 18
 19class TestPolicyViews(TestCase):
 20    """Test PolicyAccessView"""
 21
 22    def setUp(self):
 23        super().setUp()
 24        self.factory = RequestFactory()
 25        self.user = create_test_user()
 26
 27    def test_pav(self):
 28        """Test simple policy access view"""
 29        provider = Provider.objects.create(
 30            name=generate_id(),
 31        )
 32        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
 33
 34        class TestView(PolicyAccessView):
 35            def resolve_provider_application(self):
 36                self.provider = provider
 37                self.application = app
 38
 39            def get(self, *args, **kwargs):
 40                return HttpResponse("foo")
 41
 42        req = self.factory.get("/")
 43        req.user = self.user
 44        res = TestView.as_view()(req)
 45        self.assertEqual(res.status_code, 200)
 46        self.assertEqual(res.content, b"foo")
 47
 48    def test_pav_unauthenticated_no_flow(self):
 49        """Test simple policy access view (unauthenticated access, no authentication flow)"""
 50        Flow.objects.filter(designation=FlowDesignation.AUTHENTICATION).delete()
 51        provider = Provider.objects.create(
 52            name=generate_id(),
 53        )
 54        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
 55
 56        class TestView(PolicyAccessView):
 57            def resolve_provider_application(self):
 58                self.provider = provider
 59                self.application = app
 60
 61            def get(self, *args, **kwargs):
 62                return HttpResponse("foo")
 63
 64        req = self.factory.get("/")
 65        req.brand = create_test_brand()
 66        with self.assertRaises(Http404):
 67            TestView.as_view()(req)
 68
 69    @apply_blueprint("default/flow-default-authentication-flow.yaml")
 70    def test_pav_unauthenticated_flow_no_acccess(self):
 71        """Test simple policy access view (unauthenticated access,
 72        authentication flow with policy)"""
 73        provider = Provider.objects.create(
 74            name=generate_id(),
 75        )
 76        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
 77        flow = Flow.objects.get(slug="default-authentication-flow")
 78        PolicyBinding.objects.create(
 79            target=flow, group=Group.objects.create(name=generate_id()), order=0
 80        )
 81
 82        class TestView(PolicyAccessView):
 83            def resolve_provider_application(self):
 84                self.provider = provider
 85                self.application = app
 86
 87            def get(self, *args, **kwargs):
 88                return HttpResponse("foo")
 89
 90        req = self.factory.get("/")
 91        req.brand = create_test_brand(flow_authentication=flow)
 92        with self.assertRaises(Http404):
 93            TestView.as_view()(req)
 94
 95    @apply_blueprint("default/flow-default-authentication-flow.yaml")
 96    def test_pav_unauthenticated_next_param(self):
 97        """Test simple policy access view (unauthenticated access, with checking next param)"""
 98        provider = Provider.objects.create(
 99            name=generate_id(),
100        )
101        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
102        flow = Flow.objects.get(slug="default-authentication-flow")
103
104        class TestView(PolicyAccessView):
105            def resolve_provider_application(self):
106                self.provider = provider
107                self.application = app
108
109            def get(self, *args, **kwargs):
110                return HttpResponse("foo")
111
112        req = self.factory.get("/")
113        req.brand = create_test_brand(flow_authentication=flow)
114        res = TestView.as_view()(req)
115        self.assertEqual(res.status_code, 302)
116        self.assertEqual(res.url, "/if/flow/default-authentication-flow/?next=%2F")
class TestPolicyViews(django.test.testcases.TestCase):
 20class TestPolicyViews(TestCase):
 21    """Test PolicyAccessView"""
 22
 23    def setUp(self):
 24        super().setUp()
 25        self.factory = RequestFactory()
 26        self.user = create_test_user()
 27
 28    def test_pav(self):
 29        """Test simple policy access view"""
 30        provider = Provider.objects.create(
 31            name=generate_id(),
 32        )
 33        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
 34
 35        class TestView(PolicyAccessView):
 36            def resolve_provider_application(self):
 37                self.provider = provider
 38                self.application = app
 39
 40            def get(self, *args, **kwargs):
 41                return HttpResponse("foo")
 42
 43        req = self.factory.get("/")
 44        req.user = self.user
 45        res = TestView.as_view()(req)
 46        self.assertEqual(res.status_code, 200)
 47        self.assertEqual(res.content, b"foo")
 48
 49    def test_pav_unauthenticated_no_flow(self):
 50        """Test simple policy access view (unauthenticated access, no authentication flow)"""
 51        Flow.objects.filter(designation=FlowDesignation.AUTHENTICATION).delete()
 52        provider = Provider.objects.create(
 53            name=generate_id(),
 54        )
 55        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
 56
 57        class TestView(PolicyAccessView):
 58            def resolve_provider_application(self):
 59                self.provider = provider
 60                self.application = app
 61
 62            def get(self, *args, **kwargs):
 63                return HttpResponse("foo")
 64
 65        req = self.factory.get("/")
 66        req.brand = create_test_brand()
 67        with self.assertRaises(Http404):
 68            TestView.as_view()(req)
 69
 70    @apply_blueprint("default/flow-default-authentication-flow.yaml")
 71    def test_pav_unauthenticated_flow_no_acccess(self):
 72        """Test simple policy access view (unauthenticated access,
 73        authentication flow with policy)"""
 74        provider = Provider.objects.create(
 75            name=generate_id(),
 76        )
 77        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
 78        flow = Flow.objects.get(slug="default-authentication-flow")
 79        PolicyBinding.objects.create(
 80            target=flow, group=Group.objects.create(name=generate_id()), order=0
 81        )
 82
 83        class TestView(PolicyAccessView):
 84            def resolve_provider_application(self):
 85                self.provider = provider
 86                self.application = app
 87
 88            def get(self, *args, **kwargs):
 89                return HttpResponse("foo")
 90
 91        req = self.factory.get("/")
 92        req.brand = create_test_brand(flow_authentication=flow)
 93        with self.assertRaises(Http404):
 94            TestView.as_view()(req)
 95
 96    @apply_blueprint("default/flow-default-authentication-flow.yaml")
 97    def test_pav_unauthenticated_next_param(self):
 98        """Test simple policy access view (unauthenticated access, with checking next param)"""
 99        provider = Provider.objects.create(
100            name=generate_id(),
101        )
102        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
103        flow = Flow.objects.get(slug="default-authentication-flow")
104
105        class TestView(PolicyAccessView):
106            def resolve_provider_application(self):
107                self.provider = provider
108                self.application = app
109
110            def get(self, *args, **kwargs):
111                return HttpResponse("foo")
112
113        req = self.factory.get("/")
114        req.brand = create_test_brand(flow_authentication=flow)
115        res = TestView.as_view()(req)
116        self.assertEqual(res.status_code, 302)
117        self.assertEqual(res.url, "/if/flow/default-authentication-flow/?next=%2F")

Test PolicyAccessView

def setUp(self):
23    def setUp(self):
24        super().setUp()
25        self.factory = RequestFactory()
26        self.user = create_test_user()

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

def test_pav(self):
28    def test_pav(self):
29        """Test simple policy access view"""
30        provider = Provider.objects.create(
31            name=generate_id(),
32        )
33        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
34
35        class TestView(PolicyAccessView):
36            def resolve_provider_application(self):
37                self.provider = provider
38                self.application = app
39
40            def get(self, *args, **kwargs):
41                return HttpResponse("foo")
42
43        req = self.factory.get("/")
44        req.user = self.user
45        res = TestView.as_view()(req)
46        self.assertEqual(res.status_code, 200)
47        self.assertEqual(res.content, b"foo")

Test simple policy access view

def test_pav_unauthenticated_no_flow(self):
49    def test_pav_unauthenticated_no_flow(self):
50        """Test simple policy access view (unauthenticated access, no authentication flow)"""
51        Flow.objects.filter(designation=FlowDesignation.AUTHENTICATION).delete()
52        provider = Provider.objects.create(
53            name=generate_id(),
54        )
55        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
56
57        class TestView(PolicyAccessView):
58            def resolve_provider_application(self):
59                self.provider = provider
60                self.application = app
61
62            def get(self, *args, **kwargs):
63                return HttpResponse("foo")
64
65        req = self.factory.get("/")
66        req.brand = create_test_brand()
67        with self.assertRaises(Http404):
68            TestView.as_view()(req)

Test simple policy access view (unauthenticated access, no authentication flow)

@apply_blueprint('default/flow-default-authentication-flow.yaml')
def test_pav_unauthenticated_flow_no_acccess(self):
70    @apply_blueprint("default/flow-default-authentication-flow.yaml")
71    def test_pav_unauthenticated_flow_no_acccess(self):
72        """Test simple policy access view (unauthenticated access,
73        authentication flow with policy)"""
74        provider = Provider.objects.create(
75            name=generate_id(),
76        )
77        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
78        flow = Flow.objects.get(slug="default-authentication-flow")
79        PolicyBinding.objects.create(
80            target=flow, group=Group.objects.create(name=generate_id()), order=0
81        )
82
83        class TestView(PolicyAccessView):
84            def resolve_provider_application(self):
85                self.provider = provider
86                self.application = app
87
88            def get(self, *args, **kwargs):
89                return HttpResponse("foo")
90
91        req = self.factory.get("/")
92        req.brand = create_test_brand(flow_authentication=flow)
93        with self.assertRaises(Http404):
94            TestView.as_view()(req)

Test simple policy access view (unauthenticated access, authentication flow with policy)

@apply_blueprint('default/flow-default-authentication-flow.yaml')
def test_pav_unauthenticated_next_param(self):
 96    @apply_blueprint("default/flow-default-authentication-flow.yaml")
 97    def test_pav_unauthenticated_next_param(self):
 98        """Test simple policy access view (unauthenticated access, with checking next param)"""
 99        provider = Provider.objects.create(
100            name=generate_id(),
101        )
102        app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider)
103        flow = Flow.objects.get(slug="default-authentication-flow")
104
105        class TestView(PolicyAccessView):
106            def resolve_provider_application(self):
107                self.provider = provider
108                self.application = app
109
110            def get(self, *args, **kwargs):
111                return HttpResponse("foo")
112
113        req = self.factory.get("/")
114        req.brand = create_test_brand(flow_authentication=flow)
115        res = TestView.as_view()(req)
116        self.assertEqual(res.status_code, 302)
117        self.assertEqual(res.url, "/if/flow/default-authentication-flow/?next=%2F")

Test simple policy access view (unauthenticated access, with checking next param)