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.

9.0 KB
Raw Blame
"""News API input validation (originally PR #1939), re-ported to FastAPI.

Verifies:
- ``limit`` is clamped to ``[1, NEWS_FEED_MAX_LIMIT]``
- JSON-body endpoints reject a non-dict body instead of handing it onward

HISTORY — why this file was rewritten rather than edited:

The original suite inspected ``web/routes/news_routes.py`` SOURCE for the
literal string ``"max(1, min(limit, 200))"``. When the Flask blueprint was
deleted the whole module was skipped at import with ``allow_module_level=True``
and a "re-port pending" note, which silenced all 7 tests. A skipped file is
invisible in a green run, so the clamp and the body guards were unprotected
without anything looking wrong.

The live module is ``web/routers/news_flask_api.py`` (the migration kept the
legacy name). Two behaviour notes found while re-porting:

* the bound tightened from a hardcoded 200 to ``NEWS_FEED_MAX_LIMIT = 100``,
  so these assert against the constant rather than a magic number;
* the ``@require_json_body`` decorator is gone. Each handler now does
  ``data = await request.json()`` then ``if not isinstance(data, dict)``, so
  the guard is pinned by BEHAVIOUR (send a non-dict, require a 400) instead of
  by grepping for a decorator name.

Asserting on behaviour rather than on source text is the point of the re-port:
the original tests would have passed against a handler that contained the right
string and did nothing with it.
"""

from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient

from local_deep_research.web.routers.news_flask_api import (
    NEWS_FEED_MAX_LIMIT,
)


@pytest.fixture(scope="module")
def client():
    """Authenticated client on its own source IP.

    The per-IP rate limiter buckets by client address, so a distinct
    X-Forwarded-For keeps this module from draining a bucket shared with
    other test modules (registration is limited to a few per hour).
    """
    import uuid

    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)
    c.headers.update({"X-Forwarded-For": "10.44.7.21"})

    user = f"test_newsval_{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"Auth bootstrap failed: expected 302, got {resp.status_code}: "
            f"{resp.text[:300]}"
        )

    token = c.get("/auth/csrf-token")
    if token.status_code == 200 and token.json().get("csrf_token"):
        c.headers.update({"X-CSRFToken": token.json()["csrf_token"]})

    yield c

    c.post("/auth/logout", follow_redirects=False)


class TestLimitClamping:
    """The clamp is asserted by capturing what the handler passes DOWNSTREAM.

    A response-length assertion would be vacuous here: with an empty news
    database every limit returns zero items, so `len(items) <= 100` passes
    even with the clamp deleted. Patching the downstream call and reading the
    argument it actually received is the only assertion that binds.
    """

    @pytest.mark.parametrize(
        "requested,expected",
        [
            ("99999", NEWS_FEED_MAX_LIMIT),  # above the ceiling
            (str(NEWS_FEED_MAX_LIMIT + 1), NEWS_FEED_MAX_LIMIT),  # just above
            ("0", 1),  # below the floor
            ("-5", 1),  # negative
            ("7", 7),  # in range, passed through untouched
        ],
    )
    def test_subscription_history_clamps_limit(
        self, client, requested, expected
    ):
        with patch(
            "local_deep_research.web.routers.news_flask_api.api."
            "get_subscription_history",
            return_value={"subscription": {}, "history": [], "total_runs": 0},
        ) as spy:
            client.get(
                "/news/api/subscriptions/"
                "123e4567-e89b-12d3-a456-426614174000/history",
                params={"limit": requested},
            )

        assert spy.called, (
            "the handler never reached api.get_subscription_history — the "
            "clamp assertion below would be vacuous"
        )
        # signature: get_subscription_history(subscription_id, limit, username=...)
        assert spy.call_args.args[1] == expected, (
            f"limit={requested!r} reached the service layer as "
            f"{spy.call_args.args[1]!r}, expected {expected!r}"
        )

    def test_non_numeric_limit_does_not_crash(self, client):
        """A junk limit must fall back to the default, not 500."""
        with patch(
            "local_deep_research.web.routers.news_flask_api.api."
            "get_subscription_history",
            return_value={"subscription": {}, "history": [], "total_runs": 0},
        ) as spy:
            resp = client.get(
                "/news/api/subscriptions/"
                "123e4567-e89b-12d3-a456-426614174000/history",
                params={"limit": "'; DROP TABLE--"},
            )

        assert resp.status_code != 500, resp.text[:300]
        assert spy.called
        limit = spy.call_args.args[1]
        assert isinstance(limit, int) and 1 <= limit <= NEWS_FEED_MAX_LIMIT, (
            f"non-numeric limit resolved to {limit!r}, which is outside "
            f"[1, {NEWS_FEED_MAX_LIMIT}]"
        )

    def test_ceiling_is_not_wider_than_main(self):
        """main hardcoded 200; the port tightened it. Guard against it
        silently widening back."""
        assert 1 <= NEWS_FEED_MAX_LIMIT <= 200


class TestJSONBodyValidation:
    """Endpoints must reject a non-dict JSON body.

    main asserted this by grepping each handler for ``@require_json_body``.
    That decorator no longer exists — the handlers inline
    ``if not isinstance(data, dict)`` — so a source-text check would now fail
    for the wrong reason. These drive the real routes instead, which is what
    the original tests were a proxy for.
    """

    # (method, path) for each handler the deleted suite named. Kept as one
    # table so a new unguarded body endpoint is a one-line addition.
    _UUID = "123e4567-e89b-12d3-a456-426614174000"
    BODY_ROUTES = [
        ("post", "/news/api/subscribe"),  # create_subscription
        ("put", f"/news/api/subscriptions/{_UUID}"),  # update_subscription
        ("post", "/news/api/feedback/card-1"),  # submit_feedback
        ("post", "/news/api/preferences"),  # save_preferences
        ("post", "/news/api/vote"),  # vote_on_news
        ("post", "/news/api/feedback/batch"),  # get_batch_feedback
        ("post", "/news/api/subscription/folders"),  # create_folder
        ("post", "/news/api/search-history"),  # add_search_history
    ]

    @pytest.mark.parametrize("method,path", BODY_ROUTES)
    @pytest.mark.parametrize(
        "body", ["null", "[]", '"a string"', "123", "true"]
    )
    def test_non_dict_json_body_is_rejected(self, client, method, path, body):
        resp = getattr(client, method)(
            path,
            content=body.encode(),
            headers={"Content-Type": "application/json"},
        )

        assert resp.status_code == 400, (
            f"{method.upper()} {path} accepted non-dict body {body!r} "
            f"with {resp.status_code} — the isinstance(data, dict) guard is "
            f"missing or bypassed. Body: {resp.text[:200]}"
        )

    @pytest.mark.parametrize(
        "method,path",
        # research_news_item takes an OPTIONAL body (`await request.json()
        # or {}`), so `null` and `[]` are legitimately coerced to `{}` and it
        # is excluded from the non-dict test above. Malformed bytes are still
        # a 400 for it, so it belongs here.
        BODY_ROUTES + [("post", "/news/api/research/card-1")],
    )
    def test_malformed_json_is_rejected(self, client, method, path):
        resp = getattr(client, method)(
            path,
            content=b"{not valid json",
            headers={"Content-Type": "application/json"},
        )

        assert resp.status_code == 400, (
            f"{method.upper()} {path} returned {resp.status_code} for "
            f"malformed JSON, expected 400: {resp.text[:200]}"
        )

    def test_a_dict_body_is_not_rejected_by_the_guard(self, client):
        """Control: the guard must reject non-dicts, not everything.

        Without this, a handler that 400'd unconditionally would satisfy
        every assertion above.
        """
        resp = client.post("/news/api/preferences", json={"preferences": {}})

        assert resp.status_code != 400, (
            "a well-formed dict body was rejected by the JSON guard — the "
            "tests above would then pass for the wrong reason"
        )