From aeff24d03471bf3bea08b282061844387b185e3c Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 17 Sep 2022 11:59:39 +0200 Subject: [PATCH] use create_test_flow where possible Signed-off-by: Jens Langhammer --- authentik/core/tests/test_applications_api.py | 8 +--- .../core/tests/test_applications_views.py | 15 ++----- .../core/tests/test_source_flow_manager.py | 6 +-- authentik/flows/tests/test_inspector.py | 11 ++---- authentik/flows/tests/test_planner.py | 39 ++++--------------- authentik/outposts/tests/test_ws.py | 6 +-- .../authenticator_validate/tests/test_sms.py | 10 ++--- .../tests/test_stage.py | 6 +-- .../tests/test_webauthn.py | 4 +- authentik/stages/identification/tests.py | 15 ++----- authentik/stages/prompt/tests.py | 10 ++--- authentik/stages/user_write/tests.py | 10 ++--- tests/integration/test_outpost_docker.py | 6 +-- tests/integration/test_proxy_docker.py | 6 +-- 14 files changed, 43 insertions(+), 109 deletions(-) diff --git a/authentik/core/tests/test_applications_api.py b/authentik/core/tests/test_applications_api.py index a455c31dcf..9eeaf31318 100644 --- a/authentik/core/tests/test_applications_api.py +++ b/authentik/core/tests/test_applications_api.py @@ -5,8 +5,7 @@ from django.urls import reverse from rest_framework.test import APITestCase from authentik.core.models import Application -from authentik.core.tests.utils import create_test_admin_user -from authentik.flows.models import Flow +from authentik.core.tests.utils import create_test_admin_user, create_test_flow from authentik.policies.dummy.models import DummyPolicy from authentik.policies.models import PolicyBinding from authentik.providers.oauth2.models import OAuth2Provider @@ -20,10 +19,7 @@ class TestApplicationsAPI(APITestCase): self.provider = OAuth2Provider.objects.create( name="test", redirect_uris="http://some-other-domain", - authorization_flow=Flow.objects.create( - name="test", - slug="test", - ), + authorization_flow=create_test_flow(), ) self.allowed = Application.objects.create( name="allowed", diff --git a/authentik/core/tests/test_applications_views.py b/authentik/core/tests/test_applications_views.py index 6a8e69949c..7c30539593 100644 --- a/authentik/core/tests/test_applications_views.py +++ b/authentik/core/tests/test_applications_views.py @@ -4,8 +4,7 @@ from unittest.mock import MagicMock, patch from django.urls import reverse from authentik.core.models import Application -from authentik.core.tests.utils import create_test_admin_user, create_test_tenant -from authentik.flows.models import Flow, FlowDesignation +from authentik.core.tests.utils import create_test_admin_user, create_test_flow, create_test_tenant from authentik.flows.tests import FlowTestCase from authentik.tenants.models import Tenant @@ -21,11 +20,7 @@ class TestApplicationsViews(FlowTestCase): def test_check_redirect(self): """Test redirect""" - empty_flow = Flow.objects.create( - name="foo", - slug="foo", - designation=FlowDesignation.AUTHENTICATION, - ) + empty_flow = create_test_flow() tenant: Tenant = create_test_tenant() tenant.flow_authentication = empty_flow tenant.save() @@ -49,11 +44,7 @@ class TestApplicationsViews(FlowTestCase): def test_check_redirect_auth(self): """Test redirect""" self.client.force_login(self.user) - empty_flow = Flow.objects.create( - name="foo", - slug="foo", - designation=FlowDesignation.AUTHENTICATION, - ) + empty_flow = create_test_flow() tenant: Tenant = create_test_tenant() tenant.flow_authentication = empty_flow tenant.save() diff --git a/authentik/core/tests/test_source_flow_manager.py b/authentik/core/tests/test_source_flow_manager.py index 9c77b1b2ca..3e6885cde4 100644 --- a/authentik/core/tests/test_source_flow_manager.py +++ b/authentik/core/tests/test_source_flow_manager.py @@ -6,7 +6,7 @@ from guardian.utils import get_anonymous_user from authentik.core.models import SourceUserMatchingModes, User from authentik.core.sources.flow_manager import Action -from authentik.flows.models import Flow, FlowDesignation +from authentik.core.tests.utils import create_test_flow from authentik.lib.generators import generate_id from authentik.lib.tests.utils import get_request from authentik.policies.denied import AccessDeniedResponse @@ -152,9 +152,7 @@ class TestSourceFlowManager(TestCase): """Test error handling when a source selected flow is non-applicable due to a policy""" self.source.user_matching_mode = SourceUserMatchingModes.USERNAME_LINK - flow = Flow.objects.create( - name="test", slug="test", title="test", designation=FlowDesignation.ENROLLMENT - ) + flow = create_test_flow() policy = ExpressionPolicy.objects.create( name="false", expression="""ak_message("foo");return False""" ) diff --git a/authentik/flows/tests/test_inspector.py b/authentik/flows/tests/test_inspector.py index 822b9088e9..25fafe926a 100644 --- a/authentik/flows/tests/test_inspector.py +++ b/authentik/flows/tests/test_inspector.py @@ -6,10 +6,9 @@ from django.test.client import RequestFactory from django.urls.base import reverse from rest_framework.test import APITestCase -from authentik.core.tests.utils import create_test_admin_user +from authentik.core.tests.utils import create_test_admin_user, create_test_flow from authentik.flows.challenge import ChallengeTypes -from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding, InvalidResponseAction -from authentik.lib.generators import generate_id +from authentik.flows.models import FlowStageBinding, InvalidResponseAction from authentik.stages.dummy.models import DummyStage from authentik.stages.identification.models import IdentificationStage, UserFields @@ -24,11 +23,7 @@ class TestFlowInspector(APITestCase): def test(self): """test inspector""" - flow = Flow.objects.create( - name=generate_id(), - slug=generate_id(), - designation=FlowDesignation.AUTHENTICATION, - ) + flow = create_test_flow() # Stage 1 is an identification stage ident_stage = IdentificationStage.objects.create( diff --git a/authentik/flows/tests/test_planner.py b/authentik/flows/tests/test_planner.py index cb7bf44f71..92141b6080 100644 --- a/authentik/flows/tests/test_planner.py +++ b/authentik/flows/tests/test_planner.py @@ -8,9 +8,10 @@ from django.urls import reverse from guardian.shortcuts import get_anonymous_user from authentik.core.models import User +from authentik.core.tests.utils import create_test_flow from authentik.flows.exceptions import EmptyFlowException, FlowNonApplicableException from authentik.flows.markers import ReevaluateMarker, StageMarker -from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding +from authentik.flows.models import FlowStageBinding from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlanner, cache_key from authentik.lib.tests.utils import dummy_get_response from authentik.policies.dummy.models import DummyPolicy @@ -32,11 +33,7 @@ class TestFlowPlanner(TestCase): def test_empty_plan(self): """Test that empty plan raises exception""" - flow = Flow.objects.create( - name="test-empty", - slug="test-empty", - designation=FlowDesignation.AUTHENTICATION, - ) + flow = create_test_flow() request = self.request_factory.get( reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}), ) @@ -52,11 +49,7 @@ class TestFlowPlanner(TestCase): ) def test_non_applicable_plan(self): """Test that empty plan raises exception""" - flow = Flow.objects.create( - name="test-empty", - slug="test-empty", - designation=FlowDesignation.AUTHENTICATION, - ) + flow = create_test_flow() request = self.request_factory.get( reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}), ) @@ -69,11 +62,7 @@ class TestFlowPlanner(TestCase): @patch("authentik.flows.planner.cache", CACHE_MOCK) def test_planner_cache(self): """Test planner cache""" - flow = Flow.objects.create( - name="test-cache", - slug="test-cache", - designation=FlowDesignation.AUTHENTICATION, - ) + flow = create_test_flow() FlowStageBinding.objects.create( target=flow, stage=DummyStage.objects.create(name="dummy"), order=0 ) @@ -92,11 +81,7 @@ class TestFlowPlanner(TestCase): def test_planner_default_context(self): """Test planner with default_context""" - flow = Flow.objects.create( - name="test-default-context", - slug="test-default-context", - designation=FlowDesignation.AUTHENTICATION, - ) + flow = create_test_flow() FlowStageBinding.objects.create( target=flow, stage=DummyStage.objects.create(name="dummy"), order=0 ) @@ -113,11 +98,7 @@ class TestFlowPlanner(TestCase): def test_planner_marker_reevaluate(self): """Test that the planner creates the proper marker""" - flow = Flow.objects.create( - name="test-default-context", - slug="test-default-context", - designation=FlowDesignation.AUTHENTICATION, - ) + flow = create_test_flow() FlowStageBinding.objects.create( target=flow, @@ -138,11 +119,7 @@ class TestFlowPlanner(TestCase): def test_planner_reevaluate_actual(self): """Test planner with re-evaluate""" - flow = Flow.objects.create( - name="test-default-context", - slug="test-default-context", - designation=FlowDesignation.AUTHENTICATION, - ) + flow = create_test_flow() false_policy = DummyPolicy.objects.create(result=False, wait_min=1, wait_max=2) binding = FlowStageBinding.objects.create( diff --git a/authentik/outposts/tests/test_ws.py b/authentik/outposts/tests/test_ws.py index 982fe515a3..9d85460446 100644 --- a/authentik/outposts/tests/test_ws.py +++ b/authentik/outposts/tests/test_ws.py @@ -6,7 +6,7 @@ from channels.testing import WebsocketCommunicator from django.test import TransactionTestCase from authentik import __version__ -from authentik.flows.models import Flow, FlowDesignation +from authentik.core.tests.utils import create_test_flow from authentik.outposts.channels import WebsocketMessage, WebsocketMessageInstruction from authentik.outposts.models import Outpost, OutpostType from authentik.providers.proxy.models import ProxyProvider @@ -21,9 +21,7 @@ class TestOutpostWS(TransactionTestCase): name="test", internal_host="http://localhost", external_host="http://localhost", - authorization_flow=Flow.objects.create( - name="foo", slug="foo", designation=FlowDesignation.AUTHORIZATION - ), + authorization_flow=create_test_flow(), ) self.outpost: Outpost = Outpost.objects.create( name="test", diff --git a/authentik/stages/authenticator_validate/tests/test_sms.py b/authentik/stages/authenticator_validate/tests/test_sms.py index 7288bc96c8..e171b1ef87 100644 --- a/authentik/stages/authenticator_validate/tests/test_sms.py +++ b/authentik/stages/authenticator_validate/tests/test_sms.py @@ -4,8 +4,8 @@ from unittest.mock import MagicMock, patch from django.test.client import RequestFactory from django.urls.base import reverse -from authentik.core.tests.utils import create_test_admin_user -from authentik.flows.models import Flow, FlowStageBinding, NotConfiguredAction +from authentik.core.tests.utils import create_test_admin_user, create_test_flow +from authentik.flows.models import FlowStageBinding, NotConfiguredAction from authentik.flows.tests import FlowTestCase from authentik.lib.generators import generate_id from authentik.stages.authenticator_sms.models import AuthenticatorSMSStage, SMSDevice, SMSProviders @@ -47,7 +47,7 @@ class AuthenticatorValidateStageSMSTests(FlowTestCase): device_classes=[DeviceClasses.SMS], ) stage.configuration_stages.set([ident_stage]) - flow = Flow.objects.create(name="test", slug="test", title="test") + flow = create_test_flow() FlowStageBinding.objects.create(target=flow, stage=ident_stage, order=0) FlowStageBinding.objects.create(target=flow, stage=stage, order=1) @@ -84,7 +84,7 @@ class AuthenticatorValidateStageSMSTests(FlowTestCase): device_classes=[DeviceClasses.SMS], ) stage.configuration_stages.set([ident_stage]) - flow = Flow.objects.create(name="test", slug="test", title="test") + flow = create_test_flow() FlowStageBinding.objects.create(target=flow, stage=ident_stage, order=0) FlowStageBinding.objects.create(target=flow, stage=stage, order=1) @@ -140,7 +140,7 @@ class AuthenticatorValidateStageSMSTests(FlowTestCase): device_classes=[DeviceClasses.SMS], ) stage.configuration_stages.set([ident_stage]) - flow = Flow.objects.create(name="test", slug="test", title="test") + flow = create_test_flow() FlowStageBinding.objects.create(target=flow, stage=ident_stage, order=0) FlowStageBinding.objects.create(target=flow, stage=stage, order=1) diff --git a/authentik/stages/authenticator_validate/tests/test_stage.py b/authentik/stages/authenticator_validate/tests/test_stage.py index 8c717e7df8..3fed9a6c00 100644 --- a/authentik/stages/authenticator_validate/tests/test_stage.py +++ b/authentik/stages/authenticator_validate/tests/test_stage.py @@ -4,8 +4,8 @@ from django.test.client import RequestFactory from django.urls.base import reverse from rest_framework.exceptions import ValidationError -from authentik.core.tests.utils import create_test_admin_user -from authentik.flows.models import Flow, FlowStageBinding, NotConfiguredAction +from authentik.core.tests.utils import create_test_admin_user, create_test_flow +from authentik.flows.models import FlowStageBinding, NotConfiguredAction from authentik.flows.stage import StageView from authentik.flows.tests import FlowTestCase from authentik.flows.views.executor import FlowExecutorView @@ -40,7 +40,7 @@ class AuthenticatorValidateStageTests(FlowTestCase): not_configured_action=NotConfiguredAction.CONFIGURE, ) stage.configuration_stages.set([conf_stage]) - flow = Flow.objects.create(name="test", slug="test", title="test") + flow = create_test_flow() FlowStageBinding.objects.create(target=flow, stage=conf_stage, order=0) FlowStageBinding.objects.create(target=flow, stage=stage, order=1) diff --git a/authentik/stages/authenticator_validate/tests/test_webauthn.py b/authentik/stages/authenticator_validate/tests/test_webauthn.py index 46de159017..c275a65468 100644 --- a/authentik/stages/authenticator_validate/tests/test_webauthn.py +++ b/authentik/stages/authenticator_validate/tests/test_webauthn.py @@ -8,7 +8,7 @@ from webauthn.helpers.base64url_to_bytes import base64url_to_bytes from webauthn.helpers.bytes_to_base64url import bytes_to_base64url from authentik.core.tests.utils import create_test_admin_user, create_test_flow -from authentik.flows.models import Flow, FlowStageBinding, NotConfiguredAction +from authentik.flows.models import FlowStageBinding, NotConfiguredAction from authentik.flows.stage import StageView from authentik.flows.tests import FlowTestCase from authentik.flows.views.executor import FlowExecutorView @@ -54,7 +54,7 @@ class AuthenticatorValidateStageWebAuthnTests(FlowTestCase): ) sleep(1) stage.configuration_stages.set([ident_stage]) - flow = Flow.objects.create(name="test", slug="test", title="test") + flow = create_test_flow() FlowStageBinding.objects.create(target=flow, stage=ident_stage, order=0) FlowStageBinding.objects.create(target=flow, stage=stage, order=1) diff --git a/authentik/stages/identification/tests.py b/authentik/stages/identification/tests.py index b7b8d79da1..c35c6499d5 100644 --- a/authentik/stages/identification/tests.py +++ b/authentik/stages/identification/tests.py @@ -3,7 +3,7 @@ from django.urls import reverse from authentik.core.tests.utils import create_test_admin_user, create_test_flow from authentik.flows.challenge import ChallengeTypes -from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding +from authentik.flows.models import FlowDesignation, FlowStageBinding from authentik.flows.tests import FlowTestCase from authentik.sources.oauth.models import OAuthSource from authentik.stages.identification.models import IdentificationStage, UserFields @@ -148,12 +148,7 @@ class TestIdentificationStage(FlowTestCase): def test_enrollment_flow(self): """Test that enrollment flow is linked correctly""" - flow = Flow.objects.create( - name="enroll-test", - slug="unique-enrollment-string", - title="unique-enrollment-string", - designation=FlowDesignation.ENROLLMENT, - ) + flow = create_test_flow() self.stage.enrollment_flow = flow self.stage.save() FlowStageBinding.objects.create( @@ -192,11 +187,7 @@ class TestIdentificationStage(FlowTestCase): def test_recovery_flow(self): """Test that recovery flow is linked correctly""" - flow = Flow.objects.create( - name="recovery-test", - slug="unique-recovery-string", - designation=FlowDesignation.RECOVERY, - ) + flow = create_test_flow() self.stage.recovery_flow = flow self.stage.save() FlowStageBinding.objects.create( diff --git a/authentik/stages/prompt/tests.py b/authentik/stages/prompt/tests.py index 414d197516..ab79563514 100644 --- a/authentik/stages/prompt/tests.py +++ b/authentik/stages/prompt/tests.py @@ -5,9 +5,9 @@ from django.test import RequestFactory from django.urls import reverse from rest_framework.exceptions import ErrorDetail, ValidationError -from authentik.core.tests.utils import create_test_admin_user +from authentik.core.tests.utils import create_test_admin_user, create_test_flow from authentik.flows.markers import StageMarker -from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding +from authentik.flows.models import FlowStageBinding from authentik.flows.planner import FlowPlan from authentik.flows.tests import FlowTestCase from authentik.flows.views.executor import SESSION_KEY_PLAN @@ -24,11 +24,7 @@ class TestPromptStage(FlowTestCase): super().setUp() self.user = create_test_admin_user() self.factory = RequestFactory() - self.flow = Flow.objects.create( - name="test-prompt", - slug="test-prompt", - designation=FlowDesignation.AUTHENTICATION, - ) + self.flow = create_test_flow() username_prompt = Prompt.objects.create( field_key="username_prompt", label="USERNAME_LABEL", diff --git a/authentik/stages/user_write/tests.py b/authentik/stages/user_write/tests.py index ae4aeb1c21..8c3be63e63 100644 --- a/authentik/stages/user_write/tests.py +++ b/authentik/stages/user_write/tests.py @@ -7,9 +7,9 @@ from django.urls import reverse from authentik.core.models import USER_ATTRIBUTE_SOURCES, Group, Source, User, UserSourceConnection from authentik.core.sources.stage import PLAN_CONTEXT_SOURCES_CONNECTION -from authentik.core.tests.utils import create_test_admin_user +from authentik.core.tests.utils import create_test_admin_user, create_test_flow from authentik.flows.markers import StageMarker -from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding +from authentik.flows.models import FlowStageBinding from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.tests import FlowTestCase from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK @@ -24,11 +24,7 @@ class TestUserWriteStage(FlowTestCase): def setUp(self): super().setUp() - self.flow = Flow.objects.create( - name="test-write", - slug="test-write", - designation=FlowDesignation.AUTHENTICATION, - ) + self.flow = create_test_flow() self.group = Group.objects.create(name="test-group") self.other_group = Group.objects.create(name="other-group") self.stage = UserWriteStage.objects.create( diff --git a/tests/integration/test_outpost_docker.py b/tests/integration/test_outpost_docker.py index 11ede61ef2..314d2a89cb 100644 --- a/tests/integration/test_outpost_docker.py +++ b/tests/integration/test_outpost_docker.py @@ -10,8 +10,8 @@ from docker.models.containers import Container from docker.types.healthcheck import Healthcheck from authentik import __version__ +from authentik.core.tests.utils import create_test_flow from authentik.crypto.models import CertificateKeyPair -from authentik.flows.models import Flow, FlowDesignation from authentik.outposts.controllers.docker import DockerController from authentik.outposts.models import ( DockerServiceConnection, @@ -64,9 +64,7 @@ class OutpostDockerTests(ChannelsLiveServerTestCase): name="test", internal_host="http://localhost", external_host="http://localhost", - authorization_flow=Flow.objects.create( - name="foo", slug="foo", designation=FlowDesignation.AUTHORIZATION - ), + authorization_flow=create_test_flow(), ) authentication_kp = CertificateKeyPair.objects.create( name="docker-authentication", diff --git a/tests/integration/test_proxy_docker.py b/tests/integration/test_proxy_docker.py index e71b5f68cd..d2e9327cb8 100644 --- a/tests/integration/test_proxy_docker.py +++ b/tests/integration/test_proxy_docker.py @@ -10,8 +10,8 @@ from docker.models.containers import Container from docker.types.healthcheck import Healthcheck from authentik import __version__ +from authentik.core.tests.utils import create_test_flow from authentik.crypto.models import CertificateKeyPair -from authentik.flows.models import Flow, FlowDesignation from authentik.outposts.models import ( DockerServiceConnection, Outpost, @@ -64,9 +64,7 @@ class TestProxyDocker(ChannelsLiveServerTestCase): name="test", internal_host="http://localhost", external_host="http://localhost", - authorization_flow=Flow.objects.create( - name="foo", slug="foo", designation=FlowDesignation.AUTHORIZATION - ), + authorization_flow=create_test_flow(), ) authentication_kp = CertificateKeyPair.objects.create( name="docker-authentication",