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.

4.0 KB
Raw Blame
"""Smoke coverage for the FastAPI Zotero router (web/routers/zotero.py).

Ported from the Flask ``zotero_routes`` blueprint (feature #4723) during the
FastAPI migration. The blueprint had no route-level test; this pins the
request-boundary contracts the UI relies on:

- every endpoint requires authentication,
- endpoints that only read local settings/state (config, status) return 200
  even when Zotero is unconfigured,
- endpoints that reach the Zotero API (collections, groups, sync) return a
  clean 400 — not an opaque 500 — when Zotero is unconfigured.

``ZoteroSyncService`` is exercised for real against the freshly-registered
(unconfigured) user's encrypted DB; no network call is reached because the
``is_configured`` guard short-circuits first.
"""

import uuid

import pytest
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def auth_client():
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)
    user = f"zotero_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    def _csrf():
        c.get("/auth/login")
        r = c.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = c.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Login bootstrap failed: expected 302, got {resp.status_code}: "
            f"{resp.text[:500]}"
        )
    yield c


def test_endpoints_require_auth():
    from local_deep_research.web.fastapi_app import app

    anon = TestClient(app, raise_server_exceptions=False)
    for path in (
        "/library/api/zotero/config",
        "/library/api/zotero/status",
        "/library/api/zotero/collections",
        "/library/api/zotero/groups",
    ):
        r = anon.get(path, follow_redirects=False)
        # Unauthenticated: never a 200 payload and never a server error.
        assert r.status_code != 200, f"{path} served an anonymous 200"
        assert r.status_code < 500, (
            f"{path} 5xx'd for anonymous: {r.text[:150]}"
        )


def test_config_reports_unconfigured(auth_client):
    r = auth_client.get("/library/api/zotero/config")
    assert r.status_code == 200
    body = r.json()
    assert body["success"] is True
    # Fresh user: Zotero is off and not configured, and no key is present.
    assert body["configured"] is False
    assert body["has_api_key"] is False


def test_status_returns_empty_when_unconfigured(auth_client):
    r = auth_client.get("/library/api/zotero/status")
    assert r.status_code == 200
    body = r.json()
    assert body["success"] is True
    assert body["collections"] == []


@pytest.mark.parametrize(
    "path", ["/library/api/zotero/collections", "/library/api/zotero/groups"]
)
def test_api_endpoints_400_when_unconfigured(auth_client, path):
    # These reach out to the Zotero API; unconfigured must be a clean 400,
    # not an opaque 500 (the is_configured guard short-circuits before any
    # network call).
    r = auth_client.get(path)
    assert r.status_code == 400
    assert r.json()["success"] is False


def test_sync_400_when_unconfigured(auth_client):
    auth_client.get("/auth/login")
    token = auth_client.get("/auth/csrf-token").json().get("csrf_token", "")
    r = auth_client.post(
        "/library/api/zotero/sync", headers={"X-CSRFToken": token}
    )
    assert r.status_code == 400
    body = r.json()
    assert body["success"] is False
    assert "not enabled/configured" in body["error"]


def test_zotero_page_renders(auth_client):
    r = auth_client.get("/library/zotero")
    assert r.status_code == 200
    assert "text/html" in r.headers.get("content-type", "")