From ba4062683877f52e074eb9a26fa690e4ffb6a06e Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Wed, 10 Sep 2025 08:52:02 -0700 Subject: [PATCH] Experiment with disabling whitenoise middleware --- pyproject.toml | 4 ++++ src/documents/tests/conftest.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 41e1a49ac..a3c446832 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -249,6 +249,10 @@ norecursedirs = [ "src/locale/", ".venv/", "src-ui/" ] DJANGO_SETTINGS_MODULE = "paperless.settings" +markers = [ + "use_whitenoise: mark test to run with Whitenoise middleware enabled", +] + [tool.pytest_env] PAPERLESS_DISABLE_DBHANDLER = "true" PAPERLESS_CACHE_BACKEND = "django.core.cache.backends.locmem.LocMemCache" diff --git a/src/documents/tests/conftest.py b/src/documents/tests/conftest.py index 8c88cee9f..3b2b57e94 100644 --- a/src/documents/tests/conftest.py +++ b/src/documents/tests/conftest.py @@ -28,3 +28,24 @@ def authenticated_rest_api_client(rest_api_client: APIClient): user = UserModel.objects.create_user(username="testuser", password="password") rest_api_client.force_authenticate(user=user) yield rest_api_client + + +@pytest.fixture(autouse=True) +def configure_whitenoise_middleware(request, settings): + """ + By default, remove Whitenoise middleware from tests. + Only include it when test is marked with @pytest.mark.use_whitenoise + """ + # Check if the test is marked to use whitenoise + use_whitenoise_marker = request.node.get_closest_marker("use_whitenoise") + + if not use_whitenoise_marker: + # Filter out whitenoise middleware using pytest-django's settings fixture + middleware_without_whitenoise = [ + mw + for mw in settings.MIDDLEWARE + if "whitenoise.middleware.WhiteNoiseMiddleware" not in mw.lower() + ] + + settings.MIDDLEWARE = middleware_without_whitenoise + yield