forked from mtusi2026/mtusi2026lab5
112 lines
4.3 KiB
Python
112 lines
4.3 KiB
Python
"""Сквозные свойства pipeline v1 без остановки сервисов"""
|
|
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import struct
|
|
import zlib
|
|
|
|
from helpers import assert_error, auth, png_bytes, psql, upload_raw, wait_attachment
|
|
from PIL import Image
|
|
|
|
|
|
def test_variants_have_real_dimensions_checksums_and_no_metadata(
|
|
api, make_user, make_chat, upload
|
|
):
|
|
alice = make_user("pipeline")
|
|
chat = make_chat(alice, [alice])
|
|
image = Image.new("RGBA", (320, 200), (255, 0, 0, 0))
|
|
source = io.BytesIO()
|
|
image.save(source, "PNG")
|
|
ready = upload(alice, chat, source.getvalue())
|
|
expected = {
|
|
"thumbnail": ((256, 160), "JPEG"),
|
|
"crop": ((128, 128), "JPEG"),
|
|
"optimized": ((320, 200), "WEBP"),
|
|
}
|
|
for variant in ready["variants"]:
|
|
response = api.get(
|
|
f"/api/v1/attachments/{ready['id']}/content",
|
|
params={"variant": variant["name"]},
|
|
headers=auth(alice),
|
|
)
|
|
assert response.status_code == 200
|
|
assert len(response.content) == variant["size_bytes"]
|
|
assert hashlib.sha256(response.content).hexdigest() == variant["sha256"]
|
|
assert response.headers["content-type"] == variant["content_type"]
|
|
with Image.open(io.BytesIO(response.content)) as actual:
|
|
assert (actual.size, actual.format) == expected[variant["name"]]
|
|
assert actual.size == (variant["width"], variant["height"])
|
|
assert not actual.getexif() and not actual.info.get("icc_profile")
|
|
assert min(actual.convert("RGB").getpixel((0, 0))) >= 250 # alpha → белый
|
|
|
|
|
|
def test_orientation_and_no_upscale(api, make_user, make_chat, upload):
|
|
alice = make_user("orientation")
|
|
chat = make_chat(alice, [alice])
|
|
source = io.BytesIO()
|
|
image = Image.new("RGB", (12, 24), "red")
|
|
exif = Image.Exif()
|
|
exif[274] = 6
|
|
exif[315] = "must be stripped"
|
|
image.save(source, "JPEG", exif=exif)
|
|
ready = upload(alice, chat, source.getvalue(), "image/jpeg")
|
|
by_name = {v["name"]: v for v in ready["variants"]}
|
|
for name in ("thumbnail", "optimized"):
|
|
assert (by_name[name]["width"], by_name[name]["height"]) == (24, 12)
|
|
response = api.get(
|
|
f"/api/v1/attachments/{ready['id']}/content?variant={name}",
|
|
headers=auth(alice),
|
|
)
|
|
with Image.open(io.BytesIO(response.content)) as actual:
|
|
assert not actual.getexif()
|
|
assert (by_name["crop"]["width"], by_name["crop"]["height"]) == (128, 128)
|
|
original = api.get(
|
|
f"/api/v1/attachments/{ready['id']}/content", headers=auth(alice)
|
|
)
|
|
assert original.content == source.getvalue()
|
|
|
|
|
|
def broken_idat() -> bytes:
|
|
"""Корректная структура/CRC, но недекодируемый zlib: обнаруживает именно worker"""
|
|
data = png_bytes(32, 32)
|
|
result = bytearray(data[:8])
|
|
offset = 8
|
|
while offset < len(data):
|
|
size = struct.unpack(">I", data[offset : offset + 4])[0]
|
|
kind = data[offset + 4 : offset + 8]
|
|
body = data[offset + 8 : offset + 8 + size]
|
|
if kind == b"IDAT":
|
|
body = b"invalid zlib"
|
|
result += (
|
|
struct.pack(">I", len(body))
|
|
+ kind
|
|
+ body
|
|
+ struct.pack(">I", zlib.crc32(kind + body))
|
|
)
|
|
offset += size + 12
|
|
return bytes(result)
|
|
|
|
|
|
def test_permanent_failure_is_observable_and_not_linkable(
|
|
api, make_user, make_chat, send_attachments
|
|
):
|
|
alice = make_user("invalid")
|
|
chat = make_chat(alice, [alice])
|
|
response = upload_raw(api, alice, chat["id"], broken_idat())
|
|
assert response.status_code == 202 and response.json()["status"] == "queued"
|
|
attachment = wait_attachment(api, alice, response.json()["id"], terminal="failed")
|
|
assert attachment["error_code"] == "invalid_image" and not attachment["variants"]
|
|
assert_error(
|
|
api.get(f"/api/v1/attachments/{attachment['id']}/content", headers=auth(alice)),
|
|
409,
|
|
"conflict",
|
|
)
|
|
assert_error(send_attachments(alice, chat, [attachment["id"]]), 409, "conflict")
|
|
row = json.loads(
|
|
psql(
|
|
f"SELECT json_build_object('attempt', attempt, 'failures', failures) FROM attachment_jobs WHERE attachment_id = '{attachment['id']}';"
|
|
)
|
|
)
|
|
assert row == {"attempt": 1, "failures": 1}
|