forked from mtusi2026/mtusi2026lab3
Реализовать лабораторную работу 3
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
"""Reactions are durable, private and idempotent across the actual API replicas."""
|
||||
import argparse
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from compose_helpers import sql
|
||||
from client import Client, check, run
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--base-url", default="http://127.0.0.1:18080")
|
||||
args = parser.parse_args()
|
||||
client = Client(args.base_url)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
message = client.call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, actor=alice,
|
||||
body={"text": "Реакции через разные API"}, headers={"Idempotency-Key": str(uuid.uuid4())}, schema="Message")["json"]
|
||||
route = f"/api/ui/chats/{chat['id']}/messages/{message['id']}/reactions/like"
|
||||
summary = f"/api/ui/chats/{chat['id']}/reactions?message_ids={message['id']}"
|
||||
|
||||
def items(actor):
|
||||
return json.loads(client.call("GET", summary, actor=actor)["bytes"])["items"]
|
||||
|
||||
client.call("PUT", route, 401)
|
||||
client.call("GET", summary, 401)
|
||||
client.call("PUT", route, 404, actor=eve)
|
||||
client.call("GET", summary, 404, actor=eve)
|
||||
instances = set()
|
||||
for _ in range(8):
|
||||
response = client.call("PUT", route, 204, actor=alice)
|
||||
instances.add(response["headers"]["X-Instance-Id"])
|
||||
check(len(instances) >= 2, "writes did not reach two API instances")
|
||||
check(items(alice)[0]["count"] == 1 and items(alice)[0]["mine"], "repeated PUT counted twice")
|
||||
check(not items(bob)[0]["mine"], "mine flag leaked between users")
|
||||
client.call("PUT", route, 204, actor=bob)
|
||||
check(items(alice)[0]["count"] == 2, "two users did not aggregate")
|
||||
check(int(sql(f"SELECT count(*) FROM message_reactions WHERE message_id = '{message['id']}';")) == 2,
|
||||
"reactions were not persisted")
|
||||
client.call("DELETE", route, 204, actor=alice)
|
||||
client.call("DELETE", route, 204, actor=alice)
|
||||
check(items(alice)[0]["count"] == 1 and not items(alice)[0]["mine"], "DELETE affected another user's reaction")
|
||||
client.call("PUT", route.rsplit('/', 1)[0] + "/invalid", 400, actor=alice)
|
||||
missing = route.replace(message["id"], str(uuid.uuid4()))
|
||||
client.call("PUT", missing, 404, actor=alice)
|
||||
other = client.call("POST", "/api/v1/chats", 201, actor=alice,
|
||||
body={"title": "Другая комната", "member_ids": [alice["id"]]}, schema="Chat")["json"]
|
||||
client.call("PUT", route.replace(chat["id"], other["id"]), 404, actor=alice)
|
||||
mixed = json.loads(client.call("GET", summary.replace(chat["id"], other["id"]), actor=alice)["bytes"])
|
||||
check(mixed["items"] == [], "foreign message reaction escaped its chat")
|
||||
client.call("GET", summary.rsplit('=',1)[0] + '=bad', 400, actor=alice)
|
||||
client.call("GET", "/api/ui/cluster", 401)
|
||||
cluster = json.loads(client.call("GET", "/api/ui/cluster", actor=alice)["bytes"])
|
||||
check(cluster["redis_available"] and cluster["postgres_available"], "cluster dependency snapshot failed")
|
||||
check(sum(node["ready"] for node in cluster["instances"]) >= 2, "missing actual cluster heartbeats")
|
||||
print("PASS: two-replica reaction writes, idempotency, user isolation, chat privacy, PostgreSQL persistence, cluster heartbeat")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user