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.

16.1 KB
Raw Blame
"""Success paths of the news API that only the deleted duplicate pinned.

Ported from ``tests/web/routes/test_news_routes.py`` (35 tests), which drove
the ``/api/news/*`` blueprint. That blueprint was deleted on this branch as
a documented duplicate of ``/news/api/*`` (``changelog.d/3299.breaking.md``
carries the migration table; ``tests/web/test_route_table_parity.py``'s
``EXPECTED_REMOVED`` fences the removal). The *behaviour* it tested did not
go away — it lives on the surviving ``/news/api/*`` routes in
``web/routers/news_flask_api.py``, so the assertions are re-aimed there
rather than dropped.

Re-aimed paths (from the changelog's own table):
``POST /api/news/subscriptions`` -> ``POST /news/api/subscribe``;
``POST /api/news/feedback`` -> ``POST /news/api/feedback/{card_id}``;
``POST /api/news/research`` -> ``POST /news/api/research/{card_id}``.

Superseded and NOT re-ported: the feed's limit/use_cache/focus parsing and
result passthrough, the wildcard-``subscription_id`` guards, the custom
endpoint SSRF guard on create and update, the subscriptions list, delete,
the history limit clamp, and every ``NewsAPIException`` envelope — see
``tests/news/test_news_router_contracts.py``,
``tests/news/test_news_input_validation.py``,
``tests/security/test_news_scheduler_isolation_fastapi.py`` and
``tests/web/routers/test_news_subscribe_exception_contract.py``.

What is recovered here is the plain success path of eight handlers, which
the branch's tests systematically skip. Three of them are textbook
sibling-branch traps:

* ``GET /news/api/categories`` — ``test_news_categories_error_scrub.py``
  is a sharp test *of the ``except NotImplementedException`` branch*.
  ``test_all_endpoints.py`` lists the path under ``KNOWN_NON_2XX``, so it
  actively asserts the route is not 200. The ``return
  api.get_news_categories()`` line has no test at all.
* ``POST /news/api/research/{card_id}`` and ``POST /news/api/preferences``
  — ``test_full_surface_smoke.py`` allows both a 501 because the backing
  ``news.api`` function currently always raises ``NotImplementedException``.
  Nothing checks what the route hands that function, so the ``depth``
  default and the ``preferences`` unwrapping are unguarded.
* ``GET|PUT /news/api/subscriptions/{id}`` and its ``/history`` — the
  ownership-scoping tests patch with a bare ``MagicMock`` return (which
  serialises as a 500) and assert only on the spy, so the 200 + body
  contract is untested.

Dropped as Flask-only or structurally gone:

* the four ``TestHandleApiErrorsDecorator`` tests — ``handle_api_errors``
  does not exist on the branch (each handler carries an inline
  ``except NewsAPIException: raise`` / ``except Exception``), and
  ``__name__`` preservation is a ``functools.wraps`` artefact with no
  FastAPI analogue. The behaviour the decorator existed for is covered by
  ``test_news_subscribe_exception_contract.py``;
* ``test_submit_feedback_missing_card_id`` and
  ``test_research_news_item_missing_card_id`` — ``card_id`` moved from the
  body into the path, so a missing id is now a routing 404, not a
  handler 400;
* ``test_update_subscription_patch`` — ``PATCH`` was removed deliberately
  (it is in ``EXPECTED_REMOVED``; use ``PUT``);
* the ``201`` in ``test_create_subscription_success`` — the surviving
  route answers 200 with ``subscription_id``, matching what ``/news/api``
  always did.
"""

import uuid

import pytest

API = "local_deep_research.news.api"

FEED = "/news/api/feed"
SUBSCRIBE = "/news/api/subscribe"
CATEGORIES = "/news/api/categories"
PREFERENCES = "/news/api/preferences"


@pytest.fixture
def subscription_id():
    """A well-formed UUID: the history route rejects anything else with a
    400 before it reaches the service, which would make these tests
    vacuous."""
    return str(uuid.uuid4())


class _Spy:
    """Records the call and returns a fixed value."""

    def __init__(self, result=None, exc=None):
        self.result = result
        self.exc = exc
        self.calls = []

    def __call__(self, *args, **kwargs):
        self.calls.append((args, kwargs))
        if self.exc is not None:
            raise self.exc
        return self.result

    @property
    def called_once(self):
        return len(self.calls) == 1


# ---------------------------------------------------------------------------
# GET /news/api/feed
# ---------------------------------------------------------------------------


def test_feed_turns_an_unexpected_exception_into_a_500(
    authenticated_client, monkeypatch
):
    """The generic ``except Exception`` tail of the feed handler.

    Its two neighbours are covered and this one is not: the
    ``if "error" in result`` return-value branch by
    ``test_news_router_contracts.py::TestFeedResultPassthrough::
    test_other_service_error_is_a_500``, and the
    ``except NewsAPIException: raise`` branch by
    ``test_news_subscribe_exception_contract.py``. Nothing drives a plain
    raised exception through, so neither the status nor the
    ``news_items: []`` fallback is pinned.
    """
    monkeypatch.setattr(
        f"{API}.get_news_feed", _Spy(exc=Exception("Database error"))
    )

    resp = authenticated_client.get(FEED)

    assert resp.status_code == 500, resp.text
    body = resp.json()
    assert "error" in body
    assert body["news_items"] == []
    # The scrub: the raw exception text must not reach the client.
    assert "Database error" not in resp.text


# ---------------------------------------------------------------------------
# POST /news/api/subscribe
# ---------------------------------------------------------------------------


def test_subscribe_forwards_every_optional_field_to_the_service(
    authenticated_client, monkeypatch
):
    """The handler reads fourteen fields off the body and hands them to
    ``api.create_subscription`` as keywords. Nothing on the branch posts
    more than ``query``, so a field silently dropped from that call —
    the exact shape of the ``normalize_provider`` regression in this same
    handler — would go unnoticed.
    """
    spy = _Spy(result={"status": "success", "subscription_id": "sub-123"})
    monkeypatch.setattr(f"{API}.create_subscription", spy)

    resp = authenticated_client.post(
        SUBSCRIBE,
        json={
            "query": "Test",
            "subscription_type": "search",
            "refresh_minutes": 60,
            "model_provider": "ollama",
            "model": "llama3",
            "search_strategy": "standard",
            "name": "My Subscription",
            "is_active": True,
            "search_engine": "searxng",
            "search_iterations": 3,
            "questions_per_iteration": 2,
        },
    )

    assert resp.status_code == 200, resp.text
    assert spy.called_once
    kwargs = spy.calls[0][1]
    assert kwargs["query"] == "Test"
    assert kwargs["subscription_type"] == "search"
    assert kwargs["refresh_minutes"] == 60
    assert kwargs["model_provider"] == "ollama"
    assert kwargs["model"] == "llama3"
    assert kwargs["search_strategy"] == "standard"
    assert kwargs["name"] == "My Subscription"
    assert kwargs["is_active"] is True
    assert kwargs["search_engine"] == "searxng"
    assert kwargs["search_iterations"] == 3
    assert kwargs["questions_per_iteration"] == 2


def test_subscribe_defaults_the_subscription_type_and_strategy(
    authenticated_client, monkeypatch
):
    """A body carrying only ``query`` still reaches the service with the
    two defaults filled in, not with ``None``."""
    spy = _Spy(result={"status": "success", "subscription_id": "sub-1"})
    monkeypatch.setattr(f"{API}.create_subscription", spy)

    resp = authenticated_client.post(SUBSCRIBE, json={"query": "Test"})

    assert resp.status_code == 200, resp.text
    kwargs = spy.calls[0][1]
    assert kwargs["subscription_type"] == "search"
    assert kwargs["search_strategy"] == "news_aggregation"
    assert kwargs["is_active"] is True


def test_subscribe_rejects_an_unresolvable_custom_endpoint(
    authenticated_client, monkeypatch
):
    """The SSRF census covers metadata IPs and non-HTTP schemes; a
    scheme-less garbage hostname takes a different path (``normalize_url``
    prepends ``http://``, then the resolve fails) and is not among them.
    The rejection must land before any subscription is created.
    """
    spy = _Spy(result={"status": "success"})
    monkeypatch.setattr(f"{API}.create_subscription", spy)

    resp = authenticated_client.post(
        SUBSCRIBE,
        json={"query": "q", "custom_endpoint": "not-a-url"},
    )

    assert resp.status_code == 400, resp.text
    assert spy.calls == [], "a hostile endpoint reached create_subscription"


# ---------------------------------------------------------------------------
# GET / PUT /news/api/subscriptions/{id}
# ---------------------------------------------------------------------------


def test_get_subscription_returns_the_service_body_unwrapped(
    authenticated_client, monkeypatch, subscription_id
):
    """The route returns whatever the service returns, verbatim — it does
    not wrap it in a ``{"subscription": ...}`` envelope."""
    monkeypatch.setattr(
        f"{API}.get_subscription",
        _Spy(result={"id": subscription_id, "query": "Test"}),
    )

    resp = authenticated_client.get(
        f"/news/api/subscriptions/{subscription_id}"
    )

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["id"] == subscription_id
    assert body["query"] == "Test"


def test_put_subscription_returns_the_service_body(
    authenticated_client, monkeypatch, subscription_id
):
    spy = _Spy(result={"id": subscription_id, "query": "Updated"})
    monkeypatch.setattr(f"{API}.update_subscription", spy)

    resp = authenticated_client.put(
        f"/news/api/subscriptions/{subscription_id}",
        json={"query": "Updated query"},
    )

    assert resp.status_code == 200, resp.text
    assert resp.json()["query"] == "Updated"
    assert spy.called_once


def test_subscription_history_returns_the_service_body(
    authenticated_client, monkeypatch, subscription_id
):
    monkeypatch.setattr(
        f"{API}.get_subscription_history",
        _Spy(
            result={
                "subscription": {"id": subscription_id},
                "history": [],
                "total_runs": 0,
            }
        ),
    )

    resp = authenticated_client.get(
        f"/news/api/subscriptions/{subscription_id}/history"
    )

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["history"] == []
    assert body["total_runs"] == 0


# ---------------------------------------------------------------------------
# POST /news/api/feedback/{card_id}
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("vote", ["up", "down"])
def test_feedback_accepts_both_votes_and_forwards_the_card_id(
    authenticated_client, monkeypatch, vote
):
    spy = _Spy(result={"success": True})
    monkeypatch.setattr(f"{API}.submit_feedback", spy)

    resp = authenticated_client.post(
        "/news/api/feedback/card-123", json={"vote": vote}
    )

    assert resp.status_code == 200, resp.text
    assert spy.called_once
    kwargs = spy.calls[0][1]
    assert kwargs["card_id"] == "card-123"
    assert kwargs["vote"] == vote


def test_feedback_rejects_a_vote_that_is_neither_up_nor_down(
    authenticated_client,
):
    """Deliberately NOT mocked.

    The guard moved layers in the port: the deleted route checked
    ``vote not in ["up", "down"]`` itself, while the surviving one checks
    only ``if not vote`` and relies on ``news.api.submit_feedback``
    raising ``ValueError`` into its ``except ValueError`` -> 400. Mocking
    the service would remove the only thing left that rejects the value,
    making the test vacuous.
    """
    resp = authenticated_client.post(
        "/news/api/feedback/card-123", json={"vote": "invalid"}
    )

    assert resp.status_code == 400, resp.text


# ---------------------------------------------------------------------------
# POST /news/api/research/{card_id}
# ---------------------------------------------------------------------------


def test_research_news_item_forwards_the_card_id_and_depth(
    authenticated_client, monkeypatch
):
    spy = _Spy(result={"research_id": "res-123"})
    monkeypatch.setattr(f"{API}.research_news_item", spy)

    resp = authenticated_client.post(
        "/news/api/research/card-123", json={"depth": "detailed"}
    )

    assert resp.status_code == 200, resp.text
    assert resp.json()["research_id"] == "res-123"
    # The service is called positionally: (card_id, depth).
    assert spy.calls[0][0] == ("card-123", "detailed")


def test_research_news_item_defaults_the_depth_to_quick(
    authenticated_client, monkeypatch
):
    """An absent body is valid here, and must still pick a depth. The
    smoke test that covers this route allows it a 501 from the
    not-yet-implemented service, so nothing checks the argument."""
    spy = _Spy(result={"research_id": "res-123"})
    monkeypatch.setattr(f"{API}.research_news_item", spy)

    resp = authenticated_client.post("/news/api/research/card-123", json={})

    assert resp.status_code == 200, resp.text
    assert spy.calls[0][0] == ("card-123", "quick")


# ---------------------------------------------------------------------------
# POST /news/api/preferences
# ---------------------------------------------------------------------------


def test_save_preferences_forwards_the_inner_preferences_object(
    authenticated_client, monkeypatch
):
    """The route unwraps ``data["preferences"]`` before calling the
    service — it does not pass the whole request body through."""
    spy = _Spy(result={"saved": True})
    monkeypatch.setattr(f"{API}.save_news_preferences", spy)

    resp = authenticated_client.post(
        PREFERENCES,
        json={"preferences": {"categories": ["tech", "science"]}},
    )

    assert resp.status_code == 200, resp.text
    assert resp.json() == {"saved": True}
    assert spy.calls[0][0][1] == {"categories": ["tech", "science"]}


def test_save_preferences_defaults_to_an_empty_object(
    authenticated_client, monkeypatch
):
    spy = _Spy(result={"saved": True})
    monkeypatch.setattr(f"{API}.save_news_preferences", spy)

    resp = authenticated_client.post(PREFERENCES, json={})

    assert resp.status_code == 200, resp.text
    assert spy.calls[0][0][1] == {}


# ---------------------------------------------------------------------------
# GET /news/api/categories
# ---------------------------------------------------------------------------


def test_categories_returns_the_service_category_list(
    authenticated_client, monkeypatch
):
    """The success branch of a route whose only other test asserts the
    501 from its ``except NotImplementedException`` handler — and which
    ``test_all_endpoints.py`` lists under ``KNOWN_NON_2XX``, i.e. asserts
    is *not* 200. Deleting the ``return api.get_news_categories()`` line
    would leave both of those green.
    """
    monkeypatch.setattr(
        f"{API}.get_news_categories",
        _Spy(
            result={
                "categories": [
                    {"name": "Technology", "count": 10},
                    {"name": "Science", "count": 5},
                ]
            }
        ),
    )

    resp = authenticated_client.get(CATEGORIES)

    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert [entry["name"] for entry in body["categories"]] == [
        "Technology",
        "Science",
    ]


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 news_flask_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"