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.

12.1 KB
Raw Blame
# allow: no-sut-import — drives the shipped web entrypoint in a child process
"""Black-box integration coverage for the FastAPI/uvicorn boundary.

Most migration tests call the ASGI app through ``TestClient``.  That does not
exercise ``web.app``, uvicorn's proxy-header middleware, a real TCP response,
or an actual WebSocket upgrade.  These tests launch the production entrypoint
against an isolated data directory and cover those seams over loopback only.

The child-process harness uses POSIX signals for graceful shutdown. It skips
on Windows; these results do not qualify Windows process-group/control-event
shutdown or Windows runtime behavior.
"""

from __future__ import annotations

import json
import os
import signal
import socket
import subprocess
import sys
import time
import uuid
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from urllib.parse import urlsplit

import httpx
import pytest
from websockets.sync.client import connect as websocket_connect

PROJECT_ROOT = Path(__file__).resolve().parents[2]
SRC_ROOT = PROJECT_ROOT / "src"
START_TIMEOUT = 120.0
STOP_TIMEOUT = 30.0
REQUEST_TIMEOUT = 10.0
USER_PROVISIONING_READ_TIMEOUT = 60.0
EXPECTED_HSTS = "max-age=31536000; includeSubDomains"


@dataclass(frozen=True)
class LiveServer:
    base_url: str
    websocket_url: str
    log_path: Path


def _unused_loopback_port() -> int:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(("127.0.0.1", 0))
        return int(sock.getsockname()[1])


def _log_tail(log_handle, log_path: Path) -> str:
    log_handle.flush()
    if not log_path.exists():
        return "<server log was not created>"
    return log_path.read_text(encoding="utf-8", errors="replace")[-6000:]


def _wait_until_ready(
    process: subprocess.Popen,
    server: LiveServer,
    log_handle,
) -> None:
    deadline = time.monotonic() + START_TIMEOUT
    last_error = "server did not answer"

    with httpx.Client(
        base_url=server.base_url,
        timeout=1.0,
        trust_env=False,
    ) as client:
        while time.monotonic() < deadline:
            return_code = process.poll()
            if return_code is not None:
                raise AssertionError(
                    "uvicorn exited before becoming ready "
                    f"(code {return_code}).\n--- server log ---\n"
                    f"{_log_tail(log_handle, server.log_path)}"
                )
            try:
                response = client.get("/api/v1/health")
                if response.status_code == 200:
                    return
                last_error = f"health returned {response.status_code}"
            except httpx.HTTPError as exc:
                last_error = f"{type(exc).__name__}: {exc}"
            time.sleep(0.1)

    raise AssertionError(
        f"uvicorn was not ready within {START_TIMEOUT:.0f}s ({last_error}).\n"
        f"--- server log ---\n{_log_tail(log_handle, server.log_path)}"
    )


def _stop_server(process: subprocess.Popen) -> None:
    if process.poll() is not None:
        return
    process.send_signal(signal.SIGINT)
    try:
        process.wait(timeout=STOP_TIMEOUT)
    except subprocess.TimeoutExpired:
        process.terminate()
        try:
            process.wait(timeout=5)
        except subprocess.TimeoutExpired:
            process.kill()
            process.wait(timeout=5)


@contextmanager
def _run_server(workdir: Path, *, trust_proxy_headers: bool):
    """Run the shipped module entrypoint and require a graceful shutdown."""
    if os.name != "posix":
        pytest.skip(
            "Live Uvicorn smoke requires POSIX subprocess signals; "
            "Windows shutdown needs separate validation"
        )
    data_dir = workdir / "data"
    data_dir.mkdir(parents=True)
    port = _unused_loopback_port()
    log_path = workdir / "uvicorn.log"

    env = dict(os.environ)
    env.update(
        {
            "LDR_APP_ALLOW_REGISTRATIONS": "true",
            "LDR_BOOTSTRAP_ALLOW_UNENCRYPTED": "true",
            "LDR_DATA_DIR": str(data_dir),
            "LDR_DISABLE_RATE_LIMITING": "true",
            "LDR_NEWS_SCHEDULER_ENABLED": "false",
            "LDR_TEST_MODE": "false",
            "LDR_TESTING_WITH_MOCKS": "true",
            "LDR_WEB_HOST": "127.0.0.1",
            "LDR_WEB_PORT": str(port),
            "LDR_WEB_QUEUE_PROCESSOR_ENABLED": "false",
            "LDR_WEB_USE_HTTPS": "false",
            "PYTHONUNBUFFERED": "1",
            "PYTHONPATH": os.pathsep.join(
                [str(SRC_ROOT), env.get("PYTHONPATH", "")]
            ).rstrip(os.pathsep),
        }
    )
    # pytest exports this only while a test is running.  Letting the child
    # inherit it would disable production Secure-cookie behavior and would
    # make the proxy assertions pass through the wrong code path.
    env.pop("PYTEST_CURRENT_TEST", None)
    if trust_proxy_headers:
        env["TRUST_PROXY_HEADERS"] = "true"
        # As of the fix for the round-7 regression, the startup guard in
        # web/dependencies/rate_limit.py only hard-fails
        # TRUST_PROXY_HEADERS=true + a wildcard LDR_SERVER_FORWARDED_
        # ALLOW_IPS when the operator has ALSO opted into
        # LDR_SERVER_TRUSTED_PROXY_COUNT or set LDR_SERVER_FORWARDED_
        # ALLOW_IPS explicitly -- neither of which this fixture does, so
        # omitting the line below would no longer abort startup (it would
        # log a warning and boot). It is kept anyway, restricted to
        # 127.0.0.1, so uvicorn's ProxyHeadersMiddleware only trusts
        # X-Forwarded-For from the test process's own direct peer --
        # mirroring the single-hop reverse-proxy-on-loopback deployment
        # this combination is meant for, and keeping these tests exercising
        # the "safe, restricted" code path rather than the merely-warned
        # legacy one.
        env["LDR_SERVER_FORWARDED_ALLOW_IPS"] = "127.0.0.1"
    else:
        env.pop("TRUST_PROXY_HEADERS", None)
        env.pop("LDR_SERVER_FORWARDED_ALLOW_IPS", None)

    server = LiveServer(
        base_url=f"http://127.0.0.1:{port}",
        websocket_url=(
            f"ws://127.0.0.1:{port}/ws/socket.io/?EIO=4&transport=websocket"
        ),
        log_path=log_path,
    )

    return_code = None
    with log_path.open("w", encoding="utf-8") as log_handle:
        process = subprocess.Popen(
            [sys.executable, "-m", "local_deep_research.web.app"],
            cwd=PROJECT_ROOT,
            env=env,
            stdout=log_handle,
            stderr=subprocess.STDOUT,
            text=True,
        )
        try:
            _wait_until_ready(process, server, log_handle)
            yield server
        finally:
            _stop_server(process)
            return_code = process.returncode
            log_handle.flush()

    log_text = log_path.read_text(encoding="utf-8", errors="replace")
    assert return_code == 0, (
        "uvicorn did not complete its lifespan shutdown cleanly "
        f"(code {return_code}).\n--- server log ---\n"
        f"{log_text[-6000:]}"
    )
    assert "Shutting down Local Deep Research" in log_text, (
        "uvicorn exited without resuming the FastAPI lifespan shutdown.\n"
        f"--- server log ---\n{log_text[-6000:]}"
    )


@pytest.fixture(scope="module")
def trusted_proxy_server(tmp_path_factory) -> LiveServer:
    workdir = tmp_path_factory.mktemp("live_uvicorn_trusted_proxy")
    with _run_server(workdir, trust_proxy_headers=True) as server:
        yield server


def _session_cookie_headers(response: httpx.Response) -> list[str]:
    return [
        value
        for value in response.headers.get_list("set-cookie")
        if value.lower().startswith("session=")
    ]


def test_forwarded_https_is_ignored_when_proxy_trust_is_disabled(tmp_path):
    with _run_server(tmp_path, trust_proxy_headers=False) as server:
        with httpx.Client(
            base_url=server.base_url,
            headers={"X-Forwarded-Proto": "https"},
            timeout=REQUEST_TIMEOUT,
            trust_env=False,
        ) as client:
            response = client.get("/auth/csrf-token")
            redirect = client.get("/settings")

    cookies = _session_cookie_headers(response)
    assert response.status_code == 200
    assert cookies, "the CSRF endpoint did not persist its session"
    assert "strict-transport-security" not in response.headers
    assert all("; secure" not in value.lower() for value in cookies)
    assert redirect.status_code == 307
    assert urlsplit(redirect.headers["location"]).scheme == "http"


def test_production_entrypoint_serves_health_over_real_tcp(
    trusted_proxy_server: LiveServer,
):
    with httpx.Client(
        base_url=trusted_proxy_server.base_url,
        timeout=REQUEST_TIMEOUT,
        trust_env=False,
    ) as client:
        response = client.get("/api/v1/health")

    assert response.status_code == 200
    assert response.http_version == "HTTP/1.1"
    assert response.json()["status"] == "ok"
    assert response.headers["x-content-type-options"] == "nosniff"
    assert "server" not in response.headers, (
        "the real uvicorn response exposed its Server fingerprint"
    )


def test_trusted_forwarded_https_controls_hsts_and_secure_cookie(
    trusted_proxy_server: LiveServer,
):
    with httpx.Client(
        base_url=trusted_proxy_server.base_url,
        headers={"X-Forwarded-Proto": "https"},
        timeout=REQUEST_TIMEOUT,
        trust_env=False,
    ) as client:
        response = client.get("/auth/csrf-token")
        redirect = client.get("/settings")

    cookies = _session_cookie_headers(response)
    assert response.status_code == 200
    assert cookies, "the CSRF endpoint did not persist its session"
    assert response.headers.get("strict-transport-security") == EXPECTED_HSTS
    assert all("; secure" in value.lower() for value in cookies)
    assert redirect.status_code == 307
    assert urlsplit(redirect.headers["location"]).scheme == "https"


def _socketio_connect_ack(url: str, cookie: str | None) -> tuple[str, str]:
    headers = {"Cookie": f"session={cookie}"} if cookie is not None else None
    with websocket_connect(
        url,
        additional_headers=headers,
        open_timeout=REQUEST_TIMEOUT,
        close_timeout=5,
        proxy=None,
    ) as websocket:
        open_packet = websocket.recv(timeout=REQUEST_TIMEOUT)
        assert isinstance(open_packet, str) and open_packet.startswith("0"), (
            f"unexpected Engine.IO open packet: {open_packet!r}"
        )
        websocket.send("40")
        while True:
            ack = websocket.recv(timeout=REQUEST_TIMEOUT)
            assert isinstance(ack, str), ack
            if ack == "2":
                websocket.send("3")
                continue
            return open_packet, ack


def test_authenticated_session_crosses_a_real_websocket_upgrade(
    trusted_proxy_server: LiveServer,
):
    username = f"live_uvicorn_{uuid.uuid4().hex[:10]}"
    password = "LiveUvicornPass123!"  # noqa: S105
    with httpx.Client(
        base_url=trusted_proxy_server.base_url,
        timeout=REQUEST_TIMEOUT,
        trust_env=False,
        follow_redirects=False,
    ) as client:
        csrf_response = client.get("/auth/csrf-token")
        csrf_token = csrf_response.json()["csrf_token"]
        registered = client.post(
            "/auth/register",
            data={
                "username": username,
                "password": password,
                "confirm_password": password,
                "acknowledge": "true",
                "csrf_token": csrf_token,
            },
            timeout=httpx.Timeout(
                REQUEST_TIMEOUT, read=USER_PROVISIONING_READ_TIMEOUT
            ),
        )
        assert registered.status_code == 302, registered.text[:500]
        session_cookie = client.cookies.get("session")
        assert session_cookie, "registration did not issue a session cookie"

    open_packet, accepted = _socketio_connect_ack(
        trusted_proxy_server.websocket_url, session_cookie
    )
    assert json.loads(open_packet[1:])["upgrades"] == []
    assert accepted.startswith("40"), accepted

    _, rejected = _socketio_connect_ack(
        trusted_proxy_server.websocket_url, None
    )
    assert rejected.startswith("44"), rejected