feat: add image attachments to messages
- upload, metadata and download endpoints for PNG/JPEG attachments:
limits before reading and decompression, type by content,
structural integrity check without decoding pixels, safe filename
- write order: pending row with object key -> PUT to S3 -> ready;
per-step DB sessions so no pool connection is held during transfers
- attachment_ids in messages (validation, idempotency hash, history);
whitespace-only text allowed with an attachment (teacher, 2026-10-20)
- migration: attachments and message_attachments tables, text length
CHECK relaxed with NOT VALID
- student tests, fault checks and driver commands for attachments;
rollout profile starts from e1161f7 and checks the commit first
- sync attachments deltas into main specs; rules, AGENTS.md, PLAN.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ from typing import Any
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
import images
|
||||
from api_client import (
|
||||
API,
|
||||
Actor,
|
||||
@@ -19,6 +20,7 @@ from api_client import (
|
||||
unique_username,
|
||||
)
|
||||
from contract import (
|
||||
assert_content,
|
||||
assert_created_at,
|
||||
assert_error,
|
||||
assert_errors,
|
||||
@@ -389,3 +391,70 @@ def test_upper_case_uuid_in_path(
|
||||
"MessagePage",
|
||||
)
|
||||
assert upper_page == lower_page == {"items": [sent], "next_cursor": None}
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Лимит и тип вложения")
|
||||
def test_attachment_limit_and_type_codes(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
over = images.png_of_size(images.MAX_BYTES + 1)
|
||||
assert_error(api.post_attachment(alice, chat["id"], over), 413)
|
||||
text = "не картинка".encode()
|
||||
assert_error(api.post_attachment(alice, chat["id"], text), 415)
|
||||
|
||||
|
||||
def attachment_requests(
|
||||
api: Api,
|
||||
actor: Actor,
|
||||
attachment_id: str,
|
||||
) -> dict[str, httpx.Response]:
|
||||
quoted = urllib.parse.quote(attachment_id, safe="")
|
||||
return {
|
||||
"meta": api.attachment_meta(actor, quoted),
|
||||
"content": api.attachment_content(actor, quoted),
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Идентификаторы вложений в другой записи")
|
||||
def test_attachment_ids_in_other_forms(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment = api.upload(alice, chat["id"], images.sample_png())
|
||||
responses = {
|
||||
f"{name} / {form}": resp
|
||||
for form, value in other_uuid_forms(attachment["id"]).items()
|
||||
for name, resp in attachment_requests(api, alice, value).items()
|
||||
}
|
||||
responses |= {
|
||||
f"attachment_ids / {form}": api.post_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
json={"text": "x", "attachment_ids": [value]},
|
||||
)
|
||||
for form, value in other_uuid_forms(attachment["id"]).items()
|
||||
}
|
||||
assert_errors(responses, 400)
|
||||
assert api.all_messages(alice, chat["id"]) == []
|
||||
|
||||
|
||||
@pytest.mark.spec(
|
||||
"api-conventions",
|
||||
"Идентификаторы вложений в верхнем регистре",
|
||||
)
|
||||
def test_attachment_ids_in_upper_case(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
data = images.sample_png()
|
||||
attachment = api.upload(alice, chat["id"], data)
|
||||
upper = attachment["id"].upper()
|
||||
meta = assert_ok(api.attachment_meta(alice, upper), 200, "Attachment")
|
||||
assert meta == attachment
|
||||
assert_content(api.attachment_content(alice, upper), data, "image/png")
|
||||
message = api.send_message(alice, chat["id"], "x", attachment_ids=[upper])
|
||||
assert message["attachment_ids"] == [attachment["id"]]
|
||||
|
||||
Reference in New Issue
Block a user