feat: file upload, allowlist, and download/preview

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
肉末
2026-07-14 12:47:58 +08:00
parent 0e59936e34
commit eda7010cae
4 changed files with 219 additions and 7 deletions
+31
View File
@@ -68,3 +68,34 @@ def test_title_from_first_line(client):
def test_explicit_title(client):
r = client.post("/api/items", json={"body": "body text", "title": "Custom"})
assert r.json()["title"] == "Custom"
def test_upload_small_file(client):
files = {"file": ("hi.txt", b"abc", "text/plain")}
data = {"is_public": "true", "ttl": "24h"}
r = client.post("/api/items/upload", files=files, data=data)
assert r.status_code == 200
slug = r.json()["slug"]
f = client.get(f"/api/items/{slug}/file")
assert f.status_code == 200
assert f.content == b"abc"
def test_reject_oversize(client, monkeypatch):
import app.config as config_module
monkeypatch.setattr(config_module.settings, "max_upload_mb", 1)
big = b"x" * (1 * 1024 * 1024 + 1)
files = {"file": ("big.txt", big, "text/plain")}
r = client.post("/api/items/upload", files=files, data={"is_public": "true", "ttl": "24h"})
assert r.status_code == 413
def test_reject_disallowed_extension(client):
files = {"file": ("malware.exe", b"mz", "application/octet-stream")}
r = client.post(
"/api/items/upload",
files=files,
data={"is_public": "true", "ttl": "24h"},
)
assert r.status_code == 400