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.flows.planner import PLAN_CONTEXT_CONTINUOUS_LOGIN_HOLD 13from authentik.flows.views.executor import SESSION_KEY_PLAN 14from authentik.lib.generators import generate_id 15from authentik.policies.models import PolicyBinding 16from authentik.policies.views import ( 17 PolicyAccessView, 18) 19 20 21class TestPolicyViews(TestCase): 22 """Test PolicyAccessView""" 23 24 def setUp(self): 25 super().setUp() 26 self.factory = RequestFactory() 27 self.user = create_test_user() 28 29 def test_pav(self): 30 """Test simple policy access view""" 31 provider = Provider.objects.create( 32 name=generate_id(), 33 ) 34 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 35 36 class TestView(PolicyAccessView): 37 def resolve_provider_application(self): 38 self.provider = provider 39 self.application = app 40 41 def get(self, *args, **kwargs): 42 return HttpResponse("foo") 43 44 req = self.factory.get("/") 45 req.user = self.user 46 res = TestView.as_view()(req) 47 self.assertEqual(res.status_code, 200) 48 self.assertEqual(res.content, b"foo") 49 50 def test_pav_unauthenticated_no_flow(self): 51 """Test simple policy access view (unauthenticated access, no authentication flow)""" 52 Flow.objects.filter(designation=FlowDesignation.AUTHENTICATION).delete() 53 provider = Provider.objects.create( 54 name=generate_id(), 55 ) 56 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 57 58 class TestView(PolicyAccessView): 59 def resolve_provider_application(self): 60 self.provider = provider 61 self.application = app 62 63 def get(self, *args, **kwargs): 64 return HttpResponse("foo") 65 66 req = self.factory.get("/") 67 req.brand = create_test_brand() 68 with self.assertRaises(Http404): 69 TestView.as_view()(req) 70 71 @apply_blueprint("default/flow-default-authentication-flow.yaml") 72 def test_pav_unauthenticated_flow_no_acccess(self): 73 """Test simple policy access view (unauthenticated access, 74 authentication flow with policy)""" 75 provider = Provider.objects.create( 76 name=generate_id(), 77 ) 78 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 79 flow = Flow.objects.get(slug="default-authentication-flow") 80 PolicyBinding.objects.create( 81 target=flow, group=Group.objects.create(name=generate_id()), order=0 82 ) 83 84 class TestView(PolicyAccessView): 85 def resolve_provider_application(self): 86 self.provider = provider 87 self.application = app 88 89 def get(self, *args, **kwargs): 90 return HttpResponse("foo") 91 92 req = self.factory.get("/") 93 req.brand = create_test_brand(flow_authentication=flow) 94 with self.assertRaises(Http404): 95 TestView.as_view()(req) 96 97 @apply_blueprint("default/flow-default-authentication-flow.yaml") 98 def test_pav_unauthenticated_next_param(self): 99 """Test simple policy access view (unauthenticated access, with checking next param)""" 100 provider = Provider.objects.create( 101 name=generate_id(), 102 ) 103 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 104 flow = Flow.objects.get(slug="default-authentication-flow") 105 106 class TestView(PolicyAccessView): 107 def resolve_provider_application(self): 108 self.provider = provider 109 self.application = app 110 111 def get(self, *args, **kwargs): 112 return HttpResponse("foo") 113 114 req = self.factory.get("/") 115 req.brand = create_test_brand(flow_authentication=flow) 116 res = TestView.as_view()(req) 117 self.assertEqual(res.status_code, 302) 118 self.assertEqual(res.url, "/if/flow/default-authentication-flow/?next=%2F") 119 120 @apply_blueprint("default/flow-default-authentication-flow.yaml") 121 def test_pav_continuous_login_hold_context(self): 122 """Provider views can release continuous-login tabs after authentication.""" 123 provider = Provider.objects.create(name=generate_id()) 124 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 125 flow = Flow.objects.get(slug="default-authentication-flow") 126 127 class TestView(PolicyAccessView): 128 def resolve_provider_application(self): 129 self.provider = provider 130 self.application = app 131 132 def continuous_login_hold_required(self) -> bool: 133 return False 134 135 req = self.factory.get("/") 136 req.brand = create_test_brand(flow_authentication=flow) 137 138 response = TestView.as_view()(req) 139 140 self.assertEqual(response.status_code, 302) 141 self.assertFalse(req.session[SESSION_KEY_PLAN].context[PLAN_CONTEXT_CONTINUOUS_LOGIN_HOLD])
class
TestPolicyViews(django.test.testcases.TestCase):
22class TestPolicyViews(TestCase): 23 """Test PolicyAccessView""" 24 25 def setUp(self): 26 super().setUp() 27 self.factory = RequestFactory() 28 self.user = create_test_user() 29 30 def test_pav(self): 31 """Test simple policy access view""" 32 provider = Provider.objects.create( 33 name=generate_id(), 34 ) 35 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 36 37 class TestView(PolicyAccessView): 38 def resolve_provider_application(self): 39 self.provider = provider 40 self.application = app 41 42 def get(self, *args, **kwargs): 43 return HttpResponse("foo") 44 45 req = self.factory.get("/") 46 req.user = self.user 47 res = TestView.as_view()(req) 48 self.assertEqual(res.status_code, 200) 49 self.assertEqual(res.content, b"foo") 50 51 def test_pav_unauthenticated_no_flow(self): 52 """Test simple policy access view (unauthenticated access, no authentication flow)""" 53 Flow.objects.filter(designation=FlowDesignation.AUTHENTICATION).delete() 54 provider = Provider.objects.create( 55 name=generate_id(), 56 ) 57 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 58 59 class TestView(PolicyAccessView): 60 def resolve_provider_application(self): 61 self.provider = provider 62 self.application = app 63 64 def get(self, *args, **kwargs): 65 return HttpResponse("foo") 66 67 req = self.factory.get("/") 68 req.brand = create_test_brand() 69 with self.assertRaises(Http404): 70 TestView.as_view()(req) 71 72 @apply_blueprint("default/flow-default-authentication-flow.yaml") 73 def test_pav_unauthenticated_flow_no_acccess(self): 74 """Test simple policy access view (unauthenticated access, 75 authentication flow with policy)""" 76 provider = Provider.objects.create( 77 name=generate_id(), 78 ) 79 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 80 flow = Flow.objects.get(slug="default-authentication-flow") 81 PolicyBinding.objects.create( 82 target=flow, group=Group.objects.create(name=generate_id()), order=0 83 ) 84 85 class TestView(PolicyAccessView): 86 def resolve_provider_application(self): 87 self.provider = provider 88 self.application = app 89 90 def get(self, *args, **kwargs): 91 return HttpResponse("foo") 92 93 req = self.factory.get("/") 94 req.brand = create_test_brand(flow_authentication=flow) 95 with self.assertRaises(Http404): 96 TestView.as_view()(req) 97 98 @apply_blueprint("default/flow-default-authentication-flow.yaml") 99 def test_pav_unauthenticated_next_param(self): 100 """Test simple policy access view (unauthenticated access, with checking next param)""" 101 provider = Provider.objects.create( 102 name=generate_id(), 103 ) 104 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 105 flow = Flow.objects.get(slug="default-authentication-flow") 106 107 class TestView(PolicyAccessView): 108 def resolve_provider_application(self): 109 self.provider = provider 110 self.application = app 111 112 def get(self, *args, **kwargs): 113 return HttpResponse("foo") 114 115 req = self.factory.get("/") 116 req.brand = create_test_brand(flow_authentication=flow) 117 res = TestView.as_view()(req) 118 self.assertEqual(res.status_code, 302) 119 self.assertEqual(res.url, "/if/flow/default-authentication-flow/?next=%2F") 120 121 @apply_blueprint("default/flow-default-authentication-flow.yaml") 122 def test_pav_continuous_login_hold_context(self): 123 """Provider views can release continuous-login tabs after authentication.""" 124 provider = Provider.objects.create(name=generate_id()) 125 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 126 flow = Flow.objects.get(slug="default-authentication-flow") 127 128 class TestView(PolicyAccessView): 129 def resolve_provider_application(self): 130 self.provider = provider 131 self.application = app 132 133 def continuous_login_hold_required(self) -> bool: 134 return False 135 136 req = self.factory.get("/") 137 req.brand = create_test_brand(flow_authentication=flow) 138 139 response = TestView.as_view()(req) 140 141 self.assertEqual(response.status_code, 302) 142 self.assertFalse(req.session[SESSION_KEY_PLAN].context[PLAN_CONTEXT_CONTINUOUS_LOGIN_HOLD])
Test PolicyAccessView
def
setUp(self):
25 def setUp(self): 26 super().setUp() 27 self.factory = RequestFactory() 28 self.user = create_test_user()
Hook method for setting up the test fixture before exercising it.
def
test_pav(self):
30 def test_pav(self): 31 """Test simple policy access view""" 32 provider = Provider.objects.create( 33 name=generate_id(), 34 ) 35 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 36 37 class TestView(PolicyAccessView): 38 def resolve_provider_application(self): 39 self.provider = provider 40 self.application = app 41 42 def get(self, *args, **kwargs): 43 return HttpResponse("foo") 44 45 req = self.factory.get("/") 46 req.user = self.user 47 res = TestView.as_view()(req) 48 self.assertEqual(res.status_code, 200) 49 self.assertEqual(res.content, b"foo")
Test simple policy access view
def
test_pav_unauthenticated_no_flow(self):
51 def test_pav_unauthenticated_no_flow(self): 52 """Test simple policy access view (unauthenticated access, no authentication flow)""" 53 Flow.objects.filter(designation=FlowDesignation.AUTHENTICATION).delete() 54 provider = Provider.objects.create( 55 name=generate_id(), 56 ) 57 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 58 59 class TestView(PolicyAccessView): 60 def resolve_provider_application(self): 61 self.provider = provider 62 self.application = app 63 64 def get(self, *args, **kwargs): 65 return HttpResponse("foo") 66 67 req = self.factory.get("/") 68 req.brand = create_test_brand() 69 with self.assertRaises(Http404): 70 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):
72 @apply_blueprint("default/flow-default-authentication-flow.yaml") 73 def test_pav_unauthenticated_flow_no_acccess(self): 74 """Test simple policy access view (unauthenticated access, 75 authentication flow with policy)""" 76 provider = Provider.objects.create( 77 name=generate_id(), 78 ) 79 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 80 flow = Flow.objects.get(slug="default-authentication-flow") 81 PolicyBinding.objects.create( 82 target=flow, group=Group.objects.create(name=generate_id()), order=0 83 ) 84 85 class TestView(PolicyAccessView): 86 def resolve_provider_application(self): 87 self.provider = provider 88 self.application = app 89 90 def get(self, *args, **kwargs): 91 return HttpResponse("foo") 92 93 req = self.factory.get("/") 94 req.brand = create_test_brand(flow_authentication=flow) 95 with self.assertRaises(Http404): 96 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):
98 @apply_blueprint("default/flow-default-authentication-flow.yaml") 99 def test_pav_unauthenticated_next_param(self): 100 """Test simple policy access view (unauthenticated access, with checking next param)""" 101 provider = Provider.objects.create( 102 name=generate_id(), 103 ) 104 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 105 flow = Flow.objects.get(slug="default-authentication-flow") 106 107 class TestView(PolicyAccessView): 108 def resolve_provider_application(self): 109 self.provider = provider 110 self.application = app 111 112 def get(self, *args, **kwargs): 113 return HttpResponse("foo") 114 115 req = self.factory.get("/") 116 req.brand = create_test_brand(flow_authentication=flow) 117 res = TestView.as_view()(req) 118 self.assertEqual(res.status_code, 302) 119 self.assertEqual(res.url, "/if/flow/default-authentication-flow/?next=%2F")
Test simple policy access view (unauthenticated access, with checking next param)
@apply_blueprint('default/flow-default-authentication-flow.yaml')
def
test_pav_continuous_login_hold_context(self):
121 @apply_blueprint("default/flow-default-authentication-flow.yaml") 122 def test_pav_continuous_login_hold_context(self): 123 """Provider views can release continuous-login tabs after authentication.""" 124 provider = Provider.objects.create(name=generate_id()) 125 app = Application.objects.create(name=generate_id(), slug=generate_id(), provider=provider) 126 flow = Flow.objects.get(slug="default-authentication-flow") 127 128 class TestView(PolicyAccessView): 129 def resolve_provider_application(self): 130 self.provider = provider 131 self.application = app 132 133 def continuous_login_hold_required(self) -> bool: 134 return False 135 136 req = self.factory.get("/") 137 req.brand = create_test_brand(flow_authentication=flow) 138 139 response = TestView.as_view()(req) 140 141 self.assertEqual(response.status_code, 302) 142 self.assertFalse(req.session[SESSION_KEY_PLAN].context[PLAN_CONTEXT_CONTINUOUS_LOGIN_HOLD])
Provider views can release continuous-login tabs after authentication.