diff --git a/authentik/outposts/tests/test_ws.py b/authentik/outposts/tests/test_ws.py index 76357f30df..b15e415c7c 100644 --- a/authentik/outposts/tests/test_ws.py +++ b/authentik/outposts/tests/test_ws.py @@ -1,11 +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__ @@ -16,12 +14,6 @@ from authentik.providers.proxy.models import ProxyProvider from authentik.root import websocket -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/test_runner.py b/authentik/root/test_runner.py index a46e207f8e..04b70a8bd9 100644 --- a/authentik/root/test_runner.py +++ b/authentik/root/test_runner.py @@ -3,21 +3,38 @@ 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 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}" + + +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.""" @@ -149,8 +166,9 @@ class PytestTestRunner(DiscoverRunner): # pragma: no cover 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/tests/e2e/test_provider_ldap.py b/tests/e2e/test_provider_ldap.py index 06ea7f9685..4eb163797c 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 @@ -16,12 +15,10 @@ from authentik.flows.models import Flow 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 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 a5838c401a..0ac9300e6f 100644 --- a/tests/e2e/test_provider_proxy.py +++ b/tests/e2e/test_provider_proxy.py @@ -6,7 +6,6 @@ from json import loads 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 @@ -18,12 +17,10 @@ from authentik.flows.models import Flow 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 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 74336bd842..8060d8c520 100644 --- a/tests/e2e/test_provider_proxy_forward.py +++ b/tests/e2e/test_provider_proxy_forward.py @@ -4,7 +4,6 @@ from json import loads from pathlib import Path from time import sleep from unittest import skip -from unittest.mock import patch from selenium.webdriver.common.by import By @@ -13,12 +12,10 @@ from authentik.core.models import Application 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 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 94a73a3bdc..0392a77372 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 @@ -13,12 +12,10 @@ from authentik.core.models import Application, User 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 tests.e2e.utils import SeleniumTestCase, retry -@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached) class TestProviderRadius(SeleniumTestCase): """Radius Outpost e2e tests""" diff --git a/tests/e2e/utils.py b/tests/e2e/utils.py index 90db46e371..c281781cde 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.api.users import UserSerializer 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()