Files
mtusi2026lab3/student-tests/test_idempotency.py
DaniilKuznetsovandЮгай Артур Рустамович 09aece58fe ЛР3 на 3
Co-authored-by: Югай Артур Рустамович <f1ekserr@yandex.ru>
2026-11-16 22:23:04 +03:00

141 lines
7.5 KiB
Python

from concurrent.futures import ThreadPoolExecutor
import base64
import json
import os
from pathlib import Path
import subprocess
import sys
import unittest
from uuid import uuid4
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from helpers import Client, project_name
class IdempotencyTests(unittest.TestCase):
def setUp(self):
self.client = Client(os.environ.get("BASE_URL", "http://127.0.0.1:18103"))
self.alice, self.bob, self.eve, self.chat = self.client.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"] + "/messages"
def history(self):
return self.client.call("GET", self.path + "?limit=100", actor=self.alice, schema="MessagePage")["json"]["items"]
def sql(self, statement):
project = project_name()
command = ["docker", "compose", "--project-name", project, "--env-file", ".env",
"exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
"-v", "ON_ERROR_STOP=1", "-At", "-c", statement]
result = subprocess.run(command, cwd=ROOT, capture_output=True, text=True, encoding="utf-8")
if result.returncode:
raise AssertionError("Database test command failed")
return result.stdout.strip()
def test_key_required_and_format(self):
for key in (None, "", "two words", "a/b", "é", "a" * 129):
headers = {} if key is None else {"Idempotency-Key": key}
self.client.call("POST", self.path, 400, actor=self.alice, body={"text": "test"}, headers=headers)
for key in ("a", ".:_-", "a" * 128):
self.client.message(self.alice, self.chat, key, key=key)
self.assertEqual(len(self.history()), 3)
def test_replay_normalizes_json(self):
key = uuid4().hex
text = "Привет 👋"
first = self.client.message(self.alice, self.chat, text, key=key)
raw = json.dumps({"text": text}, ensure_ascii=True, indent=4).encode("ascii")
second = self.client.call("POST", self.path, 201, actor=self.alice, raw=raw,
headers={"Idempotency-Key": key, "Content-Type": "application/json"}, schema="Message")
self.assertEqual(first["json"], second["json"])
self.assertNotEqual(first["headers"]["X-Request-Id"], second["headers"]["X-Request-Id"])
self.assertEqual(len(self.history()), 1)
def test_different_body_conflicts(self):
key = uuid4().hex
original = self.client.message(self.alice, self.chat, "first", key=key)["json"]
self.client.call("POST", self.path, 409, actor=self.alice,
body={"text": "second"}, headers={"Idempotency-Key": key})
self.assertEqual(self.client.message(self.alice, self.chat, "first", key=key)["json"], original)
self.assertEqual(self.history(), [original])
def test_twenty_concurrent_requests_use_both_replicas(self):
key = uuid4().hex
def send(_):
return Client(self.client.base).message(self.alice, self.chat, "one message", key=key)
with ThreadPoolExecutor(max_workers=20) as pool:
responses = list(pool.map(send, range(20)))
self.assertEqual(len({item["json"]["id"] for item in responses}), 1)
self.assertEqual(len({item["headers"]["X-Instance-Id"] for item in responses}), 2)
self.assertTrue(all(item["json"] == responses[0]["json"] for item in responses))
self.assertEqual(self.history(), [responses[0]["json"]])
def test_scope_is_user_and_chat_not_session(self):
key = uuid4().hex
first = self.client.message(self.alice, self.chat, "shared", key=key)["json"]
second = self.client.message(self.bob, self.chat, "shared", key=key)["json"]
other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice,
body={"title": "other", "member_ids": [self.alice["id"]]}, schema="Chat")["json"]
third = self.client.message(self.alice, other, "shared", key=key)["json"]
self.assertEqual(len({first["id"], second["id"], third["id"]}), 3)
source = self.alice["registration"]
value = base64.b64encode((source["username"] + ":" + source["password"]).encode()).decode()
session = self.client.call("POST", "/api/v1/auth/sessions", 201,
headers={"Authorization": "Basic " + value}, schema="Session")["json"]
actor = {"id": self.alice["id"], "token": session["token"]}
self.assertEqual(self.client.message(actor, self.chat, "shared", key=key)["json"], first)
def test_replay_still_checks_access_and_session(self):
key = uuid4().hex
self.client.message(self.alice, self.chat, "private", key=key)
self.client.call("POST", self.path, 404, actor=self.eve,
body={"text": "private"}, headers={"Idempotency-Key": key})
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice)
self.client.call("POST", self.path, 401, actor=self.alice,
body={"text": "private"}, headers={"Idempotency-Key": key})
def test_failure_rolls_back_message_and_key(self):
key = "fault-" + uuid4().hex
constraint = "test_reject_" + uuid4().hex[:12]
self.sql("ALTER TABLE idempotency_keys ADD CONSTRAINT " + constraint
+ " CHECK(request_key <> '" + key + "') NOT VALID")
try:
self.client.call("POST", self.path, 500, actor=self.alice,
body={"text": "rollback"}, headers={"Idempotency-Key": key})
self.assertEqual(self.history(), [])
self.assertEqual(self.sql("SELECT count(*) FROM idempotency_keys WHERE request_key='" + key + "'"), "0")
finally:
self.sql("ALTER TABLE idempotency_keys DROP CONSTRAINT " + constraint)
result = self.client.message(self.alice, self.chat, "rollback", key=key)["json"]
self.assertEqual(self.client.message(self.alice, self.chat, "rollback", key=key)["json"], result)
self.assertEqual(self.history(), [result])
def test_retention_and_expired_key_reuse(self):
key = uuid4().hex
first = self.client.message(self.alice, self.chat, "old", key=key)["json"]
retained = self.sql("SELECT expires_at-created_at > INTERVAL '23 hours 59 minutes 59 seconds'"
+ " FROM idempotency_keys WHERE request_key='" + key + "'")
self.assertEqual(retained, "t")
self.sql("UPDATE idempotency_keys SET expires_at=created_at + INTERVAL '1 microsecond'"
+ " WHERE request_key='" + key + "'")
second = self.client.message(self.alice, self.chat, "new", key=key)["json"]
self.assertNotEqual(first["id"], second["id"])
self.assertEqual(self.history(), [first, second])
def test_same_session_on_both_instances(self):
responses = [self.client.call("GET", "/api/v1/users/me", actor=self.alice, schema="User")
for _ in range(12)]
self.assertEqual(len({item["headers"]["X-Instance-Id"] for item in responses}), 2)
self.assertTrue(all(item["json"] == self.alice["user"] for item in responses))
def test_invalid_body_does_not_reserve_key(self):
key = uuid4().hex
self.client.call("POST", self.path, 400, actor=self.alice,
body={"text": ""}, headers={"Idempotency-Key": key})
result = self.client.message(self.alice, self.chat, "fixed", key=key)["json"]
self.assertEqual(self.history(), [result])
if __name__ == "__main__":
unittest.main(verbosity=2)