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=1,
 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                },
 59                "flow_designation": "authentication",
 60                "passkey_challenge": None,
 61                "password_fields": False,
 62                "primary_action": "Log in",
 63                "sources": [],
 64                "show_source_labels": False,
 65                "pending_user_identifier": None,
 66                "user_fields": ["username"],
 67            },
 68        )
 69
 70        ins = self.client.get(
 71            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 72        )
 73        content = loads(ins.content)
 74        self.assertEqual(content["is_completed"], False)
 75        self.assertEqual(
 76            content["current_plan"]["current_stage"]["stage_obj"]["name"], ident_stage.name
 77        )
 78        self.assertEqual(
 79            content["current_plan"]["next_planned_stage"]["stage_obj"]["name"], dummy_stage.name
 80        )
 81
 82        self.client.post(
 83            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
 84            {"uid_field": self.admin.username},
 85            follow=True,
 86        )
 87
 88        ins = self.client.get(
 89            reverse("authentik_api:flow-inspector", kwargs={"flow_slug": flow.slug}),
 90        )
 91        content = loads(ins.content)
 92        self.assertEqual(content["is_completed"], False)
 93        self.assertEqual(
 94            content["plans"][0]["current_stage"]["stage_obj"]["name"], ident_stage.name
 95        )
 96        self.assertEqual(
 97            content["current_plan"]["current_stage"]["stage_obj"]["name"], dummy_stage.name
 98        )
 99        self.assertEqual(
100            content["current_plan"]["plan_context"]["pending_user"]["username"], self.admin.username
101        )
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=1,
 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                },
 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        )

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=1,
 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                },
 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        )

test inspector