- X-Instance-Id в каждом ответе; обязательный Idempotency-Key, таблица idempotency_keys (миграция 0002, одна транзакция, конкурентные повторы через уникальный индекс) - graceful shutdown: SIGTERM -> ready=503 -> drain 3 с -> закрытие порта - compose: lb (HAProxy) + api x2 без портов + разовый migrate + postgres, лимиты CPU/RAM - хуки stop-one/restart/restore-replicas, scripts/experiments.py (failover, idempotency, rolling) - свои тесты: 44 (13 новых); evidence: smoke, pytest, persistence, failover, эксперименты
70 lines
3.0 KiB
Python
70 lines
3.0 KiB
Python
import uuid
|
|
|
|
from sqlalchemy import text
|
|
|
|
from conftest import PASSWORD, assert_error
|
|
from core.db import engine
|
|
|
|
|
|
def test_invalid_json_returns_400(client):
|
|
response = client.post("/api/v1/users", content=b'{"username": "bob",',
|
|
headers={"Content-Type": "application/json"})
|
|
assert_error(response, 400, "invalid_request")
|
|
|
|
|
|
def test_extra_field_and_wrong_type_return_400(client):
|
|
response = client.post("/api/v1/users", json={"username": "bob", "display_name": "Bob", "password": PASSWORD,
|
|
"admin": True})
|
|
assert_error(response, 400, "invalid_request")
|
|
response = client.post("/api/v1/users", json={"username": "bob", "display_name": 123, "password": PASSWORD})
|
|
assert_error(response, 400, "invalid_request")
|
|
|
|
|
|
def test_duplicate_username_returns_409(client, alice):
|
|
response = client.post("/api/v1/users", json={"username": "alice", "display_name": "Другая Алиса",
|
|
"password": PASSWORD})
|
|
assert_error(response, 409, "conflict")
|
|
|
|
|
|
def test_unicode_display_name_is_kept(client):
|
|
response = client.post("/api/v1/users", json={"username": "uni", "display_name": "Алиса 👋",
|
|
"password": PASSWORD})
|
|
assert response.status_code == 201
|
|
assert response.json()["display_name"] == "Алиса 👋"
|
|
|
|
|
|
def test_password_is_required_and_length_checked(client):
|
|
for password in (None, "short", "x" * 129):
|
|
body = {"username": "pwd", "display_name": "P"}
|
|
if password is not None:
|
|
body["password"] = password
|
|
assert_error(client.post("/api/v1/users", json=body), 400, "invalid_request")
|
|
# 12 символов (не байт) — допустимо.
|
|
response = client.post("/api/v1/users", json={"username": "pwd", "display_name": "P", "password": "пароль123456"})
|
|
assert response.status_code == 201
|
|
|
|
|
|
def test_password_is_hashed_and_never_returned(client, alice):
|
|
assert "password" not in alice and "password_hash" not in alice
|
|
with engine.connect() as connection:
|
|
stored = connection.scalar(text("SELECT password_hash FROM users WHERE username = 'alice'"))
|
|
assert stored.startswith("$argon2id$")
|
|
assert PASSWORD not in stored
|
|
|
|
|
|
def test_me_returns_current_user(client, alice):
|
|
response = client.get("/api/v1/users/me", headers=alice["auth"])
|
|
assert response.status_code == 200
|
|
assert response.json() == {key: alice[key] for key in ("id", "username", "display_name", "created_at")}
|
|
|
|
|
|
def test_x_user_id_no_longer_authenticates(client, alice):
|
|
assert_error(client.get("/api/v1/users/me", headers={"X-User-Id": alice["id"]}), 401, "unauthorized")
|
|
assert_error(client.get("/api/v1/users/me", headers={"X-User-Id": str(uuid.uuid4())}), 401, "unauthorized")
|
|
|
|
|
|
def test_every_response_has_request_id(client):
|
|
assert client.get("/health/live").headers["X-Request-Id"]
|
|
assert client.get("/health/ready").headers["X-Request-Id"]
|
|
assert_error(client.get("/no/such/path"), 404, "not_found")
|