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.

46.1 KB
Raw Blame
"""Contract tests for the news routers, ``web/routers/news_flask_api.py``
and ``web/routers/news_pages.py``.

Scope chosen to sit in the gaps left by the files already in the tree —
none of the following is re-asserted here:

* ``tests/news/test_news_input_validation.py`` — the ``limit`` clamp on
  ``GET /news/api/subscriptions/{id}/history`` and the non-dict/malformed
  JSON-body guard on 8 routes.  The IDENTICAL clamp on ``GET
  /news/api/feed`` is a separate copy of the code (news_flask_api.py:276
  vs :943) and had no test at all; that copy is what ``TestFeedLimitClamp``
  below pins.
* ``tests/web/routers/test_news_scheduler_check_now.py`` — the
  ``_wrap_job`` / ``replace_existing`` / per-user job-id contract for
  ``POST /news/api/scheduler/check-now``.  Its sibling
  ``POST /news/api/scheduler/cleanup-now`` carries an in-code comment
  saying those two properties "both matter here, and both were lost in
  the port", yet the only test of that route
  (``tests/security/test_scheduler_control_and_news_limits_fastapi.py``)
  asserts nothing beyond ``200`` + ``status == "triggered"``.
  ``TestCleanupNowJobRegistration`` pins the add_job call itself.
* ``tests/web/routers/test_news_subscribe_exception_contract.py`` — that
  a NewsAPIException raised under ``subscribe`` / ``feed`` /
  ``subscriptions/current`` survives the route's broad ``except``.  What
  is pinned here instead is the *envelope shape* that reaches the client,
  and specifically that it DIVERGES from ``WebAPIException``'s.
* ``tests/security/test_news_scheduler_isolation_fastapi.py`` —
  ``_is_job_owned_by_user`` and the scheduler status/stats scoping.

WHAT "OWNERSHIP SCOPING" MEANS IN THIS SUBSYSTEM
------------------------------------------------
There is no ``user_id`` column filter on the subscription queries — see
``news/api.py::get_subscription``/``update_subscription``/
``delete_subscription``, each of which does a bare
``.filter_by(id=subscription_id)``.  The entire isolation boundary is
*which database is opened*: ``get_user_db_session(username)`` selects a
per-user encrypted SQLCipher file.  So "can user A touch user B's
subscription?" reduces to exactly one question at the router layer —
**does the router pass the authenticated session's username, or something
the caller controls?**  ``TestSubscriptionOwnershipScoping`` asserts the
former for every subscription route, sending a hostile ``user_id`` /
``username`` in the body or query each time.  A cross-user *end-to-end*
test is not possible from here: the ``authenticated_client`` fixture
``shutil.rmtree``s the whole ``encrypted_databases`` directory when it
builds a user, so two live users cannot coexist inside one test.

NOTE ON "UNSUBSCRIBE": the news subsystem has no ``/unsubscribe`` route —
grepping ``src/`` for the word finds only the Socket.IO
``unsubscribe_from_research`` handler, which is the research subsystem.
``DELETE /news/api/subscriptions/{id}`` IS the unsubscribe path, and its
session handling is covered by the three tests at the end of
``TestSubscriptionOwnershipScoping``: CSRF refuses a token-less caller,
``require_auth`` refuses a session-less one, and a cookie captured before
logout is refused when replayed after it. The last is the one that proves
the SERVER-SIDE session is re-validated on every request, rather than the
username claim inside the signed cookie being taken at face value.

No network is touched: every feed / research / scheduler boundary is a
patched attribute on the module the router resolves at call time.

FALSIFICATION: every assertion class here was shown RED against a mutated
copy of the source (a hard-linked tree with only the three relevant files
unlinked and rewritten, loaded via ``PYTHONPATH``; the checked-in tree was
never modified). The mutations were: ``create_subscription`` honouring a
body-supplied ``user_id``; the feed clamp deleted; ``use_cache`` parsed
leniently; the feed's ``_is_valid_uuid`` guard deleted; an identity field
added to the update allow-list; ``delete_subscription`` honouring a
query-supplied ``username``; ``cleanup-now`` losing both ``_wrap_job`` and
``replace_existing``; ``subscriptions/current`` losing its
``except NewsAPIException: raise``; the health probe reverted to the
``"health_check"`` sentinel; the edit page's error branch losing
``custom_endpoint``; and ``NewsAPIException.to_dict`` renaming ``error``
to ``message``. Each was caught, by the intended test and no other.
"""

import uuid
from datetime import datetime, timezone
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

from local_deep_research.news.exceptions import (
    NewsAPIException,
    SubscriptionNotFoundException,
)
from local_deep_research.web.exceptions import WebAPIException
from local_deep_research.web.routers.news_flask_api import NEWS_FEED_MAX_LIMIT

# ``news.feed.default_limit`` in defaults/default_settings.json. Pinned as a
# literal so a silent change to the shipped default shows up here as a
# failure rather than being absorbed by a test that reads the same source.
DEFAULT_FEED_LIMIT = 20

FEED = "/news/api/feed"
SUBSCRIBE = "/news/api/subscribe"
SUBSCRIPTIONS_CURRENT = "/news/api/subscriptions/current"
CLEANUP_NOW = "/news/api/scheduler/cleanup-now"
GATE_ENV = "LDR_NEWS_SCHEDULER_ALLOW_API_CONTROL"
SCHEDULER_TARGET = (
    "local_deep_research.scheduler.background.get_background_job_scheduler"
)
STORAGE_MANAGER_TARGET = (
    "local_deep_research.news.core.storage_manager.StorageManager"
)

# The routers reach these through ``api.<name>`` / a late import, so the
# authoritative patch point is the defining module.
API = "local_deep_research.news.api"

# A syntactically valid UUID that no user's database contains. Several
# routes reject a non-UUID subscription_id at the boundary (_is_valid_uuid),
# so probes have to be well-formed to reach the handler body.
ABSENT_SUB_ID = "123e4567-e89b-12d3-a456-426614174000"


def _anon_client(forwarded_ip):
    """A TestClient with no session, on its own rate-limit bucket."""
    from local_deep_research.web.fastapi_app import app

    anon = TestClient(app, raise_server_exceptions=False)
    anon.headers.update({"X-Forwarded-For": forwarded_ip})
    return anon


def _register_and_login(client, username, password):
    """Drive the real register + login forms, then arm the CSRF header."""

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

    client.post(
        "/auth/register",
        data={
            "username": username,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": password,
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Auth bootstrap failed for {username}: expected 302, got "
            f"{resp.status_code}: {resp.text[:300]}"
        )
    token = client.get("/auth/csrf-token")
    if token.status_code == 200 and token.json().get("csrf_token"):
        client.headers.update({"X-CSRFToken": token.json()["csrf_token"]})
    return client


def _throwaway_logged_in_user(forwarded_ip):
    """A second, disposable logged-in user plus a copy of its cookie jar.

    Used only by the cookie-replay test, which needs a session it is free
    to destroy — logging the module fixture's user out would break every
    test scheduled after it.
    """
    client = _anon_client(forwarded_ip)
    username = f"test_newsrev_{uuid.uuid4().hex[:8]}"
    _register_and_login(client, username, "TestPassword123!")
    return client, username, dict(client.cookies)


@pytest.fixture(scope="module")
def news_user():
    """A registered, logged-in user plus its TestClient.

    Module-scoped and on a dedicated ``X-Forwarded-For``: registration is
    rate-limited to a few per hour per source IP, and the slowapi bucket is
    keyed on the forwarded address. Yields ``(client, username)`` because
    almost every assertion below is "the router forwarded THIS username".
    """
    client = _anon_client("10.61.19.4")
    username = f"test_newsctr_{uuid.uuid4().hex[:8]}"
    _register_and_login(client, username, "TestPassword123!")

    yield client, username

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


@pytest.fixture
def client(news_user):
    return news_user[0]


@pytest.fixture
def username(news_user):
    return news_user[1]


# ===========================================================================
# Ownership scoping — the router must pick the DB, never the caller
# ===========================================================================

# A body/query field named like an identity claim. Every route below is
# handed one; none of them may honour it.
HOSTILE_IDENTITY = {
    "user_id": "victim_user",
    "username": "victim_user",
    "owner": "victim_user",
}


class TestSubscriptionOwnershipScoping:
    """Each subscription route must reach ``news.api`` carrying the
    *session's* username.

    Why this is the whole of ownership here: see the module docstring —
    ``news/api.py`` filters subscriptions by ``id`` alone, so the username
    the router forwards is literally the choice of which encrypted database
    is unlocked. A route that let the body pick it would be a full
    cross-tenant read/write primitive, and it would still pass every
    existing news test in the tree, all of which drive a single user.
    """

    def test_create_forwards_session_user_and_ignores_body_identity(
        self, client, username
    ):
        with patch(f"{API}.create_subscription") as spy:
            spy.return_value = {"status": "success", "subscription_id": "s1"}
            resp = client.post(
                SUBSCRIBE,
                json={"query": "quantum computing", **HOSTILE_IDENTITY},
            )

        assert spy.called, (
            f"POST {SUBSCRIBE} never reached api.create_subscription "
            f"({resp.status_code}: {resp.text[:300]}) — the assertion below "
            f"would be vacuous"
        )
        assert spy.call_args.kwargs["user_id"] == username, (
            f"create_subscription was called for user_id="
            f"{spy.call_args.kwargs['user_id']!r}; the session belongs to "
            f"{username!r}. A caller-supplied identity field reached the "
            f"database selector."
        )

    def test_update_forwards_session_user_and_ignores_body_identity(
        self, client, username
    ):
        with patch(f"{API}.update_subscription") as spy:
            spy.return_value = {"status": "success", "subscription": {}}
            resp = client.put(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}",
                json={"name": "renamed", **HOSTILE_IDENTITY},
            )

        assert spy.called, (
            f"PUT never reached api.update_subscription "
            f"({resp.status_code}: {resp.text[:300]})"
        )
        assert spy.call_args.kwargs["username"] == username, (
            f"update_subscription was scoped to "
            f"{spy.call_args.kwargs['username']!r}, not the session user "
            f"{username!r}"
        )

    def test_update_field_mapping_cannot_reassign_ownership(self, client):
        """The update body is copied field-by-field through an explicit
        ``field_mapping`` allow-list. Nothing identity-shaped may make it
        into the dict handed to the storage layer — otherwise a subscription
        could be re-parented onto another user by name.
        """
        with patch(f"{API}.update_subscription") as spy:
            spy.return_value = {"status": "success", "subscription": {}}
            client.put(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}",
                json={"name": "renamed", **HOSTILE_IDENTITY},
            )

        assert spy.called
        update_data = spy.call_args.args[1]
        assert update_data == {"name": "renamed"}, (
            f"the update payload reaching storage was {update_data!r}; only "
            f"allow-listed fields may survive, and no identity field may."
        )

    def test_delete_forwards_session_user_and_ignores_query_identity(
        self, client, username
    ):
        with patch(f"{API}.delete_subscription") as spy:
            spy.return_value = {"status": "success", "deleted": ABSENT_SUB_ID}
            resp = client.delete(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}",
                params=HOSTILE_IDENTITY,
            )

        assert spy.called, (
            f"DELETE never reached api.delete_subscription "
            f"({resp.status_code}: {resp.text[:300]})"
        )
        assert spy.call_args.args[0] == ABSENT_SUB_ID
        assert spy.call_args.kwargs["username"] == username, (
            f"delete_subscription was scoped to "
            f"{spy.call_args.kwargs['username']!r}, not {username!r} — the "
            f"unsubscribe path would delete out of another user's database"
        )

    def test_get_one_forwards_session_user(self, client, username):
        with patch(f"{API}.get_subscription") as spy:
            spy.return_value = {"id": ABSENT_SUB_ID}
            client.get(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}",
                params=HOSTILE_IDENTITY,
            )

        assert spy.called
        assert spy.call_args.kwargs["username"] == username

    def test_history_forwards_session_user(self, client, username):
        with patch(f"{API}.get_subscription_history") as spy:
            spy.return_value = {
                "subscription": {},
                "history": [],
                "total_runs": 0,
            }
            client.get(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}/history",
                params=HOSTILE_IDENTITY,
            )

        assert spy.called
        assert spy.call_args.kwargs["username"] == username

    def test_list_current_forwards_session_user(self, client, username):
        with patch(f"{API}.get_subscriptions") as spy:
            spy.return_value = {"subscriptions": []}
            client.get(SUBSCRIPTIONS_CURRENT, params=HOSTILE_IDENTITY)

        assert spy.called
        assert spy.call_args.args[0] == username, (
            f"get_subscriptions listed for {spy.call_args.args[0]!r} instead "
            f"of the session user {username!r}"
        )

    def test_delete_without_csrf_is_refused_before_the_handler(self):
        """First gate on the unsubscribe path: CSRF, fail-closed."""
        anon = _anon_client("10.61.19.5")

        with patch(f"{API}.delete_subscription") as spy:
            resp = anon.delete(f"/news/api/subscriptions/{ABSENT_SUB_ID}")

        assert resp.status_code == 403, resp.text[:300]
        assert not spy.called

    def test_delete_rejects_a_session_less_caller_without_reaching_handler(
        self,
    ):
        """Second gate: the session itself, re-checked per request.

        ``require_auth`` is a per-request dependency, not something
        established once at login, so a caller that carries a valid CSRF
        token but no session must be turned away BEFORE
        ``api.delete_subscription`` is consulted. Asserting the spy was
        never called is the load-bearing half — a 401 alone would also be
        produced by a route that ran the deletion and then failed to
        serialise a response.
        """
        anon = _anon_client("10.61.19.5")
        anon.get("/auth/login")
        token = anon.get("/auth/csrf-token").json()["csrf_token"]

        with patch(f"{API}.delete_subscription") as spy:
            resp = anon.delete(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}",
                headers={"X-CSRFToken": token},
            )

        assert not spy.called, (
            "a caller with no session reached api.delete_subscription"
        )
        assert resp.status_code == 401, (
            f"session-less DELETE answered {resp.status_code}, expected 401: "
            f"{resp.text[:300]}"
        )

    def test_delete_rejects_a_replayed_cookie_from_a_logged_out_session(self):
        """The unsubscribe path re-validates the SERVER-SIDE session, not
        just the username claim inside the signed cookie.

        Without server-side validation, a cookie captured before logout
        keeps working the moment that user (on any device) logs in again,
        because the remaining checks are username-scoped rather than
        session-scoped. This drives the full capture/logout/replay sequence
        against DELETE — the one news route that destroys data — and
        requires both a 401 and an untouched storage layer.
        """
        client, username, cookies = _throwaway_logged_in_user("10.61.19.8")

        # Control: the captured cookie works while the session is alive.
        replay = _anon_client("10.61.19.8")
        replay.cookies.update(cookies)
        with patch(
            f"{API}.delete_subscription",
            return_value={"status": "success", "deleted": ABSENT_SUB_ID},
        ) as live_spy:
            live = replay.delete(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}",
                headers={"X-CSRFToken": client.headers["X-CSRFToken"]},
            )
        assert live.status_code == 200, (
            f"the captured cookie did not work even before logout "
            f"({live.status_code}: {live.text[:200]}) — the replay "
            f"assertion below would prove nothing about revocation"
        )
        assert live_spy.call_args.kwargs["username"] == username

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

        replay_after = _anon_client("10.61.19.8")
        replay_after.cookies.update(cookies)
        with patch(f"{API}.delete_subscription") as spy:
            resp = replay_after.delete(
                f"/news/api/subscriptions/{ABSENT_SUB_ID}",
                headers={"X-CSRFToken": client.headers["X-CSRFToken"]},
            )

        assert not spy.called, (
            "a cookie replayed after logout reached api.delete_subscription "
            "— the unsubscribe path does not re-validate the session"
        )
        assert resp.status_code in (401, 403), (
            f"replayed post-logout cookie answered {resp.status_code}: "
            f"{resp.text[:300]}"
        )


# ===========================================================================
# Feed generation + paging
# ===========================================================================


class TestFeedLimitClamp:
    """``GET /news/api/feed`` clamps ``limit`` into ``[1, 100]``.

    Asserted on what the handler passes DOWNSTREAM, not on the response
    length: the test user's news database is empty, so every limit yields
    zero items and a length assertion would hold with the clamp deleted.

    ``limit`` is the feed's only paging control — there is no offset/page
    parameter on this route, so "pagination" here is exactly this clamp
    plus the default below.
    """

    @pytest.mark.parametrize(
        ("requested", "expected"),
        [
            ("99999", NEWS_FEED_MAX_LIMIT),
            (str(NEWS_FEED_MAX_LIMIT + 1), NEWS_FEED_MAX_LIMIT),
            (str(NEWS_FEED_MAX_LIMIT), NEWS_FEED_MAX_LIMIT),
            ("0", 1),
            ("-5", 1),
            ("1", 1),
            ("7", 7),
        ],
    )
    def test_limit_is_clamped(self, client, requested, expected):
        with patch(f"{API}.get_news_feed") as spy:
            spy.return_value = {"news_items": []}
            client.get(FEED, params={"limit": requested})

        assert spy.called, (
            "the handler never reached api.get_news_feed — the clamp "
            "assertion would be vacuous"
        )
        assert spy.call_args.kwargs["limit"] == expected, (
            f"limit={requested!r} reached the service layer as "
            f"{spy.call_args.kwargs['limit']!r}, expected {expected!r}"
        )

    def test_absent_limit_uses_the_configured_default(self, client):
        with patch(f"{API}.get_news_feed") as spy:
            spy.return_value = {"news_items": []}
            client.get(FEED)

        assert spy.called
        assert spy.call_args.kwargs["limit"] == DEFAULT_FEED_LIMIT, (
            f"a feed request with no ?limit resolved to "
            f"{spy.call_args.kwargs['limit']!r}; the shipped "
            f"news.feed.default_limit is {DEFAULT_FEED_LIMIT}"
        )

    @pytest.mark.parametrize(
        "junk", ["abc", "'; DROP TABLE--", "", "1e3", "3.5"]
    )
    def test_unparseable_limit_falls_back_to_the_default(self, client, junk):
        """A junk limit must degrade to the default, never 500 and never
        reach the service layer as a non-int."""
        with patch(f"{API}.get_news_feed") as spy:
            spy.return_value = {"news_items": []}
            resp = client.get(FEED, params={"limit": junk})

        assert resp.status_code != 500, resp.text[:300]
        assert spy.called
        limit = spy.call_args.kwargs["limit"]
        assert limit == DEFAULT_FEED_LIMIT, (
            f"limit={junk!r} resolved to {limit!r} instead of the default "
            f"{DEFAULT_FEED_LIMIT}"
        )

    def test_invalid_subscription_id_is_rejected_before_the_service_layer(
        self, client
    ):
        """``subscription_id`` feeds a LIKE-pattern query downstream, so a
        non-UUID must be refused at the boundary. The spy assertion is the
        point — a 400 that still ran the query would not be a fix."""
        with patch(f"{API}.get_news_feed") as spy:
            resp = client.get(FEED, params={"subscription_id": "%"})

        assert resp.status_code == 400, resp.text[:300]
        assert not spy.called, (
            "a wildcard subscription_id reached api.get_news_feed"
        )


class TestFeedQueryParamParsing:
    """The remaining feed query params, pinned as they behave today."""

    @pytest.mark.parametrize(
        ("raw", "expected"),
        [
            ("true", True),
            ("TRUE", True),
            ("True", True),
            ("false", False),
            ("FALSE", False),
            # Worth marking rather than "fixing" here: the parse is a
            # literal case-folded ``== "true"``, so every spelling that
            # is not exactly "true" — including the ones a caller would
            # reasonably expect to mean ON — turns the cache OFF and
            # forces a fresh feed build. It fails toward more work, not
            # toward stale data, which is why it is pinned as-is.
            ("0", False),
            ("1", False),
            ("yes", False),
            ("", False),
        ],
    )
    def test_use_cache_parsing(self, client, raw, expected):
        with patch(f"{API}.get_news_feed") as spy:
            spy.return_value = {"news_items": []}
            client.get(FEED, params={"use_cache": raw})

        assert spy.called
        assert spy.call_args.kwargs["use_cache"] is expected, (
            f"use_cache={raw!r} was parsed as "
            f"{spy.call_args.kwargs['use_cache']!r}, expected {expected!r}"
        )

    def test_absent_use_cache_defaults_to_true(self, client):
        with patch(f"{API}.get_news_feed") as spy:
            spy.return_value = {"news_items": []}
            client.get(FEED)

        assert spy.called
        assert spy.call_args.kwargs["use_cache"] is True

    def test_focus_and_strategy_are_forwarded_verbatim(self, client):
        with patch(f"{API}.get_news_feed") as spy:
            spy.return_value = {"news_items": []}
            client.get(
                FEED, params={"focus": "climate", "strategy": "source-based"}
            )

        assert spy.called
        assert spy.call_args.kwargs["focus"] == "climate"
        assert spy.call_args.kwargs["search_strategy"] == "source-based"

    def test_absent_focus_and_strategy_are_none_not_empty_string(self, client):
        """Downstream treats ``None`` as "use the configured default"; an
        empty string is a value. The route must not conflate them."""
        with patch(f"{API}.get_news_feed") as spy:
            spy.return_value = {"news_items": []}
            client.get(FEED)

        assert spy.called
        assert spy.call_args.kwargs["focus"] is None
        assert spy.call_args.kwargs["search_strategy"] is None


class TestFeedResultPassthrough:
    """The success body is the service result, unwrapped."""

    def test_service_result_is_returned_unchanged(self, client):
        payload = {
            "news_items": [{"id": "card-1", "headline": "h"}],
            "total": 1,
        }
        with patch(f"{API}.get_news_feed", return_value=payload):
            resp = client.get(FEED)

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == payload

    def test_range_error_from_the_service_is_a_400_not_a_500(self, client):
        """``get_news_feed`` distinguishes a caller-side range complaint
        from a server failure by sniffing the service's error text. Pinned
        because the two branches differ only by that substring."""
        with patch(
            f"{API}.get_news_feed",
            return_value={
                "error": "limit must be between 1 and 100",
                "news_items": [],
            },
        ):
            resp = client.get(FEED)

        assert resp.status_code == 400, resp.text[:300]
        assert resp.json()["news_items"] == []

    def test_other_service_error_is_a_500(self, client):
        """Control for the test above: without it, "the route always 400s"
        would satisfy it equally well."""
        with patch(
            f"{API}.get_news_feed",
            return_value={"error": "disk on fire", "news_items": []},
        ):
            resp = client.get(FEED)

        assert resp.status_code == 500, resp.text[:300]
        assert "disk on fire" not in resp.text, (
            "the raw service error text was reflected to the client"
        )


# ===========================================================================
# Scheduled news job — cleanup-now's add_job contract
# ===========================================================================


def _running_scheduler():
    scheduler = MagicMock(name="background_job_scheduler")
    scheduler.is_running = True
    scheduler.user_sessions = {}
    return scheduler


class TestCleanupNowJobRegistration:
    """``POST /news/api/scheduler/cleanup-now`` must enqueue the cleanup
    through ``_wrap_job`` under a fixed id with ``replace_existing=True``.

    Both properties are load-bearing and both were lost once already in
    this port (see the comment at news_flask_api.py:1281):

    * ``replace_existing=True`` — the job id is a constant and the run_date
      is one second out, so a second POST inside that window raises
      APScheduler's ``ConflictingIdError``, which the route's blanket
      ``except Exception`` renders as a 500. A double-click on the admin
      button is enough to trigger it.
    * ``_wrap_job`` — supplies the worker-side context handling every other
      ``add_job`` call in the tree goes through.

    The APScheduler singleton is the one boundary mocked; the auth
    dependency and the ``news.scheduler.allow_api_control`` gate both run
    for real.
    """

    @pytest.fixture
    def enqueued(self, client, monkeypatch):
        monkeypatch.setenv(GATE_ENV, "true")
        scheduler = _running_scheduler()
        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = client.post(CLEANUP_NOW)
        assert resp.status_code == 200, (
            f"POST {CLEANUP_NOW} answered {resp.status_code}: "
            f"{resp.text[:300]} — every assertion below would be vacuous"
        )
        assert scheduler.scheduler.add_job.called, (
            "the route returned 200 without enqueueing anything"
        )
        return scheduler, resp

    def test_response_reports_the_job_was_triggered(self, enqueued):
        _scheduler, resp = enqueued
        assert resp.json()["status"] == "triggered"

    def test_job_function_is_the_wrapped_cleanup_runner(self, enqueued):
        scheduler, _resp = enqueued

        assert scheduler._wrap_job.call_args.args == (
            scheduler._run_cleanup_with_tracking,
        ), (
            f"_wrap_job was handed {scheduler._wrap_job.call_args!r}; it must "
            f"wrap _run_cleanup_with_tracking"
        )
        submitted = scheduler.scheduler.add_job.call_args.args[0]
        assert submitted is scheduler._wrap_job.return_value, (
            "add_job received the raw cleanup callable, not the _wrap_job "
            "wrapper — the worker-side context handling is bypassed"
        )

    def test_job_is_replaceable_under_a_fixed_id(self, enqueued):
        scheduler, _resp = enqueued
        kwargs = scheduler.scheduler.add_job.call_args.kwargs

        assert kwargs.get("id") == "manual_cleanup_trigger", (
            f"cleanup job id was {kwargs.get('id')!r}"
        )
        assert kwargs.get("replace_existing") is True, (
            "replace_existing was not passed — a second POST inside the "
            "one-second window raises ConflictingIdError, which this "
            "route's blanket except turns into a 500"
        )

    def test_job_is_a_one_shot_date_trigger_in_the_future(self, enqueued):
        scheduler, _resp = enqueued
        call = scheduler.scheduler.add_job.call_args

        assert call.args[1] == "date", (
            f"cleanup was enqueued with trigger {call.args[1]!r}, expected a "
            f"one-shot 'date' trigger"
        )
        run_date = call.kwargs.get("run_date")
        assert isinstance(run_date, datetime)
        assert run_date > datetime.now(timezone.utc), (
            f"run_date {run_date!r} is not in the future"
        )

    def test_a_repeat_post_re_enqueues_rather_than_erroring(
        self, client, monkeypatch
    ):
        """The double-click case the fixed id + replace_existing exist for.
        Two POSTs inside the one-second window must both be 200s and both
        must enqueue."""
        monkeypatch.setenv(GATE_ENV, "true")
        scheduler = _running_scheduler()
        with patch(SCHEDULER_TARGET, return_value=scheduler):
            first = client.post(CLEANUP_NOW)
            second = client.post(CLEANUP_NOW)

        assert (first.status_code, second.status_code) == (200, 200), (
            f"{first.status_code} then {second.status_code}: "
            f"{second.text[:300]}"
        )
        assert scheduler.scheduler.add_job.call_count == 2
        ids = {
            c.kwargs.get("id")
            for c in scheduler.scheduler.add_job.call_args_list
        }
        assert ids == {"manual_cleanup_trigger"}, (
            f"the two enqueues used different job ids ({ids!r}); the dedup "
            f"depends on them colliding"
        )

    def test_stopped_scheduler_enqueues_nothing(self, client, monkeypatch):
        """Control: the assertions above must mean "the route enqueues
        correctly", not "add_job is called no matter what"."""
        monkeypatch.setenv(GATE_ENV, "true")
        scheduler = _running_scheduler()
        scheduler.is_running = False

        with patch(SCHEDULER_TARGET, return_value=scheduler):
            resp = client.post(CLEANUP_NOW)

        assert resp.status_code == 400, resp.text[:300]
        assert not scheduler.scheduler.add_job.called
        assert not scheduler._wrap_job.called


# ===========================================================================
# Error envelopes — NewsAPIException and WebAPIException DIVERGE
# ===========================================================================


class TestErrorEnvelopeDivergence:
    """Two structured-error base classes are registered on the same app
    (``fastapi_app.py::_register_exception_handlers``) and they render
    DIFFERENT bodies for the same inputs.

    This is pinned as-is rather than as "one of them is wrong": a client
    that has learned to read ``body["error"]`` from one and
    ``body["message"]`` from the other is depending on the difference, so
    unifying them is a breaking change that should have to break a test.
    """

    NEWS_KEYS = {"error", "error_code", "status_code", "details"}
    WEB_KEYS = {"status", "message", "error_code", "details"}

    def test_the_two_to_dict_shapes_share_only_error_code_and_details(self):
        args = dict(
            status_code=404, error_code="THING_MISSING", details={"id": "abc"}
        )
        news = NewsAPIException("thing is missing", **args).to_dict()
        web = WebAPIException("thing is missing", **args).to_dict()

        assert set(news) == self.NEWS_KEYS, news
        assert set(web) == self.WEB_KEYS, web
        assert set(news) & set(web) == {"error_code", "details"}

    def test_the_human_message_lives_under_a_different_key_in_each(self):
        news = NewsAPIException("boom").to_dict()
        web = WebAPIException("boom").to_dict()

        assert news["error"] == "boom"
        assert web["message"] == "boom"
        assert "message" not in news, (
            "NewsAPIException grew a 'message' key — the two envelopes were "
            "unified; update the clients before updating this test"
        )
        assert "error" not in web

    def test_only_news_repeats_the_http_status_inside_the_body(self):
        news = NewsAPIException("boom", status_code=418).to_dict()
        web = WebAPIException("boom", status_code=418).to_dict()

        assert news["status_code"] == 418
        assert "status_code" not in web
        assert web["status"] == "error", (
            "WebAPIException's 'status' is the literal string 'error', not "
            "the HTTP code — the near-identical key names are the trap this "
            "test exists to mark"
        )

    def test_neither_envelope_emits_details_when_there_are_none(self):
        assert "details" not in NewsAPIException("boom").to_dict()
        assert "details" not in WebAPIException("boom").to_dict()


class TestNewsRouteErrorEnvelopeOverHttp:
    """The shape above, as it actually reaches a news client."""

    def test_news_exception_renders_its_own_envelope_verbatim(self, client):
        with patch(
            f"{API}.get_subscription",
            side_effect=SubscriptionNotFoundException(ABSENT_SUB_ID),
        ):
            resp = client.get(f"/news/api/subscriptions/{ABSENT_SUB_ID}")

        assert resp.status_code == 404, resp.text[:300]
        body = resp.json()
        assert set(body) == TestErrorEnvelopeDivergence.NEWS_KEYS, body
        assert body["error_code"] == "SUBSCRIPTION_NOT_FOUND"
        assert body["details"] == {"subscription_id": ABSENT_SUB_ID}
        assert ABSENT_SUB_ID in body["error"]
        assert body["status_code"] == resp.status_code, (
            "the status_code echoed in the body disagrees with the HTTP "
            "status the client actually received"
        )

    def test_a_web_exception_raised_under_a_news_route_is_swallowed(
        self, client
    ):
        """DEFECT PIN (behaviour, not endorsement).

        Every news route guards its broad ``except Exception`` with an
        ``except NewsAPIException: raise`` — and only that. A
        ``WebAPIException`` raised on the same path is therefore caught by
        the broad clause and flattened into a generic 500 ``{"error": ...}``
        before the registered ``@app.exception_handler(WebAPIException)``
        can ever see it. Its ``status_code`` (404 here) is lost too.

        Recorded as the current contract so that a future change to either
        the guard clauses or the handler registration surfaces here.
        """
        with patch(
            f"{API}.get_subscriptions",
            side_effect=WebAPIException(
                "web-layer failure",
                status_code=404,
                error_code="WEB_THING_MISSING",
                details={"id": "abc"},
            ),
        ):
            resp = client.get(SUBSCRIPTIONS_CURRENT)

        assert resp.status_code == 500, (
            f"WebAPIException's own status_code=404 now survives a news "
            f"route (got {resp.status_code}) — the swallow documented here "
            f"has changed: {resp.text[:300]}"
        )
        body = resp.json()
        assert "error_code" not in body, (
            f"the WebAPIException envelope now reaches the client: {body!r}"
        )
        assert "web-layer failure" not in resp.text, (
            "the raw exception message leaked through the generic handler"
        )

    def test_control_a_news_exception_on_the_same_route_is_not_swallowed(
        self, client
    ):
        """Control for the test above. Without it, "the route flattens
        everything" and "the route flattens WebAPIException specifically"
        are indistinguishable."""
        with patch(
            f"{API}.get_subscriptions",
            side_effect=NewsAPIException(
                "news-layer failure",
                status_code=404,
                error_code="NEWS_THING_MISSING",
            ),
        ):
            resp = client.get(SUBSCRIPTIONS_CURRENT)

        assert resp.status_code == 404, resp.text[:300]
        assert resp.json()["error_code"] == "NEWS_THING_MISSING"

    def test_delete_success_body_is_the_documented_shape(self, client):
        """Positive control for the 404 envelope test: the same route must
        be able to succeed, or "DELETE always errors" would satisfy it."""
        with patch(
            f"{API}.delete_subscription",
            return_value={"status": "success", "deleted": ABSENT_SUB_ID},
        ):
            resp = client.delete(f"/news/api/subscriptions/{ABSENT_SUB_ID}")

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {
            "status": "success",
            "message": f"Subscription {ABSENT_SUB_ID} deleted",
        }

    def test_delete_of_an_absent_subscription_is_a_structured_404(self, client):
        with patch(
            f"{API}.delete_subscription",
            side_effect=SubscriptionNotFoundException(ABSENT_SUB_ID),
        ):
            resp = client.delete(f"/news/api/subscriptions/{ABSENT_SUB_ID}")

        assert resp.status_code == 404, resp.text[:300]
        assert resp.json()["error_code"] == "SUBSCRIPTION_NOT_FOUND"


# ===========================================================================
# news_pages.py
# ===========================================================================


class TestNewsHealthEndpoint:
    """``GET /news/health``. Existing coverage asserts only ``== 200``."""

    def test_healthy_probe_is_scoped_to_the_caller(self, client, username):
        """The route's docstring claims it replaced a hardcoded
        ``user_id="health_check"`` sentinel — which both leaked
        infrastructure state and wrote a spurious DB row per probe — with a
        caller-scoped read. Nothing asserted that; this does."""
        storage = MagicMock(name="StorageManager")
        with patch(STORAGE_MANAGER_TARGET, return_value=storage):
            resp = client.get("/news/health")

        assert resp.status_code == 200, resp.text[:300]
        assert resp.json() == {
            "status": "healthy",
            "enabled": True,
            "database": "connected",
        }
        assert storage.get_user_feed.called
        probed = storage.get_user_feed.call_args.args[0]
        assert probed == username, (
            f"the health probe read the feed of {probed!r} rather than the "
            f"authenticated caller {username!r} — the 'health_check' "
            f"sentinel is back"
        )
        assert storage.get_user_feed.call_args.kwargs.get("limit") == 1, (
            "the probe must read a single row, not a full feed"
        )

    def test_unhealthy_probe_is_a_500_that_leaks_nothing(self, client):
        secret = "sqlcipher key rotation failed at /var/lib/ldr/u42.db"
        with patch(STORAGE_MANAGER_TARGET, side_effect=RuntimeError(secret)):
            resp = client.get("/news/health")

        assert resp.status_code == 500, resp.text[:300]
        assert resp.json() == {
            "status": "unhealthy",
            "error": "An internal error has occurred.",
        }
        assert secret not in resp.text
        assert "sqlcipher" not in resp.text.lower()

    def test_health_requires_a_session(self):
        """It is deliberately NOT the public liveness probe — /api/v1/health
        is. An unauthenticated caller must not learn whether the news
        database is reachable.

        The two shapes are both pinned because they differ, and the
        difference bites operators: ``/news/health`` sits outside the
        ``/api/`` prefix that ``fastapi_app._is_api_request`` keys on, so a
        monitoring probe that does not send ``Accept: application/json``
        gets a **302 to the login form**, not a 401 — i.e. a naive checker
        that follows redirects sees an HTTP 200 login page and reports the
        news subsystem as healthy. Recorded here as current behaviour.
        """
        anon = _anon_client("10.61.19.6")

        with patch(STORAGE_MANAGER_TARGET) as storage_cls:
            browser = anon.get("/news/health", follow_redirects=False)
            api_shaped = anon.get(
                "/news/health",
                headers={"Accept": "application/json"},
                follow_redirects=False,
            )

        assert api_shaped.status_code == 401, (
            f"a JSON-shaped anonymous probe got {api_shaped.status_code}: "
            f"{api_shaped.text[:300]}"
        )
        assert browser.status_code == 302, (
            f"a browser-shaped anonymous probe got {browser.status_code}, "
            f"expected the login redirect: {browser.text[:200]}"
        )
        assert browser.headers["location"].startswith("/auth/login"), (
            browser.headers.get("location")
        )
        assert not storage_cls.called, (
            "an anonymous request still constructed a StorageManager"
        )


class TestSubscriptionFormPages:
    """The two pages that render ``news-subscription-form.html``.

    The template does ``{{ default_settings.custom_endpoint | tojson }}``,
    and Jinja's Undefined is not JSON-serialisable — so any branch that
    hands the template an incomplete ``default_settings`` renders as a 500
    rather than a page. That is exactly what makes the not-found and error
    branches below worth a test.
    """

    def test_new_subscription_page_renders(self, client):
        resp = client.get("/news/subscriptions/new")

        assert resp.status_code == 200, resp.text[:400]
        assert "text/html" in resp.headers["content-type"]

    def test_edit_page_for_an_absent_subscription_renders_not_500(self, client):
        """``news.api.get_subscription`` raises rather than returning None,
        so this lands in the page's ``except`` branch. That branch skips
        ``_load_user_settings`` entirely — it must still supply every key
        the template serialises."""
        with patch(
            f"{API}.get_subscription",
            side_effect=SubscriptionNotFoundException(ABSENT_SUB_ID),
        ):
            resp = client.get(f"/news/subscriptions/{ABSENT_SUB_ID}/edit")

        assert resp.status_code == 200, (
            f"the absent-subscription branch rendered {resp.status_code} "
            f"instead of a page: {resp.text[:400]}"
        )
        assert "text/html" in resp.headers["content-type"]

    def test_edit_page_error_branch_leaks_no_exception_text(self, client):
        secret = "Traceback: sqlite3.OperationalError no such table shhh"
        with patch(f"{API}.get_subscription", side_effect=RuntimeError(secret)):
            resp = client.get(f"/news/subscriptions/{ABSENT_SUB_ID}/edit")

        assert resp.status_code == 200, resp.text[:400]
        assert secret not in resp.text
        assert "OperationalError" not in resp.text

    def test_edit_page_renders_a_found_subscription(self, client):
        """Control: the two branches above must mean "the error paths still
        render", not "this page renders the same thing regardless"."""
        marker = "Fusion energy weekly digest"
        subscription = {
            "id": ABSENT_SUB_ID,
            "name": marker,
            "query_or_topic": marker,
            "subscription_type": "search",
            "refresh_interval_minutes": 60,
            "is_active": True,
            "status": "active",
            "folder_id": None,
            "model_provider": "ollama",
            "model": "",
            "search_strategy": "source-based",
            "custom_endpoint": "",
            "search_engine": "searxng",
            "search_iterations": 3,
            "questions_per_iteration": 5,
            "created_at": None,
            "updated_at": None,
        }
        with patch(f"{API}.get_subscription", return_value=subscription):
            resp = client.get(f"/news/subscriptions/{ABSENT_SUB_ID}/edit")

        assert resp.status_code == 200, resp.text[:400]
        assert marker in resp.text, (
            "the edit page did not render the subscription it was given — "
            "the error-branch tests above prove nothing"
        )

    @pytest.mark.parametrize(
        "path",
        [
            "/news/",
            "/news/subscriptions",
            "/news/subscriptions/new",
            f"/news/subscriptions/{ABSENT_SUB_ID}/edit",
        ],
    )
    def test_subscription_pages_bounce_an_anonymous_caller_to_login(self, path):
        """These are HTML pages, so ``require_auth``'s 401 is rewritten by
        ``handle_http_exception`` into a 302 carrying the original path as
        ``?next=``. Pinned including the ``next`` value: the port had
        already truncated a deep link's query once (see the comment at
        fastapi_app.py:1057), and losing the path as well would strand a
        signed-out user on the news index."""
        anon = _anon_client("10.61.19.7")

        with patch(f"{API}.get_subscription") as spy:
            resp = anon.get(path, follow_redirects=False)

        assert resp.status_code == 302, (
            f"GET {path} answered an anonymous caller with "
            f"{resp.status_code}: {resp.text[:200]}"
        )
        location = resp.headers["location"]
        assert location.startswith("/auth/login"), location
        if path != "/news/":
            assert f"next={path}" in location, (
                f"the login bounce for {path} lost the return path: {location}"
            )
        assert not spy.called, (
            f"GET {path} reached news.api before the auth bounce"
        )