flows: add test for PLAN_CONTEXT_PENDING_USER_IDENTIFIER

This commit is contained in:
Jens Langhammer 2020-12-13 18:23:19 +01:00
parent aa5381fd59
commit 17732eea08
2 changed files with 35 additions and 4 deletions

View File

@ -1,4 +1,7 @@
"""flow views tests""" """flow views tests"""
from django.test.client import RequestFactory
from authentik.flows.stage import PLAN_CONTEXT_PENDING_USER_IDENTIFIER, StageView
from authentik.core.models import User
from unittest.mock import MagicMock, PropertyMock, patch from unittest.mock import MagicMock, PropertyMock, patch
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
@ -9,8 +12,8 @@ from django.utils.encoding import force_str
from authentik.flows.exceptions import EmptyFlowException, FlowNonApplicableException from authentik.flows.exceptions import EmptyFlowException, FlowNonApplicableException
from authentik.flows.markers import ReevaluateMarker, StageMarker from authentik.flows.markers import ReevaluateMarker, StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding
from authentik.flows.planner import FlowPlan from authentik.flows.planner import FlowPlan, FlowPlanner
from authentik.flows.views import NEXT_ARG_NAME, SESSION_KEY_PLAN from authentik.flows.views import FlowExecutorView, NEXT_ARG_NAME, SESSION_KEY_PLAN
from authentik.lib.config import CONFIG from authentik.lib.config import CONFIG
from authentik.policies.dummy.models import DummyPolicy from authentik.policies.dummy.models import DummyPolicy
from authentik.policies.http import AccessDeniedResponse from authentik.policies.http import AccessDeniedResponse
@ -35,7 +38,7 @@ class TestFlowExecutor(TestCase):
"""Test views logic""" """Test views logic"""
def setUp(self): def setUp(self):
self.client = Client() self.request_factory = RequestFactory()
def test_existing_plan_diff_flow(self): def test_existing_plan_diff_flow(self):
"""Check that a plan for a different flow cancels the current plan""" """Check that a plan for a different flow cancels the current plan"""
@ -428,3 +431,31 @@ class TestFlowExecutor(TestCase):
force_str(response.content), force_str(response.content),
{"type": "redirect", "to": reverse("authentik_core:shell")}, {"type": "redirect", "to": reverse("authentik_core:shell")},
) )
def test_stageview_user_identifier(self):
"""Test PLAN_CONTEXT_PENDING_USER_IDENTIFIER"""
flow = Flow.objects.create(
name="test-default-context",
slug="test-default-context",
designation=FlowDesignation.AUTHENTICATION,
)
FlowStageBinding.objects.create(
target=flow, stage=DummyStage.objects.create(name="dummy"), order=0
)
ident = "test-identifier"
user = User.objects.create(username="test-user")
request = self.request_factory.get(
reverse("authentik_flows:flow-executor", kwargs={"flow_slug": flow.slug}),
)
request.user = user
planner = FlowPlanner(flow)
plan = planner.plan(request, default_context={PLAN_CONTEXT_PENDING_USER_IDENTIFIER: ident})
executor = FlowExecutorView()
executor.plan = plan
executor.flow = flow
stage_view = StageView(executor)
self.assertEqual(ident, stage_view.get_context_data()["user"].username)

View File

@ -61,7 +61,7 @@ class DataclassEncoder(JSONEncoder):
return asdict(o) return asdict(o)
if isinstance(o, UUID): if isinstance(o, UUID):
return str(o) return str(o)
return super().default(o) return super().default(o) # pragma: no cover
class EntryInvalidError(SentryIgnoredException): class EntryInvalidError(SentryIgnoredException):