authentik.flows.tests.test_planner

flow planner tests

  1"""flow planner tests"""
  2
  3from pickle import dumps, loads  # nosec
  4from unittest.mock import MagicMock, Mock, PropertyMock, patch
  5
  6from django.core.cache import cache
  7from django.http import HttpRequest, HttpResponseBadRequest, QueryDict
  8from django.shortcuts import redirect
  9from django.test import TestCase
 10from django.urls import reverse
 11
 12from authentik.blueprints.tests import reconcile_app
 13from authentik.core.models import User
 14from authentik.core.tests.utils import (
 15    RequestFactory,
 16    create_test_admin_user,
 17    create_test_flow,
 18    dummy_get_response,
 19)
 20from authentik.flows.exceptions import EmptyFlowException, FlowNonApplicableException
 21from authentik.flows.markers import ReevaluateMarker, StageMarker
 22from authentik.flows.models import (
 23    FlowAuthenticationRequirement,
 24    FlowDesignation,
 25    FlowStageBinding,
 26    in_memory_stage,
 27)
 28from authentik.flows.planner import (
 29    PLAN_CONTEXT_IS_REDIRECTED,
 30    PLAN_CONTEXT_IS_RESTORED,
 31    PLAN_CONTEXT_PENDING_USER,
 32    FlowPlan,
 33    FlowPlanner,
 34    cache_key,
 35)
 36from authentik.flows.stage import StageView
 37from authentik.flows.views.executor import SESSION_KEY_GET, SESSION_KEY_PLAN
 38from authentik.lib.generators import generate_id
 39from authentik.outposts.apps import MANAGED_OUTPOST
 40from authentik.outposts.models import Outpost
 41from authentik.policies.dummy.models import DummyPolicy
 42from authentik.policies.models import PolicyBinding
 43from authentik.policies.types import PolicyResult
 44from authentik.root.middleware import ClientIPMiddleware
 45from authentik.stages.dummy.models import DummyStage
 46
 47POLICY_RETURN_FALSE = PropertyMock(return_value=PolicyResult(False))
 48CACHE_MOCK = Mock(wraps=cache)
 49
 50POLICY_RETURN_TRUE = MagicMock(return_value=PolicyResult(True))
 51
 52
 53class TestFlowPlanner(TestCase):
 54    """Test planner logic"""
 55
 56    def setUp(self):
 57        self.request_factory = RequestFactory()
 58
 59    def test_empty_plan(self):
 60        """Test that empty plan raises exception"""
 61        flow = create_test_flow()
 62        request = self.request_factory.get(
 63            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 64        )
 65
 66        with self.assertRaises(EmptyFlowException):
 67            planner = FlowPlanner(flow)
 68            planner.plan(request)
 69
 70    def test_authentication(self):
 71        """Test flow authentication"""
 72        flow = create_test_flow()
 73        flow.authentication = FlowAuthenticationRequirement.NONE
 74        request = self.request_factory.get(
 75            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 76        )
 77        planner = FlowPlanner(flow)
 78        planner.allow_empty_flows = True
 79        planner.plan(request)
 80
 81        with self.assertRaises(FlowNonApplicableException):
 82            flow.authentication = FlowAuthenticationRequirement.REQUIRE_AUTHENTICATED
 83            FlowPlanner(flow).plan(request)
 84        with self.assertRaises(FlowNonApplicableException):
 85            flow.authentication = FlowAuthenticationRequirement.REQUIRE_SUPERUSER
 86            FlowPlanner(flow).plan(request)
 87
 88        request.user = create_test_admin_user()
 89        planner = FlowPlanner(flow)
 90        planner.allow_empty_flows = True
 91        planner.plan(request)
 92
 93    def test_authentication_redirect_required(self):
 94        """Test flow authentication (redirect required)"""
 95        flow = create_test_flow()
 96        flow.authentication = FlowAuthenticationRequirement.REQUIRE_REDIRECT
 97        request = self.request_factory.get(
 98            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 99        )
100        planner = FlowPlanner(flow)
101        planner.allow_empty_flows = True
102
103        with self.assertRaises(FlowNonApplicableException):
104            planner.plan(request)
105
106        context = {}
107        context[PLAN_CONTEXT_IS_REDIRECTED] = create_test_flow()
108        planner.plan(request, context)
109
110    @reconcile_app("authentik_outposts")
111    def test_authentication_outpost(self):
112        """Test flow authentication (outpost)"""
113        flow = create_test_flow()
114        flow.authentication = FlowAuthenticationRequirement.REQUIRE_OUTPOST
115        request = self.request_factory.get(
116            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
117        )
118        with self.assertRaises(FlowNonApplicableException):
119            planner = FlowPlanner(flow)
120            planner.allow_empty_flows = True
121            planner.plan(request)
122
123        outpost = Outpost.objects.filter(managed=MANAGED_OUTPOST).first()
124        request = self.request_factory.get(
125            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
126            HTTP_X_AUTHENTIK_OUTPOST_TOKEN=outpost.token.key,
127            HTTP_X_AUTHENTIK_REMOTE_IP="1.2.3.4",
128        )
129        middleware = ClientIPMiddleware(dummy_get_response)
130        middleware(request)
131
132        planner = FlowPlanner(flow)
133        planner.allow_empty_flows = True
134        planner.plan(request)
135
136    def test_authentication_require_token(self):
137        """Test flow authentication (require_token)"""
138        flow = create_test_flow()
139        flow.authentication = FlowAuthenticationRequirement.REQUIRE_TOKEN
140        request = self.request_factory.get(
141            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
142        )
143        planner = FlowPlanner(flow)
144        planner.allow_empty_flows = True
145
146        with self.assertRaises(FlowNonApplicableException):
147            planner.plan(request)
148
149        context = {PLAN_CONTEXT_IS_RESTORED: True}
150        planner.plan(request, context)
151
152    @patch(
153        "authentik.policies.engine.PolicyEngine.result",
154        POLICY_RETURN_FALSE,
155    )
156    def test_non_applicable_plan(self):
157        """Test that empty plan raises exception"""
158        flow = create_test_flow()
159        request = self.request_factory.get(
160            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
161        )
162
163        with self.assertRaises(FlowNonApplicableException):
164            planner = FlowPlanner(flow)
165            planner.plan(request)
166
167    @patch("authentik.flows.planner.cache", CACHE_MOCK)
168    def test_planner_cache(self):
169        """Test planner cache"""
170        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
171        FlowStageBinding.objects.create(
172            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
173        )
174        request = self.request_factory.get(
175            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
176        )
177
178        planner = FlowPlanner(flow)
179        planner.plan(request)
180        self.assertEqual(CACHE_MOCK.set.call_count, 1)  # Ensure plan is written to cache
181        planner = FlowPlanner(flow)
182        planner.plan(request)
183        self.assertEqual(CACHE_MOCK.set.call_count, 1)  # Ensure nothing is written to cache
184        self.assertEqual(CACHE_MOCK.get.call_count, 2)  # Get is called twice
185
186    def test_planner_stage_configuration_not_cached(self):
187        """Test that stage configuration plans, which are never read from the cache,
188        aren't written to it"""
189        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
190        FlowStageBinding.objects.create(
191            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
192        )
193        request = self.request_factory.get(
194            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
195        )
196        FlowPlanner(flow).plan(request)
197        self.assertEqual(cache.keys(f"{cache_key(flow)}*"), [])
198
199    def test_planner_stage_cached_on_binding(self):
200        """Test that the plan carries subclassed stages, so the executor doesn't re-query them"""
201        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
202        stage = DummyStage.objects.create(name=generate_id())
203        FlowStageBinding.objects.create(target=flow, stage=stage, order=0)
204        request = self.request_factory.get(
205            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
206        )
207        plan = loads(dumps(FlowPlanner(flow).plan(request)))  # nosec
208        with self.assertNumQueries(0):
209            self.assertIsInstance(plan.bindings[0].stage, DummyStage)
210
211    def test_planner_default_context(self):
212        """Test planner with default_context"""
213        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
214        FlowStageBinding.objects.create(
215            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
216        )
217
218        user = User.objects.create(username="test-user")
219        request = self.request_factory.get(
220            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
221        )
222        request.user = user
223        planner = FlowPlanner(flow)
224        planner.plan(request, default_context={PLAN_CONTEXT_PENDING_USER: user})
225        key = cache_key(flow, user)
226        self.assertTrue(cache.get(key) is not None)
227
228    def test_dry_run_policy_does_not_block_plan(self):
229        """A failing dry-run policy does not block flow planning."""
230        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
231        stage_binding = FlowStageBinding.objects.create(
232            target=flow,
233            stage=DummyStage.objects.create(name=generate_id()),
234            order=0,
235        )
236        policy = DummyPolicy.objects.create(
237            name=generate_id(), result=False, wait_min=0, wait_max=1
238        )
239        PolicyBinding.objects.create(target=flow, policy=policy, order=0, dry_run=True)
240        request = self.request_factory.get(
241            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
242        )
243
244        plan = FlowPlanner(flow).plan(request)
245
246        self.assertEqual(plan.bindings, [stage_binding])
247
248    def test_planner_marker_reevaluate(self):
249        """Test that the planner creates the proper marker"""
250        flow = create_test_flow()
251
252        FlowStageBinding.objects.create(
253            target=flow,
254            stage=DummyStage.objects.create(name=generate_id()),
255            order=0,
256            re_evaluate_policies=True,
257        )
258
259        request = self.request_factory.get(
260            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
261        )
262
263        planner = FlowPlanner(flow)
264        plan = planner.plan(request)
265
266        self.assertEqual(plan.markers[0].__class__, ReevaluateMarker)
267
268    def test_planner_reevaluate_actual(self):
269        """Test planner with re-evaluate"""
270        flow = create_test_flow()
271        false_policy = DummyPolicy.objects.create(result=False, wait_min=1, wait_max=2)
272
273        binding = FlowStageBinding.objects.create(
274            target=flow,
275            stage=DummyStage.objects.create(name=generate_id()),
276            order=0,
277            re_evaluate_policies=False,
278        )
279        binding2 = FlowStageBinding.objects.create(
280            target=flow,
281            stage=DummyStage.objects.create(name=generate_id()),
282            order=1,
283            re_evaluate_policies=True,
284        )
285
286        PolicyBinding.objects.create(policy=false_policy, target=binding2, order=0)
287
288        request = self.request_factory.get(
289            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
290        )
291
292        # Here we patch the dummy policy to evaluate to true so the stage is included
293        with patch("authentik.policies.dummy.models.DummyPolicy.passes", POLICY_RETURN_TRUE):
294            planner = FlowPlanner(flow)
295            plan = planner.plan(request)
296
297            self.assertEqual(plan.bindings[0], binding)
298            self.assertEqual(plan.bindings[1], binding2)
299
300            self.assertEqual(plan.markers[0].__class__, StageMarker)
301            self.assertEqual(plan.markers[1].__class__, ReevaluateMarker)
302            self.assertIsInstance(plan.markers[0], StageMarker)
303            self.assertIsInstance(plan.markers[1], ReevaluateMarker)
304
305    def test_to_redirect(self):
306        """Test to_redirect and skipping the flow executor"""
307        flow = create_test_flow()
308        flow.authentication = FlowAuthenticationRequirement.NONE
309        request = self.request_factory.get(
310            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
311        )
312
313        planner = FlowPlanner(flow)
314        planner.allow_empty_flows = True
315        plan = planner.plan(request)
316        self.assertTrue(plan.requires_flow_executor())
317        self.assertEqual(
318            plan.to_redirect(request, flow).url,
319            reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug}),
320        )
321
322    def test_to_redirect_skip_simple(self):
323        """Test to_redirect and skipping the flow executor"""
324        flow = create_test_flow()
325        flow.authentication = FlowAuthenticationRequirement.NONE
326        request = self.request_factory.get(
327            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
328        )
329        planner = FlowPlanner(flow)
330        planner.allow_empty_flows = True
331        plan = planner.plan(request)
332
333        class TStageView(StageView):
334            def dispatch(self, request: HttpRequest, *args, **kwargs):
335                return redirect("https://authentik.company")
336
337        plan.append_stage(in_memory_stage(TStageView))
338        self.assertFalse(plan.requires_flow_executor(allowed_silent_types=[TStageView]))
339        self.assertEqual(
340            plan.to_redirect(request, flow, allowed_silent_types=[TStageView]).url,
341            "https://authentik.company",
342        )
343
344    def test_to_redirect_skip_keeps_session_plan(self):
345        """Direct execution leaves the session's flow state to the flow that owns it, even when
346        the direct stage completes or cancels its plan."""
347        flow = create_test_flow()
348        flow.authentication = FlowAuthenticationRequirement.NONE
349        other_plan = FlowPlan(flow_pk=create_test_flow().pk.hex)
350        other_get = QueryDict("next=%2Fother%2F")
351
352        for completion in ["stage_ok", "cancel"]:
353            with self.subTest(completion=completion):
354                request = self.request_factory.get(
355                    reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
356                )
357                request.session[SESSION_KEY_PLAN] = other_plan
358                request.session[SESSION_KEY_GET] = other_get
359                planner = FlowPlanner(flow)
360                planner.allow_empty_flows = True
361                plan = planner.plan(request)
362
363                class TStageView(StageView):
364                    completion_method = completion
365
366                    def dispatch(self, request: HttpRequest, *args, **kwargs):
367                        getattr(self.executor, self.completion_method)()
368                        return HttpResponseBadRequest()
369
370                plan.append_stage(in_memory_stage(TStageView))
371
372                response = plan.to_redirect(request, flow, allowed_silent_types=[TStageView])
373
374                self.assertEqual(response.status_code, 400)
375                self.assertIs(request.session[SESSION_KEY_PLAN], other_plan)
376                self.assertEqual(request.session[SESSION_KEY_GET], other_get)
377
378    def test_to_redirect_skip_stage(self):
379        """Test to_redirect and skipping the flow executor
380        (with a stage bound that cannot be skipped)"""
381        flow = create_test_flow()
382        flow.authentication = FlowAuthenticationRequirement.NONE
383
384        FlowStageBinding.objects.create(
385            target=flow, stage=DummyStage.objects.create(name="dummy"), order=0
386        )
387
388        request = self.request_factory.get(
389            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
390        )
391        planner = FlowPlanner(flow)
392        planner.allow_empty_flows = True
393        plan = planner.plan(request)
394
395        class TStageView(StageView):
396            def dispatch(self, request: HttpRequest, *args, **kwargs):
397                return redirect("https://authentik.company")
398
399        plan.append_stage(in_memory_stage(TStageView))
400        self.assertTrue(plan.requires_flow_executor(allowed_silent_types=[TStageView]))
401
402    def test_to_redirect_skip_policies(self):
403        """Test to_redirect and skipping the flow executor
404        (with a marker on the stage view type that can be skipped)
405
406        Note that this is not actually used anywhere in the code, all stages that are dynamically
407        added are statically added"""
408        flow = create_test_flow()
409        flow.authentication = FlowAuthenticationRequirement.NONE
410
411        request = self.request_factory.get(
412            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
413        )
414        planner = FlowPlanner(flow)
415        planner.allow_empty_flows = True
416        plan = planner.plan(request)
417
418        class TStageView(StageView):
419            def dispatch(self, request: HttpRequest, *args, **kwargs):
420                return redirect("https://authentik.company")
421
422        plan.append_stage(in_memory_stage(TStageView), ReevaluateMarker(None))
423        self.assertTrue(plan.requires_flow_executor(allowed_silent_types=[TStageView]))
POLICY_RETURN_FALSE

A mock intended to be used as a property, or other descriptor, on a class. PropertyMock provides __get__ and __set__ methods so you can specify a return value when it is fetched.

Fetching a PropertyMock instance from an object calls the mock, with no args. Setting it calls the mock with the value being set.

CACHE_MOCK = <Mock id='123519884924576'>
POLICY_RETURN_TRUE = <MagicMock id='123519884924912'>
class TestFlowPlanner(django.test.testcases.TestCase):
 54class TestFlowPlanner(TestCase):
 55    """Test planner logic"""
 56
 57    def setUp(self):
 58        self.request_factory = RequestFactory()
 59
 60    def test_empty_plan(self):
 61        """Test that empty plan raises exception"""
 62        flow = create_test_flow()
 63        request = self.request_factory.get(
 64            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 65        )
 66
 67        with self.assertRaises(EmptyFlowException):
 68            planner = FlowPlanner(flow)
 69            planner.plan(request)
 70
 71    def test_authentication(self):
 72        """Test flow authentication"""
 73        flow = create_test_flow()
 74        flow.authentication = FlowAuthenticationRequirement.NONE
 75        request = self.request_factory.get(
 76            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 77        )
 78        planner = FlowPlanner(flow)
 79        planner.allow_empty_flows = True
 80        planner.plan(request)
 81
 82        with self.assertRaises(FlowNonApplicableException):
 83            flow.authentication = FlowAuthenticationRequirement.REQUIRE_AUTHENTICATED
 84            FlowPlanner(flow).plan(request)
 85        with self.assertRaises(FlowNonApplicableException):
 86            flow.authentication = FlowAuthenticationRequirement.REQUIRE_SUPERUSER
 87            FlowPlanner(flow).plan(request)
 88
 89        request.user = create_test_admin_user()
 90        planner = FlowPlanner(flow)
 91        planner.allow_empty_flows = True
 92        planner.plan(request)
 93
 94    def test_authentication_redirect_required(self):
 95        """Test flow authentication (redirect required)"""
 96        flow = create_test_flow()
 97        flow.authentication = FlowAuthenticationRequirement.REQUIRE_REDIRECT
 98        request = self.request_factory.get(
 99            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
100        )
101        planner = FlowPlanner(flow)
102        planner.allow_empty_flows = True
103
104        with self.assertRaises(FlowNonApplicableException):
105            planner.plan(request)
106
107        context = {}
108        context[PLAN_CONTEXT_IS_REDIRECTED] = create_test_flow()
109        planner.plan(request, context)
110
111    @reconcile_app("authentik_outposts")
112    def test_authentication_outpost(self):
113        """Test flow authentication (outpost)"""
114        flow = create_test_flow()
115        flow.authentication = FlowAuthenticationRequirement.REQUIRE_OUTPOST
116        request = self.request_factory.get(
117            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
118        )
119        with self.assertRaises(FlowNonApplicableException):
120            planner = FlowPlanner(flow)
121            planner.allow_empty_flows = True
122            planner.plan(request)
123
124        outpost = Outpost.objects.filter(managed=MANAGED_OUTPOST).first()
125        request = self.request_factory.get(
126            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
127            HTTP_X_AUTHENTIK_OUTPOST_TOKEN=outpost.token.key,
128            HTTP_X_AUTHENTIK_REMOTE_IP="1.2.3.4",
129        )
130        middleware = ClientIPMiddleware(dummy_get_response)
131        middleware(request)
132
133        planner = FlowPlanner(flow)
134        planner.allow_empty_flows = True
135        planner.plan(request)
136
137    def test_authentication_require_token(self):
138        """Test flow authentication (require_token)"""
139        flow = create_test_flow()
140        flow.authentication = FlowAuthenticationRequirement.REQUIRE_TOKEN
141        request = self.request_factory.get(
142            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
143        )
144        planner = FlowPlanner(flow)
145        planner.allow_empty_flows = True
146
147        with self.assertRaises(FlowNonApplicableException):
148            planner.plan(request)
149
150        context = {PLAN_CONTEXT_IS_RESTORED: True}
151        planner.plan(request, context)
152
153    @patch(
154        "authentik.policies.engine.PolicyEngine.result",
155        POLICY_RETURN_FALSE,
156    )
157    def test_non_applicable_plan(self):
158        """Test that empty plan raises exception"""
159        flow = create_test_flow()
160        request = self.request_factory.get(
161            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
162        )
163
164        with self.assertRaises(FlowNonApplicableException):
165            planner = FlowPlanner(flow)
166            planner.plan(request)
167
168    @patch("authentik.flows.planner.cache", CACHE_MOCK)
169    def test_planner_cache(self):
170        """Test planner cache"""
171        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
172        FlowStageBinding.objects.create(
173            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
174        )
175        request = self.request_factory.get(
176            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
177        )
178
179        planner = FlowPlanner(flow)
180        planner.plan(request)
181        self.assertEqual(CACHE_MOCK.set.call_count, 1)  # Ensure plan is written to cache
182        planner = FlowPlanner(flow)
183        planner.plan(request)
184        self.assertEqual(CACHE_MOCK.set.call_count, 1)  # Ensure nothing is written to cache
185        self.assertEqual(CACHE_MOCK.get.call_count, 2)  # Get is called twice
186
187    def test_planner_stage_configuration_not_cached(self):
188        """Test that stage configuration plans, which are never read from the cache,
189        aren't written to it"""
190        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
191        FlowStageBinding.objects.create(
192            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
193        )
194        request = self.request_factory.get(
195            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
196        )
197        FlowPlanner(flow).plan(request)
198        self.assertEqual(cache.keys(f"{cache_key(flow)}*"), [])
199
200    def test_planner_stage_cached_on_binding(self):
201        """Test that the plan carries subclassed stages, so the executor doesn't re-query them"""
202        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
203        stage = DummyStage.objects.create(name=generate_id())
204        FlowStageBinding.objects.create(target=flow, stage=stage, order=0)
205        request = self.request_factory.get(
206            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
207        )
208        plan = loads(dumps(FlowPlanner(flow).plan(request)))  # nosec
209        with self.assertNumQueries(0):
210            self.assertIsInstance(plan.bindings[0].stage, DummyStage)
211
212    def test_planner_default_context(self):
213        """Test planner with default_context"""
214        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
215        FlowStageBinding.objects.create(
216            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
217        )
218
219        user = User.objects.create(username="test-user")
220        request = self.request_factory.get(
221            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
222        )
223        request.user = user
224        planner = FlowPlanner(flow)
225        planner.plan(request, default_context={PLAN_CONTEXT_PENDING_USER: user})
226        key = cache_key(flow, user)
227        self.assertTrue(cache.get(key) is not None)
228
229    def test_dry_run_policy_does_not_block_plan(self):
230        """A failing dry-run policy does not block flow planning."""
231        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
232        stage_binding = FlowStageBinding.objects.create(
233            target=flow,
234            stage=DummyStage.objects.create(name=generate_id()),
235            order=0,
236        )
237        policy = DummyPolicy.objects.create(
238            name=generate_id(), result=False, wait_min=0, wait_max=1
239        )
240        PolicyBinding.objects.create(target=flow, policy=policy, order=0, dry_run=True)
241        request = self.request_factory.get(
242            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
243        )
244
245        plan = FlowPlanner(flow).plan(request)
246
247        self.assertEqual(plan.bindings, [stage_binding])
248
249    def test_planner_marker_reevaluate(self):
250        """Test that the planner creates the proper marker"""
251        flow = create_test_flow()
252
253        FlowStageBinding.objects.create(
254            target=flow,
255            stage=DummyStage.objects.create(name=generate_id()),
256            order=0,
257            re_evaluate_policies=True,
258        )
259
260        request = self.request_factory.get(
261            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
262        )
263
264        planner = FlowPlanner(flow)
265        plan = planner.plan(request)
266
267        self.assertEqual(plan.markers[0].__class__, ReevaluateMarker)
268
269    def test_planner_reevaluate_actual(self):
270        """Test planner with re-evaluate"""
271        flow = create_test_flow()
272        false_policy = DummyPolicy.objects.create(result=False, wait_min=1, wait_max=2)
273
274        binding = FlowStageBinding.objects.create(
275            target=flow,
276            stage=DummyStage.objects.create(name=generate_id()),
277            order=0,
278            re_evaluate_policies=False,
279        )
280        binding2 = FlowStageBinding.objects.create(
281            target=flow,
282            stage=DummyStage.objects.create(name=generate_id()),
283            order=1,
284            re_evaluate_policies=True,
285        )
286
287        PolicyBinding.objects.create(policy=false_policy, target=binding2, order=0)
288
289        request = self.request_factory.get(
290            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
291        )
292
293        # Here we patch the dummy policy to evaluate to true so the stage is included
294        with patch("authentik.policies.dummy.models.DummyPolicy.passes", POLICY_RETURN_TRUE):
295            planner = FlowPlanner(flow)
296            plan = planner.plan(request)
297
298            self.assertEqual(plan.bindings[0], binding)
299            self.assertEqual(plan.bindings[1], binding2)
300
301            self.assertEqual(plan.markers[0].__class__, StageMarker)
302            self.assertEqual(plan.markers[1].__class__, ReevaluateMarker)
303            self.assertIsInstance(plan.markers[0], StageMarker)
304            self.assertIsInstance(plan.markers[1], ReevaluateMarker)
305
306    def test_to_redirect(self):
307        """Test to_redirect and skipping the flow executor"""
308        flow = create_test_flow()
309        flow.authentication = FlowAuthenticationRequirement.NONE
310        request = self.request_factory.get(
311            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
312        )
313
314        planner = FlowPlanner(flow)
315        planner.allow_empty_flows = True
316        plan = planner.plan(request)
317        self.assertTrue(plan.requires_flow_executor())
318        self.assertEqual(
319            plan.to_redirect(request, flow).url,
320            reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug}),
321        )
322
323    def test_to_redirect_skip_simple(self):
324        """Test to_redirect and skipping the flow executor"""
325        flow = create_test_flow()
326        flow.authentication = FlowAuthenticationRequirement.NONE
327        request = self.request_factory.get(
328            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
329        )
330        planner = FlowPlanner(flow)
331        planner.allow_empty_flows = True
332        plan = planner.plan(request)
333
334        class TStageView(StageView):
335            def dispatch(self, request: HttpRequest, *args, **kwargs):
336                return redirect("https://authentik.company")
337
338        plan.append_stage(in_memory_stage(TStageView))
339        self.assertFalse(plan.requires_flow_executor(allowed_silent_types=[TStageView]))
340        self.assertEqual(
341            plan.to_redirect(request, flow, allowed_silent_types=[TStageView]).url,
342            "https://authentik.company",
343        )
344
345    def test_to_redirect_skip_keeps_session_plan(self):
346        """Direct execution leaves the session's flow state to the flow that owns it, even when
347        the direct stage completes or cancels its plan."""
348        flow = create_test_flow()
349        flow.authentication = FlowAuthenticationRequirement.NONE
350        other_plan = FlowPlan(flow_pk=create_test_flow().pk.hex)
351        other_get = QueryDict("next=%2Fother%2F")
352
353        for completion in ["stage_ok", "cancel"]:
354            with self.subTest(completion=completion):
355                request = self.request_factory.get(
356                    reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
357                )
358                request.session[SESSION_KEY_PLAN] = other_plan
359                request.session[SESSION_KEY_GET] = other_get
360                planner = FlowPlanner(flow)
361                planner.allow_empty_flows = True
362                plan = planner.plan(request)
363
364                class TStageView(StageView):
365                    completion_method = completion
366
367                    def dispatch(self, request: HttpRequest, *args, **kwargs):
368                        getattr(self.executor, self.completion_method)()
369                        return HttpResponseBadRequest()
370
371                plan.append_stage(in_memory_stage(TStageView))
372
373                response = plan.to_redirect(request, flow, allowed_silent_types=[TStageView])
374
375                self.assertEqual(response.status_code, 400)
376                self.assertIs(request.session[SESSION_KEY_PLAN], other_plan)
377                self.assertEqual(request.session[SESSION_KEY_GET], other_get)
378
379    def test_to_redirect_skip_stage(self):
380        """Test to_redirect and skipping the flow executor
381        (with a stage bound that cannot be skipped)"""
382        flow = create_test_flow()
383        flow.authentication = FlowAuthenticationRequirement.NONE
384
385        FlowStageBinding.objects.create(
386            target=flow, stage=DummyStage.objects.create(name="dummy"), order=0
387        )
388
389        request = self.request_factory.get(
390            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
391        )
392        planner = FlowPlanner(flow)
393        planner.allow_empty_flows = True
394        plan = planner.plan(request)
395
396        class TStageView(StageView):
397            def dispatch(self, request: HttpRequest, *args, **kwargs):
398                return redirect("https://authentik.company")
399
400        plan.append_stage(in_memory_stage(TStageView))
401        self.assertTrue(plan.requires_flow_executor(allowed_silent_types=[TStageView]))
402
403    def test_to_redirect_skip_policies(self):
404        """Test to_redirect and skipping the flow executor
405        (with a marker on the stage view type that can be skipped)
406
407        Note that this is not actually used anywhere in the code, all stages that are dynamically
408        added are statically added"""
409        flow = create_test_flow()
410        flow.authentication = FlowAuthenticationRequirement.NONE
411
412        request = self.request_factory.get(
413            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
414        )
415        planner = FlowPlanner(flow)
416        planner.allow_empty_flows = True
417        plan = planner.plan(request)
418
419        class TStageView(StageView):
420            def dispatch(self, request: HttpRequest, *args, **kwargs):
421                return redirect("https://authentik.company")
422
423        plan.append_stage(in_memory_stage(TStageView), ReevaluateMarker(None))
424        self.assertTrue(plan.requires_flow_executor(allowed_silent_types=[TStageView]))

Test planner logic

def setUp(self):
57    def setUp(self):
58        self.request_factory = RequestFactory()

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

def test_empty_plan(self):
60    def test_empty_plan(self):
61        """Test that empty plan raises exception"""
62        flow = create_test_flow()
63        request = self.request_factory.get(
64            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
65        )
66
67        with self.assertRaises(EmptyFlowException):
68            planner = FlowPlanner(flow)
69            planner.plan(request)

Test that empty plan raises exception

def test_authentication(self):
71    def test_authentication(self):
72        """Test flow authentication"""
73        flow = create_test_flow()
74        flow.authentication = FlowAuthenticationRequirement.NONE
75        request = self.request_factory.get(
76            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
77        )
78        planner = FlowPlanner(flow)
79        planner.allow_empty_flows = True
80        planner.plan(request)
81
82        with self.assertRaises(FlowNonApplicableException):
83            flow.authentication = FlowAuthenticationRequirement.REQUIRE_AUTHENTICATED
84            FlowPlanner(flow).plan(request)
85        with self.assertRaises(FlowNonApplicableException):
86            flow.authentication = FlowAuthenticationRequirement.REQUIRE_SUPERUSER
87            FlowPlanner(flow).plan(request)
88
89        request.user = create_test_admin_user()
90        planner = FlowPlanner(flow)
91        planner.allow_empty_flows = True
92        planner.plan(request)

Test flow authentication

def test_authentication_redirect_required(self):
 94    def test_authentication_redirect_required(self):
 95        """Test flow authentication (redirect required)"""
 96        flow = create_test_flow()
 97        flow.authentication = FlowAuthenticationRequirement.REQUIRE_REDIRECT
 98        request = self.request_factory.get(
 99            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
100        )
101        planner = FlowPlanner(flow)
102        planner.allow_empty_flows = True
103
104        with self.assertRaises(FlowNonApplicableException):
105            planner.plan(request)
106
107        context = {}
108        context[PLAN_CONTEXT_IS_REDIRECTED] = create_test_flow()
109        planner.plan(request, context)

Test flow authentication (redirect required)

@reconcile_app('authentik_outposts')
def test_authentication_outpost(self):
111    @reconcile_app("authentik_outposts")
112    def test_authentication_outpost(self):
113        """Test flow authentication (outpost)"""
114        flow = create_test_flow()
115        flow.authentication = FlowAuthenticationRequirement.REQUIRE_OUTPOST
116        request = self.request_factory.get(
117            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
118        )
119        with self.assertRaises(FlowNonApplicableException):
120            planner = FlowPlanner(flow)
121            planner.allow_empty_flows = True
122            planner.plan(request)
123
124        outpost = Outpost.objects.filter(managed=MANAGED_OUTPOST).first()
125        request = self.request_factory.get(
126            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
127            HTTP_X_AUTHENTIK_OUTPOST_TOKEN=outpost.token.key,
128            HTTP_X_AUTHENTIK_REMOTE_IP="1.2.3.4",
129        )
130        middleware = ClientIPMiddleware(dummy_get_response)
131        middleware(request)
132
133        planner = FlowPlanner(flow)
134        planner.allow_empty_flows = True
135        planner.plan(request)

Test flow authentication (outpost)

def test_authentication_require_token(self):
137    def test_authentication_require_token(self):
138        """Test flow authentication (require_token)"""
139        flow = create_test_flow()
140        flow.authentication = FlowAuthenticationRequirement.REQUIRE_TOKEN
141        request = self.request_factory.get(
142            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
143        )
144        planner = FlowPlanner(flow)
145        planner.allow_empty_flows = True
146
147        with self.assertRaises(FlowNonApplicableException):
148            planner.plan(request)
149
150        context = {PLAN_CONTEXT_IS_RESTORED: True}
151        planner.plan(request, context)

Test flow authentication (require_token)

@patch('authentik.policies.engine.PolicyEngine.result', POLICY_RETURN_FALSE)
def test_non_applicable_plan(self):
153    @patch(
154        "authentik.policies.engine.PolicyEngine.result",
155        POLICY_RETURN_FALSE,
156    )
157    def test_non_applicable_plan(self):
158        """Test that empty plan raises exception"""
159        flow = create_test_flow()
160        request = self.request_factory.get(
161            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
162        )
163
164        with self.assertRaises(FlowNonApplicableException):
165            planner = FlowPlanner(flow)
166            planner.plan(request)

Test that empty plan raises exception

@patch('authentik.flows.planner.cache', CACHE_MOCK)
def test_planner_cache(self):
168    @patch("authentik.flows.planner.cache", CACHE_MOCK)
169    def test_planner_cache(self):
170        """Test planner cache"""
171        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
172        FlowStageBinding.objects.create(
173            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
174        )
175        request = self.request_factory.get(
176            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
177        )
178
179        planner = FlowPlanner(flow)
180        planner.plan(request)
181        self.assertEqual(CACHE_MOCK.set.call_count, 1)  # Ensure plan is written to cache
182        planner = FlowPlanner(flow)
183        planner.plan(request)
184        self.assertEqual(CACHE_MOCK.set.call_count, 1)  # Ensure nothing is written to cache
185        self.assertEqual(CACHE_MOCK.get.call_count, 2)  # Get is called twice

Test planner cache

def test_planner_stage_configuration_not_cached(self):
187    def test_planner_stage_configuration_not_cached(self):
188        """Test that stage configuration plans, which are never read from the cache,
189        aren't written to it"""
190        flow = create_test_flow(FlowDesignation.STAGE_CONFIGURATION)
191        FlowStageBinding.objects.create(
192            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
193        )
194        request = self.request_factory.get(
195            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
196        )
197        FlowPlanner(flow).plan(request)
198        self.assertEqual(cache.keys(f"{cache_key(flow)}*"), [])

Test that stage configuration plans, which are never read from the cache, aren't written to it

def test_planner_stage_cached_on_binding(self):
200    def test_planner_stage_cached_on_binding(self):
201        """Test that the plan carries subclassed stages, so the executor doesn't re-query them"""
202        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
203        stage = DummyStage.objects.create(name=generate_id())
204        FlowStageBinding.objects.create(target=flow, stage=stage, order=0)
205        request = self.request_factory.get(
206            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
207        )
208        plan = loads(dumps(FlowPlanner(flow).plan(request)))  # nosec
209        with self.assertNumQueries(0):
210            self.assertIsInstance(plan.bindings[0].stage, DummyStage)

Test that the plan carries subclassed stages, so the executor doesn't re-query them

def test_planner_default_context(self):
212    def test_planner_default_context(self):
213        """Test planner with default_context"""
214        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
215        FlowStageBinding.objects.create(
216            target=flow, stage=DummyStage.objects.create(name=generate_id()), order=0
217        )
218
219        user = User.objects.create(username="test-user")
220        request = self.request_factory.get(
221            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
222        )
223        request.user = user
224        planner = FlowPlanner(flow)
225        planner.plan(request, default_context={PLAN_CONTEXT_PENDING_USER: user})
226        key = cache_key(flow, user)
227        self.assertTrue(cache.get(key) is not None)

Test planner with default_context

def test_dry_run_policy_does_not_block_plan(self):
229    def test_dry_run_policy_does_not_block_plan(self):
230        """A failing dry-run policy does not block flow planning."""
231        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
232        stage_binding = FlowStageBinding.objects.create(
233            target=flow,
234            stage=DummyStage.objects.create(name=generate_id()),
235            order=0,
236        )
237        policy = DummyPolicy.objects.create(
238            name=generate_id(), result=False, wait_min=0, wait_max=1
239        )
240        PolicyBinding.objects.create(target=flow, policy=policy, order=0, dry_run=True)
241        request = self.request_factory.get(
242            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
243        )
244
245        plan = FlowPlanner(flow).plan(request)
246
247        self.assertEqual(plan.bindings, [stage_binding])

A failing dry-run policy does not block flow planning.

def test_planner_marker_reevaluate(self):
249    def test_planner_marker_reevaluate(self):
250        """Test that the planner creates the proper marker"""
251        flow = create_test_flow()
252
253        FlowStageBinding.objects.create(
254            target=flow,
255            stage=DummyStage.objects.create(name=generate_id()),
256            order=0,
257            re_evaluate_policies=True,
258        )
259
260        request = self.request_factory.get(
261            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
262        )
263
264        planner = FlowPlanner(flow)
265        plan = planner.plan(request)
266
267        self.assertEqual(plan.markers[0].__class__, ReevaluateMarker)

Test that the planner creates the proper marker

def test_planner_reevaluate_actual(self):
269    def test_planner_reevaluate_actual(self):
270        """Test planner with re-evaluate"""
271        flow = create_test_flow()
272        false_policy = DummyPolicy.objects.create(result=False, wait_min=1, wait_max=2)
273
274        binding = FlowStageBinding.objects.create(
275            target=flow,
276            stage=DummyStage.objects.create(name=generate_id()),
277            order=0,
278            re_evaluate_policies=False,
279        )
280        binding2 = FlowStageBinding.objects.create(
281            target=flow,
282            stage=DummyStage.objects.create(name=generate_id()),
283            order=1,
284            re_evaluate_policies=True,
285        )
286
287        PolicyBinding.objects.create(policy=false_policy, target=binding2, order=0)
288
289        request = self.request_factory.get(
290            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
291        )
292
293        # Here we patch the dummy policy to evaluate to true so the stage is included
294        with patch("authentik.policies.dummy.models.DummyPolicy.passes", POLICY_RETURN_TRUE):
295            planner = FlowPlanner(flow)
296            plan = planner.plan(request)
297
298            self.assertEqual(plan.bindings[0], binding)
299            self.assertEqual(plan.bindings[1], binding2)
300
301            self.assertEqual(plan.markers[0].__class__, StageMarker)
302            self.assertEqual(plan.markers[1].__class__, ReevaluateMarker)
303            self.assertIsInstance(plan.markers[0], StageMarker)
304            self.assertIsInstance(plan.markers[1], ReevaluateMarker)

Test planner with re-evaluate

def test_to_redirect(self):
306    def test_to_redirect(self):
307        """Test to_redirect and skipping the flow executor"""
308        flow = create_test_flow()
309        flow.authentication = FlowAuthenticationRequirement.NONE
310        request = self.request_factory.get(
311            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
312        )
313
314        planner = FlowPlanner(flow)
315        planner.allow_empty_flows = True
316        plan = planner.plan(request)
317        self.assertTrue(plan.requires_flow_executor())
318        self.assertEqual(
319            plan.to_redirect(request, flow).url,
320            reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug}),
321        )

Test to_redirect and skipping the flow executor

def test_to_redirect_skip_simple(self):
323    def test_to_redirect_skip_simple(self):
324        """Test to_redirect and skipping the flow executor"""
325        flow = create_test_flow()
326        flow.authentication = FlowAuthenticationRequirement.NONE
327        request = self.request_factory.get(
328            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
329        )
330        planner = FlowPlanner(flow)
331        planner.allow_empty_flows = True
332        plan = planner.plan(request)
333
334        class TStageView(StageView):
335            def dispatch(self, request: HttpRequest, *args, **kwargs):
336                return redirect("https://authentik.company")
337
338        plan.append_stage(in_memory_stage(TStageView))
339        self.assertFalse(plan.requires_flow_executor(allowed_silent_types=[TStageView]))
340        self.assertEqual(
341            plan.to_redirect(request, flow, allowed_silent_types=[TStageView]).url,
342            "https://authentik.company",
343        )

Test to_redirect and skipping the flow executor

def test_to_redirect_skip_keeps_session_plan(self):
345    def test_to_redirect_skip_keeps_session_plan(self):
346        """Direct execution leaves the session's flow state to the flow that owns it, even when
347        the direct stage completes or cancels its plan."""
348        flow = create_test_flow()
349        flow.authentication = FlowAuthenticationRequirement.NONE
350        other_plan = FlowPlan(flow_pk=create_test_flow().pk.hex)
351        other_get = QueryDict("next=%2Fother%2F")
352
353        for completion in ["stage_ok", "cancel"]:
354            with self.subTest(completion=completion):
355                request = self.request_factory.get(
356                    reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
357                )
358                request.session[SESSION_KEY_PLAN] = other_plan
359                request.session[SESSION_KEY_GET] = other_get
360                planner = FlowPlanner(flow)
361                planner.allow_empty_flows = True
362                plan = planner.plan(request)
363
364                class TStageView(StageView):
365                    completion_method = completion
366
367                    def dispatch(self, request: HttpRequest, *args, **kwargs):
368                        getattr(self.executor, self.completion_method)()
369                        return HttpResponseBadRequest()
370
371                plan.append_stage(in_memory_stage(TStageView))
372
373                response = plan.to_redirect(request, flow, allowed_silent_types=[TStageView])
374
375                self.assertEqual(response.status_code, 400)
376                self.assertIs(request.session[SESSION_KEY_PLAN], other_plan)
377                self.assertEqual(request.session[SESSION_KEY_GET], other_get)

Direct execution leaves the session's flow state to the flow that owns it, even when the direct stage completes or cancels its plan.

def test_to_redirect_skip_stage(self):
379    def test_to_redirect_skip_stage(self):
380        """Test to_redirect and skipping the flow executor
381        (with a stage bound that cannot be skipped)"""
382        flow = create_test_flow()
383        flow.authentication = FlowAuthenticationRequirement.NONE
384
385        FlowStageBinding.objects.create(
386            target=flow, stage=DummyStage.objects.create(name="dummy"), order=0
387        )
388
389        request = self.request_factory.get(
390            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
391        )
392        planner = FlowPlanner(flow)
393        planner.allow_empty_flows = True
394        plan = planner.plan(request)
395
396        class TStageView(StageView):
397            def dispatch(self, request: HttpRequest, *args, **kwargs):
398                return redirect("https://authentik.company")
399
400        plan.append_stage(in_memory_stage(TStageView))
401        self.assertTrue(plan.requires_flow_executor(allowed_silent_types=[TStageView]))

Test to_redirect and skipping the flow executor (with a stage bound that cannot be skipped)

def test_to_redirect_skip_policies(self):
403    def test_to_redirect_skip_policies(self):
404        """Test to_redirect and skipping the flow executor
405        (with a marker on the stage view type that can be skipped)
406
407        Note that this is not actually used anywhere in the code, all stages that are dynamically
408        added are statically added"""
409        flow = create_test_flow()
410        flow.authentication = FlowAuthenticationRequirement.NONE
411
412        request = self.request_factory.get(
413            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
414        )
415        planner = FlowPlanner(flow)
416        planner.allow_empty_flows = True
417        plan = planner.plan(request)
418
419        class TStageView(StageView):
420            def dispatch(self, request: HttpRequest, *args, **kwargs):
421                return redirect("https://authentik.company")
422
423        plan.append_stage(in_memory_stage(TStageView), ReevaluateMarker(None))
424        self.assertTrue(plan.requires_flow_executor(allowed_silent_types=[TStageView]))

Test to_redirect and skipping the flow executor (with a marker on the stage view type that can be skipped)

Note that this is not actually used anywhere in the code, all stages that are dynamically added are statically added