From df49fb5df4c343a3d5566c6bd4788c824c82ed99 Mon Sep 17 00:00:00 2001 From: gytall Date: Fri, 30 Oct 2026 00:53:49 +0300 Subject: [PATCH] create skeleton for lab 3 --- .env.example | 52 ++- .gitignore | 4 + Dockerfile | 29 ++ Makefile | 147 ++++++++- alembic.ini | 38 +++ app/__init__.py | 0 app/__main__.py | 4 + app/core/__init__.py | 0 app/core/config.py | 95 ++++++ app/core/database.py | 44 +++ app/core/instance.py | 40 +++ app/core/lifecycle.py | 32 ++ app/core/redis.py | 38 +++ app/core/request_capacity.py | 40 +++ app/core/request_id.py | 22 ++ app/core/security.py | 63 ++++ app/domain/__init__.py | 1 + app/domain/auth.py | 84 +++++ app/domain/chats.py | 104 ++++++ app/domain/idempotency.py | 75 +++++ app/domain/messages.py | 62 ++++ app/domain/pagination.py | 83 +++++ app/domain/users.py | 41 +++ app/endpoints/__init__.py | 0 app/endpoints/auth.py | 32 ++ app/endpoints/chats.py | 32 ++ app/endpoints/dependencies.py | 100 ++++++ app/endpoints/health.py | 26 ++ app/endpoints/messages.py | 39 +++ app/endpoints/users.py | 17 + app/errors/__init__.py | 0 app/errors/exceptions.py | 85 +++++ app/errors/handlers.py | 197 ++++++++++++ app/main.py | 80 +++++ app/models/__init__.py | 8 + app/models/base.py | 27 ++ app/models/chat.py | 32 ++ app/models/chat_member.py | 26 ++ app/models/counter.py | 13 + app/models/idempotency_key.py | 28 ++ app/models/message.py | 30 ++ app/models/user.py | 31 ++ app/schemas/__init__.py | 6 + app/schemas/base.py | 35 ++ app/schemas/chat.py | 22 ++ app/schemas/message.py | 22 ++ app/schemas/session.py | 17 + app/schemas/user.py | 25 ++ app/storage/__init__.py | 1 + app/storage/chats.py | 71 +++++ app/storage/messages.py | 58 ++++ app/storage/sessions.py | 38 +++ app/storage/types.py | 6 + app/storage/users.py | 47 +++ compose.test.yaml | 10 + compose.yaml | 105 ++++++ infra/nginx/nginx.conf | 55 ++++ infra/redis/redis.conf | 5 + migrations/env.py | 68 ++++ migrations/script.py.mako | 25 ++ migrations/versions/.gitkeep | 0 .../versions/abd273076068_add_counters.py | 33 ++ .../db06fd3126ff_create_initial_schema.py | 107 +++++++ pytest.ini | 6 + requirements.txt | 50 +++ scripts/expiry_demo.sh | 28 ++ scripts/explain_history.py | 132 ++++++++ scripts/failure_demo.sh | 64 ++++ scripts/restart.sh | 35 ++ scripts/stop-one.sh | 21 ++ student-tests/conftest.py | 200 ++++++++++++ student-tests/helpers.py | 88 ++++++ student-tests/test_auth.py | 298 ++++++++++++++++++ student-tests/test_chats.py | 209 ++++++++++++ student-tests/test_concurrency.py | 266 ++++++++++++++++ student-tests/test_constraints.py | 46 +++ student-tests/test_errors.py | 76 +++++ student-tests/test_failures.py | 138 ++++++++ student-tests/test_health.py | 62 ++++ student-tests/test_history_plan.py | 78 +++++ student-tests/test_limits.py | 98 ++++++ student-tests/test_messages.py | 237 ++++++++++++++ student-tests/test_migrations.py | 67 ++++ student-tests/test_pagination.py | 137 ++++++++ student-tests/test_sessions.py | 173 ++++++++++ student-tests/test_users.py | 128 ++++++++ 86 files changed, 5284 insertions(+), 10 deletions(-) create mode 100644 Dockerfile create mode 100644 alembic.ini create mode 100644 app/__init__.py create mode 100644 app/__main__.py create mode 100644 app/core/__init__.py create mode 100644 app/core/config.py create mode 100644 app/core/database.py create mode 100644 app/core/instance.py create mode 100644 app/core/lifecycle.py create mode 100644 app/core/redis.py create mode 100644 app/core/request_capacity.py create mode 100644 app/core/request_id.py create mode 100644 app/core/security.py create mode 100644 app/domain/__init__.py create mode 100644 app/domain/auth.py create mode 100644 app/domain/chats.py create mode 100644 app/domain/idempotency.py create mode 100644 app/domain/messages.py create mode 100644 app/domain/pagination.py create mode 100644 app/domain/users.py create mode 100644 app/endpoints/__init__.py create mode 100644 app/endpoints/auth.py create mode 100644 app/endpoints/chats.py create mode 100644 app/endpoints/dependencies.py create mode 100644 app/endpoints/health.py create mode 100644 app/endpoints/messages.py create mode 100644 app/endpoints/users.py create mode 100644 app/errors/__init__.py create mode 100644 app/errors/exceptions.py create mode 100644 app/errors/handlers.py create mode 100644 app/main.py create mode 100644 app/models/__init__.py create mode 100644 app/models/base.py create mode 100644 app/models/chat.py create mode 100644 app/models/chat_member.py create mode 100644 app/models/counter.py create mode 100644 app/models/idempotency_key.py create mode 100644 app/models/message.py create mode 100644 app/models/user.py create mode 100644 app/schemas/__init__.py create mode 100644 app/schemas/base.py create mode 100644 app/schemas/chat.py create mode 100644 app/schemas/message.py create mode 100644 app/schemas/session.py create mode 100644 app/schemas/user.py create mode 100644 app/storage/__init__.py create mode 100644 app/storage/chats.py create mode 100644 app/storage/messages.py create mode 100644 app/storage/sessions.py create mode 100644 app/storage/types.py create mode 100644 app/storage/users.py create mode 100644 compose.test.yaml create mode 100644 compose.yaml create mode 100644 infra/nginx/nginx.conf create mode 100644 infra/redis/redis.conf create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/.gitkeep create mode 100644 migrations/versions/abd273076068_add_counters.py create mode 100644 migrations/versions/db06fd3126ff_create_initial_schema.py create mode 100644 pytest.ini create mode 100644 requirements.txt create mode 100755 scripts/expiry_demo.sh create mode 100644 scripts/explain_history.py create mode 100755 scripts/failure_demo.sh create mode 100755 scripts/restart.sh create mode 100755 scripts/stop-one.sh create mode 100644 student-tests/conftest.py create mode 100644 student-tests/helpers.py create mode 100644 student-tests/test_auth.py create mode 100644 student-tests/test_chats.py create mode 100644 student-tests/test_concurrency.py create mode 100644 student-tests/test_constraints.py create mode 100644 student-tests/test_errors.py create mode 100644 student-tests/test_failures.py create mode 100644 student-tests/test_health.py create mode 100644 student-tests/test_history_plan.py create mode 100644 student-tests/test_limits.py create mode 100644 student-tests/test_messages.py create mode 100644 student-tests/test_migrations.py create mode 100644 student-tests/test_pagination.py create mode 100644 student-tests/test_sessions.py create mode 100644 student-tests/test_users.py diff --git a/.env.example b/.env.example index d32b9be0..bc813a80 100644 --- a/.env.example +++ b/.env.example @@ -1,13 +1,51 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. +# Скопируйте в .env для локального окружения; .env не коммитится. +# Имена переменных соответствуют полям app/core/config.py. APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab +LOG_LEVEL=INFO + +MAX_MESSAGES=100000 +# Лимит одновременных запросов НА ОДНУ реплику +MAX_IN_FLIGHT=16 + SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url +SESSION_STORE=redis + +REDIS_HOST=redis +REDIS_PORT=6379 +REDIS_DB=0 +REDIS_PASSWORD=change-me +REDIS_MAX_CONNECTIONS=20 +REDIS_POOL_TIMEOUT_SECONDS=1 +REDIS_CONNECT_TIMEOUT_SECONDS=1 +REDIS_SOCKET_TIMEOUT_SECONDS=0.5 + +DB_HOST=postgres +DB_PORT=5432 +DB_NAME=messenger +DB_USER=messenger +DB_PASSWORD=change-me + +# Пул на одну реплику: суммарно API_REPLICAS * (POOL_SIZE + MAX_OVERFLOW) +# соединений должно помещаться в max_connections PostgreSQL. +DB_POOL_SIZE=5 +DB_MAX_OVERFLOW=10 +DB_POOL_TIMEOUT_SECONDS=3 +DB_CONNECT_TIMEOUT_SECONDS=3 +DB_STATEMENT_TIMEOUT_MS=5000 + +# --- Балансировка и идемпотентность --- API_REPLICAS=2 IDEMPOTENCY_TTL_SECONDS=86400 +# Пусто -> id вычисляется в app/core/instance.py (TODO 3.2) +# INSTANCE_ID=api-1 +# TODO(4.1): сколько секунд отдавать readiness=503 перед закрытием сокета +SHUTDOWN_DRAIN_SECONDS=0 +GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS=5 + +# Порт единственной публичной точки входа (nginx) +LB_HOST_PORT=8080 +# Используются только тестовым стендом (compose.test.yaml) +DB_HOST_PORT=15432 +REDIS_HOST_PORT=16379 diff --git a/.gitignore b/.gitignore index b5a71896..c0355e14 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,7 @@ coverage/ .vscode/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. + +/venv +.ruff_cache/ +/docs \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..e1b67766 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM python:3.12.15-slim-bookworm + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=1 \ + PIP_DISABLE_PIP_VERSION_CHECK=1 + +WORKDIR /app + +RUN useradd --system --uid 10001 --no-create-home appuser + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY alembic.ini ./ +COPY migrations ./migrations +COPY app ./app + +USER appuser + +# TODO(4.1): uvicorn корректно завершается по SIGTERM (сигнал Docker по умолчанию). +# Проверьте, что python — PID 1 (exec-форма CMD), иначе сигнал не дойдёт. + +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD ["python", "-c", "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:' + os.environ['APP_PORT'] + '/health/live', timeout=2)"] + +CMD ["python", "-m", "app"] diff --git a/Makefile b/Makefile index 5d86d171..cbc1821b 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,54 @@ -PYTHON ?= python3 +-include .env + +PYTHON ?= $(if $(wildcard venv/bin/python),venv/bin/python,python3) BASE_URL ?= http://localhost:8080 CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +COMPOSE = docker compose --env-file .env + +# Отдельный тестовый стенд: свой compose-проект, свои порты, открытые БД/Redis +# (compose.test.yaml). COMPOSE_FILE подхватывают и scripts/*.sh. +TEST_PROJECT ?= $(notdir $(CURDIR))-test +TEST_LB_PORT ?= 18080 +TEST_DB_PORT ?= 15432 +TEST_REDIS_PORT ?= 16379 +TEST_BASE_URL = http://localhost:$(TEST_LB_PORT) +TEST_ENV = COMPOSE_PROJECT_NAME=$(TEST_PROJECT) COMPOSE_FILE=compose.yaml:compose.test.yaml \ + LB_HOST_PORT=$(TEST_LB_PORT) DB_HOST_PORT=$(TEST_DB_PORT) REDIS_HOST_PORT=$(TEST_REDIS_PORT) +TEST_COMPOSE = $(TEST_ENV) $(COMPOSE) + +TEST_DATABASE_URL ?= postgresql+asyncpg://$(DB_USER):$(DB_PASSWORD)@localhost:$(TEST_DB_PORT)/$(DB_NAME)_test +TEST_REDIS_HOST ?= localhost +TEST_REDIS_PASSWORD ?= $(REDIS_PASSWORD) +TEST_REDIS_DB ?= 15 +export TEST_DATABASE_URL TEST_REDIS_HOST TEST_REDIS_PORT TEST_REDIS_PASSWORD TEST_REDIS_DB + +define wait_ready + @for i in $$(seq 1 90); do \ + curl -sf "$(1)/health/ready" >/dev/null && exit 0; sleep 1; \ + done; echo "API $(1) не готов за 90 секунд" >&2; exit 1 +endef + +.PHONY: help check validate test load test-persistence test-failover \ + up down restart restore ps logs migrate migration instances \ + test-stand-up test-stand-down test-stand-destroy test-stand-ps \ + test-stand-smoke test-stand-persistence test-stand-failover \ + test-own test-docker test-limits test-concurrency test-migrations test-all \ + explain expiry-demo failure-demo help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "Проверки: check | validate | test | load | test-persistence ACTION_SCRIPT=scripts/restart.sh" + @echo " test-failover ACTION_SCRIPT=scripts/stop-one.sh" + @echo "Основной: up | down | restart | restore | ps | logs | migrate | instances" + @echo "Тестовый: test-stand-up | test-stand-down | test-stand-destroy | test-stand-ps" + @echo " test-stand-smoke | test-stand-persistence | test-stand-failover" + @echo "Тесты: test-own | test-docker | test-limits | test-concurrency | test-migrations | test-all" + @echo "Демо: explain | expiry-demo | failure-demo (на тестовом стенде)" + @echo "Миграции: migration m='описание' (нужен поднятый тестовый стенд)" + +# --- Проверки API и сценариев отказа (tests/) --- check: $(PYTHON) scripts/check_contract.py @@ -28,3 +69,103 @@ test-persistence: test-failover: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +# --- Основной стенд --- + +up: + $(COMPOSE) up -d --build + $(call wait_ready,$(BASE_URL)) + +down: + $(COMPOSE) down + +restart: + scripts/restart.sh + $(call wait_ready,$(BASE_URL)) + +# Вернуть остановленную stop-one.sh реплику (compose up стартует остановленные контейнеры). +restore: + $(COMPOSE) up -d --wait api + +ps: + $(COMPOSE) ps + +logs: + $(COMPOSE) logs -f api nginx + +migrate: + $(COMPOSE) run --rm --build migrate + +# Показать, какие реплики отвечают через LB (по заголовку X-Instance-Id). +instances: + @for i in $$(seq 1 10); do \ + curl -s -o /dev/null -D - "$(BASE_URL)/health/live" | grep -i '^x-instance-id' || echo "нет X-Instance-Id"; \ + done + +# Генерация миграции против БД тестового стенда (основной стенд БД не публикует). +migration: + DB_HOST=localhost DB_PORT=$(TEST_DB_PORT) $(PYTHON) -m alembic revision --autogenerate -m "$(m)" + +# --- Тестовый стенд --- + +test-stand-up: + $(TEST_COMPOSE) up -d --build + $(call wait_ready,$(TEST_BASE_URL)) + $(TEST_COMPOSE) exec -T postgres sh -c 'createdb -U "$$POSTGRES_USER" "$$POSTGRES_DB"_test 2>&1 | grep -v "already exists"; true' + +test-stand-down: + $(TEST_COMPOSE) down + +test-stand-destroy: + $(TEST_COMPOSE) down -v + +test-stand-ps: + $(TEST_COMPOSE) ps + +test-stand-smoke: + $(PYTHON) tests/smoke.py --base-url "$(TEST_BASE_URL)" + +test-stand-persistence: + $(TEST_COMPOSE) ps + $(TEST_ENV) $(PYTHON) tests/resilience.py --mode persistence --action-script scripts/restart.sh --base-url "$(TEST_BASE_URL)" + $(TEST_COMPOSE) ps + +test-stand-failover: + $(TEST_COMPOSE) ps + $(TEST_ENV) $(PYTHON) tests/resilience.py --mode failover --action-script scripts/stop-one.sh --base-url "$(TEST_BASE_URL)" + $(TEST_COMPOSE) ps -a api + $(TEST_COMPOSE) up -d --wait api + +# --- Собственные тесты (student-tests/) --- + +test-own: + $(PYTHON) -m pytest -q + +test-docker: + $(TEST_ENV) DOCKER_TESTS=1 $(PYTHON) -m pytest -q + +test-limits: + $(PYTHON) -m pytest student-tests/test_limits.py -q + +test-concurrency: + $(PYTHON) -m pytest student-tests/test_concurrency.py -q + +test-migrations: + $(PYTHON) -m pytest student-tests/test_migrations.py -q + +test-all: test-stand-up test-docker test-stand-smoke test-stand-persistence + +# --- Демо (на тестовом стенде) --- + +explain: + $(TEST_ENV) DB_HOST=localhost DB_PORT=$(TEST_DB_PORT) PYTHONPATH=. $(PYTHON) scripts/explain_history.py + +expiry-demo: test-stand-up + $(TEST_ENV) SESSION_TTL_SECONDS=3 $(COMPOSE) up -d api + $(call wait_ready,$(TEST_BASE_URL)) + BASE_URL=$(TEST_BASE_URL) TTL=3 scripts/expiry_demo.sh + $(TEST_COMPOSE) up -d api + $(call wait_ready,$(TEST_BASE_URL)) + +failure-demo: test-stand-up + $(TEST_ENV) BASE_URL=$(TEST_BASE_URL) scripts/failure_demo.sh diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..f95b20ac --- /dev/null +++ b/alembic.ini @@ -0,0 +1,38 @@ +[alembic] +script_location = migrations +prepend_sys_path = . +path_separator = os + +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARNING +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARNING +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/__main__.py b/app/__main__.py new file mode 100644 index 00000000..ee209955 --- /dev/null +++ b/app/__main__.py @@ -0,0 +1,4 @@ +from app.main import main + +if __name__ == "__main__": + main() diff --git a/app/core/__init__.py b/app/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 00000000..20d0069b --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,95 @@ +from enum import StrEnum +from typing import Literal, Self + +from pydantic import Field, SecretStr, model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict +from sqlalchemy import URL + +ASYNC_DRIVER = "postgresql+asyncpg" + + +class LogLevel(StrEnum): + DEBUG = "DEBUG" + INFO = "INFO" + WARNING = "WARNING" + ERROR = "ERROR" + CRITICAL = "CRITICAL" + + +class Settings(BaseSettings): + app_host: str + app_port: int = Field(ge=1, le=65535) + app_env: str + log_level: LogLevel = LogLevel.INFO + + max_messages: int = Field(ge=1) + max_in_flight: int = Field(ge=1) + + session_ttl_seconds: int = Field(default=3600, ge=1) + session_store: Literal["redis"] = "redis" + + redis_host: str = "localhost" + redis_port: int = Field(default=6379, ge=1, le=65535) + redis_db: int = Field(default=0, ge=0) + redis_password: SecretStr | None = None + + redis_max_connections: int = Field(default=20, ge=1) + redis_pool_timeout_seconds: float = Field(default=1.0, gt=0) + redis_connect_timeout_seconds: float = Field(default=1.0, gt=0) + redis_socket_timeout_seconds: float = Field(default=0.5, gt=0) + + database_url: SecretStr | None = None + db_host: str | None = None + db_port: int = Field(default=5432, ge=1, le=65535) + db_name: str | None = None + db_user: str | None = None + db_password: SecretStr | None = None + + db_pool_size: int = Field(default=5, ge=1) + db_max_overflow: int = Field(default=5, ge=0) + db_pool_timeout_seconds: float = Field(default=3.0, gt=0) + db_connect_timeout_seconds: int = Field(default=3, ge=1) + db_statement_timeout_ms: int = Field(default=5000, ge=1) + + # Балансировка между репликами и идемпотентность + instance_id: str | None = None # TODO(3.2): None -> вычислить в resolve_instance_id + idempotency_ttl_seconds: int = Field(default=86400, ge=1) # TODO(3.4) + shutdown_drain_seconds: float = Field(default=0, ge=0) # TODO(4.1) + graceful_shutdown_timeout_seconds: int = Field(default=5, ge=1) # TODO(4.1) + + # TODO(4.3): секреты из файлов (Docker secrets): pydantic-settings умеет + # secrets_dir="/run/secrets" — имя файла = имя поля (db_password, ...). + model_config = SettingsConfigDict( + env_file=".env", + env_file_encoding="utf-8", + case_sensitive=False, + extra="ignore", + ) + + @model_validator(mode="after") + def _require_database_source(self) -> Self: + if self.database_url is not None: + if not self.database_url.get_secret_value().startswith(ASYNC_DRIVER): + raise ValueError(f"DATABASE_URL должен начинаться с {ASYNC_DRIVER}://") + return self + + if not (self.db_host and self.db_name and self.db_user and self.db_password): + raise ValueError( + "Задайте DATABASE_URL или DB_HOST, DB_NAME, DB_USER и DB_PASSWORD" + ) + return self + + def get_database_url(self) -> str: + if self.database_url is not None: + return self.database_url.get_secret_value() + + assert self.db_password is not None + url = URL.create( + ASYNC_DRIVER, + username=self.db_user, + password=self.db_password.get_secret_value(), + host=self.db_host, + port=self.db_port, + database=self.db_name, + ) + return url.render_as_string(hide_password=False) diff --git a/app/core/database.py b/app/core/database.py new file mode 100644 index 00000000..c87bf430 --- /dev/null +++ b/app/core/database.py @@ -0,0 +1,44 @@ +import logging + +from sqlalchemy import text +from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.ext.asyncio import ( + AsyncEngine, + AsyncSession, + async_sessionmaker, + create_async_engine, +) + +from app.core.config import Settings + +logger = logging.getLogger(__name__) + + +def create_db_engine(settings: Settings) -> AsyncEngine: + return create_async_engine( + settings.get_database_url(), + pool_size=settings.db_pool_size, + max_overflow=settings.db_max_overflow, + pool_timeout=settings.db_pool_timeout_seconds, + pool_pre_ping=True, + connect_args={ + "timeout": settings.db_connect_timeout_seconds, + "server_settings": { + "statement_timeout": str(settings.db_statement_timeout_ms) + }, + }, + ) + + +def create_session_factory(engine: AsyncEngine) -> async_sessionmaker[AsyncSession]: + return async_sessionmaker(bind=engine, expire_on_commit=False, autoflush=False) + + +async def check_database(engine: AsyncEngine) -> bool: + try: + async with engine.connect() as connection: + await connection.execute(text("SELECT 1")) + except (SQLAlchemyError, OSError, TimeoutError): + logger.warning("database readiness check failed", exc_info=True) + return False + return True diff --git a/app/core/instance.py b/app/core/instance.py new file mode 100644 index 00000000..6ab0d3f1 --- /dev/null +++ b/app/core/instance.py @@ -0,0 +1,40 @@ +"""Идентификатор API-реплики и заголовок X-Instance-Id.""" + +from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint +from starlette.requests import Request +from starlette.responses import Response + +INSTANCE_ID_HEADER = "X-Instance-Id" + + +def resolve_instance_id(configured: str | None) -> str: + """Возвращает id реплики, стабильный в течение жизни процесса. + + TODO(3.2): выбрать источник id. + - Если задан непустой INSTANCE_ID в настройках — вернуть его. + - Иначе — что-то, что позволит stop-one.sh найти именно этот контейнер + (подсказка: Docker кладёт короткий id контейнера в переменную HOSTNAME, + а `docker stop <короткий id>` работает). + - Значение вычисляется один раз при старте и не меняется до конца процесса. + """ + raise NotImplementedError("TODO(3.2): resolve_instance_id") + + +class InstanceIdMiddleware(BaseHTTPMiddleware): + """Добавляет X-Instance-Id в каждый ответ API, включая ошибки и /health/*. + + TODO(3.2): реализовать dispatch по образцу RequestIdMiddleware + (app/core/request_id.py) и подключить в create_app (app/main.py). + Проверьте, что заголовок есть и у ответов из exception handlers. + """ + + def __init__(self, app, instance_id: str) -> None: + super().__init__(app) + self.instance_id = instance_id + + async def dispatch( + self, + request: Request, + call_next: RequestResponseEndpoint, + ) -> Response: + raise NotImplementedError("TODO(3.2): InstanceIdMiddleware.dispatch") diff --git a/app/core/lifecycle.py b/app/core/lifecycle.py new file mode 100644 index 00000000..5756d856 --- /dev/null +++ b/app/core/lifecycle.py @@ -0,0 +1,32 @@ +"""Состояние жизненного цикла реплики: готова / уходит (graceful shutdown). + +Идея: при SIGTERM реплика сначала перестаёт быть "ready" (/health/ready -> 503), +чтобы балансировщик успел исключить её, и только затем uvicorn перестаёт +принимать соединения и дожидается текущих запросов. +""" + + +class Lifecycle: + def __init__(self) -> None: + self._draining = False + + @property + def draining(self) -> bool: + return self._draining + + def start_draining(self) -> None: + """TODO(4.1): вызвать при получении SIGTERM / в начале shutdown.""" + self._draining = True + + +# TODO(4.1): решить, где перехватывать SIGTERM. +# uvicorn сам ставит обработчик SIGTERM и сразу закрывает listening socket, +# поэтому "сначала readiness=503, потом закрыть приём" нужно сделать явно: +# - либо обернуть/переопределить обработчик сигнала uvicorn +# (uvicorn.Server.handle_exit) так, чтобы он сначала вызывал +# lifecycle.start_draining(), ждал SHUTDOWN_DRAIN_SECONDS и только потом +# передавал управление штатному завершению; +# - либо полагаться на passive-проверки nginx (proxy_next_upstream) и честно +# описать в REPORT.md, сколько запросов теряется. +# Сумма drain + timeout_graceful_shutdown должна быть меньше stop_grace_period +# в compose.yaml, иначе Docker добьёт процесс SIGKILL. diff --git a/app/core/redis.py b/app/core/redis.py new file mode 100644 index 00000000..921d4d12 --- /dev/null +++ b/app/core/redis.py @@ -0,0 +1,38 @@ +import logging + +from redis.asyncio import BlockingConnectionPool, Redis +from redis.asyncio.retry import Retry +from redis.backoff import NoBackoff +from redis.exceptions import RedisError + +from app.core.config import Settings + +logger = logging.getLogger(__name__) + + +def create_redis_client(settings: Settings) -> Redis: + password = ( + settings.redis_password.get_secret_value() if settings.redis_password else None + ) + pool = BlockingConnectionPool( + host=settings.redis_host, + port=settings.redis_port, + db=settings.redis_db, + password=password, + max_connections=settings.redis_max_connections, + timeout=settings.redis_pool_timeout_seconds, + socket_connect_timeout=settings.redis_connect_timeout_seconds, + socket_timeout=settings.redis_socket_timeout_seconds, + retry=Retry(NoBackoff(), retries=1), + decode_responses=True, + ) + return Redis.from_pool(pool) + + +async def check_redis(client: Redis) -> bool: + try: + await client.ping() + except (RedisError, OSError): + logger.warning("redis readiness check failed", exc_info=True) + return False + return True diff --git a/app/core/request_capacity.py b/app/core/request_capacity.py new file mode 100644 index 00000000..acfb15a3 --- /dev/null +++ b/app/core/request_capacity.py @@ -0,0 +1,40 @@ +from collections.abc import AsyncIterator + +from fastapi import Request + +from app.errors.exceptions import rate_limited + + +class RequestCapacity: + def __init__(self, max_in_flight: int) -> None: + self._max_in_flight = max_in_flight + self._in_flight = 0 + + @property + def in_flight(self) -> int: + return self._in_flight + + def try_acquire(self) -> bool: + if self._in_flight >= self._max_in_flight: + return False + self._in_flight += 1 + return True + + def release(self) -> None: + self._in_flight -= 1 + + +def create_request_capacity(max_in_flight: int) -> RequestCapacity: + return RequestCapacity(max_in_flight) + + +async def acquire_request_slot(request: Request) -> AsyncIterator[None]: + capacity: RequestCapacity = request.app.state.request_capacity + + if not capacity.try_acquire(): + raise rate_limited() + + try: + yield + finally: + capacity.release() diff --git a/app/core/request_id.py b/app/core/request_id.py new file mode 100644 index 00000000..a78a8607 --- /dev/null +++ b/app/core/request_id.py @@ -0,0 +1,22 @@ +from uuid import uuid4 + +from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint +from starlette.requests import Request +from starlette.responses import Response + +REQUEST_ID_HEADER = "X-Request-Id" + + +class RequestIdMiddleware(BaseHTTPMiddleware): + async def dispatch( + self, + request: Request, + call_next: RequestResponseEndpoint, + ) -> Response: + request_id = str(uuid4()) + request.state.request_id = request_id + + response = await call_next(request) + response.headers[REQUEST_ID_HEADER] = request_id + + return response diff --git a/app/core/security.py b/app/core/security.py new file mode 100644 index 00000000..70952e78 --- /dev/null +++ b/app/core/security.py @@ -0,0 +1,63 @@ +"""Хеширование паролей (Argon2id) и выпуск непрозрачных токенов сессий.""" + +import asyncio +import hashlib +import os +import secrets +from concurrent.futures import ThreadPoolExecutor + +from argon2 import PasswordHasher +from argon2.exceptions import InvalidHashError, VerificationError + +TOKEN_BYTES = 32 + +_hasher = PasswordHasher() +_executor = ThreadPoolExecutor( + max_workers=max(1, (os.cpu_count() or 1) // _hasher.parallelism), + thread_name_prefix="argon2", +) + + +def _hash_password_sync(password: str) -> str: + """Блокирующий расчёт Argon2id-хеша со случайной солью в формате PHC.""" + return _hasher.hash(password) + + +def _verify_password_sync(password: str, password_hash: str) -> bool: + """Блокирующая проверка пароля по PHC-строке; False при несовпадении или повреждённом хеше.""" + try: + return _hasher.verify(password_hash, password) + except (VerificationError, InvalidHashError): + return False + + +_DUMMY_HASH = _hash_password_sync(secrets.token_urlsafe(TOKEN_BYTES)) + + +async def hash_password(password: str) -> str: + """Хеширует пароль в пуле потоков, не блокируя event loop.""" + loop = asyncio.get_running_loop() + return await loop.run_in_executor(_executor, _hash_password_sync, password) + + +async def verify_password(password: str, password_hash: str) -> bool: + """Проверяет пароль в пуле потоков, не блокируя event loop.""" + loop = asyncio.get_running_loop() + return await loop.run_in_executor( + _executor, _verify_password_sync, password, password_hash + ) + + +async def dummy_verify_password(password: str) -> None: + """Проверка против фиктивного хеша: время ответа не выдаёт отсутствие пользователя.""" + await verify_password(password, _DUMMY_HASH) + + +def generate_token() -> str: + """Непрозрачный токен сессии: 32 случайных байта в base64url.""" + return secrets.token_urlsafe(TOKEN_BYTES) + + +def hash_token(token: str) -> str: + """SHA-256 токена в hex: детерминированный ключ для хранилища сессий.""" + return hashlib.sha256(token.encode()).hexdigest() diff --git a/app/domain/__init__.py b/app/domain/__init__.py new file mode 100644 index 00000000..93c96d54 --- /dev/null +++ b/app/domain/__init__.py @@ -0,0 +1 @@ +"""Бизнес-логика: проверки доступа, транзакции и ошибки API.""" diff --git a/app/domain/auth.py b/app/domain/auth.py new file mode 100644 index 00000000..63f4f3cc --- /dev/null +++ b/app/domain/auth.py @@ -0,0 +1,84 @@ +import re +from datetime import UTC, datetime, timedelta + +from redis.asyncio import Redis +from sqlalchemy.ext.asyncio import AsyncSession + +from app.core import security +from app.domain.users import to_user_schema +from app.errors.exceptions import unauthorized_basic, unauthorized_bearer +from app.schemas.session import AuthSession +from app.schemas.user import ( + PASSWORD_MAX_LENGTH, + PASSWORD_MIN_LENGTH, + USERNAME_PATTERN, + User, +) +from app.storage import sessions as sessions_storage +from app.storage import users as users_storage + +USERNAME_RE = re.compile(USERNAME_PATTERN) + + +async def create_session( + session: AsyncSession, + redis: Redis, + username: str, + password: str, + ttl_seconds: int, +) -> AuthSession: + """Проверяет учётные данные и выдаёт новый Bearer-токен.""" + if ( + USERNAME_RE.fullmatch(username) is None + or not PASSWORD_MIN_LENGTH <= len(password) <= PASSWORD_MAX_LENGTH + or not password.strip() + ): + raise unauthorized_basic() + + user = await users_storage.get_user_by_username(session, username) + await session.commit() + + if user is None: + await security.dummy_verify_password(password) + raise unauthorized_basic() + + if not await security.verify_password(password, user.password_hash): + raise unauthorized_basic() + + token = security.generate_token() + expires_at = datetime.now(UTC) + timedelta(seconds=ttl_seconds) + await sessions_storage.create_session( + redis, + token_hash=security.hash_token(token), + user_id=user.id, + expires_at=expires_at, + ) + auth_session = AuthSession( + token=token, expires_at=expires_at, user=to_user_schema(user) + ) + return auth_session + + +async def authenticate_token(session: AsyncSession, redis: Redis, token: str) -> User: + """Возвращает владельца действующей сессии или бросает 401.""" + user_id = await sessions_storage.get_session_user_id( + redis, security.hash_token(token) + ) + if user_id is None: + raise unauthorized_bearer() + + user = await users_storage.get_user_by_id(session, user_id) + if user is None: + raise unauthorized_bearer() + + current_user = to_user_schema(user) + return current_user + + +async def revoke_session(redis: Redis, token: str) -> None: + """Отзывает текущую сессию; повторный отзыв даёт 401.""" + is_deleted = await sessions_storage.delete_session( + redis, security.hash_token(token) + ) + if not is_deleted: + raise unauthorized_bearer() diff --git a/app/domain/chats.py b/app/domain/chats.py new file mode 100644 index 00000000..c810a982 --- /dev/null +++ b/app/domain/chats.py @@ -0,0 +1,104 @@ +from uuid import UUID + +from sqlalchemy.exc import IntegrityError +from sqlalchemy.ext.asyncio import AsyncSession + +from app.domain.pagination import ( + KIND_CHATS, + build_next_cursor, + decode_cursor, + split_page, +) +from app.errors.exceptions import invalid_request, not_found +from app.models.chat import Chat as ChatModel +from app.schemas.chat import Chat, ChatPage, CreateChat +from app.schemas.user import User +from app.storage import chats as chats_storage +from app.storage import users as users_storage +from app.storage.types import Position + +UNKNOWN_MEMBER_MESSAGE = "member_ids содержит неизвестного пользователя" + + +def to_chat_schema(chat: ChatModel) -> Chat: + """Преобразует ORM-модель чата с загруженными участниками в схему.""" + chat_schema = Chat( + id=chat.id, + title=chat.title, + member_ids=[member.user_id for member in chat.members], + created_at=chat.created_at, + ) + return chat_schema + + +def validate_new_chat(creator: User, new_chat: CreateChat) -> None: + """Проверяет состав участников без обращения к базе.""" + member_ids = new_chat.member_ids + + if creator.id not in member_ids: + raise invalid_request("Создатель чата должен входить в member_ids") + + if len(set(member_ids)) != len(member_ids): + raise invalid_request("member_ids содержит дубликаты") + + +async def create_chat( + session: AsyncSession, creator: User, new_chat: CreateChat +) -> Chat: + """Создаёт чат с участниками атомарно; неверный состав даёт 400.""" + validate_new_chat(creator, new_chat) + + existing_ids = await users_storage.get_existing_user_ids( + session, new_chat.member_ids + ) + if existing_ids != set(new_chat.member_ids): + raise invalid_request(UNKNOWN_MEMBER_MESSAGE) + + try: + chat = await chats_storage.create_chat( + session, title=new_chat.title, member_ids=new_chat.member_ids + ) + await session.commit() + except IntegrityError: + await session.rollback() + raise invalid_request(UNKNOWN_MEMBER_MESSAGE) from None + + created_chat = to_chat_schema(chat) + return created_chat + + +async def get_chat(session: AsyncSession, user: User, chat_id: UUID) -> Chat: + """Возвращает чат участнику; чужой и несуществующий чат дают 404.""" + chat = await chats_storage.get_chat_for_member(session, chat_id, user.id) + if chat is None: + raise not_found() + + found_chat = to_chat_schema(chat) + return found_chat + + +async def ensure_chat_member(session: AsyncSession, user: User, chat_id: UUID) -> None: + """Бросает 404, если пользователь не состоит в чате или чата нет.""" + if not await chats_storage.is_chat_member(session, chat_id, user.id): + raise not_found() + + +async def list_chats( + session: AsyncSession, + user: User, + cursor: str | None, + limit: int, +) -> ChatPage: + """Возвращает страницу чатов пользователя по (created_at, id).""" + after: Position | None = None + if cursor is not None: + after = decode_cursor(cursor, KIND_CHATS, user.id) + + rows = await chats_storage.list_chats_for_user(session, user.id, after, limit + 1) + page_rows, has_next = split_page(rows, limit) + items = [to_chat_schema(chat) for chat in page_rows] + page = ChatPage( + items=items, + next_cursor=build_next_cursor(KIND_CHATS, user.id, items, has_next), + ) + return page diff --git a/app/domain/idempotency.py b/app/domain/idempotency.py new file mode 100644 index 00000000..c5e1cff1 --- /dev/null +++ b/app/domain/idempotency.py @@ -0,0 +1,75 @@ +"""Идемпотентность POST сообщения, общая для всех API-реплик. + +Семантика: + - ключ: ASCII [A-Za-z0-9._:-]{1,128}; нет/неверный — 400; + - область уникальности: пользователь + метод + путь чата + ключ; + - тот же JSON в течение 24 ч -> тот же 201 и то же Message; + - другой JSON с тем же ключом -> 409; + - одновременные повторы через разные реплики -> ровно одно сообщение; + - повтор снова проверяет сессию и членство в чате; + - транзиентная 5xx не фиксируется как окончательный результат; + - побочный эффект (INSERT сообщения) и запись результата — атомарно. +""" + +import hashlib +import json +import re +from uuid import UUID + +from sqlalchemy.ext.asyncio import AsyncSession + +from app.schemas.message import CreateMessage, Message +from app.schemas.user import User + +IDEMPOTENCY_KEY_PATTERN = re.compile(r"[A-Za-z0-9._:-]{1,128}") + + +def validate_idempotency_key(key: str | None) -> str: + """Возвращает ключ или бросает invalid_request (400). + + TODO(3.4): проверить наличие и формат (fullmatch по IDEMPOTENCY_KEY_PATTERN). + """ + raise NotImplementedError("TODO(3.4): validate_idempotency_key") + + +def request_fingerprint(new_message: CreateMessage) -> str: + """Хеш семантического содержимого запроса для сравнения повторов. + + Порядок ключей JSON не должен влиять на результат, поэтому хешируйте + нормализованную форму (например, json.dumps(..., sort_keys=True) + от new_message.model_dump()), а не сырые байты тела. + """ + canonical = json.dumps( + new_message.model_dump(mode="json"), sort_keys=True, ensure_ascii=False + ) + return hashlib.sha256(canonical.encode()).hexdigest() + + +async def send_message_idempotent( + session: AsyncSession, + sender: User, + chat_id: UUID, + new_message: CreateMessage, + idempotency_key: str, + max_messages: int, + ttl_seconds: int, +) -> Message: + """Отправляет сообщение ровно один раз для (sender, POST, chat_id, key). + + TODO(3.4). Один из рабочих вариантов на PostgreSQL: + 1. ensure_chat_member(...) — доступ проверяется и на повторе. + 2. В одной транзакции: + INSERT INTO idempotency_keys (...) VALUES (...) + ON CONFLICT (user_id, method, path, key) DO NOTHING RETURNING ... + - вставилось -> создать сообщение, записать message_id в строку ключа, + COMMIT (сообщение и ключ фиксируются вместе или не фиксируются вовсе); + - конфликт -> прочитать существующую строку. Параллельная транзакция + с тем же ключом будет ждать на уникальном индексе, пока первая + не завершится, поэтому дубля не будет. + 3. Если fingerprint отличается -> conflict() (409). + 4. Если совпадает -> вернуть сохранённое сообщение (тот же id/created_at). + 5. Просроченные (> ttl_seconds) ключи считать отсутствующими. + Альтернатива — Redis SET NX + PostgreSQL; тогда отдельно продумайте + атомарность "сообщение записано, а ключ — нет" и наоборот. + """ + raise NotImplementedError("TODO(3.4): send_message_idempotent") diff --git a/app/domain/messages.py b/app/domain/messages.py new file mode 100644 index 00000000..2f7405f1 --- /dev/null +++ b/app/domain/messages.py @@ -0,0 +1,62 @@ +from uuid import UUID + +from sqlalchemy.ext.asyncio import AsyncSession + +from app.domain.chats import ensure_chat_member +from app.domain.pagination import ( + KIND_MESSAGES, + build_next_cursor, + decode_cursor, + split_page, +) +from app.errors.exceptions import unavailable +from app.schemas.message import CreateMessage, Message, MessagePage +from app.schemas.user import User +from app.storage import messages as messages_storage +from app.storage.types import Position + + +async def send_message( + session: AsyncSession, + sender: User, + chat_id: UUID, + new_message: CreateMessage, + max_messages: int, +) -> Message: + """Отправляет сообщение от имени участника; при исчерпании лимита даёт 503.""" + await ensure_chat_member(session, sender, chat_id) + + if not await messages_storage.reserve_message_slot(session, max_messages): + raise unavailable("Достигнут лимит количества сообщений") + + message = await messages_storage.create_message( + session, chat_id=chat_id, sender_id=sender.id, text=new_message.text + ) + await session.commit() + + sent_message = Message.model_validate(message) + return sent_message + + +async def list_messages( + session: AsyncSession, + user: User, + chat_id: UUID, + cursor: str | None, + limit: int, +) -> MessagePage: + """Возвращает страницу истории чата по (created_at, id).""" + after: Position | None = None + if cursor is not None: + after = decode_cursor(cursor, KIND_MESSAGES, chat_id) + + await ensure_chat_member(session, user, chat_id) + + rows = await messages_storage.list_messages(session, chat_id, after, limit + 1) + page_rows, has_next = split_page(rows, limit) + items = [Message.model_validate(message) for message in page_rows] + page = MessagePage( + items=items, + next_cursor=build_next_cursor(KIND_MESSAGES, chat_id, items, has_next), + ) + return page diff --git a/app/domain/pagination.py b/app/domain/pagination.py new file mode 100644 index 00000000..9894437c --- /dev/null +++ b/app/domain/pagination.py @@ -0,0 +1,83 @@ +import base64 +import json +from collections.abc import Sequence +from datetime import datetime +from typing import Protocol, TypeVar +from uuid import UUID + +from app.errors.exceptions import invalid_request +from app.storage.types import Position + +KIND_CHATS = "chats" +KIND_MESSAGES = "messages" + +T = TypeVar("T") + + +class Positioned(Protocol): + """Объект с позицией в порядке (created_at, id).""" + + @property + def id(self) -> UUID: ... + + @property + def created_at(self) -> datetime: ... + + +def encode_cursor( + kind: str, + resource_id: UUID, + created_at: datetime, + item_id: UUID, +) -> str: + """Кодирует позицию и ресурс, к которому она привязана, в непрозрачный cursor.""" + payload = { + "kind": kind, + "resource_id": str(resource_id), + "created_at": created_at.isoformat(), + "item_id": str(item_id), + } + raw = json.dumps(payload).encode() + return base64.urlsafe_b64encode(raw).decode() + + +def decode_cursor(cursor: str, kind: str, resource_id: UUID) -> Position: + """Возвращает позицию из cursor; битый или чужой cursor даёт 400.""" + try: + payload = json.loads(base64.urlsafe_b64decode(cursor)) + cursor_kind = payload["kind"] + cursor_resource_id = UUID(payload["resource_id"]) + created_at = datetime.fromisoformat(payload["created_at"]) + item_id = UUID(payload["item_id"]) + except (ValueError, KeyError, TypeError, AttributeError): + raise invalid_request("Некорректный cursor") from None + + if ( + cursor_kind != kind + or cursor_resource_id != resource_id + or created_at.tzinfo is None + ): + raise invalid_request("Некорректный cursor") + + return created_at, item_id + + +def split_page(rows: list[T], limit: int) -> tuple[list[T], bool]: + """Отделяет страницу от лишней строки limit + 1, по которой видно продолжение.""" + has_next = len(rows) > limit + items = rows[:limit] + return items, has_next + + +def build_next_cursor( + kind: str, + resource_id: UUID, + items: Sequence[Positioned], + has_next: bool, +) -> str | None: + """Возвращает cursor на последний элемент страницы или None, если она последняя.""" + if not has_next: + return None + + last_item = items[-1] + return encode_cursor(kind, resource_id, last_item.created_at, last_item.id) diff --git a/app/domain/users.py b/app/domain/users.py new file mode 100644 index 00000000..461bf0d9 --- /dev/null +++ b/app/domain/users.py @@ -0,0 +1,41 @@ +from sqlalchemy.exc import IntegrityError +from sqlalchemy.ext.asyncio import AsyncSession + +from app.core import security +from app.errors.exceptions import conflict +from app.models.user import User as UserModel +from app.schemas.user import CreateUser, User +from app.storage import users as users_storage + +USERNAME_TAKEN_MESSAGE = "Имя пользователя уже занято" + + +def to_user_schema(user: UserModel) -> User: + """Преобразует ORM-модель в публичную схему без password_hash.""" + user_schema = User.model_validate(user) + return user_schema + + +async def register_user(session: AsyncSession, new_user: CreateUser) -> User: + """Регистрирует пользователя; занятый username даёт 409.""" + existing_user = await users_storage.get_user_by_username(session, new_user.username) + await session.commit() + if existing_user is not None: + raise conflict(USERNAME_TAKEN_MESSAGE) + + password_hash = await security.hash_password(new_user.password) + + try: + user = await users_storage.create_user( + session, + username=new_user.username, + display_name=new_user.display_name, + password_hash=password_hash, + ) + await session.commit() + except IntegrityError: + await session.rollback() + raise conflict(USERNAME_TAKEN_MESSAGE) from None + + created_user = to_user_schema(user) + return created_user diff --git a/app/endpoints/__init__.py b/app/endpoints/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/endpoints/auth.py b/app/endpoints/auth.py new file mode 100644 index 00000000..69f4535f --- /dev/null +++ b/app/endpoints/auth.py @@ -0,0 +1,32 @@ +from fastapi import APIRouter, Response + +from app.domain.auth import create_session, revoke_session +from app.endpoints.dependencies import ( + AppSettings, + BasicCredentials, + BearerToken, + DbSession, + RedisClient, +) +from app.schemas.session import AuthSession + +router = APIRouter(prefix="/api/v1/auth", tags=["auth"]) + + +@router.post("/sessions", status_code=201) +async def login( + credentials: BasicCredentials, + session: DbSession, + redis: RedisClient, + settings: AppSettings, +) -> AuthSession: + username, password = credentials + return await create_session( + session, redis, username, password, settings.session_ttl_seconds + ) + + +@router.delete("/sessions/current", status_code=204) +async def logout(token: BearerToken, redis: RedisClient) -> Response: + await revoke_session(redis, token) + return Response(status_code=204) diff --git a/app/endpoints/chats.py b/app/endpoints/chats.py new file mode 100644 index 00000000..91f2b211 --- /dev/null +++ b/app/endpoints/chats.py @@ -0,0 +1,32 @@ +from typing import Annotated +from uuid import UUID + +from fastapi import APIRouter, Query + +from app.domain.chats import create_chat, get_chat, list_chats +from app.endpoints.dependencies import CurrentUser, DbSession +from app.schemas.chat import Chat, ChatPage, CreateChat + +router = APIRouter(prefix="/api/v1/chats", tags=["chats"]) + + +@router.post("", status_code=201) +async def create_new_chat( + new_chat: CreateChat, creator: CurrentUser, session: DbSession +) -> Chat: + return await create_chat(session, creator, new_chat) + + +@router.get("") +async def list_user_chats( + user: CurrentUser, + session: DbSession, + limit: Annotated[int, Query(ge=1, le=100)] = 50, + cursor: Annotated[str | None, Query(min_length=1)] = None, +) -> ChatPage: + return await list_chats(session, user, cursor, limit) + + +@router.get("/{chat_id}") +async def get_chat_by_id(chat_id: UUID, user: CurrentUser, session: DbSession) -> Chat: + return await get_chat(session, user, chat_id) diff --git a/app/endpoints/dependencies.py b/app/endpoints/dependencies.py new file mode 100644 index 00000000..eccb50dd --- /dev/null +++ b/app/endpoints/dependencies.py @@ -0,0 +1,100 @@ +import base64 +import binascii +from collections.abc import AsyncIterator +from typing import Annotated + +from fastapi import Depends, Request, Security +from fastapi.openapi.models import HTTPBase, HTTPBearer +from fastapi.security.base import SecurityBase +from redis.asyncio import Redis +from sqlalchemy.ext.asyncio import AsyncSession + +from app.core.config import Settings +from app.domain.auth import authenticate_token +from app.errors.exceptions import unauthorized_basic, unauthorized_bearer +from app.schemas.user import User + + +async def get_settings(request: Request) -> Settings: + settings: Settings = request.app.state.settings + return settings + + +async def get_db(request: Request) -> AsyncIterator[AsyncSession]: + """Одна AsyncSession на HTTP-запрос; незакоммиченная транзакция откатывается.""" + session_factory = request.app.state.session_factory + async with session_factory() as session: + yield session + + +async def get_redis(request: Request) -> Redis: + redis: Redis = request.app.state.redis + return redis + + +class AuthorizationHeader(SecurityBase): + """Отдаёт сырой заголовок Authorization и описывает схему для OpenAPI.""" + + def __init__(self, model: HTTPBase, scheme_name: str) -> None: + self.model = model + self.scheme_name = scheme_name + + async def __call__(self, request: Request) -> str | None: + return request.headers.get("Authorization") + + +basic_auth = AuthorizationHeader(HTTPBase(scheme="basic"), "BasicAuth") +bearer_auth = AuthorizationHeader(HTTPBearer(), "BearerAuth") + + +def _split_authorization(authorization: str | None) -> tuple[str, str] | None: + if authorization is None: + return None + scheme, _, value = authorization.partition(" ") + if not value.strip(): + return None + return scheme.lower(), value.strip() + + +async def get_bearer_token( + authorization: Annotated[str | None, Security(bearer_auth)], +) -> str: + parts = _split_authorization(authorization) + if parts is None or parts[0] != "bearer": + raise unauthorized_bearer() + return parts[1] + + +async def get_basic_credentials( + authorization: Annotated[str | None, Security(basic_auth)], +) -> tuple[str, str]: + """(username, password) из `Authorization: Basic`; пароль может содержать ':'.""" + parts = _split_authorization(authorization) + if parts is None or parts[0] != "basic": + raise unauthorized_basic() + + try: + decoded = base64.b64decode(parts[1], validate=True).decode("utf-8") + except (binascii.Error, UnicodeDecodeError, ValueError): + raise unauthorized_basic() from None + + username, separator, password = decoded.partition(":") + if not separator or not username: + raise unauthorized_basic() + return username, password + + +DbSession = Annotated[AsyncSession, Depends(get_db)] +RedisClient = Annotated[Redis, Depends(get_redis)] +AppSettings = Annotated[Settings, Depends(get_settings)] +BearerToken = Annotated[str, Depends(get_bearer_token)] +BasicCredentials = Annotated[tuple[str, str], Depends(get_basic_credentials)] + + +async def get_current_user( + token: BearerToken, session: DbSession, redis: RedisClient +) -> User: + return await authenticate_token(session, redis, token) + + +CurrentUser = Annotated[User, Depends(get_current_user)] diff --git a/app/endpoints/health.py b/app/endpoints/health.py new file mode 100644 index 00000000..daf8a37f --- /dev/null +++ b/app/endpoints/health.py @@ -0,0 +1,26 @@ +from fastapi import APIRouter, Request + +from app.core.database import check_database +from app.core.redis import check_redis +from app.errors.exceptions import unavailable +from app.schemas.base import Health + +router = APIRouter(prefix="/health", tags=["health"]) + + +@router.get("/live") +async def liveness() -> Health: + return Health(status="ok") + + +@router.get("/ready") +async def readiness(request: Request) -> Health: + # TODO(4.1): если реплика получила SIGTERM (lifecycle.draining) — сразу 503, + # чтобы LB исключил её до закрытия сокета. + # TODO(4.1): проверки зависимостей должны укладываться в timeout healthcheck; + # зафиксируйте в REPORT.md, какие зависимости критичны. + if not await check_database(request.app.state.engine): + raise unavailable() + if not await check_redis(request.app.state.redis): + raise unavailable() + return Health(status="ok") diff --git a/app/endpoints/messages.py b/app/endpoints/messages.py new file mode 100644 index 00000000..f8a734d9 --- /dev/null +++ b/app/endpoints/messages.py @@ -0,0 +1,39 @@ +from typing import Annotated +from uuid import UUID + +from fastapi import APIRouter, Query + +from app.domain.messages import list_messages, send_message +from app.endpoints.dependencies import AppSettings, CurrentUser, DbSession +from app.schemas.message import CreateMessage, Message, MessagePage + +router = APIRouter(prefix="/api/v1/chats", tags=["messages"]) + + +@router.post("/{chat_id}/messages", status_code=201) +async def send_new_message( + chat_id: UUID, + new_message: CreateMessage, + sender: CurrentUser, + session: DbSession, + settings: AppSettings, + # TODO(3.4): принять заголовок Idempotency-Key: + # idempotency_key: Annotated[str | None, Header(alias="Idempotency-Key")] = None + # и вызывать app.domain.idempotency.send_message_idempotent(...) вместо send_message. + # Нет/неверный ключ -> 400 (validate_idempotency_key). Валидацию делайте сами, + # а не через pattern= у Header, чтобы текст ошибки был в общем формате. +) -> Message: + return await send_message( + session, sender, chat_id, new_message, settings.max_messages + ) + + +@router.get("/{chat_id}/messages") +async def get_history_messages( + chat_id: UUID, + user: CurrentUser, + session: DbSession, + limit: Annotated[int, Query(ge=1, le=100)] = 50, + cursor: Annotated[str | None, Query(min_length=1)] = None, +) -> MessagePage: + return await list_messages(session, user, chat_id, cursor, limit) diff --git a/app/endpoints/users.py b/app/endpoints/users.py new file mode 100644 index 00000000..c502d010 --- /dev/null +++ b/app/endpoints/users.py @@ -0,0 +1,17 @@ +from fastapi import APIRouter + +from app.domain.users import register_user +from app.endpoints.dependencies import CurrentUser, DbSession +from app.schemas.user import CreateUser, User + +router = APIRouter(prefix="/api/v1/users", tags=["users"]) + + +@router.post("", status_code=201) +async def create_user(new_user: CreateUser, session: DbSession) -> User: + return await register_user(session, new_user) + + +@router.get("/me") +async def get_me(user: CurrentUser) -> User: + return user diff --git a/app/errors/__init__.py b/app/errors/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/errors/exceptions.py b/app/errors/exceptions.py new file mode 100644 index 00000000..92aa2820 --- /dev/null +++ b/app/errors/exceptions.py @@ -0,0 +1,85 @@ +from collections.abc import Mapping + +BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"' +BEARER_CHALLENGE = 'Bearer realm="messenger"' + + +class ApiError(Exception): + def __init__( + self, + status_code: int, + code: str, + message: str, + headers: Mapping[str, str] | None = None, + ) -> None: + super().__init__(message) + self.status_code = status_code + self.code = code + self.message = message + self.headers = headers + + +def invalid_request(message: str = "Неверный запрос") -> ApiError: + """Создаёт ошибку некорректного или недопустимого запроса.""" + return ApiError(400, "invalid_request", message) + + +def unauthorized( + message: str = "Нет действительной аутентификации", + www_authenticate: str | None = None, +) -> ApiError: + """Создаёт ошибку отсутствующей или недействительной аутентификации.""" + headers = {"WWW-Authenticate": www_authenticate} if www_authenticate else None + return ApiError(401, "unauthorized", message, headers) + + +def unauthorized_basic(message: str = "Неверные учётные данные") -> ApiError: + """401 для POST /auth/sessions (одинаковый для неверного username и пароля).""" + return unauthorized(message, BASIC_CHALLENGE) + + +def unauthorized_bearer(message: str = "Нет действительной сессии") -> ApiError: + """401 для бизнес-endpoint: нет, истёк, отозван или неизвестен токен.""" + return unauthorized(message, BEARER_CHALLENGE) + + +def not_found(message: str = "Объект не найден") -> ApiError: + """Создаёт ошибку, указывающую на отсутствие запрошенного ресурса.""" + return ApiError(404, "not_found", message) + + +def conflict(message: str = "Конфликт") -> ApiError: + """Создаёт ошибку конфликта с текущим состоянием ресурса.""" + return ApiError(409, "conflict", message) + + +def payload_too_large(message: str = "Слишком большое тело запроса") -> ApiError: + """Создаёт ошибку, указывающую на превышение допустимого размера запроса.""" + return ApiError(413, "payload_too_large", message) + + +def unsupported_media_type( + message: str = "Требуется Content-Type: application/json", +) -> ApiError: + """Создаёт ошибку неподдерживаемого формата или типа содержимого запроса.""" + return ApiError(415, "unsupported_media_type", message) + + +def rate_limited(retry_after: int = 1) -> ApiError: + """Создаёт ошибку превышения допустимой частоты запросов.""" + return ApiError( + 429, + "rate_limited", + "Слишком много запросов", + {"Retry-After": str(max(1, retry_after))}, + ) + + +def unavailable(message: str = "Сервис временно недоступен") -> ApiError: + """Создаёт ошибку временной недоступности сервиса.""" + return ApiError(503, "unavailable", message) + + +def internal_error() -> ApiError: + """Создаёт ошибку внутренней ошибки сервера.""" + return ApiError(500, "internal_error", "Внутренняя ошибка сервера") diff --git a/app/errors/handlers.py b/app/errors/handlers.py new file mode 100644 index 00000000..8aa80a78 --- /dev/null +++ b/app/errors/handlers.py @@ -0,0 +1,197 @@ +import logging +from collections.abc import Mapping +from uuid import uuid4 + +from fastapi import FastAPI, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from redis.exceptions import ConnectionError as RedisConnectionError +from redis.exceptions import TimeoutError as RedisTimeoutError +from sqlalchemy.exc import DBAPIError, InterfaceError, OperationalError +from sqlalchemy.exc import TimeoutError as PoolTimeoutError +from starlette.exceptions import HTTPException as StarletteHTTPException + +from app.core.request_id import REQUEST_ID_HEADER +from app.errors.exceptions import ApiError, internal_error, unavailable + +logger = logging.getLogger(__name__) + +TRANSIENT_SQLSTATE_CLASSES = ("08", "40", "53", "57") +TRANSIENT_SQLSTATES = frozenset({"55P03"}) + + +def _get_request_id(request: Request) -> str: + request_id = getattr(request.state, "request_id", None) + + if request_id is None: + request_id = str(uuid4()) + request.state.request_id = request_id + + return request_id + + +def _build_response( + request: Request, + status_code: int, + code: str, + message: str, + headers: Mapping[str, str] | None = None, +) -> JSONResponse: + request_id = _get_request_id(request) + + response_headers = { + **(headers or {}), + REQUEST_ID_HEADER: request_id, + } + + body = { + "error": { + "code": code, + "message": message, + "request_id": request_id, + } + } + + return JSONResponse( + status_code=status_code, + content=body, + headers=response_headers, + ) + + +async def api_error_handler( + request: Request, + exc: Exception, +) -> JSONResponse: + assert isinstance(exc, ApiError) + + return _build_response( + request, + exc.status_code, + exc.code, + exc.message, + exc.headers, + ) + + +async def routing_error_handler( + request: Request, + exc: Exception, +) -> JSONResponse: + assert isinstance(exc, StarletteHTTPException) + + if exc.status_code == 404: + return _build_response( + request, + 404, + "not_found", + "Объект не найден", + exc.headers, + ) + + return _build_response( + request, + exc.status_code, + "invalid_request", + "Неверный запрос", + exc.headers, + ) + + +async def validation_error_handler( + request: Request, + exc: Exception, +) -> JSONResponse: + assert isinstance(exc, RequestValidationError) + + fields = sorted({".".join(map(str, error["loc"])) for error in exc.errors()}) + message = "Неверный запрос" + + if fields: + message = f"{message}: {', '.join(fields)}" + + return _build_response( + request, + 400, + "invalid_request", + message, + ) + + +async def dependency_unavailable_handler( + request: Request, + exc: Exception, +) -> JSONResponse: + """PostgreSQL или Redis недоступны либо исчерпан пул: 503 без внутренних деталей.""" + request_id = _get_request_id(request) + logger.error( + "Dependency unavailable request_id=%s: %s", + request_id, + type(exc).__name__, + exc_info=exc, + ) + + error = unavailable() + return _build_response( + request, + error.status_code, + error.code, + error.message, + {"Retry-After": "1"}, + ) + + +def _is_transient_database_error(exc: DBAPIError) -> bool: + sqlstate = getattr(exc.orig, "sqlstate", None) or "" + return ( + sqlstate.startswith(TRANSIENT_SQLSTATE_CLASSES) + or sqlstate in TRANSIENT_SQLSTATES + ) + + +async def database_error_handler( + request: Request, + exc: Exception, +) -> JSONResponse: + """Временные ошибки PostgreSQL (таймаут запроса, блокировки, нехватка ресурсов) — 503.""" + assert isinstance(exc, DBAPIError) + + if _is_transient_database_error(exc): + return await dependency_unavailable_handler(request, exc) + return await unhandled_error_handler(request, exc) + + +async def unhandled_error_handler( + request: Request, + exc: Exception, +) -> JSONResponse: + request_id = _get_request_id(request) + + logger.error( + "Unhandled error request_id=%s", + request_id, + exc_info=exc, + ) + + error = internal_error() + + return _build_response( + request, + error.status_code, + error.code, + error.message, + ) + + +def register_exception_handlers(app: FastAPI) -> None: + app.add_exception_handler(ApiError, api_error_handler) + app.add_exception_handler(StarletteHTTPException, routing_error_handler) + app.add_exception_handler(RequestValidationError, validation_error_handler) + app.add_exception_handler(OperationalError, dependency_unavailable_handler) + app.add_exception_handler(InterfaceError, dependency_unavailable_handler) + app.add_exception_handler(PoolTimeoutError, dependency_unavailable_handler) + app.add_exception_handler(DBAPIError, database_error_handler) + app.add_exception_handler(RedisConnectionError, dependency_unavailable_handler) + app.add_exception_handler(RedisTimeoutError, dependency_unavailable_handler) + app.add_exception_handler(OSError, dependency_unavailable_handler) + app.add_exception_handler(Exception, unhandled_error_handler) diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..f0a4c12a --- /dev/null +++ b/app/main.py @@ -0,0 +1,80 @@ +import logging +from collections.abc import AsyncGenerator +from contextlib import asynccontextmanager + +import uvicorn +from fastapi import Depends, FastAPI + +from app.core.config import Settings +from app.core.database import create_db_engine, create_session_factory +from app.core.redis import create_redis_client +from app.core.request_capacity import acquire_request_slot, create_request_capacity +from app.core.request_id import RequestIdMiddleware +from app.endpoints.auth import router as auth_router +from app.endpoints.chats import router as chats_router +from app.endpoints.health import router as health_router +from app.endpoints.messages import router as messages_router +from app.endpoints.users import router as users_router +from app.errors.handlers import register_exception_handlers + + +def configure_logging(settings: Settings) -> None: + logging.basicConfig( + level=settings.log_level, + format="%(asctime)s %(levelname)s %(name)s %(message)s", + ) + + +def create_app(settings: Settings | None = None) -> FastAPI: + settings = settings or Settings() # type: ignore + configure_logging(settings) + + engine = create_db_engine(settings) + redis = create_redis_client(settings) + + @asynccontextmanager + async def lifespan(_: FastAPI) -> AsyncGenerator[None]: + yield + await redis.aclose() + await engine.dispose() + + application = FastAPI(title="Messenger", version="1.3.0", lifespan=lifespan) + + application.state.settings = settings + application.state.engine = engine + application.state.session_factory = create_session_factory(engine) + application.state.redis = redis + application.state.request_capacity = create_request_capacity(settings.max_in_flight) + + # TODO(3.2): application.state.instance_id = resolve_instance_id(settings.instance_id) + # и application.add_middleware(InstanceIdMiddleware, instance_id=...) + # (app/core/instance.py). Middleware, добавленный последним, — внешний. + # TODO(4.1): application.state.lifecycle = Lifecycle() (app/core/lifecycle.py) + application.add_middleware(RequestIdMiddleware) + register_exception_handlers(application) + + limited = [Depends(acquire_request_slot)] + application.include_router(health_router) + application.include_router(users_router, dependencies=limited) + application.include_router(auth_router, dependencies=limited) + application.include_router(chats_router, dependencies=limited) + application.include_router(messages_router, dependencies=limited) + + return application + + +def main() -> None: + settings = Settings() # type: ignore + uvicorn.run( + "app.main:create_app", + factory=True, + host=settings.app_host, + port=settings.app_port, + log_level=settings.log_level.lower(), + # TODO(4.1): согласовать с stop_grace_period в compose.yaml и drain-периодом + timeout_graceful_shutdown=settings.graceful_shutdown_timeout_seconds, + ) + + +if __name__ == "__main__": + main() diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 00000000..9d56e6e2 --- /dev/null +++ b/app/models/__init__.py @@ -0,0 +1,8 @@ +from app.models.base import Base +from app.models.chat import Chat +from app.models.chat_member import ChatMember +from app.models.counter import Counter +from app.models.message import Message +from app.models.user import User + +__all__ = ["Base", "Chat", "ChatMember", "Counter", "Message", "User"] diff --git a/app/models/base.py b/app/models/base.py new file mode 100644 index 00000000..15a748e5 --- /dev/null +++ b/app/models/base.py @@ -0,0 +1,27 @@ +import uuid +from datetime import datetime +from typing import Any, ClassVar + +from sqlalchemy import DateTime, MetaData +from sqlalchemy.dialects.postgresql import UUID as PG_UUID +from sqlalchemy.orm import DeclarativeBase +from sqlalchemy.types import TypeEngine + +NAMING_CONVENTION = { + "ix": "ix_%(column_0_label)s", + "uq": "uq_%(table_name)s_%(column_0_name)s", + "ck": "ck_%(table_name)s_%(constraint_name)s", + "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", + "pk": "pk_%(table_name)s", +} + + +class Base(DeclarativeBase): + """Базовый класс ORM-моделей.""" + + metadata = MetaData(naming_convention=NAMING_CONVENTION) + + type_annotation_map: ClassVar[dict[Any, TypeEngine[Any]]] = { + uuid.UUID: PG_UUID(as_uuid=True), + datetime: DateTime(timezone=True), + } diff --git a/app/models/chat.py b/app/models/chat.py new file mode 100644 index 00000000..ffff7ec2 --- /dev/null +++ b/app/models/chat.py @@ -0,0 +1,32 @@ +import uuid +from datetime import datetime +from typing import TYPE_CHECKING + +from sqlalchemy import String, func +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.models.base import Base + +if TYPE_CHECKING: + from app.models.chat_member import ChatMember + from app.models.message import Message + + +class Chat(Base): + """Чат с фиксированным составом участников.""" + + __tablename__ = "chats" + + id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4) + title: Mapped[str] = mapped_column(String(100)) + created_at: Mapped[datetime] = mapped_column(server_default=func.now()) + + members: Mapped[list["ChatMember"]] = relationship( + back_populates="chat", + cascade="all, delete-orphan", + passive_deletes=True, + lazy="selectin", + ) + messages: Mapped[list["Message"]] = relationship( + back_populates="chat", lazy="raise" + ) diff --git a/app/models/chat_member.py b/app/models/chat_member.py new file mode 100644 index 00000000..567ffd66 --- /dev/null +++ b/app/models/chat_member.py @@ -0,0 +1,26 @@ +import uuid +from typing import TYPE_CHECKING + +from sqlalchemy import ForeignKey, Index +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.models.base import Base + +if TYPE_CHECKING: + from app.models.chat import Chat + from app.models.user import User + + +class ChatMember(Base): + """Членство пользователя в чате.""" + + __tablename__ = "chat_members" + __table_args__ = (Index("ix_chat_members_user_id_chat_id", "user_id", "chat_id"),) + + chat_id: Mapped[uuid.UUID] = mapped_column( + ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True + ) + user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"), primary_key=True) + + chat: Mapped["Chat"] = relationship(back_populates="members", lazy="raise") + user: Mapped["User"] = relationship(back_populates="memberships", lazy="raise") diff --git a/app/models/counter.py b/app/models/counter.py new file mode 100644 index 00000000..5f6706b5 --- /dev/null +++ b/app/models/counter.py @@ -0,0 +1,13 @@ +from sqlalchemy import BigInteger, String +from sqlalchemy.orm import Mapped, mapped_column + +from app.models.base import Base + + +class Counter(Base): + """Именованный счётчик с атомарным обновлением.""" + + __tablename__ = "counters" + + name: Mapped[str] = mapped_column(String(64), primary_key=True) + value: Mapped[int] = mapped_column(BigInteger) diff --git a/app/models/idempotency_key.py b/app/models/idempotency_key.py new file mode 100644 index 00000000..7c10fdd2 --- /dev/null +++ b/app/models/idempotency_key.py @@ -0,0 +1,28 @@ +"""ORM-модель записи идемпотентности. + +TODO(3.4): раскомментировать/доработать, добавить в app/models/__init__.py +и сгенерировать миграцию: `make migration m="add idempotency keys"` +(с поднятой БД), затем проверить сгенерированный файл руками. + +Примерная схема: + + class IdempotencyKey(Base): + __tablename__ = "idempotency_keys" + __table_args__ = ( + PrimaryKeyConstraint("user_id", "method", "path", "key"), + Index("ix_idempotency_keys_created_at", "created_at"), # для чистки по TTL + ) + + user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id")) + method: Mapped[str] = mapped_column(String(8)) + path: Mapped[str] = mapped_column(String(255)) + key: Mapped[str] = mapped_column(String(128)) + request_hash: Mapped[str] = mapped_column(String(64)) + message_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("messages.id")) + created_at: Mapped[datetime] = mapped_column(server_default=func.now()) + +Вопросы проектирования: + - почему достаточно сохранить message_id, а не весь JSON ответа; + - как и когда удаляются ключи старше IDEMPOTENCY_TTL_SECONDS; + - что видит второй параллельный запрос, пока первый ещё не COMMIT. +""" diff --git a/app/models/message.py b/app/models/message.py new file mode 100644 index 00000000..25dc8cf3 --- /dev/null +++ b/app/models/message.py @@ -0,0 +1,30 @@ +import uuid +from datetime import datetime +from typing import TYPE_CHECKING + +from sqlalchemy import ForeignKey, Index, Text, func +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.models.base import Base + +if TYPE_CHECKING: + from app.models.chat import Chat + from app.models.user import User + + +class Message(Base): + """Сообщение в чате.""" + + __tablename__ = "messages" + __table_args__ = ( + Index("ix_messages_chat_id_created_at_id", "chat_id", "created_at", "id"), + ) + + id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4) + chat_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("chats.id")) + sender_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id")) + text: Mapped[str] = mapped_column(Text) + created_at: Mapped[datetime] = mapped_column(server_default=func.clock_timestamp()) + + chat: Mapped["Chat"] = relationship(back_populates="messages", lazy="raise") + sender: Mapped["User"] = relationship(back_populates="messages", lazy="raise") diff --git a/app/models/user.py b/app/models/user.py new file mode 100644 index 00000000..74b690e6 --- /dev/null +++ b/app/models/user.py @@ -0,0 +1,31 @@ +import uuid +from datetime import datetime +from typing import TYPE_CHECKING + +from sqlalchemy import String, func +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.models.base import Base + +if TYPE_CHECKING: + from app.models.chat_member import ChatMember + from app.models.message import Message + + +class User(Base): + """Пользователь и его учётные данные.""" + + __tablename__ = "users" + + id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4) + username: Mapped[str] = mapped_column(String(32), unique=True) + display_name: Mapped[str] = mapped_column(String(100)) + password_hash: Mapped[str] = mapped_column(String(255)) + created_at: Mapped[datetime] = mapped_column(server_default=func.now()) + + memberships: Mapped[list["ChatMember"]] = relationship( + back_populates="user", lazy="raise" + ) + messages: Mapped[list["Message"]] = relationship( + back_populates="sender", lazy="raise" + ) diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py new file mode 100644 index 00000000..2f038940 --- /dev/null +++ b/app/schemas/__init__.py @@ -0,0 +1,6 @@ +from app.schemas.chat import Chat +from app.schemas.message import Message +from app.schemas.session import AuthSession +from app.schemas.user import User + +__all__ = ["AuthSession", "Chat", "Message", "User"] diff --git a/app/schemas/base.py b/app/schemas/base.py new file mode 100644 index 00000000..ca7f4de8 --- /dev/null +++ b/app/schemas/base.py @@ -0,0 +1,35 @@ +from typing import Literal +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, field_validator + + +class Base(BaseModel): + model_config = ConfigDict(extra="forbid", from_attributes=True) + + id: UUID + + +class Health(BaseModel): + model_config = ConfigDict(extra="forbid") + + status: Literal["ok"] + + +class BaseCreate(BaseModel): + model_config = ConfigDict(extra="forbid") + + @field_validator("*", mode="before") + @classmethod + def reject_invalid_strings(cls, value: object) -> object: + """Отклоняет строки из одних пробелов и строки с NUL, который не хранит PostgreSQL.""" + if isinstance(value, str): + if not value.strip(): + raise ValueError("Строка не может состоять только из пробелов") + if "\x00" in value: + raise ValueError("Строка не может содержать символ NUL") + return value + + +class BasePage(BaseModel): + model_config = ConfigDict(extra="forbid") diff --git a/app/schemas/chat.py b/app/schemas/chat.py new file mode 100644 index 00000000..c0436dab --- /dev/null +++ b/app/schemas/chat.py @@ -0,0 +1,22 @@ +from datetime import datetime +from uuid import UUID + +from pydantic import Field + +from app.schemas.base import Base, BaseCreate, BasePage + + +class Chat(Base): + title: str = Field(min_length=1, max_length=100) + member_ids: list[UUID] = Field(min_length=1, max_length=100) + created_at: datetime + + +class CreateChat(BaseCreate): + title: str = Field(min_length=1, max_length=100) + member_ids: list[UUID] = Field(min_length=1, max_length=100) + + +class ChatPage(BasePage): + items: list[Chat] + next_cursor: str | None diff --git a/app/schemas/message.py b/app/schemas/message.py new file mode 100644 index 00000000..f4ef2add --- /dev/null +++ b/app/schemas/message.py @@ -0,0 +1,22 @@ +from datetime import datetime +from uuid import UUID + +from pydantic import Field + +from app.schemas.base import Base, BaseCreate, BasePage + + +class Message(Base): + chat_id: UUID + sender_id: UUID + text: str = Field(min_length=1, max_length=4000) + created_at: datetime + + +class CreateMessage(BaseCreate): + text: str = Field(min_length=1, max_length=4000) + + +class MessagePage(BasePage): + items: list[Message] + next_cursor: str | None diff --git a/app/schemas/session.py b/app/schemas/session.py new file mode 100644 index 00000000..1cae0d21 --- /dev/null +++ b/app/schemas/session.py @@ -0,0 +1,17 @@ +from datetime import datetime +from typing import Literal + +from pydantic import BaseModel, ConfigDict, Field + +from app.schemas.user import User + + +class AuthSession(BaseModel): + """Ответ POST /auth/sessions (в OpenAPI — Session).""" + + model_config = ConfigDict(extra="forbid") + + token: str = Field(min_length=32) + token_type: Literal["Bearer"] = "Bearer" + expires_at: datetime + user: User diff --git a/app/schemas/user.py b/app/schemas/user.py new file mode 100644 index 00000000..16548516 --- /dev/null +++ b/app/schemas/user.py @@ -0,0 +1,25 @@ +from datetime import datetime + +from pydantic import Field + +from app.schemas.base import Base, BaseCreate + +USERNAME_PATTERN = r"^[a-z0-9_]{3,32}$" +PASSWORD_MIN_LENGTH = 12 +PASSWORD_MAX_LENGTH = 128 + + +class User(Base): + """Публичное представление пользователя. Пароль/хеш сюда не попадают никогда.""" + + username: str = Field(pattern=USERNAME_PATTERN) + display_name: str = Field(min_length=1, max_length=100) + created_at: datetime + + +class CreateUser(BaseCreate): + username: str = Field(pattern=USERNAME_PATTERN) + display_name: str = Field(min_length=1, max_length=100) + password: str = Field( + min_length=PASSWORD_MIN_LENGTH, max_length=PASSWORD_MAX_LENGTH, repr=False + ) diff --git a/app/storage/__init__.py b/app/storage/__init__.py new file mode 100644 index 00000000..c881665f --- /dev/null +++ b/app/storage/__init__.py @@ -0,0 +1 @@ +"""Доступ к данным PostgreSQL и Redis.""" diff --git a/app/storage/chats.py b/app/storage/chats.py new file mode 100644 index 00000000..f88ee0cc --- /dev/null +++ b/app/storage/chats.py @@ -0,0 +1,71 @@ +from collections.abc import Sequence +from uuid import UUID + +from sqlalchemy import exists, select, tuple_ +from sqlalchemy.ext.asyncio import AsyncSession + +from app.models.chat import Chat +from app.models.chat_member import ChatMember +from app.storage.types import Position + + +async def create_chat( + session: AsyncSession, + *, + title: str, + member_ids: Sequence[UUID], +) -> Chat: + """Добавляет чат вместе с участниками.""" + chat = Chat( + title=title, + members=[ChatMember(user_id=user_id) for user_id in member_ids], + ) + session.add(chat) + await session.flush() + return chat + + +async def get_chat_for_member( + session: AsyncSession, + chat_id: UUID, + user_id: UUID, +) -> Chat | None: + """Возвращает чат, если пользователь в нём состоит, иначе None.""" + stmt = ( + select(Chat) + .join(ChatMember, ChatMember.chat_id == Chat.id) + .where(Chat.id == chat_id, ChatMember.user_id == user_id) + ) + result = await session.execute(stmt) + chat = result.scalar_one_or_none() + return chat + + +async def is_chat_member(session: AsyncSession, chat_id: UUID, user_id: UUID) -> bool: + """Возвращает True, если пользователь состоит в чате; одна проверка по PK.""" + stmt = select( + exists().where(ChatMember.chat_id == chat_id, ChatMember.user_id == user_id) + ) + result = await session.execute(stmt) + is_member = bool(result.scalar_one()) + return is_member + + +async def list_chats_for_user( + session: AsyncSession, + user_id: UUID, + after: Position | None, + limit: int, +) -> list[Chat]: + """Возвращает чаты пользователя по (created_at, id) строго после позиции after.""" + stmt = ( + select(Chat) + .join(ChatMember, ChatMember.chat_id == Chat.id) + .where(ChatMember.user_id == user_id) + ) + if after is not None: + stmt = stmt.where(tuple_(Chat.created_at, Chat.id) > tuple_(*after)) + stmt = stmt.order_by(Chat.created_at, Chat.id).limit(limit) + result = await session.execute(stmt) + chats = list(result.scalars().all()) + return chats diff --git a/app/storage/messages.py b/app/storage/messages.py new file mode 100644 index 00000000..19960367 --- /dev/null +++ b/app/storage/messages.py @@ -0,0 +1,58 @@ +from uuid import UUID + +from sqlalchemy import select, tuple_ +from sqlalchemy.dialects.postgresql import insert +from sqlalchemy.ext.asyncio import AsyncSession + +from app.models.counter import Counter +from app.models.message import Message +from app.storage.types import Position + +MESSAGES_COUNTER = "messages" + + +async def create_message( + session: AsyncSession, + *, + chat_id: UUID, + sender_id: UUID, + text: str, +) -> Message: + """Добавляет сообщение; id и created_at назначает сервер.""" + message = Message(chat_id=chat_id, sender_id=sender_id, text=text) + session.add(message) + await session.flush() + return message + + +async def list_messages( + session: AsyncSession, + chat_id: UUID, + after: Position | None, + limit: int, +) -> list[Message]: + """Возвращает сообщения чата по (created_at, id) строго после позиции after.""" + stmt = select(Message).where(Message.chat_id == chat_id) + if after is not None: + stmt = stmt.where(tuple_(Message.created_at, Message.id) > tuple_(*after)) + stmt = stmt.order_by(Message.created_at, Message.id).limit(limit) + result = await session.execute(stmt) + messages = list(result.scalars().all()) + return messages + + +async def reserve_message_slot(session: AsyncSession, max_messages: int) -> bool: + """Атомарно увеличивает счётчик сообщений, если лимит не исчерпан.""" + stmt = ( + insert(Counter) + .values(name=MESSAGES_COUNTER, value=1) + .on_conflict_do_update( + index_elements=[Counter.name], + set_={"value": Counter.value + 1}, + where=Counter.value < max_messages, + ) + .returning(Counter.value) + ) + result = await session.execute(stmt) + is_reserved = result.scalar_one_or_none() is not None + return is_reserved diff --git a/app/storage/sessions.py b/app/storage/sessions.py new file mode 100644 index 00000000..d8a30f29 --- /dev/null +++ b/app/storage/sessions.py @@ -0,0 +1,38 @@ +from datetime import datetime +from uuid import UUID + +from redis.asyncio import Redis + +SESSION_KEY_PREFIX = "session:" + + +def session_key(token_hash: str) -> str: + """Возвращает ключ Redis для хеша токена.""" + return f"{SESSION_KEY_PREFIX}{token_hash}" + + +async def create_session( + redis: Redis, + *, + token_hash: str, + user_id: UUID, + expires_at: datetime, +) -> None: + """Добавляет сессию с абсолютным временем истечения.""" + await redis.set(session_key(token_hash), str(user_id), pxat=expires_at) + + +async def get_session_user_id(redis: Redis, token_hash: str) -> UUID | None: + """Возвращает id владельца действующей сессии или None.""" + raw_user_id = await redis.get(session_key(token_hash)) + if isinstance(raw_user_id, bytes): + raw_user_id = raw_user_id.decode() + user_id = UUID(raw_user_id) if raw_user_id else None + return user_id + + +async def delete_session(redis: Redis, token_hash: str) -> bool: + """Удаляет сессию; возвращает True, если она существовала.""" + deleted_count = await redis.delete(session_key(token_hash)) + is_deleted = deleted_count > 0 + return is_deleted diff --git a/app/storage/types.py b/app/storage/types.py new file mode 100644 index 00000000..e0357cfa --- /dev/null +++ b/app/storage/types.py @@ -0,0 +1,6 @@ +"""Общие типы слоя хранения.""" + +from datetime import datetime +from uuid import UUID + +Position = tuple[datetime, UUID] diff --git a/app/storage/users.py b/app/storage/users.py new file mode 100644 index 00000000..45214e9b --- /dev/null +++ b/app/storage/users.py @@ -0,0 +1,47 @@ +from collections.abc import Collection +from uuid import UUID + +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from app.models.user import User + + +async def create_user( + session: AsyncSession, + *, + username: str, + display_name: str, + password_hash: str, +) -> User: + """Добавляет пользователя; занятый username даёт IntegrityError при flush.""" + user = User( + username=username, display_name=display_name, password_hash=password_hash + ) + session.add(user) + await session.flush() + return user + + +async def get_user_by_id(session: AsyncSession, user_id: UUID) -> User | None: + """Возвращает пользователя по id или None.""" + user = await session.get(User, user_id) + return user + + +async def get_user_by_username(session: AsyncSession, username: str) -> User | None: + """Возвращает пользователя по username или None.""" + stmt = select(User).where(User.username == username) + result = await session.execute(stmt) + user = result.scalar_one_or_none() + return user + + +async def get_existing_user_ids( + session: AsyncSession, user_ids: Collection[UUID] +) -> set[UUID]: + """Возвращает те из user_ids, что существуют, одним запросом.""" + stmt = select(User.id).where(User.id.in_(user_ids)) + result = await session.execute(stmt) + existing_ids = set(result.scalars().all()) + return existing_ids diff --git a/compose.test.yaml b/compose.test.yaml new file mode 100644 index 00000000..46c9448f --- /dev/null +++ b/compose.test.yaml @@ -0,0 +1,10 @@ +# Override для отдельного тестового стенда (make test-stand-up): +# публикует PostgreSQL и Redis на 127.0.0.1, чтобы pytest из student-tests/ +# мог подключаться к ним напрямую. В основном стенде эти порты закрыты. +services: + postgres: + ports: + - "127.0.0.1:${DB_HOST_PORT:-15432}:5432" + redis: + ports: + - "127.0.0.1:${REDIS_HOST_PORT:-16379}:6379" diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..cc05f4c5 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,105 @@ +# Клиент → nginx (LB) → api × N → postgres + redis. +# Незавершённые места помечены TODO(<номер>). +# +# Запуск: docker compose --env-file .env up -d --build (или make up) + +x-api-image: &api-image + build: . + image: mtusi-messenger:lab3 + +services: + postgres: + image: postgres:16.10-bookworm + restart: unless-stopped + # TODO(4.3): секреты через `secrets:` (POSTGRES_PASSWORD_FILE), а не env. + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_DB: ${DB_NAME} + POSTGRES_PASSWORD: ${DB_PASSWORD} + volumes: + - pgdata:/var/lib/postgresql/data + # Порты наружу не публикуются; для локальных pytest см. compose.test.yaml. + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 5s + timeout: 3s + retries: 10 + # TODO(4.3): deploy.resources.limits (cpus / memory) + + redis: + image: redis:7.4.2-bookworm + restart: unless-stopped + # TODO(4.3): пароль через secrets, а не в командной строке + command: ["redis-server", "/usr/local/etc/redis/redis.conf", "--requirepass", "${REDIS_PASSWORD}"] + environment: + REDISCLI_AUTH: ${REDIS_PASSWORD} + volumes: + - ./infra/redis/redis.conf:/usr/local/etc/redis/redis.conf:ro + - redisdata:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + # TODO(4.3): deploy.resources.limits + + # Однократные миграции до старта реплик: api ждёт service_completed_successfully, + # поэтому реплики не устраивают гонку `alembic upgrade`. + # TODO(4.4): описать совместимость миграций с работающей версией (expand/contract). + migrate: + <<: *api-image + command: ["alembic", "upgrade", "head"] + env_file: .env + depends_on: + postgres: + condition: service_healthy + restart: "no" + + api: + <<: *api-image + restart: unless-stopped + env_file: .env + environment: + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + # Не задавайте container_name и ports: несколько реплик одного service, + # доступ только через nginx. + deploy: + replicas: ${API_REPLICAS:-2} + # TODO(4.3): resources.limits: { cpus: "...", memory: "..." } + # TODO(4.1): healthcheck по /health/ready (interval/timeout/retries), + # согласованный с проверками nginx; сейчас действует HEALTHCHECK + # из Dockerfile по /health/live. + # TODO(4.1): stop_signal / stop_grace_period > drain + graceful timeout. + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + migrate: + condition: service_completed_successfully + + nginx: + image: nginx:1.28.0-alpine + restart: unless-stopped + volumes: + - ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro + ports: + # Единственная публичная точка входа. + - "127.0.0.1:${LB_HOST_PORT:-8080}:8080" + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/lb-health"] + interval: 5s + timeout: 3s + retries: 5 + depends_on: + api: + condition: service_healthy + # TODO(4.3): deploy.resources.limits + +volumes: + pgdata: + redisdata: + +# TODO(4.3): secrets: +# db_password: +# file: ./secrets/db_password # каталог secrets/ в .gitignore diff --git a/infra/nginx/nginx.conf b/infra/nginx/nginx.conf new file mode 100644 index 00000000..bc967757 --- /dev/null +++ b/infra/nginx/nginx.conf @@ -0,0 +1,55 @@ +# nginx как единая точка входа перед репликами api. +# Сейчас — минимальный рабочий round-robin; доработки помечены TODO. + +worker_processes auto; + +events { + worker_connections 1024; +} + +http { + access_log /dev/stdout; + error_log /dev/stderr warn; + + # TODO(3.3): DNS-обновление адресов backend. + # Сейчас `server api:8080` резолвится ОДИН раз при старте nginx: если + # реплику пересоздали с новым IP или увеличили число реплик, nginx об этом + # не узнает. Варианты: + # resolver 127.0.0.11 valid=5s ipv6=off; # встроенный DNS Docker + # upstream api_backend { zone api_backend 64k; server api:8080 resolve; } + # (параметр `resolve` в open-source nginx доступен с 1.27.3). + + upstream api_backend { + server api:8080; + # TODO(4.1): пассивные проверки: max_fails=... fail_timeout=... + # (активных health checks в open-source nginx нет — это nginx Plus). + # TODO(4.1): keepalive N; + proxy_http_version 1.1 и пустой Connection. + } + + server { + listen 8080; + + # Проверка самого LB для healthcheck в compose; не проксируется в API. + location = /lb-health { + access_log off; + default_type text/plain; + return 200 "ok\n"; + } + + location / { + proxy_pass http://api_backend; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + + # TODO(4.1): таймауты, согласованные с API и тестами: + # proxy_connect_timeout ...; proxy_send_timeout ...; proxy_read_timeout ...; + # TODO(4.1): переключение на другой backend при отказе: + # proxy_next_upstream error timeout http_502 http_503 ...; + # proxy_next_upstream_tries ...; proxy_next_upstream_timeout ...; + # Внимание: POST nginx по умолчанию НЕ повторяет после отправки + # запроса (non_idempotent). Подумайте, безопасно ли добавлять + # non_idempotent при наличии Idempotency-Key. + # Не используйте ip_hash/sticky как "лечение" раздельного состояния. + } + } +} diff --git a/infra/redis/redis.conf b/infra/redis/redis.conf new file mode 100644 index 00000000..f2ae982f --- /dev/null +++ b/infra/redis/redis.conf @@ -0,0 +1,5 @@ +dir /data +appendonly yes +appendfsync always +maxmemory 256mb +maxmemory-policy noeviction diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..d914a274 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,68 @@ +import asyncio +from logging.config import fileConfig + +from alembic import context +from sqlalchemy import pool +from sqlalchemy.engine import Connection +from sqlalchemy.ext.asyncio import async_engine_from_config + +from app.core.config import Settings +from app.models import Base + +config = context.config + +if config.config_file_name is not None: + fileConfig(config.config_file_name, disable_existing_loggers=False) + +target_metadata = Base.metadata + + +def _database_url() -> str: + explicit = config.get_main_option("sqlalchemy.url") + if explicit: + return explicit + return Settings().get_database_url() # type: ignore + + +def run_migrations_offline() -> None: + context.configure( + url=_database_url(), + target_metadata=target_metadata, + literal_binds=True, + compare_type=True, + ) + with context.begin_transaction(): + context.run_migrations() + + +def do_run_migrations(connection: Connection) -> None: + context.configure( + connection=connection, + target_metadata=target_metadata, + compare_type=True, + ) + with context.begin_transaction(): + context.run_migrations() + + +async def run_async_migrations() -> None: + section = config.get_section(config.config_ini_section, {}) + section["sqlalchemy.url"] = _database_url().replace("%", "%%") + connectable = async_engine_from_config( + section, prefix="sqlalchemy.", poolclass=pool.NullPool + ) + + async with connectable.connect() as connection: + await connection.run_sync(do_run_migrations) + + await connectable.dispose() + + +def run_migrations_online() -> None: + asyncio.run(run_async_migrations()) + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 00000000..17dcba0e --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,25 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/.gitkeep b/migrations/versions/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/migrations/versions/abd273076068_add_counters.py b/migrations/versions/abd273076068_add_counters.py new file mode 100644 index 00000000..9c809b06 --- /dev/null +++ b/migrations/versions/abd273076068_add_counters.py @@ -0,0 +1,33 @@ +"""add counters + +Revision ID: abd273076068 +Revises: db06fd3126ff +Create Date: 2026-10-26 22:01:01.910396 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +revision: str = 'abd273076068' +down_revision: Union[str, None] = 'db06fd3126ff' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('counters', + sa.Column('name', sa.String(length=64), nullable=False), + sa.Column('value', sa.BigInteger(), nullable=False), + sa.PrimaryKeyConstraint('name', name=op.f('pk_counters')) + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('counters') + # ### end Alembic commands ### diff --git a/migrations/versions/db06fd3126ff_create_initial_schema.py b/migrations/versions/db06fd3126ff_create_initial_schema.py new file mode 100644 index 00000000..eec6e51b --- /dev/null +++ b/migrations/versions/db06fd3126ff_create_initial_schema.py @@ -0,0 +1,107 @@ +"""create initial schema + +Revision ID: db06fd3126ff +Revises: +Create Date: 2026-10-26 17:09:16.675339 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + +revision: str = "db06fd3126ff" +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "chats", + sa.Column("id", sa.UUID(), nullable=False), + sa.Column("title", sa.String(length=100), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("now()"), + nullable=False, + ), + sa.PrimaryKeyConstraint("id", name=op.f("pk_chats")), + ) + op.create_table( + "users", + sa.Column("id", sa.UUID(), nullable=False), + sa.Column("username", sa.String(length=32), nullable=False), + sa.Column("display_name", sa.String(length=100), nullable=False), + sa.Column("password_hash", sa.String(length=255), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("now()"), + nullable=False, + ), + sa.PrimaryKeyConstraint("id", name=op.f("pk_users")), + sa.UniqueConstraint("username", name=op.f("uq_users_username")), + ) + op.create_table( + "chat_members", + sa.Column("chat_id", sa.UUID(), nullable=False), + sa.Column("user_id", sa.UUID(), nullable=False), + sa.ForeignKeyConstraint( + ["chat_id"], + ["chats.id"], + name=op.f("fk_chat_members_chat_id_chats"), + ondelete="CASCADE", + ), + sa.ForeignKeyConstraint( + ["user_id"], ["users.id"], name=op.f("fk_chat_members_user_id_users") + ), + sa.PrimaryKeyConstraint("chat_id", "user_id", name=op.f("pk_chat_members")), + ) + op.create_index( + "ix_chat_members_user_id_chat_id", + "chat_members", + ["user_id", "chat_id"], + unique=False, + ) + op.create_table( + "messages", + sa.Column("id", sa.UUID(), nullable=False), + sa.Column("chat_id", sa.UUID(), nullable=False), + sa.Column("sender_id", sa.UUID(), nullable=False), + sa.Column("text", sa.Text(), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("clock_timestamp()"), + nullable=False, + ), + sa.ForeignKeyConstraint( + ["chat_id"], ["chats.id"], name=op.f("fk_messages_chat_id_chats") + ), + sa.ForeignKeyConstraint( + ["sender_id"], ["users.id"], name=op.f("fk_messages_sender_id_users") + ), + sa.PrimaryKeyConstraint("id", name=op.f("pk_messages")), + ) + op.create_index( + "ix_messages_chat_id_created_at_id", + "messages", + ["chat_id", "created_at", "id"], + unique=False, + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index("ix_messages_chat_id_created_at_id", table_name="messages") + op.drop_table("messages") + op.drop_index("ix_chat_members_user_id_chat_id", table_name="chat_members") + op.drop_table("chat_members") + op.drop_table("users") + op.drop_table("chats") + # ### end Alembic commands ### diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..b7dd8fa8 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,6 @@ +[pytest] +testpaths = student-tests +python_files = test_*.py +asyncio_mode = auto +asyncio_default_fixture_loop_scope = session +asyncio_default_test_loop_scope = session diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..a64e71a1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,50 @@ +alembic==1.20.0 +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.15.1 +attrs==26.1.0 +certifi==2026.7.22 +charset-normalizer==3.5.2 +click==8.5.0 +fastapi==0.143.3 +h11==0.16.0 +httpcore2==2.13.1 +httpx2==2.13.1 +idna==3.20 +iniconfig==2.3.1 +jsonschema==4.26.0 +jsonschema-path==0.3.4 +jsonschema-specifications==2025.9.1 +lazy-object-proxy==1.12.0 +Mako==1.4.3 +MarkupSafe==3.0.4 +openapi-schema-validator==0.6.3 +openapi-spec-validator==0.7.2 +opentelemetry-api==1.45.1 +packaging==26.3 +pathable==0.4.4 +pluggy==1.7.0 +pydantic==2.14.1 +pydantic_core==2.50.1 +Pygments==2.21.0 +pytest==9.1.1 +PyYAML==6.0.3 +referencing==0.36.2 +requests==2.34.2 +rfc3339-validator==0.1.4 +rpds-py==2026.9.1 +six==1.17.0 +SQLAlchemy==2.1.4 +starlette==1.8.0 +truststore==0.10.5 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +urllib3==2.8.0 +uvicorn==0.54.0 +asyncpg==0.32.0 +argon2-cffi==25.1.0 +redis==8.1.0 +greenlet==3.5.6 +pydantic-settings==2.16.0 +pytest-asyncio==1.4.1 +httpx==0.28.1 diff --git a/scripts/expiry_demo.sh b/scripts/expiry_demo.sh new file mode 100755 index 00000000..cc32541d --- /dev/null +++ b/scripts/expiry_demo.sh @@ -0,0 +1,28 @@ +#!/bin/sh +set -eu + +BASE_URL="${BASE_URL:-http://localhost:8080}" +TTL="${TTL:?укажите TTL, с которым запущен API (SESSION_TTL_SECONDS)}" +API="$BASE_URL/api/v1" +USERNAME="expiry_$(date +%s)" +PASSWORD="expiry-password-123" + +code() { + curl -s -o /dev/null -w '%{http_code}' "$@" +} + +curl -sf -o /dev/null -X POST "$API/users" -H 'Content-Type: application/json' \ + -d "{\"username\":\"$USERNAME\",\"display_name\":\"Expiry\",\"password\":\"$PASSWORD\"}" +SESSION=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD") +TOKEN=$(printf '%s' "$SESSION" | python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])') +EXPIRES_AT=$(printf '%s' "$SESSION" | python3 -c 'import json, sys; print(json.load(sys.stdin)["expires_at"])') +AUTH="Authorization: Bearer $TOKEN" + +echo "SESSION_TTL_SECONDS=$TTL, login at $(date -u +%H:%M:%S)Z, expires_at=$EXPIRES_AT" +echo "GET /users/me right after login -> $(code "$API/users/me" -H "$AUTH")" +sleep $((TTL + 1)) +printf "%-35s -> %s\n" "GET /users/me after $((TTL + 1)) s" "$(code "$API/users/me" -H "$AUTH")" +echo "DELETE /auth/sessions/current after -> $(code -X DELETE "$API/auth/sessions/current" -H "$AUTH")" +NEW_TOKEN=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD" \ + | python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])') +echo "GET /users/me with new login token -> $(code "$API/users/me" -H "Authorization: Bearer $NEW_TOKEN")" diff --git a/scripts/explain_history.py b/scripts/explain_history.py new file mode 100644 index 00000000..9d571c42 --- /dev/null +++ b/scripts/explain_history.py @@ -0,0 +1,132 @@ +"""EXPLAIN (ANALYZE, BUFFERS) запроса истории на синтетических данных. + +Данные создаются в одной транзакции и откатываются в конце: база не меняется. +""" + +import asyncio +from uuid import uuid4 + +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncConnection + +from app.core.config import Settings +from app.core.database import create_db_engine + +TARGET_MESSAGES = 10_000 +NOISE_MESSAGES = 40_000 +PAGE = 51 + +HISTORY_FIRST_PAGE = """ +SELECT * FROM messages +WHERE chat_id = :chat_id +ORDER BY created_at, id +LIMIT :page +""" + +HISTORY_AFTER_CURSOR = """ +SELECT * FROM messages +WHERE chat_id = :chat_id AND (created_at, id) > (:after_created_at, :after_id) +ORDER BY created_at, id +LIMIT :page +""" + + +async def seed(connection: AsyncConnection) -> dict[str, object]: + user_id, target_id, noise_id = uuid4(), uuid4(), uuid4() + await connection.execute( + text( + "INSERT INTO users (id, username, display_name, password_hash) " + "VALUES (:id, :username, 'Explain', 'x')" + ), + {"id": user_id, "username": "explain_" + uuid4().hex[:8]}, + ) + await connection.execute( + text("INSERT INTO chats (id, title) VALUES (:a, 'target'), (:b, 'noise')"), + {"a": target_id, "b": noise_id}, + ) + await connection.execute( + text("INSERT INTO chat_members (chat_id, user_id) VALUES (:a, :u), (:b, :u)"), + {"a": target_id, "b": noise_id, "u": user_id}, + ) + for chat_id, count in ((target_id, TARGET_MESSAGES), (noise_id, NOISE_MESSAGES)): + await connection.execute( + text( + "INSERT INTO messages (id, chat_id, sender_id, text, created_at) " + "SELECT gen_random_uuid(), :chat_id, :user_id, 'message ' || g, " + "now() - make_interval(secs => :count - g) " + "FROM generate_series(1, :count) AS g" + ), + {"chat_id": chat_id, "user_id": user_id, "count": count}, + ) + await connection.execute(text("ANALYZE messages")) + + middle = ( + await connection.execute( + text( + "SELECT created_at, id FROM messages WHERE chat_id = :c " + "ORDER BY created_at, id OFFSET :o LIMIT 1" + ), + {"c": target_id, "o": TARGET_MESSAGES // 2}, + ) + ).one() + return { + "chat_id": target_id, + "after_created_at": middle.created_at, + "after_id": middle.id, + "page": PAGE, + } + + +async def explain( + connection: AsyncConnection, title: str, query: str, params: dict[str, object] +) -> None: + rows = await connection.execute( + text(f"EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING ON) {query}"), params + ) + print(f"== {title}") + print(query.strip()) + print() + for row in rows: + print(row[0]) + print() + + +async def main() -> None: + engine = create_db_engine(Settings()) # type: ignore[call-arg] + async with engine.connect() as connection: + transaction = await connection.begin() + params = await seed(connection) + total = ( + await connection.execute(text("SELECT count(*) FROM messages")) + ).scalar_one() + print( + f"messages in table: {total}; target chat: {TARGET_MESSAGES}; noise chat: {NOISE_MESSAGES}" + ) + print() + + first_page = {"chat_id": params["chat_id"], "page": PAGE} + await explain( + connection, "first page (limit+1 = 51)", HISTORY_FIRST_PAGE, first_page + ) + await explain( + connection, + "page after cursor in the middle of history", + HISTORY_AFTER_CURSOR, + params, + ) + + await connection.execute(text("SET LOCAL enable_indexscan = off")) + await connection.execute(text("SET LOCAL enable_bitmapscan = off")) + await explain( + connection, + "same page with index scans disabled (for comparison)", + HISTORY_AFTER_CURSOR, + params, + ) + + await transaction.rollback() + await engine.dispose() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/scripts/failure_demo.sh b/scripts/failure_demo.sh new file mode 100755 index 00000000..b5c7332d --- /dev/null +++ b/scripts/failure_demo.sh @@ -0,0 +1,64 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." + +BASE_URL="${BASE_URL:-http://localhost:8080}" +API="$BASE_URL/api/v1" +USERNAME="demo_$(date +%s)" +PASSWORD="demo-password-123" + +compose() { + docker compose --env-file .env "$@" +} + +code() { + curl -s -o /dev/null -w '%{http_code} (%{time_total}s)' "$@" +} + +wait_healthy() { + attempts=0 + until [ "$(compose ps --format '{{.Health}}' "$1")" = "healthy" ]; do + attempts=$((attempts + 1)) + [ "$attempts" -lt 60 ] || { echo "$1 не стал healthy" >&2; exit 1; } + sleep 1 + done +} + +curl -sf -o /dev/null -X POST "$API/users" -H 'Content-Type: application/json' \ + -d "{\"username\":\"$USERNAME\",\"display_name\":\"Demo\",\"password\":\"$PASSWORD\"}" +TOKEN=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD" \ + | python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])') +AUTH="Authorization: Bearer $TOKEN" + +echo "== baseline" +echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")" + +echo +echo "== redis stopped" +compose stop redis >/dev/null 2>&1 +echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")" +echo "POST /auth/sessions (Basic) -> $(code -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD")" +echo "GET /users/me (no token) -> $(code "$API/users/me")" +echo "GET /health/ready -> $(code "$BASE_URL/health/ready")" +echo "GET /health/live -> $(code "$BASE_URL/health/live")" +compose start redis >/dev/null 2>&1 +wait_healthy redis +echo "-- redis started again (AOF restored)" +echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")" + +echo +echo "== postgres stopped" +compose stop postgres >/dev/null 2>&1 +echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")" +echo "GET /api/v1/chats (Bearer) -> $(code "$API/chats" -H "$AUTH")" +echo "GET /health/ready -> $(code "$BASE_URL/health/ready")" +echo "GET /health/live -> $(code "$BASE_URL/health/live")" +compose start postgres >/dev/null 2>&1 +wait_healthy postgres +echo "-- postgres started again" +echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")" + +echo +echo "== logout" +echo "DELETE /auth/sessions/current -> $(code -X DELETE "$API/auth/sessions/current" -H "$AUTH")" +echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")" diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..d9ab44b3 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# Перезапуск API и всех хранилищ с сохранением volumes (make test-persistence). +# У api несколько реплик, перед ними nginx. +set -eu +cd "$(dirname "$0")/.." + +compose() { + docker compose --env-file .env "$@" +} + +# Ждёт, пока ВСЕ контейнеры service станут healthy (у api их несколько). +wait_healthy() { + for service in "$@"; do + attempts=0 + until states=$(compose ps --format '{{.Health}}' "$service") \ + && [ -n "$states" ] \ + && [ -z "$(printf '%s\n' "$states" | grep -v '^healthy$')" ]; do + attempts=$((attempts + 1)) + if [ "$attempts" -ge 60 ]; then + echo "$service не стал healthy за 60 секунд" >&2 + exit 1 + fi + sleep 1 + done + done +} + +compose restart postgres redis +wait_healthy postgres redis +compose restart api +wait_healthy api +# TODO(3.3): если nginx перерезолвит DNS сам, рестарт LB не обязателен — +# но перезапуск «всего стенда» в защите всё равно его включает. +compose restart nginx +wait_healthy nginx diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..34437faf --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Останавливает одну API-реплику по TARGET_INSTANCE_ID (сценарий отказа, make test-failover). +# TODO(3.5): реализовать — см. docs/LAB3_TASKS.md, задача 3.5. +set -eu +cd "$(dirname "$0")/.." +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" + +# Что нужно сделать: +# 1. Сопоставить TARGET_INSTANCE_ID (значение X-Instance-Id) с контейнером. +# Проще всего, если X-Instance-Id = короткий id контейнера (HOSTNAME): +# docker compose --env-file .env ps -q api -> полные id реплик api +# и найти тот, что начинается с TARGET_INSTANCE_ID. +# 2. Убедиться, что это контейнер service `api` ЭТОГО compose-проекта, +# а не postgres/nginx/чужой контейнер. Не нашли -> exit 1. +# 3. Остановить его (docker stop [-t N]) и оставить остановленным. +# Не трогать БД/LB/volumes. Уложиться в 10 секунд. +# 4. Напечатать (в stderr), что именно остановлено — пригодится для отчёта. +# Восстановление — отдельно: make restore. + +echo 'TODO: implement stop-one.sh' >&2 +exit 2 diff --git a/student-tests/conftest.py b/student-tests/conftest.py new file mode 100644 index 00000000..f2becc50 --- /dev/null +++ b/student-tests/conftest.py @@ -0,0 +1,200 @@ +"""Фикстуры async-тестов на реальных PostgreSQL и Redis. + +Перед запуском: `make test-stand-up` (тестовый стенд из compose.yaml + compose.test.yaml +и база _test). +Переменные: + TEST_DATABASE_URL по умолчанию postgresql+asyncpg://messenger:messenger@localhost:5432/messenger_test + TEST_USE_MIGRATIONS=1 строить схему через `alembic upgrade head` вместо Base.metadata.create_all + TEST_REDIS_HOST/PORT/PASSWORD/DB по умолчанию localhost:6379, база 15 + DOCKER_TESTS=1 включить тесты, которые перезапускают контейнеры compose +""" + +import asyncio +import os +from collections.abc import AsyncIterator, Callable + +import httpx +import pytest +from alembic import command +from alembic.config import Config +from fastapi import FastAPI +from redis.asyncio import Redis +from redis.exceptions import RedisError +from sqlalchemy import text +from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine +from sqlalchemy.pool import NullPool + +from app.core.config import Settings +from app.core.redis import create_redis_client +from app.main import create_app +from app.models import Base + +DEFAULT_TEST_DATABASE_URL = ( + "postgresql+asyncpg://messenger:messenger@localhost:5432/messenger_test" +) + + +def _database_url() -> str: + return os.environ.get("TEST_DATABASE_URL", DEFAULT_TEST_DATABASE_URL) + + +def _redis_settings() -> dict[str, object]: + return { + "redis_host": os.environ.get("TEST_REDIS_HOST", "localhost"), + "redis_port": int(os.environ.get("TEST_REDIS_PORT", "6379")), + "redis_password": os.environ.get("TEST_REDIS_PASSWORD") or None, + "redis_db": int(os.environ.get("TEST_REDIS_DB", "15")), + } + + +def _alembic_config(url: str) -> Config: + config = Config("alembic.ini") + config.set_main_option("sqlalchemy.url", url.replace("%", "%%")) + return config + + +async def upgrade_head(url: str) -> None: + """alembic command.upgrade внутри вызывает asyncio.run, поэтому — в отдельном потоке.""" + await asyncio.to_thread(command.upgrade, _alembic_config(url), "head") + + +async def reset_schema(engine: AsyncEngine) -> None: + async with engine.begin() as connection: + await connection.run_sync(Base.metadata.drop_all) + await connection.execute(text("DROP TABLE IF EXISTS alembic_version")) + + +async def close_app(app: FastAPI) -> None: + """ASGITransport не запускает lifespan, поэтому ресурсы приложения закрываются явно.""" + await app.state.redis.aclose() + await app.state.engine.dispose() + + +@pytest.fixture(scope="session") +def database_url() -> str: + return _database_url() + + +@pytest.fixture(scope="session") +async def db_engine(database_url: str) -> AsyncIterator[AsyncEngine]: + engine = create_async_engine(database_url, poolclass=NullPool) + try: + async with engine.connect() as connection: + await connection.execute(text("SELECT 1")) + except (SQLAlchemyError, OSError) as error: + pytest.exit( + f"PostgreSQL недоступна по {engine.url.render_as_string(hide_password=True)}: " + f"{type(error).__name__}. Выполните `make test-stand-up`.", + returncode=2, + ) + + await reset_schema(engine) + + if os.environ.get("TEST_USE_MIGRATIONS") == "1": + await upgrade_head(database_url) + else: + async with engine.begin() as connection: + await connection.run_sync(Base.metadata.create_all) + + yield engine + await engine.dispose() + + +@pytest.fixture(scope="session") +async def redis_client() -> AsyncIterator[Redis]: + settings = build_settings(_database_url()) + if settings.redis_db == 0: + pytest.exit("TEST_REDIS_DB=0 запрещён: тесты очищают базу Redis.", returncode=2) + + client = create_redis_client(settings) + try: + await client.ping() + except (RedisError, OSError) as error: + pytest.exit( + f"Redis недоступен: {type(error).__name__}. Выполните `make test-stand-up`.", + returncode=2, + ) + + yield client + await client.flushdb() + await client.aclose() + + +@pytest.fixture(autouse=True) +async def clean_db(db_engine: AsyncEngine, redis_client: Redis) -> None: + names = ", ".join(f'"{table.name}"' for table in Base.metadata.sorted_tables) + if names: + async with db_engine.begin() as connection: + await connection.execute(text(f"TRUNCATE {names} RESTART IDENTITY CASCADE")) + await redis_client.flushdb() + + +def build_settings(database_url: str, **overrides: object) -> Settings: + values: dict[str, object] = { + "app_host": "127.0.0.1", + "app_port": 8080, + "app_env": "test", + "max_messages": 1000, + "max_in_flight": 32, + "log_level": "CRITICAL", + "database_url": database_url, + "session_ttl_seconds": 300, + "db_pool_size": 10, + "db_max_overflow": 10, + **_redis_settings(), + } + values.update(overrides) + return Settings(_env_file=None, **values) # type: ignore[arg-type] + + +@pytest.fixture +def settings(database_url: str) -> Settings: + return build_settings(database_url) + + +@pytest.fixture +def make_settings(database_url: str) -> Callable[..., Settings]: + def factory(**overrides: object) -> Settings: + url = str(overrides.pop("database_url", database_url)) + return build_settings(url, **overrides) + + return factory + + +def make_client( + app, *, raise_app_exceptions: bool = True, timeout: float = 30 +) -> httpx.AsyncClient: + """ASGITransport не запускает lifespan, поэтому ресурсы закрывает вызывающий (close_app).""" + return httpx.AsyncClient( + transport=httpx.ASGITransport( + app=app, raise_app_exceptions=raise_app_exceptions + ), + base_url="http://testserver", + timeout=timeout, + ) + + +@pytest.fixture +def app(settings: Settings): + application = create_app(settings) + + @application.get("/__test__/explode", include_in_schema=False) + async def explode() -> None: + raise RuntimeError("test-secret") + + return application + + +@pytest.fixture +async def client(app) -> AsyncIterator[httpx.AsyncClient]: + async with make_client(app) as test_client: + yield test_client + await close_app(app) + + +@pytest.fixture +async def client_no_raise(app) -> AsyncIterator[httpx.AsyncClient]: + async with make_client(app, raise_app_exceptions=False) as test_client: + yield test_client + await close_app(app) diff --git a/student-tests/helpers.py b/student-tests/helpers.py new file mode 100644 index 00000000..97bf8f99 --- /dev/null +++ b/student-tests/helpers.py @@ -0,0 +1,88 @@ +import base64 +from uuid import uuid4 + +from httpx import AsyncClient + +PASSWORD = "Correct-horse-battery-1" + + +def basic_header(username: str, password: str) -> dict[str, str]: + raw = f"{username}:{password}".encode() + return {"Authorization": "Basic " + base64.b64encode(raw).decode()} + + +def bearer(actor: dict) -> dict[str, str]: + return {"Authorization": "Bearer " + actor["token"]} + + +async def register( + client: AsyncClient, + username: str | None = None, + display_name: str = "Тестовый пользователь", + password: str = PASSWORD, +) -> dict: + """Регистрация без входа. Возвращает User + password (нужен для login).""" + actual_username = username or "user_" + uuid4().hex[:12] + response = await client.post( + "/api/v1/users", + json={ + "username": actual_username, + "display_name": display_name, + "password": password, + }, + ) + assert response.status_code == 201, response.text + return {**response.json(), "password": password} + + +async def login(client: AsyncClient, user: dict) -> dict: + response = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], user["password"]), + ) + assert response.status_code == 201, response.text + return {**user, "token": response.json()["token"]} + + +async def create_user(client: AsyncClient, **kwargs) -> dict: + """Регистрация + вход: актор с полями id, username, token, ...""" + return await login(client, await register(client, **kwargs)) + + +async def create_chat( + client: AsyncClient, + user: dict, + *members: dict, + title: str = "Тестовый чат", +) -> dict: + member_ids = [user["id"], *(member["id"] for member in members)] + response = await client.post( + "/api/v1/chats", + headers=bearer(user), + json={"title": title, "member_ids": member_ids}, + ) + assert response.status_code == 201, response.text + return response.json() + + +def idempotency_header(key: str | None = None) -> dict[str, str]: + return {"Idempotency-Key": key or uuid4().hex} + + +async def send_message( + client: AsyncClient, user: dict, chat: dict, text: str, key: str | None = None +): + """POST сообщения требует Idempotency-Key; по умолчанию — новый ключ.""" + return await client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers={**bearer(user), **idempotency_header(key)}, + json={"text": text}, + ) + + +def assert_request_id(response) -> str: + header_id = response.headers.get("X-Request-Id") + assert header_id + if response.status_code >= 400: + assert response.json()["error"]["request_id"] == header_id + return header_id diff --git a/student-tests/test_auth.py b/student-tests/test_auth.py new file mode 100644 index 00000000..6746320f --- /dev/null +++ b/student-tests/test_auth.py @@ -0,0 +1,298 @@ +import asyncio +import hashlib +from datetime import datetime + +from httpx import AsyncClient + +from app.main import create_app +from conftest import close_app, make_client +from helpers import ( + PASSWORD, + assert_request_id, + basic_header, + bearer, + create_user, + login, + register, +) + + +async def test_login_returns_opaque_bearer_session(client: AsyncClient): + user = await register(client) + + response = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], user["password"]), + ) + + assert response.status_code == 201 + body = response.json() + assert set(body) == {"token", "token_type", "expires_at", "user"} + assert body["token_type"] == "Bearer" + assert len(body["token"]) >= 43 + assert body["token"] not in (user["id"], user["username"]) + assert body["user"]["id"] == user["id"] + assert body["expires_at"].endswith("Z") + assert datetime.fromisoformat(body["expires_at"]) + assert_request_id(response) + + +async def test_each_login_issues_a_different_token(client: AsyncClient): + user = await register(client) + first = await login(client, user) + second = await login(client, user) + + assert first["token"] != second["token"] + + +async def test_password_with_colon_works(client: AsyncClient): + user = await register(client, password="pass:with:colons-123") + + assert (await login(client, user))["token"] + + +async def test_unicode_password_works(client: AsyncClient): + user = await register(client, password="Пароль-с-юникодом-🔑-123") + + assert (await login(client, user))["token"] + + +async def test_wrong_password_and_unknown_user_look_identical(client: AsyncClient): + user = await register(client) + + wrong_password = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], "wrong-password-123"), + ) + unknown_user = await client.post( + "/api/v1/auth/sessions", + headers=basic_header("no_such_user", "wrong-password-123"), + ) + + for response in (wrong_password, unknown_user): + assert response.status_code == 401 + assert response.json()["error"]["code"] == "unauthorized" + assert response.headers["WWW-Authenticate"].startswith( + 'Basic realm="messenger"' + ) + assert_request_id(response) + assert ( + wrong_password.json()["error"]["message"] + == unknown_user.json()["error"]["message"] + ) + + +async def test_login_requires_basic_scheme(client: AsyncClient): + user = await register(client) + session = await login(client, user) + + for headers in ({}, bearer(session), {"Authorization": "Basic !!!not-base64"}): + response = await client.post("/api/v1/auth/sessions", headers=headers) + assert response.status_code == 401 + assert response.headers["WWW-Authenticate"].startswith("Basic") + + +async def test_business_endpoints_require_bearer(client: AsyncClient): + user = await register(client) + + for headers in ( + {}, + basic_header(user["username"], user["password"]), + {"Authorization": "Bearer "}, + {"Authorization": "Bearer unknown-token-value"}, + {"X-User-Id": user["id"]}, + ): + response = await client.get("/api/v1/users/me", headers=headers) + assert response.status_code == 401, headers + assert response.json()["error"]["code"] == "unauthorized" + assert response.headers["WWW-Authenticate"].startswith( + 'Bearer realm="messenger"' + ) + assert_request_id(response) + + +async def test_x_user_id_cannot_override_bearer_identity(client: AsyncClient): + alice = await create_user(client) + eve = await create_user(client) + + response = await client.get( + "/api/v1/users/me", + headers={**bearer(alice), "X-User-Id": eve["id"]}, + ) + + assert response.status_code == 200 + assert response.json()["id"] == alice["id"] + + +async def test_logout_revokes_only_current_session(client: AsyncClient): + user = await register(client) + first = await login(client, user) + second = await login(client, user) + + assert ( + await client.delete("/api/v1/auth/sessions/current", headers=bearer(first)) + ).status_code == 204 + + assert ( + await client.get("/api/v1/users/me", headers=bearer(first)) + ).status_code == 401 + assert ( + await client.get("/api/v1/users/me", headers=bearer(second)) + ).status_code == 200 + + +async def test_logout_twice_returns_401(client: AsyncClient): + session = await create_user(client) + + first = await client.delete( + "/api/v1/auth/sessions/current", headers=bearer(session) + ) + again = await client.delete( + "/api/v1/auth/sessions/current", headers=bearer(session) + ) + + assert first.status_code == 204 + assert first.content == b"" + assert again.status_code == 401 + + +async def test_session_expires_after_ttl(make_settings): + app = create_app(make_settings(session_ttl_seconds=2)) + async with make_client(app) as client: + session = await create_user(client) + assert ( + await client.get("/api/v1/users/me", headers=bearer(session)) + ).status_code == 200 + + await asyncio.sleep(3) + + response = await client.get("/api/v1/users/me", headers=bearer(session)) + assert response.status_code == 401 + assert response.headers["WWW-Authenticate"].startswith("Bearer") + await close_app(app) + + +async def test_token_is_stored_only_as_sha256_key(client: AsyncClient, redis_client): + session = await create_user(client) + + keys = [key async for key in redis_client.scan_iter("session:*")] + token_hash = hashlib.sha256(session["token"].encode()).hexdigest() + + assert keys == [f"session:{token_hash}"] + assert await redis_client.get(keys[0]) == session["id"] + assert all(session["token"] not in key for key in keys) + + +async def test_password_is_not_stored_in_plaintext(client: AsyncClient, db_engine): + from sqlalchemy import text + + await register(client, username="hash_check") + + async with db_engine.connect() as connection: + result = await connection.execute( + text("SELECT password_hash FROM users WHERE username = 'hash_check'") + ) + stored = result.scalar_one() + + assert PASSWORD not in stored + assert stored.startswith("$argon2id$") + + +async def test_login_rejects_out_of_range_password_like_wrong_password( + client: AsyncClient, +): + user = await register(client) + reference = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], "wrong-password-123"), + ) + + for password in ("x" * 11, "x" * 129, ""): + response = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], password), + ) + assert response.status_code == 401 + assert ( + response.json()["error"]["message"] == reference.json()["error"]["message"] + ) + assert ( + response.headers["WWW-Authenticate"] + == reference.headers["WWW-Authenticate"] + ) + + +async def test_malformed_basic_credentials_return_401(client: AsyncClient): + import base64 + + def raw(value: bytes) -> dict[str, str]: + return {"Authorization": "Basic " + base64.b64encode(value).decode()} + + for headers in ( + raw(b"no-colon-here"), + raw(b":password-without-user"), + raw(b"\xff\xfe:invalid-utf8"), + {"Authorization": "Basic"}, + {"Authorization": "Digest abc"}, + ): + response = await client.post("/api/v1/auth/sessions", headers=headers) + assert response.status_code == 401, headers + assert response.headers["WWW-Authenticate"].startswith("Basic") + + +async def test_unknown_user_takes_comparable_time_to_wrong_password( + client: AsyncClient, +): + import statistics + import time + + user = await register(client) + + async def measure(username: str) -> float: + samples = [] + for _ in range(5): + started = time.perf_counter() + await client.post( + "/api/v1/auth/sessions", + headers=basic_header(username, "wrong-password-123"), + ) + samples.append(time.perf_counter() - started) + return statistics.median(samples) + + wrong_password = await measure(user["username"]) + unknown_user = await measure("no_such_user") + + assert unknown_user > wrong_password * 0.5 + + +async def test_invalid_basic_username_returns_401_not_500(client: AsyncClient): + user = await register(client) + reference = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], "wrong-password-123"), + ) + + for username in ("bad\u0000name", "UPPER", "no", "x" * 33, "имя", "a b c"): + response = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(username, "wrong-password-123"), + ) + assert response.status_code == 401, username + assert ( + response.json()["error"]["message"] == reference.json()["error"]["message"] + ) + assert ( + response.headers["WWW-Authenticate"] + == reference.headers["WWW-Authenticate"] + ) + + +async def test_whitespace_only_password_at_login_returns_401(client: AsyncClient): + user = await register(client) + + response = await client.post( + "/api/v1/auth/sessions", headers=basic_header(user["username"], " " * 12) + ) + + assert response.status_code == 401 + assert response.headers["WWW-Authenticate"].startswith("Basic") diff --git a/student-tests/test_chats.py b/student-tests/test_chats.py new file mode 100644 index 00000000..475c946a --- /dev/null +++ b/student-tests/test_chats.py @@ -0,0 +1,209 @@ +from urllib.parse import quote +from uuid import uuid4 + +import pytest +from httpx import AsyncClient + +from helpers import assert_request_id, bearer, create_chat, create_user + + +async def test_create_and_get_chat(client: AsyncClient): + alice = await create_user(client) + bob = await create_user(client) + chat = await create_chat(client, alice, bob) + + response = await client.get( + f"/api/v1/chats/{chat['id']}", + headers=bearer(bob), + ) + + assert response.status_code == 200 + assert response.json() == chat + assert_request_id(response) + + +async def test_chat_is_not_visible_to_outsider(client: AsyncClient): + alice = await create_user(client) + bob = await create_user(client) + eve = await create_user(client) + chat = await create_chat(client, alice, bob) + + response = await client.get( + f"/api/v1/chats/{chat['id']}", + headers=bearer(eve), + ) + + assert response.status_code == 404 + assert response.json()["error"]["code"] == "not_found" + assert_request_id(response) + + +async def test_chat_creation_requires_creator_in_members(client: AsyncClient): + alice = await create_user(client) + bob = await create_user(client) + + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "Чат", "member_ids": [bob["id"]]}, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + + +@pytest.mark.parametrize( + "payload", + [ + {"title": "", "member_ids": []}, + {"title": " ", "member_ids": []}, + ], +) +async def test_chat_payload_boundaries(client: AsyncClient, payload: dict): + alice = await create_user(client) + payload["member_ids"] = [alice["id"]] + + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json=payload, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + + +async def test_duplicate_members_are_rejected(client: AsyncClient): + alice = await create_user(client) + bob = await create_user(client) + + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "Чат", "member_ids": [alice["id"], bob["id"], bob["id"]]}, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + + +async def test_unknown_member_is_rejected(client: AsyncClient): + alice = await create_user(client) + + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "Чат", "member_ids": [alice["id"], str(uuid4())]}, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + + +async def test_chat_exact_title_limit_is_accepted(client: AsyncClient): + alice = await create_user(client) + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "x" * 100, "member_ids": [alice["id"]]}, + ) + + assert response.status_code == 201 + assert len(response.json()["title"]) == 100 + + +async def test_chat_list_hides_chats_without_membership(client: AsyncClient): + alice = await create_user(client) + bob = await create_user(client) + chat_for_bob = await create_chat(client, bob, title="Only Bob") + chat_for_alice = await create_chat(client, alice, title="Only Alice") + + response = await client.get( + "/api/v1/chats?limit=100", + headers=bearer(alice), + ) + + assert response.status_code == 200 + ids = {item["id"] for item in response.json()["items"]} + assert chat_for_alice["id"] in ids + assert chat_for_bob["id"] not in ids + + +async def test_nonexistent_chat_returns_404(client: AsyncClient): + alice = await create_user(client) + + response = await client.get( + "/api/v1/chats/00000000-0000-4000-8000-000000000000", + headers=bearer(alice), + ) + + assert response.status_code == 404 + assert response.json()["error"]["code"] == "not_found" + assert_request_id(response) + + +async def test_failed_chat_creation_leaves_no_partial_data( + client: AsyncClient, db_engine +): + """Атомарность: неизвестный участник -> нет ни чата, ни записей членства.""" + from sqlalchemy import text + + alice = await create_user(client) + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "Чат", "member_ids": [alice["id"], str(uuid4())]}, + ) + assert response.status_code == 400 + + async with db_engine.connect() as connection: + chats = ( + await connection.execute(text("SELECT count(*) FROM chats")) + ).scalar_one() + members = ( + await connection.execute(text("SELECT count(*) FROM chat_members")) + ).scalar_one() + assert chats == 0 and members == 0 + + +async def test_chat_list_pagination_and_order(client: AsyncClient): + alice = await create_user(client) + chats = [await create_chat(client, alice, title=f"chat-{i}") for i in range(5)] + + page1 = (await client.get("/api/v1/chats?limit=2", headers=bearer(alice))).json() + page2 = ( + await client.get( + f"/api/v1/chats?limit=2&cursor={quote(page1['next_cursor'], safe='')}", + headers=bearer(alice), + ) + ).json() + page3 = ( + await client.get( + f"/api/v1/chats?limit=2&cursor={quote(page2['next_cursor'], safe='')}", + headers=bearer(alice), + ) + ).json() + + ids = [c["id"] for page in (page1, page2, page3) for c in page["items"]] + assert ids == [c["id"] for c in chats] + assert page3["next_cursor"] is None + + +async def test_chat_endpoints_require_bearer(client: AsyncClient): + assert (await client.get("/api/v1/chats")).status_code == 401 + assert ( + await client.post("/api/v1/chats", json={"title": "x", "member_ids": []}) + ).status_code in (401, 400) + + +async def test_nul_in_chat_title_is_rejected(client: AsyncClient): + alice = await create_user(client) + + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "chat\u0000title", "member_ids": [alice["id"]]}, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..9417866e --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,266 @@ +"""Конкурентность на реальной БД: гонка регистрации и параллельная запись.""" + +import asyncio +from collections import Counter +from uuid import uuid4 + +from sqlalchemy import text + +from app.main import create_app +from conftest import close_app, make_client +from helpers import PASSWORD, basic_header, bearer, create_user, register + + +async def test_parallel_registration_of_same_username_creates_exactly_one_user( + make_settings, +): + app = create_app( + make_settings(max_in_flight=64, db_pool_size=20, db_max_overflow=20) + ) + + async with make_client(app) as client: + responses = await asyncio.gather( + *( + client.post( + "/api/v1/users", + json={ + "username": "race_user", + "display_name": "Race", + "password": PASSWORD, + }, + ) + for _ in range(16) + ) + ) + await close_app(app) + + statuses = Counter(response.status_code for response in responses) + assert statuses == Counter({201: 1, 409: 15}), statuses + + +async def test_100_messages_from_10_clients(make_settings): + app = create_app( + make_settings( + max_messages=1000, max_in_flight=100, db_pool_size=20, db_max_overflow=20 + ) + ) + + async with make_client(app) as client: + users = [ + await create_user(client, username=f"load_{i}_{uuid4().hex[:6]}") + for i in range(10) + ] + created = await client.post( + "/api/v1/chats", + headers=bearer(users[0]), + json={"title": "Concurrency", "member_ids": [u["id"] for u in users]}, + ) + chat = created.json() + + async def worker(index: int, user: dict) -> list[dict]: + responses = await asyncio.gather( + *( + client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers=bearer(user), + json={"text": f"client-{index}-message-{n}"}, + ) + for n in range(10) + ) + ) + assert all(r.status_code == 201 for r in responses), [ + r.text for r in responses if r.status_code != 201 + ] + return [r.json() for r in responses] + + results = await asyncio.gather(*(worker(i, u) for i, u in enumerate(users))) + messages = [m for batch in results for m in batch] + + assert len(messages) == 100 + assert len({m["id"] for m in messages}) == 100 + assert Counter(m["sender_id"] for m in messages) == Counter( + {u["id"]: 10 for u in users} + ) + + history = await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=100", headers=bearer(users[0]) + ) + items = history.json()["items"] + assert len(items) == 100 + assert len({m["id"] for m in items}) == 100 + await close_app(app) + + +async def test_chat_creation_in_parallel_is_atomic(make_settings, db_engine): + app = create_app( + make_settings(max_in_flight=32, db_pool_size=20, db_max_overflow=20) + ) + + async with make_client(app) as client: + alice = await create_user(client) + bob = await create_user(client) + responses = await asyncio.gather( + *( + client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": f"c{i}", "member_ids": [alice["id"], bob["id"]]}, + ) + for i in range(10) + ) + ) + assert all(r.status_code == 201 for r in responses) + await close_app(app) + + async with db_engine.connect() as connection: + chats = ( + await connection.execute(text("SELECT count(*) FROM chats")) + ).scalar_one() + members = ( + await connection.execute(text("SELECT count(*) FROM chat_members")) + ).scalar_one() + assert chats == 10 and members == 20 + + +async def test_parallel_registration_of_distinct_usernames_all_succeed(make_settings): + app = create_app(make_settings(max_in_flight=64)) + async with make_client(app) as client: + responses = await asyncio.gather( + *( + client.post( + "/api/v1/users", + json={ + "username": f"distinct_{i}", + "display_name": "D", + "password": PASSWORD, + }, + ) + for i in range(12) + ) + ) + await close_app(app) + + assert [r.status_code for r in responses] == [201] * 12 + assert len({r.json()["id"] for r in responses}) == 12 + + +async def test_parallel_logins_issue_distinct_valid_tokens(make_settings): + app = create_app(make_settings(max_in_flight=64)) + async with make_client(app) as client: + user = await register(client) + responses = await asyncio.gather( + *( + client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], user["password"]), + ) + for _ in range(10) + ) + ) + tokens = [r.json()["token"] for r in responses] + checks = await asyncio.gather( + *( + client.get( + "/api/v1/users/me", headers={"Authorization": f"Bearer {token}"} + ) + for token in tokens + ) + ) + await close_app(app) + + assert all(r.status_code == 201 for r in responses) + assert len(set(tokens)) == 10 + assert all(r.status_code == 200 for r in checks) + + +async def test_parallel_logout_of_same_token_succeeds_once(make_settings): + app = create_app(make_settings(max_in_flight=64)) + async with make_client(app) as client: + session = await create_user(client) + responses = await asyncio.gather( + *( + client.delete("/api/v1/auth/sessions/current", headers=bearer(session)) + for _ in range(10) + ) + ) + await close_app(app) + + assert Counter(r.status_code for r in responses) == Counter({204: 1, 401: 9}) + + +async def test_message_limit_is_exact_under_concurrency(make_settings, db_engine): + app = create_app( + make_settings( + max_messages=5, max_in_flight=64, db_pool_size=20, db_max_overflow=20 + ) + ) + async with make_client(app) as client: + user = await create_user(client) + chat = ( + await client.post( + "/api/v1/chats", + headers=bearer(user), + json={"title": "limit", "member_ids": [user["id"]]}, + ) + ).json() + responses = await asyncio.gather( + *( + client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers=bearer(user), + json={"text": f"m{i}"}, + ) + for i in range(20) + ) + ) + await close_app(app) + + assert Counter(r.status_code for r in responses) == Counter({201: 5, 503: 15}) + async with db_engine.connect() as connection: + stored = ( + await connection.execute(text("SELECT count(*) FROM messages")) + ).scalar_one() + counter = ( + await connection.execute( + text("SELECT value FROM counters WHERE name = 'messages'") + ) + ).scalar_one() + assert stored == counter == 5 + + +async def test_failed_chat_insert_rolls_back_whole_transaction( + make_settings, db_engine, monkeypatch +): + import app.domain.chats as chats_domain + + async def accept_everyone(_session, user_ids): + return set(user_ids) + + app = create_app(make_settings()) + async with make_client(app) as client: + alice = await create_user(client) + monkeypatch.setattr( + chats_domain.users_storage, "get_existing_user_ids", accept_everyone + ) + response = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "broken", "member_ids": [alice["id"], str(uuid4())]}, + ) + monkeypatch.undo() + after = await client.post( + "/api/v1/chats", + headers=bearer(alice), + json={"title": "ok", "member_ids": [alice["id"]]}, + ) + await close_app(app) + + assert response.status_code == 400 + assert after.status_code == 201 + async with db_engine.connect() as connection: + titles = (await connection.execute(text("SELECT title FROM chats"))).scalars() + members = ( + await connection.execute(text("SELECT count(*) FROM chat_members")) + ).scalar_one() + assert list(titles) == ["ok"] + assert members == 1 diff --git a/student-tests/test_constraints.py b/student-tests/test_constraints.py new file mode 100644 index 00000000..11aa1f3e --- /dev/null +++ b/student-tests/test_constraints.py @@ -0,0 +1,46 @@ +"""Ограничения на уровне БД: приложение — не единственный защитник данных.""" + +import pytest +from sqlalchemy import text +from sqlalchemy.exc import IntegrityError + +from helpers import create_chat, create_user + + +async def test_duplicate_username_is_rejected_by_database(client, db_engine): + await create_user(client, username="unique_name") + + with pytest.raises(IntegrityError): + async with db_engine.begin() as connection: + await connection.execute( + text( + "INSERT INTO users (id, username, display_name, password_hash) " + "VALUES (gen_random_uuid(), 'unique_name', 'Other', 'hash')" + ) + ) + + +async def test_message_requires_existing_chat(client, db_engine): + alice = await create_user(client) + + with pytest.raises(IntegrityError): + async with db_engine.begin() as connection: + await connection.execute( + text( + "INSERT INTO messages (id, chat_id, sender_id, text) " + "VALUES (gen_random_uuid(), gen_random_uuid(), :sender, 'x')" + ), + {"sender": alice["id"]}, + ) + + +async def test_duplicate_membership_is_rejected_by_database(client, db_engine): + alice = await create_user(client) + chat = await create_chat(client, alice) + + with pytest.raises(IntegrityError): + async with db_engine.begin() as connection: + await connection.execute( + text("INSERT INTO chat_members (chat_id, user_id) VALUES (:c, :u)"), + {"c": chat["id"], "u": alice["id"]}, + ) diff --git a/student-tests/test_errors.py b/student-tests/test_errors.py new file mode 100644 index 00000000..3fe5e939 --- /dev/null +++ b/student-tests/test_errors.py @@ -0,0 +1,76 @@ +import json +import logging + +from httpx import AsyncClient + +from helpers import assert_request_id + + +async def test_missing_token_returns_401(client: AsyncClient): + response = await client.get("/api/v1/users/me") + + assert response.status_code == 401 + assert response.json()["error"]["code"] == "unauthorized" + assert_request_id(response) + + +async def test_garbage_token_returns_401(client: AsyncClient): + response = await client.get( + "/api/v1/users/me", headers={"Authorization": "Bearer garbage"} + ) + + assert response.status_code == 401 + assert response.json()["error"]["code"] == "unauthorized" + assert_request_id(response) + + +async def test_malformed_json_returns_400(client: AsyncClient): + response = await client.post( + "/api/v1/users", + content='{"username":"alice",', + headers={"Content-Type": "application/json"}, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + assert_request_id(response) + + +async def test_not_found_returns_404(client: AsyncClient): + response = await client.get("/no-such-path") + + assert response.status_code == 404 + assert response.json()["error"]["code"] == "not_found" + assert_request_id(response) + + +async def test_method_not_allowed_preserves_405(client: AsyncClient): + response = await client.delete("/api/v1/users") + + assert response.status_code == 405 + assert response.json()["error"]["code"] == "invalid_request" + assert_request_id(response) + + +async def test_unhandled_exception_is_hidden_from_client( + client_no_raise: AsyncClient, caplog +): + with caplog.at_level(logging.ERROR, logger="app.errors.handlers"): + response = await client_no_raise.get("/__test__/explode") + + assert response.status_code == 500 + payload = response.json() + assert payload["error"]["code"] == "internal_error" + assert payload["error"]["message"] == "Внутренняя ошибка сервера" + assert "test-secret" not in json.dumps(payload, ensure_ascii=False) + assert "Traceback" not in json.dumps(payload, ensure_ascii=False) + assert "Unhandled error request_id=" in caplog.text + assert "test-secret" in caplog.text + assert_request_id(response) + + +async def test_success_response_contains_request_id(client: AsyncClient): + response = await client.get("/health/live") + + assert response.status_code == 200 + assert response.headers.get("X-Request-Id") diff --git a/student-tests/test_failures.py b/student-tests/test_failures.py new file mode 100644 index 00000000..9338c3b4 --- /dev/null +++ b/student-tests/test_failures.py @@ -0,0 +1,138 @@ +"""Недоступность Redis и PostgreSQL: ограниченный 503, без анонимного доступа.""" + +import time + +from app.main import create_app +from conftest import close_app, make_client +from helpers import PASSWORD, assert_request_id, basic_header, bearer, create_user + +ME = "/api/v1/users/me" +DEAD_DATABASE_URL = "postgresql+asyncpg://nobody:secretpass@127.0.0.1:1/none" + + +def assert_unavailable(response) -> None: + assert response.status_code == 503, response.text + assert response.json()["error"]["code"] == "unavailable" + assert "secretpass" not in response.text + assert "Traceback" not in response.text + assert_request_id(response) + + +async def test_redis_down_gives_503_for_login_and_protected_endpoints(make_settings): + alive = create_app(make_settings()) + async with make_client(alive) as client: + session = await create_user(client) + await close_app(alive) + + app = create_app(make_settings(redis_port=1, redis_connect_timeout_seconds=0.5)) + async with make_client(app, raise_app_exceptions=False) as client: + started = time.perf_counter() + protected = await client.get(ME, headers=bearer(session)) + elapsed = time.perf_counter() - started + + login = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(session["username"], session["password"]), + ) + logout = await client.delete( + "/api/v1/auth/sessions/current", headers=bearer(session) + ) + ready = await client.get("/health/ready") + live = await client.get("/health/live") + + await close_app(app) + + for response in (protected, login, logout, ready): + assert_unavailable(response) + assert live.status_code == 200 + assert elapsed < 3 + + +async def test_redis_down_keeps_registration_working(make_settings): + app = create_app(make_settings(redis_port=1, redis_connect_timeout_seconds=0.5)) + async with make_client(app, raise_app_exceptions=False) as client: + response = await client.post( + "/api/v1/users", + json={"username": "no_redis", "display_name": "N", "password": PASSWORD}, + ) + await close_app(app) + + assert response.status_code == 201 + + +async def test_database_down_with_valid_session_gives_503(make_settings): + alive = create_app(make_settings()) + async with make_client(alive) as client: + session = await create_user(client) + await close_app(alive) + + app = create_app( + make_settings(database_url=DEAD_DATABASE_URL, db_connect_timeout_seconds=1) + ) + async with make_client(app, raise_app_exceptions=False) as client: + protected = await client.get(ME, headers=bearer(session)) + chats = await client.get("/api/v1/chats", headers=bearer(session)) + login = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(session["username"], session["password"]), + ) + await close_app(app) + + for response in (protected, chats, login): + assert_unavailable(response) + + +async def test_dependency_failure_never_authenticates_anonymous_requests(make_settings): + app = create_app(make_settings(redis_port=1, redis_connect_timeout_seconds=0.5)) + async with make_client(app, raise_app_exceptions=False) as client: + no_token = await client.get(ME) + garbage = await client.get(ME, headers={"Authorization": "Bearer garbage"}) + await close_app(app) + + assert no_token.status_code == 401 + assert_unavailable(garbage) + + +async def test_statement_timeout_gives_503(make_settings, monkeypatch): + from sqlalchemy import text + + import app.domain.users as users_domain + + async def slow_lookup(session, _username): + await session.execute(text("SELECT pg_sleep(1)")) + + app = create_app(make_settings(db_statement_timeout_ms=100)) + monkeypatch.setattr(users_domain.users_storage, "get_user_by_username", slow_lookup) + async with make_client(app, raise_app_exceptions=False) as client: + response = await client.post( + "/api/v1/users", + json={"username": "slow_user", "display_name": "S", "password": PASSWORD}, + ) + await close_app(app) + + assert_unavailable(response) + assert response.headers["Retry-After"] == "1" + + +async def test_non_transient_database_error_stays_500(make_settings, monkeypatch): + from sqlalchemy import text + + import app.domain.users as users_domain + + async def broken_lookup(session, _username): + await session.execute(text("SELECT 1 / 0")) + + app = create_app(make_settings()) + monkeypatch.setattr( + users_domain.users_storage, "get_user_by_username", broken_lookup + ) + async with make_client(app, raise_app_exceptions=False) as client: + response = await client.post( + "/api/v1/users", + json={"username": "broken_user", "display_name": "B", "password": PASSWORD}, + ) + await close_app(app) + + assert response.status_code == 500 + assert response.json()["error"]["code"] == "internal_error" + assert "division" not in response.text diff --git a/student-tests/test_health.py b/student-tests/test_health.py new file mode 100644 index 00000000..26d15896 --- /dev/null +++ b/student-tests/test_health.py @@ -0,0 +1,62 @@ +from httpx import AsyncClient + +from app.main import create_app +from conftest import close_app, make_client +from helpers import assert_request_id + + +async def test_liveness(client: AsyncClient): + response = await client.get("/health/live") + + assert response.status_code == 200 + assert response.json() == {"status": "ok"} + assert_request_id(response) + + +async def test_readiness(client: AsyncClient): + response = await client.get("/health/ready") + + assert response.status_code == 200 + assert response.json() == {"status": "ok"} + assert_request_id(response) + + +async def test_readiness_is_503_when_database_is_down(make_settings): + app = create_app( + make_settings( + database_url="postgresql+asyncpg://nobody:nopass@127.0.0.1:1/none", + db_connect_timeout_seconds=1, + ) + ) + async with make_client(app) as client: + assert (await client.get("/health/live")).status_code == 200 + + ready = await client.get("/health/ready") + assert ready.status_code == 503 + assert ready.json()["error"]["code"] == "unavailable" + assert_request_id(ready) + await close_app(app) + + +async def test_business_request_is_503_when_database_is_down(make_settings): + app = create_app( + make_settings( + database_url="postgresql+asyncpg://nobody:secretpass@127.0.0.1:1/none", + db_connect_timeout_seconds=1, + ) + ) + async with make_client(app, raise_app_exceptions=False) as client: + response = await client.post( + "/api/v1/users", + json={ + "username": "alice", + "display_name": "Alice", + "password": "Correct-horse-battery-1", + }, + ) + await close_app(app) + + assert response.status_code == 503 + assert response.json()["error"]["code"] == "unavailable" + assert "secretpass" not in response.text + assert_request_id(response) diff --git a/student-tests/test_history_plan.py b/student-tests/test_history_plan.py new file mode 100644 index 00000000..704c9ccd --- /dev/null +++ b/student-tests/test_history_plan.py @@ -0,0 +1,78 @@ +"""План запроса истории на >= 10 000 сообщений: индекс без Sort и полного скана.""" + +import json +from datetime import datetime +from uuid import UUID + +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncEngine + +from helpers import bearer, create_chat, create_user + +HISTORY_QUERY = """ +SELECT * FROM messages +WHERE chat_id = :chat_id AND (created_at, id) > (:after_created_at, :after_id) +ORDER BY created_at, id +LIMIT 51 +""" + + +def walk(node: dict) -> list[dict]: + nodes = [node] + for child in node.get("Plans", []): + nodes.extend(walk(child)) + return nodes + + +async def seed_messages( + engine: AsyncEngine, chat_id: str, sender_id: str, count: int +) -> None: + async with engine.begin() as connection: + await connection.execute( + text( + """ + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + SELECT gen_random_uuid(), :chat_id, :sender_id, 'message ' || g, + now() - make_interval(secs => :count - g) + FROM generate_series(1, :count) AS g + """ + ), + {"chat_id": chat_id, "sender_id": sender_id, "count": count}, + ) + await connection.execute(text("ANALYZE messages")) + + +async def test_history_page_uses_composite_index(client, db_engine: AsyncEngine): + alice = await create_user(client) + target = await create_chat(client, alice, title="target") + noise = await create_chat(client, alice, title="noise") + await seed_messages(db_engine, target["id"], alice["id"], 10_000) + await seed_messages(db_engine, noise["id"], alice["id"], 10_000) + + middle = ( + await client.get( + f"/api/v1/chats/{target['id']}/messages?limit=100", headers=bearer(alice) + ) + ).json()["items"][-1] + + async with db_engine.connect() as connection: + raw_plan = ( + await connection.execute( + text(f"EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) {HISTORY_QUERY}"), + { + "chat_id": UUID(target["id"]), + "after_created_at": datetime.fromisoformat(middle["created_at"]), + "after_id": UUID(middle["id"]), + }, + ) + ).scalar_one() + + plan = (raw_plan if isinstance(raw_plan, list) else json.loads(raw_plan))[0]["Plan"] + nodes = walk(plan) + node_types = {node["Node Type"] for node in nodes} + index_names = {node.get("Index Name") for node in nodes} + + assert "ix_messages_chat_id_created_at_id" in index_names + assert "Sort" not in node_types + assert "Seq Scan" not in node_types + assert all(node.get("Actual Rows", 0) <= 51 for node in nodes) diff --git a/student-tests/test_limits.py b/student-tests/test_limits.py new file mode 100644 index 00000000..86d53ed6 --- /dev/null +++ b/student-tests/test_limits.py @@ -0,0 +1,98 @@ +"""Ограничения нагрузки: MAX_IN_FLIGHT -> 429, MAX_MESSAGES -> 503.""" + +import asyncio + +import httpx + +from app.main import create_app +from conftest import close_app, make_client +from helpers import assert_request_id, bearer, create_chat, create_user + + +def _assert_error(response: httpx.Response, status: int, code: str) -> None: + assert response.status_code == status + body = response.json() + assert body["error"]["code"] == code + assert response.headers.get("X-Request-Id") == body["error"]["request_id"] + + +async def test_message_limit_rejects_without_deleting_confirmed_messages(make_settings): + app = create_app(make_settings(max_messages=2)) + async with make_client(app) as client: + user = await create_user(client) + chat = await create_chat(client, user) + url = f"/api/v1/chats/{chat['id']}/messages" + + for index in range(2): + ok = await client.post( + url, headers=bearer(user), json={"text": f"confirmed-{index}"} + ) + assert ok.status_code == 201 + + rejected = await client.post( + url, headers=bearer(user), json={"text": "rejected"} + ) + _assert_error(rejected, 503, "unavailable") + + history = await client.get(f"{url}?limit=100", headers=bearer(user)) + assert history.status_code == 200 + assert [m["text"] for m in history.json()["items"]] == [ + "confirmed-0", + "confirmed-1", + ] + await close_app(app) + + +async def test_in_flight_limit_returns_429_and_recovers(make_settings, monkeypatch): + import app.endpoints.messages as message_endpoint + + app = create_app(make_settings(max_in_flight=2)) + + original_send_message = message_endpoint.send_message + accepted_count = 0 + all_slots_taken = asyncio.Event() + release = asyncio.Event() + + async def blocking_send_message(*args, **kwargs): + nonlocal accepted_count + accepted_count += 1 + if accepted_count == 2: + all_slots_taken.set() + await asyncio.wait_for(release.wait(), timeout=5) + return await original_send_message(*args, **kwargs) + + async with make_client(app, timeout=10) as client: + user = await create_user(client) + chat = await create_chat(client, user) + + monkeypatch.setattr(message_endpoint, "send_message", blocking_send_message) + url = f"/api/v1/chats/{chat['id']}/messages" + + held = [ + asyncio.create_task( + client.post(url, headers=bearer(user), json={"text": f"held-{i}"}) + ) + for i in range(2) + ] + + await asyncio.wait_for(all_slots_taken.wait(), timeout=2) + + rejected = await asyncio.gather( + *( + client.post(url, headers=bearer(user), json={"text": f"rejected-{i}"}) + for i in range(4) + ) + ) + for response in rejected: + _assert_error(response, 429, "rate_limited") + assert response.headers.get("Retry-After") == "1" + + assert (await client.get("/health/live")).status_code == 200 + + release.set() + assert all(r.status_code == 201 for r in await asyncio.gather(*held)) + + recovered = await client.post(url, headers=bearer(user), json={"text": "after"}) + assert recovered.status_code == 201 + assert_request_id(recovered) + await close_app(app) diff --git a/student-tests/test_messages.py b/student-tests/test_messages.py new file mode 100644 index 00000000..2a47923e --- /dev/null +++ b/student-tests/test_messages.py @@ -0,0 +1,237 @@ +from urllib.parse import quote + +import pytest +from httpx import AsyncClient + +from helpers import ( + assert_request_id, + bearer, + create_chat, + create_user, + send_message, +) + + +async def _message(client: AsyncClient, user: dict, chat: dict, text: str): + return await send_message(client, user, chat, text) + + +async def test_send_and_list_message(client: AsyncClient): + alice = await create_user(client) + bob = await create_user(client) + chat = await create_chat(client, alice, bob) + + created = await _message(client, alice, chat, "Привет, мир 👋") + + assert created.status_code == 201 + message = created.json() + assert message["sender_id"] == alice["id"] + assert message["chat_id"] == chat["id"] + assert_request_id(created) + + listed = await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=1", + headers=bearer(bob), + ) + + assert listed.status_code == 200 + assert listed.json()["items"] == [message] + assert listed.json()["next_cursor"] is None + assert_request_id(listed) + + +@pytest.mark.parametrize("text", ["", " ", "x" * 4001]) +async def test_invalid_message_text(client: AsyncClient, text: str): + alice = await create_user(client) + chat = await create_chat(client, alice) + + response = await _message(client, alice, chat, text) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + assert_request_id(response) + + +async def test_extra_message_field_is_rejected(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + + response = await client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers=bearer(alice), + json={"text": "hello", "sender_id": alice["id"]}, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + assert_request_id(response) + + +async def test_messages_are_private_to_chat_members(client: AsyncClient): + alice = await create_user(client) + eve = await create_user(client) + chat = await create_chat(client, alice) + await _message(client, alice, chat, "секрет") + + response = await client.get( + f"/api/v1/chats/{chat['id']}/messages", + headers=bearer(eve), + ) + + assert response.status_code == 404 + assert response.json()["error"]["code"] == "not_found" + + +async def test_pagination_has_no_duplicates(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + + created = [ + (await _message(client, alice, chat, f"message-{index}")).json() + for index in range(5) + ] + + page1 = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=2", + headers=bearer(alice), + ) + ).json() + page2 = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=2&cursor={quote(page1['next_cursor'], safe='')}", + headers=bearer(alice), + ) + ).json() + page3 = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=2&cursor={quote(page2['next_cursor'], safe='')}", + headers=bearer(alice), + ) + ).json() + + ids = [item["id"] for page in (page1, page2, page3) for item in page["items"]] + assert ids == [item["id"] for item in created] + assert len(ids) == len(set(ids)) == 5 + assert len(page1["items"]) == 2 + assert len(page2["items"]) == 2 + assert len(page3["items"]) == 1 + assert page3["next_cursor"] is None + + +async def test_cursor_cannot_be_used_for_another_chat(client: AsyncClient): + alice = await create_user(client) + chat_a = await create_chat(client, alice, title="A") + chat_b = await create_chat(client, alice, title="B") + + await _message(client, alice, chat_a, "a1") + await _message(client, alice, chat_a, "a2") + await _message(client, alice, chat_b, "b1") + await _message(client, alice, chat_b, "b2") + + page = ( + await client.get( + f"/api/v1/chats/{chat_a['id']}/messages?limit=1", + headers=bearer(alice), + ) + ).json() + + response = await client.get( + f"/api/v1/chats/{chat_b['id']}/messages?limit=1&cursor={quote(page['next_cursor'], safe='')}", + headers=bearer(alice), + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + + +@pytest.mark.parametrize( + "limit, expected_status", [(0, 400), (1, 200), (100, 200), (101, 400)] +) +async def test_limit_boundaries(client: AsyncClient, limit: int, expected_status: int): + alice = await create_user(client) + chat = await create_chat(client, alice) + await _message(client, alice, chat, "one") + + response = await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit={limit}", + headers=bearer(alice), + ) + + assert response.status_code == expected_status + assert_request_id(response) + + +async def test_message_exact_max_length_is_accepted(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + + response = await _message(client, alice, chat, "x" * 4000) + + assert response.status_code == 201 + assert len(response.json()["text"]) == 4000 + + +async def test_nonexistent_chat_messages_return_404(client: AsyncClient): + alice = await create_user(client) + + response = await client.get( + "/api/v1/chats/00000000-0000-4000-8000-000000000000/messages", + headers=bearer(alice), + ) + + assert response.status_code == 404 + assert response.json()["error"]["code"] == "not_found" + assert_request_id(response) + + +async def test_unauthenticated_requests_are_rejected(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + url = f"/api/v1/chats/{chat['id']}/messages" + + assert (await client.get(url)).status_code == 401 + assert (await client.post(url, json={"text": "hi"})).status_code == 401 + + +async def test_messages_survive_new_login(client: AsyncClient): + """Сообщения принадлежат пользователю, а не сессии: после logout/login видны.""" + from helpers import login + + alice = await create_user(client) + chat = await create_chat(client, alice) + message = (await _message(client, alice, chat, "до выхода")).json() + await client.delete("/api/v1/auth/sessions/current", headers=bearer(alice)) + + alice_again = await login(client, alice) + listed = await client.get( + f"/api/v1/chats/{chat['id']}/messages", headers=bearer(alice_again) + ) + + assert listed.json()["items"] == [message] + + +async def test_history_order_is_created_at_then_id(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + created = [(await _message(client, alice, chat, f"m{i}")).json() for i in range(10)] + + listed = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=100", headers=bearer(alice) + ) + ).json()["items"] + + assert [m["id"] for m in listed] == [m["id"] for m in created] + keys = [(m["created_at"], m["id"]) for m in listed] + assert keys == sorted(keys) + + +async def test_nul_in_message_text_is_rejected(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + + response = await _message(client, alice, chat, "hello\u0000world") + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" diff --git a/student-tests/test_migrations.py b/student-tests/test_migrations.py new file mode 100644 index 00000000..b41c8830 --- /dev/null +++ b/student-tests/test_migrations.py @@ -0,0 +1,67 @@ +"""Миграции: применение с нуля, идемпотентный повтор, совпадение со схемой моделей.""" + +import pytest +from alembic.autogenerate import compare_metadata +from alembic.migration import MigrationContext +from sqlalchemy import inspect, text + +from app.models import Base +from conftest import reset_schema, upgrade_head + +EXPECTED_TABLES = {"users", "chats", "chat_members", "messages", "counters"} + + +@pytest.fixture +async def migrated_engine(database_url, db_engine): + await reset_schema(db_engine) + await upgrade_head(database_url) + yield db_engine + + await reset_schema(db_engine) + async with db_engine.begin() as connection: + await connection.run_sync(Base.metadata.create_all) + + +async def test_upgrade_creates_all_tables(migrated_engine): + async with migrated_engine.connect() as connection: + tables = await connection.run_sync( + lambda sync: set(inspect(sync).get_table_names()) + ) + assert EXPECTED_TABLES <= tables + + +async def test_upgrade_twice_is_idempotent_and_keeps_data( + migrated_engine, database_url +): + async with migrated_engine.begin() as connection: + await connection.execute( + text( + "INSERT INTO users (id, username, display_name, password_hash) " + "VALUES (gen_random_uuid(), 'keep_me', 'Keep', 'hash')" + ) + ) + + await upgrade_head(database_url) + + async with migrated_engine.connect() as connection: + count = ( + await connection.execute( + text("SELECT count(*) FROM users WHERE username = 'keep_me'") + ) + ).scalar_one() + assert count == 1 + + +async def test_models_and_migrations_are_in_sync(migrated_engine): + def compare(sync_connection): + context = MigrationContext.configure( + sync_connection, opts={"compare_type": True} + ) + return compare_metadata(context, Base.metadata) + + async with migrated_engine.connect() as connection: + diff = await connection.run_sync(compare) + + assert diff == [], ( + f"Создайте миграцию: alembic revision --autogenerate. Diff: {diff}" + ) diff --git a/student-tests/test_pagination.py b/student-tests/test_pagination.py new file mode 100644 index 00000000..e45e5b1b --- /dev/null +++ b/student-tests/test_pagination.py @@ -0,0 +1,137 @@ +"""Курсоры: непрозрачность, привязка к ресурсу и устойчивость к подделке.""" + +import base64 +import json +from urllib.parse import quote + +import pytest +from httpx import AsyncClient + +from helpers import bearer, create_chat, create_user, send_message + + +def forged_cursor(**payload: str) -> str: + return base64.urlsafe_b64encode(json.dumps(payload).encode()).decode() + + +@pytest.mark.parametrize( + "cursor", + [ + "not-base64-%%%", + base64.urlsafe_b64encode(b"not json").decode(), + base64.urlsafe_b64encode(b"[1, 2, 3]").decode(), + forged_cursor(kind="messages"), + ], +) +async def test_garbage_cursor_returns_400(client: AsyncClient, cursor: str): + alice = await create_user(client) + chat = await create_chat(client, alice) + + for url in ( + f"/api/v1/chats?cursor={quote(cursor, safe='')}", + f"/api/v1/chats/{chat['id']}/messages?cursor={quote(cursor, safe='')}", + ): + response = await client.get(url, headers=bearer(alice)) + assert response.status_code == 400, url + assert response.json()["error"]["code"] == "invalid_request" + + +async def test_cursor_without_timezone_is_rejected(client: AsyncClient): + alice = await create_user(client) + cursor = forged_cursor( + kind="chats", + resource_id=alice["id"], + created_at="2026-01-01T00:00:00", + item_id="00000000-0000-4000-8000-000000000000", + ) + + response = await client.get( + f"/api/v1/chats?cursor={quote(cursor, safe='')}", headers=bearer(alice) + ) + + assert response.status_code == 400 + + +async def test_chat_list_cursor_is_bound_to_its_owner(client: AsyncClient): + alice = await create_user(client) + bob = await create_user(client) + for index in range(2): + await create_chat(client, alice, bob, title=f"shared-{index}") + + page = (await client.get("/api/v1/chats?limit=1", headers=bearer(alice))).json() + response = await client.get( + f"/api/v1/chats?limit=1&cursor={quote(page['next_cursor'], safe='')}", + headers=bearer(bob), + ) + + assert response.status_code == 400 + + +async def test_messages_cursor_is_not_accepted_by_chat_list(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + for index in range(2): + await send_message(client, alice, chat, f"m{index}") + + page = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=1", headers=bearer(alice) + ) + ).json() + response = await client.get( + f"/api/v1/chats?cursor={quote(page['next_cursor'], safe='')}", + headers=bearer(alice), + ) + + assert response.status_code == 400 + + +async def test_limit_can_change_between_pages(client: AsyncClient): + alice = await create_user(client) + chat = await create_chat(client, alice) + sent = [ + (await send_message(client, alice, chat, f"m{index}")).json() + for index in range(6) + ] + + first = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=1", headers=bearer(alice) + ) + ).json() + rest = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=100" + f"&cursor={quote(first['next_cursor'], safe='')}", + headers=bearer(alice), + ) + ).json() + + assert [m["id"] for m in first["items"] + rest["items"]] == [m["id"] for m in sent] + assert rest["next_cursor"] is None + + +async def test_message_sent_after_reading_last_page_is_seen_with_cursor( + client: AsyncClient, +): + alice = await create_user(client) + chat = await create_chat(client, alice) + await send_message(client, alice, chat, "first") + await send_message(client, alice, chat, "second") + + page = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=1", headers=bearer(alice) + ) + ).json() + new = (await send_message(client, alice, chat, "third")).json() + rest = ( + await client.get( + f"/api/v1/chats/{chat['id']}/messages?limit=100" + f"&cursor={quote(page['next_cursor'], safe='')}", + headers=bearer(alice), + ) + ).json() + + assert [m["text"] for m in rest["items"]] == ["second", "third"] + assert rest["items"][-1] == new diff --git a/student-tests/test_sessions.py b/student-tests/test_sessions.py new file mode 100644 index 00000000..e239e227 --- /dev/null +++ b/student-tests/test_sessions.py @@ -0,0 +1,173 @@ +"""Сессии в Redis: TTL, абсолютный срок, отзыв, потеря и перезапуск хранилища.""" + +import asyncio +import hashlib +import os +import time +from datetime import datetime + +import pytest +from httpx import AsyncClient +from redis.asyncio import Redis + +from app.main import create_app +from conftest import close_app, make_client +from helpers import basic_header, bearer, create_user, register + +ME = "/api/v1/users/me" +LOGOUT = "/api/v1/auth/sessions/current" + + +def session_key(token: str) -> str: + return "session:" + hashlib.sha256(token.encode()).hexdigest() + + +async def test_session_key_ttl_matches_expires_at( + client: AsyncClient, redis_client: Redis +): + user = await register(client) + response = await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], user["password"]), + ) + body = response.json() + + expires_at = datetime.fromisoformat(body["expires_at"]) + expire_time_ms = await redis_client.pexpiretime(session_key(body["token"])) + + assert abs(expire_time_ms - expires_at.timestamp() * 1000) < 2 + assert 299_000 < await redis_client.pttl(session_key(body["token"])) <= 300_000 + + +async def test_session_ttl_is_absolute_and_not_extended_by_activity( + client: AsyncClient, redis_client: Redis +): + session = await create_user(client) + key = session_key(session["token"]) + expire_before = await redis_client.pexpiretime(key) + + for _ in range(5): + assert (await client.get(ME, headers=bearer(session))).status_code == 200 + + assert await redis_client.pexpiretime(key) == expire_before + + +async def test_logout_removes_session_key(client: AsyncClient, redis_client: Redis): + session = await create_user(client) + key = session_key(session["token"]) + assert await redis_client.exists(key) == 1 + + assert (await client.delete(LOGOUT, headers=bearer(session))).status_code == 204 + + assert await redis_client.exists(key) == 0 + assert (await client.get(ME, headers=bearer(session))).status_code == 401 + + +async def test_expired_session_is_rejected_everywhere(make_settings): + app = create_app(make_settings(session_ttl_seconds=1)) + async with make_client(app) as client: + session = await create_user(client) + assert (await client.get(ME, headers=bearer(session))).status_code == 200 + + await asyncio.sleep(1.5) + + for method, url in (("GET", ME), ("GET", "/api/v1/chats"), ("DELETE", LOGOUT)): + response = await client.request(method, url, headers=bearer(session)) + assert response.status_code == 401, (method, url) + assert response.headers["WWW-Authenticate"].startswith("Bearer") + await close_app(app) + + +async def test_relogin_after_expiry_issues_new_working_token(make_settings): + app = create_app(make_settings(session_ttl_seconds=1)) + async with make_client(app) as client: + user = await register(client) + first = ( + await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], user["password"]), + ) + ).json() + await asyncio.sleep(1.5) + second = ( + await client.post( + "/api/v1/auth/sessions", + headers=basic_header(user["username"], user["password"]), + ) + ).json() + + assert first["token"] != second["token"] + assert (await client.get(ME, headers=bearer(first))).status_code == 401 + assert (await client.get(ME, headers=bearer(second))).status_code == 200 + await close_app(app) + + +async def test_tampered_token_is_rejected(client: AsyncClient): + session = await create_user(client) + token = session["token"] + tampered = token[:-1] + ("A" if token[-1] != "A" else "B") + + response = await client.get(ME, headers={"Authorization": f"Bearer {tampered}"}) + + assert response.status_code == 401 + + +async def test_session_of_deleted_user_is_rejected(client: AsyncClient, db_engine): + from sqlalchemy import text + + session = await create_user(client) + async with db_engine.begin() as connection: + await connection.execute( + text("DELETE FROM users WHERE id = :id"), {"id": session["id"]} + ) + + response = await client.get(ME, headers=bearer(session)) + + assert response.status_code == 401 + + +async def test_redis_flush_invalidates_all_sessions( + client: AsyncClient, redis_client: Redis +): + alice = await create_user(client) + bob = await create_user(client) + + await redis_client.flushdb() + + for session in (alice, bob): + assert (await client.get(ME, headers=bearer(session))).status_code == 401 + + +@pytest.mark.skipif( + os.environ.get("DOCKER_TESTS") != "1", + reason="перезапускает контейнер redis из compose; включается DOCKER_TESTS=1", +) +async def test_sessions_survive_redis_restart(client: AsyncClient, redis_client: Redis): + active = await create_user(client) + revoked = await create_user(client) + assert (await client.delete(LOGOUT, headers=bearer(revoked))).status_code == 204 + + process = await asyncio.create_subprocess_exec( + "docker", + "compose", + "restart", + "redis", + stdout=asyncio.subprocess.DEVNULL, + stderr=asyncio.subprocess.DEVNULL, + ) + assert await process.wait() == 0 + + deadline = time.monotonic() + 30 + while True: + try: + await redis_client.ping() + break + except Exception: + if time.monotonic() > deadline: + raise + await asyncio.sleep(0.5) + + response = await client.get(ME, headers=bearer(active)) + assert response.status_code == 200 + assert response.json()["id"] == active["id"] + assert (await client.get(ME, headers=bearer(revoked))).status_code == 401 diff --git a/student-tests/test_users.py b/student-tests/test_users.py new file mode 100644 index 00000000..554cf30d --- /dev/null +++ b/student-tests/test_users.py @@ -0,0 +1,128 @@ +import pytest +from httpx import AsyncClient + +from helpers import PASSWORD, assert_request_id, bearer, create_user, register + + +async def test_register_does_not_leak_password(client: AsyncClient): + response = await client.post( + "/api/v1/users", + json={"username": "alice", "display_name": "Алиса 👋", "password": PASSWORD}, + ) + + assert response.status_code == 201 + body = response.json() + assert set(body) == {"id", "username", "display_name", "created_at"} + assert PASSWORD not in response.text + assert_request_id(response) + + +async def test_get_current_user(client: AsyncClient): + user = await create_user(client, display_name="Алиса 👋") + + response = await client.get("/api/v1/users/me", headers=bearer(user)) + + assert response.status_code == 200 + assert response.json()["id"] == user["id"] + assert response.json()["username"] == user["username"] + assert_request_id(response) + + +@pytest.mark.parametrize( + "body", + [ + {"username": "alice", "display_name": "", "password": PASSWORD}, + {"username": "alice", "display_name": " ", "password": PASSWORD}, + {"username": "alice", "display_name": "a" * 101, "password": PASSWORD}, + {"username": "ab", "display_name": "Alice", "password": PASSWORD}, + {"username": "alice-123", "display_name": "Alice", "password": PASSWORD}, + {"username": "AlicE", "display_name": "Alice", "password": PASSWORD}, + {"username": "a" * 33, "display_name": "Alice", "password": PASSWORD}, + {"username": "alice", "display_name": "Alice"}, + {"username": "alice", "display_name": "Alice", "password": "x" * 11}, + {"username": "alice", "display_name": "Alice", "password": "x" * 129}, + ], +) +async def test_invalid_user_payloads(client: AsyncClient, body: dict): + response = await client.post("/api/v1/users", json=body) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + assert_request_id(response) + + +async def test_password_length_boundaries_are_accepted(client: AsyncClient): + for index, password in enumerate( + ["x" * 12, "y" * 128, "п" * 12, " spaces inside "] + ): + response = await client.post( + "/api/v1/users", + json={ + "username": f"pw_user_{index}", + "display_name": "Alice", + "password": password, + }, + ) + assert response.status_code == 201, (index, response.text) + + +async def test_extra_user_field_is_rejected(client: AsyncClient): + response = await client.post( + "/api/v1/users", + json={ + "username": "alice", + "display_name": "Alice", + "password": PASSWORD, + "extra": True, + }, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + + +async def test_duplicate_username_returns_conflict(client: AsyncClient): + user = await register(client, username="duplicate_user") + + response = await client.post( + "/api/v1/users", + json={ + "username": user["username"], + "display_name": "Другой", + "password": PASSWORD, + }, + ) + + assert response.status_code == 409 + assert response.json()["error"]["code"] == "conflict" + assert_request_id(response) + + +async def test_user_exact_length_boundaries_are_accepted(client: AsyncClient): + for length in (3, 32): + user = await register(client, username=("a" * length), display_name="d" * 100) + assert len(user["username"]) == length + assert len(user["display_name"]) == 100 + + +@pytest.mark.parametrize("password", [" " * 12, "\t" * 12, " \n" * 6]) +async def test_whitespace_only_password_is_rejected(client: AsyncClient, password: str): + response = await client.post( + "/api/v1/users", + json={"username": "blank_pw", "display_name": "Blank", "password": password}, + ) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request" + assert_request_id(response) + + +@pytest.mark.parametrize("field", ["display_name", "password"]) +async def test_nul_in_user_fields_is_rejected(client: AsyncClient, field: str): + body = {"username": "nul_user", "display_name": "Nul", "password": PASSWORD} + body[field] = body[field] + "\u0000tail" + + response = await client.post("/api/v1/users", json=body) + + assert response.status_code == 400 + assert response.json()["error"]["code"] == "invalid_request"