Files
mtusi2026lab4-fork/student-tests/revocation_partition.py
T

38 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Минимальное воспроизведение замечания ревью №1 (отзыв токена при разрыве с Redis).
Прогрев кэша -> разрыв сети API<->Redis без рестарта Redis -> logout ->
восстановление связи -> 8 с опроса старого токена (дольше circuit breaker).
Ожидается только 401. По умолчанию — команды docker compose-стенда:
python student-tests/revocation_partition.py --base-url http://127.0.0.1:8080
"""
import argparse, shlex, subprocess, sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import common
from common import actor, request
p = argparse.ArgumentParser()
p.add_argument("--base-url", default="http://127.0.0.1:8080")
p.add_argument("--partition", default="docker network disconnect messenger-lab3_backend messenger-lab3-redis-1")
p.add_argument("--heal", default="docker network connect --alias redis messenger-lab3_backend messenger-lab3-redis-1")
args = p.parse_args()
common.BASE_URL = args.base_url.rstrip("/")
ROOT = Path(__file__).resolve().parents[1]
def run(cmd):
subprocess.run(shlex.split(cmd), cwd=ROOT, check=True)
a = actor("repro"); t = a["_token"]
for _ in range(2): request("GET","/api/v1/users/me",token=t)
run(args.partition)
try:
print("logout during partition:", request("DELETE","/api/v1/auth/sessions/current",token=t).status)
finally:
run(args.heal)
seen=set(); end=time.monotonic()+8
while time.monotonic()<end:
seen.add(request("GET","/api/v1/users/me",token=t).status); time.sleep(0.25)
print("revoked token statuses over 8 s after heal:", sorted(seen))
ok = seen == {401}
print("PASS" if ok else "FAIL", ": revoked token never accepted after partition heals")
sys.exit(0 if ok else 1)