Skip to content

~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.

test: extract shared settings_routes test helpers (DRY) (#4912)

LearningCircuita06d8d199691
diff --git a/tests/web/routes/_settings_route_helpers.py b/tests/web/routes/_settings_route_helpers.py
new file mode 100644
index 0000000000..3aa42cec04
--- /dev/null
+++ b/tests/web/routes/_settings_route_helpers.py
@@ -0,0 +1,108 @@
+"""Shared test helpers for the settings_routes coverage suites.
+
+Extracted from the previously copy-pasted headers of the
+``test_settings_routes_*_coverage`` suites so the mock-Setting builder,
+the Flask-app factory and the authenticated-client plumbing live in one
+place. The versions here are the canonical, verified-identical copies that
+those suites each carried locally.
+
+Divergent per-file variants (e.g. the expanded ``_authenticated_client``
+that builds a Setting query mock, or ``deep_coverage2``'s trimmed
+``_make_setting``) intentionally stay in their own files and are not
+represented here.
+"""
+
+from contextlib import contextmanager
+from unittest.mock import MagicMock, Mock, patch
+
+from flask import Flask, jsonify
+
+from local_deep_research.web.auth.routes import auth_bp
+from local_deep_research.web.routes.settings_routes import settings_bp
+
+_MODULE = "local_deep_research.web.routes.settings_routes"
+_DECORATOR_MODULE = "local_deep_research.web.utils.route_decorators"
+
+
+def _make_setting(
+ key="test.key",
+ value="val",
+ ui_element="text",
+ name="Test Key",
+ description="desc",
+ category="general",
+ setting_type="app",
+ editable=True,
+ visible=True,
+ options=None,
+ min_value=None,
+ max_value=None,
+ step=None,
+ updated_at=None,
+):
+ """Build a mock Setting ORM object."""
+ s = MagicMock()
+ s.key = key
+ s.value = value
+ s.ui_element = ui_element
+ s.name = name
+ s.description = description
+ s.category = category
+ s.type = setting_type
+ s.editable = editable
+ s.visible = visible
+ s.options = options
+ s.min_value = min_value
+ s.max_value = max_value
+ s.step = step
+ s.updated_at = updated_at
+ return s
+
+
+def _create_test_app():
+ """Create a minimal Flask app with only the settings blueprint."""
+ app = Flask(__name__)
+ app.config["SECRET_KEY"] = "test-secret"
+ app.config["WTF_CSRF_ENABLED"] = False
+ app.register_blueprint(auth_bp)
+ app.register_blueprint(settings_bp)
+
+ @app.errorhandler(500)
+ def _handle_500(error):
+ return jsonify({"error": "Internal server error"}), 500
+
+ return app
+
+
+@contextmanager
+def _authenticated_client(app):
+ """Provide an authenticated test client with mocked auth and settings_limit."""
+ mock_db = Mock()
+ mock_db.connections = {"testuser": True}
+ mock_db.has_encryption = False
+
+ @contextmanager
+ def _fake_session(*args, **kwargs):
+ yield MagicMock()
+
+ patches = [
+ patch("local_deep_research.web.auth.decorators.db_manager", mock_db),
+ patch(
+ f"{_DECORATOR_MODULE}.get_user_db_session",
+ side_effect=_fake_session,
+ ),
+ patch(f"{_MODULE}.settings_limit", lambda f: f),
+ ]
+
+ started = []
+ try:
+ for p in patches:
+ started.append(p.start())
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ sess["session_id"] = "test-session-id"
+ yield client
+ finally:
+ for p in reversed(patches):
+ p.stop()
diff --git a/tests/web/routes/test_settings_routes_corrupted_coverage.py b/tests/web/routes/test_settings_routes_corrupted_coverage.py
index ce087ba10d..3bedd130e1 100644
--- a/tests/web/routes/test_settings_routes_corrupted_coverage.py
+++ b/tests/web/routes/test_settings_routes_corrupted_coverage.py
@@ -13,107 +13,21 @@ Covered areas:
- api_get_data_location: platform detection (Windows/macOS/Linux)
"""
-from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
-from flask import Flask, jsonify
+from ._settings_route_helpers import (
+ _authenticated_client,
+ _create_test_app,
+ _make_setting,
+)
-from local_deep_research.web.auth.routes import auth_bp
-from local_deep_research.web.routes.settings_routes import settings_bp
+# allow: no-sut-import — drives settings_routes.py via the shared _settings_route_helpers Flask client; the direct SUT import moved into that helper during the DRY extraction.
MODULE = "local_deep_research.web.routes.settings_routes"
DECORATOR_MODULE = "local_deep_research.web.utils.route_decorators"
SETTINGS_PREFIX = "/settings"
-# ---------------------------------------------------------------------------
-# Helpers
-# ---------------------------------------------------------------------------
-
-
-def _make_setting(
- key="test.key",
- value="val",
- ui_element="text",
- name="Test Key",
- description="desc",
- category="general",
- setting_type="app",
- editable=True,
- visible=True,
- options=None,
- min_value=None,
- max_value=None,
- step=None,
- updated_at=None,
-):
- """Build a mock Setting ORM object."""
- s = MagicMock()
- s.key = key
- s.value = value
- s.ui_element = ui_element
- s.name = name
- s.description = description
- s.category = category
- s.type = setting_type
- s.editable = editable
- s.visible = visible
- s.options = options
- s.min_value = min_value
- s.max_value = max_value
- s.step = step
- s.updated_at = updated_at
- return s
-
-
-def _create_test_app():
- """Create a minimal Flask app with only the settings blueprint."""
- app = Flask(__name__)
- app.config["SECRET_KEY"] = "test-secret"
- app.config["WTF_CSRF_ENABLED"] = False
- app.register_blueprint(auth_bp)
- app.register_blueprint(settings_bp)
-
- @app.errorhandler(500)
- def _handle_500(error):
- return jsonify({"error": "Internal server error"}), 500
-
- return app
-
-
-@contextmanager
-def _authenticated_client(app):
- """Provide an authenticated test client with mocked auth and settings_limit."""
- mock_db = Mock()
- mock_db.connections = {"testuser": True}
- mock_db.has_encryption = False
-
- @contextmanager
- def _fake_session(*args, **kwargs):
- yield MagicMock()
-
- patches = [
- patch("local_deep_research.web.auth.decorators.db_manager", mock_db),
- patch(
- f"{DECORATOR_MODULE}.get_user_db_session", side_effect=_fake_session
- ),
- patch(f"{MODULE}.settings_limit", lambda f: f),
- ]
-
- started = []
- try:
- for p in patches:
- started.append(p.start())
- with app.test_client() as client:
- with client.session_transaction() as sess:
- sess["username"] = "testuser"
- sess["session_id"] = "test-session-id"
- yield client
- finally:
- for p in reversed(patches):
- p.stop()
-
-
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
diff --git a/tests/web/routes/test_settings_routes_coverage.py b/tests/web/routes/test_settings_routes_coverage.py
index 015c7be0eb..9139e2d07b 100644
--- a/tests/web/routes/test_settings_routes_coverage.py
+++ b/tests/web/routes/test_settings_routes_coverage.py
@@ -30,44 +30,7 @@ from unittest.mock import patch, MagicMock
import pytest
-# ---------------------------------------------------------------------------
-# Helper: build a mock Setting ORM object
-# ---------------------------------------------------------------------------
-
-
-def _make_setting(
- key="test.key",
- value="val",
- ui_element="text",
- name="Test Key",
- description="desc",
- category="general",
- setting_type="app",
- editable=True,
- visible=True,
- options=None,
- min_value=None,
- max_value=None,
- step=None,
- updated_at=None,
-):
- s = MagicMock()
- s.key = key
- s.value = value
- s.ui_element = ui_element
- s.name = name
- s.description = description
- s.category = category
- s.type = setting_type
- s.editable = editable
- s.visible = visible
- s.options = options
- s.min_value = min_value
- s.max_value = max_value
- s.step = step
- s.updated_at = updated_at
- return s
-
+from ._settings_route_helpers import _make_setting
# ---------------------------------------------------------------------------
# _get_engine_icon_and_category (pure function, no Flask context needed)
diff --git a/tests/web/routes/test_settings_routes_deep_coverage.py b/tests/web/routes/test_settings_routes_deep_coverage.py
index 45f6026ff2..1e979bbd6b 100644
--- a/tests/web/routes/test_settings_routes_deep_coverage.py
+++ b/tests/web/routes/test_settings_routes_deep_coverage.py
@@ -25,12 +25,13 @@ Uncovered functions/branches targeted:
"""
from contextlib import contextmanager
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import MagicMock, patch
-from flask import Flask, jsonify
-
-from local_deep_research.web.auth.routes import auth_bp
-from local_deep_research.web.routes.settings_routes import settings_bp
+from ._settings_route_helpers import (
+ _authenticated_client,
+ _create_test_app,
+ _make_setting,
+)
# ---------------------------------------------------------------------------
# Constants
@@ -41,94 +42,6 @@ DECORATOR_MODULE = "local_deep_research.web.utils.route_decorators"
SETTINGS_PREFIX = "/settings"
-# ---------------------------------------------------------------------------
-# Helpers
-# ---------------------------------------------------------------------------
-
-
-def _make_setting(
- key="test.key",
- value="val",
- ui_element="text",
- name="Test Key",
- description="desc",
- category="general",
- setting_type="app",
- editable=True,
- visible=True,
- options=None,
- min_value=None,
- max_value=None,
- step=None,
- updated_at=None,
-):
- """Build a mock Setting ORM object."""
- s = MagicMock()
- s.key = key
- s.value = value
- s.ui_element = ui_element
- s.name = name
- s.description = description
- s.category = category
- s.type = setting_type
- s.editable = editable
- s.visible = visible
- s.options = options
- s.min_value = min_value
- s.max_value = max_value
- s.step = step
- s.updated_at = updated_at
- return s
-
-
-def _create_test_app():
- """Create a minimal Flask app with only the settings blueprint."""
- app = Flask(__name__)
- app.config["SECRET_KEY"] = "test-secret"
- app.config["WTF_CSRF_ENABLED"] = False
- app.register_blueprint(auth_bp)
- app.register_blueprint(settings_bp)
-
- @app.errorhandler(500)
- def _handle_500(error):
- return jsonify({"error": "Internal server error"}), 500
-
- return app
-
-
-@contextmanager
-def _authenticated_client(app):
- """Provide an authenticated test client with mocked auth and settings_limit."""
- mock_db = Mock()
- mock_db.connections = {"testuser": True}
- mock_db.has_encryption = False
-
- @contextmanager
- def _fake_session(*args, **kwargs):
- yield MagicMock()
-
- patches = [
- patch("local_deep_research.web.auth.decorators.db_manager", mock_db),
- patch(
- f"{DECORATOR_MODULE}.get_user_db_session", side_effect=_fake_session
- ),
- patch(f"{MODULE}.settings_limit", lambda f: f),
- ]
-
- started = []
- try:
- for p in patches:
- started.append(p.start())
- with app.test_client() as client:
- with client.session_transaction() as sess:
- sess["username"] = "testuser"
- sess["session_id"] = "test-session-id"
- yield client
- finally:
- for p in reversed(patches):
- p.stop()
-
-
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
diff --git a/tests/web/routes/test_settings_routes_deep_coverage2.py b/tests/web/routes/test_settings_routes_deep_coverage2.py
index a53926900d..42380d4063 100644
--- a/tests/web/routes/test_settings_routes_deep_coverage2.py
+++ b/tests/web/routes/test_settings_routes_deep_coverage2.py
@@ -22,10 +22,8 @@ from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
import pytest
-from flask import Flask, jsonify
-from local_deep_research.web.auth.routes import auth_bp
-from local_deep_research.web.routes.settings_routes import settings_bp
+from ._settings_route_helpers import _create_test_app
# ---------------------------------------------------------------------------
# Constants
@@ -85,21 +83,6 @@ def _make_setting(
return s
-def _create_test_app():
- """Create a minimal Flask app with auth + settings blueprints."""
- app = Flask(__name__)
- app.config["SECRET_KEY"] = "test-secret"
- app.config["WTF_CSRF_ENABLED"] = False
- app.register_blueprint(auth_bp)
- app.register_blueprint(settings_bp)
-
- @app.errorhandler(500)
- def _handle_500(error):
- return jsonify({"error": "Internal server error"}), 500
-
- return app
-
-
@contextmanager
def _authenticated_client(app, mock_settings=None):
"""Provide a test client with mocked auth and DB session."""
diff --git a/tests/web/routes/test_settings_routes_gaps_coverage.py b/tests/web/routes/test_settings_routes_gaps_coverage.py
index 69abe14d43..33bc708a1f 100644
--- a/tests/web/routes/test_settings_routes_gaps_coverage.py
+++ b/tests/web/routes/test_settings_routes_gaps_coverage.py
@@ -12,61 +12,13 @@ from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
import pytest
-from flask import Flask, jsonify
-from local_deep_research.web.auth.routes import auth_bp
-from local_deep_research.web.routes.settings_routes import settings_bp
+from ._settings_route_helpers import _authenticated_client, _create_test_app
MODULE = "local_deep_research.web.routes.settings_routes"
DECORATOR_MODULE = "local_deep_research.web.utils.route_decorators"
-def _create_test_app():
- app = Flask(__name__)
- app.config["SECRET_KEY"] = "test-secret"
- app.config["WTF_CSRF_ENABLED"] = False
- app.register_blueprint(auth_bp)
- app.register_blueprint(settings_bp)
-
- @app.errorhandler(500)
- def _handle_500(error):
- return jsonify({"error": "Internal server error"}), 500
-
- return app
-
-
-@contextmanager
-def _authenticated_client(app):
- mock_db = Mock()
- mock_db.connections = {"testuser": True}
- mock_db.has_encryption = False
-
- @contextmanager
- def _fake_session(*args, **kwargs):
- yield MagicMock()
-
- patches = [
- patch("local_deep_research.web.auth.decorators.db_manager", mock_db),
- patch(
- f"{DECORATOR_MODULE}.get_user_db_session", side_effect=_fake_session
- ),
- patch(f"{MODULE}.settings_limit", lambda f: f),
- ]
-
- started = []
- try:
- for p in patches:
- started.append(p.start())
- with app.test_client() as client:
- with client.session_transaction() as sess:
- sess["username"] = "testuser"
- sess["session_id"] = "test-session-id"
- yield client
- finally:
- for p in reversed(patches):
- p.stop()
-
-
# ===========================================================================
# api_get_all_settings — redaction
# ===========================================================================
diff --git a/tests/web/routes/test_settings_routes_save_validation_coverage.py b/tests/web/routes/test_settings_routes_save_validation_coverage.py
index b6186a021c..374b21fac1 100644
--- a/tests/web/routes/test_settings_routes_save_validation_coverage.py
+++ b/tests/web/routes/test_settings_routes_save_validation_coverage.py
@@ -12,12 +12,9 @@ Targeted functions / branches:
"""
from contextlib import contextmanager
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import MagicMock, patch
-from flask import Flask, jsonify
-
-from local_deep_research.web.auth.routes import auth_bp
-from local_deep_research.web.routes.settings_routes import settings_bp
+from ._settings_route_helpers import _create_test_app, _make_setting
# ---------------------------------------------------------------------------
# Constants
@@ -28,94 +25,6 @@ DECORATOR_MODULE = "local_deep_research.web.utils.route_decorators"
SETTINGS_PREFIX = "/settings"
-# ---------------------------------------------------------------------------
-# Helpers (mirror the pattern in test_settings_routes_deep_coverage.py)
-# ---------------------------------------------------------------------------
-
-
-def _make_setting(
- key="test.key",
- value="val",
- ui_element="text",
- name="Test Key",
- description="desc",
- category="general",
- setting_type="app",
- editable=True,
- visible=True,
- options=None,
- min_value=None,
- max_value=None,
- step=None,
- updated_at=None,
-):
- """Build a mock Setting ORM object."""
- s = MagicMock()
- s.key = key
- s.value = value
- s.ui_element = ui_element
- s.name = name
- s.description = description
- s.category = category
- s.type = setting_type
- s.editable = editable
- s.visible = visible
- s.options = options
- s.min_value = min_value
- s.max_value = max_value
- s.step = step
- s.updated_at = updated_at
- return s
-
-
-def _create_test_app():
- """Create a minimal Flask app with only the settings blueprint."""
- app = Flask(__name__)
- app.config["SECRET_KEY"] = "test-secret"
- app.config["WTF_CSRF_ENABLED"] = False
- app.register_blueprint(auth_bp)
- app.register_blueprint(settings_bp)
-
- @app.errorhandler(500)
- def _handle_500(error):
- return jsonify({"error": "Internal server error"}), 500
-
- return app
-
-
-@contextmanager
-def _authenticated_client(app):
- """Provide an authenticated test client with mocked auth and settings_limit."""
- mock_db = Mock()
- mock_db.connections = {"testuser": True}
- mock_db.has_encryption = False
-
- @contextmanager
- def _fake_session(*args, **kwargs):
- yield MagicMock()
-
- patches = [
- patch("local_deep_research.web.auth.decorators.db_manager", mock_db),
- patch(
- f"{DECORATOR_MODULE}.get_user_db_session", side_effect=_fake_session
- ),
- patch(f"{MODULE}.settings_limit", lambda f: f),
- ]
-
- started = []
- try:
- for p in patches:
- started.append(p.start())
- with app.test_client() as client:
- with client.session_transaction() as sess:
- sess["username"] = "testuser"
- sess["session_id"] = "test-session-id"
- yield client
- finally:
- for p in reversed(patches):
- p.stop()
-
-
# ---------------------------------------------------------------------------
# 1. validate_setting: checkbox with non-boolean value returns False
# ---------------------------------------------------------------------------
diff --git a/tests/web/routes/test_settings_routes_security.py b/tests/web/routes/test_settings_routes_security.py
index 30b0127a07..271b96002b 100644
--- a/tests/web/routes/test_settings_routes_security.py
+++ b/tests/web/routes/test_settings_routes_security.py
@@ -11,10 +11,7 @@ Source: src/local_deep_research/web/routes/settings_routes.py
from contextlib import contextmanager
from unittest.mock import Mock, patch
-from flask import Flask, jsonify
-
-from local_deep_research.web.auth.routes import auth_bp
-from local_deep_research.web.routes.settings_routes import settings_bp
+from ._settings_route_helpers import _create_test_app
# ---------------------------------------------------------------------------
@@ -22,20 +19,6 @@ from local_deep_research.web.routes.settings_routes import settings_bp
# ---------------------------------------------------------------------------
-def _create_test_app():
- app = Flask(__name__)
- app.config["SECRET_KEY"] = "test-secret"
- app.config["WTF_CSRF_ENABLED"] = False
- app.register_blueprint(auth_bp)
- app.register_blueprint(settings_bp)
-
- @app.errorhandler(500)
- def _handle_500(error):
- return jsonify({"error": "Internal server error"}), 500
-
- return app
-
-
@contextmanager
def _authenticated_client(app, mock_settings=None):
"""Provide test client with mocked auth and DB session for settings routes."""