Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.
This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
240 lines
7.7 KiB
Python
240 lines
7.7 KiB
Python
from dataclasses import fields
|
|
from datetime import UTC, datetime, timedelta
|
|
from itertools import pairwise
|
|
from uuid import UUID, uuid4
|
|
|
|
import pytest
|
|
|
|
from messenger.identity.domain.errors import InvalidUserData
|
|
from messenger.identity.domain.models import Session, User, token_digest
|
|
from messenger.identity.domain.values import DisplayName, Password, Username
|
|
from messenger.messaging.domain.errors import (
|
|
ChatNotFound,
|
|
IdempotencyConflict,
|
|
InvalidAttachmentList,
|
|
InvalidChatData,
|
|
InvalidMessageText,
|
|
)
|
|
from messenger.messaging.domain.models import Chat, Receipt
|
|
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(
|
|
"raw", ["ab", "a" * 33, "Alice", "alice-01", "алиса", "", "a b", "alice\n", "alice\r\n"]
|
|
)
|
|
def test_username_rejects_invalid(raw: str) -> None:
|
|
with pytest.raises(InvalidUserData):
|
|
Username(raw)
|
|
|
|
|
|
@pytest.mark.parametrize("raw", ["abc", "a_b_9", "a" * 32])
|
|
def test_username_accepts_valid(raw: str) -> None:
|
|
assert Username(raw).value == raw
|
|
|
|
|
|
@pytest.mark.parametrize("raw", ["", " ", "\t\n", "x" * 101])
|
|
def test_display_name_rejects_invalid(raw: str) -> None:
|
|
with pytest.raises(InvalidUserData):
|
|
DisplayName(raw)
|
|
|
|
|
|
@pytest.mark.parametrize("raw", ["", " " * 20, "x" * 11, "x" * 129, "\t" * 12])
|
|
def test_password_rejects_invalid(raw: str) -> None:
|
|
with pytest.raises(InvalidUserData):
|
|
Password(raw)
|
|
|
|
|
|
@pytest.mark.parametrize("raw", ["x" * 12, "п" * 128, "пароль:с:двоеточием", "👋" * 12])
|
|
def test_password_counts_code_points(raw: str) -> None:
|
|
assert Password(raw).value == raw
|
|
|
|
|
|
def test_password_is_not_printed() -> None:
|
|
assert "секретный-пароль" not in repr(Password("секретный-пароль"))
|
|
|
|
|
|
def test_session_token_is_opaque_and_stored_as_digest() -> None:
|
|
user_id = uuid4()
|
|
token, session = Session.issue(user_id=user_id, issued_at=MOMENT, ttl=timedelta(hours=1))
|
|
assert len(token) >= 43
|
|
assert str(user_id) not in token
|
|
assert session.token_hash == token_digest(token)
|
|
assert len(session.token_hash) == 32
|
|
assert session.expires_at == MOMENT + timedelta(hours=1)
|
|
assert token not in repr(session)
|
|
|
|
|
|
def test_session_tokens_are_unique() -> None:
|
|
tokens = {
|
|
Session.issue(user_id=uuid4(), issued_at=MOMENT, ttl=timedelta(seconds=1))[0]
|
|
for _ in range(1000)
|
|
}
|
|
assert len(tokens) == 1000
|
|
|
|
|
|
def test_message_text_counts_code_points_not_bytes() -> None:
|
|
emoji = "👋" * 4000
|
|
assert len(MessageText(emoji).value) == 4000
|
|
with pytest.raises(InvalidMessageText):
|
|
MessageText("👋" * 4001)
|
|
|
|
|
|
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:
|
|
ids = [uuid4() for _ in members] if members else [uuid4()]
|
|
return Chat.create(
|
|
chat_id=uuid4(),
|
|
title="Чат",
|
|
member_ids=ids,
|
|
creator_id=ids[0],
|
|
created_at=MOMENT,
|
|
)
|
|
|
|
|
|
def test_chat_requires_creator_in_members() -> None:
|
|
members = [uuid4(), uuid4()]
|
|
with pytest.raises(InvalidChatData):
|
|
Chat.create(
|
|
chat_id=uuid4(),
|
|
title="Чат",
|
|
member_ids=members,
|
|
creator_id=uuid4(),
|
|
created_at=MOMENT,
|
|
)
|
|
|
|
|
|
def test_chat_rejects_duplicate_members() -> None:
|
|
same = uuid4()
|
|
with pytest.raises(InvalidChatData):
|
|
Chat.create(
|
|
chat_id=uuid4(),
|
|
title="Чат",
|
|
member_ids=[same, same],
|
|
creator_id=same,
|
|
created_at=MOMENT,
|
|
)
|
|
|
|
|
|
def test_chat_rejects_more_than_hundred_members() -> None:
|
|
members = [uuid4() for _ in range(101)]
|
|
with pytest.raises(InvalidChatData):
|
|
Chat.create(
|
|
chat_id=uuid4(),
|
|
title="Чат",
|
|
member_ids=members,
|
|
creator_id=members[0],
|
|
created_at=MOMENT,
|
|
)
|
|
|
|
|
|
def test_outsider_cannot_post() -> None:
|
|
chat = make_chat(1)
|
|
with pytest.raises(ChatNotFound):
|
|
chat.post(message_id=uuid4(), sender_id=uuid4(), text="привет", now=MOMENT)
|
|
|
|
|
|
def test_post_never_moves_history_back() -> None:
|
|
chat = make_chat(1)
|
|
sender = chat.member_ids[0]
|
|
first = chat.post(
|
|
message_id=uuid4(), sender_id=sender, text="первое", now=MOMENT + timedelta(seconds=1)
|
|
)
|
|
second = chat.post(message_id=uuid4(), sender_id=sender, text="второе", now=MOMENT)
|
|
assert second.created_at == first.created_at + timedelta(microseconds=1)
|
|
assert chat.last_message_at == second.created_at
|
|
|
|
|
|
def test_post_keeps_clock_when_it_is_ahead() -> None:
|
|
chat = make_chat(1)
|
|
sender = chat.member_ids[0]
|
|
first = chat.post(message_id=uuid4(), sender_id=sender, text="первое", now=MOMENT)
|
|
later = MOMENT + timedelta(seconds=5)
|
|
second = chat.post(message_id=uuid4(), sender_id=sender, text="второе", now=later)
|
|
assert first.created_at == MOMENT
|
|
assert second.created_at == later
|
|
|
|
|
|
def test_post_validates_membership_before_text() -> None:
|
|
chat = make_chat(1)
|
|
with pytest.raises(ChatNotFound):
|
|
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)
|
|
receipt = Receipt("key-1", b"a" * 32, message)
|
|
assert receipt.replay(b"a" * 32) is message
|
|
with pytest.raises(IdempotencyConflict):
|
|
receipt.replay(b"b" * 32)
|
|
|
|
|
|
def test_user_registration_emits_event() -> None:
|
|
user, event = User.register(
|
|
user_id=uuid4(),
|
|
username=Username("alice_01"),
|
|
display_name=DisplayName("Алиса"),
|
|
password_hash="$argon2id$v=19$m=19456,t=2,p=1$c2FsdA$aGFzaA",
|
|
registered_at=MOMENT,
|
|
)
|
|
assert event.name == "user.registered"
|
|
assert {field.name for field in fields(event)} == {"name", "user_id", "registered_at"}
|
|
assert user.created_at == MOMENT
|
|
assert "argon2id" not in repr(user)
|
|
|
|
|
|
def test_clock_is_strictly_increasing() -> None:
|
|
clock = MonotonicUtcClock()
|
|
stamps = [clock.now() for _ in range(1000)]
|
|
assert all(later > earlier for earlier, later in pairwise(stamps))
|
|
assert all(stamp.tzinfo is UTC for stamp in stamps)
|