~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(test): restore exact pre-test module state across reload/eviction test sites (#5049)
LearningCircuit5f08bdead779
diff --git a/changelog.d/+test-reload-state-restoration.misc.md b/changelog.d/+test-reload-state-restoration.misc.mdnew file mode 100644index 0000000000..578e24c9ef--- /dev/null+++ b/changelog.d/+test-reload-state-restoration.misc.md@@ -0,0 +1 @@+Fixed cross-test state leaks from tests that reload or evict production modules: the rate-limiter tests restored a fake config, the history-routes fixture leaked auth-bypassed blueprints, the office-format tests could leak a blocked-dependency loader registry, the settings-logger tests leaked the log level, the security import-fallback helper re-created the whole security tree (stacking permanent audit hooks), and four sibling sites (thread-local-session eviction, auth-db reload, env-definitions re-import, loader-registry import guards) now restore the exact pre-test modules.diff --git a/tests/database/test_auth_db.py b/tests/database/test_auth_db.pyindex b8305a44c6..3323f1b336 100644--- a/tests/database/test_auth_db.py+++ b/tests/database/test_auth_db.py@@ -514,11 +514,19 @@ class TestInitAuthDatabaseAtomicDDL:import importlibimport local_deep_research.database.auth_db as auth_module- with patch(- "local_deep_research.database.auth_db.init_auth_database"- ) as mock_init:- importlib.reload(auth_module)- mock_init.assert_not_called()+ # The reload discards the module's cached engine state and lock;+ # restore the pre-test module dict so later tests keep working+ # against the original objects.+ snapshot = dict(auth_module.__dict__)+ try:+ with patch(+ "local_deep_research.database.auth_db.init_auth_database"+ ) as mock_init:+ importlib.reload(auth_module)+ mock_init.assert_not_called()+ finally:+ auth_module.__dict__.clear()+ auth_module.__dict__.update(snapshot)class TestAuthDbPerformancePragmas:diff --git a/tests/document_loaders/test_loader_registry_import_guards.py b/tests/document_loaders/test_loader_registry_import_guards.pyindex f2ef656730..aacd0698af 100644--- a/tests/document_loaders/test_loader_registry_import_guards.py+++ b/tests/document_loaders/test_loader_registry_import_guards.py@@ -50,15 +50,19 @@ def _reload_with_blocked_symbol(blocked_name):@pytest.fixturedef restore_module():- """Restore loader_registry to its natural import state after each test.+ """Restore loader_registry to its pre-test state after each test.The tests in this file force ImportError on specific loader imports- via module reload. Without this fixture, a reload-with-patched-import- would persist into later tests and leave the module in a degraded state.+ via module reload. Restoring the snapshot puts back the original+ module objects without re-executing the module — a teardown reload+ would recreate every class and registry entry, breaking identity+ with references bound elsewhere in the process."""- yieldmodule = importlib.import_module(MODULE_NAME)- importlib.reload(module)+ snapshot = dict(module.__dict__)+ yield+ module.__dict__.clear()+ module.__dict__.update(snapshot)class TestOptionalLoaderImportErrors:diff --git a/tests/document_loaders/test_office_format_extraction.py b/tests/document_loaders/test_office_format_extraction.pyindex 17e659d259..f97789f1d8 100644--- a/tests/document_loaders/test_office_format_extraction.py+++ b/tests/document_loaders/test_office_format_extraction.py@@ -191,8 +191,16 @@ class TestHonestDetection:@pytest.fixturedef restore_module(self):+ # Restore the pre-test module dict instead of reloading: pytest+ # finalizes this fixture before monkeypatch (LIFO, it is listed+ # after monkeypatch in the test signatures), so a teardown reload+ # would re-execute the registry with find_spec still patched and+ # leak the blocked-dependency state to later tests.+ module = importlib.import_module(REGISTRY_MODULE)+ snapshot = dict(module.__dict__)yield- _reload(REGISTRY_MODULE)+ module.__dict__.clear()+ module.__dict__.update(snapshot)@staticmethoddef _reload_without(monkeypatch, blocked):diff --git a/tests/security/test_rate_limiter.py b/tests/security/test_rate_limiter.pyindex c7fc11a8c3..f7dbd749e3 100644--- a/tests/security/test_rate_limiter.py+++ b/tests/security/test_rate_limiter.py@@ -43,6 +43,26 @@ def app():return app+def restore_rate_limiter_module():+ """Snapshot rate_limiter's module dict and restore it after the test.++ The functional tests reload the module to re-bind the upload decorators+ under test-controlled limits. Restoring the snapshot puts back the+ original objects (the module-level Limiter instance included), instead+ of reloading a third time under a hardcoded fake config — which would+ leave fake limits and a fresh Limiter in the module for later tests+ while earlier `from rate_limiter import ...` bindings kept the+ originals.+ """+ from local_deep_research.security import rate_limiter++ snapshot = dict(rate_limiter.__dict__)+ yield+ rate_limiter.__dict__.clear()+ rate_limiter.__dict__.update(snapshot)++class TestGetClientIp:"""Tests for get_client_ip function."""@@ -341,94 +361,75 @@ class TestUploadRateLimit:class TestUploadRateLimitFunctional:"""Functional tests for dual-key upload rate limiting."""- def test_upload_rate_limit_enforces_per_user(self):+ def test_upload_rate_limit_enforces_per_user(+ self, restore_rate_limiter_module+ ):"""Per-user upload limit blocks after threshold."""rate_limiter = _reload_rate_limiter_with_limits(user_limit="3 per minute", ip_limit="100 per minute")- try:- test_app = Flask(__name__)- test_app.config["SECRET_KEY"] = "test"- test_app.config["TESTING"] = True- test_app.config["RATELIMIT_ENABLED"] = True- test_app.config["RATELIMIT_STRATEGY"] = "moving-window"-- @test_app.route("/upload", methods=["POST"])- @rate_limiter.upload_rate_limit_user- @rate_limiter.upload_rate_limit_ip- def upload():- return "ok"-- rate_limiter.limiter.init_app(test_app)-- with test_app.test_client() as c:- with c.session_transaction() as sess:- sess["username"] = "uploader"- # Per-user limit is "3 per minute" — first 3 pass, 4th is blocked- for i in range(3):- resp = c.post("/upload")- assert resp.status_code == 200, (- f"Request {i + 1} should pass"- )+ test_app = Flask(__name__)+ test_app.config["SECRET_KEY"] = "test"+ test_app.config["TESTING"] = True+ test_app.config["RATELIMIT_ENABLED"] = True+ test_app.config["RATELIMIT_STRATEGY"] = "moving-window"++ @test_app.route("/upload", methods=["POST"])+ @rate_limiter.upload_rate_limit_user+ @rate_limiter.upload_rate_limit_ip+ def upload():+ return "ok"++ rate_limiter.limiter.init_app(test_app)++ with test_app.test_client() as c:+ with c.session_transaction() as sess:+ sess["username"] = "uploader"+ # Per-user limit is "3 per minute" — first 3 pass, 4th is blocked+ for i in range(3):resp = c.post("/upload")- assert resp.status_code == 429-- try:- rate_limiter.limiter.reset()- except Exception:- pass- finally:- # Restore module to normal state for subsequent tests- _reload_rate_limiter_with_limits(- user_limit="60 per minute;1000 per hour",- ip_limit="60 per minute;1000 per hour",- )+ assert resp.status_code == 200, f"Request {i + 1} should pass"+ resp = c.post("/upload")+ assert resp.status_code == 429- def test_upload_per_user_limit_is_independent(self):+ def test_upload_per_user_limit_is_independent(+ self, restore_rate_limiter_module+ ):"""Per-user upload bucket is keyed by username, not shared."""rate_limiter = _reload_rate_limiter_with_limits(user_limit="3 per minute", ip_limit="100 per minute")- try:- test_app = Flask(__name__)- test_app.config["SECRET_KEY"] = "test"- test_app.config["TESTING"] = True- test_app.config["RATELIMIT_ENABLED"] = True- test_app.config["RATELIMIT_STRATEGY"] = "moving-window"-- # Only per-user limit (no per-IP) to isolate user-key behavior- @test_app.route("/upload", methods=["POST"])- @rate_limiter.upload_rate_limit_user- def upload():- return "ok"-- rate_limiter.limiter.init_app(test_app)-- # User A exhausts their per-user limit- client_a = test_app.test_client()- with client_a.session_transaction() as sess:- sess["username"] = "user_a"- for _ in range(3):- client_a.post("/upload")- assert client_a.post("/upload").status_code == 429-- # User B is unaffected (different user bucket)- client_b = test_app.test_client()- with client_b.session_transaction() as sess:- sess["username"] = "user_b"- assert client_b.post("/upload").status_code == 200-- try:- rate_limiter.limiter.reset()- except Exception:- pass- finally:- _reload_rate_limiter_with_limits(- user_limit="60 per minute;1000 per hour",- ip_limit="60 per minute;1000 per hour",- )+ test_app = Flask(__name__)+ test_app.config["SECRET_KEY"] = "test"+ test_app.config["TESTING"] = True+ test_app.config["RATELIMIT_ENABLED"] = True+ test_app.config["RATELIMIT_STRATEGY"] = "moving-window"++ # Only per-user limit (no per-IP) to isolate user-key behavior+ @test_app.route("/upload", methods=["POST"])+ @rate_limiter.upload_rate_limit_user+ def upload():+ return "ok"- def test_upload_rate_limit_respects_env_var_override(self):+ rate_limiter.limiter.init_app(test_app)++ # User A exhausts their per-user limit+ client_a = test_app.test_client()+ with client_a.session_transaction() as sess:+ sess["username"] = "user_a"+ for _ in range(3):+ client_a.post("/upload")+ assert client_a.post("/upload").status_code == 429++ # User B is unaffected (different user bucket)+ client_b = test_app.test_client()+ with client_b.session_transaction() as sess:+ sess["username"] = "user_b"+ assert client_b.post("/upload").status_code == 200++ def test_upload_rate_limit_respects_env_var_override(+ self, restore_rate_limiter_module+ ):"""End-to-end: patched config flows through to decorator binding.Reloading rate_limiter after patching load_server_config simulates@@ -438,40 +439,29 @@ class TestUploadRateLimitFunctional:rate_limiter = _reload_rate_limiter_with_limits(user_limit="2 per minute", ip_limit="2 per minute")- try:- assert rate_limiter._UPLOAD_RATE_LIMIT_USER == "2 per minute"- assert rate_limiter._UPLOAD_RATE_LIMIT_IP == "2 per minute"-- test_app = Flask(__name__)- test_app.config["SECRET_KEY"] = "test"- test_app.config["TESTING"] = True- test_app.config["RATELIMIT_ENABLED"] = True- test_app.config["RATELIMIT_STRATEGY"] = "moving-window"-- @test_app.route("/upload", methods=["POST"])- @rate_limiter.upload_rate_limit_user- @rate_limiter.upload_rate_limit_ip- def upload():- return "ok"+ assert rate_limiter._UPLOAD_RATE_LIMIT_USER == "2 per minute"+ assert rate_limiter._UPLOAD_RATE_LIMIT_IP == "2 per minute"++ test_app = Flask(__name__)+ test_app.config["SECRET_KEY"] = "test"+ test_app.config["TESTING"] = True+ test_app.config["RATELIMIT_ENABLED"] = True+ test_app.config["RATELIMIT_STRATEGY"] = "moving-window"++ @test_app.route("/upload", methods=["POST"])+ @rate_limiter.upload_rate_limit_user+ @rate_limiter.upload_rate_limit_ip+ def upload():+ return "ok"- rate_limiter.limiter.init_app(test_app)+ rate_limiter.limiter.init_app(test_app)- with test_app.test_client() as c:- with c.session_transaction() as sess:- sess["username"] = "envtest"- assert c.post("/upload").status_code == 200- assert c.post("/upload").status_code == 200- assert c.post("/upload").status_code == 429-- try:- rate_limiter.limiter.reset()- except Exception:- pass- finally:- _reload_rate_limiter_with_limits(- user_limit="60 per minute;1000 per hour",- ip_limit="60 per minute;1000 per hour",- )+ with test_app.test_client() as c:+ with c.session_transaction() as sess:+ sess["username"] = "envtest"+ assert c.post("/upload").status_code == 200+ assert c.post("/upload").status_code == 200+ assert c.post("/upload").status_code == 429class TestApiRateLimitCaching:diff --git a/tests/security/test_security_import_fallbacks_coverage.py b/tests/security/test_security_import_fallbacks_coverage.pyindex 319716ea38..10adf99d58 100644--- a/tests/security/test_security_import_fallbacks_coverage.py+++ b/tests/security/test_security_import_fallbacks_coverage.py@@ -5,7 +5,6 @@ The missing statements are in try/except ImportError blocks:- FileUploadValidator import fails → FileUploadValidator=None, _has_file_upload_validator=False"""-import importlibimport sysfrom unittest.mock import patch@@ -13,20 +12,24 @@ import pytestdef _reload_security_with_blocked(blocked_submodule):- """Reload security package with a submodule blocked via sys.modules[...]=None."""- modules_to_remove = [- k for k in sys.modules if "local_deep_research.security" in k- ]- saved = {k: sys.modules.pop(k) for k in modules_to_remove}+ """Re-execute security/__init__ with a submodule blocked via sys.modules[...]=None.+ Only the package module itself is evicted; every submodule stays cached,+ so the re-execution reuses the original objects (no recreated classes or+ enums, and install_audit_hook() no-ops instead of stacking another+ permanent audit hook). Only the __init__ fallback branches behave+ differently: the blocked submodule's None entry makes its import raise+ ImportError.+ """+ saved = sys.modules.pop("local_deep_research.security", None)try:with patch.dict(sys.modules, {blocked_submodule: None}):import local_deep_research.security as sec_mod- importlib.reload(sec_mod)return sec_modfinally:- sys.modules.update(saved)+ if saved is not None:+ sys.modules["local_deep_research.security"] = saved@pytest.fixture(autouse=True)diff --git a/tests/settings/env_vars/test_env_auto_generation.py b/tests/settings/env_vars/test_env_auto_generation.pyindex 37081b6f35..d8b0de0939 100644--- a/tests/settings/env_vars/test_env_auto_generation.py+++ b/tests/settings/env_vars/test_env_auto_generation.py@@ -1,8 +1,11 @@"""Test environment variable auto-generation and external variables."""import os+import sysfrom unittest.mock import patch+import pytest+from local_deep_research.settings.env_settings import (BooleanSetting,StringSetting,@@ -96,6 +99,28 @@ class TestEnvVarAutoGeneration:)+def restore_testing_env_module():+ """Put back the pre-test env_definitions.testing module.++ The external-vars tests delete the module and re-import it under a+ patched environment; without a restore, the module (and its+ CI/GITHUB_ACTIONS/TESTING constants) would stay baked with the last+ test's patched values for the rest of the process.+ """+ name = "local_deep_research.settings.env_definitions.testing"+ saved = sys.modules.get(name)+ yield+ sys.modules.pop(name, None)+ parent = sys.modules.get("local_deep_research.settings.env_definitions")+ if saved is not None:+ sys.modules[name] = saved+ if parent is not None:+ parent.testing = saved+ elif parent is not None and hasattr(parent, "testing"):+ delattr(parent, "testing")++class TestExternalEnvVars:"""Test external environment variables (CI, GITHUB_ACTIONS, etc.)."""@@ -112,15 +137,15 @@ class TestExternalEnvVars:assert isinstance(GITHUB_ACTIONS, bool)assert isinstance(TESTING, bool)- def test_external_vars_respond_to_environment(self):+ def test_external_vars_respond_to_environment(+ self, restore_testing_env_module+ ):"""Test that external vars read from environment correctly."""# We need to reimport the module after changing env varswith patch.dict(os.environ, {"CI": "true", "GITHUB_ACTIONS": "1", "TESTING": "yes"}):# Clear the module from cache and reimport- import sys-if ("local_deep_research.settings.env_definitions.testing"in sys.modules@@ -139,12 +164,10 @@ class TestExternalEnvVars:assert GITHUB_ACTIONS is Trueassert TESTING is True- def test_external_vars_default_to_false(self):+ def test_external_vars_default_to_false(self, restore_testing_env_module):"""Test that external vars default to False when not set."""with patch.dict(os.environ, {}, clear=True):# Clear and reimport- import sys-if ("local_deep_research.settings.env_definitions.testing"in sys.modulesdiff --git a/tests/settings/test_settings_logger.py b/tests/settings/test_settings_logger.pyindex b921a316bd..9181a9daca 100644--- a/tests/settings/test_settings_logger.py+++ b/tests/settings/test_settings_logger.py@@ -17,6 +17,23 @@ from unittest.mock import patch# So we'll import the module itself and access functions through it[email protected](autouse=True)+def _restore_settings_logger_module():+ """Restore the pre-test module state after each test.++ The tests reload settings.logger under a patched LDR_LOG_SETTINGS to+ rebuild SETTINGS_LOG_LEVEL; without a restore, the last test's level+ (e.g. "debug") would stay in the module for the rest of the process,+ silently defeating the quiet-by-default contract for later tests.+ """+ from local_deep_research.settings import logger as settings_logger++ snapshot = dict(settings_logger.__dict__)+ yield+ settings_logger.__dict__.clear()+ settings_logger.__dict__.update(snapshot)++class TestLogSettingsNoneLevel:"""Tests for log_settings with none level."""diff --git a/tests/utilities/test_thread_context_coverage.py b/tests/utilities/test_thread_context_coverage.pyindex 92b5fd1de7..eb1f94effd 100644--- a/tests/utilities/test_thread_context_coverage.py+++ b/tests/utilities/test_thread_context_coverage.py@@ -34,7 +34,11 @@ class TestPreserveResearchContextCleanup:def setup_method(self):_cleanup()- sys.modules.pop(+ # Evict the module so each test exercises a fresh lazy import; keep+ # the original so teardown can put it back instead of leaving the+ # NEXT importer to build a new singleton distinct from the one+ # earlier-bound consumers hold.+ self._saved_tls_module = sys.modules.pop("local_deep_research.database.thread_local_session", None)@@ -43,6 +47,17 @@ class TestPreserveResearchContextCleanup:sys.modules.pop("local_deep_research.database.thread_local_session", None)+ db_pkg = sys.modules.get("local_deep_research.database")+ if self._saved_tls_module is not None:+ sys.modules["local_deep_research.database.thread_local_session"] = (+ self._saved_tls_module+ )+ if db_pkg is not None:+ db_pkg.thread_local_session = self._saved_tls_module+ elif db_pkg is not None and hasattr(db_pkg, "thread_local_session"):+ # The test's fresh import bound a throwaway module on the parent+ # package; drop it so attribute lookups can't resolve to it.+ delattr(db_pkg, "thread_local_session")def test_cleanup_current_thread_called_on_success(self):"""When context is set and the function succeeds,diff --git a/tests/utilities/test_url_thread_coverage.py b/tests/utilities/test_url_thread_coverage.pyindex 2ff2c12fbf..4f9bb4e9c2 100644--- a/tests/utilities/test_url_thread_coverage.py+++ b/tests/utilities/test_url_thread_coverage.py@@ -390,7 +390,11 @@ class TestPreserveResearchContext:def setup_method(self):clear_search_context()- sys.modules.pop(+ # Evict the module so each test exercises a fresh lazy import; keep+ # the original so teardown can put it back instead of leaving the+ # NEXT importer to build a new singleton distinct from the one+ # earlier-bound consumers hold.+ self._saved_tls_module = sys.modules.pop("local_deep_research.database.thread_local_session", None)@@ -399,6 +403,17 @@ class TestPreserveResearchContext:sys.modules.pop("local_deep_research.database.thread_local_session", None)+ db_pkg = sys.modules.get("local_deep_research.database")+ if self._saved_tls_module is not None:+ sys.modules["local_deep_research.database.thread_local_session"] = (+ self._saved_tls_module+ )+ if db_pkg is not None:+ db_pkg.thread_local_session = self._saved_tls_module+ elif db_pkg is not None and hasattr(db_pkg, "thread_local_session"):+ # The test's fresh import bound a throwaway module on the parent+ # package; drop it so attribute lookups can't resolve to it.+ delattr(db_pkg, "thread_local_session")def test_captures_context_at_decoration_time(self):set_search_context({"research_id": "snap"})diff --git a/tests/web/routes/test_history_routes.py b/tests/web/routes/test_history_routes.pyindex f0e220a391..1d97575331 100644--- a/tests/web/routes/test_history_routes.py+++ b/tests/web/routes/test_history_routes.py@@ -1,9 +1,21 @@"""Tests for history_routes module - History endpoints."""+import importlibfrom unittest.mock import patch, MagicMockimport pytest+# Import once under real conditions and snapshot the clean module state.+# The authenticated_client fixture reloads this module with login_required+# patched to a no-op; without an exact restore, the auth-bypassed blueprint+# stays live for any later create_app() in the same process — and if this+# reload triggered the first-ever import of research_routes, its routes+# would be permanently stripped of auth as well. The eager import here also+# guarantees research_routes is first-imported outside the patched window.+import local_deep_research.web.routes.history_routes as history_module++_CLEAN_MODULE_STATE = dict(history_module.__dict__)+# History routes are registered under /history prefixHISTORY_PREFIX = "/history"@@ -38,10 +50,7 @@ def authenticated_client():) as mock_limiter:mock_limiter.exempt = lambda f: f- # Import routes with patched decorators- import importlib- import local_deep_research.web.routes.history_routes as history_module-+ # Re-execute the routes module so the no-op decorators bindimportlib.reload(history_module)app = Flask(__name__)@@ -57,6 +66,11 @@ def authenticated_client():sess["username"] = "testuser"yield client+ # Put back the clean module state captured at import time so the+ # auth-bypassed view functions cannot outlive this fixture.+ history_module.__dict__.clear()+ history_module.__dict__.update(_CLEAN_MODULE_STATE)+class TestHistoryPage:"""Tests for /history/ endpoint."""