From 68fc8980428d01fa07fc380331a57ab5cce77a21 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Thu, 19 Mar 2026 10:05:07 -0700 Subject: [PATCH] Fix: Resolve more instances of tests which mutated global states (#12395) --- src/documents/tests/test_api_status.py | 18 +++++++++++------- src/paperless_ai/tests/conftest.py | 10 ++++++++++ src/paperless_ai/tests/test_ai_indexing.py | 8 -------- src/paperless_ai/tests/test_embedding.py | 9 --------- src/paperless_mail/tests/test_mail_oauth.py | 14 +++++++------- 5 files changed, 28 insertions(+), 31 deletions(-) create mode 100644 src/paperless_ai/tests/conftest.py diff --git a/src/documents/tests/test_api_status.py b/src/documents/tests/test_api_status.py index 409f4bd7f..705f28145 100644 --- a/src/documents/tests/test_api_status.py +++ b/src/documents/tests/test_api_status.py @@ -101,13 +101,17 @@ class TestSystemStatus(APITestCase): - The response contains the correct install type """ self.client.force_login(self.user) - os.environ["PNGX_CONTAINERIZED"] = "1" - response = self.client.get(self.ENDPOINT) - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data["install_type"], "docker") - os.environ["KUBERNETES_SERVICE_HOST"] = "http://localhost" - response = self.client.get(self.ENDPOINT) - self.assertEqual(response.data["install_type"], "kubernetes") + with mock.patch.dict(os.environ, {"PNGX_CONTAINERIZED": "1"}, clear=False): + response = self.client.get(self.ENDPOINT) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["install_type"], "docker") + with mock.patch.dict( + os.environ, + {"PNGX_CONTAINERIZED": "1", "KUBERNETES_SERVICE_HOST": "http://localhost"}, + clear=False, + ): + response = self.client.get(self.ENDPOINT) + self.assertEqual(response.data["install_type"], "kubernetes") @mock.patch("redis.Redis.execute_command") def test_system_status_redis_ping(self, mock_ping) -> None: diff --git a/src/paperless_ai/tests/conftest.py b/src/paperless_ai/tests/conftest.py new file mode 100644 index 000000000..2d71476c7 --- /dev/null +++ b/src/paperless_ai/tests/conftest.py @@ -0,0 +1,10 @@ +from pathlib import Path + +import pytest +from pytest_django.fixtures import SettingsWrapper + + +@pytest.fixture +def temp_llm_index_dir(tmp_path: Path, settings: SettingsWrapper): + settings.LLM_INDEX_DIR = tmp_path + return tmp_path diff --git a/src/paperless_ai/tests/test_ai_indexing.py b/src/paperless_ai/tests/test_ai_indexing.py index 724ac43e4..c1e3b64d8 100644 --- a/src/paperless_ai/tests/test_ai_indexing.py +++ b/src/paperless_ai/tests/test_ai_indexing.py @@ -13,14 +13,6 @@ from documents.models import PaperlessTask from paperless_ai import indexing -@pytest.fixture -def temp_llm_index_dir(tmp_path): - original_dir = indexing.settings.LLM_INDEX_DIR - indexing.settings.LLM_INDEX_DIR = tmp_path - yield tmp_path - indexing.settings.LLM_INDEX_DIR = original_dir - - @pytest.fixture def real_document(db): return Document.objects.create( diff --git a/src/paperless_ai/tests/test_embedding.py b/src/paperless_ai/tests/test_embedding.py index f9dbfd85d..74151ff89 100644 --- a/src/paperless_ai/tests/test_embedding.py +++ b/src/paperless_ai/tests/test_embedding.py @@ -3,7 +3,6 @@ from unittest.mock import MagicMock from unittest.mock import patch import pytest -from django.conf import settings from documents.models import Document from paperless.models import LLMEmbeddingBackend @@ -19,14 +18,6 @@ def mock_ai_config(): yield MockAIConfig -@pytest.fixture -def temp_llm_index_dir(tmp_path): - original_dir = settings.LLM_INDEX_DIR - settings.LLM_INDEX_DIR = tmp_path - yield tmp_path - settings.LLM_INDEX_DIR = original_dir - - @pytest.fixture def mock_document(): doc = MagicMock(spec=Document) diff --git a/src/paperless_mail/tests/test_mail_oauth.py b/src/paperless_mail/tests/test_mail_oauth.py index 1f7033bdd..651094673 100644 --- a/src/paperless_mail/tests/test_mail_oauth.py +++ b/src/paperless_mail/tests/test_mail_oauth.py @@ -1,7 +1,6 @@ from datetime import timedelta from unittest import mock -from django.conf import settings from django.contrib.auth.models import Permission from django.contrib.auth.models import User from django.test import TestCase @@ -16,6 +15,13 @@ from paperless_mail.models import MailAccount from paperless_mail.oauth import PaperlessMailOAuth2Manager +@override_settings( + OAUTH_CALLBACK_BASE_URL="http://localhost:8000", + GMAIL_OAUTH_CLIENT_ID="test_gmail_client_id", + GMAIL_OAUTH_CLIENT_SECRET="test_gmail_client_secret", + OUTLOOK_OAUTH_CLIENT_ID="test_outlook_client_id", + OUTLOOK_OAUTH_CLIENT_SECRET="test_outlook_client_secret", +) class TestMailOAuth( TestCase, ): @@ -31,12 +37,6 @@ class TestMailOAuth( self.user.save() self.client.force_login(self.user) self.mail_account_handler = MailAccountHandler() - # Mock settings - settings.OAUTH_CALLBACK_BASE_URL = "http://localhost:8000" - settings.GMAIL_OAUTH_CLIENT_ID = "test_gmail_client_id" - settings.GMAIL_OAUTH_CLIENT_SECRET = "test_gmail_client_secret" - settings.OUTLOOK_OAUTH_CLIENT_ID = "test_outlook_client_id" - settings.OUTLOOK_OAUTH_CLIENT_SECRET = "test_outlook_client_secret" super().setUp() def test_generate_paths(self) -> None: