PUT /uploads/<batch>/<name> (raw bytes, 50 MB cap) writes the file under ~/.local/share/ai-agent-worker/uploads (WORKER_UPLOADS_DIR) — outside every repo — atomically, and answers its absolute path. Batches older than WORKER_UPLOADS_KEEP_DAYS (30) are pruned on the next upload. Refused on the host sidecar; `ai-agent-worker uninstall` removes the folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
"""/uploads: the hub's composer attachments, copied onto a worker."""
|
|
|
|
import os
|
|
import time
|
|
|
|
import pytest
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.testclient import TestClient
|
|
|
|
import uploads
|
|
|
|
H = {"Authorization": "Bearer t"}
|
|
|
|
|
|
def _auth(h):
|
|
if h != "Bearer t":
|
|
raise HTTPException(401, "unauthorized")
|
|
|
|
|
|
@pytest.fixture()
|
|
def client(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(uploads, "IS_WORKER", True)
|
|
app = FastAPI()
|
|
app.include_router(uploads.make_router(_auth, lambda: tmp_path / "uploads"))
|
|
return TestClient(app), tmp_path / "uploads"
|
|
|
|
|
|
def test_put_writes_the_file_and_answers_its_absolute_path(client):
|
|
c, root = client
|
|
r = c.put("/uploads/abc123def456/shot.png", content=b"\x89PNG data", headers=H)
|
|
assert r.status_code == 200
|
|
path = r.json()["path"]
|
|
assert os.path.isabs(path) and path.endswith("/abc123def456/shot.png")
|
|
assert (root / "abc123def456" / "shot.png").read_bytes() == b"\x89PNG data"
|
|
assert r.json()["size"] == 9
|
|
# no temp file left behind
|
|
assert [p.name for p in (root / "abc123def456").iterdir()] == ["shot.png"]
|
|
|
|
|
|
def test_put_needs_the_bearer(client):
|
|
c, _ = client
|
|
assert c.put("/uploads/abc/x.txt", content=b"x").status_code == 401
|
|
|
|
|
|
@pytest.mark.parametrize("path", ["/uploads/..%2Fetc/x.txt", "/uploads/abc/.hidden",
|
|
"/uploads/abc/a%20b.txt", "/uploads/a.b/x.txt"])
|
|
def test_put_refuses_paths_the_hub_never_makes(client, path):
|
|
c, root = client
|
|
r = c.put(path, content=b"x", headers=H)
|
|
assert r.status_code in (400, 404)
|
|
assert not any(root.rglob("*")) if root.exists() else True
|
|
|
|
|
|
def test_put_over_the_cap_leaves_nothing(client, monkeypatch):
|
|
c, root = client
|
|
monkeypatch.setattr(uploads, "MAX_BYTES", 4)
|
|
r = c.put("/uploads/abc/big.bin", content=b"12345", headers=H)
|
|
assert r.status_code == 413
|
|
assert not any((root / "abc").iterdir())
|
|
|
|
|
|
def test_refused_on_the_host_sidecar(client, monkeypatch):
|
|
c, _ = client
|
|
monkeypatch.setattr(uploads, "IS_WORKER", False)
|
|
assert c.put("/uploads/abc/x.txt", content=b"x", headers=H).status_code == 409
|
|
|
|
|
|
def test_prune_drops_old_batches_only(tmp_path):
|
|
old, new = tmp_path / "old", tmp_path / "new"
|
|
for d in (old, new):
|
|
d.mkdir()
|
|
(d / "f.txt").write_text("x")
|
|
past = time.time() - 40 * 86400
|
|
os.utime(old, (past, past))
|
|
assert uploads.prune(tmp_path, 30) == ["old"]
|
|
assert not old.exists() and new.exists()
|
|
assert uploads.prune(tmp_path, 0) == []
|