Link messages to attachments of the same chat

A message now carries attachment_ids in the request, in every response,
in the idempotent replay and in history, in the order they were sent.
The chat aggregate checks membership first, then refuses whitespace
text, more than ten or repeated attachments, and an empty text without
attachments, so an outsider still gets 404 before any 400.

The send statement inserts the links next to the message and the
idempotency key, and the foreign key from message_attachments to
attachments of the same chat turns an unknown or foreign attachment
into 404 and rolls the whole send back, without the messaging zone
reading the attachments table. Replay and history read the list with a
subquery on the primary key. The request fingerprint covers the list,
so a missing attachment_ids and [] are the same body while a different
order is a conflict.

The zone dependency test now covers three zones, and the SQL ownership
test checks that every zone touches only its own tables.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-28 20:45:19 +03:00
co-authored by Claude Opus 5.5
parent 00826a40d3
commit f9760fc098
14 changed files with 298 additions and 52 deletions
+45 -3
View File
@@ -1,7 +1,7 @@
from dataclasses import fields
from datetime import UTC, datetime, timedelta
from itertools import pairwise
from uuid import uuid4
from uuid import UUID, uuid4
import pytest
@@ -11,6 +11,7 @@ from messenger.identity.domain.values import DisplayName, Password, Username
from messenger.messaging.domain.errors import (
ChatNotFound,
IdempotencyConflict,
InvalidAttachmentList,
InvalidChatData,
InvalidMessageText,
)
@@ -19,6 +20,7 @@ from messenger.messaging.domain.values import MessageText
from messenger.shared.infrastructure.clock import MonotonicUtcClock
MOMENT = datetime(2026, 1, 1, tzinfo=UTC)
UUID_TWICE = uuid4()
@pytest.mark.parametrize(
@@ -81,10 +83,11 @@ def test_message_text_counts_code_points_not_bytes() -> None:
MessageText("👋" * 4001)
def test_message_text_rejects_blank() -> None:
for raw in ("", " ", "\n\t "):
def test_message_text_rejects_whitespace() -> None:
for raw in (" ", "\n\t "):
with pytest.raises(InvalidMessageText):
MessageText(raw)
assert MessageText("").value == ""
def make_chat(*members: object) -> Chat:
@@ -167,6 +170,45 @@ def test_post_validates_membership_before_text() -> None:
chat.post(message_id=uuid4(), sender_id=uuid4(), text="", now=MOMENT)
def test_empty_text_needs_an_attachment() -> None:
chat = make_chat(1)
sender = chat.member_ids[0]
with pytest.raises(InvalidMessageText):
chat.post(message_id=uuid4(), sender_id=sender, text="", now=MOMENT)
ids = (uuid4(), uuid4())
message = chat.post(
message_id=uuid4(), sender_id=sender, text="", now=MOMENT, attachment_ids=ids
)
assert message.attachment_ids == ids
with pytest.raises(InvalidMessageText):
chat.post(message_id=uuid4(), sender_id=sender, text=" ", now=MOMENT, attachment_ids=ids)
@pytest.mark.parametrize("ids", [[uuid4() for _ in range(11)], [UUID_TWICE, UUID_TWICE]])
def test_attachment_list_is_bounded_and_unique(ids: list[UUID]) -> None:
chat = make_chat(1)
with pytest.raises(InvalidAttachmentList):
chat.post(
message_id=uuid4(),
sender_id=chat.member_ids[0],
text="x",
now=MOMENT,
attachment_ids=ids,
)
def test_membership_is_checked_before_attachments() -> None:
chat = make_chat(1)
with pytest.raises(ChatNotFound):
chat.post(
message_id=uuid4(),
sender_id=uuid4(),
text="",
now=MOMENT,
attachment_ids=[uuid4() for _ in range(11)],
)
def test_receipt_replays_only_the_same_request() -> None:
chat = make_chat(1)
message = chat.post(message_id=uuid4(), sender_id=chat.member_ids[0], text="раз", now=MOMENT)