*: use create_test_admin_user for all unittests

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-05-28 21:13:16 +02:00
parent 4ed8171130
commit 098b0aef6e
14 changed files with 71 additions and 141 deletions

View File

@ -1,8 +1,8 @@
"""Password flow tests""" """Password flow tests"""
from django.urls.base import reverse from django.urls.base import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.policies.password.models import PasswordPolicy from authentik.policies.password.models import PasswordPolicy
from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage
@ -12,13 +12,9 @@ class TestPasswordPolicyFlow(FlowTestCase):
"""Test Password Policy""" """Test Password Policy"""
def setUp(self) -> None: def setUp(self) -> None:
self.user = User.objects.create(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.flow = Flow.objects.create(
name="test-prompt",
slug="test-prompt",
designation=FlowDesignation.AUTHENTICATION,
)
password_prompt = Prompt.objects.create( password_prompt = Prompt.objects.create(
field_key="password", field_key="password",
label="PASSWORD_LABEL", label="PASSWORD_LABEL",

View File

@ -1,9 +1,9 @@
"""captcha tests""" """captcha tests"""
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import FlowPlan from authentik.flows.planner import FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -19,13 +19,9 @@ class TestCaptchaStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create_user(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.flow = Flow.objects.create(
name="test-captcha",
slug="test-captcha",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = CaptchaStage.objects.create( self.stage = CaptchaStage.objects.create(
name="captcha", name="captcha",
public_key=RECAPTCHA_PUBLIC_KEY, public_key=RECAPTCHA_PUBLIC_KEY,

View File

@ -3,10 +3,11 @@ from time import sleep
from django.urls import reverse from django.urls import reverse
from authentik.core.models import Application, User from authentik.core.models import Application
from authentik.core.tasks import clean_expired_models from authentik.core.tasks import clean_expired_models
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_APPLICATION, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_APPLICATION, FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -18,7 +19,7 @@ class TestConsentStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create_user(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.application = Application.objects.create( self.application = Application.objects.create(
name="test-application", name="test-application",
slug="test-application", slug="test-application",
@ -26,11 +27,7 @@ class TestConsentStage(FlowTestCase):
def test_always_required(self): def test_always_required(self):
"""Test always required consent""" """Test always required consent"""
flow = Flow.objects.create( flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-consent",
slug="test-consent",
designation=FlowDesignation.AUTHENTICATION,
)
stage = ConsentStage.objects.create(name="consent", mode=ConsentMode.ALWAYS_REQUIRE) stage = ConsentStage.objects.create(name="consent", mode=ConsentMode.ALWAYS_REQUIRE)
binding = FlowStageBinding.objects.create(target=flow, stage=stage, order=2) binding = FlowStageBinding.objects.create(target=flow, stage=stage, order=2)
@ -50,11 +47,7 @@ class TestConsentStage(FlowTestCase):
def test_permanent(self): def test_permanent(self):
"""Test permanent consent from user""" """Test permanent consent from user"""
self.client.force_login(self.user) self.client.force_login(self.user)
flow = Flow.objects.create( flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-consent",
slug="test-consent",
designation=FlowDesignation.AUTHENTICATION,
)
stage = ConsentStage.objects.create(name="consent", mode=ConsentMode.PERMANENT) stage = ConsentStage.objects.create(name="consent", mode=ConsentMode.PERMANENT)
binding = FlowStageBinding.objects.create(target=flow, stage=stage, order=2) binding = FlowStageBinding.objects.create(target=flow, stage=stage, order=2)
@ -80,11 +73,7 @@ class TestConsentStage(FlowTestCase):
def test_expire(self): def test_expire(self):
"""Test expiring consent from user""" """Test expiring consent from user"""
self.client.force_login(self.user) self.client.force_login(self.user)
flow = Flow.objects.create( flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-consent",
slug="test-consent",
designation=FlowDesignation.AUTHENTICATION,
)
stage = ConsentStage.objects.create( stage = ConsentStage.objects.create(
name="consent", mode=ConsentMode.EXPIRING, consent_expire_in="seconds=1" name="consent", mode=ConsentMode.EXPIRING, consent_expire_in="seconds=1"
) )

View File

@ -1,9 +1,9 @@
"""deny tests""" """deny tests"""
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import FlowPlan from authentik.flows.planner import FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -15,13 +15,8 @@ class TestUserDenyStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.flow = Flow.objects.create(
name="test-logout",
slug="test-logout",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = DenyStage.objects.create(name="logout") self.stage = DenyStage.objects.create(name="logout")
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2) self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)

View File

@ -1,8 +1,8 @@
"""dummy tests""" """dummy tests"""
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.stages.dummy.models import DummyStage from authentik.stages.dummy.models import DummyStage
@ -12,13 +12,8 @@ class TestDummyStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.flow = Flow.objects.create(
name="test-dummy",
slug="test-dummy",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = DummyStage.objects.create( self.stage = DummyStage.objects.create(
name="dummy", name="dummy",
) )

View File

@ -7,10 +7,10 @@ from django.core.mail.backends.locmem import EmailBackend
from django.urls import reverse from django.urls import reverse
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.events.models import Event, EventAction from authentik.events.models import Event, EventAction
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.views.executor import SESSION_KEY_PLAN from authentik.flows.views.executor import SESSION_KEY_PLAN
from authentik.stages.email.models import EmailStage from authentik.stages.email.models import EmailStage
@ -21,13 +21,9 @@ class TestEmailStageSending(APITestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create_user(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = Flow.objects.create( self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-email",
slug="test-email",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = EmailStage.objects.create( self.stage = EmailStage.objects.create(
name="email", name="email",
) )
@ -53,9 +49,9 @@ class TestEmailStageSending(APITestCase):
events = Event.objects.filter(action=EventAction.EMAIL_SENT) events = Event.objects.filter(action=EventAction.EMAIL_SENT)
self.assertEqual(len(events), 1) self.assertEqual(len(events), 1)
event = events.first() event = events.first()
self.assertEqual(event.context["message"], "Email to test@beryju.org sent") self.assertEqual(event.context["message"], f"Email to {self.user.email} sent")
self.assertEqual(event.context["subject"], "authentik") self.assertEqual(event.context["subject"], "authentik")
self.assertEqual(event.context["to_email"], ["test@beryju.org"]) self.assertEqual(event.context["to_email"], [self.user.email])
self.assertEqual(event.context["from_email"], "system@authentik.local") self.assertEqual(event.context["from_email"], "system@authentik.local")
def test_send_error(self): def test_send_error(self):

View File

@ -7,9 +7,10 @@ from django.core.mail.backends.smtp import EmailBackend as SMTPEmailBackend
from django.urls import reverse from django.urls import reverse
from django.utils.http import urlencode from django.utils.http import urlencode
from authentik.core.models import Token, User from authentik.core.models import Token
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -22,13 +23,9 @@ class TestEmailStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create_user(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = Flow.objects.create( self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-email",
slug="test-email",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = EmailStage.objects.create( self.stage = EmailStage.objects.create(
name="email", name="email",
activate_user_on_success=True, activate_user_on_success=True,
@ -75,7 +72,7 @@ class TestEmailStage(FlowTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, "authentik") self.assertEqual(mail.outbox[0].subject, "authentik")
self.assertEqual(mail.outbox[0].to, ["test@beryju.org"]) self.assertEqual(mail.outbox[0].to, [self.user.email])
def test_pending_user_override(self): def test_pending_user_override(self):
"""Test with pending user (override to)""" """Test with pending user (override to)"""

View File

@ -1,11 +1,10 @@
"""identification tests""" """identification tests"""
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.challenge import ChallengeTypes from authentik.flows.challenge import ChallengeTypes
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.lib.generators import generate_key
from authentik.sources.oauth.models import OAuthSource from authentik.sources.oauth.models import OAuthSource
from authentik.stages.identification.models import IdentificationStage, UserFields from authentik.stages.identification.models import IdentificationStage, UserFields
from authentik.stages.password import BACKEND_INBUILT from authentik.stages.password import BACKEND_INBUILT
@ -17,19 +16,12 @@ class TestIdentificationStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.password = generate_key() self.user = create_test_admin_user()
self.user = User.objects.create_user(
username="unittest", email="test@beryju.org", password=self.password
)
# OAuthSource for the login view # OAuthSource for the login view
source = OAuthSource.objects.create(name="test", slug="test") source = OAuthSource.objects.create(name="test", slug="test")
self.flow = Flow.objects.create( self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-identification",
slug="test-identification",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = IdentificationStage.objects.create( self.stage = IdentificationStage.objects.create(
name="identification", name="identification",
user_fields=[UserFields.E_MAIL], user_fields=[UserFields.E_MAIL],
@ -62,7 +54,7 @@ class TestIdentificationStage(FlowTestCase):
pw_stage = PasswordStage.objects.create(name="password", backends=[BACKEND_INBUILT]) pw_stage = PasswordStage.objects.create(name="password", backends=[BACKEND_INBUILT])
self.stage.password_stage = pw_stage self.stage.password_stage = pw_stage
self.stage.save() self.stage.save()
form_data = {"uid_field": self.user.email, "password": self.password} form_data = {"uid_field": self.user.email, "password": self.user.username}
url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}) url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug})
response = self.client.post(url, form_data) response = self.client.post(url, form_data)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -75,7 +67,7 @@ class TestIdentificationStage(FlowTestCase):
self.stage.save() self.stage.save()
form_data = { form_data = {
"uid_field": self.user.email, "uid_field": self.user.email,
"password": self.password + "test", "password": self.user.username + "test",
} }
url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}) url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug})
response = self.client.post(url, form_data) response = self.client.post(url, form_data)

View File

@ -6,10 +6,9 @@ from django.utils.http import urlencode
from guardian.shortcuts import get_anonymous_user from guardian.shortcuts import get_anonymous_user
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.core.tests.utils import create_test_admin_user
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK
@ -29,13 +28,8 @@ class TestUserLoginStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.flow = Flow.objects.create(
name="test-invitation",
slug="test-invitation",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = InvitationStage.objects.create(name="invitation") self.stage = InvitationStage.objects.create(name="invitation")
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2) self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)

View File

@ -4,14 +4,13 @@ from unittest.mock import MagicMock, patch
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK
from authentik.flows.views.executor import SESSION_KEY_PLAN from authentik.flows.views.executor import SESSION_KEY_PLAN
from authentik.lib.generators import generate_key
from authentik.stages.password import BACKEND_INBUILT from authentik.stages.password import BACKEND_INBUILT
from authentik.stages.password.models import PasswordStage from authentik.stages.password.models import PasswordStage
@ -23,16 +22,9 @@ class TestPasswordStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.password = generate_key() self.user = create_test_admin_user()
self.user = User.objects.create_user(
username="unittest", email="test@beryju.org", password=self.password
)
self.flow = Flow.objects.create( self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-password",
slug="test-password",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = PasswordStage.objects.create(name="password", backends=[BACKEND_INBUILT]) self.stage = PasswordStage.objects.create(name="password", backends=[BACKEND_INBUILT])
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2) self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)
@ -50,7 +42,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post( response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}), reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Still have to send the password so the form is valid # Still have to send the password so the form is valid
{"password": self.password}, {"password": self.user.username},
) )
self.assertStageResponse( self.assertStageResponse(
@ -62,7 +54,7 @@ class TestPasswordStage(FlowTestCase):
def test_recovery_flow_link(self): def test_recovery_flow_link(self):
"""Test link to the default recovery flow""" """Test link to the default recovery flow"""
flow = Flow.objects.create(designation=FlowDesignation.RECOVERY, slug="qewrqerqr") flow = create_test_flow(designation=FlowDesignation.RECOVERY)
plan = FlowPlan(flow_pk=self.flow.pk.hex, bindings=[self.binding], markers=[StageMarker()]) plan = FlowPlan(flow_pk=self.flow.pk.hex, bindings=[self.binding], markers=[StageMarker()])
session = self.client.session session = self.client.session
@ -86,7 +78,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post( response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}), reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data # Form data
{"password": self.password}, {"password": self.user.username},
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -103,7 +95,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post( response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}), reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data # Form data
{"password": self.password + "test"}, {"password": self.user.username + "test"},
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -122,14 +114,14 @@ class TestPasswordStage(FlowTestCase):
kwargs={"flow_slug": self.flow.slug}, kwargs={"flow_slug": self.flow.slug},
), ),
# Form data # Form data
{"password": self.password + "test"}, {"password": self.user.username + "test"},
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.post( response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}), reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data # Form data
{"password": self.password + "test"}, {"password": self.user.username + "test"},
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# To ensure the plan has been cancelled, check SESSION_KEY_PLAN # To ensure the plan has been cancelled, check SESSION_KEY_PLAN
@ -155,7 +147,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post( response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}), reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data # Form data
{"password": self.password + "test"}, {"password": self.user.username + "test"},
) )
self.assertStageResponse( self.assertStageResponse(

View File

@ -4,8 +4,9 @@ from unittest.mock import patch
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.models import User
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK
@ -18,14 +19,9 @@ class TestUserDeleteStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.username = "qerqwerqrwqwerwq" self.user = create_test_admin_user()
self.user = User.objects.create(username=self.username, email="test@beryju.org")
self.flow = Flow.objects.create( self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-delete",
slug="test-delete",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = UserDeleteStage.objects.create(name="delete") self.stage = UserDeleteStage.objects.create(name="delete")
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2) self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)
@ -59,7 +55,7 @@ class TestUserDeleteStage(FlowTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertStageRedirects(response, reverse("authentik_core:root-redirect")) self.assertStageRedirects(response, reverse("authentik_core:root-redirect"))
self.assertFalse(User.objects.filter(username=self.username).exists()) self.assertFalse(User.objects.filter(username=self.user.username).exists())
def test_user_delete_post(self): def test_user_delete_post(self):
"""Test Form render""" """Test Form render"""
@ -75,4 +71,4 @@ class TestUserDeleteStage(FlowTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertStageRedirects(response, reverse("authentik_core:root-redirect")) self.assertStageRedirects(response, reverse("authentik_core:root-redirect"))
self.assertFalse(User.objects.filter(username=self.username).exists()) self.assertFalse(User.objects.filter(username=self.user.username).exists())

View File

@ -4,9 +4,9 @@ from unittest.mock import patch
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK
@ -19,13 +19,9 @@ class TestUserLoginStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = Flow.objects.create( self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-login",
slug="test-login",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = UserLoginStage.objects.create(name="login") self.stage = UserLoginStage.objects.create(name="login")
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2) self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)

View File

@ -1,9 +1,9 @@
"""logout tests""" """logout tests"""
from django.urls import reverse from django.urls import reverse
from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.markers import StageMarker from authentik.flows.markers import StageMarker
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
from authentik.flows.tests import FlowTestCase from authentik.flows.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -17,13 +17,9 @@ class TestUserLogoutStage(FlowTestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org") self.user = create_test_admin_user()
self.flow = Flow.objects.create( self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
name="test-logout",
slug="test-logout",
designation=FlowDesignation.AUTHENTICATION,
)
self.stage = UserLogoutStage.objects.create(name="logout") self.stage = UserLogoutStage.objects.create(name="logout")
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2) self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)

View File

@ -47,7 +47,7 @@ class TestUserWriteStage(FlowTestCase):
plan.context[PLAN_CONTEXT_PROMPT] = { plan.context[PLAN_CONTEXT_PROMPT] = {
"username": "test-user", "username": "test-user",
"name": "name", "name": "name",
"email": "test@beryju.org", "email": "test@goauthentik.io",
"password": password, "password": password,
} }
plan.context[PLAN_CONTEXT_GROUPS] = [self.other_group] plan.context[PLAN_CONTEXT_GROUPS] = [self.other_group]
@ -75,7 +75,7 @@ class TestUserWriteStage(FlowTestCase):
) )
plan = FlowPlan(flow_pk=self.flow.pk.hex, bindings=[self.binding], markers=[StageMarker()]) plan = FlowPlan(flow_pk=self.flow.pk.hex, bindings=[self.binding], markers=[StageMarker()])
plan.context[PLAN_CONTEXT_PENDING_USER] = User.objects.create( plan.context[PLAN_CONTEXT_PENDING_USER] = User.objects.create(
username="unittest", email="test@beryju.org" username="unittest", email="test@goauthentik.io"
) )
plan.context[PLAN_CONTEXT_PROMPT] = { plan.context[PLAN_CONTEXT_PROMPT] = {
"username": "test-user-new", "username": "test-user-new",