Files
mtusi2026lab5/student-tests/test_lab5_remarks.py
T

59 lines
2.1 KiB
Python

"""Targeted checks for lab5 reviewer remarks (415, thumbnail floor, keys)."""
from __future__ import annotations
import hashlib
import io
import imagegen
from helpers import call, chat_for, register, upload, wait_ready
def _jpeg_dims(data: bytes):
from PIL import Image
img = Image.open(io.BytesIO(data))
return img.size
def test_corrupted_and_truncated_rejected_415():
alice = register("rem_a")
chat = chat_for(alice)
for name, data in imagegen.corrupted_pngs().items():
resp = upload(alice["token"], chat["id"], data, "bad.png", "image/png", 415)
assert resp["json"]["error"]["code"] == "unsupported_media_type", (name, resp["json"])
for name, data in imagegen.corrupted_jpegs().items():
resp = upload(alice["token"], chat["id"], data, "bad.jpg", "image/jpeg", 415)
assert resp["json"]["error"]["code"] == "unsupported_media_type", (name, resp["json"])
def test_thumbnail_319x200_is_256x160():
alice, bob = register("th_a"), register("th_b")
chat = chat_for(alice, bob)
data = imagegen.rgb_png(319, 200)
att = upload(alice["token"], chat["id"], data, "odd.png", "image/png", 202)["json"]
ready = wait_ready(bob["token"], att)
thumbs = [v for v in ready["variants"] if v["name"] == "thumbnail"]
assert len(thumbs) == 1
assert (thumbs[0]["width"], thumbs[0]["height"]) == (256, 160), thumbs[0]
body = call(
"GET",
f"/api/v1/attachments/{att['id']}/content?variant=thumbnail",
200,
token=bob["token"],
)["bytes"]
assert _jpeg_dims(body) == (256, 160)
assert hashlib.sha256(body).hexdigest() == thumbs[0]["sha256"]
def test_jpeg_ready_download_uses_stored_key():
alice = register("jpg_a")
chat = chat_for(alice)
data = imagegen.fixture("sample.jpg")
att = upload(alice["token"], chat["id"], data, "s.jpg", "image/jpeg", 202)["json"]
ready = wait_ready(alice["token"], att)
assert ready["status"] == "ready"
dl = call("GET", f"/api/v1/attachments/{att['id']}/content", 200, token=alice["token"])
assert dl["bytes"] == data
assert hashlib.sha256(dl["bytes"]).hexdigest() == att["sha256"]