Files
mtusi2026lab7/student-tests/test_attachments.py
T

204 lines
6.4 KiB
Python

"""Black-box attachment contract and edge-case regressions."""
import hashlib
import os
import struct
import uuid
import zlib
from io import BytesIO
from urllib.parse import quote
import pytest
from client import Client
from PIL import Image
@pytest.fixture(scope="module")
def scenario():
c = Client(os.environ.get("BASE_URL", "http://127.0.0.1:8085"))
return c, *c.bootstrap()
def image_bytes(fmt="PNG", size=(32, 20)):
stream = BytesIO()
Image.new("RGB", size, (100, 130, 200)).save(stream, fmt)
return stream.getvalue()
def upload(scenario, data=None, name="sample.png", mime="image/png", actor=None, status=202):
c, alice, _, _, chat = scenario
return c.call(
"POST",
f"/api/v1/chats/{chat['id']}/attachments?filename={quote(name, safe='')}",
status,
actor=actor or alice,
raw=data if data is not None else image_bytes(),
headers={"Content-Type": mime},
schema="Attachment" if status == 202 else None,
)
def test_roundtrip_acl_and_variant(scenario):
c, alice, bob, eve, chat = scenario
data = image_bytes()
a = upload(scenario, data)["json"]
assert a["sha256"] == hashlib.sha256(data).hexdigest()
assert a["status"] == "queued"
a = c.wait_ready(alice, a)
assert {v["name"] for v in a["variants"]} == {"thumbnail", "crop", "optimized"}
path = f"/api/v1/attachments/{a['id']}"
assert c.call("GET", path + "/content", 200, actor=bob)["bytes"] == data
c.call("GET", path, 404, actor=eve)
c.call("GET", path + "/content", 404, actor=eve)
c.call("GET", path + "/content?variant=thumbnail", 200, actor=alice)
c.call("GET", path + "/content?variant=unknown", 400, actor=alice)
message = c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
201,
actor=alice,
body={"text": "", "attachment_ids": [a["id"]]},
headers={"Idempotency-Key": uuid.uuid4().hex},
schema="Message",
)["json"]
assert message["text"] == "" and message["attachment_ids"] == [a["id"]]
@pytest.mark.parametrize(
"name", [" ", "../x.png", "a\\b.png", "a\0.png", "a\x7f.png", "a\x85.png", "a\n.png"]
)
def test_bad_filename(scenario, name):
upload(scenario, name=name, status=400)
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data))
def png_with_data(data, width=1, height=1, color=2, palette=b""):
return (
b"\x89PNG\r\n\x1a\n"
+ chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, color, 0, 0, 0))
+ (chunk(b"PLTE", palette) if palette else b"")
+ chunk(b"IDAT", data)
+ chunk(b"IEND", b"")
)
@pytest.mark.parametrize(
"data,mime",
[
(image_bytes()[:33], "image/png"),
(image_bytes()[:-15], "image/png"),
(png_with_data(b"broken-zlib"), "image/png"),
(png_with_data(zlib.compress(b"\x05\0\0\0")), "image/png"),
(png_with_data(zlib.compress(b"\0\0"), color=3, palette=bytes(771)), "image/png"),
(bytes.fromhex("ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9"), "image/jpeg"),
(bytes.fromhex("ffd8ffc00008080001000101ffda0002ffd9"), "image/jpeg"),
(image_bytes(), "image/jpeg"),
(b"not-an-image", "image/png"),
],
)
def test_corrupt_or_spoofed_image(scenario, data, mime):
upload(scenario, data=data, mime=mime, status=415)
@pytest.mark.parametrize("size", [(8000, 5001), (20000, 10000)])
def test_pixel_limit_before_decode(scenario, size):
data = png_with_data(b"", width=size[0], height=size[1])
upload(scenario, data=data, status=413)
def test_byte_limit_and_exact_boundary(scenario):
data = image_bytes()
exact = data + bytes(10 * 1024 * 1024 - len(data))
a = upload(scenario, data=exact)["json"]
assert a["size_bytes"] == len(exact)
upload(scenario, data=exact + b"x", status=413)
@pytest.mark.parametrize(
"body",
[
{"attachment_ids": []},
{"text": None},
{"text": "x", "attachment_ids": None},
{"text": "x", "attachment_ids": [123]},
],
)
def test_strict_message_fields(scenario, body):
c, alice, _, _, chat = scenario
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
actor=alice,
body=body,
headers={"Idempotency-Key": uuid.uuid4().hex},
)
def test_foreign_unknown_and_ordered_attachments(scenario):
c, alice, _, _, chat = scenario
first, second = upload(scenario)["json"], upload(scenario)["json"]
other = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "other", "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
for aid in [first["id"], str(uuid.uuid4())]:
c.call(
"POST",
f"/api/v1/chats/{other['id']}/messages",
404,
actor=alice,
body={"text": "", "attachment_ids": [aid]},
headers={"Idempotency-Key": uuid.uuid4().hex},
)
key = uuid.uuid4().hex
body = {"text": "with files", "attachment_ids": [second["id"], first["id"]]}
p = f"/api/v1/chats/{chat['id']}/messages"
sent = c.call(
"POST", p, 201, actor=alice, body=body, headers={"Idempotency-Key": key}, schema="Message"
)["json"]
repeat = c.call(
"POST", p, 201, actor=alice, body=body, headers={"Idempotency-Key": key}, schema="Message"
)["json"]
assert repeat == sent
c.call(
"POST",
p,
409,
actor=alice,
body={**body, "attachment_ids": list(reversed(body["attachment_ids"]))},
headers={"Idempotency-Key": key},
)
history = c.call("GET", p, 200, actor=alice, schema="MessagePage")["json"]["items"]
assert next(m for m in history if m["id"] == sent["id"]) == sent
def test_nul_message_roundtrip(scenario):
c, alice, _, _, chat = scenario
assert c.message(alice, chat, "abc\0def")["json"]["text"] == "abc\0def"
def test_truncated_jpeg_with_forged_eoi(scenario):
import random
stream = BytesIO()
Image.frombytes("RGB", (512, 512), random.Random(42).randbytes(512 * 512 * 3)).save(
stream, "JPEG", quality=95
)
original = stream.getvalue()
upload(scenario, data=original, name="real.jpg", mime="image/jpeg")
upload(
scenario,
data=original[: len(original) // 2] + b"\xff\xd9",
name="broken.jpg",
mime="image/jpeg",
status=415,
)