forked from mtusi2026/mtusi2026lab4
The zone owns the Attachment aggregate, which is staged as pending with a server-assigned object key and becomes ready only when stored, the Filename value that refuses empty, blank, overlong names and names with a path separator or a control character, and the declared image type. UploadAttachment checks membership first through the ChatMembership port, then the name and the declared type, reads the body stream only up to the limit while hashing it, inspects the image through a port, commits the pending row, writes the object through the ObjectStorage port and commits the ready row. GetAttachment and OpenAttachment answer 404 to strangers and to a ready row whose object is gone. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
70 lines
2.3 KiB
Python
70 lines
2.3 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 AttachmentStored
|
|
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_stored() -> 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.object_key == f"attachments/{attachment_id}"
|
|
assert attachment.collect_events() == []
|
|
attachment.store(MOMENT - timedelta(seconds=1))
|
|
assert attachment.status is AttachmentStatus.READY
|
|
assert attachment.updated_at == MOMENT
|
|
events = attachment.collect_events()
|
|
assert [type(event) for event in events] == [AttachmentStored]
|
|
assert attachment.collect_events() == []
|