forked from mtusi2026/mtusi2026lab2
chore: перенос решения lab1 (src, student-tests, Dockerfile, lock) без изменений
Исходная точка lab2: код из solution/bvt2302-bodanyuk лабы 1, коммит 472c1bd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
65003b6119
commit
893beaa79a
@@ -0,0 +1,3 @@
|
||||
# LF everywhere: identical files on Windows/Linux and inside `git archive | docker build -`.
|
||||
* text=auto eol=lf
|
||||
*.png binary
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
# In-memory messenger, lab 1. Build: docker build -t mtusi-messenger:lab1 .
|
||||
#
|
||||
# Python is interpreted, so there is no separate compile stage; the image
|
||||
# contains only the interpreter, locked dependencies and the source code.
|
||||
# Base image is pinned by version AND digest (python 3.13.15, Debian 12 bookworm).
|
||||
FROM python:3.13.15-slim-bookworm@sha256:2325bb286ec344af3e5898cc224b5844e2707ac6e26b1632516fd3edc84a5e26
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PIP_NO_CACHE_DIR=1 \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=1
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# 1. Dependencies first (layer cache survives source edits). Every package,
|
||||
# including transitive ones, is pinned with sha256 hashes in requirements.txt;
|
||||
# --no-deps guarantees nothing outside the lock file is installed.
|
||||
COPY requirements.txt ./
|
||||
RUN pip install --root-user-action=ignore --require-hashes --no-deps -r requirements.txt
|
||||
|
||||
# 2. Unprivileged runtime user. Code stays owned by root, so the process
|
||||
# cannot modify its own code.
|
||||
RUN groupadd --system --gid 10001 app \
|
||||
&& useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
|
||||
|
||||
COPY src/ ./src/
|
||||
# Course contract, served read-only at /openapi.json for Swagger UI (/docs).
|
||||
COPY contracts/openapi.json ./contracts/openapi.json
|
||||
|
||||
# Defaults make `docker run` work without an .env file; --env-file overrides them.
|
||||
ENV PYTHONPATH=/app/src \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080 \
|
||||
APP_ENV=lab \
|
||||
LOG_LEVEL=info \
|
||||
SHUTDOWN_TIMEOUT_SECONDS=8 \
|
||||
DOCS_ENABLED=true
|
||||
|
||||
USER app:app
|
||||
EXPOSE 8080
|
||||
STOPSIGNAL SIGTERM
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["python", "-m", "messenger.healthcheck"]
|
||||
|
||||
# Exec form: the Python server is PID 1 and receives SIGTERM directly (no shell in between).
|
||||
CMD ["python", "-m", "messenger"]
|
||||
@@ -0,0 +1,4 @@
|
||||
# Student tests (in-process, via Starlette TestClient). Runtime deps come from requirements.txt.
|
||||
pytest==9.1.1
|
||||
httpx2==2.13.1
|
||||
jsonschema==4.26.0
|
||||
@@ -0,0 +1,8 @@
|
||||
# Direct runtime dependencies. The pinned lock with hashes is requirements.txt:
|
||||
# docker run --rm -v "$PWD:/w" -w /w python:3.13.15-slim-bookworm \
|
||||
# sh -c "pip install -q pip-tools==7.6.1 && pip-compile --generate-hashes --strip-extras -o requirements.txt requirements.in"
|
||||
starlette==1.7.0
|
||||
uvicorn==0.54.0
|
||||
# Fast C parser/event loop used by uvicorn in the container (Linux only).
|
||||
httptools==0.8.0
|
||||
uvloop==0.22.1 ; sys_platform != "win32"
|
||||
@@ -0,0 +1,3 @@
|
||||
"""In-memory messenger for lab 1 (MTUSI, «Высоконагруженные приложения»)."""
|
||||
|
||||
__version__ = "1.0.0"
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Entry point: `python -m messenger`. The HTTP server is the main process (PID 1 in Docker).
|
||||
|
||||
Shutdown sequence on SIGTERM/SIGINT:
|
||||
1. readiness flips to 503 (`app.state.draining = True`);
|
||||
2. uvicorn closes the listening socket, so new connections are refused;
|
||||
3. in-flight requests get up to SHUTDOWN_TIMEOUT_SECONDS (default 8 s < Docker's 10 s) to finish;
|
||||
4. the process exits with code 0. In-memory data is lost by design.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from types import FrameType
|
||||
|
||||
import uvicorn
|
||||
|
||||
from . import __version__, logging_setup
|
||||
from .app import create_app
|
||||
from .config import ConfigError, Settings
|
||||
|
||||
log = logging.getLogger("messenger")
|
||||
|
||||
|
||||
class GracefulServer(uvicorn.Server):
|
||||
"""uvicorn.Server that marks the app as draining before the normal graceful stop."""
|
||||
|
||||
def handle_exit(self, sig: int, frame: FrameType | None) -> None:
|
||||
if not self.should_exit:
|
||||
log.info("shutdown_requested", extra={"fields": {"signal": sig, "timeout_s": self.config.timeout_graceful_shutdown}})
|
||||
self.config.app.state.draining = True # config.app is our Starlette instance
|
||||
super().handle_exit(sig, frame)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
try:
|
||||
settings = Settings.from_env()
|
||||
except ConfigError as exc:
|
||||
print(f"configuration error: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
logging_setup.configure(settings.log_level)
|
||||
app = create_app(settings)
|
||||
config = uvicorn.Config(
|
||||
app,
|
||||
host=settings.host,
|
||||
port=settings.port,
|
||||
log_config=None,
|
||||
access_log=False,
|
||||
server_header=False,
|
||||
proxy_headers=False,
|
||||
lifespan="off",
|
||||
timeout_graceful_shutdown=settings.shutdown_timeout,
|
||||
)
|
||||
server = GracefulServer(config)
|
||||
log.info("starting", extra={"fields": {
|
||||
"version": __version__, "env": settings.env, "host": settings.host, "port": settings.port,
|
||||
"python": sys.version.split()[0], "storage": "in-memory (single process)",
|
||||
}})
|
||||
server.run()
|
||||
log.info("stopped", extra={"fields": {"state": app.state.store.counts(), "note": "in-memory data discarded"}})
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,271 @@
|
||||
"""HTTP layer: Starlette routes, request-id/logging middleware and error mapping."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.routing import Route
|
||||
from starlette.types import ASGIApp, Message as AsgiMessage, Receive, Scope, Send
|
||||
|
||||
from . import cursor, docs, validation
|
||||
from .config import Settings
|
||||
from .errors import ApiError, InvalidRequest, PayloadTooLarge, Unauthorized, Unavailable
|
||||
from .store import Store, User
|
||||
|
||||
log = logging.getLogger("messenger")
|
||||
access_log = logging.getLogger("messenger.access")
|
||||
|
||||
REQUEST_ID_RE = re.compile(r"[A-Za-z0-9._:-]{1,128}")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Responses
|
||||
# --------------------------------------------------------------------------
|
||||
def error_response(request_id: str, status: int, code: str, message: str, headers: dict[str, str] | None = None) -> JSONResponse:
|
||||
body = {"error": {"code": code, "message": message, "request_id": request_id}}
|
||||
return JSONResponse(body, status_code=status, headers={"X-Request-Id": request_id, **(headers or {})})
|
||||
|
||||
|
||||
async def api_error_handler(request: Request, exc: Exception) -> JSONResponse:
|
||||
assert isinstance(exc, ApiError)
|
||||
return error_response(request.state.request_id, exc.status, exc.code, exc.message)
|
||||
|
||||
|
||||
async def http_exception_handler(request: Request, exc: Exception) -> JSONResponse:
|
||||
"""Unknown routes and wrong methods also answer in the contract error format."""
|
||||
assert isinstance(exc, HTTPException)
|
||||
if exc.status_code == 404:
|
||||
return error_response(request.state.request_id, 404, "not_found", "Объект не найден")
|
||||
if exc.status_code == 405:
|
||||
return error_response(request.state.request_id, 405, "invalid_request", "Метод не поддерживается", exc.headers)
|
||||
return error_response(request.state.request_id, exc.status_code, "invalid_request", "Неверный запрос")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Middleware (pure ASGI, so it also covers 404/405 and unexpected exceptions)
|
||||
# --------------------------------------------------------------------------
|
||||
class RequestContextMiddleware:
|
||||
"""Assigns X-Request-Id to every response, writes one access-log line, turns crashes into 500."""
|
||||
|
||||
def __init__(self, app: ASGIApp):
|
||||
self.app = app
|
||||
|
||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
if scope["type"] != "http":
|
||||
await self.app(scope, receive, send)
|
||||
return
|
||||
incoming = dict(scope["headers"]).get(b"x-request-id", b"").decode("latin-1")
|
||||
request_id = incoming if REQUEST_ID_RE.fullmatch(incoming) else uuid.uuid4().hex
|
||||
scope.setdefault("state", {})["request_id"] = request_id
|
||||
status = 500
|
||||
started = False
|
||||
begin = time.perf_counter()
|
||||
|
||||
async def send_with_id(message: AsgiMessage) -> None:
|
||||
nonlocal status, started
|
||||
if message["type"] == "http.response.start":
|
||||
started = True
|
||||
status = message["status"]
|
||||
headers = [(k, v) for k, v in message.get("headers", []) if k.lower() != b"x-request-id"]
|
||||
headers.append((b"x-request-id", request_id.encode("latin-1")))
|
||||
message = {**message, "headers": headers}
|
||||
await send(message)
|
||||
|
||||
try:
|
||||
await self.app(scope, receive, send_with_id)
|
||||
except asyncio.CancelledError:
|
||||
# uvicorn cancels requests still running when SHUTDOWN_TIMEOUT_SECONDS expires:
|
||||
# answer 503 (retryable) instead of a generic 500, then let the cancellation proceed.
|
||||
if not started:
|
||||
status = 503
|
||||
response = error_response(request_id, 503, "unavailable", "Сервер завершает работу")
|
||||
await response(scope, receive, send)
|
||||
raise
|
||||
except Exception:
|
||||
log.exception("unhandled_error", extra={"fields": {"request_id": request_id}})
|
||||
if not started:
|
||||
status = 500
|
||||
response = error_response(request_id, 500, "internal_error", "Внутренняя ошибка сервера")
|
||||
await response(scope, receive, send)
|
||||
finally:
|
||||
access_log.info("request", extra={"fields": {
|
||||
"request_id": request_id,
|
||||
"method": scope["method"],
|
||||
"path": scope["path"], # ids only; query (cursor) and bodies are never logged
|
||||
"status": status,
|
||||
"duration_ms": round((time.perf_counter() - begin) * 1000, 3),
|
||||
}})
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Request helpers
|
||||
# --------------------------------------------------------------------------
|
||||
def store_of(request: Request) -> Store:
|
||||
return request.app.state.store
|
||||
|
||||
|
||||
def current_user(request: Request) -> User:
|
||||
"""Lab 1 identification: X-User-Id must be the UUID of an existing user, otherwise 401."""
|
||||
raw = request.headers.get("x-user-id")
|
||||
if raw is None or validation.UUID_RE.fullmatch(raw) is None:
|
||||
raise Unauthorized()
|
||||
user = store_of(request).get_user(raw.lower())
|
||||
if user is None:
|
||||
raise Unauthorized()
|
||||
return user
|
||||
|
||||
|
||||
def path_uuid(request: Request, name: str) -> str:
|
||||
return validation.parse_uuid(request.path_params[name], name)
|
||||
|
||||
|
||||
async def read_json(request: Request) -> dict[str, Any]:
|
||||
content_type = request.headers.get("content-type", "")
|
||||
media_type, _, params = content_type.partition(";")
|
||||
if media_type.strip().lower() != "application/json":
|
||||
raise InvalidRequest("Content-Type должен быть application/json")
|
||||
charset = params.strip().lower()
|
||||
if charset and charset.replace(" ", "") not in {"charset=utf-8", "charset=\"utf-8\""}:
|
||||
raise InvalidRequest("Поддерживается только кодировка UTF-8")
|
||||
limit: int = request.app.state.settings.max_body_bytes
|
||||
declared = request.headers.get("content-length")
|
||||
if declared is not None and declared.isdigit() and int(declared) > limit:
|
||||
raise PayloadTooLarge()
|
||||
chunks: list[bytes] = []
|
||||
size = 0
|
||||
async for chunk in request.stream():
|
||||
size += len(chunk)
|
||||
if size > limit:
|
||||
raise PayloadTooLarge()
|
||||
chunks.append(chunk)
|
||||
return validation.parse_json_object(b"".join(chunks))
|
||||
|
||||
|
||||
def page_params(request: Request) -> tuple[int, str | None]:
|
||||
query = request.query_params.multi_items()
|
||||
limit = validation.parse_limit(validation.single_query_param(query, "limit"))
|
||||
raw_cursor = validation.single_query_param(query, "cursor")
|
||||
if raw_cursor is not None and raw_cursor == "":
|
||||
raise InvalidRequest("cursor не может быть пустым")
|
||||
return limit, raw_cursor
|
||||
|
||||
|
||||
def ok(body: Any, status: int = 200) -> JSONResponse:
|
||||
return JSONResponse(body, status_code=status)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Handlers
|
||||
# --------------------------------------------------------------------------
|
||||
async def live(request: Request) -> JSONResponse:
|
||||
return ok({"status": "ok"})
|
||||
|
||||
|
||||
async def ready(request: Request) -> JSONResponse:
|
||||
# The only critical dependency is process memory; while draining after
|
||||
# SIGTERM the instance reports 503 so no new traffic is routed to it.
|
||||
if request.app.state.draining:
|
||||
raise Unavailable("Сервер завершает работу")
|
||||
return ok({"status": "ok"})
|
||||
|
||||
|
||||
async def register_user(request: Request) -> JSONResponse:
|
||||
data = await read_json(request)
|
||||
validation.check_fields(data, {"username", "display_name"})
|
||||
username = validation.username_field(data)
|
||||
display_name = validation.text_field(data, "display_name", 100)
|
||||
user = store_of(request).create_user(username, display_name)
|
||||
log.info("user_created", extra={"fields": {"request_id": request.state.request_id, "user_id": user.id}})
|
||||
return ok(user.to_json(), 201)
|
||||
|
||||
|
||||
async def me(request: Request) -> JSONResponse:
|
||||
return ok(current_user(request).to_json())
|
||||
|
||||
|
||||
async def create_chat(request: Request) -> JSONResponse:
|
||||
user = current_user(request)
|
||||
data = await read_json(request)
|
||||
validation.check_fields(data, {"title", "member_ids"})
|
||||
title = validation.text_field(data, "title", 100)
|
||||
member_ids = validation.member_ids_field(data)
|
||||
chat = store_of(request).create_chat(user.id, title, member_ids)
|
||||
log.info("chat_created", extra={"fields": {"request_id": request.state.request_id, "chat_id": chat.id, "members": len(member_ids)}})
|
||||
return ok(chat.to_json(), 201)
|
||||
|
||||
|
||||
async def list_chats(request: Request) -> JSONResponse:
|
||||
user = current_user(request)
|
||||
limit, raw_cursor = page_params(request)
|
||||
after = cursor.decode(raw_cursor, cursor.CHATS, user.id) if raw_cursor else None
|
||||
items, has_more = store_of(request).list_chats(user.id, after, limit)
|
||||
next_cursor = cursor.encode(cursor.CHATS, user.id, items[-1].key) if has_more else None
|
||||
return ok({"items": [c.to_json() for c in items], "next_cursor": next_cursor})
|
||||
|
||||
|
||||
async def get_chat(request: Request) -> JSONResponse:
|
||||
user = current_user(request)
|
||||
chat_id = path_uuid(request, "chat_id")
|
||||
return ok(store_of(request).get_chat(user.id, chat_id).to_json())
|
||||
|
||||
|
||||
async def create_message(request: Request) -> JSONResponse:
|
||||
user = current_user(request)
|
||||
chat_id = path_uuid(request, "chat_id")
|
||||
store = store_of(request)
|
||||
store.get_chat(user.id, chat_id) # access check before reading the body: outsiders get 404
|
||||
data = await read_json(request)
|
||||
validation.check_fields(data, {"text"})
|
||||
text = validation.text_field(data, "text", 4000)
|
||||
# sender_id is derived from the identification, never taken from the body.
|
||||
message = store.add_message(user.id, chat_id, text)
|
||||
return ok(message.to_json(), 201)
|
||||
|
||||
|
||||
async def list_messages(request: Request) -> JSONResponse:
|
||||
user = current_user(request)
|
||||
chat_id = path_uuid(request, "chat_id")
|
||||
store = store_of(request)
|
||||
store.get_chat(user.id, chat_id) # 404 for outsiders before any cursor details leak
|
||||
limit, raw_cursor = page_params(request)
|
||||
after = cursor.decode(raw_cursor, cursor.MESSAGES, chat_id) if raw_cursor else None
|
||||
items, has_more = store.list_messages(user.id, chat_id, after, limit)
|
||||
next_cursor = cursor.encode(cursor.MESSAGES, chat_id, items[-1].key) if has_more else None
|
||||
return ok({"items": [m.to_json() for m in items], "next_cursor": next_cursor})
|
||||
|
||||
|
||||
def create_app(settings: Settings | None = None, store: Store | None = None) -> Starlette:
|
||||
settings = settings or Settings.from_env()
|
||||
routes = [
|
||||
Route("/health/live", live, methods=["GET"]),
|
||||
Route("/health/ready", ready, methods=["GET"]),
|
||||
Route("/api/v1/users", register_user, methods=["POST"]),
|
||||
Route("/api/v1/users/me", me, methods=["GET"]),
|
||||
Route("/api/v1/chats", create_chat, methods=["POST"]),
|
||||
Route("/api/v1/chats", list_chats, methods=["GET"]),
|
||||
Route("/api/v1/chats/{chat_id}", get_chat, methods=["GET"]),
|
||||
Route("/api/v1/chats/{chat_id}/messages", create_message, methods=["POST"]),
|
||||
Route("/api/v1/chats/{chat_id}/messages", list_messages, methods=["GET"]),
|
||||
]
|
||||
if settings.docs_enabled: # Swagger UI for manual experiments, not part of the contract
|
||||
routes += [
|
||||
Route("/docs", docs.swagger_page, methods=["GET"]),
|
||||
Route("/openapi.json", docs.openapi_json, methods=["GET"]),
|
||||
]
|
||||
app = Starlette(
|
||||
routes=routes,
|
||||
exception_handlers={ApiError: api_error_handler, HTTPException: http_exception_handler},
|
||||
)
|
||||
app.add_middleware(RequestContextMiddleware)
|
||||
app.state.settings = settings
|
||||
app.state.store = store or Store()
|
||||
app.state.draining = False
|
||||
return app
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Runtime configuration read from environment variables (12-factor style).
|
||||
|
||||
Every variable has a safe default, so `docker run` works from a clean clone
|
||||
even without an .env file. Invalid values stop the process at startup with a
|
||||
clear message instead of failing later under traffic.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
class ConfigError(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def _int(name: str, default: int, low: int, high: int) -> int:
|
||||
raw = os.environ.get(name)
|
||||
if raw is None or raw.strip() == "":
|
||||
return default
|
||||
try:
|
||||
value = int(raw.strip())
|
||||
except ValueError:
|
||||
raise ConfigError(f"{name} must be an integer, got {raw!r}") from None
|
||||
if not low <= value <= high:
|
||||
raise ConfigError(f"{name} must be in [{low}, {high}], got {value}")
|
||||
return value
|
||||
|
||||
|
||||
def _float(name: str, default: float, low: float, high: float) -> float:
|
||||
raw = os.environ.get(name)
|
||||
if raw is None or raw.strip() == "":
|
||||
return default
|
||||
try:
|
||||
value = float(raw.strip())
|
||||
except ValueError:
|
||||
raise ConfigError(f"{name} must be a number, got {raw!r}") from None
|
||||
if not low <= value <= high:
|
||||
raise ConfigError(f"{name} must be in [{low}, {high}], got {value}")
|
||||
return value
|
||||
|
||||
|
||||
def _bool(name: str, default: bool) -> bool:
|
||||
raw = os.environ.get(name, "").strip().lower()
|
||||
if raw == "":
|
||||
return default
|
||||
if raw not in {"true", "false", "1", "0"}:
|
||||
raise ConfigError(f"{name} must be true/false, got {raw!r}")
|
||||
return raw in {"true", "1"}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
host: str
|
||||
port: int
|
||||
env: str
|
||||
log_level: str
|
||||
shutdown_timeout: float
|
||||
max_body_bytes: int
|
||||
docs_enabled: bool = True
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "Settings":
|
||||
log_level = os.environ.get("LOG_LEVEL", "info").strip().lower() or "info"
|
||||
if log_level not in {"debug", "info", "warning", "error"}:
|
||||
raise ConfigError(f"LOG_LEVEL must be debug/info/warning/error, got {log_level!r}")
|
||||
return cls(
|
||||
host=os.environ.get("APP_HOST", "0.0.0.0").strip() or "0.0.0.0",
|
||||
port=_int("APP_PORT", 8080, 1, 65535),
|
||||
env=os.environ.get("APP_ENV", "lab").strip() or "lab",
|
||||
log_level=log_level,
|
||||
# Docker sends SIGKILL 10 s after SIGTERM by default; finish earlier.
|
||||
shutdown_timeout=_float("SHUTDOWN_TIMEOUT_SECONDS", 8.0, 0.0, 9.0),
|
||||
max_body_bytes=_int("MAX_BODY_BYTES", 64 * 1024, 1024, 1024 * 1024),
|
||||
docs_enabled=_bool("DOCS_ENABLED", True),
|
||||
)
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Opaque keyset cursors.
|
||||
|
||||
A cursor encodes the sort key `(created_at, id)` of the last item on the page
|
||||
plus the resource it belongs to, e.g. `v1|m|<chat_id>|<ts_us>|<message_id>`,
|
||||
packed in URL-safe base64. The client treats it as an opaque string; the server
|
||||
rejects cursors of another chat/user (400) and any non-canonical encoding.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import re
|
||||
|
||||
from .errors import InvalidRequest
|
||||
from .validation import UUID_RE
|
||||
|
||||
B64_RE = re.compile(r"[A-Za-z0-9_-]{1,512}")
|
||||
TS_RE = re.compile(r"[0-9]{1,19}")
|
||||
|
||||
MESSAGES = "m"
|
||||
CHATS = "c"
|
||||
|
||||
SortKey = tuple[int, str]
|
||||
|
||||
|
||||
def encode(scope: str, owner_id: str, key: SortKey) -> str:
|
||||
ts, item_id = key
|
||||
raw = f"v1|{scope}|{owner_id}|{ts}|{item_id}".encode("ascii")
|
||||
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode("ascii")
|
||||
|
||||
|
||||
def decode(cursor: str, scope: str, owner_id: str) -> SortKey:
|
||||
"""Return the sort key stored in the cursor or raise InvalidRequest."""
|
||||
bad = InvalidRequest("Неверный cursor")
|
||||
if B64_RE.fullmatch(cursor) is None:
|
||||
raise bad
|
||||
try:
|
||||
raw = base64.urlsafe_b64decode(cursor + "=" * (-len(cursor) % 4)).decode("ascii")
|
||||
except (ValueError, UnicodeDecodeError):
|
||||
raise bad from None
|
||||
parts = raw.split("|")
|
||||
if len(parts) != 5 or parts[0] != "v1":
|
||||
raise bad
|
||||
_, got_scope, got_owner, ts, item_id = parts
|
||||
if TS_RE.fullmatch(ts) is None or UUID_RE.fullmatch(item_id) is None or item_id != item_id.lower():
|
||||
raise bad
|
||||
key = (int(ts), item_id)
|
||||
if encode(got_scope, got_owner, key) != cursor:
|
||||
raise bad # only the canonical form produced by the server is accepted
|
||||
if got_scope != scope or got_owner != owner_id:
|
||||
raise InvalidRequest("cursor относится к другому ресурсу")
|
||||
return key
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Swagger UI for manual experiments: http://127.0.0.1:8080/docs
|
||||
|
||||
The page shows the course contract contracts/openapi.json as is (it is not
|
||||
generated from our code). Only `servers` is replaced with "/" so that the
|
||||
"Try it out" requests go to the same host the page was opened from.
|
||||
Swagger UI itself (JS/CSS) is loaded by the browser from the jsDelivr CDN.
|
||||
Disable with DOCS_ENABLED=false.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import HTMLResponse, JSONResponse
|
||||
|
||||
SPEC_PATH = Path(__file__).resolve().parents[2] / "contracts" / "openapi.json"
|
||||
SWAGGER_UI = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.33.0"
|
||||
|
||||
PAGE = f"""<!doctype html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Messenger lab1 — Swagger UI</title>
|
||||
<link rel="stylesheet" href="{SWAGGER_UI}/swagger-ui.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
<script src="{SWAGGER_UI}/swagger-ui-bundle.js"></script>
|
||||
<script>
|
||||
window.ui = SwaggerUIBundle({{
|
||||
url: "/openapi.json",
|
||||
dom_id: "#swagger-ui",
|
||||
tryItOutEnabled: true, // fields are editable right away
|
||||
persistAuthorization: true, // X-User-Id from "Authorize" survives page reload
|
||||
displayRequestDuration: true,
|
||||
}});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def _spec() -> dict:
|
||||
spec = json.loads(SPEC_PATH.read_text(encoding="utf-8"))
|
||||
spec["servers"] = [{"url": "/", "description": "этот сервер"}]
|
||||
return spec
|
||||
|
||||
|
||||
async def swagger_page(request: Request) -> HTMLResponse:
|
||||
return HTMLResponse(PAGE)
|
||||
|
||||
|
||||
async def openapi_json(request: Request) -> JSONResponse:
|
||||
return JSONResponse(_spec())
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Domain errors mapped 1:1 to the contract error codes (contracts/README.md)."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
status = 500
|
||||
code = "internal_error"
|
||||
default_message = "Внутренняя ошибка сервера"
|
||||
|
||||
def __init__(self, message: str | None = None):
|
||||
self.message = message or self.default_message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class InvalidRequest(ApiError):
|
||||
status = 400
|
||||
code = "invalid_request"
|
||||
default_message = "Неверный запрос"
|
||||
|
||||
|
||||
class Unauthorized(ApiError):
|
||||
status = 401
|
||||
code = "unauthorized"
|
||||
default_message = "Требуется X-User-Id существующего пользователя"
|
||||
|
||||
|
||||
class NotFound(ApiError):
|
||||
status = 404
|
||||
code = "not_found"
|
||||
default_message = "Объект не найден"
|
||||
|
||||
|
||||
class Conflict(ApiError):
|
||||
status = 409
|
||||
code = "conflict"
|
||||
default_message = "Конфликт"
|
||||
|
||||
|
||||
class PayloadTooLarge(ApiError):
|
||||
status = 413
|
||||
code = "payload_too_large"
|
||||
default_message = "Тело запроса слишком большое"
|
||||
|
||||
|
||||
class Unavailable(ApiError):
|
||||
status = 503
|
||||
code = "unavailable"
|
||||
default_message = "Сервис временно недоступен"
|
||||
@@ -0,0 +1,19 @@
|
||||
"""Docker HEALTHCHECK probe without curl/wget in the image: `python -m messenger.healthcheck`."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
|
||||
def main() -> int:
|
||||
port = os.environ.get("APP_PORT", "8080").strip() or "8080"
|
||||
try:
|
||||
with urllib.request.urlopen(f"http://127.0.0.1:{port}/health/ready", timeout=2) as response:
|
||||
return 0 if response.status == 200 else 1
|
||||
except Exception:
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,36 @@
|
||||
"""JSON-lines logging to stdout. Logs never contain message texts or request bodies."""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime as dt
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
|
||||
|
||||
class JsonFormatter(logging.Formatter):
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
entry = {
|
||||
"ts": dt.datetime.fromtimestamp(record.created, dt.timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z"),
|
||||
"level": record.levelname.lower(),
|
||||
"logger": record.name,
|
||||
"event": record.getMessage(),
|
||||
}
|
||||
fields = getattr(record, "fields", None)
|
||||
if fields:
|
||||
entry.update(fields)
|
||||
if record.exc_info:
|
||||
# Stack traces go only to the server log, never to the HTTP client.
|
||||
entry["exception"] = self.formatException(record.exc_info)
|
||||
return json.dumps(entry, ensure_ascii=False)
|
||||
|
||||
|
||||
def configure(level: str) -> None:
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(JsonFormatter())
|
||||
root = logging.getLogger()
|
||||
root.handlers[:] = [handler]
|
||||
root.setLevel(level.upper())
|
||||
# uvicorn's own access log is disabled; our middleware writes one line per request.
|
||||
for name in ("uvicorn", "uvicorn.error"):
|
||||
logging.getLogger(name).handlers[:] = []
|
||||
logging.getLogger(name).propagate = True
|
||||
@@ -0,0 +1,191 @@
|
||||
"""In-memory state of the messenger: the single source of truth for lab 1.
|
||||
|
||||
Concurrency model
|
||||
-----------------
|
||||
All data lives in ordinary dicts/lists of ONE process. Every public method
|
||||
takes `self._lock` for its whole critical section, so each operation is atomic
|
||||
and readers never see a half-built chat or message.
|
||||
|
||||
The server runs coroutines on one asyncio event loop, and store methods never
|
||||
`await`, so on the event loop they are already atomic. The lock additionally
|
||||
protects the store when it is used from several threads (student-tests start
|
||||
10 threads that write concurrently) and documents the invariant explicitly.
|
||||
|
||||
Ordering invariant
|
||||
------------------
|
||||
`created_at` is assigned INSIDE the same critical section that appends the
|
||||
object, from a strictly increasing clock (`_next_ts`). Therefore the append
|
||||
order of every list equals the contract order `(created_at, id)` even under
|
||||
concurrent writes and even if the wall clock jumps backwards.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import bisect
|
||||
import datetime as dt
|
||||
import operator
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from typing import Callable
|
||||
|
||||
from .cursor import SortKey
|
||||
from .errors import Conflict, InvalidRequest, NotFound
|
||||
|
||||
_EPOCH = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
|
||||
# (created_at, id) of a Chat/Message as a C-level getter: cheap to call inside bisect.
|
||||
_sort_key = operator.attrgetter("ts", "id")
|
||||
|
||||
|
||||
def format_ts(ts_us: int) -> str:
|
||||
"""RFC 3339 UTC with fixed microsecond precision, e.g. 2026-09-25T12:00:00.000001Z."""
|
||||
return (_EPOCH + dt.timedelta(microseconds=ts_us)).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
|
||||
|
||||
@dataclass(slots=True, frozen=True)
|
||||
class User:
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
ts: int
|
||||
created_at: str
|
||||
|
||||
def to_json(self) -> dict:
|
||||
return {"id": self.id, "username": self.username, "display_name": self.display_name, "created_at": self.created_at}
|
||||
|
||||
|
||||
@dataclass(slots=True, frozen=True)
|
||||
class Chat:
|
||||
id: str
|
||||
title: str
|
||||
member_ids: tuple[str, ...]
|
||||
members: frozenset[str]
|
||||
ts: int
|
||||
created_at: str
|
||||
|
||||
@property
|
||||
def key(self) -> SortKey:
|
||||
return (self.ts, self.id)
|
||||
|
||||
def to_json(self) -> dict:
|
||||
return {"id": self.id, "title": self.title, "member_ids": list(self.member_ids), "created_at": self.created_at}
|
||||
|
||||
|
||||
@dataclass(slots=True, frozen=True)
|
||||
class Message:
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
ts: int
|
||||
created_at: str
|
||||
|
||||
@property
|
||||
def key(self) -> SortKey:
|
||||
return (self.ts, self.id)
|
||||
|
||||
def to_json(self) -> dict:
|
||||
return {"id": self.id, "chat_id": self.chat_id, "sender_id": self.sender_id, "text": self.text, "created_at": self.created_at}
|
||||
|
||||
|
||||
def _page_after(items: list, after: SortKey | None, limit: int) -> tuple[list, bool]:
|
||||
"""Keyset page: up to `limit` items strictly after `after`, plus "has more".
|
||||
|
||||
`items` is always sorted by (created_at, id) (see the ordering invariant), so
|
||||
the page start is found by binary search in O(log n) instead of scanning the
|
||||
history from the beginning (O(n)); only the page itself is copied.
|
||||
"""
|
||||
start = 0 if after is None else bisect.bisect_right(items, after, key=_sort_key)
|
||||
page = items[start:start + limit + 1]
|
||||
return page[:limit], len(page) > limit
|
||||
|
||||
|
||||
class Store:
|
||||
def __init__(self, clock_ns: Callable[[], int] = time.time_ns):
|
||||
self._lock = threading.Lock()
|
||||
self._clock_ns = clock_ns
|
||||
self._last_ts = 0
|
||||
self._users: dict[str, User] = {}
|
||||
self._user_ids_by_name: dict[str, str] = {}
|
||||
self._chats: dict[str, Chat] = {}
|
||||
self._chats_by_user: dict[str, list[Chat]] = {}
|
||||
self._messages: dict[str, list[Message]] = {}
|
||||
|
||||
# -- helpers (caller holds the lock) ---------------------------------
|
||||
def _next_ts(self) -> tuple[int, str]:
|
||||
"""Strictly increasing server time in microseconds; must be called under the lock."""
|
||||
ts = max(self._clock_ns() // 1000, self._last_ts + 1)
|
||||
self._last_ts = ts
|
||||
return ts, format_ts(ts)
|
||||
|
||||
def _chat_for_member(self, user_id: str, chat_id: str) -> Chat:
|
||||
chat = self._chats.get(chat_id)
|
||||
# A missing chat and a foreign chat are indistinguishable: both 404.
|
||||
if chat is None or user_id not in chat.members:
|
||||
raise NotFound("Чат не найден")
|
||||
return chat
|
||||
|
||||
# -- users -----------------------------------------------------------
|
||||
def create_user(self, username: str, display_name: str) -> User:
|
||||
with self._lock:
|
||||
if username in self._user_ids_by_name:
|
||||
raise Conflict("Имя пользователя уже занято")
|
||||
ts, created_at = self._next_ts()
|
||||
user = User(str(uuid.uuid4()), username, display_name, ts, created_at)
|
||||
self._users[user.id] = user
|
||||
self._user_ids_by_name[username] = user.id
|
||||
self._chats_by_user[user.id] = []
|
||||
return user
|
||||
|
||||
def get_user(self, user_id: str) -> User | None:
|
||||
with self._lock:
|
||||
return self._users.get(user_id)
|
||||
|
||||
# -- chats -----------------------------------------------------------
|
||||
def create_chat(self, creator_id: str, title: str, member_ids: list[str]) -> Chat:
|
||||
with self._lock:
|
||||
if creator_id not in member_ids:
|
||||
raise InvalidRequest("member_ids должен включать текущего пользователя")
|
||||
if any(member not in self._users for member in member_ids):
|
||||
raise InvalidRequest("member_ids содержит несуществующих пользователей")
|
||||
ts, created_at = self._next_ts()
|
||||
chat = Chat(str(uuid.uuid4()), title, tuple(member_ids), frozenset(member_ids), ts, created_at)
|
||||
# All indexes are updated in one critical section: the chat is created atomically.
|
||||
self._chats[chat.id] = chat
|
||||
self._messages[chat.id] = []
|
||||
for member in member_ids:
|
||||
self._chats_by_user[member].append(chat)
|
||||
return chat
|
||||
|
||||
def get_chat(self, user_id: str, chat_id: str) -> Chat:
|
||||
with self._lock:
|
||||
return self._chat_for_member(user_id, chat_id)
|
||||
|
||||
def list_chats(self, user_id: str, after: SortKey | None, limit: int) -> tuple[list[Chat], bool]:
|
||||
"""Page of the user's chats ordered by (created_at, id); returns (items, has_more)."""
|
||||
with self._lock:
|
||||
return _page_after(self._chats_by_user[user_id], after, limit)
|
||||
|
||||
# -- messages --------------------------------------------------------
|
||||
def add_message(self, user_id: str, chat_id: str, text: str) -> Message:
|
||||
with self._lock:
|
||||
chat = self._chat_for_member(user_id, chat_id)
|
||||
ts, created_at = self._next_ts()
|
||||
message = Message(str(uuid.uuid4()), chat.id, user_id, text, ts, created_at)
|
||||
self._messages[chat.id].append(message)
|
||||
return message
|
||||
|
||||
def list_messages(self, user_id: str, chat_id: str, after: SortKey | None, limit: int) -> tuple[list[Message], bool]:
|
||||
"""Page of history strictly after `after` in (created_at, id) order; returns (items, has_more)."""
|
||||
with self._lock:
|
||||
chat = self._chat_for_member(user_id, chat_id)
|
||||
return _page_after(self._messages[chat.id], after, limit)
|
||||
|
||||
# -- diagnostics -----------------------------------------------------
|
||||
def counts(self) -> dict[str, int]:
|
||||
with self._lock:
|
||||
return {
|
||||
"users": len(self._users),
|
||||
"chats": len(self._chats),
|
||||
"messages": sum(len(h) for h in self._messages.values()),
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
"""Strict request validation according to contracts/openapi.json + contracts/README.md.
|
||||
|
||||
Written by hand instead of a schema library so that every rule is visible:
|
||||
extra fields, wrong JSON types, whitespace-only strings and lengths counted in
|
||||
Unicode code points all produce 400 invalid_request.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from .errors import InvalidRequest
|
||||
|
||||
USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}")
|
||||
# Canonical textual UUID (8-4-4-4-12 hex). Case-insensitive, normalised to lower case.
|
||||
UUID_RE = re.compile(r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
|
||||
LIMIT_RE = re.compile(r"[0-9]{1,3}")
|
||||
SURROGATES_RE = re.compile("[\ud800-\udfff]")
|
||||
|
||||
DEFAULT_LIMIT = 50
|
||||
MAX_LIMIT = 100
|
||||
|
||||
|
||||
def _reject_constant(name: str) -> Any:
|
||||
# Python's json accepts NaN/Infinity, which are not valid JSON.
|
||||
raise ValueError(f"invalid JSON constant {name}")
|
||||
|
||||
|
||||
def parse_json_object(body: bytes) -> dict[str, Any]:
|
||||
"""Decode a UTF-8 JSON object or raise InvalidRequest (malformed JSON -> 400)."""
|
||||
try:
|
||||
text = body.decode("utf-8")
|
||||
value = json.loads(text, parse_constant=_reject_constant)
|
||||
except (UnicodeDecodeError, ValueError):
|
||||
raise InvalidRequest("Тело запроса должно быть корректным JSON в UTF-8") from None
|
||||
if not isinstance(value, dict):
|
||||
raise InvalidRequest("Тело запроса должно быть JSON-объектом")
|
||||
return value
|
||||
|
||||
|
||||
def check_fields(data: dict[str, Any], required: set[str], optional: frozenset[str] = frozenset()) -> None:
|
||||
extra = set(data) - required - optional
|
||||
if extra:
|
||||
raise InvalidRequest("Неизвестные поля: " + ", ".join(sorted(extra)))
|
||||
missing = required - set(data)
|
||||
if missing:
|
||||
raise InvalidRequest("Отсутствуют обязательные поля: " + ", ".join(sorted(missing)))
|
||||
|
||||
|
||||
def text_field(data: dict[str, Any], name: str, max_len: int, *, min_len: int = 1) -> str:
|
||||
value = data[name]
|
||||
if not isinstance(value, str):
|
||||
raise InvalidRequest(f"Поле {name} должно быть строкой")
|
||||
if SURROGATES_RE.search(value):
|
||||
# "\ud800" escapes decode to lone surrogates that cannot be encoded as UTF-8.
|
||||
raise InvalidRequest(f"Поле {name} содержит недопустимые символы")
|
||||
# len() of a Python str is the number of Unicode code points, as the contract requires.
|
||||
if not min_len <= len(value) <= max_len:
|
||||
raise InvalidRequest(f"Длина поля {name} должна быть от {min_len} до {max_len} символов")
|
||||
if value.strip() == "":
|
||||
raise InvalidRequest(f"Поле {name} не может состоять только из пробелов")
|
||||
return value
|
||||
|
||||
|
||||
def username_field(data: dict[str, Any]) -> str:
|
||||
value = data["username"]
|
||||
if not isinstance(value, str) or USERNAME_RE.fullmatch(value) is None:
|
||||
raise InvalidRequest("username должен соответствовать [a-z0-9_]{3,32}")
|
||||
return value
|
||||
|
||||
|
||||
def parse_uuid(value: Any, what: str = "UUID") -> str:
|
||||
if not isinstance(value, str) or UUID_RE.fullmatch(value) is None:
|
||||
raise InvalidRequest(f"Неверный {what}")
|
||||
return value.lower()
|
||||
|
||||
|
||||
def member_ids_field(data: dict[str, Any]) -> list[str]:
|
||||
value = data["member_ids"]
|
||||
if not isinstance(value, list):
|
||||
raise InvalidRequest("member_ids должен быть массивом")
|
||||
if not 1 <= len(value) <= 100:
|
||||
raise InvalidRequest("member_ids должен содержать от 1 до 100 элементов")
|
||||
ids = [parse_uuid(item, "UUID участника") for item in value]
|
||||
if len(set(ids)) != len(ids):
|
||||
raise InvalidRequest("member_ids содержит дубликаты")
|
||||
return ids
|
||||
|
||||
|
||||
def single_query_param(query: list[tuple[str, str]], name: str) -> str | None:
|
||||
values = [v for k, v in query if k == name]
|
||||
if len(values) > 1:
|
||||
raise InvalidRequest(f"Параметр {name} указан несколько раз")
|
||||
return values[0] if values else None
|
||||
|
||||
|
||||
def parse_limit(raw: str | None) -> int:
|
||||
if raw is None:
|
||||
return DEFAULT_LIMIT
|
||||
if LIMIT_RE.fullmatch(raw) is None or not 1 <= int(raw) <= MAX_LIMIT:
|
||||
raise InvalidRequest(f"limit должен быть целым числом от 1 до {MAX_LIMIT}")
|
||||
return int(raw)
|
||||
@@ -0,0 +1,91 @@
|
||||
"""Shared fixtures: an isolated in-process app per test + OpenAPI schema validation."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from jsonschema import Draft202012Validator
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
# MESSENGER_SRC lets scripts/race_mutation_check.py run the same tests against a patched copy.
|
||||
sys.path.insert(0, os.environ.get("MESSENGER_SRC", str(ROOT / "src")))
|
||||
|
||||
from messenger.app import create_app # noqa: E402
|
||||
from messenger.config import Settings # noqa: E402
|
||||
|
||||
SPEC = json.loads((ROOT / "contracts" / "openapi.json").read_text(encoding="utf-8"))
|
||||
SETTINGS = Settings(host="127.0.0.1", port=8080, env="test", log_level="warning", shutdown_timeout=1.0, max_body_bytes=64 * 1024)
|
||||
|
||||
|
||||
def schema_errors(value, name: str) -> list[str]:
|
||||
"""Validate against components/schemas/<name> of the course contract (JSON Schema 2020-12)."""
|
||||
schema = {"$ref": f"#/components/schemas/{name}", "components": SPEC["components"]}
|
||||
validator = Draft202012Validator(schema, format_checker=Draft202012Validator.FORMAT_CHECKER)
|
||||
return [e.message for e in validator.iter_errors(value)]
|
||||
|
||||
|
||||
class Api:
|
||||
"""Thin helper over TestClient that checks the invariants every response must satisfy."""
|
||||
|
||||
def __init__(self, client: TestClient):
|
||||
self.client = client
|
||||
|
||||
def call(self, method: str, path: str, expected: int, *, user=None, json_body=None, raw=None, headers=None, schema=None):
|
||||
h = dict(headers or {})
|
||||
if user is not None:
|
||||
h["X-User-Id"] = user["id"]
|
||||
if json_body is not None:
|
||||
raw = json.dumps(json_body, ensure_ascii=False).encode("utf-8")
|
||||
h.setdefault("Content-Type", "application/json")
|
||||
response = self.client.request(method, path, content=raw, headers=h)
|
||||
assert response.status_code == expected, (method, path, response.status_code, response.text)
|
||||
request_id = response.headers.get("x-request-id")
|
||||
assert request_id, "every response must carry X-Request-Id"
|
||||
if response.status_code >= 400:
|
||||
body = response.json()
|
||||
assert schema_errors(body, "Error") == []
|
||||
assert body["error"]["request_id"] == request_id
|
||||
assert "Traceback" not in response.text
|
||||
elif schema:
|
||||
assert schema_errors(response.json(), schema) == [], response.text
|
||||
return response
|
||||
|
||||
def user(self, label: str = "u", display_name: str | None = None) -> dict:
|
||||
body = {"username": f"{label}_{uuid.uuid4().hex[:10]}", "display_name": display_name or f"Студент {label}"}
|
||||
return self.call("POST", "/api/v1/users", 201, json_body=body, schema="User").json()
|
||||
|
||||
def chat(self, owner: dict, *members: dict, title: str = "Тестовый чат") -> dict:
|
||||
ids = [owner["id"], *(m["id"] for m in members)]
|
||||
return self.call("POST", "/api/v1/chats", 201, user=owner, json_body={"title": title, "member_ids": ids}, schema="Chat").json()
|
||||
|
||||
def send(self, user: dict, chat: dict, text: str) -> dict:
|
||||
return self.call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, user=user, json_body={"text": text}, schema="Message").json()
|
||||
|
||||
def history(self, user: dict, chat: dict, limit: int) -> tuple[list[dict], int]:
|
||||
"""Walk all pages; returns (items, number_of_pages)."""
|
||||
items, pages, cursor = [], 0, None
|
||||
while True:
|
||||
query = f"?limit={limit}" + (f"&cursor={cursor}" if cursor else "")
|
||||
page = self.call("GET", f"/api/v1/chats/{chat['id']}/messages{query}", 200, user=user, schema="MessagePage").json()
|
||||
pages += 1
|
||||
items.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None:
|
||||
return items, pages
|
||||
assert len(page["items"]) == limit, "a non-final page must be full"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
return create_app(SETTINGS)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api(app) -> Api:
|
||||
with TestClient(app, raise_server_exceptions=False) as client:
|
||||
yield Api(client)
|
||||
@@ -0,0 +1,134 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Black-box race check against a RUNNING server (level 4 criterion).
|
||||
|
||||
10 clients (10 different users, one thread each) send 10 messages each into one
|
||||
chat at the same moment. Then the whole history is read page by page through
|
||||
the cursor. PASS requires: 100 confirmed writes, exactly 100 unique messages,
|
||||
nothing lost, strict (created_at, id) order and per-client order preserved.
|
||||
|
||||
python student-tests/http_concurrency.py --base-url http://127.0.0.1:8080 \
|
||||
--output evidence/concurrency.json
|
||||
|
||||
Standard library only (Python 3.9+). Creates synthetic users; run on a lab stand.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def call(base: str, method: str, path: str, body=None, user_id=None, expected=200):
|
||||
data = None if body is None else json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||
headers = {"Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
if user_id:
|
||||
headers["X-User-Id"] = user_id
|
||||
request = urllib.request.Request(base + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
status, payload = response.status, response.read()
|
||||
except urllib.error.HTTPError as error:
|
||||
status, payload = error.code, error.read()
|
||||
if status != expected:
|
||||
raise AssertionError(f"{method} {path.split('?')[0]}: expected {expected}, got {status}")
|
||||
return json.loads(payload)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
parser.add_argument("--base-url", default="http://127.0.0.1:8080")
|
||||
parser.add_argument("--clients", type=int, default=10)
|
||||
parser.add_argument("--messages", type=int, default=10, help="messages per client")
|
||||
parser.add_argument("--page-limit", type=int, default=7, help="small limit => many cursor pages")
|
||||
parser.add_argument("--output", default=None, help="optional JSON summary (no secrets inside)")
|
||||
args = parser.parse_args()
|
||||
base = args.base_url.rstrip("/")
|
||||
total = args.clients * args.messages
|
||||
|
||||
run = uuid.uuid4().hex[:8]
|
||||
users = [call(base, "POST", "/api/v1/users", {"username": f"race_{run}_{i}", "display_name": f"Клиент {i}"}, expected=201)
|
||||
for i in range(args.clients)]
|
||||
chat = call(base, "POST", "/api/v1/chats", {"title": f"race {run}", "member_ids": [u["id"] for u in users]},
|
||||
user_id=users[0]["id"], expected=201)
|
||||
|
||||
barrier = threading.Barrier(args.clients)
|
||||
confirmed: list[dict] = []
|
||||
errors: list[str] = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def client(index: int) -> None:
|
||||
barrier.wait() # all clients start sending at the same moment
|
||||
for j in range(args.messages):
|
||||
try:
|
||||
message = call(base, "POST", f"/api/v1/chats/{chat['id']}/messages",
|
||||
{"text": f"c{index}-m{j} 🧵"}, user_id=users[index]["id"], expected=201)
|
||||
with lock:
|
||||
confirmed.append(message)
|
||||
except Exception as exc: # noqa: BLE001 - reported below
|
||||
with lock:
|
||||
errors.append(f"client {index}: {exc}")
|
||||
|
||||
started = time.monotonic()
|
||||
threads = [threading.Thread(target=client, args=(i,)) for i in range(args.clients)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
elapsed = time.monotonic() - started
|
||||
|
||||
history, pages, cursor = [], 0, None
|
||||
while True:
|
||||
query = f"?limit={args.page_limit}" + (f"&cursor={cursor}" if cursor else "")
|
||||
page = call(base, "GET", f"/api/v1/chats/{chat['id']}/messages{query}", user_id=users[-1]["id"])
|
||||
pages += 1
|
||||
history.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None or len(history) > 10 * total:
|
||||
break
|
||||
|
||||
ids = [m["id"] for m in history]
|
||||
keys = [(m["created_at"], uuid.UUID(m["id"]).int) for m in history]
|
||||
per_client_ok = all(
|
||||
[m["text"] for m in history if m["sender_id"] == u["id"]] == [f"c{i}-m{j} 🧵" for j in range(args.messages)]
|
||||
for i, u in enumerate(users)
|
||||
)
|
||||
checks = {
|
||||
"all_writes_confirmed_201": len(confirmed) == total and not errors,
|
||||
"history_count_equals_sent": len(history) == total,
|
||||
"ids_unique": len(set(ids)) == len(ids),
|
||||
"no_confirmed_message_lost": {m["id"] for m in confirmed} <= set(ids),
|
||||
"strict_created_at_id_order": all(a < b for a, b in zip(keys, keys[1:])),
|
||||
"per_client_order_preserved": per_client_ok,
|
||||
}
|
||||
summary = {
|
||||
"clients": args.clients,
|
||||
"messages_per_client": args.messages,
|
||||
"sent": total,
|
||||
"confirmed_201": len(confirmed),
|
||||
"errors": errors[:5],
|
||||
"history_items": len(history),
|
||||
"unique_ids": len(set(ids)),
|
||||
"pages_read": pages,
|
||||
"page_limit": args.page_limit,
|
||||
"write_phase_seconds": round(elapsed, 3),
|
||||
"checks": checks,
|
||||
"result": "PASS" if all(checks.values()) else "FAIL",
|
||||
}
|
||||
text = json.dumps(summary, ensure_ascii=False, indent=2)
|
||||
print(text)
|
||||
if args.output:
|
||||
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(args.output).write_text(text + "\n", encoding="utf-8")
|
||||
return 0 if summary["result"] == "PASS" else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,219 @@
|
||||
"""Chats, membership (foreign chat -> 404), messages and keyset pagination."""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def key(item: dict) -> tuple[str, int]:
|
||||
# Fixed-width RFC 3339 strings compare like timestamps; ids compare as UUID integers.
|
||||
return item["created_at"], uuid.UUID(item["id"]).int
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- chats
|
||||
def test_create_chat_and_read_it(api):
|
||||
alice, bob = api.user("alice"), api.user("bob")
|
||||
chat = api.chat(alice, bob, title="Проект по ВНП 🚀")
|
||||
assert chat["member_ids"] == [alice["id"], bob["id"]]
|
||||
assert chat["title"] == "Проект по ВНП 🚀"
|
||||
assert api.call("GET", f"/api/v1/chats/{chat['id']}", 200, user=bob, schema="Chat").json() == chat
|
||||
|
||||
|
||||
@pytest.mark.parametrize("case", ["duplicate", "unknown", "without_caller", "empty", "too_many", "not_uuid", "blank_title", "long_title", "extra_field", "ids_not_list"])
|
||||
def test_invalid_chat_is_400(api, case):
|
||||
alice, bob = api.user("alice"), api.user("bob")
|
||||
body = {"title": "Чат", "member_ids": [alice["id"], bob["id"]]}
|
||||
if case == "duplicate":
|
||||
body["member_ids"] = [alice["id"], bob["id"], bob["id"].upper()]
|
||||
elif case == "unknown":
|
||||
body["member_ids"] = [alice["id"], str(uuid.uuid4())]
|
||||
elif case == "without_caller":
|
||||
body["member_ids"] = [bob["id"]]
|
||||
elif case == "empty":
|
||||
body["member_ids"] = []
|
||||
elif case == "too_many":
|
||||
body["member_ids"] = [alice["id"]] + [str(uuid.uuid4()) for _ in range(100)]
|
||||
elif case == "not_uuid":
|
||||
body["member_ids"] = [alice["id"], "bob"]
|
||||
elif case == "blank_title":
|
||||
body["title"] = " "
|
||||
elif case == "long_title":
|
||||
body["title"] = "т" * 101
|
||||
elif case == "extra_field":
|
||||
body["owner_id"] = alice["id"]
|
||||
elif case == "ids_not_list":
|
||||
body["member_ids"] = alice["id"]
|
||||
api.call("POST", "/api/v1/chats", 400, user=alice, json_body=body)
|
||||
|
||||
|
||||
def test_chat_with_only_the_caller_is_allowed(api):
|
||||
alice = api.user("alice")
|
||||
assert api.chat(alice)["member_ids"] == [alice["id"]]
|
||||
|
||||
|
||||
def test_chat_list_contains_only_own_chats_in_order(api):
|
||||
alice, bob, eve = api.user("alice"), api.user("bob"), api.user("eve")
|
||||
created = [api.chat(alice, bob, title=f"Чат {i}") for i in range(5)]
|
||||
api.chat(eve, title="Чат Евы")
|
||||
listed = api.call("GET", "/api/v1/chats?limit=100", 200, user=bob, schema="ChatPage").json()
|
||||
assert listed == {"items": created, "next_cursor": None}
|
||||
assert api.call("GET", "/api/v1/chats", 200, user=eve).json()["items"][0]["title"] == "Чат Евы"
|
||||
assert len(api.call("GET", "/api/v1/chats", 200, user=eve).json()["items"]) == 1
|
||||
|
||||
|
||||
def test_chat_list_pagination(api):
|
||||
alice = api.user("alice")
|
||||
created = [api.chat(alice, title=f"Чат {i}") for i in range(5)]
|
||||
first = api.call("GET", "/api/v1/chats?limit=2", 200, user=alice).json()
|
||||
second = api.call("GET", f"/api/v1/chats?limit=2&cursor={first['next_cursor']}", 200, user=alice).json()
|
||||
third = api.call("GET", f"/api/v1/chats?limit=2&cursor={second['next_cursor']}", 200, user=alice).json()
|
||||
assert first["items"] + second["items"] + third["items"] == created
|
||||
assert third["next_cursor"] is None
|
||||
|
||||
|
||||
def test_chat_cursor_of_another_user_is_400(api):
|
||||
alice, bob = api.user("alice"), api.user("bob")
|
||||
for i in range(3):
|
||||
api.chat(alice, bob, title=f"Чат {i}")
|
||||
cursor = api.call("GET", "/api/v1/chats?limit=1", 200, user=alice).json()["next_cursor"]
|
||||
api.call("GET", f"/api/v1/chats?limit=1&cursor={cursor}", 400, user=bob)
|
||||
|
||||
|
||||
# ------------------------------------------------------ foreign chat -> 404
|
||||
def test_foreign_chat_is_indistinguishable_from_missing_chat(api):
|
||||
alice, bob, eve = api.user("alice"), api.user("bob"), api.user("eve")
|
||||
chat = api.chat(alice, bob)
|
||||
api.send(alice, chat, "секрет")
|
||||
missing = str(uuid.uuid4())
|
||||
for chat_id in (chat["id"], missing):
|
||||
base = f"/api/v1/chats/{chat_id}"
|
||||
errors = [
|
||||
api.call("GET", base, 404, user=eve).json(),
|
||||
api.call("GET", base + "/messages", 404, user=eve).json(),
|
||||
api.call("POST", base + "/messages", 404, user=eve, json_body={"text": "чужое"}).json(),
|
||||
# Even an invalid body or cursor does not reveal that the chat exists.
|
||||
api.call("POST", base + "/messages", 404, user=eve, json_body={"text": ""}).json(),
|
||||
api.call("GET", base + "/messages?cursor=garbage", 404, user=eve).json(),
|
||||
]
|
||||
assert all(e["error"]["code"] == "not_found" for e in errors)
|
||||
assert all(e["error"]["message"] == errors[0]["error"]["message"] for e in errors)
|
||||
|
||||
|
||||
def test_invalid_chat_id_is_400_after_authentication(api):
|
||||
alice = api.user("alice")
|
||||
api.call("GET", "/api/v1/chats/not-a-uuid", 401)
|
||||
api.call("GET", "/api/v1/chats/not-a-uuid", 400, user=alice)
|
||||
api.call("GET", "/api/v1/chats/not-a-uuid/messages", 400, user=alice)
|
||||
|
||||
|
||||
# -------------------------------------------------------------- messages
|
||||
def test_message_sender_is_derived_from_identity(api):
|
||||
alice, bob = api.user("alice"), api.user("bob")
|
||||
chat = api.chat(alice, bob)
|
||||
first = api.send(alice, chat, "Привет, Боб! 👋")
|
||||
second = api.send(bob, chat, "Привет, Алиса")
|
||||
assert (first["sender_id"], second["sender_id"]) == (alice["id"], bob["id"])
|
||||
assert first["chat_id"] == second["chat_id"] == chat["id"]
|
||||
assert first["text"] == "Привет, Боб! 👋"
|
||||
assert key(first) < key(second)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("body", [
|
||||
{"text": ""},
|
||||
{"text": " \n\t "},
|
||||
{"text": "я" * 4001},
|
||||
{"text": None},
|
||||
{"text": 42},
|
||||
{},
|
||||
{"text": "ok", "sender_id": str(uuid.uuid4())}, # sender cannot be chosen by the client
|
||||
{"text": "ok", "attachment_ids": []}, # attachments appear only in lab4
|
||||
])
|
||||
def test_invalid_message_is_400(api, body):
|
||||
alice = api.user("alice")
|
||||
chat = api.chat(alice)
|
||||
error = api.call("POST", f"/api/v1/chats/{chat['id']}/messages", 400, user=alice, json_body=body).json()
|
||||
assert error["error"]["code"] == "invalid_request"
|
||||
assert api.call("GET", f"/api/v1/chats/{chat['id']}/messages", 200, user=alice).json()["items"] == []
|
||||
|
||||
|
||||
def test_message_limit_is_4000_code_points(api):
|
||||
alice = api.user("alice")
|
||||
chat = api.chat(alice)
|
||||
text = "🙂" * 4000 # 4000 code points, 16000 UTF-8 bytes
|
||||
assert api.send(alice, chat, text)["text"] == text
|
||||
assert api.send(alice, chat, " отступы и\nпереносы сохраняются ")["text"] == " отступы и\nпереносы сохраняются "
|
||||
|
||||
|
||||
# ------------------------------------------------------------ pagination
|
||||
def test_history_walks_several_pages_without_loss_or_duplicates(api):
|
||||
alice, bob = api.user("alice"), api.user("bob")
|
||||
chat = api.chat(alice, bob)
|
||||
sent = [api.send(alice if i % 2 else bob, chat, f"сообщение {i}") for i in range(25)]
|
||||
items, pages = api.history(bob, chat, limit=7)
|
||||
assert pages == 4 # 7 + 7 + 7 + 4
|
||||
assert items == sent # same order, nothing lost or repeated
|
||||
assert items == sorted(items, key=key)
|
||||
assert len({m["id"] for m in items}) == 25
|
||||
|
||||
|
||||
def test_limit_may_change_between_pages_and_default_is_50(api):
|
||||
alice = api.user("alice")
|
||||
chat = api.chat(alice)
|
||||
sent = [api.send(alice, chat, f"m{i}") for i in range(60)]
|
||||
base = f"/api/v1/chats/{chat['id']}/messages"
|
||||
first = api.call("GET", base, 200, user=alice).json()
|
||||
assert len(first["items"]) == 50 and first["next_cursor"]
|
||||
rest = api.call("GET", f"{base}?limit=100&cursor={first['next_cursor']}", 200, user=alice).json()
|
||||
assert first["items"] + rest["items"] == sent and rest["next_cursor"] is None
|
||||
|
||||
|
||||
def test_exact_page_boundary_has_null_cursor(api):
|
||||
alice = api.user("alice")
|
||||
chat = api.chat(alice)
|
||||
for i in range(3):
|
||||
api.send(alice, chat, f"m{i}")
|
||||
page = api.call("GET", f"/api/v1/chats/{chat['id']}/messages?limit=3", 200, user=alice).json()
|
||||
assert len(page["items"]) == 3 and page["next_cursor"] is None
|
||||
|
||||
|
||||
def test_new_message_after_last_page_is_reachable_from_cursor(api):
|
||||
alice = api.user("alice")
|
||||
chat = api.chat(alice)
|
||||
api.send(alice, chat, "m0")
|
||||
api.send(alice, chat, "m1")
|
||||
base = f"/api/v1/chats/{chat['id']}/messages"
|
||||
page = api.call("GET", f"{base}?limit=1", 200, user=alice).json()
|
||||
newer = api.send(alice, chat, "m2")
|
||||
tail = api.call("GET", f"{base}?limit=10&cursor={page['next_cursor']}", 200, user=alice).json()
|
||||
assert [m["text"] for m in tail["items"]] == ["m1", "m2"] and tail["items"][-1] == newer
|
||||
|
||||
|
||||
@pytest.mark.parametrize("query", ["limit=0", "limit=101", "limit=-1", "limit=abc", "limit=1.5", "limit=", "limit=%D9%A3",
|
||||
"limit=1&limit=2", "cursor=", "cursor=garbage", "cursor=%%%", "cursor=djF8bXx4fDF8Mg"])
|
||||
def test_invalid_limit_or_cursor_is_400(api, query):
|
||||
alice = api.user("alice")
|
||||
chat = api.chat(alice)
|
||||
api.send(alice, chat, "m")
|
||||
api.call("GET", f"/api/v1/chats/{chat['id']}/messages?{query}", 400, user=alice)
|
||||
|
||||
|
||||
def test_cursor_from_another_chat_is_400(api):
|
||||
alice = api.user("alice")
|
||||
chat_a, chat_b = api.chat(alice, title="A"), api.chat(alice, title="B")
|
||||
for chat in (chat_a, chat_b):
|
||||
api.send(alice, chat, "1")
|
||||
api.send(alice, chat, "2")
|
||||
cursor = api.call("GET", f"/api/v1/chats/{chat_a['id']}/messages?limit=1", 200, user=alice).json()["next_cursor"]
|
||||
api.call("GET", f"/api/v1/chats/{chat_b['id']}/messages?cursor={cursor}", 400, user=alice)
|
||||
# A cursor of the chat list is not a message cursor either.
|
||||
api.chat(alice, title="C")
|
||||
chats_cursor = api.call("GET", "/api/v1/chats?limit=1", 200, user=alice).json()["next_cursor"]
|
||||
api.call("GET", f"/api/v1/chats/{chat_a['id']}/messages?cursor={chats_cursor}", 400, user=alice)
|
||||
|
||||
|
||||
def test_invalid_cursor_is_400_even_for_empty_history(api):
|
||||
alice = api.user("alice")
|
||||
chat = api.chat(alice) # no messages at all
|
||||
api.call("GET", f"/api/v1/chats/{chat['id']}/messages?cursor=garbage", 400, user=alice)
|
||||
api.call("GET", "/api/v1/chats?cursor=garbage", 400, user=api.user("bob")) # user without chats
|
||||
@@ -0,0 +1,131 @@
|
||||
"""Race protection of the in-memory store with real OS threads.
|
||||
|
||||
The HTTP server itself runs on one event loop; these tests hit the Store from
|
||||
10 threads at once to prove the lock and the "timestamp under the lock" rule:
|
||||
no lost/duplicated messages AND the history order equals (created_at, id).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from messenger.errors import Conflict
|
||||
from messenger.store import Store
|
||||
|
||||
CLIENTS = 10
|
||||
PER_CLIENT = 10
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def frequent_thread_switches():
|
||||
"""Force the interpreter to switch threads every 0.1 ms instead of every 5 ms.
|
||||
|
||||
Without this the GIL rarely interrupts a short critical section and a race
|
||||
such as "timestamp taken before the lock" would
|
||||
pass unnoticed. With it the broken variant fails reliably.
|
||||
"""
|
||||
previous = sys.getswitchinterval()
|
||||
sys.setswitchinterval(1e-4)
|
||||
yield
|
||||
sys.setswitchinterval(previous)
|
||||
|
||||
|
||||
def run_concurrently(workers: int, target) -> list:
|
||||
barrier = threading.Barrier(workers)
|
||||
results: list = [None] * workers
|
||||
errors: list[BaseException] = []
|
||||
|
||||
def body(index: int) -> None:
|
||||
barrier.wait() # release all threads at the same moment
|
||||
try:
|
||||
results[index] = target(index)
|
||||
except BaseException as exc: # noqa: BLE001 - collected and re-raised in the test
|
||||
errors.append(exc)
|
||||
|
||||
threads = [threading.Thread(target=body, args=(i,)) for i in range(workers)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
if errors:
|
||||
raise errors[0]
|
||||
return results
|
||||
|
||||
|
||||
def walk(store: Store, user_id: str, chat_id: str, limit: int, max_items: int = 1000) -> list:
|
||||
"""Cursor traversal like a client does it. If the stored order were broken,
|
||||
a cursor could point back to an already returned page and the walk would
|
||||
never end, so the number of returned items is bounded."""
|
||||
items, after = [], None
|
||||
while True:
|
||||
page, has_more = store.list_messages(user_id, chat_id, after, limit)
|
||||
items.extend(page)
|
||||
assert len(items) <= max_items, "cursor traversal repeats messages (order is broken)"
|
||||
if not has_more:
|
||||
return items
|
||||
after = page[-1].key
|
||||
|
||||
|
||||
@pytest.mark.parametrize("round_", range(20))
|
||||
def test_100_messages_from_10_clients_are_unique_and_ordered(round_):
|
||||
store = Store()
|
||||
users = [store.create_user(f"client_{i}", f"Клиент {i}") for i in range(CLIENTS)]
|
||||
chat = store.create_chat(users[0].id, "Нагрузка", [u.id for u in users])
|
||||
|
||||
def client(index: int):
|
||||
return [store.add_message(users[index].id, chat.id, f"c{index}-m{j}") for j in range(PER_CLIENT)]
|
||||
|
||||
confirmed = [m for batch in run_concurrently(CLIENTS, client) for m in batch]
|
||||
history = walk(store, users[3].id, chat.id, limit=7) # multi-page cursor traversal
|
||||
|
||||
# Compact messages on purpose: pytest's diff of 100 dataclasses is slow and unreadable.
|
||||
assert len(confirmed) == 100, f"confirmed {len(confirmed)} writes"
|
||||
ids = [m.id for m in history]
|
||||
assert len(ids) == 100, f"history has {len(ids)} messages"
|
||||
assert set(ids) == {m.id for m in confirmed}, "a confirmed message is missing"
|
||||
assert len(set(ids)) == 100, "duplicated message in history"
|
||||
keys = [m.key for m in history]
|
||||
inversions = sum(1 for a, b in zip(keys, keys[1:]) if not a < b)
|
||||
assert inversions == 0, f"{inversions} neighbours violate strict (created_at, id) order"
|
||||
for i in range(CLIENTS):
|
||||
own = [m.text for m in history if m.sender_id == users[i].id]
|
||||
assert own == [f"c{i}-m{j}" for j in range(PER_CLIENT)], f"client {i} order changed"
|
||||
|
||||
|
||||
def test_timestamps_increase_even_if_wall_clock_goes_back():
|
||||
ticks = iter([5_000_000_000, 4_000_000_000, 4_000_000_000, 1_000_000_000] + [0] * 10)
|
||||
store = Store(clock_ns=lambda: next(ticks))
|
||||
user = store.create_user("clock_user", "Часы")
|
||||
chat = store.create_chat(user.id, "Часы", [user.id])
|
||||
messages = [store.add_message(user.id, chat.id, str(i)) for i in range(5)]
|
||||
stamps = [user.ts, chat.ts, *(m.ts for m in messages)]
|
||||
assert stamps == sorted(stamps) and len(set(stamps)) == len(stamps)
|
||||
assert [m.created_at for m in messages] == sorted(m.created_at for m in messages)
|
||||
|
||||
|
||||
def test_concurrent_registration_of_same_username_creates_one_user():
|
||||
store = Store()
|
||||
|
||||
def register(_index: int):
|
||||
try:
|
||||
return store.create_user("same_name", "Гонка")
|
||||
except Conflict:
|
||||
return None
|
||||
|
||||
created = [u for u in run_concurrently(20, register) if u is not None]
|
||||
assert len(created) == 1
|
||||
assert store.counts()["users"] == 1
|
||||
|
||||
|
||||
def test_concurrent_chat_creation_keeps_indexes_consistent():
|
||||
store = Store()
|
||||
users = [store.create_user(f"member_{i}", "Участник") for i in range(CLIENTS)]
|
||||
ids = [u.id for u in users]
|
||||
chats = run_concurrently(CLIENTS, lambda i: store.create_chat(ids[i], f"Чат {i}", ids))
|
||||
for user in users:
|
||||
listed, has_more = store.list_chats(user.id, None, 100)
|
||||
assert not has_more
|
||||
assert {c.id for c in listed} == {c.id for c in chats}
|
||||
assert [c.key for c in listed] == sorted(c.key for c in listed)
|
||||
@@ -0,0 +1,139 @@
|
||||
"""Registration, identification (X-User-Id), strict JSON validation and the error format."""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime as dt
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_health_endpoints(api):
|
||||
assert api.call("GET", "/health/live", 200, schema="Health").json() == {"status": "ok"}
|
||||
assert api.call("GET", "/health/ready", 200, schema="Health").json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_readiness_is_503_while_draining(api, app):
|
||||
app.state.draining = True
|
||||
body = api.call("GET", "/health/ready", 503).json()
|
||||
assert body["error"]["code"] == "unavailable"
|
||||
assert api.call("GET", "/health/live", 200).json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_register_returns_user_with_server_fields_and_unicode(api):
|
||||
user = api.user("alice", display_name="Алиса 👩💻 Ωμέγα")
|
||||
assert user["display_name"] == "Алиса 👩💻 Ωμέγα"
|
||||
assert uuid.UUID(user["id"]).version == 4
|
||||
assert user["created_at"].endswith("Z")
|
||||
dt.datetime.fromisoformat(user["created_at"].replace("Z", "+00:00"))
|
||||
assert set(user) == {"id", "username", "display_name", "created_at"}
|
||||
|
||||
|
||||
def test_duplicate_username_is_409(api):
|
||||
body = {"username": "dup_" + uuid.uuid4().hex[:8], "display_name": "Первый"}
|
||||
api.call("POST", "/api/v1/users", 201, json_body=body)
|
||||
error = api.call("POST", "/api/v1/users", 409, json_body={**body, "display_name": "Второй"}).json()
|
||||
assert error["error"]["code"] == "conflict"
|
||||
|
||||
|
||||
def test_display_name_limit_is_in_code_points(api):
|
||||
api.call("POST", "/api/v1/users", 201, json_body={"username": "emoji_ok", "display_name": "😀" * 100})
|
||||
api.call("POST", "/api/v1/users", 400, json_body={"username": "emoji_bad", "display_name": "😀" * 101})
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw", [
|
||||
b"{", # malformed JSON
|
||||
b"", # empty body
|
||||
b"[]", # not an object
|
||||
b'"alice"',
|
||||
b"null",
|
||||
b'{"username": "abc_def", "display_name": NaN}', # NaN is not JSON
|
||||
b'{"username": "abc_def", "display_name": "\xff\xfe"}', # invalid UTF-8
|
||||
b'{"username": "abc_def", "display_name": "\\ud800"}', # lone surrogate
|
||||
b'{"username": "abc_def", "display_name": "x"}{"a": 1}', # two objects glued together
|
||||
])
|
||||
def test_malformed_json_is_400(api, raw):
|
||||
error = api.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": "application/json"}).json()
|
||||
assert error["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("body", [
|
||||
{"username": "abc_def"}, # missing field
|
||||
{"username": "abc_def", "display_name": "x", "extra": 1}, # unknown field
|
||||
{"username": "abc_def", "display_name": "x", "password": "p"}, # lab2 field is not accepted yet
|
||||
{"username": 123, "display_name": "x"}, # wrong type
|
||||
{"username": "abc_def", "display_name": None},
|
||||
{"username": "abc_def", "display_name": ["x"]},
|
||||
{"username": "abc_def", "display_name": ""},
|
||||
{"username": "abc_def", "display_name": " \t\n "}, # only whitespace
|
||||
{"username": "ab", "display_name": "x"}, # too short
|
||||
{"username": "a" * 33, "display_name": "x"}, # too long
|
||||
{"username": "Alice", "display_name": "x"}, # upper case
|
||||
{"username": "алиса", "display_name": "x"}, # non-ASCII
|
||||
{"username": "abc-def", "display_name": "x"},
|
||||
{"username": "abc_def\n", "display_name": "x"}, # `$` trap in regex
|
||||
])
|
||||
def test_invalid_user_fields_are_400(api, body):
|
||||
api.call("POST", "/api/v1/users", 400, json_body=body)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("content_type", ["text/plain", "application/x-www-form-urlencoded", "application/json; charset=latin-1"])
|
||||
def test_wrong_content_type_is_400(api, content_type):
|
||||
raw = b'{"username": "abc_def", "display_name": "x"}'
|
||||
api.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": content_type})
|
||||
|
||||
|
||||
def test_json_with_utf8_charset_is_accepted(api):
|
||||
raw = '{"username": "charset_ok", "display_name": "Юникод"}'.encode()
|
||||
api.call("POST", "/api/v1/users", 201, raw=raw, headers={"Content-Type": "application/json; charset=UTF-8"})
|
||||
|
||||
|
||||
def test_oversized_body_is_413(api):
|
||||
body = {"username": "big_body", "display_name": "x" * 70_000}
|
||||
assert api.call("POST", "/api/v1/users", 413, json_body=body).json()["error"]["code"] == "payload_too_large"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("header", [None, "", "not-a-uuid", str(uuid.uuid4()), "11111111-1111-4111-8111-11111111111"])
|
||||
def test_missing_or_unknown_identity_is_401(api, header):
|
||||
headers = {} if header is None else {"X-User-Id": header}
|
||||
for method, path in [("GET", "/api/v1/users/me"), ("GET", "/api/v1/chats"), ("GET", f"/api/v1/chats/{uuid.uuid4()}")]:
|
||||
error = api.call(method, path, 401, headers=headers).json()
|
||||
assert error["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
def test_me_returns_selected_user(api):
|
||||
alice, bob = api.user("alice"), api.user("bob")
|
||||
assert api.call("GET", "/api/v1/users/me", 200, user=alice, schema="User").json() == alice
|
||||
assert api.call("GET", "/api/v1/users/me", 200, user=bob).json() == bob
|
||||
upper = api.call("GET", "/api/v1/users/me", 200, headers={"X-User-Id": alice["id"].upper()}).json()
|
||||
assert upper == alice
|
||||
|
||||
|
||||
def test_unknown_route_and_method_use_error_format(api):
|
||||
assert api.call("GET", "/api/v1/nope", 404).json()["error"]["code"] == "not_found"
|
||||
api.call("DELETE", "/api/v1/users", 405)
|
||||
|
||||
|
||||
def test_request_id_is_unique_and_propagated(api):
|
||||
first = api.call("GET", "/health/live", 200).headers["x-request-id"]
|
||||
second = api.call("GET", "/health/live", 200).headers["x-request-id"]
|
||||
assert first != second
|
||||
assert api.call("GET", "/health/live", 200, headers={"X-Request-Id": "trace-42"}).headers["x-request-id"] == "trace-42"
|
||||
|
||||
|
||||
def test_unexpected_exception_is_500_without_details(api, app, monkeypatch):
|
||||
def boom(*_args, **_kwargs):
|
||||
raise RuntimeError("secret internal detail")
|
||||
|
||||
monkeypatch.setattr(app.state.store, "get_user", boom)
|
||||
response = api.call("GET", "/api/v1/users/me", 500, headers={"X-User-Id": str(uuid.uuid4())})
|
||||
assert response.json()["error"]["code"] == "internal_error"
|
||||
assert "secret" not in response.text
|
||||
|
||||
|
||||
def test_swagger_ui_and_contract_are_served(api):
|
||||
page = api.call("GET", "/docs", 200)
|
||||
assert "swagger-ui" in page.text
|
||||
spec = api.call("GET", "/openapi.json", 200).json()
|
||||
assert spec["info"]["title"].startswith("MTUSI Messenger")
|
||||
assert spec["servers"] == [{"url": "/", "description": "этот сервер"}]
|
||||
assert "/api/v1/chats/{chat_id}/messages" in spec["paths"]
|
||||
Reference in New Issue
Block a user