From 6c87ec0ce3203960ea9d43b6fd8b79704bb6d2f1 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Thu, 12 Jun 2025 14:28:13 +0200 Subject: [PATCH 1/4] move patched ct to root Signed-off-by: Jens Langhammer --- authentik/outposts/tests/test_ws.py | 10 +--------- authentik/root/tests.py | 12 ++++++++++++ tests/e2e/test_provider_ldap.py | 2 +- tests/e2e/test_provider_proxy.py | 2 +- tests/e2e/test_provider_proxy_forward.py | 2 +- tests/e2e/test_provider_radius.py | 2 +- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/authentik/outposts/tests/test_ws.py b/authentik/outposts/tests/test_ws.py index 76357f30df1f..95cf9216f26b 100644 --- a/authentik/outposts/tests/test_ws.py +++ b/authentik/outposts/tests/test_ws.py @@ -1,12 +1,9 @@ """Websocket tests""" from dataclasses import asdict -from unittest.mock import patch from channels.routing import URLRouter from channels.testing import WebsocketCommunicator -from django.contrib.contenttypes.models import ContentType -from django.test import TransactionTestCase from authentik import __version__ from authentik.core.tests.utils import create_test_flow @@ -14,14 +11,9 @@ from authentik.outposts.models import Outpost, OutpostType from authentik.providers.proxy.models import ProxyProvider from authentik.root import websocket +from authentik.root.tests import TransactionTestCase -def patched__get_ct_cached(app_label, codename): - """Caches `ContentType` instances like its `QuerySet` does.""" - return ContentType.objects.get(app_label=app_label, permission__codename=codename) - - -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) class TestOutpostWS(TransactionTestCase): """Websocket tests""" diff --git a/authentik/root/tests.py b/authentik/root/tests.py index 175caa8d994c..aaf9b169acb7 100644 --- a/authentik/root/tests.py +++ b/authentik/root/tests.py @@ -3,11 +3,23 @@ from pathlib import Path from secrets import token_urlsafe from tempfile import gettempdir +from unittest.mock import patch +from django.contrib.contenttypes.models import ContentType from django.test import TestCase +from django.test import TransactionTestCase as BaseTransactionTestCase from django.urls import reverse +def patched__get_ct_cached(app_label, codename): + """Caches `ContentType` instances like its `QuerySet` does.""" + return ContentType.objects.get(app_label=app_label, permission__codename=codename) + + +@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) +class TransactionTestCase(BaseTransactionTestCase): ... + + class TestRoot(TestCase): """Test root application""" diff --git a/tests/e2e/test_provider_ldap.py b/tests/e2e/test_provider_ldap.py index 06ea7f96859a..07db115b92ed 100644 --- a/tests/e2e/test_provider_ldap.py +++ b/tests/e2e/test_provider_ldap.py @@ -16,8 +16,8 @@ from authentik.lib.generators import generate_id from authentik.outposts.apps import MANAGED_OUTPOST from authentik.outposts.models import Outpost, OutpostConfig, OutpostType -from authentik.outposts.tests.test_ws import patched__get_ct_cached from authentik.providers.ldap.models import APIAccessMode, LDAPProvider +from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry diff --git a/tests/e2e/test_provider_proxy.py b/tests/e2e/test_provider_proxy.py index a5838c401a30..a4d54641b2aa 100644 --- a/tests/e2e/test_provider_proxy.py +++ b/tests/e2e/test_provider_proxy.py @@ -18,8 +18,8 @@ from authentik.lib.generators import generate_id from authentik.outposts.models import DockerServiceConnection, Outpost, OutpostConfig, OutpostType from authentik.outposts.tasks import outpost_connection_discovery -from authentik.outposts.tests.test_ws import patched__get_ct_cached from authentik.providers.proxy.models import ProxyProvider +from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry diff --git a/tests/e2e/test_provider_proxy_forward.py b/tests/e2e/test_provider_proxy_forward.py index 74336bd842b0..fff6c3f01353 100644 --- a/tests/e2e/test_provider_proxy_forward.py +++ b/tests/e2e/test_provider_proxy_forward.py @@ -13,8 +13,8 @@ from authentik.flows.models import Flow from authentik.lib.generators import generate_id from authentik.outposts.models import Outpost, OutpostType -from authentik.outposts.tests.test_ws import patched__get_ct_cached from authentik.providers.proxy.models import ProxyMode, ProxyProvider +from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry diff --git a/tests/e2e/test_provider_radius.py b/tests/e2e/test_provider_radius.py index 94a73a3bdcda..0afa01e2b32d 100644 --- a/tests/e2e/test_provider_radius.py +++ b/tests/e2e/test_provider_radius.py @@ -13,8 +13,8 @@ from authentik.flows.models import Flow from authentik.lib.generators import generate_id, generate_key from authentik.outposts.models import Outpost, OutpostConfig, OutpostType -from authentik.outposts.tests.test_ws import patched__get_ct_cached from authentik.providers.radius.models import RadiusProvider +from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry From 570ec97457ee5c9e8d1e86069958afec93b02f86 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Thu, 12 Jun 2025 14:29:37 +0200 Subject: [PATCH 2/4] use our transaction test case as base Signed-off-by: Jens Langhammer --- authentik/blueprints/tests/test_oci.py | 2 +- authentik/blueprints/tests/test_packaged.py | 3 +-- authentik/blueprints/tests/test_v1.py | 3 +-- authentik/blueprints/tests/test_v1_conditional_fields.py | 3 +-- authentik/blueprints/tests/test_v1_conditions.py | 3 +-- authentik/blueprints/tests/test_v1_rbac.py | 2 +- authentik/blueprints/tests/test_v1_state.py | 3 +-- authentik/blueprints/tests/test_v1_tasks.py | 2 +- authentik/providers/rac/tests/test_models.py | 3 +-- 9 files changed, 9 insertions(+), 15 deletions(-) diff --git a/authentik/blueprints/tests/test_oci.py b/authentik/blueprints/tests/test_oci.py index 71b0d3480788..fcadc1235db0 100644 --- a/authentik/blueprints/tests/test_oci.py +++ b/authentik/blueprints/tests/test_oci.py @@ -1,10 +1,10 @@ """Test blueprints OCI""" -from django.test import TransactionTestCase from requests_mock import Mocker from authentik.blueprints.models import BlueprintInstance, BlueprintRetrievalFailed from authentik.blueprints.v1.oci import OCI_MEDIA_TYPE +from authentik.root.tests import TransactionTestCase class TestBlueprintOCI(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_packaged.py b/authentik/blueprints/tests/test_packaged.py index 32d392447f72..436874ca7592 100644 --- a/authentik/blueprints/tests/test_packaged.py +++ b/authentik/blueprints/tests/test_packaged.py @@ -3,12 +3,11 @@ from collections.abc import Callable from pathlib import Path -from django.test import TransactionTestCase - from authentik.blueprints.models import BlueprintInstance from authentik.blueprints.tests import apply_blueprint from authentik.blueprints.v1.importer import Importer from authentik.brands.models import Brand +from authentik.root.tests import TransactionTestCase class TestPackaged(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1.py b/authentik/blueprints/tests/test_v1.py index a001ba21008f..40795acac189 100644 --- a/authentik/blueprints/tests/test_v1.py +++ b/authentik/blueprints/tests/test_v1.py @@ -2,8 +2,6 @@ from os import environ -from django.test import TransactionTestCase - from authentik.blueprints.v1.exporter import FlowExporter from authentik.blueprints.v1.importer import Importer, transaction_rollback from authentik.core.models import Group @@ -12,6 +10,7 @@ from authentik.lib.tests.utils import load_fixture from authentik.policies.expression.models import ExpressionPolicy from authentik.policies.models import PolicyBinding +from authentik.root.tests import TransactionTestCase from authentik.sources.oauth.models import OAuthSource from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage from authentik.stages.user_login.models import UserLoginStage diff --git a/authentik/blueprints/tests/test_v1_conditional_fields.py b/authentik/blueprints/tests/test_v1_conditional_fields.py index d260e9599b32..b92ab79f88af 100644 --- a/authentik/blueprints/tests/test_v1_conditional_fields.py +++ b/authentik/blueprints/tests/test_v1_conditional_fields.py @@ -1,13 +1,12 @@ """Test blueprints v1""" -from django.test import TransactionTestCase - from authentik.blueprints.v1.importer import Importer from authentik.core.models import Application, Token, User from authentik.core.tests.utils import create_test_admin_user from authentik.flows.models import Flow from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture +from authentik.root.tests import TransactionTestCase from authentik.sources.oauth.models import OAuthSource diff --git a/authentik/blueprints/tests/test_v1_conditions.py b/authentik/blueprints/tests/test_v1_conditions.py index 8dd79563e1fe..763fc04094d6 100644 --- a/authentik/blueprints/tests/test_v1_conditions.py +++ b/authentik/blueprints/tests/test_v1_conditions.py @@ -1,11 +1,10 @@ """Test blueprints v1""" -from django.test import TransactionTestCase - from authentik.blueprints.v1.importer import Importer from authentik.flows.models import Flow from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture +from authentik.root.tests import TransactionTestCase class TestBlueprintsV1Conditions(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1_rbac.py b/authentik/blueprints/tests/test_v1_rbac.py index 87784a7e95c1..148fe905fd13 100644 --- a/authentik/blueprints/tests/test_v1_rbac.py +++ b/authentik/blueprints/tests/test_v1_rbac.py @@ -1,6 +1,5 @@ """Test blueprints v1""" -from django.test import TransactionTestCase from guardian.shortcuts import get_perms from authentik.blueprints.v1.importer import Importer @@ -9,6 +8,7 @@ from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture from authentik.rbac.models import Role +from authentik.root.tests import TransactionTestCase class TestBlueprintsV1RBAC(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1_state.py b/authentik/blueprints/tests/test_v1_state.py index 96a2d0f60239..0d41daead0df 100644 --- a/authentik/blueprints/tests/test_v1_state.py +++ b/authentik/blueprints/tests/test_v1_state.py @@ -1,11 +1,10 @@ """Test blueprints v1""" -from django.test import TransactionTestCase - from authentik.blueprints.v1.importer import Importer from authentik.flows.models import Flow from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture +from authentik.root.tests import TransactionTestCase class TestBlueprintsV1State(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1_tasks.py b/authentik/blueprints/tests/test_v1_tasks.py index b1d201419d63..231d022166e1 100644 --- a/authentik/blueprints/tests/test_v1_tasks.py +++ b/authentik/blueprints/tests/test_v1_tasks.py @@ -3,13 +3,13 @@ from hashlib import sha512 from tempfile import NamedTemporaryFile, mkdtemp -from django.test import TransactionTestCase from yaml import dump from authentik.blueprints.models import BlueprintInstance, BlueprintInstanceStatus from authentik.blueprints.v1.tasks import apply_blueprint, blueprints_discovery, blueprints_find from authentik.lib.config import CONFIG from authentik.lib.generators import generate_id +from authentik.root.tests import TransactionTestCase TMP = mkdtemp("authentik-blueprints") diff --git a/authentik/providers/rac/tests/test_models.py b/authentik/providers/rac/tests/test_models.py index f18f89e15d41..64ded20cc3bd 100644 --- a/authentik/providers/rac/tests/test_models.py +++ b/authentik/providers/rac/tests/test_models.py @@ -1,7 +1,5 @@ """Test RAC Models""" -from django.test import TransactionTestCase - from authentik.core.models import Application, AuthenticatedSession, Session from authentik.core.tests.utils import create_test_admin_user from authentik.lib.generators import generate_id @@ -12,6 +10,7 @@ RACPropertyMapping, RACProvider, ) +from authentik.root.tests import TransactionTestCase class TestModels(TransactionTestCase): From 92948d0dd59093c7192b5834b45a5d8291dc524e Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Thu, 12 Jun 2025 20:13:20 +0200 Subject: [PATCH 3/4] fix...? Signed-off-by: Jens Langhammer --- authentik/root/test_runner.py | 12 +++++++++++- authentik/root/tests.py | 5 +++++ authentik/tenants/tests/utils.py | 3 ++- tests/e2e/utils.py | 13 +------------ 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/authentik/root/test_runner.py b/authentik/root/test_runner.py index a46e207f8e3c..3716732dc745 100644 --- a/authentik/root/test_runner.py +++ b/authentik/root/test_runner.py @@ -12,12 +12,22 @@ from authentik.lib.config import CONFIG from authentik.lib.sentry import sentry_init from authentik.root.signals import post_startup, pre_startup, startup -from tests.e2e.utils import get_docker_tag # globally set maxDiff to none to show full assert error TestCase.maxDiff = None +def get_docker_tag() -> str: + """Get docker-tag based off of CI variables""" + env_pr_branch = "GITHUB_HEAD_REF" + default_branch = "GITHUB_REF" + branch_name = os.environ.get(default_branch, "main") + if os.environ.get(env_pr_branch, "") != "": + branch_name = os.environ[env_pr_branch] + branch_name = branch_name.replace("refs/heads/", "").replace("/", "-") + return f"gh-{branch_name}" + + class PytestTestRunner(DiscoverRunner): # pragma: no cover """Runs pytest to discover and run tests.""" diff --git a/authentik/root/tests.py b/authentik/root/tests.py index aaf9b169acb7..92cbc2682b3f 100644 --- a/authentik/root/tests.py +++ b/authentik/root/tests.py @@ -9,6 +9,7 @@ from django.test import TestCase from django.test import TransactionTestCase as BaseTransactionTestCase from django.urls import reverse +from rest_framework.test import APITransactionTestCase as BaseAPITransactionTestCase def patched__get_ct_cached(app_label, codename): @@ -20,6 +21,10 @@ def patched__get_ct_cached(app_label, codename): class TransactionTestCase(BaseTransactionTestCase): ... +@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) +class APITransactionTestCase(BaseAPITransactionTestCase): ... + + class TestRoot(TestCase): """Test root application""" diff --git a/authentik/tenants/tests/utils.py b/authentik/tenants/tests/utils.py index 5f6a568ed09c..d4d66cb3ddb9 100644 --- a/authentik/tenants/tests/utils.py +++ b/authentik/tenants/tests/utils.py @@ -1,6 +1,7 @@ from django.core.management import call_command from django.db import connection, connections -from rest_framework.test import APITransactionTestCase + +from authentik.root.tests import APITransactionTestCase class TenantAPITestCase(APITransactionTestCase): diff --git a/tests/e2e/utils.py b/tests/e2e/utils.py index 90db46e3711f..c281781cdeab 100644 --- a/tests/e2e/utils.py +++ b/tests/e2e/utils.py @@ -1,7 +1,6 @@ """authentik e2e testing utilities""" import json -import os import socket from collections.abc import Callable from functools import lru_cache, wraps @@ -37,22 +36,12 @@ from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user from authentik.lib.generators import generate_id +from authentik.root.test_runner import get_docker_tag IS_CI = "CI" in environ RETRIES = int(environ.get("RETRIES", "3")) if IS_CI else 1 -def get_docker_tag() -> str: - """Get docker-tag based off of CI variables""" - env_pr_branch = "GITHUB_HEAD_REF" - default_branch = "GITHUB_REF" - branch_name = os.environ.get(default_branch, "main") - if os.environ.get(env_pr_branch, "") != "": - branch_name = os.environ[env_pr_branch] - branch_name = branch_name.replace("refs/heads/", "").replace("/", "-") - return f"gh-{branch_name}" - - def get_local_ip() -> str: """Get the local machine's IP""" hostname = socket.gethostname() From 473b3428850147afbaa187e02098450e334fd737 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 13 Jun 2025 01:16:50 +0200 Subject: [PATCH 4/4] well apparently that works Signed-off-by: Jens Langhammer --- authentik/blueprints/tests/test_oci.py | 2 +- authentik/blueprints/tests/test_packaged.py | 3 ++- authentik/blueprints/tests/test_v1.py | 3 ++- .../tests/test_v1_conditional_fields.py | 3 ++- .../blueprints/tests/test_v1_conditions.py | 3 ++- authentik/blueprints/tests/test_v1_rbac.py | 2 +- authentik/blueprints/tests/test_v1_state.py | 3 ++- authentik/blueprints/tests/test_v1_tasks.py | 2 +- authentik/outposts/tests/test_ws.py | 2 +- authentik/providers/rac/tests/test_models.py | 3 ++- authentik/root/test_runner.py | 18 +++++++++++++----- authentik/root/tests.py | 17 ----------------- authentik/tenants/tests/utils.py | 3 +-- tests/e2e/test_provider_ldap.py | 3 --- tests/e2e/test_provider_proxy.py | 3 --- tests/e2e/test_provider_proxy_forward.py | 3 --- tests/e2e/test_provider_radius.py | 3 --- 17 files changed, 30 insertions(+), 46 deletions(-) diff --git a/authentik/blueprints/tests/test_oci.py b/authentik/blueprints/tests/test_oci.py index fcadc1235db0..71b0d3480788 100644 --- a/authentik/blueprints/tests/test_oci.py +++ b/authentik/blueprints/tests/test_oci.py @@ -1,10 +1,10 @@ """Test blueprints OCI""" +from django.test import TransactionTestCase from requests_mock import Mocker from authentik.blueprints.models import BlueprintInstance, BlueprintRetrievalFailed from authentik.blueprints.v1.oci import OCI_MEDIA_TYPE -from authentik.root.tests import TransactionTestCase class TestBlueprintOCI(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_packaged.py b/authentik/blueprints/tests/test_packaged.py index 436874ca7592..32d392447f72 100644 --- a/authentik/blueprints/tests/test_packaged.py +++ b/authentik/blueprints/tests/test_packaged.py @@ -3,11 +3,12 @@ from collections.abc import Callable from pathlib import Path +from django.test import TransactionTestCase + from authentik.blueprints.models import BlueprintInstance from authentik.blueprints.tests import apply_blueprint from authentik.blueprints.v1.importer import Importer from authentik.brands.models import Brand -from authentik.root.tests import TransactionTestCase class TestPackaged(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1.py b/authentik/blueprints/tests/test_v1.py index 40795acac189..a001ba21008f 100644 --- a/authentik/blueprints/tests/test_v1.py +++ b/authentik/blueprints/tests/test_v1.py @@ -2,6 +2,8 @@ from os import environ +from django.test import TransactionTestCase + from authentik.blueprints.v1.exporter import FlowExporter from authentik.blueprints.v1.importer import Importer, transaction_rollback from authentik.core.models import Group @@ -10,7 +12,6 @@ from authentik.lib.tests.utils import load_fixture from authentik.policies.expression.models import ExpressionPolicy from authentik.policies.models import PolicyBinding -from authentik.root.tests import TransactionTestCase from authentik.sources.oauth.models import OAuthSource from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage from authentik.stages.user_login.models import UserLoginStage diff --git a/authentik/blueprints/tests/test_v1_conditional_fields.py b/authentik/blueprints/tests/test_v1_conditional_fields.py index b92ab79f88af..d260e9599b32 100644 --- a/authentik/blueprints/tests/test_v1_conditional_fields.py +++ b/authentik/blueprints/tests/test_v1_conditional_fields.py @@ -1,12 +1,13 @@ """Test blueprints v1""" +from django.test import TransactionTestCase + from authentik.blueprints.v1.importer import Importer from authentik.core.models import Application, Token, User from authentik.core.tests.utils import create_test_admin_user from authentik.flows.models import Flow from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture -from authentik.root.tests import TransactionTestCase from authentik.sources.oauth.models import OAuthSource diff --git a/authentik/blueprints/tests/test_v1_conditions.py b/authentik/blueprints/tests/test_v1_conditions.py index 763fc04094d6..8dd79563e1fe 100644 --- a/authentik/blueprints/tests/test_v1_conditions.py +++ b/authentik/blueprints/tests/test_v1_conditions.py @@ -1,10 +1,11 @@ """Test blueprints v1""" +from django.test import TransactionTestCase + from authentik.blueprints.v1.importer import Importer from authentik.flows.models import Flow from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture -from authentik.root.tests import TransactionTestCase class TestBlueprintsV1Conditions(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1_rbac.py b/authentik/blueprints/tests/test_v1_rbac.py index 148fe905fd13..87784a7e95c1 100644 --- a/authentik/blueprints/tests/test_v1_rbac.py +++ b/authentik/blueprints/tests/test_v1_rbac.py @@ -1,5 +1,6 @@ """Test blueprints v1""" +from django.test import TransactionTestCase from guardian.shortcuts import get_perms from authentik.blueprints.v1.importer import Importer @@ -8,7 +9,6 @@ from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture from authentik.rbac.models import Role -from authentik.root.tests import TransactionTestCase class TestBlueprintsV1RBAC(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1_state.py b/authentik/blueprints/tests/test_v1_state.py index 0d41daead0df..96a2d0f60239 100644 --- a/authentik/blueprints/tests/test_v1_state.py +++ b/authentik/blueprints/tests/test_v1_state.py @@ -1,10 +1,11 @@ """Test blueprints v1""" +from django.test import TransactionTestCase + from authentik.blueprints.v1.importer import Importer from authentik.flows.models import Flow from authentik.lib.generators import generate_id from authentik.lib.tests.utils import load_fixture -from authentik.root.tests import TransactionTestCase class TestBlueprintsV1State(TransactionTestCase): diff --git a/authentik/blueprints/tests/test_v1_tasks.py b/authentik/blueprints/tests/test_v1_tasks.py index 231d022166e1..b1d201419d63 100644 --- a/authentik/blueprints/tests/test_v1_tasks.py +++ b/authentik/blueprints/tests/test_v1_tasks.py @@ -3,13 +3,13 @@ from hashlib import sha512 from tempfile import NamedTemporaryFile, mkdtemp +from django.test import TransactionTestCase from yaml import dump from authentik.blueprints.models import BlueprintInstance, BlueprintInstanceStatus from authentik.blueprints.v1.tasks import apply_blueprint, blueprints_discovery, blueprints_find from authentik.lib.config import CONFIG from authentik.lib.generators import generate_id -from authentik.root.tests import TransactionTestCase TMP = mkdtemp("authentik-blueprints") diff --git a/authentik/outposts/tests/test_ws.py b/authentik/outposts/tests/test_ws.py index 95cf9216f26b..b15e415c7caf 100644 --- a/authentik/outposts/tests/test_ws.py +++ b/authentik/outposts/tests/test_ws.py @@ -4,6 +4,7 @@ from channels.routing import URLRouter from channels.testing import WebsocketCommunicator +from django.test import TransactionTestCase from authentik import __version__ from authentik.core.tests.utils import create_test_flow @@ -11,7 +12,6 @@ from authentik.outposts.models import Outpost, OutpostType from authentik.providers.proxy.models import ProxyProvider from authentik.root import websocket -from authentik.root.tests import TransactionTestCase class TestOutpostWS(TransactionTestCase): diff --git a/authentik/providers/rac/tests/test_models.py b/authentik/providers/rac/tests/test_models.py index 64ded20cc3bd..f18f89e15d41 100644 --- a/authentik/providers/rac/tests/test_models.py +++ b/authentik/providers/rac/tests/test_models.py @@ -1,5 +1,7 @@ """Test RAC Models""" +from django.test import TransactionTestCase + from authentik.core.models import Application, AuthenticatedSession, Session from authentik.core.tests.utils import create_test_admin_user from authentik.lib.generators import generate_id @@ -10,7 +12,6 @@ RACPropertyMapping, RACProvider, ) -from authentik.root.tests import TransactionTestCase class TestModels(TransactionTestCase): diff --git a/authentik/root/test_runner.py b/authentik/root/test_runner.py index 3716732dc745..04b70a8bd957 100644 --- a/authentik/root/test_runner.py +++ b/authentik/root/test_runner.py @@ -3,9 +3,11 @@ import os from argparse import ArgumentParser from unittest import TestCase +from unittest.mock import patch import pytest from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.test.runner import DiscoverRunner from structlog.stdlib import get_logger @@ -28,6 +30,11 @@ def get_docker_tag() -> str: return f"gh-{branch_name}" +def patched__get_ct_cached(app_label, codename): + """Caches `ContentType` instances like its `QuerySet` does.""" + return ContentType.objects.get(app_label=app_label, permission__codename=codename) + + class PytestTestRunner(DiscoverRunner): # pragma: no cover """Runs pytest to discover and run tests.""" @@ -159,8 +166,9 @@ def run_tests(self, test_labels: list[str], extra_tests=None, **kwargs): return 1 self.logger.info("Running tests", test_files=self.args) - try: - return pytest.main(self.args) - except Exception as e: - self.logger.error("Error running tests", error=str(e), test_files=self.args) - return 1 + with patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached): + try: + return pytest.main(self.args) + except Exception as e: + self.logger.error("Error running tests", error=str(e), test_files=self.args) + return 1 diff --git a/authentik/root/tests.py b/authentik/root/tests.py index 92cbc2682b3f..175caa8d994c 100644 --- a/authentik/root/tests.py +++ b/authentik/root/tests.py @@ -3,26 +3,9 @@ from pathlib import Path from secrets import token_urlsafe from tempfile import gettempdir -from unittest.mock import patch -from django.contrib.contenttypes.models import ContentType from django.test import TestCase -from django.test import TransactionTestCase as BaseTransactionTestCase from django.urls import reverse -from rest_framework.test import APITransactionTestCase as BaseAPITransactionTestCase - - -def patched__get_ct_cached(app_label, codename): - """Caches `ContentType` instances like its `QuerySet` does.""" - return ContentType.objects.get(app_label=app_label, permission__codename=codename) - - -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) -class TransactionTestCase(BaseTransactionTestCase): ... - - -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) -class APITransactionTestCase(BaseAPITransactionTestCase): ... class TestRoot(TestCase): diff --git a/authentik/tenants/tests/utils.py b/authentik/tenants/tests/utils.py index d4d66cb3ddb9..5f6a568ed09c 100644 --- a/authentik/tenants/tests/utils.py +++ b/authentik/tenants/tests/utils.py @@ -1,7 +1,6 @@ from django.core.management import call_command from django.db import connection, connections - -from authentik.root.tests import APITransactionTestCase +from rest_framework.test import APITransactionTestCase class TenantAPITestCase(APITransactionTestCase): diff --git a/tests/e2e/test_provider_ldap.py b/tests/e2e/test_provider_ldap.py index 07db115b92ed..4eb163797c7a 100644 --- a/tests/e2e/test_provider_ldap.py +++ b/tests/e2e/test_provider_ldap.py @@ -2,7 +2,6 @@ from dataclasses import asdict from time import sleep -from unittest.mock import patch from guardian.shortcuts import assign_perm from ldap3 import ALL, ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES, SUBTREE, Connection, Server @@ -17,11 +16,9 @@ from authentik.outposts.apps import MANAGED_OUTPOST from authentik.outposts.models import Outpost, OutpostConfig, OutpostType from authentik.providers.ldap.models import APIAccessMode, LDAPProvider -from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) class TestProviderLDAP(SeleniumTestCase): """LDAP and Outpost e2e tests""" diff --git a/tests/e2e/test_provider_proxy.py b/tests/e2e/test_provider_proxy.py index a4d54641b2aa..0ac9300e6fa2 100644 --- a/tests/e2e/test_provider_proxy.py +++ b/tests/e2e/test_provider_proxy.py @@ -6,7 +6,6 @@ from sys import platform from time import sleep from unittest.case import skip, skipUnless -from unittest.mock import patch from channels.testing import ChannelsLiveServerTestCase from jwt import decode @@ -19,11 +18,9 @@ from authentik.outposts.models import DockerServiceConnection, Outpost, OutpostConfig, OutpostType from authentik.outposts.tasks import outpost_connection_discovery from authentik.providers.proxy.models import ProxyProvider -from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) class TestProviderProxy(SeleniumTestCase): """Proxy and Outpost e2e tests""" diff --git a/tests/e2e/test_provider_proxy_forward.py b/tests/e2e/test_provider_proxy_forward.py index fff6c3f01353..8060d8c520cc 100644 --- a/tests/e2e/test_provider_proxy_forward.py +++ b/tests/e2e/test_provider_proxy_forward.py @@ -4,7 +4,6 @@ from pathlib import Path from time import sleep from unittest import skip -from unittest.mock import patch from selenium.webdriver.common.by import By @@ -14,11 +13,9 @@ from authentik.lib.generators import generate_id from authentik.outposts.models import Outpost, OutpostType from authentik.providers.proxy.models import ProxyMode, ProxyProvider -from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) class TestProviderProxyForward(SeleniumTestCase): """Proxy and Outpost e2e tests""" diff --git a/tests/e2e/test_provider_radius.py b/tests/e2e/test_provider_radius.py index 0afa01e2b32d..0392a7737266 100644 --- a/tests/e2e/test_provider_radius.py +++ b/tests/e2e/test_provider_radius.py @@ -2,7 +2,6 @@ from dataclasses import asdict from time import sleep -from unittest.mock import patch from pyrad.client import Client from pyrad.dictionary import Dictionary @@ -14,11 +13,9 @@ from authentik.lib.generators import generate_id, generate_key from authentik.outposts.models import Outpost, OutpostConfig, OutpostType from authentik.providers.radius.models import RadiusProvider -from authentik.root.tests import patched__get_ct_cached from tests.e2e.utils import SeleniumTestCase, retry -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) class TestProviderRadius(SeleniumTestCase): """Radius Outpost e2e tests"""