~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
test: add 20 coverage tests for api_routes.py error paths (#2840)
LearningCircuit31f54063d5f9
diff --git a/tests/web/routes/test_api_routes_coverage.py b/tests/web/routes/test_api_routes_coverage.pynew file mode 100644index 0000000000..16a9b5d2e6--- /dev/null+++ b/tests/web/routes/test_api_routes_coverage.py@@ -0,0 +1,581 @@+"""+Coverage tests for api_routes.py targeting uncovered statements and branches.++Covers:+- get_current_config: exception path (lines 64-66)+- api_research_status: exception path (lines 117-119)+- api_terminate_research: exception path (lines 151-153)+- api_get_resources: exception path (lines 168-170)+- api_add_resource: SSRF rejection (lines 207-208)+- api_add_resource: success path with add_resource call (lines 227-245)+- api_add_resource: exception path (lines 243-247)+- api_delete_resource: exception path (lines 274-276)+- check_ollama_status: old API format (lines 342-343)+- check_ollama_status: invalid JSON response (lines 354-358)+- check_ollama_status: non-200 status code (lines 364-368)+- check_ollama_status: timeout error (lines 386-395)+- check_ollama_status: general exception (lines 397-406)+- check_ollama_model: non-200 status (lines 460-463)+- check_ollama_model: old API format (lines 492-493)+- check_ollama_model: no models found (lines 525-526)+- check_ollama_model: JSON parse error (lines 542-554)+- check_ollama_model: connection error (lines 556-567)+- check_ollama_model: timeout error (lines 568-578)+- check_ollama_model: general exception (lines 580-596)+"""++import requests+from unittest.mock import MagicMock, patch++import pytest+from flask import Flask++from local_deep_research.web.auth import auth_bp+from local_deep_research.web.routes.api_routes import api_bp++# ---------------------------------------------------------------------------+# Constants+# ---------------------------------------------------------------------------++MODULE = "local_deep_research.web.routes.api_routes"+AUTH_DB_MANAGER = "local_deep_research.web.auth.decorators.db_manager"++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------+++def _mock_auth():+ """Return a MagicMock that satisfies login_required db_manager check."""+ return MagicMock(is_user_connected=MagicMock(return_value=True))+++def _make_db_ctx(mock_session):+ """Build a mock context-manager for get_user_db_session."""+ ctx = MagicMock()+ ctx.__enter__ = MagicMock(return_value=mock_session)+ ctx.__exit__ = MagicMock(return_value=None)+ return ctx+++def _make_db_ctx_raising(exc):+ """Build a context-manager that raises on __enter__."""+ ctx = MagicMock()+ ctx.__enter__ = MagicMock(side_effect=exc)+ ctx.__exit__ = MagicMock(return_value=None)+ return ctx+++def _build_filter_chain(result):+ """Build chained SQLAlchemy mock query for filter_by().first()."""+ q = MagicMock()+ q.filter_by.return_value.first.return_value = result+ return q+++# ---------------------------------------------------------------------------+# Fixtures+# ---------------------------------------------------------------------------+++def app():+ """Minimal Flask app with auth and api blueprints."""+ application = Flask(__name__)+ application.config["SECRET_KEY"] = "test-secret"+ application.config["TESTING"] = True+ application.register_blueprint(auth_bp)+ application.register_blueprint(api_bp)+ return application+++def _authed_get(app, path, **kwargs):+ """Issue an authenticated GET request and return the response."""+ with app.test_client() as c:+ with c.session_transaction() as sess:+ sess["username"] = "testuser"+ return c.get(path, **kwargs)+++def _authed_post(app, path, **kwargs):+ """Issue an authenticated POST request and return the response."""+ with app.test_client() as c:+ with c.session_transaction() as sess:+ sess["username"] = "testuser"+ return c.post(path, **kwargs)+++def _authed_delete(app, path, **kwargs):+ """Issue an authenticated DELETE request and return the response."""+ with app.test_client() as c:+ with c.session_transaction() as sess:+ sess["username"] = "testuser"+ return c.delete(path, **kwargs)+++# ---------------------------------------------------------------------------+# get_current_config: exception path (lines 64-66)+# ---------------------------------------------------------------------------+++class TestGetCurrentConfigException:+ """Exception path in get_current_config."""++ def test_get_current_config_exception_returns_500(self, app):+ """When get_user_db_session raises, return 500 with error."""+ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.get_user_db_session",+ return_value=_make_db_ctx_raising(RuntimeError("db down")),+ ):+ resp = _authed_get(app, "/settings/current-config")+ assert resp.status_code == 500+ data = resp.get_json()+ assert data["success"] is False+++# ---------------------------------------------------------------------------+# api_research_status: exception path (lines 117-119)+# ---------------------------------------------------------------------------+++class TestApiResearchStatusException:+ """Exception path in api_research_status."""++ def test_research_status_exception_returns_500(self, app):+ """When db session raises, return 500 error."""+ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.get_user_db_session",+ return_value=_make_db_ctx_raising(RuntimeError("db error")),+ ):+ resp = _authed_get(app, "/status/some-id")+ assert resp.status_code == 500+ data = resp.get_json()+ assert data["status"] == "error"+++# ---------------------------------------------------------------------------+# api_terminate_research: exception path (lines 151-153)+# ---------------------------------------------------------------------------+++class TestApiTerminateResearchException:+ """Exception path in api_terminate_research."""++ def test_terminate_exception_returns_500(self, app):+ """When cancel_research raises, return 500."""+ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.cancel_research",+ side_effect=RuntimeError("cancel boom"),+ ):+ resp = _authed_post(app, "/terminate/some-id")+ assert resp.status_code == 500+ data = resp.get_json()+ assert data["status"] == "error"+++# ---------------------------------------------------------------------------+# api_get_resources: exception path (lines 168-170)+# ---------------------------------------------------------------------------+++class TestApiGetResourcesException:+ """Exception path in api_get_resources."""++ def test_get_resources_exception_returns_500(self, app):+ """When get_resources_for_research raises, return 500."""+ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.get_resources_for_research",+ side_effect=RuntimeError("res err"),+ ):+ resp = _authed_get(app, "/resources/some-id")+ assert resp.status_code == 500+ data = resp.get_json()+ assert data["status"] == "error"+++# ---------------------------------------------------------------------------+# api_add_resource: SSRF rejection (lines 207-208)+# ---------------------------------------------------------------------------+++class TestApiAddResourceSsrf:+ """SSRF URL rejection in api_add_resource."""++ def test_ssrf_invalid_url_rejected(self, app):+ """When validate_url returns False, return 400."""+ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ "local_deep_research.security.ssrf_validator.validate_url",+ return_value=False,+ ):+ resp = _authed_post(+ app,+ "/resources/some-id",+ json={+ "title": "Bad",+ "url": "http://169.254.169.254/latest",+ },+ content_type="application/json",+ )+ assert resp.status_code == 400+ data = resp.get_json()+ assert data["message"] == "Invalid URL"+++# ---------------------------------------------------------------------------+# api_add_resource: success path (lines 227-245)+# ---------------------------------------------------------------------------+++class TestApiAddResourceSuccess:+ """Success and exception paths in api_add_resource."""++ def test_add_resource_success(self, app):+ """When research exists and URL valid, add resource and return success."""+ mock_session = MagicMock()+ mock_session.query.return_value = _build_filter_chain(MagicMock())++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ "local_deep_research.security.ssrf_validator.validate_url",+ return_value=True,+ ):+ with patch(+ f"{MODULE}.get_user_db_session",+ return_value=_make_db_ctx(mock_session),+ ):+ with patch(+ f"{MODULE}.add_resource", return_value=42+ ) as mock_add:+ resp = _authed_post(+ app,+ "/resources/res-123",+ json={+ "title": "My Resource",+ "url": "https://example.com/page",+ "content_preview": "preview text",+ "source_type": "pdf",+ "metadata": {"key": "val"},+ },+ content_type="application/json",+ )++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["status"] == "success"+ assert data["resource_id"] == 42+ mock_add.assert_called_once_with(+ research_id="res-123",+ title="My Resource",+ url="https://example.com/page",+ content_preview="preview text",+ source_type="pdf",+ metadata={"key": "val"},+ )++ def test_add_resource_exception_returns_500(self, app):+ """When add_resource raises, return 500."""+ mock_session = MagicMock()+ mock_session.query.return_value = _build_filter_chain(MagicMock())++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ "local_deep_research.security.ssrf_validator.validate_url",+ return_value=True,+ ):+ with patch(+ f"{MODULE}.get_user_db_session",+ return_value=_make_db_ctx(mock_session),+ ):+ with patch(+ f"{MODULE}.add_resource",+ side_effect=RuntimeError("insert fail"),+ ):+ resp = _authed_post(+ app,+ "/resources/res-123",+ json={+ "title": "My Resource",+ "url": "https://example.com/page",+ },+ content_type="application/json",+ )++ assert resp.status_code == 500+ data = resp.get_json()+ assert data["status"] == "error"+++# ---------------------------------------------------------------------------+# api_delete_resource: exception path (lines 274-276)+# ---------------------------------------------------------------------------+++class TestApiDeleteResourceException:+ """Exception path in api_delete_resource."""++ def test_delete_resource_exception_returns_500(self, app):+ """When delete_resource raises, return 500."""+ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.delete_resource",+ side_effect=RuntimeError("delete boom"),+ ):+ resp = _authed_delete(app, "/resources/res-id/delete/1")+ assert resp.status_code == 500+ data = resp.get_json()+ assert data["status"] == "error"+++# ---------------------------------------------------------------------------+# check_ollama_status: edge cases (lines 342-406)+# ---------------------------------------------------------------------------+++class TestCheckOllamaStatusEdgeCases:+ """Edge cases for check_ollama_status endpoint."""++ def test_old_api_format(self, app):+ """When response has no 'models' key, use old format (array)."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "ollama_base_url": "http://localhost:11434",+ }+ mock_resp = MagicMock()+ mock_resp.status_code = 200+ mock_resp.json.return_value = [{"name": "llama3"}, {"name": "mistral"}]++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(f"{MODULE}.safe_get", return_value=mock_resp):+ resp = _authed_get(app, "/check/ollama_status")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["running"] is True+ assert data["model_count"] == 2++ def test_invalid_json_response(self, app):+ """When response.json() raises ValueError, report running with warning."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "ollama_base_url": "http://localhost:11434",+ }+ mock_resp = MagicMock()+ mock_resp.status_code = 200+ mock_resp.json.side_effect = ValueError("bad json")++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(f"{MODULE}.safe_get", return_value=mock_resp):+ resp = _authed_get(app, "/check/ollama_status")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["running"] is True+ assert "invalid" in data["message"].lower()++ def test_non_200_status(self, app):+ """When Ollama returns non-200, report not running."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "ollama_base_url": "http://localhost:11434",+ }+ mock_resp = MagicMock()+ mock_resp.status_code = 503++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(f"{MODULE}.safe_get", return_value=mock_resp):+ resp = _authed_get(app, "/check/ollama_status")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["running"] is False+ assert data["status_code"] == 503++ def test_timeout_error(self, app):+ """When safe_get raises Timeout, report not running with timeout type."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "ollama_base_url": "http://localhost:11434",+ }++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.safe_get",+ side_effect=requests.exceptions.Timeout("timed out"),+ ):+ resp = _authed_get(app, "/check/ollama_status")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["running"] is False+ assert data["error_type"] == "timeout"++ def test_general_exception(self, app):+ """When an unexpected exception occurs, report not running."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "ollama_base_url": "http://localhost:11434",+ }++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.normalize_url",+ side_effect=RuntimeError("unexpected"),+ ):+ resp = _authed_get(app, "/check/ollama_status")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["running"] is False+ assert data["error_type"] == "exception"+++# ---------------------------------------------------------------------------+# check_ollama_model: edge cases (lines 460-596)+# ---------------------------------------------------------------------------+++class TestCheckOllamaModelEdgeCases:+ """Edge cases for check_ollama_model endpoint."""++ def test_non_200_status(self, app):+ """When Ollama API returns non-200, report not available."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "model": "llama3",+ "ollama_base_url": "http://localhost:11434",+ }+ mock_resp = MagicMock()+ mock_resp.status_code = 503++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(f"{MODULE}.safe_get", return_value=mock_resp):+ resp = _authed_get(app, "/check/ollama_model")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["available"] is False+ assert data["status_code"] == 503++ def test_old_api_format(self, app):+ """When response has no 'models' key, use old format (array)."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "model": "llama3",+ "ollama_base_url": "http://localhost:11434",+ }+ mock_resp = MagicMock()+ mock_resp.status_code = 200+ mock_resp.json.return_value = [{"name": "llama3"}]++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(f"{MODULE}.safe_get", return_value=mock_resp):+ resp = _authed_get(app, "/check/ollama_model")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["available"] is True++ def test_no_models_found(self, app):+ """When models list is empty, report not available with pull message."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "model": "llama3",+ "ollama_base_url": "http://localhost:11434",+ }+ mock_resp = MagicMock()+ mock_resp.status_code = 200+ mock_resp.json.return_value = {"models": []}++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(f"{MODULE}.safe_get", return_value=mock_resp):+ resp = _authed_get(app, "/check/ollama_model")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["available"] is False+ assert "pull" in data["message"].lower()++ def test_json_parse_error(self, app):+ """When response.json() raises ValueError, report parse error."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "model": "llama3",+ "ollama_base_url": "http://localhost:11434",+ }+ mock_resp = MagicMock()+ mock_resp.status_code = 200+ mock_resp.json.side_effect = ValueError("bad json")++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(f"{MODULE}.safe_get", return_value=mock_resp):+ resp = _authed_get(app, "/check/ollama_model")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["available"] is False+ assert data["error_type"] == "json_parse_error"++ def test_connection_error(self, app):+ """When safe_get raises ConnectionError, report connection error."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "model": "llama3",+ "ollama_base_url": "http://localhost:11434",+ }++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.safe_get",+ side_effect=requests.exceptions.ConnectionError("refused"),+ ):+ resp = _authed_get(app, "/check/ollama_model")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["available"] is False+ assert data["error_type"] == "connection_error"++ def test_timeout_error(self, app):+ """When safe_get raises Timeout, report timeout."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "model": "llama3",+ "ollama_base_url": "http://localhost:11434",+ }++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.safe_get",+ side_effect=requests.exceptions.Timeout("timed out"),+ ):+ resp = _authed_get(app, "/check/ollama_model")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["available"] is False+ assert data["error_type"] == "timeout"++ def test_general_exception(self, app):+ """When an unexpected exception occurs, report exception."""+ app.config["LLM_CONFIG"] = {+ "provider": "ollama",+ "model": "llama3",+ "ollama_base_url": "http://localhost:11434",+ }++ with patch(AUTH_DB_MANAGER, _mock_auth()):+ with patch(+ f"{MODULE}.normalize_url",+ side_effect=RuntimeError("unexpected"),+ ):+ resp = _authed_get(app, "/check/ollama_model")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["available"] is False+ assert data["error_type"] == "exception"diff --git a/tests/web/routes/test_history_routes_coverage.py b/tests/web/routes/test_history_routes_coverage.pyindex 8beeb5ca8a..c4d3763460 100644--- a/tests/web/routes/test_history_routes_coverage.py+++ b/tests/web/routes/test_history_routes_coverage.py@@ -7,11 +7,13 @@ Covers:parse and parse failure (lines 165-172)- get_research_details: database exception returning 500 (lines 210-212)- get_research_details: active research with in-memory log merging (lines 231-243)+- get_research_details: non-active, non-completed progress=0 (line 248)- get_report: report_data is empty/None returning 404 (line 292)- get_report: storage exception returning 500 (lines 323-324)- get_markdown: research not found returning 404 (line 344)- get_markdown: content is empty/None returning 404 (line 354)- get_markdown: storage exception returning 500 (lines 359-360)+- get_research_logs: log formatting with missing fields (lines 389-393)"""from unittest.mock import MagicMock, patch@@ -282,6 +284,37 @@ class TestGetResearchDetailsActiveWithLogs:assert times == sorted(times)+# ---------------------------------------------------------------------------+# get_research_details: non-active, non-completed progress=0 (line 248)+# ---------------------------------------------------------------------------+++class TestGetResearchDetailsNotActiveNotCompleted:+ """Details route for non-active, non-completed research."""++ def test_not_active_not_completed_progress_zero(self, app):+ """Non-active, non-completed research details should have progress=0."""+ research = _make_research(id="pend-det", status="in_progress")+ mock_session = MagicMock()+ mock_session.query.return_value = _build_filter_chain(research)++ with (+ patch(+ f"{MODULE}.get_user_db_session",+ return_value=_make_db_ctx(mock_session),+ ),+ patch(f"{MODULE}.get_logs_for_research", return_value=[]),+ patch(f"{MODULE}.get_research_strategy", return_value="standard"),+ patch(f"{MODULE}.get_active_research_snapshot", return_value=None),+ patch(AUTH_DB_MANAGER, _mock_auth()),+ ):+ resp = _authed_get(app, "/history/details/pend-det")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["progress"] == 0++# ---------------------------------------------------------------------------# get_report: report_data is None (line 292)# ---------------------------------------------------------------------------@@ -433,6 +466,36 @@ class TestGetMarkdownContentEmpty:assert data["status"] == "error"assert "not found" in data["message"].lower()+ def test_none_content_returns_404(self, app):+ """When storage.get_report returns None, return 404."""+ research = _make_research(id="none-md")+ mock_session = MagicMock()+ mock_session.query.return_value = _build_filter_chain(research)++ mock_storage = MagicMock()+ mock_storage.get_report.return_value = None++ with (+ patch(+ f"{MODULE}.get_user_db_session",+ return_value=_make_db_ctx(mock_session),+ ),+ patch(+ "local_deep_research.storage.get_report_storage",+ return_value=mock_storage,+ ),+ patch(+ "local_deep_research.web.auth.decorators.current_user",+ return_value="testuser",+ ),+ patch(AUTH_DB_MANAGER, _mock_auth()),+ ):+ resp = _authed_get(app, "/history/markdown/none-md")++ assert resp.status_code == 404+ data = resp.get_json()+ assert data["status"] == "error"+# ---------------------------------------------------------------------------# get_markdown: storage exception (lines 359-360)@@ -505,3 +568,44 @@ class TestGetResearchStatusCompletedBadLog:data = resp.get_json()assert data["progress"] == 100assert data["log"] == []+++# ---------------------------------------------------------------------------+# get_research_logs: log formatting with missing fields (lines 389-393)+# ---------------------------------------------------------------------------+++class TestGetResearchLogsFormatting:+ """Research logs route with missing fields in log entries."""++ def test_logs_missing_fields_get_defaults(self, app):+ """Logs missing time/message/type get defaults applied."""+ research = _make_research()+ mock_session = MagicMock()+ mock_session.query.return_value = _build_filter_chain(research)++ raw_logs = [+ {"time": "10:00", "message": "ok", "type": "info"},+ {"extra": "field"}, # missing time, message, type+ ]++ with (+ patch(+ f"{MODULE}.get_user_db_session",+ return_value=_make_db_ctx(mock_session),+ ),+ patch(f"{MODULE}.get_logs_for_research", return_value=raw_logs),+ patch(AUTH_DB_MANAGER, _mock_auth()),+ ):+ resp = _authed_get(app, "/history/logs/test-id")++ assert resp.status_code == 200+ data = resp.get_json()+ assert data["status"] == "success"+ assert len(data["logs"]) == 2+ # The second log should have defaults filled in+ second = data["logs"][1]+ assert second["time"] == ""+ assert second["message"] == "No message"+ assert second["type"] == "info"+ assert second["extra"] == "field"