"""Idempotency-Key отправки сообщения: повтор, конфликт, область ключа, гонки""" import asyncio import json import uuid from collections import Counter import httpx import pytest from helpers import assert_error, auth, message_headers, psql, read_all_messages PARALLEL_REPLAYS = 20 def post_message( api: httpx.Client, user: dict, chat: dict, text: str, key: str ) -> httpx.Response: return api.post( f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=message_headers(user, key), ) def history(api: httpx.Client, user: dict, chat: dict) -> list[dict]: items, _ = read_all_messages(api, user, chat["id"], 100) return items def test_replay_returns_same_message(api: httpx.Client, make_user, make_chat) -> None: alice = make_user("alice") chat = make_chat(alice, [alice]) key = uuid.uuid4().hex first = post_message(api, alice, chat, "один раз", key) second = post_message(api, alice, chat, "один раз", key) assert first.status_code == second.status_code == 201, second.text assert first.json() == second.json() assert history(api, alice, chat) == [first.json()] def test_replay_ignores_json_formatting( api: httpx.Client, make_user, make_chat ) -> None: """Пробелы и форма записи JSON не меняют смысл тела: это тот же запрос""" alice = make_user("alice") chat = make_chat(alice, [alice]) headers = {**message_headers(alice), "Content-Type": "application/json"} path = f"/api/v1/chats/{chat['id']}/messages" escaped = b'{"text":"\\u041f\\u0440\\u0438"}' compact = api.post(path, content=escaped, headers=headers) spaced = api.post(path, content='{ "text" : "При" }'.encode(), headers=headers) assert compact.status_code == spaced.status_code == 201, spaced.text assert compact.json() == spaced.json() assert len(history(api, alice, chat)) == 1 def test_same_key_with_other_body_is_conflict( api: httpx.Client, make_user, make_chat ) -> None: alice = make_user("alice") chat = make_chat(alice, [alice]) key = uuid.uuid4().hex original = post_message(api, alice, chat, "оригинал", key).json() assert_error(post_message(api, alice, chat, "другой", key), 409, "conflict") assert history(api, alice, chat) == [original] @pytest.mark.parametrize( "headers", [ {}, {"Idempotency-Key": ""}, {"Idempotency-Key": "with space"}, {"Idempotency-Key": "ключ".encode()}, {"Idempotency-Key": "a/b"}, {"Idempotency-Key": "x" * 129}, ], ) def test_missing_or_invalid_key_is_rejected( api: httpx.Client, make_user, make_chat, headers: dict ) -> None: alice = make_user("alice") chat = make_chat(alice, [alice]) response = api.post( f"/api/v1/chats/{chat['id']}/messages", json={"text": "без ключа"}, headers={**auth(alice), **headers}, ) assert_error(response, 400, "invalid_request") assert history(api, alice, chat) == [] @pytest.mark.parametrize("key", ["x" * 128, "Az09._:-"]) def test_key_boundaries_are_accepted( api: httpx.Client, make_user, make_chat, key: str ) -> None: alice = make_user("alice") chat = make_chat(alice, [alice]) assert post_message(api, alice, chat, "граница", key).status_code == 201 def test_key_scope_is_user_and_chat(api: httpx.Client, make_user, make_chat) -> None: """Один ключ у разных пользователей и в разных чатах — независимые сообщения""" alice = make_user("alice") bob = make_user("bob") chat = make_chat(alice, [alice, bob]) other_chat = make_chat(alice, [alice]) key = uuid.uuid4().hex from_alice = post_message(api, alice, chat, "текст", key).json() from_bob = post_message(api, bob, chat, "текст", key).json() in_other_chat = post_message(api, alice, other_chat, "текст", key).json() assert len({from_alice["id"], from_bob["id"], in_other_chat["id"]}) == 3 assert from_bob["sender_id"] == bob["id"] assert len(history(api, alice, chat)) == 2 assert len(history(api, alice, other_chat)) == 1 def test_replay_rechecks_session_and_access( api: httpx.Client, make_user, make_chat ) -> None: alice = make_user("alice") eve = make_user("eve") chat = make_chat(alice, [alice]) key = uuid.uuid4().hex post_message(api, alice, chat, "секрет", key) # Чужой с тем же ключом не получает сохранённое сообщение assert_error(post_message(api, eve, chat, "секрет", key), 404, "not_found") # Отозванная сессия не даёт повтора logout = api.delete("/api/v1/auth/sessions/current", headers=auth(alice)) assert logout.status_code == 204 replay = post_message(api, alice, chat, "секрет", key) assert_error(replay, 401, "unauthorized") def test_expired_key_can_be_reused(api: httpx.Client, make_user, make_chat) -> None: """После срока хранения (24 ч) ключ снова создаёт сообщение; срок сдвигается в БД""" alice = make_user("alice") chat = make_chat(alice, [alice]) key = uuid.uuid4().hex first = post_message(api, alice, chat, "вчера", key).json() psql( "UPDATE message_idempotency_keys " "SET created_at = now() - interval '2 days', " "expires_at = now() - interval '1 day' " f"WHERE chat_id = '{chat['id']}' AND idempotency_key = '{key}';" ) second = post_message(api, alice, chat, "сегодня", key) assert second.status_code == 201, second.text assert second.json()["id"] != first["id"] assert [item["text"] for item in history(api, alice, chat)] == ["вчера", "сегодня"] stored = psql( "SELECT count(*) FROM message_idempotency_keys " f"WHERE chat_id = '{chat['id']}' AND idempotency_key = '{key}';" ) assert stored == "1" async def _post_concurrently( base_url: str, user: dict, chat: dict, key: str, texts: list[str] ) -> list[httpx.Response]: """Все запросы стартуют по одному сигналу; отдельные соединения к LB""" start = asyncio.Event() limits = httpx.Limits(max_connections=len(texts)) async with httpx.AsyncClient( base_url=base_url, timeout=30, limits=limits ) as client: async def post(text: str) -> httpx.Response: await start.wait() return await client.post( f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=message_headers(user, key), ) tasks = [asyncio.create_task(post(text)) for text in texts] await asyncio.sleep(0) start.set() return list(await asyncio.gather(*tasks)) def test_parallel_replays_through_replicas_create_one_message( api: httpx.Client, base_url: str, make_user, make_chat ) -> None: alice = make_user("alice") chat = make_chat(alice, [alice]) key = uuid.uuid4().hex responses = asyncio.run( _post_concurrently( base_url, alice, chat, key, ["одно сообщение"] * PARALLEL_REPLAYS ) ) statuses = Counter(response.status_code for response in responses) instances = Counter(response.headers["X-Instance-Id"] for response in responses) bodies = {json.dumps(response.json(), sort_keys=True) for response in responses} items = history(api, alice, chat) rows = psql(f"SELECT count(*) FROM messages WHERE chat_id = '{chat['id']}';") print( "\nPARALLEL SAME KEY:", json.dumps( { "requests": len(responses), "statuses": dict(statuses), "instances": dict(instances), "distinct_bodies": len(bodies), "messages_in_history": len(items), "rows_in_db": int(rows), } ), ) assert statuses == {201: PARALLEL_REPLAYS} assert len(instances) >= 2, "запросы не распределились по репликам" assert len(bodies) == 1 assert len(items) == 1 and rows == "1" def test_parallel_same_key_with_different_bodies( api: httpx.Client, base_url: str, make_user, make_chat ) -> None: """Победитель гонки один; совпавшие с ним тела — 201, остальные — 409""" alice = make_user("alice") chat = make_chat(alice, [alice]) key = uuid.uuid4().hex texts = ["вариант A", "вариант B"] * (PARALLEL_REPLAYS // 2) responses = asyncio.run(_post_concurrently(base_url, alice, chat, key, texts)) items = history(api, alice, chat) assert len(items) == 1 winner = items[0] for text, response in zip(texts, responses): if text == winner["text"]: assert response.status_code == 201, response.text assert response.json() == winner else: assert_error(response, 409, "conflict")