Files
mtusi2026lab2/student-tests/api/test_attachments.py
T
Artyom 12db77133f Forward the lab 5 foundation while preserving lab 6 telemetry
Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files.

Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
2026-11-06 12:29:35 +03:00

339 lines
13 KiB
Python

from collections.abc import AsyncIterator
from hashlib import sha256
from io import BytesIO
from typing import Any
from urllib.parse import quote
from uuid import UUID, uuid4
import pytest
from httpx import AsyncClient, Response
from PIL import Image
from conftest import actor, make_chat, register, resolve, running_app, scalar
from fakes import MemoryObjectStorage
from messenger.attachments.application.ports import ObjectStorage
from pictures import GIF, JPEG, SAMPLE, WEBP, png
from pipeline import settle
LIMIT = 10_485_760
PNG = {"Content-Type": "image/png"}
async def upload(
http: AsyncClient,
user: dict[str, Any],
chat: dict[str, Any],
content: bytes | AsyncIterator[bytes] = SAMPLE,
*,
filename: str | None = "sample.png",
headers: dict[str, str] | None = None,
) -> Response:
query = "" if filename is None else f"?filename={quote(filename, safe='')}"
return await http.post(
f"/api/v1/chats/{chat['id']}/attachments{query}",
content=content,
headers={**actor(user), **(PNG if headers is None else headers)},
)
async def storage(http: AsyncClient) -> MemoryObjectStorage:
found = await resolve(http, ObjectStorage)
assert isinstance(found, MemoryObjectStorage)
return found
async def trio(http: AsyncClient) -> tuple[dict[str, Any], ...]:
alice, bob, eve = [await register(http, label) for label in ("alice", "bob", "eve")]
return alice, bob, eve, await make_chat(http, alice, bob)
async def test_member_uploads_and_another_member_downloads(client: AsyncClient) -> None:
alice, bob, eve, chat = await trio(client)
response = await upload(client, alice, chat)
assert response.status_code == 202, response.text
attachment = response.json()
assert set(attachment) == {
"id",
"chat_id",
"owner_id",
"filename",
"content_type",
"size_bytes",
"sha256",
"status",
"variants",
"error_code",
"created_at",
"updated_at",
}
assert attachment["chat_id"] == chat["id"]
assert attachment["owner_id"] == alice["id"]
assert attachment["filename"] == "sample.png"
assert attachment["content_type"] == "image/png"
assert attachment["size_bytes"] == len(SAMPLE) == 111817
assert attachment["sha256"] == sha256(SAMPLE).hexdigest()
assert (attachment["status"], attachment["variants"], attachment["error_code"]) == (
"queued",
[],
None,
)
assert attachment["created_at"].endswith("Z")
path = f"/api/v1/attachments/{attachment['id']}"
assert (await client.get(path, headers=actor(bob))).json() == attachment
await settle(client)
ready = (await client.get(path, headers=actor(bob))).json()
assert (ready["status"], ready["error_code"], ready["sha256"]) == (
"ready",
None,
attachment["sha256"],
)
assert [variant["name"] for variant in ready["variants"]] == ["crop", "optimized", "thumbnail"]
content = await client.get(
f"{path}/content", headers={**actor(bob), "Accept": "application/json"}
)
assert content.status_code == 200
assert content.content == SAMPLE
assert content.headers["content-type"] == "image/png"
assert content.headers["content-length"] == str(len(SAMPLE))
assert content.headers["x-content-type-options"] == "nosniff"
assert content.headers["x-request-id"]
assert content.headers["x-instance-id"]
for suffix in ("", "/content"):
stranger = await client.get(f"{path}{suffix}", headers=actor(eve))
assert stranger.status_code == 404
assert stranger.json()["error"]["code"] == "not_found"
async def test_object_keys_are_assigned_by_server(client: AsyncClient) -> None:
alice, _, _, chat = await trio(client)
attachment = (await upload(client, alice, chat, filename="a.png")).json()
staging = await scalar("SELECT staging_key FROM attachments WHERE id = $1", attachment["id"])
assert staging == f"staging/{attachment['id']}"
assert set((await storage(client)).objects) >= {staging}
await settle(client)
original = await scalar("SELECT original_key FROM attachments WHERE id = $1", attachment["id"])
assert original == f"originals/{attachment['id']}"
assert (await storage(client)).objects[original][0] == SAMPLE
async def test_jpeg_is_accepted_by_content(client: AsyncClient) -> None:
alice, bob, _, chat = await trio(client)
response = await upload(client, alice, chat, JPEG, headers={"Content-Type": "IMAGE/JPEG"})
assert response.status_code == 202, response.text
assert response.json()["content_type"] == "image/jpeg"
await settle(client)
download = await client.get(
f"/api/v1/attachments/{response.json()['id']}/content", headers=actor(bob)
)
assert (download.content, download.headers["content-type"]) == (JPEG, "image/jpeg")
async def test_access_is_checked_before_content(client: AsyncClient) -> None:
alice, _, eve, chat = await trio(client)
assert (await upload(client, eve, chat, b"not an image", filename="../x")).status_code == 404
missing = {"id": str(uuid4())}
assert (await upload(client, alice, missing)).status_code == 404
anonymous = await client.post(
f"/api/v1/chats/{chat['id']}/attachments?filename=a.png", content=SAMPLE, headers=PNG
)
assert anonymous.status_code == 401
assert anonymous.headers["www-authenticate"] == 'Bearer realm="messenger"'
async def test_token_is_checked_before_the_declared_size() -> None:
async with running_app(max_upload_bytes=4096) as http:
alice, _, _, chat = await trio(http)
oversized = png(16, 16, size=8192)
anonymous = await http.post(
f"/api/v1/chats/{chat['id']}/attachments?filename=a.png", content=oversized, headers=PNG
)
assert anonymous.status_code == 401
assert anonymous.headers["www-authenticate"] == 'Bearer realm="messenger"'
assert (await upload(http, alice, chat, oversized)).status_code == 413
async def test_exact_limit_is_accepted(client: AsyncClient) -> None:
alice, _, _, chat = await trio(client)
response = await upload(client, alice, chat, png(16, 16, size=LIMIT))
assert response.status_code == 202, response.text
assert response.json()["size_bytes"] == LIMIT
async def test_one_byte_over_with_content_length(client: AsyncClient) -> None:
alice, _, _, chat = await trio(client)
response = await upload(client, alice, chat, png(16, 16, size=LIMIT + 1))
assert response.status_code == 413
assert response.json()["error"]["code"] == "payload_too_large"
async def test_one_byte_over_chunked(client: AsyncClient) -> None:
alice, _, _, chat = await trio(client)
body = png(16, 16, size=LIMIT + 1)
async def chunked() -> AsyncIterator[bytes]:
for start in range(0, len(body), 1 << 20):
yield body[start : start + (1 << 20)]
before = await scalar("SELECT count(*) FROM attachments")
response = await upload(client, alice, chat, chunked())
assert response.status_code == 413
assert await scalar("SELECT count(*) FROM attachments") == before
@pytest.mark.parametrize(
("width", "height", "status"),
[(8000, 5000, 202), (8001, 5000, 413), (100_000, 100_000, 413)],
)
async def test_pixel_limit_is_read_from_header(
client: AsyncClient, width: int, height: int, status: int
) -> None:
alice, _, _, chat = await trio(client)
body = png(width, height, rows=width * height <= 40_000_000)
assert len(body) < 64_000
response = await upload(client, alice, chat, body)
assert response.status_code == status, response.text
@pytest.mark.parametrize(
("content", "header"),
[
(JPEG, "image/png"),
(GIF, "image/png"),
(WEBP, "image/png"),
(b"plain text, not an image", "image/png"),
(SAMPLE, "image/jpeg"),
(SAMPLE[: len(SAMPLE) // 2], "image/png"),
(JPEG[: len(JPEG) // 2], "image/jpeg"),
(b"", "image/png"),
(SAMPLE, "application/octet-stream"),
(SAMPLE, "image/gif"),
],
ids=[
"jpeg-as-png",
"gif-as-png",
"webp-as-png",
"text-as-png",
"png-as-jpeg",
"truncated-png",
"truncated-jpeg",
"empty",
"octet-stream",
"gif-type",
],
)
async def test_type_is_checked_by_content(client: AsyncClient, content: bytes, header: str) -> None:
alice, _, _, chat = await trio(client)
response = await upload(client, alice, chat, content, headers={"Content-Type": header})
assert response.status_code == 415, response.text
assert response.json()["error"]["code"] == "unsupported_media_type"
async def test_missing_content_type_is_unsupported(client: AsyncClient) -> None:
alice, _, _, chat = await trio(client)
response = await upload(client, alice, chat, headers={})
assert response.status_code == 415
@pytest.mark.parametrize(
"filename",
[
*("../a.png", "a/b.png", "a\\b.png", "a\x00.png", "a\x01.png", "a\x7f.png"),
*("", " ", "x" * 256, None),
],
)
async def test_dangerous_filename_is_rejected(client: AsyncClient, filename: str | None) -> None:
alice, _, _, chat = await trio(client)
response = await upload(client, alice, chat, filename=filename)
assert response.status_code == 400, response.text
assert response.json()["error"]["code"] == "invalid_request"
@pytest.mark.parametrize("filename", ["фото 1.png", "я" * 255, "%41.png"])
async def test_display_name_is_kept(client: AsyncClient, filename: str) -> None:
alice, _, _, chat = await trio(client)
response = await upload(client, alice, chat, filename=filename)
assert response.status_code == 202, response.text
assert response.json()["filename"] == filename
async def test_content_waits_for_ready_and_serves_every_variant(client: AsyncClient) -> None:
alice, bob, _, chat = await trio(client)
attachment = (await upload(client, alice, chat)).json()
path = f"/api/v1/attachments/{attachment['id']}/content"
for variant in ("original", "thumbnail", "crop", "optimized"):
early = await client.get(f"{path}?variant={variant}", headers=actor(bob))
assert early.status_code == 409, early.text
assert early.json()["error"]["code"] == "conflict"
await settle(client)
assert (await client.get(f"{path}?variant=original", headers=actor(alice))).status_code == 200
meta = (await client.get(path.removesuffix("/content"), headers=actor(bob))).json()
expected = {
"crop": ("image/jpeg", "JPEG", (128, 128)),
"optimized": ("image/webp", "WEBP", (320, 200)),
"thumbnail": ("image/jpeg", "JPEG", (256, 160)),
}
for variant in meta["variants"]:
served = await client.get(f"{path}?variant={variant['name']}", headers=actor(bob))
content_type, kind, size = expected[variant["name"]]
assert (served.headers["content-type"], variant["content_type"]) == (content_type,) * 2
assert len(served.content) == variant["size_bytes"]
assert sha256(served.content).hexdigest() == variant["sha256"]
image = Image.open(BytesIO(served.content))
assert (image.format, image.size, (variant["width"], variant["height"])) == (
kind,
size,
size,
)
await scalar(
"DELETE FROM attachment_variants WHERE attachment_id = $1 AND name = 'crop'",
UUID(attachment["id"]),
)
missing = await client.get(f"{path}?variant=crop", headers=actor(alice))
assert missing.status_code == 404
assert (await client.get(f"{path}?variant=preview", headers=actor(alice))).status_code == 400
assert (
await client.get("/api/v1/attachments/x/content", headers=actor(alice))
).status_code == 400
unknown = await client.get(f"/api/v1/attachments/{uuid4()}", headers=actor(alice))
assert unknown.status_code == 404
async def test_storage_failure_leaves_nothing_visible(client: AsyncClient) -> None:
alice, _, _, chat = await trio(client)
ready = await upload(client, alice, chat)
await settle(client)
store = await storage(client)
store.failing = True
try:
failed = await upload(client, alice, chat, filename="failed.png")
download = await client.get(
f"/api/v1/attachments/{ready.json()['id']}/content", headers=actor(alice)
)
metadata = await client.get(
f"/api/v1/attachments/{ready.json()['id']}", headers=actor(alice)
)
finally:
store.failing = False
assert failed.status_code == 503
assert failed.json()["error"]["code"] == "unavailable"
assert download.status_code == 503
assert metadata.status_code == 200
status = await scalar(
"SELECT string_agg(status, ',') FROM attachments WHERE chat_id = $1 AND filename = $2",
chat["id"],
"failed.png",
)
assert status == "pending"
async def test_lost_object_is_not_found(client: AsyncClient) -> None:
alice, _, _, chat = await trio(client)
attachment = (await upload(client, alice, chat)).json()
await settle(client)
(await storage(client)).objects.clear()
response = await client.get(
f"/api/v1/attachments/{attachment['id']}/content", headers=actor(alice)
)
assert response.status_code == 404
assert response.json()["error"]["code"] == "not_found"