Files
samsik 11c8c03984 Собрать стенд lab4 с вложениями в приватном S3-bucket.
Метаданные остаются в PostgreSQL, байты — в RustFS, чтобы скачивание пережило остановку одной реплики API.
2026-11-06 08:49:44 +03:00

63 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Негативные проверки вложений для уровня 3. Токены не печатает."""
import hashlib
import struct
import sys
import uuid
import zlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, arguments, check, run # noqa: E402
def png(width: int, height: int) -> bytes:
signature = b"\x89PNG\r\n\x1a\n"
ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
chunk = b"IHDR" + ihdr
crc = zlib.crc32(chunk) & 0xFFFFFFFF
return signature + struct.pack(">I", len(ihdr)) + chunk + struct.pack(">I", crc)
def main():
args = arguments("Lab4 grade-3 attachment checks")
base = Client(args.base_url, args.ca_file)
alice, bob, eve, chat = base.bootstrap()
other = base.call(
"POST", "/api/v1/chats", 201, actor=alice,
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
sample = (ROOT / "tests/fixtures/sample.png").read_bytes()
att, content = base.attachment(alice, chat)
linked = base.message(alice, chat, "", attachments=[att["id"]])["json"]
check(linked["attachment_ids"] == [att["id"]], "message lost attachment")
downloaded = base.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=bob)
check(downloaded["bytes"] == content, "bob download differs")
check(hashlib.sha256(downloaded["bytes"]).hexdigest() == att["sha256"], "checksum differs")
for suffix in ("", "/content"):
base.call("GET", "/api/v1/attachments/" + att["id"] + suffix, 404, actor=eve)
base.call(
"POST", "/api/v1/chats/" + other["id"] + "/messages", 404, actor=alice,
body={"text": "", "attachment_ids": [att["id"]]},
headers={"Idempotency-Key": uuid.uuid4().hex},
)
base.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=a/b.png", 400,
actor=alice, raw=sample, headers={"Content-Type": "image/png"},
)
base.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=photo.png", 415,
actor=alice, raw=sample, headers={"Content-Type": "image/jpeg"},
)
base.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=huge.png", 413,
actor=alice, raw=png(10000, 5000), headers={"Content-Type": "image/png"},
)
print("PASS: filename, MIME, pixels, outsider, foreign chat")
if __name__ == "__main__":
run(main)