This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import json
|
||||
import base64
|
||||
import uuid
|
||||
import threading
|
||||
|
||||
BASE_URL = "http://localhost:8080"
|
||||
|
||||
def call(method, path, expected_status, headers=None, body=None):
|
||||
url = BASE_URL + path
|
||||
req = urllib.request.Request(url, method=method)
|
||||
if headers:
|
||||
for k, v in headers.items():
|
||||
req.add_header(k, v)
|
||||
if body:
|
||||
req.data = json.dumps(body).encode('utf-8')
|
||||
req.add_header('Content-Type', 'application/json')
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(req) as response:
|
||||
return response.status
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code
|
||||
|
||||
def main():
|
||||
print("=== Тест 1: Гонка регистрации (одновременные запросы) ===")
|
||||
username = f"race_{uuid.uuid4().hex[:6]}"
|
||||
body = {"username": username, "display_name": "Race", "password": "Password123!"}
|
||||
|
||||
results = []
|
||||
def register():
|
||||
results.append(call("POST", "/api/v1/users", None, body=body))
|
||||
|
||||
t1 = threading.Thread(target=register)
|
||||
t2 = threading.Thread(target=register)
|
||||
t1.start(); t2.start()
|
||||
t1.join(); t2.join()
|
||||
|
||||
# Один должен быть 201, второй 409
|
||||
if sorted(results) == [201, 409]:
|
||||
print("PASS: Гонка обработана (один 201, один 409).")
|
||||
else:
|
||||
print(f"FAIL: Ожидали [201, 409], получили {results}")
|
||||
|
||||
print("\n=== Тест 2: Неверный пароль ===")
|
||||
cred = base64.b64encode(f"{username}:WrongPassword".encode()).decode()
|
||||
status = call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": f"Basic {cred}"})
|
||||
if status == 401:
|
||||
print("PASS: Неверный пароль отклонен (401).")
|
||||
else:
|
||||
print(f"FAIL: Ожидали 401, получили {status}")
|
||||
|
||||
print("\n=== Тест 3: Logout ===")
|
||||
cred = base64.b64encode(f"{username}:Password123!".encode()).decode()
|
||||
|
||||
req = urllib.request.Request(BASE_URL + '/api/v1/auth/sessions', method='POST', headers={'Authorization': f'Basic {cred}'})
|
||||
with urllib.request.urlopen(req) as response:
|
||||
token = json.loads(response.read())['token']
|
||||
|
||||
status = call("DELETE", "/api/v1/auth/sessions/current", 204, headers={"Authorization": f"Bearer {token}"})
|
||||
status2 = call("GET", "/api/v1/users/me", 401, headers={"Authorization": f"Bearer {token}"})
|
||||
|
||||
if status == 204 and status2 == 401:
|
||||
print("PASS: Logout работает (204, затем 401).")
|
||||
else:
|
||||
print(f"FAIL: Logout не сработал. Коды: {status}, {status2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user