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.

6.6 KB
Raw Blame
"""Ported from ``tests/research_scheduler/test_scheduler_routes.py`` on main
(deleted by the FastAPI migration).

``tests/research_scheduler/test_scheduler_edge_cases.py`` is a partial
successor: it pins the 200/400/500 status codes and the scrubbed error
bodies. It does NOT pin three properties the deleted file did, and each of
them is invisible to it:

1. **Per-user scoping.** The successor asserts
   ``scheduler.get_document_scheduler_status.assert_called_once()`` /
   ``trigger_document_processing.assert_called_once()`` -- with no argument
   check. Deleting the ``username`` argument from either call site (or
   substituting a constant) leaves the successor green while every user
   would see, and trigger, the same scheduler state. The deleted file used
   ``assert_called_once_with("testuser")``; this file re-expresses that
   against the fixture's real username, read back from ``/auth/check``.

2. **``POST /api/scheduler/run-now`` requires authentication.** The
   successor's parametrisation is ``[(STATUS_URL, "get")]`` -- the
   *mutating* endpoint is not in it. An anonymous caller able to kick off
   document processing is the more serious of the two gaps.

3. **The 400 body explains *why* the run was refused.** The successor
   asserts only ``"error" in resp.json()``, which a bare ``{"error": ""}``
   satisfies. ``trigger_document_processing`` returning False is a
   user-configuration state ("inactive, or processing disabled") and the
   operator-facing text is the only thing that distinguishes it from a
   crash.

Also folded in from the sibling ``tests/research_scheduler/test_routes.py``:
that file's ``get_current_username`` tests are dropped -- the helper read
Flask's ``session`` and has no FastAPI counterpart (``Depends(require_auth)``
supplies the username), and its blueprint-object tests are already
superseded by ``TestSchedulerRouteRegistration`` in the edge-cases module.
"""

from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

SCHEDULER_ROUTER = "local_deep_research.web.routers.scheduler"
STATUS_URL = "/api/scheduler/status"
RUN_NOW_URL = "/api/scheduler/run-now"


def _current_username(client) -> str:
    resp = client.get("/auth/check")
    assert resp.status_code == 200, resp.text[:300]
    return resp.json()["username"]


@pytest.mark.parametrize(
    "url,method",
    [(STATUS_URL, "get"), (RUN_NOW_URL, "post")],
)
def test_scheduler_endpoints_require_authentication(app, url, method):
    """Both endpoints 401 for an anonymous caller.

    ``run-now`` is the one the successor module omits, and it is the
    mutating one: it kicks off document processing for a user.
    """
    client = TestClient(app, raise_server_exceptions=False)

    # The branch runs a fail-closed CSRF middleware ahead of the auth
    # dependency, so a bare anonymous POST is rejected 403 by CSRF before
    # ``require_auth`` is ever consulted. Carrying a valid (anonymous) token
    # steps past that gate so this test measures the AUTH gate -- otherwise
    # deleting ``Depends(require_auth)`` from the route would still look
    # "rejected" and the test would pin nothing about authentication.
    headers = {}
    if method == "post":
        headers["X-CSRFToken"] = client.get("/auth/csrf-token").json()[
            "csrf_token"
        ]

    resp = getattr(client, method)(url, headers=headers, follow_redirects=False)

    assert resp.status_code == 401, (
        f"{method.upper()} {url} returned {resp.status_code} without auth: "
        f"{resp.text[:200]}"
    )
    body = resp.json()
    message = body.get("error") or body.get("detail") or ""
    assert "authentication required" in str(message).lower(), body


def test_status_is_scoped_to_the_authenticated_user(authenticated_client):
    """``get_document_scheduler_status`` must be asked about *this* user.

    Successor to ``assert_called_once_with("testuser")``. A call with no
    username, or with a hardcoded one, would leak another account's
    scheduler state.
    """
    username = _current_username(authenticated_client)

    scheduler = MagicMock()
    scheduler.get_document_scheduler_status.return_value = {"is_running": False}

    with patch(
        f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
        return_value=scheduler,
    ):
        resp = authenticated_client.get(STATUS_URL)

    assert resp.status_code == 200, resp.text[:300]
    assert resp.json()["is_running"] is False
    scheduler.get_document_scheduler_status.assert_called_once_with(username)


def test_manual_run_is_scoped_to_the_authenticated_user(authenticated_client):
    """``trigger_document_processing`` must run for *this* user.

    Successor to ``assert_called_once_with("testuser")``. Losing the
    username here would let one user's POST start another user's (or every
    user's) document processing.
    """
    username = _current_username(authenticated_client)

    scheduler = MagicMock()
    scheduler.trigger_document_processing.return_value = True

    with patch(
        f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
        return_value=scheduler,
    ):
        resp = authenticated_client.post(RUN_NOW_URL, json={})

    assert resp.status_code == 200, resp.text[:300]
    assert "successfully" in resp.json()["message"]
    scheduler.trigger_document_processing.assert_called_once_with(username)


def test_manual_run_refusal_explains_itself(authenticated_client):
    """The 400 body names the reason, not just ``{"error": ...}``."""
    scheduler = MagicMock()
    scheduler.trigger_document_processing.return_value = False

    with patch(
        f"{SCHEDULER_ROUTER}.get_background_job_scheduler",
        return_value=scheduler,
    ):
        resp = authenticated_client.post(RUN_NOW_URL, json={})

    assert resp.status_code == 400, resp.text[:300]
    assert (
        "user may not be active or processing disabled" in resp.json()["error"]
    ), resp.json()


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