Carry over the lab 2 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack, migrations, secret generator, restart hook, evidence scripts and CI job from the lab 2 solution commit 8cddb09 unchanged, merge the lab 2 Makefile targets into the lab 3 skeleton Makefile without touching its targets, and append the lab 2 solution variables to the skeleton .env.example. The package version follows the contract version 1.3.0. This is the starting point of the lab 3 cycle: lint, own tests and the offline skeleton check pass before any lab 3 change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0ec2f9dae3
commit
416ce4f9db
@@ -0,0 +1,204 @@
|
||||
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-lab2-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-lab2-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"]])
|
||||
Reference in New Issue
Block a user