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:
@@ -11,6 +11,7 @@ from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
import images
|
||||
from api_client import (
|
||||
API,
|
||||
IDEMPOTENCY_KEY,
|
||||
@@ -148,6 +149,95 @@ def test_other_body_conflict(
|
||||
assert api.send_message(alice, chat["id"], "первое", key=key) == first
|
||||
|
||||
|
||||
def two_attachments(api: Api, actor: Actor, chat_id: str) -> list[str]:
|
||||
return [
|
||||
api.upload(actor, chat_id, images.sample_png())["id"] for _ in range(2)
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.spec("idempotency", "Вложения по умолчанию")
|
||||
def test_replay_with_default_attachments(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
key = new_key()
|
||||
first = api.send_message(alice, chat["id"], "без файлов", key=key)
|
||||
again = api.send_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
"без файлов",
|
||||
key=key,
|
||||
attachment_ids=[],
|
||||
)
|
||||
assert again == first
|
||||
assert texts(api, alice, chat["id"]) == ["без файлов"]
|
||||
|
||||
|
||||
@pytest.mark.spec("idempotency", "Повтор сообщения с вложениями")
|
||||
def test_replay_with_attachments(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
first_id, second_id = two_attachments(api, alice, chat["id"])
|
||||
key = new_key()
|
||||
ids = [first_id, second_id]
|
||||
first = api.send_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
"файлы",
|
||||
key=key,
|
||||
attachment_ids=ids,
|
||||
)
|
||||
again = api.send_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
"файлы",
|
||||
key=key,
|
||||
attachment_ids=ids,
|
||||
)
|
||||
upper = api.send_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
"файлы",
|
||||
key=key,
|
||||
attachment_ids=[item.upper() for item in ids],
|
||||
)
|
||||
assert first == again == upper
|
||||
assert first["attachment_ids"] == ids
|
||||
history = api.all_messages(alice, chat["id"])
|
||||
assert [item["id"] for item in history] == [first["id"]]
|
||||
|
||||
|
||||
@pytest.mark.spec("idempotency", "Другие вложения")
|
||||
def test_other_attachments_conflict(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
first_id, second_id = two_attachments(api, alice, chat["id"])
|
||||
key = new_key()
|
||||
first = api.send_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
"файлы",
|
||||
key=key,
|
||||
attachment_ids=[first_id, second_id],
|
||||
)
|
||||
for body in (
|
||||
{"text": "файлы", "attachment_ids": [second_id, first_id]},
|
||||
{"text": "файлы"},
|
||||
):
|
||||
assert_error(
|
||||
api.post_message(alice, chat["id"], key=key, json=body),
|
||||
409,
|
||||
)
|
||||
history = api.all_messages(alice, chat["id"])
|
||||
assert history == [first]
|
||||
assert history[0]["attachment_ids"] == [first_id, second_id]
|
||||
|
||||
|
||||
@pytest.mark.spec("idempotency", "Другой пользователь")
|
||||
def test_same_key_other_user(
|
||||
api: Api,
|
||||
|
||||
Reference in New Issue
Block a user