Files
mtusi2026lab4/student-tests/test_presigned.py
T

134 lines
4.3 KiB
Python

import base64
import hashlib
import io
import uuid
import httpx
from PIL import Image
BASE = "http://localhost:8080"
def _user(c):
u = "ps_" + uuid.uuid4().hex[:10]
p = "Test-password-1234"
r = c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "P", "password": p})
assert r.status_code == 201, r.text
user = r.json()
cred = base64.b64encode(f"{u}:{p}".encode()).decode()
s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json()
return {"id": user["id"], "token": s["token"]}
def _auth(a):
return {"Authorization": "Bearer " + a["token"]}
def _png(w=320, h=200):
buf = io.BytesIO()
Image.new("RGB", (w, h), (0, 255, 0)).save(buf, format="PNG")
return buf.getvalue()
def _chat(c, a, b):
r = c.post(
f"{BASE}/api/v1/chats",
json={"title": "ps", "member_ids": [a["id"], b["id"]]},
headers=_auth(a),
)
assert r.status_code == 201, r.text
return r.json()
def test_presigned_happy_path():
with httpx.Client(timeout=30) as c:
a = _user(c)
b = _user(c)
chat = _chat(c, a, b)
data = _png()
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign",
params={"filename": "p.png", "size": len(data), "content_type": "image/png"},
headers=_auth(a),
)
assert r.status_code == 201, r.text
presign = r.json()
put = httpx.put(presign["upload_url"], content=data, headers={"Content-Type": "image/png"})
assert put.status_code == 200, put.text
r = c.post(
f"{BASE}/api/v1/attachments/{presign['attachment_id']}/complete",
headers=_auth(a),
)
assert r.status_code == 201, r.text
att = r.json()
assert att["status"] == "ready"
assert att["sha256"] == hashlib.sha256(data).hexdigest()
dl = c.get(f"{BASE}/api/v1/attachments/{att['id']}/content", headers=_auth(b))
assert dl.status_code == 200
assert dl.content == data
def test_presigned_bad_signature_rejected():
with httpx.Client(timeout=30) as c:
a = _user(c)
b = _user(c)
chat = _chat(c, a, b)
data = _png()
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign",
params={"filename": "p.png", "size": len(data), "content_type": "image/png"},
headers=_auth(a),
)
url = r.json()["upload_url"]
bad = url.replace("X-Amz-Signature=", "X-Amz-Signature=deadbeef")
put = httpx.put(bad, content=data, headers={"Content-Type": "image/png"})
assert put.status_code == 403, put.text
def test_presigned_complete_without_upload_409():
with httpx.Client(timeout=10) as c:
a = _user(c)
b = _user(c)
chat = _chat(c, a, b)
data = _png()
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign",
params={"filename": "p.png", "size": len(data), "content_type": "image/png"},
headers=_auth(a),
)
att_id = r.json()["attachment_id"]
r = c.post(f"{BASE}/api/v1/attachments/{att_id}/complete", headers=_auth(a))
assert r.status_code == 409, r.text
def test_presigned_outsider_404():
with httpx.Client(timeout=10) as c:
a = _user(c)
b = _user(c)
eve = _user(c)
chat = _chat(c, a, b)
data = _png()
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign",
params={"filename": "p.png", "size": len(data), "content_type": "image/png"},
headers=_auth(a),
)
att_id = r.json()["attachment_id"]
r = c.post(f"{BASE}/api/v1/attachments/{att_id}/complete", headers=_auth(eve))
assert r.status_code == 404, r.text
def test_proxy_endpoint_still_works():
with httpx.Client(timeout=10) as c:
a = _user(c)
b = _user(c)
chat = _chat(c, a, b)
data = _png()
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments",
params={"filename": "proxy.png"},
content=data,
headers={**_auth(a), "Content-Type": "image/png"},
)
assert r.status_code == 201, r.text
assert r.json()["status"] == "ready"