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.

7.6 KB
Raw Blame
"""Live integration tests for the Zotero Web API client.

The unit tests in ``test_client.py`` mock the HTTP layer, so they only verify
the client against our *assumptions* about the API. These tests hit the REAL
Zotero Web API (``api.zotero.org``) to confirm those assumptions still hold and
the client works end-to-end.

Marked ``@pytest.mark.integration`` — skipped in CI mock mode; run with::

    LDR_TESTING_WITH_MOCKS=false pytest \
        tests/research_library/zotero/test_client_integration.py -v

Two tiers:

* **Contract (no credentials).** Read a real *public* Zotero group and assert
  the live response shapes + headers are exactly what the client parses. Needs
  only network access; catches a Zotero-side API change. Skips (never fails) if
  the public group or the API is unreachable.
* **Full client (needs a key).** Set ``ZOTERO_TEST_API_KEY`` (read-only is
  enough) to exercise the real client end-to-end against your own library.
  Optionally set ``ZOTERO_TEST_LIBRARY_TYPE`` / ``ZOTERO_TEST_LIBRARY_ID`` to
  target a specific library instead of the key owner's user library.
"""

import os

import pytest
import requests

from local_deep_research.research_library.zotero.client import (
    ZOTERO_API_BASE,
    ZOTERO_API_VERSION,
    ZoteroClient,
    ZoteroItem,
)
from local_deep_research.security import SafeSession

pytestmark = pytest.mark.integration

# A stable, read-only PUBLIC Zotero group for the no-credential contract checks
# (overridable in case it ever goes away).
_PUBLIC_GROUP_ID = os.environ.get("ZOTERO_TEST_PUBLIC_GROUP", "4507109")
_API_KEY = os.environ.get("ZOTERO_TEST_API_KEY")


def _get(path, params=None):
    """GET a public (no-key) Zotero endpoint through the project's SafeSession
    — the same HTTP layer the client uses. Returns the response, or ``None`` if
    unreachable so callers can SKIP rather than fail on a network blip."""
    url = f"{ZOTERO_API_BASE}{path}"
    try:
        with SafeSession() as session:
            session.headers.update({"Zotero-API-Version": ZOTERO_API_VERSION})
            return session.get(url, params=params, timeout=30)
    except (
        requests.exceptions.Timeout,
        requests.exceptions.ConnectionError,
    ):
        return None


def _public_group_or_skip(path, params=None):
    r = _get(f"/groups/{_PUBLIC_GROUP_ID}{path}", params=params)
    if r is None or r.status_code != 200:
        pytest.skip(
            f"public Zotero group {_PUBLIC_GROUP_ID} unavailable "
            f"(HTTP {getattr(r, 'status_code', 'n/a')})"
        )
    return r


# --- Tier 1: contract against the live API (no credentials) -----------------


def test_live_api_reachable_and_speaks_v3():
    r = _get("/itemTypes")  # stable, no-auth schema endpoint
    if r is None:
        pytest.skip("Zotero API unreachable from this environment")
    assert r.status_code == 200
    assert r.headers.get("Zotero-API-Version") == ZOTERO_API_VERSION
    assert isinstance(r.json(), list)


def test_live_items_shape_matches_client_parsing():
    # get_items reads entry['key'], entry['version'], entry['data']['itemType'].
    # The paginators read the Total-Results / Last-Modified-Version headers.
    r = _public_group_or_skip("/items/top", {"format": "json", "limit": 3})
    assert "Total-Results" in r.headers
    assert "Last-Modified-Version" in r.headers
    entries = r.json()
    assert isinstance(entries, list) and entries
    for entry in entries:
        assert isinstance(entry.get("key"), str) and entry["key"]
        assert isinstance(entry.get("version"), int)
        data = entry.get("data")
        assert isinstance(data, dict)
        assert isinstance(data.get("itemType"), str) and data["itemType"]


def test_live_versions_shape_is_key_to_int():
    # get_top_item_versions parses format=versions as {itemKey: intVersion}.
    r = _public_group_or_skip("/items/top", {"format": "versions", "limit": 5})
    obj = r.json()
    assert isinstance(obj, dict) and obj
    for key, version in obj.items():
        assert isinstance(key, str) and key
        assert int(version) >= 0  # client coerces the same way


def test_live_collections_shape_matches_client_parsing():
    # get_collections reads entry['key'] and entry['data']['name'].
    r = _public_group_or_skip("/collections", {"format": "json", "limit": 1})
    entries = r.json()
    assert isinstance(entries, list)
    for entry in entries:
        assert isinstance(entry.get("key"), str) and entry["key"]
        assert isinstance(entry.get("data", {}).get("name"), str)


def test_live_bad_key_is_rejected_403():
    # The client maps 403 -> ZoteroAuthError; confirm the live API really does
    # reject an invalid key with 403 (even for otherwise-public content).
    r = _get(
        f"/groups/{_PUBLIC_GROUP_ID}/items/top",
        params={"limit": 1},
    )
    if r is None:
        pytest.skip("Zotero API unreachable")
    # (no key on _get -> 200; re-request WITH a bogus key -> 403)
    try:
        with SafeSession() as session:
            session.headers.update(
                {
                    "Zotero-API-Version": ZOTERO_API_VERSION,
                    "Zotero-API-Key": "bogus_invalid_key_for_test",
                }
            )
            bad = session.get(
                f"{ZOTERO_API_BASE}/groups/{_PUBLIC_GROUP_ID}/items/top",
                params={"limit": 1},
                timeout=30,
            )
    except (
        requests.exceptions.Timeout,
        requests.exceptions.ConnectionError,
    ):
        pytest.skip("Zotero API unreachable")
    assert bad.status_code == 403


# --- Tier 2: full client end-to-end (needs a real API key) ------------------

_needs_key = pytest.mark.skipif(
    not _API_KEY,
    reason="set ZOTERO_TEST_API_KEY to run the full-client live tests",
)


def _live_client():
    """Build a read-only client for the key owner's user library (default) or
    the library named by ``ZOTERO_TEST_LIBRARY_TYPE`` / ``_ID``."""
    lib_type = os.environ.get("ZOTERO_TEST_LIBRARY_TYPE")
    lib_id = os.environ.get("ZOTERO_TEST_LIBRARY_ID")
    if not lib_id:
        # Resolve the key owner's numeric userID via /keys/current.
        probe = ZoteroClient(
            api_key=_API_KEY, library_type="user", library_id="1"
        )
        try:
            info = probe._request("/keys/current", prefixed=False).json()
        finally:
            probe.close()
        lib_id = str(info.get("userID") or "")
        lib_type = "user"
        assert lib_id, "Zotero /keys/current returned no userID"
    return ZoteroClient(
        api_key=_API_KEY,
        library_type=lib_type or "user",
        library_id=lib_id,
    )


@_needs_key
def test_full_client_connection_and_listing():
    client = _live_client()
    try:
        version = client.test_connection()
        assert isinstance(version, int) and version >= 0
        assert isinstance(client.get_collections(), list)
        versions, lib = client.get_top_item_versions()
        assert isinstance(versions, dict)
        assert isinstance(lib, int)
    finally:
        client.close()


@_needs_key
def test_full_client_item_and_children_roundtrip():
    client = _live_client()
    try:
        versions, _ = client.get_top_item_versions()
        if not versions:
            pytest.skip("target Zotero library has no top-level items")
        keys = list(versions)[:3]
        items = client.get_items(keys)
        assert {it.key for it in items} == set(keys)
        for it in items:
            assert isinstance(it, ZoteroItem)
            assert it.item_type and isinstance(it.data, dict)
        # Children of the first item (may be empty) — must round-trip, not raise.
        assert isinstance(client.get_children(keys[0]), list)
    finally:
        client.close()