Лабораторная 3 - Костылев - БПИ2303 - на 5 #151
@@ -16,11 +16,11 @@
|
|||||||
|
|
||||||
## 4. Идемпотентная отправка (зона `messaging`)
|
## 4. Идемпотентная отправка (зона `messaging`)
|
||||||
|
|
||||||
- [ ] 4.1 Домен: `Receipt` с `replay`, ошибка `IdempotencyConflict`; команда `PostMessage` с ключом и отпечатком
|
- [x] 4.1 Домен: `Receipt` с `replay`, ошибка `IdempotencyConflict`; команда `PostMessage` с ключом и отпечатком
|
||||||
- [ ] 4.2 Порт и адаптер PostgreSQL: поиск квитанции с учётом TTL, запись сообщения и ключа одним оператором; `LockNotAvailableError` → `503`
|
- [x] 4.2 Порт и адаптер PostgreSQL: поиск квитанции с учётом TTL, запись сообщения и ключа одним оператором; `LockNotAvailableError` → `503`
|
||||||
- [ ] 4.3 Обработчик: блокировка чата → `chat.post` → квитанция → повтор, `409` или запись; `IDEMPOTENCY_TTL_SECONDS` в настройках зоны
|
- [x] 4.3 Обработчик: блокировка чата → `chat.post` → квитанция → повтор, `409` или запись; `IDEMPOTENCY_TTL_SECONDS` в настройках зоны
|
||||||
- [ ] 4.4 HTTP: обязательный заголовок `Idempotency-Key` (`400`), отпечаток канонического JSON модели, `409 conflict` в реестре ошибок зоны
|
- [x] 4.4 HTTP: обязательный заголовок `Idempotency-Key` (`400`), отпечаток канонического JSON модели, `409 conflict` в реестре ошибок зоны
|
||||||
- [ ] 4.5 Проверка группы: тесты — повтор, другое тело `409`, нет и неверный ключ `400`, `4xx` не занимает ключ, другой пользователь и другой чат независимы, повтор после выхода `401`, посторонний `404`, истёкший ключ, 8 конкурентных повторов в реальном PostgreSQL дают одно сообщение; ключ во всех прежних отправках
|
- [x] 4.5 Проверка группы: тесты — повтор, другое тело `409`, нет и неверный ключ `400`, `4xx` не занимает ключ, другой пользователь и другой чат независимы, повтор после выхода `401`, посторонний `404`, истёкший ключ, 8 конкурентных повторов в реальном PostgreSQL дают одно сообщение; ключ во всех прежних отправках
|
||||||
|
|
||||||
## 5. Стенд
|
## 5. Стенд
|
||||||
|
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ class PostMessage(Command):
|
|||||||
sender_id: UUID
|
sender_id: UUID
|
||||||
chat_id: UUID
|
chat_id: UUID
|
||||||
text: str
|
text: str
|
||||||
|
key: str
|
||||||
|
fingerprint: bytes
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from messenger.messaging.application.commands import CreateChat, PostMessage
|
from messenger.messaging.application.commands import CreateChat, PostMessage
|
||||||
from messenger.messaging.application.ports import MessagingUnitOfWork
|
from messenger.messaging.application.ports import MessagingUnitOfWork
|
||||||
from messenger.messaging.domain.errors import ChatNotFound
|
from messenger.messaging.domain.errors import ChatNotFound
|
||||||
from messenger.messaging.domain.models import Chat, Message
|
from messenger.messaging.domain.models import Chat, Message, Receipt
|
||||||
from messenger.shared.application.ports import Clock, IdGenerator
|
from messenger.shared.application.ports import Clock, IdGenerator
|
||||||
|
|
||||||
|
|
||||||
@@ -28,24 +30,33 @@ class CreateChatHandler:
|
|||||||
|
|
||||||
|
|
||||||
class PostMessageHandler:
|
class PostMessageHandler:
|
||||||
__slots__ = ("_clock", "_ids", "_uow")
|
__slots__ = ("_clock", "_ids", "_ttl", "_uow")
|
||||||
|
|
||||||
def __init__(self, uow: MessagingUnitOfWork, clock: Clock, ids: IdGenerator) -> None:
|
def __init__(
|
||||||
|
self, uow: MessagingUnitOfWork, clock: Clock, ids: IdGenerator, ttl: timedelta
|
||||||
|
) -> None:
|
||||||
self._uow = uow
|
self._uow = uow
|
||||||
self._clock = clock
|
self._clock = clock
|
||||||
self._ids = ids
|
self._ids = ids
|
||||||
|
self._ttl = ttl
|
||||||
|
|
||||||
async def __call__(self, command: PostMessage) -> Message:
|
async def __call__(self, command: PostMessage) -> Message:
|
||||||
async with self._uow.transaction():
|
async with self._uow.transaction():
|
||||||
chat = await self._uow.chats.get(command.chat_id)
|
chat = await self._uow.chats.get(command.chat_id)
|
||||||
if chat is None:
|
if chat is None:
|
||||||
raise ChatNotFound("чат не найден")
|
raise ChatNotFound("чат не найден")
|
||||||
|
now = self._clock.now()
|
||||||
message = chat.post(
|
message = chat.post(
|
||||||
message_id=self._ids.new_id(),
|
message_id=self._ids.new_id(),
|
||||||
sender_id=command.sender_id,
|
sender_id=command.sender_id,
|
||||||
text=command.text,
|
text=command.text,
|
||||||
now=self._clock.now(),
|
now=now,
|
||||||
)
|
)
|
||||||
await self._uow.chats.append(message)
|
known = await self._uow.chats.receipt(
|
||||||
|
chat.id, command.sender_id, command.key, now - self._ttl
|
||||||
|
)
|
||||||
|
if known is not None:
|
||||||
|
return known.replay(command.fingerprint)
|
||||||
|
await self._uow.chats.append(Receipt(command.key, command.fingerprint, message))
|
||||||
await self._uow.commit()
|
await self._uow.commit()
|
||||||
return message
|
return message
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
from datetime import datetime
|
||||||
from typing import Protocol, runtime_checkable
|
from typing import Protocol, runtime_checkable
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from messenger.messaging.domain.keys import HistoryKey
|
from messenger.messaging.domain.keys import HistoryKey
|
||||||
from messenger.messaging.domain.models import Chat, Message
|
from messenger.messaging.domain.models import Chat, Message, Receipt
|
||||||
from messenger.shared.application.ports import UnitOfWork
|
from messenger.shared.application.ports import UnitOfWork
|
||||||
|
|
||||||
|
|
||||||
@@ -19,7 +20,11 @@ class ChatRepository(Protocol):
|
|||||||
|
|
||||||
async def get(self, chat_id: UUID) -> Chat | None: ...
|
async def get(self, chat_id: UUID) -> Chat | None: ...
|
||||||
|
|
||||||
async def append(self, message: Message) -> None: ...
|
async def receipt(
|
||||||
|
self, chat_id: UUID, sender_id: UUID, key: str, since: datetime
|
||||||
|
) -> Receipt | None: ...
|
||||||
|
|
||||||
|
async def append(self, receipt: Receipt) -> None: ...
|
||||||
|
|
||||||
|
|
||||||
@runtime_checkable
|
@runtime_checkable
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ from messenger.shared.config import Settings
|
|||||||
|
|
||||||
class MessagingSettings(Settings):
|
class MessagingSettings(Settings):
|
||||||
cursor_secret: SecretStr = Field(min_length=16)
|
cursor_secret: SecretStr = Field(min_length=16)
|
||||||
|
idempotency_ttl_seconds: int = Field(default=86_400, ge=1)
|
||||||
|
|||||||
@@ -19,3 +19,7 @@ class InvalidPageRequest(MessagingError):
|
|||||||
|
|
||||||
class ChatNotFound(MessagingError):
|
class ChatNotFound(MessagingError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class IdempotencyConflict(MessagingError):
|
||||||
|
pass
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from dataclasses import dataclass
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from messenger.messaging.domain.errors import ChatNotFound, InvalidChatData
|
from messenger.messaging.domain.errors import ChatNotFound, IdempotencyConflict, InvalidChatData
|
||||||
from messenger.messaging.domain.events import ChatCreated, MessagePosted
|
from messenger.messaging.domain.events import ChatCreated, MessagePosted
|
||||||
from messenger.messaging.domain.keys import HistoryKey
|
from messenger.messaging.domain.keys import HistoryKey
|
||||||
from messenger.messaging.domain.values import CHAT_MEMBERS_MAX, ChatTitle, MessageText
|
from messenger.messaging.domain.values import CHAT_MEMBERS_MAX, ChatTitle, MessageText
|
||||||
@@ -25,6 +25,18 @@ class Message:
|
|||||||
return (self.created_at, self.id.int)
|
return (self.created_at, self.id.int)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class Receipt:
|
||||||
|
key: str
|
||||||
|
fingerprint: bytes
|
||||||
|
message: Message
|
||||||
|
|
||||||
|
def replay(self, fingerprint: bytes) -> Message:
|
||||||
|
if fingerprint != self.fingerprint:
|
||||||
|
raise IdempotencyConflict("ключ идемпотентности уже использован с другим телом")
|
||||||
|
return self.message
|
||||||
|
|
||||||
|
|
||||||
class Chat:
|
class Chat:
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
"_events",
|
"_events",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from messenger.messaging.domain.errors import (
|
from messenger.messaging.domain.errors import (
|
||||||
ChatNotFound,
|
ChatNotFound,
|
||||||
|
IdempotencyConflict,
|
||||||
InvalidChatData,
|
InvalidChatData,
|
||||||
InvalidMessageText,
|
InvalidMessageText,
|
||||||
InvalidPageRequest,
|
InvalidPageRequest,
|
||||||
@@ -11,4 +12,5 @@ ERROR_CODES: dict[type[DomainError], tuple[str, int]] = {
|
|||||||
InvalidMessageText: ("invalid_request", 400),
|
InvalidMessageText: ("invalid_request", 400),
|
||||||
InvalidPageRequest: ("invalid_request", 400),
|
InvalidPageRequest: ("invalid_request", 400),
|
||||||
ChatNotFound: ("not_found", 404),
|
ChatNotFound: ("not_found", 404),
|
||||||
|
IdempotencyConflict: ("conflict", 409),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from messenger.messaging.entrypoints.schemas import (
|
|||||||
ChatOut,
|
ChatOut,
|
||||||
CreateChatRequest,
|
CreateChatRequest,
|
||||||
CreateMessageRequest,
|
CreateMessageRequest,
|
||||||
|
IdempotencyKey,
|
||||||
MessageOut,
|
MessageOut,
|
||||||
PageOut,
|
PageOut,
|
||||||
PageQuery,
|
PageQuery,
|
||||||
@@ -61,9 +62,18 @@ async def post_message(
|
|||||||
chat_id: UUID,
|
chat_id: UUID,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
body: CreateMessageRequest,
|
body: CreateMessageRequest,
|
||||||
|
key: IdempotencyKey,
|
||||||
bus: FromDishka[MessagingCommandBus],
|
bus: FromDishka[MessagingCommandBus],
|
||||||
) -> MessageOut:
|
) -> MessageOut:
|
||||||
message = await bus.execute(PostMessage(sender_id=actor, chat_id=chat_id, text=body.text))
|
message = await bus.execute(
|
||||||
|
PostMessage(
|
||||||
|
sender_id=actor,
|
||||||
|
chat_id=chat_id,
|
||||||
|
text=body.text,
|
||||||
|
key=key,
|
||||||
|
fingerprint=body.fingerprint(),
|
||||||
|
)
|
||||||
|
)
|
||||||
return MessageOut.of(message)
|
return MessageOut.of(message)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from dishka import Provider, Scope, provide
|
from dishka import Provider, Scope, provide
|
||||||
|
|
||||||
from messenger.messaging.application.bus import MessagingCommandBus
|
from messenger.messaging.application.bus import MessagingCommandBus
|
||||||
@@ -57,9 +59,11 @@ class MessagingProvider(Provider):
|
|||||||
publisher: EventPublisher,
|
publisher: EventPublisher,
|
||||||
clock: Clock,
|
clock: Clock,
|
||||||
ids: IdGenerator,
|
ids: IdGenerator,
|
||||||
|
settings: MessagingSettings,
|
||||||
) -> MessagingCommandBus:
|
) -> MessagingCommandBus:
|
||||||
|
ttl = timedelta(seconds=settings.idempotency_ttl_seconds)
|
||||||
handlers: dict[type[Command], CommandHandler] = {
|
handlers: dict[type[Command], CommandHandler] = {
|
||||||
CreateChat: CreateChatHandler(uow, clock, ids),
|
CreateChat: CreateChatHandler(uow, clock, ids),
|
||||||
PostMessage: PostMessageHandler(uow, clock, ids),
|
PostMessage: PostMessageHandler(uow, clock, ids, ttl),
|
||||||
}
|
}
|
||||||
return MessagingCommandBus(handlers, uow, publisher)
|
return MessagingCommandBus(handlers, uow, publisher)
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from hashlib import sha256
|
||||||
from typing import Annotated, Self
|
from typing import Annotated, Self
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import Header
|
||||||
from pydantic import BaseModel, ConfigDict, Field
|
from pydantic import BaseModel, ConfigDict, Field
|
||||||
|
|
||||||
from messenger.messaging.application.queries import Page
|
from messenger.messaging.application.queries import Page
|
||||||
@@ -10,6 +12,11 @@ from messenger.messaging.domain.models import Chat, Message
|
|||||||
|
|
||||||
STRICT = ConfigDict(extra="forbid", strict=True)
|
STRICT = ConfigDict(extra="forbid", strict=True)
|
||||||
|
|
||||||
|
type IdempotencyKey = Annotated[
|
||||||
|
str,
|
||||||
|
Header(alias="Idempotency-Key", min_length=1, max_length=128, pattern=r"^[A-Za-z0-9._:-]+$"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class CreateChatRequest(BaseModel):
|
class CreateChatRequest(BaseModel):
|
||||||
model_config = STRICT
|
model_config = STRICT
|
||||||
@@ -23,6 +30,9 @@ class CreateMessageRequest(BaseModel):
|
|||||||
|
|
||||||
text: str
|
text: str
|
||||||
|
|
||||||
|
def fingerprint(self) -> bytes:
|
||||||
|
return sha256(self.model_dump_json().encode()).digest()
|
||||||
|
|
||||||
|
|
||||||
class PageQuery(BaseModel):
|
class PageQuery(BaseModel):
|
||||||
limit: int = Field(default=50, ge=1, le=100)
|
limit: int = Field(default=50, ge=1, le=100)
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
from datetime import datetime
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from asyncpg import ForeignKeyViolationError, Record
|
from asyncpg import ForeignKeyViolationError, Record
|
||||||
|
|
||||||
from messenger.messaging.domain.errors import InvalidChatData
|
from messenger.messaging.domain.errors import InvalidChatData
|
||||||
from messenger.messaging.domain.keys import HistoryKey
|
from messenger.messaging.domain.keys import HistoryKey
|
||||||
from messenger.messaging.domain.models import Chat, Message
|
from messenger.messaging.domain.models import Chat, Message, Receipt
|
||||||
from messenger.messaging.domain.values import ChatTitle, MessageText
|
from messenger.messaging.domain.values import ChatTitle, MessageText
|
||||||
from messenger.shared.infrastructure.postgres import (
|
from messenger.shared.infrastructure.postgres import (
|
||||||
Connection,
|
Connection,
|
||||||
@@ -27,11 +28,23 @@ FROM chats c
|
|||||||
WHERE c.id = $1
|
WHERE c.id = $1
|
||||||
FOR UPDATE
|
FOR UPDATE
|
||||||
"""
|
"""
|
||||||
|
RECEIPT = """
|
||||||
|
SELECT k.fingerprint, m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||||
|
FROM idempotency_keys k JOIN messages m ON m.id = k.message_id
|
||||||
|
WHERE k.chat_id = $1 AND k.user_id = $2 AND k.key = $3 AND k.created_at > $4
|
||||||
|
"""
|
||||||
APPEND_MESSAGE = """
|
APPEND_MESSAGE = """
|
||||||
WITH message AS (
|
WITH message AS (
|
||||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
RETURNING chat_id, created_at
|
RETURNING id, chat_id, sender_id, created_at
|
||||||
|
), receipt AS (
|
||||||
|
INSERT INTO idempotency_keys (chat_id, user_id, key, fingerprint, message_id, created_at)
|
||||||
|
SELECT chat_id, sender_id, $6, $7, id, created_at FROM message
|
||||||
|
ON CONFLICT (chat_id, user_id, key) DO UPDATE
|
||||||
|
SET fingerprint = EXCLUDED.fingerprint,
|
||||||
|
message_id = EXCLUDED.message_id,
|
||||||
|
created_at = EXCLUDED.created_at
|
||||||
)
|
)
|
||||||
UPDATE chats SET last_message_at = message.created_at
|
UPDATE chats SET last_message_at = message.created_at
|
||||||
FROM message
|
FROM message
|
||||||
@@ -124,7 +137,14 @@ class PostgresChatRepository:
|
|||||||
self._seen.append(chat)
|
self._seen.append(chat)
|
||||||
return chat
|
return chat
|
||||||
|
|
||||||
async def append(self, message: Message) -> None:
|
async def receipt(
|
||||||
|
self, chat_id: UUID, sender_id: UUID, key: str, since: datetime
|
||||||
|
) -> Receipt | None:
|
||||||
|
row = await self._connection.fetchrow(RECEIPT, chat_id, sender_id, key, since)
|
||||||
|
return None if row is None else Receipt(key, row["fingerprint"], message_of(row))
|
||||||
|
|
||||||
|
async def append(self, receipt: Receipt) -> None:
|
||||||
|
message = receipt.message
|
||||||
await self._connection.execute(
|
await self._connection.execute(
|
||||||
APPEND_MESSAGE,
|
APPEND_MESSAGE,
|
||||||
message.id,
|
message.id,
|
||||||
@@ -132,6 +152,8 @@ class PostgresChatRepository:
|
|||||||
message.sender_id,
|
message.sender_id,
|
||||||
message.text.value,
|
message.text.value,
|
||||||
message.created_at,
|
message.created_at,
|
||||||
|
receipt.key,
|
||||||
|
receipt.fingerprint,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ FAILURES = (
|
|||||||
asyncpg.PostgresConnectionError,
|
asyncpg.PostgresConnectionError,
|
||||||
asyncpg.exceptions.OperatorInterventionError,
|
asyncpg.exceptions.OperatorInterventionError,
|
||||||
asyncpg.exceptions.InsufficientResourcesError,
|
asyncpg.exceptions.InsufficientResourcesError,
|
||||||
|
asyncpg.exceptions.LockNotAvailableError,
|
||||||
)
|
)
|
||||||
|
|
||||||
type Connection = asyncpg.Connection
|
type Connection = asyncpg.Connection
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from conftest import actor, make_chat, register
|
from conftest import actor, keyed, make_chat, register
|
||||||
|
|
||||||
|
|
||||||
async def test_outsider_gets_404_on_chat(client: AsyncClient) -> None:
|
async def test_outsider_gets_404_on_chat(client: AsyncClient) -> None:
|
||||||
@@ -33,7 +33,7 @@ async def test_outsider_cannot_write(client: AsyncClient) -> None:
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": "чужое"},
|
json={"text": "чужое"},
|
||||||
headers=actor(eve),
|
headers=keyed(eve),
|
||||||
)
|
)
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
history = await client.get(f"/api/v1/chats/{chat['id']}/messages", headers=actor(alice))
|
history = await client.get(f"/api/v1/chats/{chat['id']}/messages", headers=actor(alice))
|
||||||
@@ -55,7 +55,7 @@ async def test_access_checked_before_body(client: AsyncClient) -> None:
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": ""},
|
json={"text": ""},
|
||||||
headers=actor(eve),
|
headers=keyed(eve),
|
||||||
)
|
)
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ def test_zone_errors_do_not_leak_transport_codes() -> None:
|
|||||||
|
|
||||||
|
|
||||||
IDENTITY_TABLES = ("users", "sessions")
|
IDENTITY_TABLES = ("users", "sessions")
|
||||||
MESSAGING_TABLES = ("chats", "chat_members", "messages")
|
MESSAGING_TABLES = ("chats", "chat_members", "messages", "idempotency_keys")
|
||||||
|
|
||||||
|
|
||||||
def sql_tables(zone: str, tables: tuple[str, ...]) -> set[str]:
|
def sql_tables(zone: str, tables: tuple[str, ...]) -> set[str]:
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from conftest import PASSWORD, actor, make_chat, register
|
from conftest import PASSWORD, actor, keyed, make_chat, register
|
||||||
|
|
||||||
CLIENTS = 10
|
CLIENTS = 10
|
||||||
PER_CLIENT = 10
|
PER_CLIENT = 10
|
||||||
@@ -32,7 +32,7 @@ async def flood(client: AsyncClient, chat: dict, sender: dict) -> list[int]:
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": f"нагрузка-{worker}-{index}"},
|
json={"text": f"нагрузка-{worker}-{index}"},
|
||||||
headers=actor(sender),
|
headers=keyed(sender),
|
||||||
)
|
)
|
||||||
codes.append(response.status_code)
|
codes.append(response.status_code)
|
||||||
return codes
|
return codes
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from conftest import PASSWORD, actor, make_chat, new_user, register, running_app, scalar
|
from conftest import PASSWORD, actor, keyed, make_chat, new_user, register, running_app, scalar
|
||||||
|
|
||||||
REGISTRATIONS = 25
|
REGISTRATIONS = 25
|
||||||
|
|
||||||
@@ -98,14 +98,14 @@ async def test_rejected_message_does_not_enter_history() -> None:
|
|||||||
await client.post(
|
await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": " "},
|
json={"text": " "},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
).status_code == 400
|
).status_code == 400
|
||||||
assert (
|
assert (
|
||||||
await client.post(
|
await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": "чужое"},
|
json={"text": "чужое"},
|
||||||
headers=actor(eve),
|
headers=keyed(eve),
|
||||||
)
|
)
|
||||||
).status_code == 404
|
).status_code == 404
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ async def test_confirmed_write_is_visible_to_next_read() -> None:
|
|||||||
posted = await client.post(
|
posted = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": f"подтверждение {index}"},
|
json={"text": f"подтверждение {index}"},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
assert posted.status_code == 201
|
assert posted.status_code == 201
|
||||||
history = await drain_history(client, chat, bob)
|
history = await drain_history(client, chat, bob)
|
||||||
@@ -140,7 +140,7 @@ async def test_parallel_writes_across_zones_keep_counts_consistent() -> None:
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": f"поток-{worker}-{index}"},
|
json={"text": f"поток-{worker}-{index}"},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
assert response.status_code == 201
|
assert response.status_code == 201
|
||||||
|
|
||||||
@@ -173,7 +173,7 @@ async def test_chat_membership_survives_parallel_reads_and_writes() -> None:
|
|||||||
await client.post(
|
await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": f"параллельно {index}"},
|
json={"text": f"параллельно {index}"},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
|
|
||||||
async def intruder() -> int:
|
async def intruder() -> int:
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from httpx import ASGITransport, AsyncClient
|
from httpx import ASGITransport, AsyncClient
|
||||||
|
|
||||||
from conftest import actor, make_chat, register, split_settings
|
from conftest import keyed, make_chat, register, split_settings
|
||||||
from fakes import NullLogger
|
from fakes import NullLogger
|
||||||
from messenger.identity.domain.events import UserRegistered
|
from messenger.identity.domain.events import UserRegistered
|
||||||
from messenger.main import create_app
|
from messenger.main import create_app
|
||||||
@@ -108,7 +108,7 @@ async def test_publication_does_not_delay_response() -> None:
|
|||||||
http.post(
|
http.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": "не ждём обработчик"},
|
json={"text": "не ждём обработчик"},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
),
|
),
|
||||||
timeout=2,
|
timeout=2,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
import asyncio
|
||||||
|
from typing import Any
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from httpx import AsyncClient, Response
|
||||||
|
|
||||||
|
from conftest import actor, keyed, make_chat, register, running_app, scalar
|
||||||
|
|
||||||
|
INVALID_KEYS = ["", "k" * 129, "два слова", "a b", "a/b", "ключ"]
|
||||||
|
|
||||||
|
|
||||||
|
async def send(http: AsyncClient, chat: dict, user: dict, text: str, key: str) -> Response:
|
||||||
|
return await http.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=keyed(user, key)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def history(http: AsyncClient, chat: dict, user: dict) -> list[dict[str, Any]]:
|
||||||
|
response = await http.get(f"/api/v1/chats/{chat['id']}/messages?limit=100", headers=actor(user))
|
||||||
|
return response.json()["items"]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_replay_returns_the_same_message(client: AsyncClient) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
chat = await make_chat(client, alice)
|
||||||
|
first = await send(client, chat, alice, "один раз", "replay-1")
|
||||||
|
again = await send(client, chat, alice, "один раз", "replay-1")
|
||||||
|
assert first.status_code == again.status_code == 201
|
||||||
|
assert again.json() == first.json()
|
||||||
|
assert [item["id"] for item in await history(client, chat, alice)] == [first.json()["id"]]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_same_json_in_other_layout_is_a_replay(client: AsyncClient) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
chat = await make_chat(client, alice)
|
||||||
|
first = await send(client, chat, alice, "форма", "layout-1")
|
||||||
|
again = await client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
content='{ "text" : "форма" }'.encode(),
|
||||||
|
headers={**keyed(alice, "layout-1"), "Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
assert again.status_code == 201
|
||||||
|
assert again.json() == first.json()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_other_body_with_same_key_conflicts(client: AsyncClient) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
chat = await make_chat(client, alice)
|
||||||
|
await send(client, chat, alice, "первое", "conflict-1")
|
||||||
|
response = await send(client, chat, alice, "другое", "conflict-1")
|
||||||
|
assert response.status_code == 409
|
||||||
|
assert response.json()["error"]["code"] == "conflict"
|
||||||
|
assert len(await history(client, chat, alice)) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_missing_key_is_rejected(client: AsyncClient) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
chat = await make_chat(client, alice)
|
||||||
|
response = await client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages", json={"text": "без ключа"}, headers=actor(alice)
|
||||||
|
)
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.json()["error"]["code"] == "invalid_request"
|
||||||
|
assert await history(client, chat, alice) == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("key", INVALID_KEYS, ids=range(len(INVALID_KEYS)))
|
||||||
|
async def test_malformed_key_is_rejected(client: AsyncClient, key: str) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
chat = await make_chat(client, alice)
|
||||||
|
headers = {**actor(alice), "Idempotency-Key": key}
|
||||||
|
response = await client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
json={"text": "ключ плохой"},
|
||||||
|
headers={name.encode(): value.encode() for name, value in headers.items()},
|
||||||
|
)
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert await history(client, chat, alice) == []
|
||||||
|
|
||||||
|
|
||||||
|
async def test_rejected_attempt_does_not_take_the_key(client: AsyncClient) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
eve = await register(client, "eve")
|
||||||
|
chat = await make_chat(client, alice)
|
||||||
|
assert (await send(client, chat, alice, "", "spare-1")).status_code == 400
|
||||||
|
assert (await send(client, chat, eve, "чужое", "spare-1")).status_code == 404
|
||||||
|
assert (await send(client, chat, alice, "теперь можно", "spare-1")).status_code == 201
|
||||||
|
|
||||||
|
|
||||||
|
async def test_key_is_scoped_by_user_and_chat(client: AsyncClient) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
bob = await register(client, "bob")
|
||||||
|
chat = await make_chat(client, alice, bob)
|
||||||
|
other = await make_chat(client, alice)
|
||||||
|
ids = {
|
||||||
|
(await send(client, chat, alice, "алиса", "shared-1")).json()["id"],
|
||||||
|
(await send(client, chat, bob, "боб", "shared-1")).json()["id"],
|
||||||
|
(await send(client, other, alice, "другой чат", "shared-1")).json()["id"],
|
||||||
|
}
|
||||||
|
assert len(ids) == 3
|
||||||
|
|
||||||
|
|
||||||
|
async def test_replay_checks_session_and_access_again(client: AsyncClient) -> None:
|
||||||
|
alice = await register(client, "alice")
|
||||||
|
eve = await register(client, "eve")
|
||||||
|
chat = await make_chat(client, alice)
|
||||||
|
assert (await send(client, chat, alice, "до выхода", "recheck-1")).status_code == 201
|
||||||
|
assert (await send(client, chat, eve, "до выхода", "recheck-1")).status_code == 404
|
||||||
|
logout = await client.delete("/api/v1/auth/sessions/current", headers=actor(alice))
|
||||||
|
assert logout.status_code == 204
|
||||||
|
assert (await send(client, chat, alice, "до выхода", "recheck-1")).status_code == 401
|
||||||
|
|
||||||
|
|
||||||
|
async def test_expired_key_starts_over() -> None:
|
||||||
|
async with running_app(idempotency_ttl_seconds=1) as http:
|
||||||
|
alice = await register(http, "alice")
|
||||||
|
chat = await make_chat(http, alice)
|
||||||
|
first = await send(http, chat, alice, "давно", "expire-1")
|
||||||
|
await asyncio.sleep(1.1)
|
||||||
|
later = await send(http, chat, alice, "заново", "expire-1")
|
||||||
|
assert later.status_code == 201
|
||||||
|
assert later.json()["id"] != first.json()["id"]
|
||||||
|
assert len(await history(http, chat, alice)) == 2
|
||||||
|
|
||||||
|
|
||||||
|
async def test_concurrent_retries_through_two_replicas_store_one_message() -> None:
|
||||||
|
async with (
|
||||||
|
running_app(instance_id="replica-a") as first,
|
||||||
|
running_app(instance_id="replica-b") as second,
|
||||||
|
):
|
||||||
|
alice = await register(first, "alice")
|
||||||
|
chat = await make_chat(first, alice)
|
||||||
|
key, text = uuid4().hex, "гонка ключа"
|
||||||
|
responses = await asyncio.gather(
|
||||||
|
*(send(http, chat, alice, text, key) for http in (first, second) * 4)
|
||||||
|
)
|
||||||
|
assert {response.status_code for response in responses} == {201}
|
||||||
|
assert {response.headers["X-Instance-Id"] for response in responses} == {
|
||||||
|
"replica-a",
|
||||||
|
"replica-b",
|
||||||
|
}
|
||||||
|
assert all(response.json() == responses[0].json() for response in responses)
|
||||||
|
assert [item["text"] for item in await history(second, chat, alice)] == [text]
|
||||||
|
assert await scalar("SELECT count(*) FROM idempotency_keys WHERE key = $1", key) == 1
|
||||||
@@ -3,7 +3,7 @@ from uuid import uuid4
|
|||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from conftest import PASSWORD, actor, make_chat, register
|
from conftest import PASSWORD, actor, keyed, make_chat, register
|
||||||
|
|
||||||
ERROR_CODES = {
|
ERROR_CODES = {
|
||||||
400: "invalid_request",
|
400: "invalid_request",
|
||||||
@@ -122,7 +122,7 @@ async def test_empty_message_rejected(client: AsyncClient) -> None:
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": text},
|
json={"text": text},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
assert_error(response, 400)
|
assert_error(response, 400)
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ async def test_long_message_rejected(client: AsyncClient) -> None:
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": "я" * 4001},
|
json={"text": "я" * 4001},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
assert_error(response, 400)
|
assert_error(response, 400)
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ async def test_sender_id_not_accepted(client: AsyncClient) -> None:
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": "подмена", "sender_id": bob["id"]},
|
json={"text": "подмена", "sender_id": bob["id"]},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
assert_error(response, 400)
|
assert_error(response, 400)
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from httpx import ASGITransport, AsyncClient
|
from httpx import ASGITransport, AsyncClient
|
||||||
|
|
||||||
from conftest import PASSWORD, actor, make_chat, register, running_app, split_settings
|
from conftest import PASSWORD, actor, keyed, make_chat, register, running_app, split_settings
|
||||||
from messenger.main import create_app
|
from messenger.main import create_app
|
||||||
|
|
||||||
MARKER_TEXT = "маркер-который-не-должен-попасть-в-логи-9f3a"
|
MARKER_TEXT = "маркер-который-не-должен-попасть-в-логи-9f3a"
|
||||||
@@ -67,7 +67,7 @@ async def test_logs_do_not_contain_message_text(capsys) -> None:
|
|||||||
response = await http.post(
|
response = await http.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": MARKER_TEXT},
|
json={"text": MARKER_TEXT},
|
||||||
headers=actor(alice),
|
headers=keyed(alice),
|
||||||
)
|
)
|
||||||
assert response.status_code == 201
|
assert response.status_code == 201
|
||||||
captured = capsys.readouterr()
|
captured = capsys.readouterr()
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from urllib.parse import quote
|
|||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from conftest import actor, make_chat, register
|
from conftest import actor, keyed, make_chat, register
|
||||||
|
|
||||||
TOTAL = 12
|
TOTAL = 12
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ async def fill(client: AsyncClient, chat: dict, sender: dict, count: int) -> lis
|
|||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages",
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
json={"text": text},
|
json={"text": text},
|
||||||
headers=actor(sender),
|
headers=keyed(sender),
|
||||||
)
|
)
|
||||||
assert response.status_code == 201, response.text
|
assert response.status_code == 201, response.text
|
||||||
texts.append(text)
|
texts.append(text)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from contextlib import asynccontextmanager
|
|||||||
|
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from conftest import actor, make_chat, register, resolve, running_app
|
from conftest import keyed, make_chat, register, resolve, running_app
|
||||||
from messenger.messaging.application import handlers
|
from messenger.messaging.application import handlers
|
||||||
from messenger.messaging.domain.events import MessagePosted
|
from messenger.messaging.domain.events import MessagePosted
|
||||||
from messenger.shared.application.messagebus import EventBus
|
from messenger.shared.application.messagebus import EventBus
|
||||||
@@ -29,7 +29,7 @@ async def slow_post(delay: float) -> AsyncIterator[None]:
|
|||||||
|
|
||||||
async def post(client: AsyncClient, chat: dict, sender: dict, text: str):
|
async def post(client: AsyncClient, chat: dict, sender: dict, text: str):
|
||||||
return await client.post(
|
return await client.post(
|
||||||
f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=actor(sender)
|
f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=keyed(sender)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,8 @@ async def test_created_at_is_rfc3339_utc(monkeypatch: pytest.MonkeyPatch, moment
|
|||||||
assert_wire(listed, {"items": [chat], "next_cursor": None})
|
assert_wire(listed, {"items": [chat], "next_cursor": None})
|
||||||
|
|
||||||
path = f"/api/v1/chats/{chat_id}/messages"
|
path = f"/api/v1/chats/{chat_id}/messages"
|
||||||
response = await http.post(path, json={"text": "привет"}, headers=headers)
|
sent = {**headers, "Idempotency-Key": "wire-1"}
|
||||||
|
response = await http.post(path, json={"text": "привет"}, headers=sent)
|
||||||
message = {
|
message = {
|
||||||
"id": response.json()["id"],
|
"id": response.json()["id"],
|
||||||
"chat_id": chat_id,
|
"chat_id": chat_id,
|
||||||
|
|||||||
@@ -202,3 +202,7 @@ async def make_chat(
|
|||||||
|
|
||||||
def actor(user: dict[str, Any]) -> dict[str, str]:
|
def actor(user: dict[str, Any]) -> dict[str, str]:
|
||||||
return bearer(TOKENS[user["id"]])
|
return bearer(TOKENS[user["id"]])
|
||||||
|
|
||||||
|
|
||||||
|
def keyed(user: dict[str, Any], key: str | None = None) -> dict[str, str]:
|
||||||
|
return {**actor(user), "Idempotency-Key": key or uuid4().hex}
|
||||||
|
|||||||
@@ -8,8 +8,13 @@ import pytest
|
|||||||
from messenger.identity.domain.errors import InvalidUserData
|
from messenger.identity.domain.errors import InvalidUserData
|
||||||
from messenger.identity.domain.models import Session, User, token_digest
|
from messenger.identity.domain.models import Session, User, token_digest
|
||||||
from messenger.identity.domain.values import DisplayName, Password, Username
|
from messenger.identity.domain.values import DisplayName, Password, Username
|
||||||
from messenger.messaging.domain.errors import ChatNotFound, InvalidChatData, InvalidMessageText
|
from messenger.messaging.domain.errors import (
|
||||||
from messenger.messaging.domain.models import Chat
|
ChatNotFound,
|
||||||
|
IdempotencyConflict,
|
||||||
|
InvalidChatData,
|
||||||
|
InvalidMessageText,
|
||||||
|
)
|
||||||
|
from messenger.messaging.domain.models import Chat, Receipt
|
||||||
from messenger.messaging.domain.values import MessageText
|
from messenger.messaging.domain.values import MessageText
|
||||||
from messenger.shared.infrastructure.clock import MonotonicUtcClock
|
from messenger.shared.infrastructure.clock import MonotonicUtcClock
|
||||||
|
|
||||||
@@ -162,6 +167,15 @@ def test_post_validates_membership_before_text() -> None:
|
|||||||
chat.post(message_id=uuid4(), sender_id=uuid4(), text="", now=MOMENT)
|
chat.post(message_id=uuid4(), sender_id=uuid4(), text="", now=MOMENT)
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
def test_user_registration_emits_event() -> None:
|
||||||
user, event = User.register(
|
user, event = User.register(
|
||||||
user_id=uuid4(),
|
user_id=uuid4(),
|
||||||
|
|||||||
Reference in New Issue
Block a user