authentik.flows.tests.test_stage_binding_policies
Stage binding policy tests
1"""Stage binding policy tests""" 2 3from unittest.mock import MagicMock, patch 4 5from django.db import OperationalError 6from django.urls import reverse 7 8from authentik.blueprints.tests import apply_blueprint 9from authentik.core.tests.utils import create_test_flow, create_test_user 10from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding 11from authentik.flows.tests import APIFlowTestCase 12from authentik.lib.generators import generate_id 13from authentik.policies.expression.models import ExpressionPolicy 14from authentik.policies.models import PolicyBinding 15from authentik.policies.process import PolicyProcess 16from authentik.stages.authenticator_totp.models import TOTPDevice 17from authentik.stages.deny.models import DenyStage 18from authentik.stages.dummy.models import DummyStage 19from authentik.stages.identification.models import IdentificationStage 20from authentik.stages.password.models import PasswordStage 21 22 23class TestStageBindingPolicies(APIFlowTestCase): 24 """Stage binding policy tests""" 25 26 @apply_blueprint("default/flow-default-authentication-flow.yaml") 27 @patch( 28 "authentik.policies.process.cache", 29 MagicMock(set=MagicMock(side_effect=OperationalError())), 30 ) 31 def test_reevaluate_keep_cache_error(self): 32 """Stage is kept when its policy result can't be cached""" 33 flow = Flow.objects.get(slug="default-authentication-flow") 34 user = create_test_user() 35 TOTPDevice.objects.create(user=user, confirmed=True) 36 37 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 38 response = self.client.get(exec_url) 39 self.assertStageResponse(response, component="ak-stage-identification") 40 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 41 self.assertStageResponse(response, component="ak-stage-password") 42 43 @apply_blueprint("default/flow-default-authentication-flow.yaml") 44 @patch( 45 "authentik.policies.process.cache", 46 MagicMock(set=MagicMock(side_effect=OperationalError())), 47 ) 48 def test_reevaluate_keep_cache_error_failure_result_false(self): 49 """Policy result, not the failure result, applies when the result can't be cached""" 50 PolicyBinding.objects.filter( 51 policy__name="default-authentication-flow-password-stage" 52 ).update(failure_result=False) 53 flow = Flow.objects.get(slug="default-authentication-flow") 54 user = create_test_user() 55 TOTPDevice.objects.create(user=user, confirmed=True) 56 57 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 58 response = self.client.get(exec_url) 59 self.assertStageResponse(response, component="ak-stage-identification") 60 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 61 self.assertStageResponse(response, component="ak-stage-password") 62 63 @apply_blueprint("default/flow-default-authentication-flow.yaml") 64 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 65 def test_reevaluate_keep_logging_error(self): 66 """Stage is kept when its policy execution can't be logged""" 67 ExpressionPolicy.objects.filter(name="default-authentication-flow-password-stage").update( 68 execution_logging=True 69 ) 70 flow = Flow.objects.get(slug="default-authentication-flow") 71 user = create_test_user() 72 TOTPDevice.objects.create(user=user, confirmed=True) 73 74 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 75 response = self.client.get(exec_url) 76 self.assertStageResponse(response, component="ak-stage-identification") 77 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 78 self.assertStageResponse(response, component="ak-stage-password") 79 80 @apply_blueprint("default/flow-default-authentication-flow.yaml") 81 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 82 def test_reevaluate_keep_logging_error_failure_result_false(self): 83 """Policy result, not the failure result, applies when the execution can't be logged""" 84 ExpressionPolicy.objects.filter(name="default-authentication-flow-password-stage").update( 85 execution_logging=True 86 ) 87 PolicyBinding.objects.filter( 88 policy__name="default-authentication-flow-password-stage" 89 ).update(failure_result=False) 90 flow = Flow.objects.get(slug="default-authentication-flow") 91 user = create_test_user() 92 TOTPDevice.objects.create(user=user, confirmed=True) 93 94 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 95 response = self.client.get(exec_url) 96 self.assertStageResponse(response, component="ak-stage-identification") 97 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 98 self.assertStageResponse(response, component="ak-stage-password") 99 100 @apply_blueprint("default/flow-default-authentication-flow.yaml") 101 @patch.object(ExpressionPolicy, "passes", MagicMock(side_effect=OperationalError())) 102 def test_reevaluate_keep_policy_error(self): 103 """Stage is kept when its policy can't be evaluated""" 104 flow = Flow.objects.get(slug="default-authentication-flow") 105 user = create_test_user() 106 TOTPDevice.objects.create(user=user, confirmed=True) 107 108 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 109 response = self.client.get(exec_url) 110 self.assertStageResponse(response, component="ak-stage-identification") 111 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 112 self.assertStageResponse(response, component="ak-stage-password") 113 114 @apply_blueprint("default/flow-default-authentication-flow.yaml") 115 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 116 def test_reevaluate_keep_dry_run_error(self): 117 """Stage is kept when its only binding is a dry-run binding that errors""" 118 PolicyBinding.objects.filter( 119 policy__name="default-authentication-flow-password-stage" 120 ).update(dry_run=True, failure_result=False) 121 flow = Flow.objects.get(slug="default-authentication-flow") 122 user = create_test_user() 123 TOTPDevice.objects.create(user=user, confirmed=True) 124 125 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 126 response = self.client.get(exec_url) 127 self.assertStageResponse(response, component="ak-stage-identification") 128 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 129 self.assertStageResponse(response, component="ak-stage-password") 130 131 @patch( 132 "authentik.policies.process.cache", 133 MagicMock(set=MagicMock(side_effect=OperationalError())), 134 ) 135 def test_plan_keep_cache_error(self): 136 """Stage evaluated during planning is kept when its policy result can't be cached""" 137 flow = create_test_flow(FlowDesignation.AUTHORIZATION) 138 deny_binding = FlowStageBinding.objects.create( 139 target=flow, 140 stage=DenyStage.objects.create(name=generate_id()), 141 order=0, 142 evaluate_on_plan=True, 143 re_evaluate_policies=False, 144 ) 145 PolicyBinding.objects.create( 146 target=deny_binding, 147 policy=ExpressionPolicy.objects.create(name=generate_id(), expression="return True"), 148 order=0, 149 failure_result=True, 150 ) 151 FlowStageBinding.objects.create( 152 target=flow, 153 stage=DummyStage.objects.create(name=generate_id()), 154 order=1, 155 ) 156 self.client.force_login(create_test_user()) 157 158 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 159 response = self.client.get(exec_url) 160 self.assertStageResponse(response, component="ak-stage-access-denied") 161 162 @patch( 163 "authentik.policies.process.cache", 164 MagicMock(set=MagicMock(side_effect=OperationalError())), 165 ) 166 def test_plan_keep_cache_error_failure_result_false(self): 167 """Policy result, not the failure result, applies during planning when the result can't 168 be cached""" 169 flow = create_test_flow(FlowDesignation.AUTHORIZATION) 170 deny_binding = FlowStageBinding.objects.create( 171 target=flow, 172 stage=DenyStage.objects.create(name=generate_id()), 173 order=0, 174 evaluate_on_plan=True, 175 re_evaluate_policies=False, 176 ) 177 PolicyBinding.objects.create( 178 target=deny_binding, 179 policy=ExpressionPolicy.objects.create(name=generate_id(), expression="return True"), 180 order=0, 181 failure_result=False, 182 ) 183 FlowStageBinding.objects.create( 184 target=flow, 185 stage=DummyStage.objects.create(name=generate_id()), 186 order=1, 187 ) 188 self.client.force_login(create_test_user()) 189 190 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 191 response = self.client.get(exec_url) 192 self.assertStageResponse(response, component="ak-stage-access-denied") 193 194 @apply_blueprint("default/flow-default-authentication-flow.yaml") 195 def test_reevaluate_remove_authenticated(self): 196 """Password stage is removed once the identification stage checked the password""" 197 IdentificationStage.objects.filter(name="default-authentication-identification").update( 198 password_stage=PasswordStage.objects.get(name="default-authentication-password") 199 ) 200 flow = Flow.objects.get(slug="default-authentication-flow") 201 user = create_test_user() 202 TOTPDevice.objects.create(user=user, confirmed=True) 203 204 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 205 response = self.client.get(exec_url) 206 self.assertStageResponse(response, component="ak-stage-identification") 207 response = self.client.post( 208 exec_url, {"uid_field": user.username, "password": user.username}, follow=True 209 ) 210 self.assertStageResponse(response, component="ak-stage-authenticator-validate")
24class TestStageBindingPolicies(APIFlowTestCase): 25 """Stage binding policy tests""" 26 27 @apply_blueprint("default/flow-default-authentication-flow.yaml") 28 @patch( 29 "authentik.policies.process.cache", 30 MagicMock(set=MagicMock(side_effect=OperationalError())), 31 ) 32 def test_reevaluate_keep_cache_error(self): 33 """Stage is kept when its policy result can't be cached""" 34 flow = Flow.objects.get(slug="default-authentication-flow") 35 user = create_test_user() 36 TOTPDevice.objects.create(user=user, confirmed=True) 37 38 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 39 response = self.client.get(exec_url) 40 self.assertStageResponse(response, component="ak-stage-identification") 41 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 42 self.assertStageResponse(response, component="ak-stage-password") 43 44 @apply_blueprint("default/flow-default-authentication-flow.yaml") 45 @patch( 46 "authentik.policies.process.cache", 47 MagicMock(set=MagicMock(side_effect=OperationalError())), 48 ) 49 def test_reevaluate_keep_cache_error_failure_result_false(self): 50 """Policy result, not the failure result, applies when the result can't be cached""" 51 PolicyBinding.objects.filter( 52 policy__name="default-authentication-flow-password-stage" 53 ).update(failure_result=False) 54 flow = Flow.objects.get(slug="default-authentication-flow") 55 user = create_test_user() 56 TOTPDevice.objects.create(user=user, confirmed=True) 57 58 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 59 response = self.client.get(exec_url) 60 self.assertStageResponse(response, component="ak-stage-identification") 61 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 62 self.assertStageResponse(response, component="ak-stage-password") 63 64 @apply_blueprint("default/flow-default-authentication-flow.yaml") 65 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 66 def test_reevaluate_keep_logging_error(self): 67 """Stage is kept when its policy execution can't be logged""" 68 ExpressionPolicy.objects.filter(name="default-authentication-flow-password-stage").update( 69 execution_logging=True 70 ) 71 flow = Flow.objects.get(slug="default-authentication-flow") 72 user = create_test_user() 73 TOTPDevice.objects.create(user=user, confirmed=True) 74 75 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 76 response = self.client.get(exec_url) 77 self.assertStageResponse(response, component="ak-stage-identification") 78 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 79 self.assertStageResponse(response, component="ak-stage-password") 80 81 @apply_blueprint("default/flow-default-authentication-flow.yaml") 82 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 83 def test_reevaluate_keep_logging_error_failure_result_false(self): 84 """Policy result, not the failure result, applies when the execution can't be logged""" 85 ExpressionPolicy.objects.filter(name="default-authentication-flow-password-stage").update( 86 execution_logging=True 87 ) 88 PolicyBinding.objects.filter( 89 policy__name="default-authentication-flow-password-stage" 90 ).update(failure_result=False) 91 flow = Flow.objects.get(slug="default-authentication-flow") 92 user = create_test_user() 93 TOTPDevice.objects.create(user=user, confirmed=True) 94 95 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 96 response = self.client.get(exec_url) 97 self.assertStageResponse(response, component="ak-stage-identification") 98 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 99 self.assertStageResponse(response, component="ak-stage-password") 100 101 @apply_blueprint("default/flow-default-authentication-flow.yaml") 102 @patch.object(ExpressionPolicy, "passes", MagicMock(side_effect=OperationalError())) 103 def test_reevaluate_keep_policy_error(self): 104 """Stage is kept when its policy can't be evaluated""" 105 flow = Flow.objects.get(slug="default-authentication-flow") 106 user = create_test_user() 107 TOTPDevice.objects.create(user=user, confirmed=True) 108 109 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 110 response = self.client.get(exec_url) 111 self.assertStageResponse(response, component="ak-stage-identification") 112 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 113 self.assertStageResponse(response, component="ak-stage-password") 114 115 @apply_blueprint("default/flow-default-authentication-flow.yaml") 116 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 117 def test_reevaluate_keep_dry_run_error(self): 118 """Stage is kept when its only binding is a dry-run binding that errors""" 119 PolicyBinding.objects.filter( 120 policy__name="default-authentication-flow-password-stage" 121 ).update(dry_run=True, failure_result=False) 122 flow = Flow.objects.get(slug="default-authentication-flow") 123 user = create_test_user() 124 TOTPDevice.objects.create(user=user, confirmed=True) 125 126 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 127 response = self.client.get(exec_url) 128 self.assertStageResponse(response, component="ak-stage-identification") 129 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 130 self.assertStageResponse(response, component="ak-stage-password") 131 132 @patch( 133 "authentik.policies.process.cache", 134 MagicMock(set=MagicMock(side_effect=OperationalError())), 135 ) 136 def test_plan_keep_cache_error(self): 137 """Stage evaluated during planning is kept when its policy result can't be cached""" 138 flow = create_test_flow(FlowDesignation.AUTHORIZATION) 139 deny_binding = FlowStageBinding.objects.create( 140 target=flow, 141 stage=DenyStage.objects.create(name=generate_id()), 142 order=0, 143 evaluate_on_plan=True, 144 re_evaluate_policies=False, 145 ) 146 PolicyBinding.objects.create( 147 target=deny_binding, 148 policy=ExpressionPolicy.objects.create(name=generate_id(), expression="return True"), 149 order=0, 150 failure_result=True, 151 ) 152 FlowStageBinding.objects.create( 153 target=flow, 154 stage=DummyStage.objects.create(name=generate_id()), 155 order=1, 156 ) 157 self.client.force_login(create_test_user()) 158 159 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 160 response = self.client.get(exec_url) 161 self.assertStageResponse(response, component="ak-stage-access-denied") 162 163 @patch( 164 "authentik.policies.process.cache", 165 MagicMock(set=MagicMock(side_effect=OperationalError())), 166 ) 167 def test_plan_keep_cache_error_failure_result_false(self): 168 """Policy result, not the failure result, applies during planning when the result can't 169 be cached""" 170 flow = create_test_flow(FlowDesignation.AUTHORIZATION) 171 deny_binding = FlowStageBinding.objects.create( 172 target=flow, 173 stage=DenyStage.objects.create(name=generate_id()), 174 order=0, 175 evaluate_on_plan=True, 176 re_evaluate_policies=False, 177 ) 178 PolicyBinding.objects.create( 179 target=deny_binding, 180 policy=ExpressionPolicy.objects.create(name=generate_id(), expression="return True"), 181 order=0, 182 failure_result=False, 183 ) 184 FlowStageBinding.objects.create( 185 target=flow, 186 stage=DummyStage.objects.create(name=generate_id()), 187 order=1, 188 ) 189 self.client.force_login(create_test_user()) 190 191 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 192 response = self.client.get(exec_url) 193 self.assertStageResponse(response, component="ak-stage-access-denied") 194 195 @apply_blueprint("default/flow-default-authentication-flow.yaml") 196 def test_reevaluate_remove_authenticated(self): 197 """Password stage is removed once the identification stage checked the password""" 198 IdentificationStage.objects.filter(name="default-authentication-identification").update( 199 password_stage=PasswordStage.objects.get(name="default-authentication-password") 200 ) 201 flow = Flow.objects.get(slug="default-authentication-flow") 202 user = create_test_user() 203 TOTPDevice.objects.create(user=user, confirmed=True) 204 205 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 206 response = self.client.get(exec_url) 207 self.assertStageResponse(response, component="ak-stage-identification") 208 response = self.client.post( 209 exec_url, {"uid_field": user.username, "password": user.username}, follow=True 210 ) 211 self.assertStageResponse(response, component="ak-stage-authenticator-validate")
Stage binding policy tests
@apply_blueprint('default/flow-default-authentication-flow.yaml')
@patch('authentik.policies.process.cache', MagicMock(set=MagicMock(side_effect=OperationalError())))
def
test_reevaluate_keep_cache_error(self):
27 @apply_blueprint("default/flow-default-authentication-flow.yaml") 28 @patch( 29 "authentik.policies.process.cache", 30 MagicMock(set=MagicMock(side_effect=OperationalError())), 31 ) 32 def test_reevaluate_keep_cache_error(self): 33 """Stage is kept when its policy result can't be cached""" 34 flow = Flow.objects.get(slug="default-authentication-flow") 35 user = create_test_user() 36 TOTPDevice.objects.create(user=user, confirmed=True) 37 38 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 39 response = self.client.get(exec_url) 40 self.assertStageResponse(response, component="ak-stage-identification") 41 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 42 self.assertStageResponse(response, component="ak-stage-password")
Stage is kept when its policy result can't be cached
@apply_blueprint('default/flow-default-authentication-flow.yaml')
@patch('authentik.policies.process.cache', MagicMock(set=MagicMock(side_effect=OperationalError())))
def
test_reevaluate_keep_cache_error_failure_result_false(self):
44 @apply_blueprint("default/flow-default-authentication-flow.yaml") 45 @patch( 46 "authentik.policies.process.cache", 47 MagicMock(set=MagicMock(side_effect=OperationalError())), 48 ) 49 def test_reevaluate_keep_cache_error_failure_result_false(self): 50 """Policy result, not the failure result, applies when the result can't be cached""" 51 PolicyBinding.objects.filter( 52 policy__name="default-authentication-flow-password-stage" 53 ).update(failure_result=False) 54 flow = Flow.objects.get(slug="default-authentication-flow") 55 user = create_test_user() 56 TOTPDevice.objects.create(user=user, confirmed=True) 57 58 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 59 response = self.client.get(exec_url) 60 self.assertStageResponse(response, component="ak-stage-identification") 61 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 62 self.assertStageResponse(response, component="ak-stage-password")
Policy result, not the failure result, applies when the result can't be cached
@apply_blueprint('default/flow-default-authentication-flow.yaml')
@patch.object(PolicyProcess, 'create_event', MagicMock(side_effect=OperationalError()))
def
test_reevaluate_keep_logging_error(self):
64 @apply_blueprint("default/flow-default-authentication-flow.yaml") 65 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 66 def test_reevaluate_keep_logging_error(self): 67 """Stage is kept when its policy execution can't be logged""" 68 ExpressionPolicy.objects.filter(name="default-authentication-flow-password-stage").update( 69 execution_logging=True 70 ) 71 flow = Flow.objects.get(slug="default-authentication-flow") 72 user = create_test_user() 73 TOTPDevice.objects.create(user=user, confirmed=True) 74 75 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 76 response = self.client.get(exec_url) 77 self.assertStageResponse(response, component="ak-stage-identification") 78 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 79 self.assertStageResponse(response, component="ak-stage-password")
Stage is kept when its policy execution can't be logged
@apply_blueprint('default/flow-default-authentication-flow.yaml')
@patch.object(PolicyProcess, 'create_event', MagicMock(side_effect=OperationalError()))
def
test_reevaluate_keep_logging_error_failure_result_false(self):
81 @apply_blueprint("default/flow-default-authentication-flow.yaml") 82 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 83 def test_reevaluate_keep_logging_error_failure_result_false(self): 84 """Policy result, not the failure result, applies when the execution can't be logged""" 85 ExpressionPolicy.objects.filter(name="default-authentication-flow-password-stage").update( 86 execution_logging=True 87 ) 88 PolicyBinding.objects.filter( 89 policy__name="default-authentication-flow-password-stage" 90 ).update(failure_result=False) 91 flow = Flow.objects.get(slug="default-authentication-flow") 92 user = create_test_user() 93 TOTPDevice.objects.create(user=user, confirmed=True) 94 95 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 96 response = self.client.get(exec_url) 97 self.assertStageResponse(response, component="ak-stage-identification") 98 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 99 self.assertStageResponse(response, component="ak-stage-password")
Policy result, not the failure result, applies when the execution can't be logged
@apply_blueprint('default/flow-default-authentication-flow.yaml')
@patch.object(ExpressionPolicy, 'passes', MagicMock(side_effect=OperationalError()))
def
test_reevaluate_keep_policy_error(self):
101 @apply_blueprint("default/flow-default-authentication-flow.yaml") 102 @patch.object(ExpressionPolicy, "passes", MagicMock(side_effect=OperationalError())) 103 def test_reevaluate_keep_policy_error(self): 104 """Stage is kept when its policy can't be evaluated""" 105 flow = Flow.objects.get(slug="default-authentication-flow") 106 user = create_test_user() 107 TOTPDevice.objects.create(user=user, confirmed=True) 108 109 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 110 response = self.client.get(exec_url) 111 self.assertStageResponse(response, component="ak-stage-identification") 112 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 113 self.assertStageResponse(response, component="ak-stage-password")
Stage is kept when its policy can't be evaluated
@apply_blueprint('default/flow-default-authentication-flow.yaml')
@patch.object(PolicyProcess, 'create_event', MagicMock(side_effect=OperationalError()))
def
test_reevaluate_keep_dry_run_error(self):
115 @apply_blueprint("default/flow-default-authentication-flow.yaml") 116 @patch.object(PolicyProcess, "create_event", MagicMock(side_effect=OperationalError())) 117 def test_reevaluate_keep_dry_run_error(self): 118 """Stage is kept when its only binding is a dry-run binding that errors""" 119 PolicyBinding.objects.filter( 120 policy__name="default-authentication-flow-password-stage" 121 ).update(dry_run=True, failure_result=False) 122 flow = Flow.objects.get(slug="default-authentication-flow") 123 user = create_test_user() 124 TOTPDevice.objects.create(user=user, confirmed=True) 125 126 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 127 response = self.client.get(exec_url) 128 self.assertStageResponse(response, component="ak-stage-identification") 129 response = self.client.post(exec_url, {"uid_field": user.username}, follow=True) 130 self.assertStageResponse(response, component="ak-stage-password")
Stage is kept when its only binding is a dry-run binding that errors
@patch('authentik.policies.process.cache', MagicMock(set=MagicMock(side_effect=OperationalError())))
def
test_plan_keep_cache_error(self):
132 @patch( 133 "authentik.policies.process.cache", 134 MagicMock(set=MagicMock(side_effect=OperationalError())), 135 ) 136 def test_plan_keep_cache_error(self): 137 """Stage evaluated during planning is kept when its policy result can't be cached""" 138 flow = create_test_flow(FlowDesignation.AUTHORIZATION) 139 deny_binding = FlowStageBinding.objects.create( 140 target=flow, 141 stage=DenyStage.objects.create(name=generate_id()), 142 order=0, 143 evaluate_on_plan=True, 144 re_evaluate_policies=False, 145 ) 146 PolicyBinding.objects.create( 147 target=deny_binding, 148 policy=ExpressionPolicy.objects.create(name=generate_id(), expression="return True"), 149 order=0, 150 failure_result=True, 151 ) 152 FlowStageBinding.objects.create( 153 target=flow, 154 stage=DummyStage.objects.create(name=generate_id()), 155 order=1, 156 ) 157 self.client.force_login(create_test_user()) 158 159 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 160 response = self.client.get(exec_url) 161 self.assertStageResponse(response, component="ak-stage-access-denied")
Stage evaluated during planning is kept when its policy result can't be cached
@patch('authentik.policies.process.cache', MagicMock(set=MagicMock(side_effect=OperationalError())))
def
test_plan_keep_cache_error_failure_result_false(self):
163 @patch( 164 "authentik.policies.process.cache", 165 MagicMock(set=MagicMock(side_effect=OperationalError())), 166 ) 167 def test_plan_keep_cache_error_failure_result_false(self): 168 """Policy result, not the failure result, applies during planning when the result can't 169 be cached""" 170 flow = create_test_flow(FlowDesignation.AUTHORIZATION) 171 deny_binding = FlowStageBinding.objects.create( 172 target=flow, 173 stage=DenyStage.objects.create(name=generate_id()), 174 order=0, 175 evaluate_on_plan=True, 176 re_evaluate_policies=False, 177 ) 178 PolicyBinding.objects.create( 179 target=deny_binding, 180 policy=ExpressionPolicy.objects.create(name=generate_id(), expression="return True"), 181 order=0, 182 failure_result=False, 183 ) 184 FlowStageBinding.objects.create( 185 target=flow, 186 stage=DummyStage.objects.create(name=generate_id()), 187 order=1, 188 ) 189 self.client.force_login(create_test_user()) 190 191 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 192 response = self.client.get(exec_url) 193 self.assertStageResponse(response, component="ak-stage-access-denied")
Policy result, not the failure result, applies during planning when the result can't be cached
@apply_blueprint('default/flow-default-authentication-flow.yaml')
def
test_reevaluate_remove_authenticated(self):
195 @apply_blueprint("default/flow-default-authentication-flow.yaml") 196 def test_reevaluate_remove_authenticated(self): 197 """Password stage is removed once the identification stage checked the password""" 198 IdentificationStage.objects.filter(name="default-authentication-identification").update( 199 password_stage=PasswordStage.objects.get(name="default-authentication-password") 200 ) 201 flow = Flow.objects.get(slug="default-authentication-flow") 202 user = create_test_user() 203 TOTPDevice.objects.create(user=user, confirmed=True) 204 205 exec_url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) 206 response = self.client.get(exec_url) 207 self.assertStageResponse(response, component="ak-stage-identification") 208 response = self.client.post( 209 exec_url, {"uid_field": user.username, "password": user.username}, follow=True 210 ) 211 self.assertStageResponse(response, component="ak-stage-authenticator-validate")
Password stage is removed once the identification stage checked the password