forked from mtusi2026/mtusi2026lab5
191 lines
6.1 KiB
Python
191 lines
6.1 KiB
Python
import base64
|
|
import io
|
|
import uuid
|
|
|
|
import httpx
|
|
from PIL import Image
|
|
|
|
BASE = "http://localhost:8080"
|
|
|
|
|
|
def _register_and_login(client: httpx.Client) -> dict:
|
|
username = "att_" + uuid.uuid4().hex[:10]
|
|
password = "Test-password-1234"
|
|
r = client.post(
|
|
f"{BASE}/api/v1/users",
|
|
json={"username": username, "display_name": "A", "password": password},
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
user = r.json()
|
|
cred = base64.b64encode((username + ":" + password).encode()).decode()
|
|
r = client.post(
|
|
f"{BASE}/api/v1/auth/sessions",
|
|
headers={"Authorization": "Basic " + cred},
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
return {"id": user["id"], "token": r.json()["token"]}
|
|
|
|
|
|
def _auth(actor: dict) -> dict:
|
|
return {"Authorization": "Bearer " + actor["token"]}
|
|
|
|
|
|
def _idem(actor: dict) -> dict:
|
|
return {**_auth(actor), "Idempotency-Key": uuid.uuid4().hex}
|
|
|
|
|
|
def _png(w: int = 320, h: int = 200, color=(255, 0, 0)) -> bytes:
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (w, h), color).save(buf, format="PNG")
|
|
return buf.getvalue()
|
|
|
|
|
|
def _chat(client: httpx.Client, owner: dict, *members: dict) -> dict:
|
|
member_ids = [owner["id"]] + [m["id"] for m in members]
|
|
r = client.post(
|
|
f"{BASE}/api/v1/chats",
|
|
json={"title": "att", "member_ids": member_ids},
|
|
headers=_auth(owner),
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
return r.json()
|
|
|
|
|
|
def _upload(client, actor, chat_id, data, filename="x.png", content_type="image/png"):
|
|
return client.post(
|
|
f"{BASE}/api/v1/chats/{chat_id}/attachments",
|
|
params={"filename": filename},
|
|
content=data,
|
|
headers={**_auth(actor), "Content-Type": content_type},
|
|
)
|
|
|
|
|
|
def test_oversize_returns_413():
|
|
with httpx.Client(timeout=30) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
big = b"\x89PNG\r\n\x1a\n" + b"\x00" * (10 * 1024 * 1024)
|
|
r = _upload(c, a, chat["id"], big, filename="big.png")
|
|
assert r.status_code == 413, r.text
|
|
assert r.json()["error"]["code"] == "payload_too_large"
|
|
|
|
|
|
def test_corrupted_image_returns_415():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
data = _png()[:50]
|
|
r = _upload(c, a, chat["id"], data)
|
|
assert r.status_code == 415, r.text
|
|
assert r.json()["error"]["code"] == "unsupported_media_type"
|
|
|
|
|
|
def test_mime_spoof_returns_415():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
r = _upload(c, a, chat["id"], b"not an image at all")
|
|
assert r.status_code == 415, r.text
|
|
assert r.json()["error"]["code"] == "unsupported_media_type"
|
|
|
|
|
|
def test_bad_filename_returns_400():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
r = _upload(c, a, chat["id"], _png(), filename="../etc/passwd")
|
|
assert r.status_code == 400, r.text
|
|
assert r.json()["error"]["code"] == "invalid_request"
|
|
|
|
|
|
def test_empty_file_returns_415():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
r = _upload(c, a, chat["id"], b"")
|
|
assert r.status_code == 415, r.text
|
|
|
|
|
|
def test_outsider_cannot_download():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
eve = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
att = _upload(c, a, chat["id"], _png()).json()
|
|
r = c.get(f"{BASE}/api/v1/attachments/{att['id']}", headers=_auth(eve))
|
|
assert r.status_code == 404
|
|
r = c.get(f"{BASE}/api/v1/attachments/{att['id']}/content", headers=_auth(eve))
|
|
assert r.status_code == 404
|
|
|
|
|
|
def test_attach_foreign_attachment_returns_404():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat1 = _chat(c, a, b)
|
|
chat2 = _chat(c, a, b)
|
|
att = _upload(c, a, chat1["id"], _png()).json()
|
|
r = c.post(
|
|
f"{BASE}/api/v1/chats/{chat2['id']}/messages",
|
|
json={"text": "hi", "attachment_ids": [att["id"]]},
|
|
headers=_idem(a),
|
|
)
|
|
assert r.status_code == 404, r.text
|
|
|
|
|
|
def test_empty_text_without_attachments_returns_400():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
r = c.post(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": ""},
|
|
headers=_idem(a),
|
|
)
|
|
assert r.status_code == 400, r.text
|
|
assert r.json()["error"]["code"] == "invalid_request"
|
|
|
|
|
|
def test_empty_text_with_attachment_returns_201():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
att = _upload(c, a, chat["id"], _png()).json()
|
|
r = c.post(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": "", "attachment_ids": [att["id"]]},
|
|
headers=_idem(a),
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
assert r.json()["attachment_ids"] == [att["id"]]
|
|
|
|
|
|
def test_user_error_is_not_500():
|
|
with httpx.Client(timeout=10) as c:
|
|
a = _register_and_login(c)
|
|
b = _register_and_login(c)
|
|
chat = _chat(c, a, b)
|
|
cases = [
|
|
(_upload(c, a, chat["id"], b"not image"), 415),
|
|
(_upload(c, a, chat["id"], b""), 415),
|
|
(_upload(c, a, chat["id"], _png(), filename="../x"), 400),
|
|
(
|
|
c.post(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": ""},
|
|
headers=_idem(a),
|
|
),
|
|
400,
|
|
),
|
|
]
|
|
for response, expected in cases:
|
|
assert response.status_code == expected, response.text
|
|
assert response.status_code < 500 |