Refuse failed attachments in messages through a zone port
The messaging zone asks a new AttachmentStates port whether any listed attachment of the chat has failed and answers 409 conflict before the message is stored; queued and processing attachments can still be sent right after the upload. The check runs only on the first execution of a key, so a replay returns the original 201 even if the attachment failed later. main.py wires the port to the attachments directory, so neither zone reads the other's tables. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b152b6e07e
commit
34741dc2d2
@@ -4,8 +4,11 @@ from uuid import uuid4
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
from conftest import actor, keyed, make_chat, register
|
||||
from conftest import actor, keyed, make_chat, register, resolve
|
||||
from fakes import MemoryObjectStorage
|
||||
from messenger.attachments.application.ports import ObjectStorage
|
||||
from pictures import SAMPLE
|
||||
from pipeline import settle
|
||||
|
||||
|
||||
async def attach(http: AsyncClient, user: dict[str, Any], chat: dict[str, Any]) -> str:
|
||||
@@ -106,3 +109,41 @@ async def test_one_attachment_in_several_messages(client: AsyncClient) -> None:
|
||||
response = await send(client, user, chat, {"text": "", "attachment_ids": [file]})
|
||||
assert response.status_code == 201
|
||||
assert response.json()["attachment_ids"] == [file]
|
||||
|
||||
|
||||
async def broken(http: AsyncClient, user: dict[str, Any], chat: dict[str, Any]) -> str:
|
||||
attachment_id = await attach(http, user, chat)
|
||||
store = await resolve(http, ObjectStorage)
|
||||
assert isinstance(store, MemoryObjectStorage)
|
||||
store.objects[f"staging/{attachment_id}"] = (b"\x89PNG\r\n\x1a\n broken", "image/png")
|
||||
await settle(http)
|
||||
return attachment_id
|
||||
|
||||
|
||||
async def test_queued_attachment_is_sent_and_failed_is_conflict(client: AsyncClient) -> None:
|
||||
alice, bob, _, chat, _ = await scene(client)
|
||||
queued = await attach(client, alice, chat)
|
||||
early = await send(client, alice, chat, {"text": "", "attachment_ids": [queued]})
|
||||
assert early.status_code == 201, early.text
|
||||
failed = await broken(client, bob, chat)
|
||||
meta = (await client.get(f"/api/v1/attachments/{failed}", headers=actor(alice))).json()
|
||||
assert (meta["status"], meta["error_code"]) == ("failed", "invalid_image")
|
||||
refused = await send(client, alice, chat, {"text": "x", "attachment_ids": [queued, failed]})
|
||||
assert refused.status_code == 409
|
||||
assert refused.json()["error"]["code"] == "conflict"
|
||||
content = await client.get(f"/api/v1/attachments/{failed}/content", headers=actor(bob))
|
||||
assert content.status_code == 409
|
||||
|
||||
|
||||
async def test_replay_survives_a_later_failure(client: AsyncClient) -> None:
|
||||
alice, _, _, chat, _ = await scene(client)
|
||||
attachment_id = await attach(client, alice, chat)
|
||||
body = {"text": "", "attachment_ids": [attachment_id]}
|
||||
first = await send(client, alice, chat, body, key="replay-after-failure")
|
||||
store = await resolve(client, ObjectStorage)
|
||||
assert isinstance(store, MemoryObjectStorage)
|
||||
store.objects[f"staging/{attachment_id}"] = (b"garbage", "image/png")
|
||||
await settle(client)
|
||||
again = await send(client, alice, chat, body, key="replay-after-failure")
|
||||
assert (first.status_code, again.status_code) == (201, 201)
|
||||
assert again.json() == first.json()
|
||||
|
||||
Reference in New Issue
Block a user