"""Спек `idempotency`: повторная отправка сообщения с `Idempotency-Key`. Сценарии, которым нужен особый стенд (сбой записи, остановка БД, короткий срок ключа), — в `test_failures.py`; гонка одного ключа — в `test_race.py`; повтор после рестарта API — исключение с evidence. Повтору через другую реплику нужны ≥ 2 реплики за `BASE_URL` (фикстура `replicas`), против одного процесса он пропускается. """ from typing import Any import pytest import images from api_client import ( API, IDEMPOTENCY_KEY, Actor, Api, key_headers, new_key, ) from contract import ( BEARER_CHALLENGE, assert_error, assert_instance_id, assert_ok, assert_replicas, assert_unauthorized, ) # 128 символов, в том числе все допустимые знаки, кроме букв и цифр LONGEST_KEY = "a1.B_:-Z" * 16 def texts(api: Api, actor: Actor, chat_id: str) -> list[str]: return [item["text"] for item in api.all_messages(actor, chat_id)] @pytest.mark.spec("idempotency", "Нет ключа") def test_no_key(api: Api, alice: Actor, chat: dict[str, Any]) -> None: resp = api.post_message(alice, chat["id"], key=None, json={"text": "x"}) assert_error(resp, 400) assert texts(api, alice, chat["id"]) == [] @pytest.mark.spec("idempotency", "Неверный ключ") def test_bad_key(api: Api, alice: Actor, chat: dict[str, Any]) -> None: for key in ("", "a" * 129, "a b", "a/b", "ключ"): # не-ASCII заголовок httpx шлёт только байтами resp = api.post_message( alice, chat["id"], key=None, headers={IDEMPOTENCY_KEY: key.encode()}, json={"text": "x"}, ) assert_error(resp, 400) if key: assert key not in resp.text, f"key {key!r} echoed in response" assert texts(api, alice, chat["id"]) == [] @pytest.mark.spec("idempotency", "Граница длины ключа") def test_longest_key(api: Api, alice: Actor, chat: dict[str, Any]) -> None: assert len(LONGEST_KEY) == 128 message = api.send_message( alice, chat["id"], "длинный ключ", key=LONGEST_KEY, ) assert texts(api, alice, chat["id"]) == [message["text"]] @pytest.mark.spec("idempotency", "Ключ у других запросов") def test_key_on_other_requests(api: Api, alice: Actor, bob: Actor) -> None: headers = key_headers() created = [ assert_ok( api.request( "POST", f"{API}/chats", actor=alice, headers=headers, json={ "title": "Тот же ключ", "member_ids": [alice.id, bob.id], }, ), 201, "Chat", ) for _ in range(2) ] assert created[0]["id"] != created[1]["id"] @pytest.mark.spec("idempotency", "Повтор того же тела") def test_replay_same_body( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() first = api.send_message(alice, chat["id"], "Привет, мир 👋", key=key) again = api.send_message(alice, chat["id"], "Привет, мир 👋", key=key) assert again == first assert texts(api, alice, chat["id"]) == ["Привет, мир 👋"] @pytest.mark.spec("idempotency", "Тот же JSON в другой записи") def test_replay_other_json_spelling( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() first = api.send_message(alice, chat["id"], "Привет", key=key) # те же поле и текст: пробелы и \u-экранирование вместо UTF-8 raw = b'{ "text" :\n "\\u041f\\u0440\\u0438\\u0432\\u0435\\u0442" }' resp = api.post_message( alice, chat["id"], key=key, headers={"Content-Type": "application/json"}, content=raw, ) assert assert_ok(resp, 201, "Message") == first assert texts(api, alice, chat["id"]) == ["Привет"] @pytest.mark.spec("idempotency", "Другой текст") def test_other_body_conflict( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() first = api.send_message(alice, chat["id"], "первое", key=key) resp = api.post_message( alice, chat["id"], key=key, json={"text": "второе"}, ) assert_error(resp, 409) assert texts(api, alice, chat["id"]) == ["первое"] assert api.send_message(alice, chat["id"], "первое", key=key) == first def two_attachments(api: Api, actor: Actor, chat_id: str) -> list[str]: return [ api.upload(actor, chat_id, images.sample_png())["id"] for _ in range(2) ] @pytest.mark.spec("idempotency", "Вложения по умолчанию") def test_replay_with_default_attachments( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() first = api.send_message(alice, chat["id"], "без файлов", key=key) again = api.send_message( alice, chat["id"], "без файлов", key=key, attachment_ids=[], ) assert again == first assert texts(api, alice, chat["id"]) == ["без файлов"] @pytest.mark.spec("idempotency", "Повтор сообщения с вложениями") def test_replay_with_attachments( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: first_id, second_id = two_attachments(api, alice, chat["id"]) key = new_key() ids = [first_id, second_id] first = api.send_message( alice, chat["id"], "файлы", key=key, attachment_ids=ids, ) again = api.send_message( alice, chat["id"], "файлы", key=key, attachment_ids=ids, ) upper = api.send_message( alice, chat["id"], "файлы", key=key, attachment_ids=[item.upper() for item in ids], ) assert first == again == upper assert first["attachment_ids"] == ids history = api.all_messages(alice, chat["id"]) assert [item["id"] for item in history] == [first["id"]] @pytest.mark.spec("idempotency", "Другие вложения") def test_other_attachments_conflict( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: first_id, second_id = two_attachments(api, alice, chat["id"]) key = new_key() first = api.send_message( alice, chat["id"], "файлы", key=key, attachment_ids=[first_id, second_id], ) for body in ( {"text": "файлы", "attachment_ids": [second_id, first_id]}, {"text": "файлы"}, ): assert_error( api.post_message(alice, chat["id"], key=key, json=body), 409, ) history = api.all_messages(alice, chat["id"]) assert history == [first] assert history[0]["attachment_ids"] == [first_id, second_id] @pytest.mark.spec("idempotency", "Другой пользователь") def test_same_key_other_user( api: Api, alice: Actor, bob: Actor, chat: dict[str, Any], ) -> None: key = new_key() from_alice = api.send_message(alice, chat["id"], "от Алисы", key=key) from_bob = api.send_message(bob, chat["id"], "от Боба", key=key) assert from_alice["id"] != from_bob["id"] assert sorted(texts(api, alice, chat["id"])) == ["от Алисы", "от Боба"] @pytest.mark.spec("idempotency", "Другой чат") def test_same_key_other_chat( api: Api, alice: Actor, bob: Actor, chat: dict[str, Any], ) -> None: other = api.create_chat(alice, bob, title="Второй чат") key = new_key() first = api.send_message(alice, chat["id"], "одно и то же", key=key) second = api.send_message(alice, other["id"], "одно и то же", key=key) assert first["id"] != second["id"] assert second["chat_id"] == other["id"] for chat_id in (chat["id"], other["id"]): assert texts(api, alice, chat_id) == ["одно и то же"] @pytest.mark.spec("idempotency", "Отозванный токен") def test_replay_with_revoked_token( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() api.send_message(alice, chat["id"], "до выхода", key=key) assert api.logout(alice).status_code == 204 resp = api.post_message( alice, chat["id"], key=key, json={"text": "до выхода"}, ) assert_unauthorized(resp, BEARER_CHALLENGE) @pytest.mark.spec("idempotency", "Новая сессия того же пользователя") def test_replay_with_new_session( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() first = api.send_message(alice, chat["id"], "одна сессия", key=key) again = api.login(alice.user, alice.password) assert again.token != alice.token assert api.send_message(again, chat["id"], "одна сессия", key=key) == first @pytest.mark.spec("idempotency", "Посторонний с чужим ключом") def test_outsider_with_members_key( api: Api, alice: Actor, eve: Actor, chat: dict[str, Any], ) -> None: key = new_key() first = api.send_message(alice, chat["id"], "только участникам", key=key) resp = api.post_message( eve, chat["id"], key=key, json={"text": "только участникам"}, ) assert_error(resp, 404) assert ( api.send_message( alice, chat["id"], "только участникам", key=key, ) == first ) assert texts(api, alice, chat["id"]) == ["только участникам"] @pytest.mark.spec("idempotency", "Без сессии и без ключа") def test_no_session_and_no_key(api: Api, chat: dict[str, Any]) -> None: resp = api.post_message(None, chat["id"], key=None, json={"text": "x"}) assert_unauthorized(resp, BEARER_CHALLENGE) @pytest.mark.spec("idempotency", "Посторонний без ключа") def test_outsider_without_key( api: Api, eve: Actor, chat: dict[str, Any], ) -> None: resp = api.post_message(eve, chat["id"], key=None, json={"text": "x"}) assert_error(resp, 400) @pytest.mark.spec("idempotency", "Невалидное тело с занятым ключом") def test_invalid_body_with_used_key( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() api.send_message(alice, chat["id"], "первое", key=key) resp = api.post_message(alice, chat["id"], key=key, json={"text": ""}) assert_error(resp, 400) @pytest.mark.spec("idempotency", "Невалидный запрос не занимает ключ") def test_invalid_request_keeps_key_free( api: Api, alice: Actor, chat: dict[str, Any], ) -> None: key = new_key() resp = api.post_message(alice, chat["id"], key=key, json={"text": ""}) assert_error(resp, 400) message = api.send_message(alice, chat["id"], "теперь валидно", key=key) assert texts(api, alice, chat["id"]) == [message["text"]] @pytest.mark.spec("idempotency", "Повтор через другую реплику") def test_replay_on_other_replica( api: Api, alice: Actor, chat: dict[str, Any], replicas: int, ) -> None: """Повтор с тем же ключом, который обслужила другая реплика, — тот же `Message`; другое тело с этим ключом — 409 от разных реплик.""" key = new_key() body = {"text": "повтор через реплики"} resp = api.post_message(alice, chat["id"], key=key, json=body) first = assert_ok(resp, 201, "Message") origin = assert_instance_id(resp) replays: dict[str, Any] = {} for _ in range(2 * replicas): resp = api.post_message(alice, chat["id"], key=key, json=body) replays[assert_instance_id(resp)] = assert_ok(resp, 201, "Message") if replays.keys() - {origin}: break print(f"sent via {origin}, replayed via {sorted(replays)}") # повтор через другую реплику: среди отправки и повторов ≥ 2 значения assert_replicas({origin, *replays}, replicas) assert all(message == first for message in replays.values()) conflicts = set() for _ in range(2 * replicas): resp = api.post_message(alice, chat["id"], key=key, json={"text": "?"}) assert_error(resp, 409) conflicts.add(assert_instance_id(resp)) print(f"409 via {sorted(conflicts)}") assert_replicas(conflicts, replicas) assert texts(api, alice, chat["id"]) == [body["text"]]