Files
mtusi2026lab7-fork/student-tests/revocation_partition.py

39 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Минимальное воспроизведение замечания ревью №1 (отзыв токена при разрыве с Redis).
Прогрев кэша -> разрыв сети API<->Redis без рестарта Redis -> logout ->
восстановление связи -> 8 с опроса старого токена (дольше circuit breaker).
Ожидается только 401. По умолчанию — команды docker compose-стенда:
python student-tests/revocation_partition.py --base-url https://localhost:8443
"""
import tlsenv # noqa: F401 lab7: HTTPS с проверкой CA стенда
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="https://localhost:8443")
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)