30 lines
2.0 KiB
Python
30 lines
2.0 KiB
Python
"""Небольшая проверка требований уровня 4."""
|
|
import base64, json, os, threading, time, urllib.error, urllib.request, uuid
|
|
|
|
BASE = os.getenv("BASE_URL", "http://localhost:8080")
|
|
def call(method, path, body=None, headers=None):
|
|
data = json.dumps(body, ensure_ascii=False).encode() if body is not None else None
|
|
h = {"Content-Type":"application/json"} if body is not None else {}; h.update(headers or {})
|
|
req = urllib.request.Request(BASE+path, data=data, headers=h, method=method)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=5) as r: return r.status, json.loads(r.read() or b"{}")
|
|
except urllib.error.HTTPError as e: return e.code, json.loads(e.read() or b"{}")
|
|
def main():
|
|
username = "race_"+uuid.uuid4().hex[:12]; password="Level4-password-123"; body={"username":username,"display_name":"Race","password":password}
|
|
results=[]
|
|
def register(): results.append(call("POST","/api/v1/users",body)[0])
|
|
threads=[threading.Thread(target=register) for _ in range(8)]
|
|
for t in threads:t.start()
|
|
for t in threads:t.join()
|
|
assert results.count(201)==1 and results.count(409)==7, results
|
|
token=base64.b64encode((username+":"+password).encode()).decode(); status,session=call("POST","/api/v1/auth/sessions",headers={"Authorization":"Basic "+token}); assert status==201
|
|
auth={"Authorization":"Bearer "+session["token"]}; assert call("GET","/api/v1/users/me",headers=auth)[0]==200
|
|
rollback_title = "rollback_"+uuid.uuid4().hex[:8]
|
|
assert call("POST", "/api/v1/chats", {"title": rollback_title, "member_ids": ["00000000-0000-4000-8000-000000000000"]}, auth)[0] == 400
|
|
listed = call("GET", "/api/v1/chats", headers=auth)[1]
|
|
assert all(item["title"] != rollback_title for item in listed["items"])
|
|
if int(os.getenv("SESSION_TTL_SECONDS","3600")) <= 3:
|
|
time.sleep(3); assert call("GET","/api/v1/users/me",headers=auth)[0]==401
|
|
print("PASS: registration race and optional short-TTL expiry")
|
|
if __name__ == "__main__": main()
|