Files
mtusi2026lab7/student-tests/rate_limit.py
T
wanderinho 7ee5192cbd
Skeleton contracts / contracts (pull_request) Blocked by required conditions
ЛР7 - БПИ2303 - Стругов - на 4
2026-12-06 23:11:49 +03:00

35 lines
1.2 KiB
Python

"""Verify the shared login rate limit on an idle, disposable two-replica lab."""
import base64
from pathlib import Path
import sys
import uuid
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, run
def main():
client = Client(ca_file=str(ROOT / 'secrets/pki/ca.crt'))
actor = client.user('limit')
username = actor['registration']['username']
wrong = base64.b64encode(f'{username}:invalid-{uuid.uuid4().hex}'.encode()).decode()
responders = set()
refused = set()
for _ in range(72):
response = client.call('POST', '/api/v1/auth/sessions', [401, 429],
headers={'Authorization': 'Basic ' + wrong})
instance = response['headers']['X-Instance-Id']
responders.add(instance)
if response['status'] == 429:
assert int(response['headers']['Retry-After']) > 0
refused.add(instance)
if len(responders) >= 2 and len(refused) >= 2:
print('PASS: shared login budget and Retry-After on both replicas')
return
raise AssertionError('the two replicas did not both enforce the shared ceiling')
if __name__ == '__main__':
run(main)