Files
mtusi2026lab7/tests/rate_limit_grade3.py
T
d1frend cc41f601e3
Skeleton contracts / contracts (pull_request) Canceled after 0s
на оценку 4
2026-11-22 23:33:11 +03:00

32 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Verify that login throttling is shared by both API replicas."""
import base64
from client import Client, arguments, check, run
def main():
args = arguments("Grade 3 shared login rate-limit check")
client = Client(args.base_url, args.ca_file)
actor = client.user("ratelimit")
username = actor["registration"]["username"]
bad = base64.b64encode((username + ":definitely-wrong").encode()).decode()
headers = {"Authorization": "Basic " + bad}
instances = set()
for _ in range(5):
result = client.call("POST", "/api/v1/auth/sessions", 401, headers=headers)
instances.add(result["headers"]["X-Instance-Id"])
blocked = client.call("POST", "/api/v1/auth/sessions", 429, headers=headers)
instances.add(blocked["headers"]["X-Instance-Id"])
retry_after = blocked["headers"].get("Retry-After", "")
check(retry_after.isdigit() and int(retry_after) > 0, "invalid Retry-After")
check(blocked["json"]["error"]["code"] == "rate_limited", "wrong 429 error code")
check(len(instances) >= 2, "requests did not traverse both API replicas")
print("PASS: login limit is shared across replicas and returns 429 with Retry-After.")
if __name__ == "__main__":
run(main)