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.

19.4 KB
Raw Blame
"""Port of ``tests/web/routes/test_api_routes_coverage.py`` to FastAPI.

The Flask original was deleted by the migration. It is the only thing that
ever exercised the *error* branches of ``web/routers/api.py``:

* every handler's ``except`` -> 500,
* ``api_add_resource``'s SSRF rejection and its success call contract,
* every ``check/ollama_status`` and ``check/ollama_model`` outcome that
  ``test_check_ollama_unit.py`` does not reach (old array API format,
  invalid JSON, no-models, non-200 for the model check, json_parse_error,
  timeout for the model check, and both generic-exception fallbacks),
* the ``_probe_ollama_tags`` helper itself.

Plumbing translation only: ``app.config["LLM_CONFIG"]`` no longer exists, so
provider/model/URL are injected through the router's
``get_user_db_session`` + ``SettingsManager`` seam.
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import requests

API = "local_deep_research.web.routers.api"
API_PREFIX = "/research/api"


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


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


def _llm_settings(values):
    """Patch the router's DB/settings seam so ``get_setting`` resolves from
    ``values`` (falling back to each call site's default)."""

    @contextmanager
    def _fake_session(*a, **kw):
        yield MagicMock()

    manager = MagicMock()
    manager.get_setting.side_effect = lambda key, default=None: values.get(
        key, default
    )
    return (
        patch(f"{API}.get_user_db_session", side_effect=_fake_session),
        patch(f"{API}.SettingsManager", return_value=manager),
    )


_OLLAMA = {
    "llm.provider": "ollama",
    "llm.ollama.url": "http://localhost:11434",
}
_OLLAMA_MODEL = dict(_OLLAMA, **{"llm.model": "llama3"})


# ---------------------------------------------------------------------------
# get_current_config: exception path
# ---------------------------------------------------------------------------


class TestGetCurrentConfigException:
    """Exception path in get_current_config."""

    def test_get_current_config_exception_returns_500(
        self, authenticated_client
    ):
        """When get_user_db_session raises, return 500 with error."""
        with patch(
            f"{API}.get_user_db_session",
            return_value=_make_db_ctx_raising(RuntimeError("db down")),
        ):
            resp = authenticated_client.get(
                f"{API_PREFIX}/settings/current-config"
            )
        assert resp.status_code == 500
        assert resp.json()["success"] is False


# ---------------------------------------------------------------------------
# api_research_status: exception path
# ---------------------------------------------------------------------------


class TestApiResearchStatusException:
    """Exception path in api_research_status."""

    def test_research_status_exception_returns_500(self, authenticated_client):
        """When db session raises, return 500 error."""
        with patch(
            f"{API}.get_user_db_session",
            return_value=_make_db_ctx_raising(RuntimeError("db error")),
        ):
            resp = authenticated_client.get(f"{API_PREFIX}/status/some-id")
        assert resp.status_code == 500
        assert resp.json()["status"] == "error"


# ---------------------------------------------------------------------------
# api_terminate_research: exception path
# ---------------------------------------------------------------------------


class TestApiTerminateResearchException:
    """Exception path in api_terminate_research."""

    def test_terminate_exception_returns_500(self, authenticated_client):
        """When cancel_research raises, return 500."""
        with patch(
            f"{API}.cancel_research", side_effect=RuntimeError("cancel boom")
        ):
            resp = authenticated_client.post(f"{API_PREFIX}/terminate/some-id")
        assert resp.status_code == 500
        assert resp.json()["status"] == "error"


# ---------------------------------------------------------------------------
# api_get_resources: exception path
# ---------------------------------------------------------------------------


class TestApiGetResourcesException:
    """Exception path in api_get_resources."""

    def test_get_resources_exception_returns_500(self, authenticated_client):
        """When get_resources_for_research raises, return 500."""
        with patch(
            f"{API}.get_resources_for_research",
            side_effect=RuntimeError("res err"),
        ):
            resp = authenticated_client.get(f"{API_PREFIX}/resources/some-id")
        assert resp.status_code == 500
        assert resp.json()["status"] == "error"


# ---------------------------------------------------------------------------
# api_add_resource: SSRF rejection
# ---------------------------------------------------------------------------


class TestApiAddResourceSsrf:
    """SSRF URL rejection in api_add_resource."""

    def test_ssrf_invalid_url_rejected(self, authenticated_client):
        """When validate_url returns False, return 400."""
        with patch(
            "local_deep_research.security.ssrf_validator.validate_url",
            return_value=False,
        ):
            resp = authenticated_client.post(
                f"{API_PREFIX}/resources/some-id",
                json={
                    "title": "Bad",
                    "url": "http://169.254.169.254/latest",
                },
            )
        assert resp.status_code == 400
        assert resp.json()["message"] == "Invalid URL"


# ---------------------------------------------------------------------------
# api_add_resource: success + exception paths
# ---------------------------------------------------------------------------


class TestApiAddResourceSuccess:
    """Success and exception paths in api_add_resource."""

    def test_add_resource_success(self, authenticated_client):
        """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(
                "local_deep_research.security.ssrf_validator.validate_url",
                return_value=True,
            ),
            patch(
                f"{API}.get_user_db_session",
                return_value=_make_db_ctx(mock_session),
            ),
            patch(f"{API}.add_resource", return_value=42) as mock_add,
        ):
            resp = authenticated_client.post(
                f"{API_PREFIX}/resources/res-123",
                json={
                    "title": "My Resource",
                    "url": "https://example.com/page",
                    "content_preview": "preview text",
                    "source_type": "pdf",
                    "metadata": {"key": "val"},
                },
            )

        assert resp.status_code == 200
        data = resp.json()
        assert data["status"] == "success"
        assert data["resource_id"] == 42
        mock_add.assert_called_once()
        kwargs = mock_add.call_args.kwargs
        assert kwargs["research_id"] == "res-123"
        assert kwargs["title"] == "My Resource"
        assert kwargs["url"] == "https://example.com/page"
        assert kwargs["content_preview"] == "preview text"
        assert kwargs["source_type"] == "pdf"
        assert kwargs["metadata"] == {"key": "val"}
        # User scoping added by the migration: the resource must be written
        # into the CALLER's encrypted DB, so the kwarg has to be present.
        # Asserted as present-and-correct (not via a defaulted .get) so
        # deleting it fails here.
        assert "username" in kwargs, (
            "add_resource must be called with the authenticated username; "
            "without it the write lands in no user's database"
        )
        assert kwargs["username"]

    def test_add_resource_exception_returns_500(self, authenticated_client):
        """When add_resource raises, return 500."""
        mock_session = MagicMock()
        mock_session.query.return_value = _build_filter_chain(MagicMock())

        with (
            patch(
                "local_deep_research.security.ssrf_validator.validate_url",
                return_value=True,
            ),
            patch(
                f"{API}.get_user_db_session",
                return_value=_make_db_ctx(mock_session),
            ),
            patch(
                f"{API}.add_resource", side_effect=RuntimeError("insert fail")
            ),
        ):
            resp = authenticated_client.post(
                f"{API_PREFIX}/resources/res-123",
                json={
                    "title": "My Resource",
                    "url": "https://example.com/page",
                },
            )

        assert resp.status_code == 500
        assert resp.json()["status"] == "error"


# ---------------------------------------------------------------------------
# api_delete_resource: exception path
# ---------------------------------------------------------------------------


class TestApiDeleteResourceException:
    """Exception path in api_delete_resource."""

    def test_delete_resource_exception_returns_500(self, authenticated_client):
        """When delete_resource raises, return 500."""
        with patch(
            f"{API}.delete_resource", side_effect=RuntimeError("delete boom")
        ):
            resp = authenticated_client.delete(
                f"{API_PREFIX}/resources/res-id/delete/1"
            )
        assert resp.status_code == 500
        assert resp.json()["status"] == "error"


# ---------------------------------------------------------------------------
# check_ollama_status: edge cases
# ---------------------------------------------------------------------------


class TestCheckOllamaStatusEdgeCases:
    """Edge cases for check_ollama_status endpoint."""

    def _get(self, client, settings, **patches):
        p1, p2 = _llm_settings(settings)
        with p1, p2:
            if patches:
                ((name, kw),) = patches.items()
                with patch(f"{API}.{name}", **kw):
                    return client.get(f"{API_PREFIX}/check/ollama_status")
            return client.get(f"{API_PREFIX}/check/ollama_status")

    def test_old_api_format(self, authenticated_client):
        """When response has no 'models' key, use old format (array)."""
        mock_resp = MagicMock()
        mock_resp.status_code = 200
        mock_resp.json.return_value = [{"name": "llama3"}, {"name": "mistral"}]

        resp = self._get(
            authenticated_client,
            _OLLAMA,
            safe_get={"return_value": mock_resp},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["running"] is True
        assert data["model_count"] == 2

    def test_invalid_json_response(self, authenticated_client):
        """When response.json() raises ValueError, report running with warning."""
        mock_resp = MagicMock()
        mock_resp.status_code = 200
        mock_resp.json.side_effect = ValueError("bad json")

        resp = self._get(
            authenticated_client,
            _OLLAMA,
            safe_get={"return_value": mock_resp},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["running"] is True
        assert "invalid" in data["message"].lower()

    def test_non_200_status(self, authenticated_client):
        """When Ollama returns non-200, report not running."""
        mock_resp = MagicMock()
        mock_resp.status_code = 503

        resp = self._get(
            authenticated_client,
            _OLLAMA,
            safe_get={"return_value": mock_resp},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["running"] is False
        assert data["status_code"] == 503

    def test_timeout_error(self, authenticated_client):
        """When safe_get raises Timeout, report not running with timeout type."""
        resp = self._get(
            authenticated_client,
            _OLLAMA,
            safe_get={"side_effect": requests.exceptions.Timeout("timed out")},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["running"] is False
        assert data["error_type"] == "timeout"

    def test_general_exception(self, authenticated_client):
        """When an unexpected exception occurs, report not running."""
        resp = self._get(
            authenticated_client,
            _OLLAMA,
            normalize_url={"side_effect": RuntimeError("unexpected")},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["running"] is False
        assert data["error_type"] == "exception"


# ---------------------------------------------------------------------------
# check_ollama_model: edge cases
# ---------------------------------------------------------------------------


class TestCheckOllamaModelEdgeCases:
    """Edge cases for check_ollama_model endpoint."""

    def _get(self, client, settings, **patches):
        p1, p2 = _llm_settings(settings)
        with p1, p2:
            ((name, kw),) = patches.items()
            with patch(f"{API}.{name}", **kw):
                return client.get(f"{API_PREFIX}/check/ollama_model")

    def test_non_200_status(self, authenticated_client):
        """When Ollama API returns non-200, report not available."""
        mock_resp = MagicMock()
        mock_resp.status_code = 503

        resp = self._get(
            authenticated_client,
            _OLLAMA_MODEL,
            safe_get={"return_value": mock_resp},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["available"] is False
        assert data["status_code"] == 503

    def test_old_api_format(self, authenticated_client):
        """When response has no 'models' key, use old format (array)."""
        mock_resp = MagicMock()
        mock_resp.status_code = 200
        mock_resp.json.return_value = [{"name": "llama3"}]

        resp = self._get(
            authenticated_client,
            _OLLAMA_MODEL,
            safe_get={"return_value": mock_resp},
        )

        assert resp.status_code == 200
        assert resp.json()["available"] is True

    def test_no_models_found(self, authenticated_client):
        """When models list is empty, report not available with pull message."""
        mock_resp = MagicMock()
        mock_resp.status_code = 200
        mock_resp.json.return_value = {"models": []}

        resp = self._get(
            authenticated_client,
            _OLLAMA_MODEL,
            safe_get={"return_value": mock_resp},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["available"] is False
        assert "pull" in data["message"].lower()

    def test_json_parse_error(self, authenticated_client):
        """When response.json() raises ValueError, report parse error."""
        mock_resp = MagicMock()
        mock_resp.status_code = 200
        mock_resp.json.side_effect = ValueError("bad json")

        resp = self._get(
            authenticated_client,
            _OLLAMA_MODEL,
            safe_get={"return_value": mock_resp},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["available"] is False
        assert data["error_type"] == "json_parse_error"

    def test_connection_error(self, authenticated_client):
        """When safe_get raises ConnectionError, report connection error."""
        resp = self._get(
            authenticated_client,
            _OLLAMA_MODEL,
            safe_get={
                "side_effect": requests.exceptions.ConnectionError("refused")
            },
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["available"] is False
        assert data["error_type"] == "connection_error"

    def test_timeout_error(self, authenticated_client):
        """When safe_get raises Timeout, report timeout."""
        resp = self._get(
            authenticated_client,
            _OLLAMA_MODEL,
            safe_get={"side_effect": requests.exceptions.Timeout("timed out")},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["available"] is False
        assert data["error_type"] == "timeout"

    def test_general_exception(self, authenticated_client):
        """When an unexpected exception occurs, report exception."""
        resp = self._get(
            authenticated_client,
            _OLLAMA_MODEL,
            normalize_url={"side_effect": RuntimeError("unexpected")},
        )

        assert resp.status_code == 200
        data = resp.json()
        assert data["available"] is False
        assert data["error_type"] == "exception"


class TestProbeOllamaTags:
    """Direct tests for the shared _probe_ollama_tags helper — the single
    source the status and model-availability checks both consume, so an
    'is Ollama up?' answer can no longer drift between them."""

    def _probe(self, base_url="http://localhost:11434"):
        from local_deep_research.web.routers.api import _probe_ollama_tags

        return _probe_ollama_tags(base_url)

    def test_ok_new_format(self):
        resp = MagicMock()
        resp.status_code = 200
        resp.json.return_value = {"models": [{"name": "llama3"}]}
        with patch(f"{API}.safe_get", return_value=resp):
            outcome, payload = self._probe()
        assert outcome == "ok"
        assert payload == [{"name": "llama3"}]

    def test_ok_old_format(self):
        resp = MagicMock()
        resp.status_code = 200
        resp.json.return_value = [{"name": "a"}, {"name": "b"}]
        with patch(f"{API}.safe_get", return_value=resp):
            outcome, payload = self._probe()
        assert outcome == "ok"
        assert len(payload) == 2

    def test_bad_status_returns_status_code(self):
        resp = MagicMock()
        resp.status_code = 503
        with patch(f"{API}.safe_get", return_value=resp):
            outcome, payload = self._probe()
        assert outcome == "bad_status"
        assert payload == 503

    def test_invalid_json(self):
        resp = MagicMock()
        resp.status_code = 200
        resp.json.side_effect = ValueError("bad")
        with patch(f"{API}.safe_get", return_value=resp):
            outcome, payload = self._probe()
        assert outcome == "invalid_json"
        assert payload is None

    def test_connection_error(self):
        with patch(
            f"{API}.safe_get",
            side_effect=requests.exceptions.ConnectionError(),
        ):
            outcome, payload = self._probe()
        assert outcome == "connection_error"

    def test_timeout(self):
        with patch(
            f"{API}.safe_get", side_effect=requests.exceptions.Timeout()
        ):
            outcome, payload = self._probe()
        assert outcome == "timeout"