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.

3.8 KB
Raw Blame
"""Multipart uploads must reach the handler as files — not be filtered out.

Regression fence: starlette's form parser yields STARLETTE
``UploadFile`` instances, while ``fastapi.UploadFile`` is a *subclass* of
it (fastapi>=0.113). The upload handlers filtered form entries with
``isinstance(f, fastapi.UploadFile)``, which is False for every real
upload — so ``POST /library/api/collections/{id}/upload`` (and
``/api/upload/pdf``) always returned 400 "No files provided" while unit
tests that construct fastapi.UploadFile directly stayed green. Found by
the Puppeteer api-crud shard; this pins the behavior at plain-HTTP level
so the fast CI suite catches it too.
"""

import uuid

import pytest
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def auth_client():
    """Authenticated test client (same pattern as test_settings_api.py)."""
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)

    user = f"test_upload_{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 broken: login returned {resp.status_code} "
            f"(expected 302): {resp.text[:300]}"
        )

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

    yield c

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


@pytest.mark.timeout(120)
def test_collection_upload_accepts_multipart_file(auth_client):
    # Create a collection to upload into.
    create = auth_client.post(
        "/library/api/collections",
        json={"name": f"upload-fence-{uuid.uuid4().hex[:6]}"},
    )
    assert create.status_code == 200, create.text
    collection_id = create.json()["collection"]["id"]

    resp = auth_client.post(
        f"/library/api/collections/{collection_id}/upload",
        files={
            "files": (
                "fence.txt",
                b"upload regression fence document\n",
                "text/plain",
            )
        },
        data={"storage_mode": "database"},
    )
    assert resp.status_code == 200, (
        f"multipart upload rejected ({resp.status_code}): {resp.text} — "
        "if the body is 'No files provided', the UploadFile isinstance "
        "filter is dropping starlette UploadFile instances again"
    )
    body = resp.json()
    assert body.get("success") is True, body
    assert body.get("summary", {}).get("successful") == 1, body


@pytest.mark.timeout(120)
def test_pdf_upload_endpoint_sees_multipart_files(auth_client):
    # A .txt is rejected by PDF validation — but the rejection must be the
    # per-file error path, NOT the blanket "No files provided" (which means
    # the file never reached the handler).
    resp = auth_client.post(
        "/api/upload/pdf",
        files={"files": ("fence.txt", b"not a pdf", "text/plain")},
    )
    assert resp.status_code == 400, resp.text
    body = resp.json()
    assert body.get("error") != "No files provided", (
        "upload_pdf never saw the file — the UploadFile isinstance filter "
        "is dropping starlette UploadFile instances again"
    )