Upload now stages the checked original under staging/, and one transaction moves the intent to queued, inserts the job (pipeline version 1, attempt 1), its outbox row and an audit event; the 202 is the in-memory snapshot, never a reread. Content answers 409 until the attachment is ready, the thumbnail variant is served from the published version and absent variants are 404. The relay publishes outbox rows through FastStream with publisher confirms, mandatory routing and persistent delivery, marks a row only after the broker acked it, returns jobs whose lease expired and deletes the staging object of ready attachments. The worker consumes the quorum queue through a FastStream subscriber that declares nothing, takes prefetch from WORKER_CONCURRENCY and acks manually after the result commit, rejects messages off jobs.schema.json to the dead letter exchange and requeues after an infrastructure error. A job is claimed under a lease token; the original is copied to originals/ and a 256 px JPEG thumbnail is written under a content-addressed key; the commit is fenced by the token, inserts variants with ON CONFLICT DO NOTHING and publishes only a newer pipeline version. A transient error reschedules the same job with an exponential delay and a new outbox row, the last attempt fails with retry_exhausted, an undecodable image fails at once with invalid_image, and a redelivered finished job is a recorded no-op. The worker and the relay are assembled in worker.py and relay.py with the dishka FastStream integration and the platform providers of the API; the shared core imports neither FastStream nor aio-pika. Tests cover the thumbnail geometry and rendering, the job message against the contract, the pipeline against PostgreSQL (duplicates, parallel copies, retries, the attempt bound, leases and fencing) and the broker adapters against a throwaway RabbitMQ with the stand's topology and permissions. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
97 lines
3.1 KiB
Python
97 lines
3.1 KiB
Python
from datetime import UTC, datetime, timedelta
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
|
|
from messenger.attachments.domain.errors import InvalidFilename, UnsupportedImage
|
|
from messenger.attachments.domain.events import AttachmentDiscarded, AttachmentQueued
|
|
from messenger.attachments.domain.models import Attachment, AttachmentStatus
|
|
from messenger.attachments.domain.values import Filename, ImageInfo, ImageKind
|
|
|
|
MOMENT = datetime(2026, 1, 1, tzinfo=UTC)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"raw",
|
|
[
|
|
*("", " ", "\t\n", "../a.png", "a/b.png", "a\\b.png"),
|
|
*("a\x00.png", "a\x01.png", "a\x7f.png", "a\n.png", "\x9f.png", "x" * 256),
|
|
],
|
|
)
|
|
def test_filename_rejects_dangerous_and_empty(raw: str) -> None:
|
|
with pytest.raises(InvalidFilename):
|
|
Filename(raw)
|
|
|
|
|
|
@pytest.mark.parametrize("raw", ["sample.png", "фото 1.png", "я" * 255, "👋.jpg", "a..b.png"])
|
|
def test_filename_keeps_display_names(raw: str) -> None:
|
|
assert Filename(raw).value == raw
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("header", "kind"),
|
|
[
|
|
("image/png", ImageKind.PNG),
|
|
("IMAGE/JPEG", ImageKind.JPEG),
|
|
("image/png; charset=binary", ImageKind.PNG),
|
|
],
|
|
)
|
|
def test_declared_type_ignores_case_and_parameters(header: str, kind: ImageKind) -> None:
|
|
assert ImageKind.declared(header) is kind
|
|
|
|
|
|
@pytest.mark.parametrize("header", [None, "", "image/gif", "application/octet-stream", "image/*"])
|
|
def test_declared_type_accepts_only_png_and_jpeg(header: str | None) -> None:
|
|
with pytest.raises(UnsupportedImage):
|
|
ImageKind.declared(header)
|
|
|
|
|
|
def test_attachment_is_pending_until_queued() -> None:
|
|
attachment_id = uuid4()
|
|
attachment = Attachment.stage(
|
|
attachment_id=attachment_id,
|
|
chat_id=uuid4(),
|
|
owner_id=uuid4(),
|
|
filename=Filename("a.png"),
|
|
image=ImageInfo(ImageKind.PNG, 2, 3),
|
|
size_bytes=10,
|
|
sha256="0" * 64,
|
|
now=MOMENT,
|
|
)
|
|
assert attachment.status is AttachmentStatus.PENDING
|
|
assert attachment.staging_key == f"staging/{attachment_id}"
|
|
assert (attachment.original_key, attachment.published_version, attachment.variants) == (
|
|
None,
|
|
0,
|
|
(),
|
|
)
|
|
assert attachment.collect_events() == []
|
|
attachment.enqueue(MOMENT - timedelta(seconds=1))
|
|
assert attachment.status is AttachmentStatus.QUEUED
|
|
assert not attachment.ready
|
|
assert attachment.updated_at == MOMENT
|
|
events = attachment.collect_events()
|
|
assert [type(event) for event in events] == [AttachmentQueued]
|
|
assert attachment.collect_events() == []
|
|
|
|
|
|
def test_discarded_intent_records_its_reason() -> None:
|
|
attachment = Attachment.stage(
|
|
attachment_id=uuid4(),
|
|
chat_id=uuid4(),
|
|
owner_id=uuid4(),
|
|
filename=Filename("a.png"),
|
|
image=ImageInfo(ImageKind.PNG, 1, 1),
|
|
size_bytes=1,
|
|
sha256="0" * 64,
|
|
now=MOMENT,
|
|
)
|
|
attachment.discard("abandoned")
|
|
(event,) = attachment.collect_events()
|
|
assert isinstance(event, AttachmentDiscarded)
|
|
assert (event.attachment_id, event.chat_id, event.reason) == (
|
|
attachment.id,
|
|
attachment.chat_id,
|
|
"abandoned",
|
|
)
|