forked from mtusi2026/mtusi2026lab2
feat: функционал первой лабы перенос
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"""Дубликат username и идентификация через X-User-Id"""
|
||||
|
||||
import uuid
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from helpers import assert_error, unique_username
|
||||
|
||||
|
||||
def test_duplicate_username_is_conflict(api: httpx.Client) -> None:
|
||||
username = unique_username("dup")
|
||||
first = api.post("/api/v1/users", json={"username": username, "display_name": "A"})
|
||||
assert first.status_code == 201
|
||||
second = api.post("/api/v1/users", json={"username": username, "display_name": "B"})
|
||||
assert_error(second, 409, "conflict")
|
||||
me = api.get("/api/v1/users/me", headers={"X-User-Id": first.json()["id"]})
|
||||
assert me.json()["display_name"] == "A"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"headers",
|
||||
[
|
||||
{},
|
||||
{"X-User-Id": ""},
|
||||
{"X-User-Id": "not-a-uuid"},
|
||||
{"X-User-Id": str(uuid.uuid4())},
|
||||
],
|
||||
)
|
||||
def test_missing_or_unknown_user_is_unauthorized(
|
||||
api: httpx.Client, headers: dict
|
||||
) -> None:
|
||||
assert_error(api.get("/api/v1/users/me", headers=headers), 401, "unauthorized")
|
||||
assert_error(api.get("/api/v1/chats", headers=headers), 401, "unauthorized")
|
||||
|
||||
|
||||
def test_current_user(api: httpx.Client, make_user) -> None:
|
||||
alice = make_user("alice")
|
||||
me = api.get("/api/v1/users/me", headers={"X-User-Id": alice["id"]})
|
||||
assert me.status_code == 200
|
||||
assert me.json() == alice
|
||||
Reference in New Issue
Block a user