forked from mtusi2026/mtusi2026lab3
lab3: nginx, replicas, idempotency, tests
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import uuid
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8080"
|
||||
|
||||
|
||||
def register_and_login(username, password="valid_password_123"):
|
||||
requests.post(f"{BASE_URL}/api/v1/users", json={
|
||||
"username": username,
|
||||
"display_name": "Test",
|
||||
"password": password,
|
||||
})
|
||||
resp = requests.post(f"{BASE_URL}/api/v1/auth/sessions", auth=(username, password))
|
||||
assert resp.status_code == 201, f"Логин не удался: {resp.text}"
|
||||
return resp.json()["token"]
|
||||
|
||||
|
||||
def create_chat(token):
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
me = requests.get(f"{BASE_URL}/api/v1/users/me", headers=headers).json()
|
||||
resp = requests.post(f"{BASE_URL}/api/v1/chats", headers=headers, json={
|
||||
"title": "Test Chat",
|
||||
"member_ids": [me["id"]],
|
||||
})
|
||||
assert resp.status_code == 201, f"Чат не создан: {resp.text}"
|
||||
return resp.json()["id"]
|
||||
|
||||
|
||||
def test_idempotency_same_body_returns_same_id():
|
||||
token = register_and_login("test_idem_same")
|
||||
chat_id = create_chat(token)
|
||||
key = str(uuid.uuid4())
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Idempotency-Key": key,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
body = {"text": "Hello"}
|
||||
|
||||
r1 = requests.post(f"{BASE_URL}/api/v1/chats/{chat_id}/messages", headers=headers, json=body)
|
||||
r2 = requests.post(f"{BASE_URL}/api/v1/chats/{chat_id}/messages", headers=headers, json=body)
|
||||
|
||||
assert r1.status_code == 201, f"Первый запрос: {r1.status_code}"
|
||||
assert r2.status_code == 201, f"Повтор: {r2.status_code}"
|
||||
assert r1.json()["id"] == r2.json()["id"], "Id (uuid) сообщения должен быть одинаковым"
|
||||
|
||||
|
||||
def test_idempotency_different_body_returns_409():
|
||||
token = register_and_login("test_idem_diff")
|
||||
chat_id = create_chat(token)
|
||||
key = str(uuid.uuid4())
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Idempotency-Key": key,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
r1 = requests.post(f"{BASE_URL}/api/v1/chats/{chat_id}/messages",
|
||||
headers=headers, json={"text": "First"})
|
||||
assert r1.status_code == 201
|
||||
|
||||
r2 = requests.post(f"{BASE_URL}/api/v1/chats/{chat_id}/messages",
|
||||
headers=headers, json={"text": "Second"})
|
||||
assert r2.status_code == 409, f"Ожидался 409, получен {r2.status_code}"
|
||||
assert r2.json()["error"]["code"] == "conflict"
|
||||
|
||||
|
||||
def test_shared_session_across_replicas():
|
||||
token = register_and_login("test_shared_session")
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
instance_ids = set()
|
||||
for _ in range(10):
|
||||
resp = requests.get(f"{BASE_URL}/api/v1/users/me", headers=headers)
|
||||
assert resp.status_code == 200, f"Сессия не работает: {resp.status_code}"
|
||||
instance_ids.add(resp.headers.get("x-instance-id"))
|
||||
|
||||
assert len(instance_ids) >= 2, (
|
||||
f"Ожидались ≥2 разных x-instance-id, получено: {instance_ids}. "
|
||||
f"Проверь, что app1 и app2 запущены и Nginx балансирует."
|
||||
)
|
||||
Reference in New Issue
Block a user