*: 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"""
from django.urls.base import reverse
from authentik.core.models import User
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.tests import FlowTestCase
from authentik.policies.password.models import PasswordPolicy
from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage
@ -12,13 +12,9 @@ class TestPasswordPolicyFlow(FlowTestCase):
"""Test Password Policy"""
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(
field_key="password",
label="PASSWORD_LABEL",

View file

@ -1,9 +1,9 @@
"""captcha tests"""
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.models import Flow, FlowDesignation, FlowStageBinding
from authentik.flows.models import FlowDesignation, FlowStageBinding
from authentik.flows.planner import FlowPlan
from authentik.flows.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -19,13 +19,9 @@ class TestCaptchaStage(FlowTestCase):
def setUp(self):
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(
name="captcha",
public_key=RECAPTCHA_PUBLIC_KEY,

View file

@ -3,10 +3,11 @@ from time import sleep
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.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 FlowDesignation, FlowStageBinding
from authentik.flows.planner import PLAN_CONTEXT_APPLICATION, FlowPlan
from authentik.flows.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -18,7 +19,7 @@ class TestConsentStage(FlowTestCase):
def setUp(self):
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(
name="test-application",
slug="test-application",
@ -26,11 +27,7 @@ class TestConsentStage(FlowTestCase):
def test_always_required(self):
"""Test always required consent"""
flow = Flow.objects.create(
name="test-consent",
slug="test-consent",
designation=FlowDesignation.AUTHENTICATION,
)
flow = create_test_flow(FlowDesignation.AUTHENTICATION)
stage = ConsentStage.objects.create(name="consent", mode=ConsentMode.ALWAYS_REQUIRE)
binding = FlowStageBinding.objects.create(target=flow, stage=stage, order=2)
@ -50,11 +47,7 @@ class TestConsentStage(FlowTestCase):
def test_permanent(self):
"""Test permanent consent from user"""
self.client.force_login(self.user)
flow = Flow.objects.create(
name="test-consent",
slug="test-consent",
designation=FlowDesignation.AUTHENTICATION,
)
flow = create_test_flow(FlowDesignation.AUTHENTICATION)
stage = ConsentStage.objects.create(name="consent", mode=ConsentMode.PERMANENT)
binding = FlowStageBinding.objects.create(target=flow, stage=stage, order=2)
@ -80,11 +73,7 @@ class TestConsentStage(FlowTestCase):
def test_expire(self):
"""Test expiring consent from user"""
self.client.force_login(self.user)
flow = Flow.objects.create(
name="test-consent",
slug="test-consent",
designation=FlowDesignation.AUTHENTICATION,
)
flow = create_test_flow(FlowDesignation.AUTHENTICATION)
stage = ConsentStage.objects.create(
name="consent", mode=ConsentMode.EXPIRING, consent_expire_in="seconds=1"
)

View file

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

View file

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

View file

@ -7,10 +7,10 @@ from django.core.mail.backends.locmem import EmailBackend
from django.urls import reverse
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.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.views.executor import SESSION_KEY_PLAN
from authentik.stages.email.models import EmailStage
@ -21,13 +21,9 @@ class TestEmailStageSending(APITestCase):
def setUp(self):
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(
name="test-email",
slug="test-email",
designation=FlowDesignation.AUTHENTICATION,
)
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = EmailStage.objects.create(
name="email",
)
@ -53,9 +49,9 @@ class TestEmailStageSending(APITestCase):
events = Event.objects.filter(action=EventAction.EMAIL_SENT)
self.assertEqual(len(events), 1)
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["to_email"], ["test@beryju.org"])
self.assertEqual(event.context["to_email"], [self.user.email])
self.assertEqual(event.context["from_email"], "system@authentik.local")
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.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.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.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -22,13 +23,9 @@ class TestEmailStage(FlowTestCase):
def setUp(self):
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(
name="test-email",
slug="test-email",
designation=FlowDesignation.AUTHENTICATION,
)
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = EmailStage.objects.create(
name="email",
activate_user_on_success=True,
@ -75,7 +72,7 @@ class TestEmailStage(FlowTestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 1)
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):
"""Test with pending user (override to)"""

View file

@ -1,11 +1,10 @@
"""identification tests"""
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.models import Flow, FlowDesignation, FlowStageBinding
from authentik.flows.tests import FlowTestCase
from authentik.lib.generators import generate_key
from authentik.sources.oauth.models import OAuthSource
from authentik.stages.identification.models import IdentificationStage, UserFields
from authentik.stages.password import BACKEND_INBUILT
@ -17,19 +16,12 @@ class TestIdentificationStage(FlowTestCase):
def setUp(self):
super().setUp()
self.password = generate_key()
self.user = User.objects.create_user(
username="unittest", email="test@beryju.org", password=self.password
)
self.user = create_test_admin_user()
# OAuthSource for the login view
source = OAuthSource.objects.create(name="test", slug="test")
self.flow = Flow.objects.create(
name="test-identification",
slug="test-identification",
designation=FlowDesignation.AUTHENTICATION,
)
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = IdentificationStage.objects.create(
name="identification",
user_fields=[UserFields.E_MAIL],
@ -62,7 +54,7 @@ class TestIdentificationStage(FlowTestCase):
pw_stage = PasswordStage.objects.create(name="password", backends=[BACKEND_INBUILT])
self.stage.password_stage = pw_stage
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})
response = self.client.post(url, form_data)
self.assertEqual(response.status_code, 200)
@ -75,7 +67,7 @@ class TestIdentificationStage(FlowTestCase):
self.stage.save()
form_data = {
"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})
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 rest_framework.test import APITestCase
from authentik.core.models import User
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 FlowDesignation, 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
@ -29,13 +28,8 @@ class TestUserLoginStage(FlowTestCase):
def setUp(self):
super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org")
self.flow = Flow.objects.create(
name="test-invitation",
slug="test-invitation",
designation=FlowDesignation.AUTHENTICATION,
)
self.user = create_test_admin_user()
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = InvitationStage.objects.create(name="invitation")
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.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.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.tests import FlowTestCase
from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK
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.models import PasswordStage
@ -23,16 +22,9 @@ class TestPasswordStage(FlowTestCase):
def setUp(self):
super().setUp()
self.password = generate_key()
self.user = User.objects.create_user(
username="unittest", email="test@beryju.org", password=self.password
)
self.user = create_test_admin_user()
self.flow = Flow.objects.create(
name="test-password",
slug="test-password",
designation=FlowDesignation.AUTHENTICATION,
)
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = PasswordStage.objects.create(name="password", backends=[BACKEND_INBUILT])
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)
@ -50,7 +42,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Still have to send the password so the form is valid
{"password": self.password},
{"password": self.user.username},
)
self.assertStageResponse(
@ -62,7 +54,7 @@ class TestPasswordStage(FlowTestCase):
def test_recovery_flow_link(self):
"""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()])
session = self.client.session
@ -86,7 +78,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data
{"password": self.password},
{"password": self.user.username},
)
self.assertEqual(response.status_code, 200)
@ -103,7 +95,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data
{"password": self.password + "test"},
{"password": self.user.username + "test"},
)
self.assertEqual(response.status_code, 200)
@ -122,14 +114,14 @@ class TestPasswordStage(FlowTestCase):
kwargs={"flow_slug": self.flow.slug},
),
# Form data
{"password": self.password + "test"},
{"password": self.user.username + "test"},
)
self.assertEqual(response.status_code, 200)
response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data
{"password": self.password + "test"},
{"password": self.user.username + "test"},
)
self.assertEqual(response.status_code, 200)
# To ensure the plan has been cancelled, check SESSION_KEY_PLAN
@ -155,7 +147,7 @@ class TestPasswordStage(FlowTestCase):
response = self.client.post(
reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}),
# Form data
{"password": self.password + "test"},
{"password": self.user.username + "test"},
)
self.assertStageResponse(

View file

@ -4,8 +4,9 @@ from unittest.mock import patch
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.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.tests import FlowTestCase
from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK
@ -18,14 +19,9 @@ class TestUserDeleteStage(FlowTestCase):
def setUp(self):
super().setUp()
self.username = "qerqwerqrwqwerwq"
self.user = User.objects.create(username=self.username, email="test@beryju.org")
self.user = create_test_admin_user()
self.flow = Flow.objects.create(
name="test-delete",
slug="test-delete",
designation=FlowDesignation.AUTHENTICATION,
)
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = UserDeleteStage.objects.create(name="delete")
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.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):
"""Test Form render"""
@ -75,4 +71,4 @@ class TestUserDeleteStage(FlowTestCase):
self.assertEqual(response.status_code, 200)
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 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.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.tests import FlowTestCase
from authentik.flows.tests.test_executor import TO_STAGE_RESPONSE_MOCK
@ -19,13 +19,9 @@ class TestUserLoginStage(FlowTestCase):
def setUp(self):
super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org")
self.user = create_test_admin_user()
self.flow = Flow.objects.create(
name="test-login",
slug="test-login",
designation=FlowDesignation.AUTHENTICATION,
)
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = UserLoginStage.objects.create(name="login")
self.binding = FlowStageBinding.objects.create(target=self.flow, stage=self.stage, order=2)

View file

@ -1,9 +1,9 @@
"""logout tests"""
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.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.tests import FlowTestCase
from authentik.flows.views.executor import SESSION_KEY_PLAN
@ -17,13 +17,9 @@ class TestUserLogoutStage(FlowTestCase):
def setUp(self):
super().setUp()
self.user = User.objects.create(username="unittest", email="test@beryju.org")
self.user = create_test_admin_user()
self.flow = Flow.objects.create(
name="test-logout",
slug="test-logout",
designation=FlowDesignation.AUTHENTICATION,
)
self.flow = create_test_flow(FlowDesignation.AUTHENTICATION)
self.stage = UserLogoutStage.objects.create(name="logout")
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] = {
"username": "test-user",
"name": "name",
"email": "test@beryju.org",
"email": "test@goauthentik.io",
"password": password,
}
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.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] = {
"username": "test-user-new",