add async with all functions

This commit is contained in:
2026-10-23 00:20:58 +03:00
parent a85702a30e
commit 7b7ca1b888
42 changed files with 901 additions and 763 deletions
+13 -13
View File
@@ -1,7 +1,7 @@
import base64
from uuid import uuid4
from fastapi.testclient import TestClient
from httpx import AsyncClient
PASSWORD = "Correct-horse-battery-1" # синтетический пароль, только для тестов
@@ -15,15 +15,15 @@ def bearer(actor: dict) -> dict[str, str]:
return {"Authorization": "Bearer " + actor["token"]}
def register(
client: TestClient,
async def register(
client: AsyncClient,
username: str | None = None,
display_name: str = "Тестовый пользователь",
password: str = PASSWORD,
) -> dict:
"""Регистрация без входа. Возвращает User + password (нужен для login)."""
actual_username = username or "user_" + uuid4().hex[:12]
response = client.post(
response = await client.post(
"/api/v1/users",
json={
"username": actual_username,
@@ -35,8 +35,8 @@ def register(
return {**response.json(), "password": password}
def login(client: TestClient, user: dict) -> dict:
response = client.post(
async def login(client: AsyncClient, user: dict) -> dict:
response = await client.post(
"/api/v1/auth/sessions",
headers=basic_header(user["username"], user["password"]),
)
@@ -44,19 +44,19 @@ def login(client: TestClient, user: dict) -> dict:
return {**user, "token": response.json()["token"]}
def create_user(client: TestClient, **kwargs) -> dict:
async def create_user(client: AsyncClient, **kwargs) -> dict:
"""Регистрация + вход: актор с полями id, username, token, ..."""
return login(client, register(client, **kwargs))
return await login(client, await register(client, **kwargs))
def create_chat(
client: TestClient,
async def create_chat(
client: AsyncClient,
user: dict,
*members: dict,
title: str = "Тестовый чат",
) -> dict:
member_ids = [user["id"], *(member["id"] for member in members)]
response = client.post(
response = await client.post(
"/api/v1/chats",
headers=bearer(user),
json={"title": title, "member_ids": member_ids},
@@ -65,8 +65,8 @@ def create_chat(
return response.json()
def send_message(client: TestClient, user: dict, chat: dict, text: str):
return client.post(
async def send_message(client: AsyncClient, user: dict, chat: dict, text: str):
return await client.post(
f"/api/v1/chats/{chat['id']}/messages",
headers=bearer(user),
json={"text": text},