Brings the system changes of lab 2 from 8cddb09..102579e onto the lab 3 code, resolving each conflict by meaning: - the request pipeline counts the body bytes actually received, so a chunked body over the limit gets 413 instead of reaching the app; - logout revokes through a SessionRevocations port and withdraws the mark when PostgreSQL rejects the delete, so 503 means the session still works; PostgresSessionDirectory loses its no-op invalidate; - an empty cursor is 400 on history and on the chat list; - EventBus.project and the test that allowed importing an event across zones are gone, the config rules speak of the schema foreign key; - the worker waits one second less than the rest of the shutdown budget, so it cancels a stuck request with 500 and exits 0 instead of being killed by the gunicorn master. Lab 3 keeps 3 s after the drain window for the database timeouts, so SHUTDOWN_TIMEOUT_SECONDS becomes 6 s and the stop hooks wait 7 s, still under the 8 s grace; - the lab 3 single statement post already locks the chat only for a member, so lab 2's LOCK_CHAT edit is not needed; its test is kept; - lab 2 tests come along with Idempotency-Key where lab 3 needs it; the short TTL after restart runs without api-shortttl by shortening the session in the database; test_drain adds a stuck request; - make validate uses .venv with openapi-spec-validator, PYTEST_ARGS, faults-evidence, down with the bench profile, the image is tagged by the compose project, start probes every 200 ms, cache evidence fails without a tombstone and checks the key expiry, kdf.txt shows the seed stub apart. The track 2 tooling of lab 2 (track.py, postgres-restore and the database argument of migrate) and its evidence scripts stay out. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
209 lines
6.7 KiB
Python
209 lines
6.7 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", "-fv", 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", "-fv", 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"]])
|
|
|
|
|
|
def keyed(user: dict[str, Any], key: str | None = None) -> dict[str, str]:
|
|
return {**actor(user), "Idempotency-Key": key or uuid4().hex}
|