authentik.flows.tests.test_inspector

Flow inspector tests

  1"""Flow inspector tests"""
  2
  3from json import loads
  4
  5from django.test.client import RequestFactory
  6from django.urls.base import reverse
  7from rest_framework.test import APITestCase
  8
  9from authentik.core.tests.utils import create_test_admin_user, create_test_flow
 10from authentik.flows.models import FlowDesignation, FlowStageBinding, InvalidResponseAction
 11from authentik.lib.generators import generate_id
 12from authentik.stages.dummy.models import DummyStage
 13from authentik.stages.identification.models import IdentificationStage, UserFields
 14
 15
 16class TestFlowInspector(APITestCase):
 17    """Test inspector"""
 18
 19    def setUp(self):
 20        self.request_factory = RequestFactory()
 21        self.admin = create_test_admin_user()
 22        self.client.force_login(self.admin)
 23
 24    def test(self):
 25        """test inspector"""
 26        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
 27
 28        # Stage 1 is an identification stage
 29        ident_stage = IdentificationStage.objects.create(
 30            name=generate_id(),
 31            user_fields=[UserFields.USERNAME],
 32        )
 33        FlowStageBinding.objects.create(
 34            target=flow,
 35            stage=ident_stage,
 36            order=0,
 37            invalid_response_action=InvalidResponseAction.RESTART_WITH_CONTEXT,
 38        )
 39        dummy_stage = DummyStage.objects.create(name=generate_id())
 40        FlowStageBinding.objects.create(target=flow, stage=dummy_stage, order=1)
 41
 42        res = self.client.get(
 43            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 44        )
 45        self.assertJSONEqual(
 46            res.content,
 47            {
 48                "allow_show_password": False,
 49                "captcha_stage": None,
 50                "component": "ak-stage-identification",
 51                "enable_remember_me": False,
 52                "flow_info": {
 53                    "background": "/static/dist/assets/images/flow_background.jpg",
 54                    "background_themed_urls": None,
 55                    "cancel_url": reverse("authentik_flows:cancel"),
 56                    "title": flow.title,
 57                    "layout": "stacked",
 58                    "messages": [],
 59                },
 60                "flow_designation": "authentication",
 61                "passkey_challenge": None,
 62                "password_fields": False,
 63                "primary_action": "Log in",
 64                "sources": [],
 65                "show_source_labels": False,
 66                "pending_user_identifier": None,
 67                "user_fields": ["username"],
 68            },
 69        )
 70
 71        ins = self.client.get(
 72            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 73        )
 74        content = loads(ins.content)
 75        self.assertEqual(content["is_completed"], False)
 76        self.assertEqual(
 77            content["current_plan"]["current_stage"]["stage_obj"]["name"], ident_stage.name
 78        )
 79        self.assertEqual(
 80            content["current_plan"]["next_planned_stage"]["stage_obj"]["name"], dummy_stage.name
 81        )
 82
 83        self.client.post(
 84            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 85            {"uid_field": self.admin.username},
 86            follow=True,
 87        )
 88
 89        ins = self.client.get(
 90            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 91        )
 92        content = loads(ins.content)
 93        self.assertEqual(content["is_completed"], False)
 94        self.assertEqual(
 95            content["plans"][0]["current_stage"]["stage_obj"]["name"], ident_stage.name
 96        )
 97        self.assertEqual(
 98            content["current_plan"]["current_stage"]["stage_obj"]["name"], dummy_stage.name
 99        )
100        self.assertEqual(
101            content["current_plan"]["plan_context"]["pending_user"]["username"], self.admin.username
102        )
class TestFlowInspector(rest_framework.test.APITestCase):
 17class TestFlowInspector(APITestCase):
 18    """Test inspector"""
 19
 20    def setUp(self):
 21        self.request_factory = RequestFactory()
 22        self.admin = create_test_admin_user()
 23        self.client.force_login(self.admin)
 24
 25    def test(self):
 26        """test inspector"""
 27        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
 28
 29        # Stage 1 is an identification stage
 30        ident_stage = IdentificationStage.objects.create(
 31            name=generate_id(),
 32            user_fields=[UserFields.USERNAME],
 33        )
 34        FlowStageBinding.objects.create(
 35            target=flow,
 36            stage=ident_stage,
 37            order=0,
 38            invalid_response_action=InvalidResponseAction.RESTART_WITH_CONTEXT,
 39        )
 40        dummy_stage = DummyStage.objects.create(name=generate_id())
 41        FlowStageBinding.objects.create(target=flow, stage=dummy_stage, order=1)
 42
 43        res = self.client.get(
 44            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 45        )
 46        self.assertJSONEqual(
 47            res.content,
 48            {
 49                "allow_show_password": False,
 50                "captcha_stage": None,
 51                "component": "ak-stage-identification",
 52                "enable_remember_me": False,
 53                "flow_info": {
 54                    "background": "/static/dist/assets/images/flow_background.jpg",
 55                    "background_themed_urls": None,
 56                    "cancel_url": reverse("authentik_flows:cancel"),
 57                    "title": flow.title,
 58                    "layout": "stacked",
 59                    "messages": [],
 60                },
 61                "flow_designation": "authentication",
 62                "passkey_challenge": None,
 63                "password_fields": False,
 64                "primary_action": "Log in",
 65                "sources": [],
 66                "show_source_labels": False,
 67                "pending_user_identifier": None,
 68                "user_fields": ["username"],
 69            },
 70        )
 71
 72        ins = self.client.get(
 73            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 74        )
 75        content = loads(ins.content)
 76        self.assertEqual(content["is_completed"], False)
 77        self.assertEqual(
 78            content["current_plan"]["current_stage"]["stage_obj"]["name"], ident_stage.name
 79        )
 80        self.assertEqual(
 81            content["current_plan"]["next_planned_stage"]["stage_obj"]["name"], dummy_stage.name
 82        )
 83
 84        self.client.post(
 85            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 86            {"uid_field": self.admin.username},
 87            follow=True,
 88        )
 89
 90        ins = self.client.get(
 91            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 92        )
 93        content = loads(ins.content)
 94        self.assertEqual(content["is_completed"], False)
 95        self.assertEqual(
 96            content["plans"][0]["current_stage"]["stage_obj"]["name"], ident_stage.name
 97        )
 98        self.assertEqual(
 99            content["current_plan"]["current_stage"]["stage_obj"]["name"], dummy_stage.name
100        )
101        self.assertEqual(
102            content["current_plan"]["plan_context"]["pending_user"]["username"], self.admin.username
103        )

Test inspector

def setUp(self):
20    def setUp(self):
21        self.request_factory = RequestFactory()
22        self.admin = create_test_admin_user()
23        self.client.force_login(self.admin)

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

def test(self):
 25    def test(self):
 26        """test inspector"""
 27        flow = create_test_flow(FlowDesignation.AUTHENTICATION)
 28
 29        # Stage 1 is an identification stage
 30        ident_stage = IdentificationStage.objects.create(
 31            name=generate_id(),
 32            user_fields=[UserFields.USERNAME],
 33        )
 34        FlowStageBinding.objects.create(
 35            target=flow,
 36            stage=ident_stage,
 37            order=0,
 38            invalid_response_action=InvalidResponseAction.RESTART_WITH_CONTEXT,
 39        )
 40        dummy_stage = DummyStage.objects.create(name=generate_id())
 41        FlowStageBinding.objects.create(target=flow, stage=dummy_stage, order=1)
 42
 43        res = self.client.get(
 44            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 45        )
 46        self.assertJSONEqual(
 47            res.content,
 48            {
 49                "allow_show_password": False,
 50                "captcha_stage": None,
 51                "component": "ak-stage-identification",
 52                "enable_remember_me": False,
 53                "flow_info": {
 54                    "background": "/static/dist/assets/images/flow_background.jpg",
 55                    "background_themed_urls": None,
 56                    "cancel_url": reverse("authentik_flows:cancel"),
 57                    "title": flow.title,
 58                    "layout": "stacked",
 59                    "messages": [],
 60                },
 61                "flow_designation": "authentication",
 62                "passkey_challenge": None,
 63                "password_fields": False,
 64                "primary_action": "Log in",
 65                "sources": [],
 66                "show_source_labels": False,
 67                "pending_user_identifier": None,
 68                "user_fields": ["username"],
 69            },
 70        )
 71
 72        ins = self.client.get(
 73            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 74        )
 75        content = loads(ins.content)
 76        self.assertEqual(content["is_completed"], False)
 77        self.assertEqual(
 78            content["current_plan"]["current_stage"]["stage_obj"]["name"], ident_stage.name
 79        )
 80        self.assertEqual(
 81            content["current_plan"]["next_planned_stage"]["stage_obj"]["name"], dummy_stage.name
 82        )
 83
 84        self.client.post(
 85            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 86            {"uid_field": self.admin.username},
 87            follow=True,
 88        )
 89
 90        ins = self.client.get(
 91            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 92        )
 93        content = loads(ins.content)
 94        self.assertEqual(content["is_completed"], False)
 95        self.assertEqual(
 96            content["plans"][0]["current_stage"]["stage_obj"]["name"], ident_stage.name
 97        )
 98        self.assertEqual(
 99            content["current_plan"]["current_stage"]["stage_obj"]["name"], dummy_stage.name
100        )
101        self.assertEqual(
102            content["current_plan"]["plan_context"]["pending_user"]["username"], self.admin.username
103        )

test inspector