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>
70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
import json
|
|
import re
|
|
from datetime import UTC, datetime
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from httpx import Response
|
|
|
|
from conftest import bearer, login, new_user, running_app
|
|
from messenger.shared.infrastructure.clock import MonotonicUtcClock
|
|
|
|
RFC3339_UTC = re.compile(r"\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,6})?Z\Z")
|
|
MOMENTS = [
|
|
datetime(2026, 10, 12, 8, 49, 2, tzinfo=UTC),
|
|
datetime(2026, 10, 12, 8, 49, 2, 120, tzinfo=UTC),
|
|
]
|
|
|
|
|
|
def assert_wire(response: Response, expected: dict[str, Any]) -> None:
|
|
assert (
|
|
response.content == json.dumps(expected, ensure_ascii=False, separators=(",", ":")).encode()
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("moment", MOMENTS, ids=["whole-second", "microseconds"])
|
|
async def test_created_at_is_rfc3339_utc(monkeypatch: pytest.MonkeyPatch, moment: datetime) -> None:
|
|
monkeypatch.setattr(MonotonicUtcClock, "now", lambda _self: moment)
|
|
stamp = moment.isoformat().replace("+00:00", "Z")
|
|
assert RFC3339_UTC.match(stamp)
|
|
async with running_app() as http:
|
|
body = new_user("wire")
|
|
response = await http.post("/api/v1/users", json=body)
|
|
user_id = response.json()["id"]
|
|
user = {
|
|
"id": user_id,
|
|
"username": body["username"],
|
|
"display_name": body["display_name"],
|
|
"created_at": stamp,
|
|
}
|
|
assert_wire(response, user)
|
|
session = await login(http, body["username"])
|
|
headers = bearer(session["token"])
|
|
assert session["user"] == user
|
|
assert_wire(await http.get("/api/v1/users/me", headers=headers), user)
|
|
|
|
response = await http.post(
|
|
"/api/v1/chats", json={"title": "Чат", "member_ids": [user_id]}, headers=headers
|
|
)
|
|
chat_id = response.json()["id"]
|
|
chat = {"id": chat_id, "title": "Чат", "member_ids": [user_id], "created_at": stamp}
|
|
assert_wire(response, chat)
|
|
assert_wire(await http.get(f"/api/v1/chats/{chat_id}", headers=headers), chat)
|
|
listed = await http.get("/api/v1/chats", headers=headers)
|
|
assert_wire(listed, {"items": [chat], "next_cursor": None})
|
|
|
|
path = f"/api/v1/chats/{chat_id}/messages"
|
|
sent = {**headers, "Idempotency-Key": "wire-1"}
|
|
response = await http.post(path, json={"text": "привет"}, headers=sent)
|
|
message = {
|
|
"id": response.json()["id"],
|
|
"chat_id": chat_id,
|
|
"sender_id": user_id,
|
|
"text": "привет",
|
|
"created_at": stamp,
|
|
"attachment_ids": [],
|
|
}
|
|
assert_wire(response, message)
|
|
history = await http.get(path, headers=headers)
|
|
assert_wire(history, {"items": [message], "next_cursor": None})
|