chore: carry lab 3 over
- carry the lab 3 solution over from mtusi2026lab3-safronov (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests, bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts, Dockerfile, compose, pyproject and uv lock - merge Makefile, .gitignore and .dockerignore (the lab 3 versions only add lines to the skeleton, which is the same in lab 4) and .env.example (lab 4 skeleton names listed in the header) - rename the compose project to mtusi-lab4 and the image to :lab4 so the stand does not reuse the lab 3 containers and volumes - update AGENTS.md for lab 4: current lab, origin commit, contract and test files of the new skeleton Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,315 @@
|
||||
"""Спек `idempotency`: повторная отправка сообщения с `Idempotency-Key`.
|
||||
|
||||
Сценарии, которым нужен особый стенд (сбой записи, остановка БД, короткий
|
||||
срок ключа), — в `test_failures.py`; гонка одного ключа — в
|
||||
`test_race.py`; повтор после рестарта API — исключение с evidence.
|
||||
Повтору через другую реплику нужны ≥ 2 реплики за `BASE_URL` (фикстура
|
||||
`replicas`), против одного процесса он пропускается.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
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
|
||||
|
||||
|
||||
@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"]]
|
||||
Reference in New Issue
Block a user