Files
mtusi2026lab5/tests/custom/attachments.py
T
Mikhail Verkovykh 9e43faa536 feat(lab5): durable async attachment pipeline through grade 4
- RabbitMQ quorum queue, persistent messages and publisher confirms
- S3 staging plus PostgreSQL attachments/jobs outbox transaction
- worker variants (thumbnail, crop, optimized WebP), DB lease and lease sweeper
- jittered retry, permanent failures, explicit replay and admission cap
- deterministic crash hooks, resilience script and evidence for grade 4
- async-aware custom tests, oversized pixel case, docs and changelog
2026-11-15 00:30:36 +03:00

210 lines
7.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Grade-4 negative checks for the attachment path.
The public smoke covers the happy path. This test adds the failure cases the
assignment requires: an empty body, an oversized body, a corrupted image, a
MIME spoof, a foreign attachment id and an attachment that belongs to another
chat. Every client mistake must remain a 4xx response with the contract error
envelope, not a 500.
"""
import argparse
import hashlib
import struct
import sys
import uuid
import zlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
FIXTURE = ROOT / "tests" / "fixtures" / "sample.png"
MAX_UPLOAD_BYTES = 10 * 1024 * 1024
MAX_IMAGE_PIXELS = 40_000_000
def png_chunk(kind, payload):
crc = zlib.crc32(kind + payload) & 0xFFFFFFFF
return struct.pack(">I", len(payload)) + kind + payload + struct.pack(">I", crc)
def oversized_png(width, height):
"""Build a valid grayscale PNG whose dimensions exceed the pixel cap.
The image is solid black, so the IDAT stream stays tiny and the request is
far below the byte limit. Only the declared width*height can trigger 413.
"""
ihdr = struct.pack(">IIBBBBB", width, height, 8, 0, 0, 0, 0)
compressor = zlib.compressobj(9)
idat = bytearray()
row = b"\x00" + b"\x00" * width # filter type 0 + one 8-bit sample per pixel
for _ in range(height):
idat += compressor.compress(row)
idat += compressor.flush()
return (
b"\x89PNG\r\n\x1a\n"
+ png_chunk(b"IHDR", ihdr)
+ png_chunk(b"IDAT", bytes(idat))
+ png_chunk(b"IEND", b"")
)
def upload(client, actor, chat_id, raw, filename, content_type):
headers = {} if content_type is None else {"Content-Type": content_type}
return client.call(
"POST",
f"/api/v1/chats/{chat_id}/attachments?filename={filename}",
expected=(202, 400, 404, 413, 415),
actor=actor,
raw=raw,
headers=headers,
)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--ca-file", default=None)
args = parser.parse_args()
c = Client(args.base_url, args.ca_file, timeout=60)
alice, bob, eve, chat = c.bootstrap()
png = FIXTURE.read_bytes()
# Happy path first, so the negative checks can refer to a real attachment.
good = c.wait_ready(alice, c.attachment(alice, chat)[0])
check(good["status"] == "ready", "valid upload is not ready")
check(good["error_code"] is None, "ready attachment carries an error")
check(
{item["name"] for item in good["variants"]} == {"thumbnail", "crop", "optimized"},
"ready attachment is missing a grade-4 variant",
)
# 1. One byte over the contract limit must be a 413, decided before decode.
oversized = b"\x00" * (MAX_UPLOAD_BYTES + 1)
response = upload(c, alice, chat["id"], oversized, "big.png", "image/png")
check(response["status"] == 413, "oversized upload did not return 413")
# 1b. A decompression bomb passes the byte limit but exceeds the pixel
# cap. It must be rejected from the header before pixel allocation.
bomb = oversized_png(7000, 6000) # 42 Mp > 40 Mp hard cap
check(len(bomb) < MAX_UPLOAD_BYTES, "pixel-bomb fixture unexpectedly exceeds bytes")
response = upload(c, alice, chat["id"], bomb, "bomb.png", "image/png")
check(response["status"] == 413, "pixel-limit upload did not return 413")
# 2. A file with a valid PNG signature but a broken body must be a 415.
corrupt = png[:8] + b"\x00" * 64
response = upload(c, alice, chat["id"], corrupt, "broken.png", "image/png")
check(response["status"] == 415, "corrupted image did not return 415")
# 3. The declared type disagrees with the actual PNG bytes: MIME spoof.
response = upload(c, alice, chat["id"], png, "spoof.jpg", "image/jpeg")
check(response["status"] == 415, "MIME spoof did not return 415")
# 4. A non-image content type is rejected without touching the decoder.
response = upload(c, alice, chat["id"], b"hello", "file.txt", "text/plain")
check(response["status"] == 415, "non-image Content-Type did not return 415")
# 4b. An empty body is rejected before validation and leaves no metadata.
response = upload(c, alice, chat["id"], b"", "empty.png", "image/png")
check(response["status"] == 415, "empty upload did not return 415")
# 5. Path separators in the display name are rejected before I/O.
for bad_name in ("../escape.png", "dir/photo.png", "back\\slash.png"):
response = upload(c, alice, chat["id"], png, bad_name, "image/png")
check(response["status"] == 400, f"filename {bad_name!r} did not return 400")
# 6. Eve is not a chat member: both metadata and bytes are hidden as 404.
for suffix in ("", "/content"):
response = c.call(
"GET", f"/api/v1/attachments/{good['id']}{suffix}", expected=404, actor=eve
)
check(
response["json"]["error"]["code"] == "not_found",
"foreign attachment error code mismatch",
)
# 7. An attachment of another chat cannot be linked to this chat.
other = c.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
other_attachment = c.wait_ready(alice, c.attachment(alice, other)[0])
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
404,
actor=alice,
body={"text": "wrong chat", "attachment_ids": [other_attachment["id"]]},
headers={"Idempotency-Key": "wrong-chat-" + uuid.uuid4().hex},
)
c.call(
"POST",
f"/api/v1/chats/{other['id']}/messages",
404,
actor=alice,
body={"text": "wrong chat", "attachment_ids": [good["id"]]},
headers={"Idempotency-Key": "wrong-chat-" + uuid.uuid4().hex},
)
# 8. Unknown ids, duplicates and an oversized list are cheap 400/404s.
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
404,
actor=alice,
body={"text": "unknown", "attachment_ids": [str(uuid.uuid4())]},
headers={"Idempotency-Key": "unknown-" + uuid.uuid4().hex},
)
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
actor=alice,
body={"text": "duplicate", "attachment_ids": [good["id"], good["id"]]},
headers={"Idempotency-Key": "duplicate-" + uuid.uuid4().hex},
)
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
actor=alice,
body={"text": "many", "attachment_ids": [str(uuid.uuid4())] * 11},
headers={"Idempotency-Key": "many-" + uuid.uuid4().hex},
)
# 9. Uploading into a chat the caller does not belong to is a 404, and the
# response body must not become a 500 even though authorization wins
# before the body is read.
response = upload(c, eve, chat["id"], png, "foreign.png", "image/png")
check(response["status"] == 404, "foreign-chat upload did not return 404")
# 10. A valid image attached to a message is visible to the other member
# with the exact original checksum, independently of the uploader.
message = c.message(alice, chat, "", attachments=[good["id"]])["json"]
check(message["attachment_ids"] == [good["id"]], "message lost attachment id")
download = c.call(
"GET", f"/api/v1/attachments/{good['id']}/content", actor=bob
)
check(download["bytes"] == png, "member download differs from original")
check(
hashlib.sha256(download["bytes"]).hexdigest() == good["sha256"],
"member download checksum mismatch",
)
print(
"PASS: graded negative cases (413 byte/pixel, 415/404/400), no 5xx, "
"cross-member download checksum matches"
)
if __name__ == "__main__":
main()