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.

11.0 KB
Raw Blame
"""``/metrics/api/journal-data/*`` — the message-construction block.

Ported from ``tests/web/routes/test_metrics_routes.py``, deleted by the
Flask->FastAPI migration.

``tests/security/test_metrics_hostile_input_fastapi.py`` covers this
endpoint well on the *refusal* side: the egress-scope 403s
(``PRIVATE_ONLY`` / ``STRICT`` / a corrupt scope), the
``"unprotected"``-with-gate-off fallback, and a canary proving no
exception text or traceback is echoed. Every one of its stubs, however,
passes ``counts=None`` and then asserts only ``success is True``.

So the block that builds the user-facing string
(``metrics.py``, the ``if counts is not None:`` branch) has never run
under test. That block is the whole reason ``internal_message`` from the
downloader is discarded rather than returned: the response is
safe-by-construction because it is assembled locally from integers and
developer-authored source labels. A refactor that "simplified" it back to
``return {"success": True, "message": internal_message}`` would keep every
existing test green — including the canary, which only fires on the
*failure* path — while re-opening the leak the discard exists to prevent.

Also recovered here: the download route's non-exceptional failure branch
(a failed download is a **200** with ``success: False``, not a 5xx — the
button reports the outcome rather than erroring), its outer 500, and
``GET /api/journal-data/status``.

The route functions are called directly. ``api_journal_data_download``
carries a slowapi ``shared_limit`` (2 per hour, per user) that rejects a
``Mock``, so a real ``starlette.requests.Request`` is built.
"""

import asyncio
import json
from unittest.mock import MagicMock, patch

import pytest
from starlette.requests import Request

MODULE = "local_deep_research.web.routers.metrics"
DOWNLOADER = "local_deep_research.journal_quality.downloader"
SETTINGS_MANAGER = "local_deep_research.utilities.db_utils.get_settings_manager"

#: A string that must never survive into the response body.
CANARY = "TAINT-CANARY-8f3a9e-do-not-leak"


def _request(body=None, *, raw_body=None):
    scope = {
        "type": "http",
        "method": "POST",
        "path": "/metrics/api/journal-data/download",
        "query_string": b"",
        "headers": [(b"content-type", b"application/json")],
        "client": ("10.0.0.1", 1234),
    }
    payload = (
        raw_body
        if raw_body is not None
        else json.dumps(body if body is not None else {}).encode()
    )
    sent = {"done": False}

    async def receive():
        if sent["done"]:
            return {"type": "http.disconnect"}
        sent["done"] = True
        return {"type": "http.request", "body": payload, "more_body": False}

    return Request(scope, receive)


def _adaptive_scope():
    """A settings manager reporting an egress scope that permits the
    download, so these tests exercise the post-policy code."""
    manager = MagicMock()
    manager.get_setting.side_effect = lambda key, default=None: (
        "adaptive" if key == "policy.egress_scope" else default
    )
    return patch(SETTINGS_MANAGER, return_value=manager)


def _call_download(download_result, counts, body=None):
    from local_deep_research.web.routers.metrics import (
        api_journal_data_download,
    )

    with (
        _adaptive_scope(),
        patch(
            f"{DOWNLOADER}.download_journal_data", return_value=download_result
        ),
        patch(
            f"{DOWNLOADER}.get_download_state", return_value={"counts": counts}
        ),
    ):
        return asyncio.run(
            api_journal_data_download(_request(body), username="alice")
        )


def _body(resp):
    return json.loads(resp.body)


class TestJournalDataDownloadMessage:
    def test_the_message_is_rebuilt_from_structured_counts(self):
        """Every source's count and its developer-authored label appear in
        the message — and the downloader's own string does not."""
        result = _call_download(
            (True, f"Fetched 42 OpenAlex sources ... {CANARY}"),
            counts={
                "openalex": 42,
                "doaj": 7,
                "jabref": 3,
                "predatory": 1,
                "institutions": 5,
            },
            body={"force": True},
        )

        assert result["success"] is True
        message = result["message"]
        assert CANARY not in message
        assert "42 OpenAlex sources" in message
        assert "7 DOAJ journals" in message
        assert "1 predatory entries" in message
        assert "3 abbreviations" in message
        assert "5 institutions" in message
        assert "Database rebuilt successfully" in message

    def test_a_missing_count_is_reported_as_zero_rather_than_none(self):
        """``int(counts.get(src.key) or 0)`` — a source the downloader did
        not report must render as 0, not ``None`` and not a KeyError."""
        result = _call_download(
            (True, "ok"), counts={"openalex": 9}, body={"force": True}
        )

        assert result["success"] is True
        assert "9 OpenAlex sources" in result["message"]
        assert "0 DOAJ journals" in result["message"]

    def test_no_counts_at_all_means_the_data_was_already_current(self):
        """``counts`` is None only on the downloader's early-return
        branch, where no fetch ran — a distinct message from a rebuild."""
        result = _call_download(
            (True, f"already up to date {CANARY}"), counts=None
        )

        assert result["success"] is True
        assert "already up to date" in result["message"].lower()
        assert CANARY not in result["message"]

    def test_a_failed_download_is_a_200_reporting_the_failure(self):
        """Not a 5xx: the button asks for an outcome and gets one. The
        downloader's message is still discarded on this path."""
        result = _call_download((False, f"boom {CANARY}"), counts=None)

        assert result == {"success": False, "message": "Download failed"}

    def test_an_unexpected_failure_is_a_500_that_still_says_nothing(self):
        from local_deep_research.web.routers.metrics import (
            api_journal_data_download,
        )

        with (
            _adaptive_scope(),
            patch(
                f"{DOWNLOADER}.download_journal_data",
                side_effect=RuntimeError(CANARY),
            ),
        ):
            resp = asyncio.run(
                api_journal_data_download(_request(), username="alice")
            )

        assert resp.status_code == 500
        body = _body(resp)
        assert body["success"] is False
        assert CANARY not in json.dumps(body)


class TestJournalDataDownloadForceValidation:
    @pytest.mark.parametrize("force", [False, True])
    def test_forwards_json_booleans_without_coercion(self, force):
        from local_deep_research.web.routers.metrics import (
            api_journal_data_download,
        )

        with (
            _adaptive_scope(),
            patch(
                f"{DOWNLOADER}.download_journal_data",
                return_value=(True, "ok"),
            ) as download,
            patch(
                f"{DOWNLOADER}.get_download_state",
                return_value={"counts": None},
            ),
        ):
            result = asyncio.run(
                api_journal_data_download(
                    _request({"force": force}), username="alice"
                )
            )

        assert result["success"] is True
        download.assert_called_once_with(force=force)

    @pytest.mark.parametrize("force", ["false", "true", 0, 1, None, [], {}])
    def test_rejects_non_boolean_force_without_starting_download(self, force):
        from local_deep_research.web.routers.metrics import (
            api_journal_data_download,
        )

        with (
            _adaptive_scope(),
            patch(f"{DOWNLOADER}.download_journal_data") as download,
        ):
            response = asyncio.run(
                api_journal_data_download(
                    _request({"force": force}), username="alice"
                )
            )

        assert response.status_code == 400
        assert _body(response) == {
            "success": False,
            "message": "force must be a boolean",
        }
        download.assert_not_called()

    @pytest.mark.parametrize("body", ["false", 1, [False]])
    def test_rejects_non_object_json_without_starting_download(self, body):
        from local_deep_research.web.routers.metrics import (
            api_journal_data_download,
        )

        with (
            _adaptive_scope(),
            patch(f"{DOWNLOADER}.download_journal_data") as download,
        ):
            response = asyncio.run(
                api_journal_data_download(_request(body), username="alice")
            )

        assert response.status_code == 400
        assert _body(response) == {
            "success": False,
            "message": "Request body must be valid JSON",
        }
        download.assert_not_called()

    def test_rejects_malformed_json_without_starting_download(self):
        from local_deep_research.web.routers.metrics import (
            api_journal_data_download,
        )

        with (
            _adaptive_scope(),
            patch(f"{DOWNLOADER}.download_journal_data") as download,
        ):
            response = asyncio.run(
                api_journal_data_download(
                    _request(raw_body=b'{"force":'), username="alice"
                )
            )

        assert response.status_code == 400
        assert _body(response) == {
            "success": False,
            "message": "Request body must be valid JSON",
        }
        download.assert_not_called()


class TestJournalDataStatus:
    def test_status_returns_the_downloader_state_verbatim(self):
        from local_deep_research.web.routers.metrics import (
            api_journal_data_status,
        )

        state = {"openalex": {"present": True, "size": 123}}
        with patch(f"{DOWNLOADER}.get_journal_data_status", return_value=state):
            result = api_journal_data_status(_request(), username="alice")

        assert result == state

    def test_status_failure_is_a_500(self):
        from local_deep_research.web.routers.metrics import (
            api_journal_data_status,
        )

        with patch(
            f"{DOWNLOADER}.get_journal_data_status",
            side_effect=RuntimeError("boom"),
        ):
            resp = api_journal_data_status(_request(), username="alice")

        assert resp.status_code == 500
        assert "error" in _body(resp)


class TestStarReviewsFailurePath:
    def test_a_database_failure_is_a_500_without_a_success_envelope(self):
        """``test_metrics_star_reviews.py`` covers the payload shape in
        depth against a real seeded DB, but never the failure tail."""
        from local_deep_research.web.routers.metrics import api_star_reviews

        with patch(
            f"{MODULE}.get_user_db_session", side_effect=RuntimeError("db down")
        ):
            resp = api_star_reviews(_request(), username="alice")

        assert resp.status_code == 500
        assert "error" in _body(resp)