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.

20.3 KB
Raw Blame
"""Port of ``tests/web/routes/test_api_routes.py`` to the FastAPI surface.

The Flask original was deleted by the migration. The endpoints live on
``web/routers/api.py`` under the same ``/research/api`` prefix.

Translation notes (plumbing only — every assertion is the original's):

* ``app.config["LLM_CONFIG"]`` is gone. The Ollama checks now read
  ``llm.provider`` / ``llm.model`` / ``llm.ollama.url`` from the user's
  settings, so the config is injected by patching ``get_user_db_session`` +
  ``SettingsManager`` on the router module (same seam
  ``test_check_ollama_unit.py`` uses).
* ``CSRFMiddleware`` is unconditional on the FastAPI app and runs BEFORE
  auth, so an anonymous POST/DELETE with no token is rejected as 403 by
  CSRF and never reaches the auth check. The unauthenticated tests below
  therefore stamp a valid anonymous CSRF token first, so the 401 they
  assert is the auth rejection the original pinned — not a CSRF artefact.
* ``api_delete_resource``'s not-found path: on main the service returned
  ``bool`` per its route, but ``delete_resource`` has always raised
  ``ValueError`` for a missing row and never returned False — main's route
  reached its 404 only because the test mocked a False return, while a real
  miss 500'd. The branch handles ``ValueError`` explicitly, so the port
  drives the real not-found signal and keeps the original's 404 assertion.
"""

from unittest.mock import AsyncMock, MagicMock, patch

import pytest

API = "local_deep_research.web.routers.api"

# API routes are registered under /research/api prefix
API_PREFIX = "/research/api"


@pytest.fixture()
def anon_client(client):
    """Unauthenticated client carrying a valid CSRF token.

    Without it, CSRFMiddleware (which runs ahead of auth) answers every
    state-changing request with 403 and the auth guard under test is never
    reached.
    """
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    return client


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

    @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),
    )


class TestGetCurrentConfig:
    """Tests for /settings/current-config endpoint."""

    def test_requires_authentication(self, client):
        """Should require authentication."""
        response = client.get(f"{API_PREFIX}/settings/current-config")
        # Should redirect to login or return 401
        assert response.status_code == 401, response.status_code

    def test_returns_config_when_authenticated(self, authenticated_client):
        """Should return config when authenticated."""
        with patch(f"{API}.get_user_db_session") as mock_session_ctx:
            mock_session = MagicMock()
            mock_session_ctx.return_value.__enter__ = MagicMock(
                return_value=mock_session
            )
            mock_session_ctx.return_value.__exit__ = MagicMock(
                return_value=None
            )

            with patch(f"{API}.SettingsManager") as mock_sm:
                mock_instance = MagicMock()
                mock_instance.get_setting.side_effect = lambda key, default: {
                    "llm.provider": "ollama",
                    "llm.model": "llama3",
                    "search.tool": "searxng",
                    "search.iterations": 8,
                    "search.questions_per_iteration": 5,
                    "search.search_strategy": "focused_iteration",
                }.get(key, default)
                mock_sm.return_value = mock_instance

                response = authenticated_client.get(
                    f"{API_PREFIX}/settings/current-config"
                )

                assert response.status_code == 200
                data = response.json()
                assert data["success"] is True
                assert "config" in data
                assert data["config"]["provider"] == "ollama"


class TestApiStartResearch:
    """Tests for /start endpoint."""

    def test_requires_authentication(self, anon_client):
        """Should require authentication."""
        response = anon_client.post(
            f"{API_PREFIX}/start", json={"query": "test query", "mode": "quick"}
        )
        assert response.status_code == 401, response.status_code

    def test_requires_query(self, authenticated_client):
        """Should require query parameter."""
        response = authenticated_client.post(
            f"{API_PREFIX}/start", json={"mode": "quick"}
        )
        assert response.status_code == 400
        data = response.json()
        assert "Query is required" in data.get("message", "")

    def test_empty_query_rejected(self, authenticated_client):
        """Should reject empty query."""
        response = authenticated_client.post(
            f"{API_PREFIX}/start", json={"query": "", "mode": "quick"}
        )
        assert response.status_code == 400

    def test_starts_research_successfully(self, authenticated_client):
        """Should delegate to start_research and return success."""
        with patch(
            "local_deep_research.web.routers.research.start_research",
            new_callable=AsyncMock,
        ) as mock_start:
            mock_start.return_value = {
                "status": "success",
                "research_id": "test-uuid",
            }

            response = authenticated_client.post(
                f"{API_PREFIX}/start",
                json={"query": "What is AI?", "mode": "quick"},
            )

            assert response.status_code == 200
            data = response.json()
            assert data["status"] == "success"
            assert data["research_id"] == "test-uuid"
            mock_start.assert_called_once()

    def test_starts_research_queued(self, authenticated_client):
        """Should delegate to start_research and return queued status."""
        with patch(
            "local_deep_research.web.routers.research.start_research",
            new_callable=AsyncMock,
        ) as mock_start:
            mock_start.return_value = {
                "status": "queued",
                "research_id": "test-uuid",
                "queue_position": 1,
            }

            response = authenticated_client.post(
                f"{API_PREFIX}/start",
                json={"query": "What is AI?", "mode": "quick"},
            )

            assert response.status_code == 200
            data = response.json()
            assert data["status"] == "queued"
            assert data["research_id"] == "test-uuid"
            mock_start.assert_called_once()


class TestApiResearchStatus:
    """Tests for /status/<research_id> endpoint."""

    def test_requires_authentication(self, client):
        """Should require authentication."""
        response = client.get(f"{API_PREFIX}/status/test-id")
        assert response.status_code == 401, response.status_code

    def test_returns_404_for_nonexistent(self, authenticated_client):
        """Should return 404 for non-existent research."""
        with patch(f"{API}.get_user_db_session") as mock_session_ctx:
            mock_session = MagicMock()
            mock_session_ctx.return_value.__enter__ = MagicMock(
                return_value=mock_session
            )
            mock_session_ctx.return_value.__exit__ = MagicMock(
                return_value=None
            )

            mock_query = MagicMock()
            mock_query.filter_by.return_value.first.return_value = None
            mock_session.query.return_value = mock_query

            response = authenticated_client.get(
                f"{API_PREFIX}/status/nonexistent-id"
            )
            assert response.status_code == 404

    def test_returns_status_for_existing(self, authenticated_client):
        """Should return status for existing research."""
        with patch(f"{API}.get_user_db_session") as mock_session_ctx:
            mock_session = MagicMock()
            mock_session_ctx.return_value.__enter__ = MagicMock(
                return_value=mock_session
            )
            mock_session_ctx.return_value.__exit__ = MagicMock(
                return_value=None
            )

            mock_research = MagicMock()
            mock_research.status = "completed"
            mock_research.progress = 100
            mock_research.completed_at = "2024-01-01T00:00:00"
            mock_research.report_path = "/path/to/report.md"
            mock_research.research_meta = {"key": "value"}

            mock_query = MagicMock()
            mock_query.filter_by.return_value.first.return_value = mock_research
            mock_session.query.return_value = mock_query

            response = authenticated_client.get(
                f"{API_PREFIX}/status/existing-id"
            )

            assert response.status_code == 200
            data = response.json()
            assert data["status"] == "completed"
            assert data["progress"] == 100


class TestApiTerminateResearch:
    """Tests for /terminate/<research_id> endpoint."""

    def test_requires_authentication(self, anon_client):
        """Should require authentication."""
        response = anon_client.post(f"{API_PREFIX}/terminate/test-id")
        assert response.status_code == 401, response.status_code

    def test_terminates_research(self, authenticated_client):
        """Should terminate research."""
        with patch(f"{API}.cancel_research") as mock_cancel:
            mock_cancel.return_value = True

            response = authenticated_client.post(
                f"{API_PREFIX}/terminate/test-id"
            )

            assert response.status_code == 200
            data = response.json()
            assert data["status"] == "success"

    def test_handles_not_found(self, authenticated_client):
        """Should handle research not found."""
        with patch(f"{API}.cancel_research") as mock_cancel:
            mock_cancel.return_value = False

            response = authenticated_client.post(
                f"{API_PREFIX}/terminate/nonexistent"
            )

            assert response.status_code == 200
            data = response.json()
            assert "not found or already completed" in data["message"]


class TestApiGetResources:
    """Tests for GET /resources/<research_id> endpoint."""

    def test_requires_authentication(self, client):
        """Should require authentication."""
        response = client.get(f"{API_PREFIX}/resources/test-id")
        assert response.status_code == 401, response.status_code

    def test_returns_resources(self, authenticated_client):
        """Should return resources for research."""
        with patch(f"{API}.get_resources_for_research") as mock_get:
            mock_get.return_value = [
                {"id": 1, "title": "Resource 1", "url": "https://example.com"}
            ]

            response = authenticated_client.get(
                f"{API_PREFIX}/resources/test-id"
            )

            assert response.status_code == 200
            data = response.json()
            assert data["status"] == "success"
            assert len(data["resources"]) == 1


class TestApiAddResource:
    """Tests for POST /resources/<research_id> endpoint."""

    def test_requires_authentication(self, anon_client):
        """Should require authentication."""
        response = anon_client.post(
            f"{API_PREFIX}/resources/test-id",
            json={"title": "Test", "url": "https://example.com"},
        )
        assert response.status_code == 401, response.status_code

    def test_requires_title_and_url(self, authenticated_client):
        """Should require both title and URL."""
        response = authenticated_client.post(
            f"{API_PREFIX}/resources/test-id", json={"title": "Test only"}
        )
        assert response.status_code == 400

        response = authenticated_client.post(
            f"{API_PREFIX}/resources/test-id",
            json={"url": "https://example.com"},
        )
        assert response.status_code == 400

    def test_returns_404_for_nonexistent_research(self, authenticated_client):
        """Should return 404 if research doesn't exist."""
        with (
            patch(f"{API}.get_user_db_session") as mock_session_ctx,
            patch(
                "local_deep_research.security.ssrf_validator.validate_url",
                return_value=True,
            ),
        ):
            mock_session = MagicMock()
            mock_session_ctx.return_value.__enter__ = MagicMock(
                return_value=mock_session
            )
            mock_session_ctx.return_value.__exit__ = MagicMock(
                return_value=None
            )

            mock_query = MagicMock()
            mock_query.filter_by.return_value.first.return_value = None
            mock_session.query.return_value = mock_query

            response = authenticated_client.post(
                f"{API_PREFIX}/resources/nonexistent",
                json={"title": "Test", "url": "https://example.com"},
            )

            assert response.status_code == 404


class TestApiDeleteResource:
    """Tests for DELETE /resources/<research_id>/delete/<resource_id> endpoint."""

    def test_requires_authentication(self, anon_client):
        """Should require authentication."""
        response = anon_client.delete(
            f"{API_PREFIX}/resources/test-id/delete/1"
        )
        assert response.status_code == 401, response.status_code

    def test_deletes_resource(self, authenticated_client):
        """Should delete resource successfully."""
        with patch(f"{API}.delete_resource") as mock_delete:
            mock_delete.return_value = True

            response = authenticated_client.delete(
                f"{API_PREFIX}/resources/test-id/delete/1"
            )

            assert response.status_code == 200
            data = response.json()
            assert data["status"] == "success"

    def test_returns_404_for_nonexistent(self, authenticated_client):
        """Should return 404 for non-existent resource."""
        with patch(f"{API}.delete_resource") as mock_delete:
            # The service signals "no such resource" with ValueError; it never
            # returns False (see the module docstring).
            mock_delete.side_effect = ValueError(
                "Resource with ID 999 not found"
            )

            response = authenticated_client.delete(
                f"{API_PREFIX}/resources/test-id/delete/999"
            )

            assert response.status_code == 404


class TestCheckOllamaStatus:
    """Tests for /check/ollama_status endpoint."""

    def test_requires_authentication(self, client):
        """Should require authentication."""
        response = client.get(f"{API_PREFIX}/check/ollama_status")
        assert response.status_code == 401, response.status_code

    def test_non_ollama_provider(self, authenticated_client):
        """Should return running=True for non-Ollama providers."""
        p1, p2 = _patch_llm_settings({"llm.provider": "openai"})
        with p1, p2:
            response = authenticated_client.get(
                f"{API_PREFIX}/check/ollama_status"
            )

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

    def test_ollama_connection_error(self, authenticated_client):
        """Should return running=False on connection error."""
        import requests

        p1, p2 = _patch_llm_settings(
            {
                "llm.provider": "ollama",
                "llm.ollama.url": "http://localhost:11434",
            }
        )
        with (
            p1,
            p2,
            patch(
                f"{API}.safe_get",
                side_effect=requests.exceptions.ConnectionError(
                    "Connection refused"
                ),
            ),
        ):
            response = authenticated_client.get(
                f"{API_PREFIX}/check/ollama_status"
            )

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

    def test_ollama_running(self, authenticated_client):
        """Should return running=True when Ollama is running."""
        mock_response = MagicMock()
        mock_response.status_code = 200
        mock_response.json.return_value = {"models": [{"name": "llama3"}]}

        p1, p2 = _patch_llm_settings(
            {
                "llm.provider": "ollama",
                "llm.ollama.url": "http://localhost:11434",
            }
        )
        with p1, p2, patch(f"{API}.safe_get", return_value=mock_response):
            response = authenticated_client.get(
                f"{API_PREFIX}/check/ollama_status"
            )

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


class TestCheckOllamaModel:
    """Tests for /check/ollama_model endpoint."""

    def test_requires_authentication(self, client):
        """Should require authentication."""
        response = client.get(f"{API_PREFIX}/check/ollama_model")
        assert response.status_code == 401, response.status_code

    def test_non_ollama_provider(self, authenticated_client):
        """Should return available=True for non-Ollama providers."""
        p1, p2 = _patch_llm_settings({"llm.provider": "openai"})
        with p1, p2:
            response = authenticated_client.get(
                f"{API_PREFIX}/check/ollama_model"
            )

        assert response.status_code == 200
        data = response.json()
        assert data["available"] is True
        assert data["provider"] == "openai"

    def test_model_available(self, authenticated_client):
        """Should return available=True when model exists."""
        mock_response = MagicMock()
        mock_response.status_code = 200
        mock_response.json.return_value = {"models": [{"name": "llama3"}]}

        p1, p2 = _patch_llm_settings(
            {
                "llm.provider": "ollama",
                "llm.model": "llama3",
                "llm.ollama.url": "http://localhost:11434",
            }
        )
        with p1, p2, patch(f"{API}.safe_get", return_value=mock_response):
            response = authenticated_client.get(
                f"{API_PREFIX}/check/ollama_model"
            )

        assert response.status_code == 200
        data = response.json()
        assert data["available"] is True
        assert data["model"] == "llama3"

    def test_model_not_available(self, authenticated_client):
        """Should return available=False when model doesn't exist."""
        mock_response = MagicMock()
        mock_response.status_code = 200
        mock_response.json.return_value = {"models": [{"name": "llama3"}]}

        p1, p2 = _patch_llm_settings(
            {
                "llm.provider": "ollama",
                "llm.model": "nonexistent-model",
                "llm.ollama.url": "http://localhost:11434",
            }
        )
        with p1, p2, patch(f"{API}.safe_get", return_value=mock_response):
            response = authenticated_client.get(
                f"{API_PREFIX}/check/ollama_model"
            )

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


def test_the_routes_this_file_drives_are_mounted_from_the_expected_module(app):
    """Pin the wiring, not just the responses.

    Every assertion above goes through HTTP, so they would all still pass if
    these paths were re-pointed at a different module returning the same
    shapes. This audit found guards that survived the port but stopped being
    *reached* (#5959), so the wiring is asserted separately.
    """
    from local_deep_research.web.routers import api as _sut

    declared = {r.path for r in _sut.router.routes if getattr(r, "path", None)}
    mounted = {r.path for r in app.routes if getattr(r, "path", None)}
    missing = declared - mounted
    assert not missing, f"declared but not mounted: {sorted(missing)}"
    assert declared, "the module under test declares no routes"