authentik.flows.tests.test_messages

Tests for messages attached to challenges

  1"""Tests for messages attached to challenges"""
  2
  3from django.contrib.messages import add_message
  4from django.contrib.messages.constants import SUCCESS, WARNING
  5from django.contrib.messages.storage.session import SessionStorage
  6from django.http import HttpRequest, HttpResponse
  7from django.urls import reverse
  8
  9from authentik.core.tests.utils import create_test_flow
 10from authentik.flows.challenge import Challenge, ChallengeResponse
 11from authentik.flows.models import FlowStageBinding, in_memory_stage
 12from authentik.flows.planner import FlowPlan
 13from authentik.flows.stage import ChallengeStageView, StageView
 14from authentik.flows.tests import APIFlowTestCase
 15from authentik.stages.dummy.models import DummyStage
 16
 17
 18class MessageStageView(StageView):
 19    """Stage which queues a message and continues to the next stage"""
 20
 21    def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
 22        add_message(request, SUCCESS, "stage message")
 23        return self.executor.stage_ok()
 24
 25
 26class MessageChallengeStageView(ChallengeStageView):
 27    """Stage which queues a message while rendering its challenge"""
 28
 29    def get_challenge(self, *args, **kwargs) -> Challenge:
 30        add_message(self.request, WARNING, "challenge message")
 31        return Challenge(data={"component": "ak-stage-dummy"})
 32
 33    def challenge_valid(self, response: ChallengeResponse) -> HttpResponse:
 34        return self.executor.stage_ok()
 35
 36
 37class TestFlowMessages(APIFlowTestCase):
 38    """Test messages attached to challenges"""
 39
 40    def setUp(self):
 41        self.flow = create_test_flow()
 42        self.url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug})
 43
 44    def test_challenge(self):
 45        """Test message queued while the challenge is rendered"""
 46        plan = FlowPlan(flow_pk=self.flow.pk.hex)
 47        plan.append_stage(in_memory_stage(MessageChallengeStageView))
 48        self.set_flow_plan(plan)
 49
 50        response = self.client.get(self.url)
 51        raw_response = self.assertStageResponse(response, self.flow)
 52        self.assertEqual(
 53            raw_response["flow_info"]["messages"],
 54            [{"level": "warning", "message": "challenge message"}],
 55        )
 56
 57    def test_challenge_not_repeated(self):
 58        """Test that a message is only ever attached to a single challenge"""
 59        plan = FlowPlan(flow_pk=self.flow.pk.hex)
 60        plan.append_stage(in_memory_stage(MessageChallengeStageView))
 61        self.set_flow_plan(plan)
 62
 63        self.client.get(self.url)
 64        # The stage queues a new message on every render, so the second challenge only
 65        # contains the message queued for it
 66        response = self.client.get(self.url)
 67        raw_response = self.assertStageResponse(response, self.flow)
 68        self.assertEqual(
 69            raw_response["flow_info"]["messages"],
 70            [{"level": "warning", "message": "challenge message"}],
 71        )
 72
 73    def test_previous_stage(self):
 74        """Test message queued by a stage which doesn't render a challenge itself, it is
 75        attached to the challenge of the next stage"""
 76        FlowStageBinding.objects.create(
 77            target=self.flow, stage=DummyStage.objects.create(name="dummy"), order=0
 78        )
 79        plan = FlowPlan(flow_pk=self.flow.pk.hex)
 80        plan.append_stage(in_memory_stage(MessageStageView))
 81        plan.append(FlowStageBinding.objects.filter(target=self.flow).first())
 82        self.set_flow_plan(plan)
 83
 84        response = self.client.get(self.url, follow=True)
 85        raw_response = self.assertStageResponse(
 86            response,
 87            self.flow,
 88            component="ak-stage-dummy",
 89        )
 90        self.assertEqual(
 91            raw_response["flow_info"]["messages"],
 92            [{"level": "success", "message": "stage message"}],
 93        )
 94
 95    def test_redirect_challenge(self):
 96        """Test message queued by the last stage of a flow. The client navigates away as soon
 97        as it gets the redirect challenge the flow finishes with, so the message is left
 98        queued for the page we redirect to"""
 99        plan = FlowPlan(flow_pk=self.flow.pk.hex)
100        plan.append_stage(in_memory_stage(MessageStageView))
101        self.set_flow_plan(plan)
102
103        response = self.client.get(self.url)
104        raw_response = self.assertStageResponse(response, component="xak-flow-redirect")
105        self.assertNotIn("messages", raw_response.get("flow_info", {}))
106        self.assertIn("stage message", self.client.session[SessionStorage.session_key])
107
108    def test_no_messages(self):
109        """Test that challenges without messages have an empty list"""
110        FlowStageBinding.objects.create(
111            target=self.flow, stage=DummyStage.objects.create(name="dummy"), order=0
112        )
113
114        response = self.client.get(self.url)
115        raw_response = self.assertStageResponse(response)
116        self.assertEqual(raw_response["flow_info"]["messages"], [])
class MessageStageView(authentik.flows.stage.StageView):
19class MessageStageView(StageView):
20    """Stage which queues a message and continues to the next stage"""
21
22    def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
23        add_message(request, SUCCESS, "stage message")
24        return self.executor.stage_ok()

Stage which queues a message and continues to the next stage

def dispatch( self, request: django.http.request.HttpRequest, *args, **kwargs) -> django.http.response.HttpResponse:
22    def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
23        add_message(request, SUCCESS, "stage message")
24        return self.executor.stage_ok()
class MessageChallengeStageView(authentik.flows.stage.ChallengeStageView):
27class MessageChallengeStageView(ChallengeStageView):
28    """Stage which queues a message while rendering its challenge"""
29
30    def get_challenge(self, *args, **kwargs) -> Challenge:
31        add_message(self.request, WARNING, "challenge message")
32        return Challenge(data={"component": "ak-stage-dummy"})
33
34    def challenge_valid(self, response: ChallengeResponse) -> HttpResponse:
35        return self.executor.stage_ok()

Stage which queues a message while rendering its challenge

def get_challenge(self, *args, **kwargs) -> authentik.flows.challenge.Challenge:
30    def get_challenge(self, *args, **kwargs) -> Challenge:
31        add_message(self.request, WARNING, "challenge message")
32        return Challenge(data={"component": "ak-stage-dummy"})

Return the challenge that the client should solve

def challenge_valid( self, response: authentik.flows.challenge.ChallengeResponse) -> django.http.response.HttpResponse:
34    def challenge_valid(self, response: ChallengeResponse) -> HttpResponse:
35        return self.executor.stage_ok()

Callback when the challenge has the correct format

class TestFlowMessages(authentik.flows.tests.APIFlowTestCase):
 38class TestFlowMessages(APIFlowTestCase):
 39    """Test messages attached to challenges"""
 40
 41    def setUp(self):
 42        self.flow = create_test_flow()
 43        self.url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug})
 44
 45    def test_challenge(self):
 46        """Test message queued while the challenge is rendered"""
 47        plan = FlowPlan(flow_pk=self.flow.pk.hex)
 48        plan.append_stage(in_memory_stage(MessageChallengeStageView))
 49        self.set_flow_plan(plan)
 50
 51        response = self.client.get(self.url)
 52        raw_response = self.assertStageResponse(response, self.flow)
 53        self.assertEqual(
 54            raw_response["flow_info"]["messages"],
 55            [{"level": "warning", "message": "challenge message"}],
 56        )
 57
 58    def test_challenge_not_repeated(self):
 59        """Test that a message is only ever attached to a single challenge"""
 60        plan = FlowPlan(flow_pk=self.flow.pk.hex)
 61        plan.append_stage(in_memory_stage(MessageChallengeStageView))
 62        self.set_flow_plan(plan)
 63
 64        self.client.get(self.url)
 65        # The stage queues a new message on every render, so the second challenge only
 66        # contains the message queued for it
 67        response = self.client.get(self.url)
 68        raw_response = self.assertStageResponse(response, self.flow)
 69        self.assertEqual(
 70            raw_response["flow_info"]["messages"],
 71            [{"level": "warning", "message": "challenge message"}],
 72        )
 73
 74    def test_previous_stage(self):
 75        """Test message queued by a stage which doesn't render a challenge itself, it is
 76        attached to the challenge of the next stage"""
 77        FlowStageBinding.objects.create(
 78            target=self.flow, stage=DummyStage.objects.create(name="dummy"), order=0
 79        )
 80        plan = FlowPlan(flow_pk=self.flow.pk.hex)
 81        plan.append_stage(in_memory_stage(MessageStageView))
 82        plan.append(FlowStageBinding.objects.filter(target=self.flow).first())
 83        self.set_flow_plan(plan)
 84
 85        response = self.client.get(self.url, follow=True)
 86        raw_response = self.assertStageResponse(
 87            response,
 88            self.flow,
 89            component="ak-stage-dummy",
 90        )
 91        self.assertEqual(
 92            raw_response["flow_info"]["messages"],
 93            [{"level": "success", "message": "stage message"}],
 94        )
 95
 96    def test_redirect_challenge(self):
 97        """Test message queued by the last stage of a flow. The client navigates away as soon
 98        as it gets the redirect challenge the flow finishes with, so the message is left
 99        queued for the page we redirect to"""
100        plan = FlowPlan(flow_pk=self.flow.pk.hex)
101        plan.append_stage(in_memory_stage(MessageStageView))
102        self.set_flow_plan(plan)
103
104        response = self.client.get(self.url)
105        raw_response = self.assertStageResponse(response, component="xak-flow-redirect")
106        self.assertNotIn("messages", raw_response.get("flow_info", {}))
107        self.assertIn("stage message", self.client.session[SessionStorage.session_key])
108
109    def test_no_messages(self):
110        """Test that challenges without messages have an empty list"""
111        FlowStageBinding.objects.create(
112            target=self.flow, stage=DummyStage.objects.create(name="dummy"), order=0
113        )
114
115        response = self.client.get(self.url)
116        raw_response = self.assertStageResponse(response)
117        self.assertEqual(raw_response["flow_info"]["messages"], [])

Test messages attached to challenges

def setUp(self):
41    def setUp(self):
42        self.flow = create_test_flow()
43        self.url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug})

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

def test_challenge(self):
45    def test_challenge(self):
46        """Test message queued while the challenge is rendered"""
47        plan = FlowPlan(flow_pk=self.flow.pk.hex)
48        plan.append_stage(in_memory_stage(MessageChallengeStageView))
49        self.set_flow_plan(plan)
50
51        response = self.client.get(self.url)
52        raw_response = self.assertStageResponse(response, self.flow)
53        self.assertEqual(
54            raw_response["flow_info"]["messages"],
55            [{"level": "warning", "message": "challenge message"}],
56        )

Test message queued while the challenge is rendered

def test_challenge_not_repeated(self):
58    def test_challenge_not_repeated(self):
59        """Test that a message is only ever attached to a single challenge"""
60        plan = FlowPlan(flow_pk=self.flow.pk.hex)
61        plan.append_stage(in_memory_stage(MessageChallengeStageView))
62        self.set_flow_plan(plan)
63
64        self.client.get(self.url)
65        # The stage queues a new message on every render, so the second challenge only
66        # contains the message queued for it
67        response = self.client.get(self.url)
68        raw_response = self.assertStageResponse(response, self.flow)
69        self.assertEqual(
70            raw_response["flow_info"]["messages"],
71            [{"level": "warning", "message": "challenge message"}],
72        )

Test that a message is only ever attached to a single challenge

def test_previous_stage(self):
74    def test_previous_stage(self):
75        """Test message queued by a stage which doesn't render a challenge itself, it is
76        attached to the challenge of the next stage"""
77        FlowStageBinding.objects.create(
78            target=self.flow, stage=DummyStage.objects.create(name="dummy"), order=0
79        )
80        plan = FlowPlan(flow_pk=self.flow.pk.hex)
81        plan.append_stage(in_memory_stage(MessageStageView))
82        plan.append(FlowStageBinding.objects.filter(target=self.flow).first())
83        self.set_flow_plan(plan)
84
85        response = self.client.get(self.url, follow=True)
86        raw_response = self.assertStageResponse(
87            response,
88            self.flow,
89            component="ak-stage-dummy",
90        )
91        self.assertEqual(
92            raw_response["flow_info"]["messages"],
93            [{"level": "success", "message": "stage message"}],
94        )

Test message queued by a stage which doesn't render a challenge itself, it is attached to the challenge of the next stage

def test_redirect_challenge(self):
 96    def test_redirect_challenge(self):
 97        """Test message queued by the last stage of a flow. The client navigates away as soon
 98        as it gets the redirect challenge the flow finishes with, so the message is left
 99        queued for the page we redirect to"""
100        plan = FlowPlan(flow_pk=self.flow.pk.hex)
101        plan.append_stage(in_memory_stage(MessageStageView))
102        self.set_flow_plan(plan)
103
104        response = self.client.get(self.url)
105        raw_response = self.assertStageResponse(response, component="xak-flow-redirect")
106        self.assertNotIn("messages", raw_response.get("flow_info", {}))
107        self.assertIn("stage message", self.client.session[SessionStorage.session_key])

Test message queued by the last stage of a flow. The client navigates away as soon as it gets the redirect challenge the flow finishes with, so the message is left queued for the page we redirect to

def test_no_messages(self):
109    def test_no_messages(self):
110        """Test that challenges without messages have an empty list"""
111        FlowStageBinding.objects.create(
112            target=self.flow, stage=DummyStage.objects.create(name="dummy"), order=0
113        )
114
115        response = self.client.get(self.url)
116        raw_response = self.assertStageResponse(response)
117        self.assertEqual(raw_response["flow_info"]["messages"], [])

Test that challenges without messages have an empty list