тесты lab4 переведены на 202 + worker; unit-тесты pipeline; кейсы рецензий как 415

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

PNG с фильтром строки 5/0xff, IDAT не zlib, JPEG из 27 байт, SOS с компонентом не из SOF:
415 при загрузке. 40 Мпикс проходит до ready в лимите памяти worker.
This commit is contained in:
Alexey Bodanyuk
2026-10-31 16:12:52 +03:00
parent bc32a50967
commit aef6eb1d4c
8 changed files with 449 additions and 91 deletions
+35 -12
View File
@@ -1,10 +1,14 @@
"""Attachments through the load balancer (lab 4, levels 3 and 4): upload/download, ACL, limits,
content checks, filename rules and messages with attachment_ids.
"""Attachments through the load balancer (lab 4, levels 3 and 4; lab 5: upload is 202 queued and
the worker publishes the original + variants): upload/download, ACL, limits, content checks,
filename rules and messages with attachment_ids.
Negative inputs include every case the reviewer used against other lab 4 solutions: corrupted
IDAT with valid CRCs, the first 33 bytes of the fixture, JPEG without scan data, a 20000x10000
header (DecompressionBomb), exactly 10 MiB through the LB, C1 control U+0085 and a whitespace
filename, text=null / missing text / attachment_ids=null in a message."""
filename, text=null / missing text / attachment_ids=null in a message; and the lab 4/5 review
cases: PNG row filter 5 and 0xff, IDAT that is not a zlib stream, the 27-byte JPEG without tables
and scan data, a JPEG whose SOS names a component missing from SOF - all 415 AT UPLOAD, never
202 followed by failed/invalid_image."""
from __future__ import annotations
import hashlib
@@ -42,12 +46,14 @@ def test_png_and_jpeg_roundtrip_byte_for_byte(api, trio):
jpg = imagegen.jpeg(321, 123, noise=True)
for data, ctype in ((SAMPLE, "image/png"), (jpg, "image/jpeg")):
att = api.upload(alice, chat, data, content_type=ctype, filename="кот 😺.bin").json()
assert att["status"] == "ready" and att["variants"] == [] and att["error_code"] is None
assert att["status"] == "queued" and att["variants"] == [] and att["error_code"] is None
assert att["chat_id"] == chat["id"] and att["owner_id"] == alice["id"]
assert att["size_bytes"] == len(data) and att["sha256"] == hashlib.sha256(data).hexdigest()
assert att["content_type"] == ctype and att["filename"] == "кот 😺.bin"
meta = api.call("GET", f"/api/v1/attachments/{att['id']}", 200, token=bob["token"], schema="Attachment").json()
assert meta == att
meta = api.wait_status(bob, att["id"], "ready")
assert {k: v for k, v in meta.items() if k not in ("status", "variants", "updated_at")} == \
{k: v for k, v in att.items() if k not in ("status", "variants", "updated_at")}
assert [v["name"] for v in meta["variants"]] == ["thumbnail", "crop", "optimized"]
got = content(api, bob, att["id"])
assert got.content == data and got.headers["content-type"] == ctype
assert got.headers["content-length"] == str(len(data))
@@ -59,13 +65,16 @@ def test_png_and_jpeg_roundtrip_byte_for_byte(api, trio):
def test_metadata_reveals_no_storage_details(api, trio):
alice, _, _, chat, _ = trio
att = api.upload(alice, chat, SAMPLE).json()
api.wait_status(alice, att["id"], "ready")
row = attachment_row(att["id"])
assert row["status"] == "ready" and row["object_key"].startswith("o/")
assert row["status"] == "ready" and row["object_key"].startswith(f"a/{att['id']}/")
assert row["staging_key"].startswith("s/")
text = api.call("GET", f"/api/v1/attachments/{att['id']}", 200, token=alice["token"]).text
assert row["object_key"] not in text and "messenger-attachments" not in text and "s3" not in text.lower()
# The key is random: neither the id nor the filename is part of it.
assert att["id"].replace("-", "") not in row["object_key"] and "photo" not in row["object_key"]
assert s3_size(row["object_key"]) == len(SAMPLE) # the bytes ARE in the bucket
for secret in (row["object_key"], row["staging_key"], "messenger-attachments", "messenger-staging", "a/", "s3"):
assert secret not in text.lower(), secret
# The staging key is random: neither the id nor the filename is part of it.
assert att["id"].replace("-", "") not in row["staging_key"] and "photo" not in row["staging_key"]
assert s3_size(row["object_key"]) == len(SAMPLE) # the published original IS in the final bucket
def test_original_is_not_stored_on_api_containers(api, trio):
@@ -174,6 +183,7 @@ def test_size_limit_through_the_load_balancer(api, trio):
exact = imagegen.png_exactly(10 * MiB)
assert len(exact) == 10 * MiB
att = api.upload(alice, chat, exact).json() # exactly the limit: accepted via LB
api.wait_status(bob, att["id"], "ready")
assert content(api, bob, att["id"]).content == exact
started = time.monotonic()
api.upload(alice, chat, exact + b"\x00", 413) # Content-Length over the limit
@@ -201,6 +211,10 @@ def test_exactly_40_megapixels_is_accepted(api, trio):
data = imagegen.png(8000, 5000, compress_level=9) # 40 000 000 px, ~120 KB file
att = api.upload(alice, chat, data).json()
assert att["size_bytes"] == len(data)
# Lab 5: the worker processes the biggest allowed picture within its memory limit.
ready = api.wait_status(alice, att["id"], "ready", timeout=120)
assert {v["name"]: (v["width"], v["height"]) for v in ready["variants"]} == \
{"thumbnail": (256, 160), "crop": (128, 128), "optimized": (1280, 800)}
# ------------------------------------------------------------------ content checks
@@ -213,6 +227,11 @@ BROKEN = {
"png: bytes after IEND": (SAMPLE + b"<?php echo 1; ?>", "image/png"),
"jpeg: no scan data after SOS": (imagegen.jpeg_without_scan_data(), "image/jpeg"),
"jpeg: truncated": (imagegen.jpeg(400, 300, noise=True)[:5000], "image/jpeg"),
"review: png row filter 5": (imagegen.png_bad_filter(5), "image/png"),
"review: png row filter 0xff": (imagegen.png_bad_filter(0xFF), "image/png"),
"review: png IDAT not-a-zlib-stream": (imagegen.png_idat_not_zlib(), "image/png"),
"review: jpeg 27 bytes, no tables/scan": (imagegen.JPEG_27_BYTES, "image/jpeg"),
"review: jpeg SOS component not in SOF": (imagegen.jpeg_sos_unknown_component(), "image/jpeg"),
"mime: png bytes declared jpeg": (SAMPLE, "image/jpeg"),
"mime: jpeg bytes declared png": (imagegen.jpeg(), "image/png"),
"mime: gif declared png": (b"GIF89a" + bytes(100), "image/png"),
@@ -253,6 +272,7 @@ def test_unicode_filename_is_kept_and_safely_exposed(api, trio):
name = 'отчёт "Q3"; ok.png'
att = api.upload(alice, chat, SAMPLE, filename=name).json()
assert att["filename"] == name
api.wait_status(alice, att["id"], "ready")
disposition = content(api, alice, att["id"]).headers["content-disposition"]
assert '"' not in disposition.split('filename="')[1].split('";')[0] # quotes neutralised in the fallback
assert "filename*=UTF-8''" in disposition
@@ -262,7 +282,10 @@ def test_unicode_filename_is_kept_and_safely_exposed(api, trio):
def test_variant_parameter(api, trio):
alice, _, eve, chat, _ = trio
att = api.upload(alice, chat, SAMPLE).json()
api.wait_status(alice, att["id"], "ready")
assert content(api, alice, att["id"], query="?variant=original").content == SAMPLE
content(api, alice, att["id"], 404, query="?variant=thumbnail") # schema name, not produced in lab 4
assert content(api, alice, att["id"], query="?variant=thumbnail").headers["content-type"] == "image/jpeg"
content(api, alice, att["id"], 400, query="?variant=huge")
content(api, alice, att["id"], 400, query="?variant=thumbnail&variant=crop")
content(api, eve, att["id"], 404, query="?variant=thumbnail") # outsider: 404, nothing revealed
content(api, eve, att["id"], 404, query="?variant=huge") # outsider first, then the value