Add the adapters behind the attachments ports: PostgreSQL for the pending and ready rows, with a foreign key violation on the chat member turned into 404; Pillow that opens only PNG and JPEG, refuses a type that differs from Content-Type, checks the pixel count from the header before any decoding, verifies PNG chunks and decodes JPEG at 1/8 scale in a worker thread under a semaphore; and S3 on aiobotocore with path style, SigV4, short timeouts and checksums only when required. botocore sends Expect: 100-continue for streamed bodies and RustFS behind the Docker port proxy never answers it, so the PUT hung until the read timeout; the handler that adds the header is unregistered. The HTTP layer streams the raw body into the use case, returns the contract Attachment and streams the original back with its content type and length and no redirect. The request pipeline takes a body limit per route, so uploads may carry 10 MiB while JSON stays at 1 MiB. main.py wires the zone and answers its membership port from the messaging chat directory. Tests run the API with an in-memory object store: download by another member, 404 for strangers, the exact 10 MiB limit and one byte over it with Content-Length and chunked, 40 MP and above, spoofed and truncated images, unsupported types, dangerous names, the variant, a failing store and a lost object. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
214 lines
7.0 KiB
Python
214 lines
7.0 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 fakes import MemoryStorageProvider
|
|
from messenger.attachments.config import AttachmentsSettings
|
|
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-lab4-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-lab4-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"),
|
|
"s3_access_key_id": "test-key",
|
|
"s3_secret_access_key": "test-secret-key",
|
|
}
|
|
base.update(overrides)
|
|
groups = {
|
|
"platform": PlatformSettings,
|
|
"transport": TransportSettings,
|
|
"identity": IdentitySettings,
|
|
"messaging": MessagingSettings,
|
|
"attachments": AttachmentsSettings,
|
|
}
|
|
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), overrides=(MemoryStorageProvider(),))
|
|
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}
|