import json import socket import threading import time import urllib.parse from collections import Counter from concurrent.futures import ThreadPoolExecutor from common import BASE_URL, call, check, new_chat, new_key, new_user, read_history, run_tests, send PARALLEL = 20 def post_raw(user, chat_id, raw, key): """POST с телом «как есть» (bytes) — чтобы проверить другой порядок ключей и пробелы.""" return call("POST", f"/api/v1/chats/{chat_id}/messages", (201, 409, 400), token=user["token"], raw=raw, headers={"Idempotency-Key": key}) def texts_in(user, chat_id): history, _ = read_history(user, chat_id) return [message["text"] for message in history] def test_replay_returns_same_message(): alice, bob = new_user("alice"), new_user("bob") chat = new_chat(alice, bob) key = new_key() first = send(alice, chat["id"], "Привет 👋", key=key) # То же JSON-значение, записанное иначе: пробелы и \u-экранирование. same = post_raw(alice, chat["id"], b'{ "text" : "\\u041f\\u0440\\u0438\\u0432\\u0435\\u0442 \\ud83d\\udc4b" }', key) again = send(alice, chat["id"], "Привет 👋", key=key) print(f"first -> {first.status}; same JSON written differently -> {same.status}; plain retry -> {again.status}") check(same.status == 201 and same.json == first.json and again.json == first.json, "retry must return the original 201 and the same message") check(texts_in(bob, chat["id"]).count("Привет 👋") == 1, "retry created a duplicate") print("PASS: a retry with the same key returns the original 201 and message, no duplicate") def test_same_key_parallel_through_replicas(): alice, bob = new_user("alice"), new_user("bob") chat = new_chat(alice, bob) key = new_key() barrier = threading.Barrier(PARALLEL) def post(_): barrier.wait() # все запросы стартуют одновременно return send(alice, chat["id"], "one message for 20 requests", key=key) with ThreadPoolExecutor(max_workers=PARALLEL) as pool: replies = list(pool.map(post, range(PARALLEL))) bodies = {json.dumps(reply.json, sort_keys=True) for reply in replies} served = Counter(reply.instance for reply in replies) count = texts_in(bob, chat["id"]).count("one message for 20 requests") print(f"{PARALLEL} parallel POSTs with one key: statuses {dict(Counter(r.status for r in replies))}, " f"replicas {dict(served)}, different bodies in answers: {len(bodies)}, messages in history: {count}") check(len(served) >= 2, "requests did not go through different replicas") check(len(bodies) == 1 and count == 1, "parallel retries created more than one message") print("PASS: parallel requests with one key through different replicas create exactly one message") def test_same_key_different_bodies_parallel(): alice = new_user("alice") chat = new_chat(alice) key = new_key() barrier = threading.Barrier(PARALLEL) def post(index): text = "body A" if index % 2 == 0 else "body B" barrier.wait() reply = call("POST", f"/api/v1/chats/{chat['id']}/messages", (201, 409), token=alice["token"], body={"text": text}, headers={"Idempotency-Key": key}) return text, reply with ThreadPoolExecutor(max_workers=PARALLEL) as pool: results = list(pool.map(post, range(PARALLEL))) texts = texts_in(alice, chat["id"]) winner = [t for t in texts if t in ("body A", "body B")] print(f"{PARALLEL} parallel POSTs, one key, two different bodies: " f"{dict(Counter((t, r.status) for t, r in results))}; saved: {winner}") check(len(winner) == 1, "exactly one message must be created") for text, reply in results: expected = 201 if text == winner[0] else 409 check(reply.status == expected, f"{text}: expected {expected}, got {reply.status}") print("PASS: the first body wins, the same body gets 201, the other body gets 409") def test_key_scope(): alice, bob = new_user("alice"), new_user("bob") chat = new_chat(alice, bob) other = new_chat(alice, bob) key = new_key() a1 = send(alice, chat["id"], "scope", key=key).json b1 = send(bob, chat["id"], "scope", key=key).json a2 = send(alice, other["id"], "scope", key=key).json print(f"one key: alice in chat 1, bob in chat 1, alice in chat 2 -> {len({a1['id'], b1['id'], a2['id']})} messages") check(len({a1["id"], b1["id"], a2["id"]}) == 3, "a key must be scoped to user + chat") call("POST", f"/api/v1/chats/{chat['id']}/messages", 409, token=alice["token"], body={"text": "other text"}, headers={"Idempotency-Key": key}) print("PASS: the key is scoped to the current user and chat; another body with a used key -> 409") def test_key_validation(): alice = new_user("alice") chat = new_chat(alice) path = f"/api/v1/chats/{chat['id']}/messages" call("POST", path, 400, token=alice["token"], body={"text": "no key"}) for bad in ("", "a" * 129, "has space", "slash/inside", "ключ", "quote\"", "a+b"): call("POST", path, 400, token=alice["token"], body={"text": "bad key"}, headers={"Idempotency-Key": bad.encode("utf-8").decode("latin-1")}) # как есть, байтами UTF-8 longest = ("Az09._:-" * 16)[:128] call("POST", path, 201, token=alice["token"], body={"text": "longest key"}, headers={"Idempotency-Key": longest}) print("missing, empty, 129 chars, space, '/', non-ASCII, '\"', '+' -> 400; 128 chars of A-Z a-z 0-9 . _ : - -> 201") print("PASS: Idempotency-Key is required and checked: [A-Za-z0-9._:-]{1,128}") def test_failed_request_does_not_burn_key(): alice, eve = new_user("alice"), new_user("eve") chat = new_chat(alice) key = new_key() path = f"/api/v1/chats/{chat['id']}/messages" call("POST", path, 400, token=alice["token"], body={"text": " "}, headers={"Idempotency-Key": key}) call("POST", path, 404, token=eve["token"], body={"text": "not my chat"}, headers={"Idempotency-Key": key}) created = call("POST", path, 201, token=alice["token"], body={"text": "valid now"}, headers={"Idempotency-Key": key}) print(f"key used by a 400 and by a 404 first, then valid request -> {created.status}") print("PASS: rejected requests (400, 404) do not store a result for the key") def test_replay_checks_session_and_access(): alice, eve = new_user("alice"), new_user("eve") chat = new_chat(alice) key = new_key() send(alice, chat["id"], "private", key=key) stolen = call("POST", f"/api/v1/chats/{chat['id']}/messages", 404, token=eve["token"], body={"text": "private"}, headers={"Idempotency-Key": key}) call("DELETE", "/api/v1/auth/sessions/current", 204, token=alice["token"]) revoked = call("POST", f"/api/v1/chats/{chat['id']}/messages", 401, token=alice["token"], body={"text": "private"}, headers={"Idempotency-Key": key}) print(f"same key and body by an outsider -> {stolen.status}; by alice after logout -> {revoked.status}") print("PASS: a retry checks the session and chat access again, the saved answer is not leaked") def test_lost_response_then_retry(): """Клиент отправил POST и не дождался ответа (оборвал соединение). Повтор с тем же ключом безопасен.""" alice = new_user("alice") chat = new_chat(alice) key = new_key() body = json.dumps({"text": "answer was lost"}).encode("utf-8") url = urllib.parse.urlsplit(BASE_URL) request = (f"POST /api/v1/chats/{chat['id']}/messages HTTP/1.1\r\nHost: {url.netloc}\r\n" f"Authorization: Bearer {alice['token']}\r\nIdempotency-Key: {key}\r\n" f"Content-Type: application/json\r\nContent-Length: {len(body)}\r\nConnection: close\r\n\r\n").encode() with socket.create_connection((url.hostname, url.port or 80), timeout=5) as connection: connection.sendall(request + body) # и сразу закрываем, ответ не читаем time.sleep(1) retry = call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, token=alice["token"], body={"text": "answer was lost"}, headers={"Idempotency-Key": key}) count = texts_in(alice, chat["id"]).count("answer was lost") print(f"POST sent, connection closed without reading the answer; retry -> {retry.status}; messages: {count}") check(count == 1, "the retry after a lost answer created a duplicate") print("PASS: an unconfirmed POST can be safely retried with the same key") if __name__ == "__main__": run_tests([ test_replay_returns_same_message, test_same_key_parallel_through_replicas, test_same_key_different_bodies_parallel, test_key_scope, test_key_validation, test_failed_request_does_not_burn_key, test_replay_checks_session_and_access, test_lost_response_then_retry, ])