46 lines
1.9 KiB
Python
46 lines
1.9 KiB
Python
"""Повторный username: 409, в том числе при одновременной регистрации."""
|
|
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
from conftest import assert_error, run_sql
|
|
|
|
|
|
def body(username: str, display_name: str = "Имя") -> dict:
|
|
"""Тело регистрации."""
|
|
return {"username": username, "display_name": display_name, "password": "long-enough-password"}
|
|
|
|
|
|
def test_second_registration_with_same_username_is_409(client):
|
|
name = "dup_" + uuid.uuid4().hex[:10]
|
|
assert client.post("/api/v1/users", json=body(name)).status_code == 201
|
|
assert_error(client.post("/api/v1/users", json=body(name, "Другое имя")), 409, "conflict")
|
|
|
|
|
|
def test_duplicate_does_not_replace_original_user(client):
|
|
name = "dup_" + uuid.uuid4().hex[:10]
|
|
first = client.post("/api/v1/users", json=body(name, "Первый")).json()
|
|
client.post("/api/v1/users", json=body(name, "Второй"))
|
|
rows = run_sql("SELECT display_name FROM users WHERE username = $1", name)
|
|
assert [row["display_name"] for row in rows] == ["Первый"]
|
|
assert first["display_name"] == "Первый"
|
|
|
|
|
|
def test_username_is_case_sensitive_lowercase_only(client):
|
|
name = "dup_" + uuid.uuid4().hex[:10]
|
|
assert client.post("/api/v1/users", json=body(name)).status_code == 201
|
|
assert_error(client.post("/api/v1/users", json=body(name.upper())), 400, "invalid_request")
|
|
|
|
|
|
def test_parallel_registration_of_same_username_creates_exactly_one(client):
|
|
name = "race_" + uuid.uuid4().hex[:10]
|
|
|
|
def register(_):
|
|
return client.post("/api/v1/users", json=body(name)).status_code
|
|
|
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
|
codes = list(pool.map(register, range(10)))
|
|
assert codes.count(201) == 1
|
|
assert codes.count(409) == 9
|
|
assert len(run_sql("SELECT 1 FROM users WHERE username = $1", name)) == 1
|