forked from mtusi2026/mtusi2026lab3
The platform settings default the instance id to the container hostname, computed once per process, so every gunicorn worker of a replica reports the same value and it matches docker ps. The request pipeline stamps X-Instance-Id next to X-Request-Id on every response it sends, and the unhandled error handler adds it itself, because the 500 answer is produced outside the pipeline. Own tests cover 200, 204, 400, 401, 404, the hidden 405, 413 and 500 with a fixed instance id, and the hostname default. The application version follows the contract version 1.3.0. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
205 lines
6.6 KiB
Python
205 lines
6.6 KiB
Python
import asyncio
|
|
import re
|
|
import subprocess
|
|
import time
|
|
from base64 import b64encode
|
|
from collections.abc import AsyncIterator, Iterator
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
from typing import Any
|
|
from uuid import uuid4
|
|
|
|
import asyncpg
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from messenger.identity.config import IdentitySettings
|
|
from messenger.main import create_app
|
|
from messenger.messaging.config import MessagingSettings
|
|
from messenger.migrate import migrate
|
|
from messenger.shared.config import PlatformSettings
|
|
from messenger.shared.entrypoints.config import TransportSettings
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
POSTGRES_IMAGE = re.compile(r"image: (postgres:\S+)")
|
|
VALKEY_IMAGE = re.compile(r"image: (valkey/valkey:\S+)")
|
|
STARTUP_SECONDS = 60
|
|
PASSWORD = "Пароль-для-тестов-01"
|
|
APPS: dict[int, FastAPI] = {}
|
|
TOKENS: dict[str, str] = {}
|
|
DATABASE: dict[str, str] = {}
|
|
|
|
|
|
def docker(*args: str) -> str:
|
|
done = subprocess.run(["docker", *args], check=True, capture_output=True, text=True)
|
|
return done.stdout.strip()
|
|
|
|
|
|
async def migrated(dsn: str) -> None:
|
|
deadline = time.monotonic() + STARTUP_SECONDS
|
|
while True:
|
|
try:
|
|
await migrate(dsn)
|
|
return
|
|
except OSError, asyncpg.PostgresError, asyncpg.InterfaceError:
|
|
if time.monotonic() > deadline:
|
|
raise
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def database_for_api(request: pytest.FixtureRequest) -> None:
|
|
if "api" in request.node.path.parts:
|
|
request.getfixturevalue("database")
|
|
|
|
|
|
def image(pattern: re.Pattern[str]) -> str:
|
|
found = pattern.search((ROOT / "compose.yaml").read_text())
|
|
assert found is not None
|
|
return found[1]
|
|
|
|
|
|
def published(name: str, port: int) -> int:
|
|
return int(docker("port", name, f"{port}/tcp").splitlines()[0].rsplit(":", 1)[1])
|
|
|
|
|
|
def container(name: str, *args: str) -> None:
|
|
docker("run", "-d", "--rm", "--name", name, *args)
|
|
|
|
|
|
def answering(name: str, *probe: str) -> bool:
|
|
done = subprocess.run(["docker", "exec", name, *probe], capture_output=True, check=False)
|
|
return done.returncode == 0 and b"PONG" in done.stdout
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def valkey() -> Iterator[dict[str, str]]:
|
|
name = f"mtusi-lab3-test-valkey-{uuid4().hex[:8]}"
|
|
container(name, "-p", "127.0.0.1::6379", image(VALKEY_IMAGE))
|
|
try:
|
|
deadline = time.monotonic() + STARTUP_SECONDS
|
|
while not answering(name, "valkey-cli", "ping"):
|
|
assert time.monotonic() < deadline
|
|
time.sleep(0.2)
|
|
yield {"name": name, "url": f"redis://127.0.0.1:{published(name, 6379)}/0"}
|
|
finally:
|
|
subprocess.run(["docker", "rm", "-f", name], capture_output=True, check=False)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def database() -> Iterator[str]:
|
|
name = f"mtusi-lab3-test-{uuid4().hex[:8]}"
|
|
tuning = ["-c", "fsync=off", "-c", "synchronous_commit=off", "-c", "full_page_writes=off"]
|
|
environment = ["-e", "POSTGRES_PASSWORD=test", "-e", "POSTGRES_DB=messenger"]
|
|
container(name, *environment, "-p", "127.0.0.1::5432", image(POSTGRES_IMAGE), *tuning)
|
|
try:
|
|
dsn = f"postgresql://postgres:test@127.0.0.1:{published(name, 5432)}/messenger"
|
|
asyncio.run(migrated(dsn))
|
|
DATABASE["url"] = dsn
|
|
yield dsn
|
|
finally:
|
|
subprocess.run(["docker", "rm", "-f", name], capture_output=True, check=False)
|
|
|
|
|
|
def split_settings(**overrides: Any) -> dict[str, Any]:
|
|
base: dict[str, Any] = {
|
|
"log_level": "CRITICAL",
|
|
"cursor_secret": "test-secret-for-cursors",
|
|
"database_url": DATABASE.get("url", "postgresql://unused@127.0.0.1:1/unused"),
|
|
}
|
|
base.update(overrides)
|
|
groups = {
|
|
"platform": PlatformSettings,
|
|
"transport": TransportSettings,
|
|
"identity": IdentitySettings,
|
|
"messaging": MessagingSettings,
|
|
}
|
|
return {
|
|
name: kind.model_validate(
|
|
{key: value for key, value in base.items() if key in kind.model_fields}
|
|
)
|
|
for name, kind in groups.items()
|
|
}
|
|
|
|
|
|
@asynccontextmanager
|
|
async def running_app(**overrides: Any) -> AsyncIterator[AsyncClient]:
|
|
app = create_app(**split_settings(**overrides))
|
|
async with (
|
|
app.router.lifespan_context(app),
|
|
AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") as http,
|
|
):
|
|
APPS[id(http)] = app
|
|
try:
|
|
yield http
|
|
finally:
|
|
APPS.pop(id(http), None)
|
|
|
|
|
|
async def resolve[DependencyT](http: AsyncClient, dependency: type[DependencyT]) -> DependencyT:
|
|
return await APPS[id(http)].state.dishka_container.get(dependency)
|
|
|
|
|
|
@pytest.fixture
|
|
async def client() -> AsyncIterator[AsyncClient]:
|
|
async with running_app() as http:
|
|
yield http
|
|
|
|
|
|
async def scalar(sql: str, *args: Any) -> Any:
|
|
connection = await asyncpg.connect(DATABASE["url"])
|
|
try:
|
|
return await connection.fetchval(sql, *args)
|
|
finally:
|
|
await connection.close()
|
|
|
|
|
|
def basic(username: str, password: str) -> dict[str, str]:
|
|
encoded = b64encode(f"{username}:{password}".encode()).decode()
|
|
return {"Authorization": f"Basic {encoded}"}
|
|
|
|
|
|
def bearer(token: str) -> dict[str, str]:
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
def new_user(label: str, password: str = PASSWORD) -> dict[str, str]:
|
|
return {
|
|
"username": f"u_{label}_{uuid4().hex[:12]}",
|
|
"display_name": f"Студент {label}",
|
|
"password": password,
|
|
}
|
|
|
|
|
|
async def login(http: AsyncClient, username: str, password: str = PASSWORD) -> dict[str, Any]:
|
|
response = await http.post("/api/v1/auth/sessions", headers=basic(username, password))
|
|
assert response.status_code == 201, response.text
|
|
return response.json()
|
|
|
|
|
|
async def register(http: AsyncClient, label: str, password: str = PASSWORD) -> dict[str, Any]:
|
|
body = new_user(label, password)
|
|
response = await http.post("/api/v1/users", json=body)
|
|
assert response.status_code == 201, response.text
|
|
user = response.json()
|
|
TOKENS[user["id"]] = (await login(http, body["username"], password))["token"]
|
|
return user
|
|
|
|
|
|
async def make_chat(
|
|
http: AsyncClient, owner: dict[str, Any], *members: dict[str, Any]
|
|
) -> dict[str, Any]:
|
|
member_ids = [owner["id"], *[member["id"] for member in members]]
|
|
response = await http.post(
|
|
"/api/v1/chats",
|
|
json={"title": "Тестовый чат", "member_ids": member_ids},
|
|
headers=actor(owner),
|
|
)
|
|
assert response.status_code == 201, response.text
|
|
return response.json()
|
|
|
|
|
|
def actor(user: dict[str, Any]) -> dict[str, str]:
|
|
return bearer(TOKENS[user["id"]])
|