Files
mtusi2026lab7/tests/test_login_rate_limit.py
T
sinveria 6b5461a6b4
Skeleton contracts / contracts (pull_request) Canceled after 0s
Implement Lab 7 security and infrastructure
2026-11-22 11:55:09 +03:00

56 lines
1.3 KiB
Python

#!/usr/bin/env python3
import base64
import uuid
from client import Client, arguments, check, run
def main():
args = arguments("Login rate limit security test")
client = Client(args.base_url, args.ca_file)
username = "rate_" + uuid.uuid4().hex[:12]
password = "Correct-" + uuid.uuid4().hex
client.call(
"POST",
"/api/v1/users",
201,
body={
"username": username,
"display_name": "Rate limit test",
"password": password,
},
schema="User",
)
wrong = base64.b64encode(
f"{username}:Wrong-{uuid.uuid4().hex}".encode()
).decode()
for attempt in range(5):
client.call(
"POST",
"/api/v1/auth/sessions",
401,
headers={"Authorization": "Basic " + wrong},
)
blocked = client.call(
"POST",
"/api/v1/auth/sessions",
429,
headers={"Authorization": "Basic " + wrong},
)
retry_after = blocked["headers"].get("Retry-After")
check(retry_after is not None, "429 response has no Retry-After")
check(int(retry_after) > 0, "Retry-After must be positive")
print("PASS: login rate limit returns 429 and Retry-After")
if __name__ == "__main__":
run(main)