diff --git a/authentik/flows/tests/test_views.py b/authentik/flows/tests/test_views.py index c3bb838c2..74cb48c69 100644 --- a/authentik/flows/tests/test_views.py +++ b/authentik/flows/tests/test_views.py @@ -2,10 +2,10 @@ from unittest.mock import MagicMock, PropertyMock, patch from django.http import HttpRequest, HttpResponse -from django.test import TestCase from django.test.client import RequestFactory from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -37,7 +37,7 @@ def to_stage_response(request: HttpRequest, source: HttpResponse): TO_STAGE_RESPONSE_MOCK = MagicMock(side_effect=to_stage_response) -class TestFlowExecutor(TestCase): +class TestFlowExecutor(APITestCase): """Test views logic""" def setUp(self): diff --git a/authentik/flows/tests/test_views_helper.py b/authentik/flows/tests/test_views_helper.py index 5e04b5232..235cf49df 100644 --- a/authentik/flows/tests/test_views_helper.py +++ b/authentik/flows/tests/test_views_helper.py @@ -1,5 +1,5 @@ """flow views tests""" -from django.test import Client, TestCase +from django.test import TestCase from django.urls import reverse from authentik.flows.models import Flow, FlowDesignation @@ -10,9 +10,6 @@ from authentik.flows.views import SESSION_KEY_PLAN class TestHelperView(TestCase): """Test helper views logic""" - def setUp(self): - self.client = Client() - def test_default_view(self): """Test that ToDefaultFlow returns the expected URL""" flow = Flow.objects.filter( diff --git a/authentik/policies/password/tests/test_flows.py b/authentik/policies/password/tests/test_flows.py index 18b686f3e..e84b06c0a 100644 --- a/authentik/policies/password/tests/test_flows.py +++ b/authentik/policies/password/tests/test_flows.py @@ -1,7 +1,7 @@ """Password flow tests""" -from django.test import TestCase from django.urls.base import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -10,7 +10,7 @@ from authentik.policies.password.models import PasswordPolicy from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage -class TestPasswordPolicyFlow(TestCase): +class TestPasswordPolicyFlow(APITestCase): """Test Password Policy""" def setUp(self) -> None: diff --git a/authentik/root/settings.py b/authentik/root/settings.py index 7caabfa65..27f70de06 100644 --- a/authentik/root/settings.py +++ b/authentik/root/settings.py @@ -191,8 +191,8 @@ REST_FRAMEWORK = { "rest_framework.filters.OrderingFilter", "rest_framework.filters.SearchFilter", ], - 'DEFAULT_PARSER_CLASSES': [ - 'rest_framework.parsers.JSONParser', + "DEFAULT_PARSER_CLASSES": [ + "rest_framework.parsers.JSONParser", ], "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.DjangoObjectPermissions",), "DEFAULT_AUTHENTICATION_CLASSES": ( @@ -203,6 +203,7 @@ REST_FRAMEWORK = { "rest_framework.renderers.JSONRenderer", ], "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", + "TEST_REQUEST_DEFAULT_FORMAT": "json", } REDIS_PROTOCOL_PREFIX = "redis://" diff --git a/authentik/root/tests.py b/authentik/root/tests.py index 9824a03c6..4ed4f9532 100644 --- a/authentik/root/tests.py +++ b/authentik/root/tests.py @@ -2,17 +2,13 @@ from base64 import b64encode from django.conf import settings -from django.test import Client, TestCase +from django.test import TestCase from django.urls import reverse class TestRoot(TestCase): """Test root application""" - def setUp(self): - super().setUp() - self.client = Client() - def test_monitoring_error(self): """Test monitoring without any credentials""" response = self.client.get(reverse("metrics")) diff --git a/authentik/sources/oauth/tests/test_type_twitter.py b/authentik/sources/oauth/tests/test_type_twitter.py index 2d8944d98..b950c4d84 100644 --- a/authentik/sources/oauth/tests/test_type_twitter.py +++ b/authentik/sources/oauth/tests/test_type_twitter.py @@ -1,5 +1,5 @@ """Twitter Type tests""" -from django.test import Client, TestCase +from django.test import TestCase from authentik.sources.oauth.models import OAuthSource from authentik.sources.oauth.types.twitter import TwitterOAuthCallback @@ -92,7 +92,6 @@ class TestTypeGitHub(TestCase): """OAuth Source tests""" def setUp(self): - self.client = Client() self.source = OAuthSource.objects.create( name="test", slug="test", diff --git a/authentik/stages/authenticator_validate/tests.py b/authentik/stages/authenticator_validate/tests.py index 6eacb525a..27b2cca24 100644 --- a/authentik/stages/authenticator_validate/tests.py +++ b/authentik/stages/authenticator_validate/tests.py @@ -1,12 +1,12 @@ """Test validator stage""" from unittest.mock import MagicMock, patch -from django.test import TestCase from django.test.client import RequestFactory from django.urls.base import reverse from django.utils.encoding import force_str from django_otp.plugins.otp_totp.models import TOTPDevice from rest_framework.exceptions import ValidationError +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -26,7 +26,7 @@ from authentik.stages.authenticator_webauthn.models import WebAuthnDevice from authentik.stages.identification.models import IdentificationStage, UserFields -class AuthenticatorValidateStageTests(TestCase): +class AuthenticatorValidateStageTests(APITestCase): """Test validator stage""" def setUp(self) -> None: diff --git a/authentik/stages/captcha/tests.py b/authentik/stages/captcha/tests.py index b9cebb8c1..21673dfd7 100644 --- a/authentik/stages/captcha/tests.py +++ b/authentik/stages/captcha/tests.py @@ -1,7 +1,7 @@ """captcha tests""" -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -16,13 +16,12 @@ RECAPTCHA_PUBLIC_KEY = "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" RECAPTCHA_PRIVATE_KEY = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe" -class TestCaptchaStage(TestCase): +class TestCaptchaStage(APITestCase): """Captcha tests""" def setUp(self): super().setUp() self.user = User.objects.create_user(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-captcha", diff --git a/authentik/stages/consent/tests.py b/authentik/stages/consent/tests.py index c73b11c28..dd0d46ea9 100644 --- a/authentik/stages/consent/tests.py +++ b/authentik/stages/consent/tests.py @@ -1,9 +1,9 @@ """consent tests""" from time import sleep -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import Application, User from authentik.core.tasks import clean_expired_models @@ -15,7 +15,7 @@ from authentik.flows.views import SESSION_KEY_PLAN from authentik.stages.consent.models import ConsentMode, ConsentStage, UserConsent -class TestConsentStage(TestCase): +class TestConsentStage(APITestCase): """Consent tests""" def setUp(self): @@ -25,7 +25,6 @@ class TestConsentStage(TestCase): name="test-application", slug="test-application", ) - self.client = Client() def test_always_required(self): """Test always required consent""" diff --git a/authentik/stages/deny/tests.py b/authentik/stages/deny/tests.py index 7713eaf62..d2d0c072f 100644 --- a/authentik/stages/deny/tests.py +++ b/authentik/stages/deny/tests.py @@ -1,7 +1,7 @@ """deny tests""" -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -12,13 +12,12 @@ from authentik.flows.views import SESSION_KEY_PLAN from authentik.stages.deny.models import DenyStage -class TestUserDenyStage(TestCase): +class TestUserDenyStage(APITestCase): """Deny tests""" def setUp(self): super().setUp() self.user = User.objects.create(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-logout", diff --git a/authentik/stages/dummy/tests.py b/authentik/stages/dummy/tests.py index 388366d9b..6a16be491 100644 --- a/authentik/stages/dummy/tests.py +++ b/authentik/stages/dummy/tests.py @@ -1,7 +1,7 @@ """dummy tests""" -from django.test import TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -9,7 +9,7 @@ from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding from authentik.stages.dummy.models import DummyStage -class TestDummyStage(TestCase): +class TestDummyStage(APITestCase): """Dummy tests""" def setUp(self): diff --git a/authentik/stages/email/tests/test_sending.py b/authentik/stages/email/tests/test_sending.py index 8966e5a71..131451a54 100644 --- a/authentik/stages/email/tests/test_sending.py +++ b/authentik/stages/email/tests/test_sending.py @@ -4,8 +4,8 @@ from unittest.mock import MagicMock, patch from django.core import mail from django.core.mail.backends.locmem import EmailBackend -from django.test import Client, TestCase from django.urls import reverse +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.events.models import Event, EventAction @@ -16,13 +16,12 @@ from authentik.flows.views import SESSION_KEY_PLAN from authentik.stages.email.models import EmailStage -class TestEmailStageSending(TestCase): +class TestEmailStageSending(APITestCase): """Email tests""" def setUp(self): super().setUp() self.user = User.objects.create_user(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-email", diff --git a/authentik/stages/email/tests/test_stage.py b/authentik/stages/email/tests/test_stage.py index 0c88c8f5a..c67ce8881 100644 --- a/authentik/stages/email/tests/test_stage.py +++ b/authentik/stages/email/tests/test_stage.py @@ -2,10 +2,10 @@ from unittest.mock import MagicMock, patch from django.core import mail -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str from django.utils.http import urlencode +from rest_framework.test import APITestCase from authentik.core.models import Token, User from authentik.flows.challenge import ChallengeTypes @@ -17,13 +17,12 @@ from authentik.stages.email.models import EmailStage from authentik.stages.email.stage import QS_KEY_TOKEN -class TestEmailStage(TestCase): +class TestEmailStage(APITestCase): """Email tests""" def setUp(self): super().setUp() self.user = User.objects.create_user(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-email", diff --git a/authentik/stages/identification/tests.py b/authentik/stages/identification/tests.py index 5cd8d63cf..3d7569b04 100644 --- a/authentik/stages/identification/tests.py +++ b/authentik/stages/identification/tests.py @@ -1,7 +1,7 @@ """identification tests""" -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -13,7 +13,7 @@ from authentik.stages.password import BACKEND_INBUILT from authentik.stages.password.models import PasswordStage -class TestIdentificationStage(TestCase): +class TestIdentificationStage(APITestCase): """Identification tests""" def setUp(self): @@ -22,7 +22,6 @@ class TestIdentificationStage(TestCase): self.user = User.objects.create_user( username="unittest", email="test@beryju.org", password=self.password ) - self.client = Client() # OAuthSource for the login view source = OAuthSource.objects.create(name="test", slug="test") diff --git a/authentik/stages/invitation/tests.py b/authentik/stages/invitation/tests.py index 6f84dca7b..fa5d2b6f1 100644 --- a/authentik/stages/invitation/tests.py +++ b/authentik/stages/invitation/tests.py @@ -1,7 +1,6 @@ """invitation tests""" from unittest.mock import MagicMock, patch -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str from django.utils.http import urlencode @@ -21,13 +20,12 @@ from authentik.stages.password import BACKEND_INBUILT from authentik.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND -class TestUserLoginStage(TestCase): +class TestUserLoginStage(APITestCase): """Login tests""" def setUp(self): super().setUp() self.user = User.objects.create(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-invitation", diff --git a/authentik/stages/password/tests.py b/authentik/stages/password/tests.py index ebd1e137d..9cb640dda 100644 --- a/authentik/stages/password/tests.py +++ b/authentik/stages/password/tests.py @@ -2,9 +2,9 @@ from unittest.mock import MagicMock, patch from django.core.exceptions import PermissionDenied -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -20,7 +20,7 @@ from authentik.stages.password.models import PasswordStage MOCK_BACKEND_AUTHENTICATE = MagicMock(side_effect=PermissionDenied("test")) -class TestPasswordStage(TestCase): +class TestPasswordStage(APITestCase): """Password tests""" def setUp(self): @@ -29,7 +29,6 @@ class TestPasswordStage(TestCase): self.user = User.objects.create_user( username="unittest", email="test@beryju.org", password=self.password ) - self.client = Client() self.flow = Flow.objects.create( name="test-password", diff --git a/authentik/stages/prompt/tests.py b/authentik/stages/prompt/tests.py index f128513b8..a294e1432 100644 --- a/authentik/stages/prompt/tests.py +++ b/authentik/stages/prompt/tests.py @@ -1,10 +1,10 @@ """Prompt tests""" from unittest.mock import MagicMock, patch -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str from rest_framework.exceptions import ErrorDetail +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -17,13 +17,12 @@ from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage from authentik.stages.prompt.stage import PLAN_CONTEXT_PROMPT, PromptChallengeResponse -class TestPromptStage(TestCase): +class TestPromptStage(APITestCase): """Prompt tests""" def setUp(self): super().setUp() self.user = User.objects.create(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-prompt", diff --git a/authentik/stages/user_delete/tests.py b/authentik/stages/user_delete/tests.py index 544121036..e70b80552 100644 --- a/authentik/stages/user_delete/tests.py +++ b/authentik/stages/user_delete/tests.py @@ -1,9 +1,9 @@ """delete tests""" from unittest.mock import patch -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -15,14 +15,13 @@ from authentik.flows.views import SESSION_KEY_PLAN from authentik.stages.user_delete.models import UserDeleteStage -class TestUserDeleteStage(TestCase): +class TestUserDeleteStage(APITestCase): """Delete tests""" def setUp(self): super().setUp() self.username = "qerqwerqrwqwerwq" self.user = User.objects.create(username=self.username, email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-delete", diff --git a/authentik/stages/user_login/tests.py b/authentik/stages/user_login/tests.py index cc31133b8..0940324a6 100644 --- a/authentik/stages/user_login/tests.py +++ b/authentik/stages/user_login/tests.py @@ -2,9 +2,9 @@ from time import sleep from unittest.mock import patch -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -16,13 +16,12 @@ from authentik.flows.views import SESSION_KEY_PLAN from authentik.stages.user_login.models import UserLoginStage -class TestUserLoginStage(TestCase): +class TestUserLoginStage(APITestCase): """Login tests""" def setUp(self): super().setUp() self.user = User.objects.create(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-login", diff --git a/authentik/stages/user_logout/tests.py b/authentik/stages/user_logout/tests.py index cf2ec16bd..c13e90cba 100644 --- a/authentik/stages/user_logout/tests.py +++ b/authentik/stages/user_logout/tests.py @@ -1,7 +1,7 @@ """logout tests""" -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import User from authentik.flows.challenge import ChallengeTypes @@ -14,13 +14,12 @@ from authentik.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND from authentik.stages.user_logout.models import UserLogoutStage -class TestUserLogoutStage(TestCase): +class TestUserLogoutStage(APITestCase): """Logout tests""" def setUp(self): super().setUp() self.user = User.objects.create(username="unittest", email="test@beryju.org") - self.client = Client() self.flow = Flow.objects.create( name="test-logout", diff --git a/authentik/stages/user_write/tests.py b/authentik/stages/user_write/tests.py index eb9ac2454..75ba7243f 100644 --- a/authentik/stages/user_write/tests.py +++ b/authentik/stages/user_write/tests.py @@ -3,9 +3,9 @@ import string from random import SystemRandom from unittest.mock import patch -from django.test import Client, TestCase from django.urls import reverse from django.utils.encoding import force_str +from rest_framework.test import APITestCase from authentik.core.models import USER_ATTRIBUTE_SOURCES, Source, User, UserSourceConnection from authentik.core.sources.stage import PLAN_CONTEXT_SOURCES_CONNECTION @@ -19,13 +19,11 @@ from authentik.stages.prompt.stage import PLAN_CONTEXT_PROMPT from authentik.stages.user_write.models import UserWriteStage -class TestUserWriteStage(TestCase): +class TestUserWriteStage(APITestCase): """Write tests""" def setUp(self): super().setUp() - self.client = Client() - self.flow = Flow.objects.create( name="test-write", slug="test-write",