diff --git a/.env.example b/.env.example index 714c8076..89eba8ec 100644 --- a/.env.example +++ b/.env.example @@ -1,11 +1,35 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. 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 + +DB_POOL_SIZE=5 +DB_MAX_OVERFLOW=10 +DB_POOL_TIMEOUT_SECONDS=3 +DB_CONNECT_TIMEOUT_SECONDS=3 +DB_STATEMENT_TIMEOUT_MS=5000 + +API_HOST_PORT=8080 +DB_HOST_PORT=5432 +REDIS_HOST_PORT=6379 diff --git a/.gitignore b/.gitignore index b5a71896..6f50f78f 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ 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/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..0e140a1c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +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 + +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 9ecb223e..e450feb4 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,50 @@ -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 +COMPOSE = docker compose --env-file .env + +TEST_PROJECT ?= $(notdir $(CURDIR))-test +TEST_API_PORT ?= 18080 +TEST_DB_PORT ?= 15432 +TEST_REDIS_PORT ?= 16379 +TEST_BASE_URL = http://localhost:$(TEST_API_PORT) +TEST_ENV = COMPOSE_PROJECT_NAME=$(TEST_PROJECT) API_HOST_PORT=$(TEST_API_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 \ + up down restart ps logs migrate migration \ + test-stand-up test-stand-down test-stand-destroy test-stand-ps \ + test-stand-smoke test-stand-persistence db-up \ + test-own test-docker test-limits test-concurrency test-migrations test-all \ + explain evidence 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 (smoke по BASE_URL) | load | test-persistence ACTION_SCRIPT=scripts/restart.sh" + @echo "Основной: up | down | restart | ps | logs | migrate | migration m='описание'" + @echo "Тестовый: test-stand-up | test-stand-down | test-stand-destroy | test-stand-ps" + @echo " test-stand-smoke | test-stand-persistence" + @echo "Тесты: test-own | test-docker | test-limits | test-concurrency | test-migrations" + @echo "Всё: test-all (стенд + pytest + smoke + persistence на тестовом стенде)" + @echo "Демо: explain | expiry-demo | failure-demo (на тестовом стенде)" + @echo "Отчёт: evidence (перегенерировать evidence/ на тестовом стенде)" check: $(PYTHON) scripts/check_contract.py @@ -24,3 +61,83 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --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)) + +ps: + $(COMPOSE) ps + +logs: + $(COMPOSE) logs -f api + +migrate: + $(COMPOSE) run --rm --build migrate + +migration: + DB_HOST=localhost $(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' + +db-up: test-stand-up + +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-own: + $(PYTHON) -m pytest tests -q + +test-docker: + $(TEST_ENV) DOCKER_TESTS=1 $(PYTHON) -m pytest tests -q + +test-limits: + $(PYTHON) -m pytest tests/test_limits.py -q + +test-concurrency: + $(PYTHON) -m pytest tests/test_concurrency.py -q + +test-migrations: + $(PYTHON) -m pytest 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 + +evidence: test-stand-up + $(TEST_ENV) PYTHON=$(PYTHON) BASE_URL=$(TEST_BASE_URL) scripts/collect_evidence.sh + +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/REPORT.md b/REPORT.md index e42f2b05..1e2a2bfc 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,135 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Григорян Илья, БПИ2303 (ветка `solution/BPI2303-Grigoryan`) +- Целевая оценка: **4**, трек 5 не заявлен +- Commit/PR: commit, на котором собраны доказательства(e3b316352c4095dc9c4b31e935206d3e07de6a4a), записан в [evidence/00-environment.txt](evidence/00-environment.txt) +- Источники и библиотеки: FastAPI, SQLAlchemy 2.1 (async) + asyncpg, Alembic, redis-py, argon2-cffi, Pydantic / pydantic-settings, pytest + pytest-asyncio + httpx; документация PostgreSQL (transactions, EXPLAIN), Redis persistence, RFC 7617 +- Помощь ИИ: ассистент Claude (Claude Code) — ревью схемы БД, реализация и ревью слоёв storage/domain, тесты, скрипты сбора доказательств, черновик отчёта. Решения обсуждались и проверялись автором ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требования: Docker Engine с Compose v2 (проверено на Docker 29.9.2, Compose 5.6.1), `make`, `curl`, Python 3.12 — только для тестов. Образы закреплены: `python:3.12.15-slim-bookworm`, `postgres:16.10-bookworm`, `redis:7.4.2-bookworm`. + +```sh + +cp .env.example .env # заменить DB_PASSWORD и REDIS_PASSWORD + +make check + +docker compose --env-file .env config --quiet + +make up # сборка → postgres, redis healthy → migrate → api; ждёт /health/ready + +make test # публичный smoke на http://localhost:8080 + +make test-persistence ACTION_SCRIPT=scripts/restart.sh + +``` + +- Миграции: сервис `migrate` выполняет `alembic upgrade head` при каждом `up`, повтор не дублирует данные. Ручных действий, кроме паролей в `.env`, нет. +- Безопасная остановка без потери данных: `make down`. Намеренное удаление учебных данных: `docker compose down -v`. +- Собственные тесты и доказательства идут на **отдельном тестовом стенде** (проект compose `<каталог>-test`, порты 18080/15432/16379), основной стенд они не трогают: + +```sh + +python3 -m venv venv && venv/bin/pip install -r requirements.txt + +make test-all # тестовый стенд + pytest (включая рестарт Redis) + smoke + persistence + +make evidence # перегенерировать evidence/ + +make test-stand-down # остановить; make test-stand-destroy — удалить вместе с volumes + +``` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Клиент → API (FastAPI, один процесс uvicorn) → PostgreSQL (все данные) и Redis (сессии). + +| Роль | Service | Образ | Health / readiness | Volume | Порт хоста | Credentials | +| -------- | ---------- | ------------------------- | ---------------------------------------------------------------------------- | ----------- | ---------------- | ---------------------------------------------------------------------------------- | +| API | `api` | сборка из `Dockerfile` | `HEALTHCHECK` → `/health/live`; `/health/ready` проверяет PostgreSQL и Redis | — | `127.0.0.1:8080` | `env_file: .env` | +| Миграции | `migrate` | тот же образ | one-shot; `api` ждёт успешного завершения | — | — | `.env` | +| БД | `postgres` | `postgres:16.10-bookworm` | `pg_isready` | `pgdata` | `127.0.0.1:5432` | `POSTGRES_PASSWORD` из `.env` | +| Сессии | `redis` | `redis:7.4.2-bookworm` | `redis-cli ping` | `redisdata` | `127.0.0.1:6379` | `--requirepass` из `.env`; конфиг [infra/redis/redis.conf](infra/redis/redis.conf) | + +**Источник истины.** PostgreSQL — пользователи, чаты, участники, сообщения, счётчик сообщений. Redis — единственное хранилище сессий. + +**Схема** (миграции `db06fd3126ff`, `abd273076068`): UUID-ключи, внешние ключи, `UNIQUE(username)`, составной PK `chat_members(chat_id, user_id)`, индексы `messages(chat_id, created_at, id)` под keyset-историю и `chat_members(user_id, chat_id)` под список чатов пользователя, `timestamptz`. `created_at` сообщений берётся из `clock_timestamp()` в БД. Форматы и длины проверяются в одном месте — Pydantic-схемах; в БД остаются структурные ограничения. + +**Сессии.** + +- Ключ `session:` → `user_id`, запись `SET … PXAT expires_at`. Сам токен (256 бит, `secrets.token_urlsafe(32)`) не хранится. +- Expiry: срок абсолютный (`SESSION_TTL_SECONDS`), Redis удаляет ключ сам. Активность и рестарт срок не продлевают. +- Инвалидация: logout — атомарный `DEL`; повторный logout получает 401. +- Persistence: AOF с `appendfsync always`, чтобы не терять logout. `maxmemory-policy noeviction`, чтобы Redis не вытеснял сессии. Volume `redisdata`. После `FLUSHALL` все пользователи входят заново, данные PostgreSQL не затронуты. + +**Пароли.** Argon2id (`m=64 MiB, t=3, p=4`) в отдельном пуле потоков размером `cpu / p`. Для неизвестного username выполняется проверка против фиктивного хеша, поэтому время ответа не выдаёт существование пользователя. + + +**Транзакционные границы.** `commit` и `rollback` есть только в слое domain: + +- **регистрация:** проверка username → `commit` (соединение возвращается в пул до Argon2) → INSERT → `commit`. `IntegrityError` → rollback → 409; +- **вход:** SELECT → `commit` до проверки пароля → `SET` в Redis; +- **создание чата:** чат и участники вставляются одной транзакцией; +- **сообщение:** проверка членства → атомарный upsert счётчика с условием `value < MAX_MESSAGES` → INSERT → `commit`. Лимит точен и при конкурентных отправках. + + +**Пулы и таймауты.** + +- PostgreSQL: пул 5 + 10, ожидание соединения 3 с, подключение 3 с, `statement_timeout` 5 с. +- Redis: блокирующий пул на 20 соединений, ожидание 1 с, подключение 1 с, операция 0.5 с, один повтор. +- Лимиты: `MAX_IN_FLIGHT` → 429, `MAX_MESSAGES` → 503. SQL только через SQLAlchemy, все запросы параметризованы. + + +**Из lab1 перенесено:** API, формат ошибок, `X-Request-Id`, курсорная пагинация, лимиты. +**Изменено:** хранилище in-memory → PostgreSQL; `X-User-Id` → Basic (только `POST /auth/sessions`) + Bearer; регистрация с паролем. + +**Переменные `.env.example`:** + +- `APP_*`, `LOG_LEVEL` — uvicorn; +- `SESSION_TTL_SECONDS`, `SESSION_STORE=redis` — сессии; +- `REDIS_*` — `app/core/redis.py`; +- `DB_*` — `app/core/database.py`; +- `MAX_*` — лимиты; +- `*_HOST_PORT` — публикация портов в compose. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Критерий README | Команда / сценарий | Наблюдаемый результат | Файл | +| -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | --------------------------------------------------------- | +| Публичный smoke | `make test` | PASS | [02-smoke.txt](evidence/02-smoke.txt) | +| Данные и сессии переживают рестарт API и всех хранилищ | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; `Up N seconds` у api, postgres, redis до и после | [03-persistence.txt](evidence/03-persistence.txt) | +| PK/FK, уникальность username, миграции с нуля и повторно | `test_constraints.py`, `test_migrations.py` | ограничения срабатывают в БД; повторный `upgrade` не дублирует; модели совпадают с миграциями | [01-pytest.txt](evidence/01-pytest.txt) | +| Атомарное создание чата | `test_failed_chat_creation_leaves_no_partial_data`, `test_failed_chat_insert_rolls_back_whole_transaction` | 400, ни чата, ни участников | 01-pytest.txt | +| Basic только на входе, Bearer везде, `X-User-Id` игнорируется | `test_auth.py`, smoke | 401 + `WWW-Authenticate`; `X-User-Id` не меняет пользователя | 01-pytest.txt | +| Срок жизни и отзыв сессии | `make expiry-demo`, `test_sessions.py` | 200 → 401 после TTL; logout → 401 | [08-expiry.txt](evidence/08-expiry.txt) | +| Пароли — KDF | `test_password_is_not_stored_in_plaintext` | `$argon2id$…` в БД | 01-pytest.txt | +| Redis — сессии с TTL и persistence | `make evidence` | `appendfsync always`, `noeviction`, `aof_enabled:1`; TTL ключа = `expires_at` | [06-redis-config.txt](evidence/06-redis-config.txt) | +| Пул, таймауты, параметризованные запросы | код `app/core/database.py`, `app/core/redis.py` | пулы ограничены, отказ Redis отвечает за ≈1 с | [05-failures.txt](evidence/05-failures.txt) | +| Гонка регистрации и rollback составной операции | `test_concurrency.py` | 16 одновременных → 1×201 + 15×409; откат при сбое вставки | 01-pytest.txt | +| Индекс истории + EXPLAIN на ≥10 000 | `make explain`, `test_history_plan.py` | Index Scan, 51 строка, 4–6 буферов; без индекса — Seq Scan + Sort, 618 буферов | [04-explain-history.txt](evidence/04-explain-history.txt) | +| Автопроверки expiry, logout, неверного пароля, отказа Redis/БД | `make test-docker` | 112 passed | 01-pytest.txt, 05-failures.txt | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Гипотеза: при потере Redis или PostgreSQL API отвечает ограниченным 503 и не пропускает анонимов, а после возврата сервиса прежний токен снова работает. +Стенд: i7-10510U (8 потоков), 15 GiB RAM, без CPU/RAM-лимитов на контейнерах, один процесс API — см. [00-environment.txt](evidence/00-environment.txt). -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Воздействие | Что ожидаем | Что наблюдали | Восстановление / потеря данных | +| --------------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | +| Остановка Redis | 503 на вход и Bearer-запросы, ready 503, live 200 | 503 за ≈1 с; без токена 401; live 200 | после старта тот же токен → 200 (AOF), потерь нет | +| Остановка PostgreSQL | 503 на бизнес-запросы, ready 503 | 503, детали подключения не раскрываются | после старта → 200, потерь нет | +| Рестарт api + postgres + redis | данные и токен сохраняются | PASS persistence | ≈15 с до готовности, потерь нет | +| `FLUSHDB` Redis | все сессии недействительны | 401 для всех токенов | пользователи входят заново, PostgreSQL не затронут | +| Нагрузка: closed-loop POST сообщений, 20 с, concurrency 8 | без ошибок | 172 RPS, 0 % ошибок, p50/p95/p99 = 36/112/172 мс; api ≈95 % CPU, postgres ≈18 % | [07-load.json](evidence/07-load.json), [07-load-resources.txt](evidence/07-load-resources.txt) | + +Ограничения измерения нагрузки: один короткий прогон, closed-loop клиент на той же машине (coordinated omission), latency только успешных запросов. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Узкое место — один процесс API (CPU). Горизонтальное масштабирование — lab3. +- Счётчик сообщений — одна строка, все отправки проходят через её блокировку до `commit`. При росте нагрузки — шардирование счётчика с менее строгим лимитом. +- Redis — единственное хранилище сессий: потеря volume или `FLUSHALL` разлогинивает всех. `appendfsync always` удорожает только вход и выход. +- Один Docker-host и одиночные PostgreSQL/Redis без репликации: отказ хоста не переживается. +- HTTP без TLS — только локальный стенд с синтетическими данными. Пароль Redis виден в `docker inspect`; управление секретами — lab7. \ No newline at end of file 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..60980fa9 --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,87 @@ +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) + + 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/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/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..d0e1a3f3 --- /dev/null +++ b/app/endpoints/health.py @@ -0,0 +1,22 @@ +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: + 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..afadef53 --- /dev/null +++ b/app/endpoints/messages.py @@ -0,0 +1,34 @@ +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, +) -> 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..9e53e44e --- /dev/null +++ b/app/errors/handlers.py @@ -0,0 +1,173 @@ +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 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__) + + +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"}, + ) + + +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(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..bce48d3f --- /dev/null +++ b/app/main.py @@ -0,0 +1,75 @@ +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) + + 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(), + timeout_graceful_shutdown=5, + ) + + +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/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.yaml b/compose.yaml new file mode 100644 index 00000000..e5bd4d72 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,65 @@ +services: + postgres: + image: postgres:16.10-bookworm + restart: unless-stopped + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_DB: ${DB_NAME} + POSTGRES_PASSWORD: ${DB_PASSWORD} + volumes: + - pgdata:/var/lib/postgresql/data + ports: + - "127.0.0.1:${DB_HOST_PORT:-5432}:5432" + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 5s + timeout: 3s + retries: 10 + + redis: + image: redis:7.4.2-bookworm + restart: unless-stopped + 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 + ports: + - "127.0.0.1:${REDIS_HOST_PORT:-6379}:6379" + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + + migrate: + build: . + image: mtusi-messenger:lab2 + command: ["alembic", "upgrade", "head"] + env_file: .env + depends_on: + postgres: + condition: service_healthy + restart: "no" + + api: + build: . + image: mtusi-messenger:lab2 + restart: unless-stopped + env_file: .env + environment: + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + ports: + - "127.0.0.1:${API_HOST_PORT:-8080}:${APP_PORT:-8080}" + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + migrate: + condition: service_completed_successfully + +volumes: + pgdata: + redisdata: \ No newline at end of file diff --git a/evidence/00-environment.txt b/evidence/00-environment.txt new file mode 100644 index 00000000..9e38ca74 --- /dev/null +++ b/evidence/00-environment.txt @@ -0,0 +1,15 @@ +date: 2026-10-28T17:22:39Z +commit: e3b3163 (+ uncommitted changes) +branch: solution/BPI2303-Grigoryan +cpu: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz (8 threads) +memory: 15Gi +kernel: 7.2.11-zen1-1-zen +docker: 29.9.2 +compose: 5.6.1 +python: Python 3.12.14 +stand: http://localhost:18080 (compose project mtusi2026lab2-fork-test) + +SERVICE IMAGE STATUS +api mtusi-messenger:lab2 Up 2 seconds (health: starting) +postgres postgres:16.10-bookworm Up 9 seconds (healthy) +redis redis:7.4.2-bookworm Up 9 seconds (healthy) diff --git a/evidence/01-pytest.txt b/evidence/01-pytest.txt new file mode 100644 index 00000000..e2690699 --- /dev/null +++ b/evidence/01-pytest.txt @@ -0,0 +1,122 @@ +tests/test_auth.py::test_login_returns_opaque_bearer_session PASSED [ 0%] +tests/test_auth.py::test_each_login_issues_a_different_token PASSED [ 1%] +tests/test_auth.py::test_password_with_colon_works PASSED [ 2%] +tests/test_auth.py::test_unicode_password_works PASSED [ 3%] +tests/test_auth.py::test_wrong_password_and_unknown_user_look_identical PASSED [ 4%] +tests/test_auth.py::test_login_requires_basic_scheme PASSED [ 4%] +tests/test_auth.py::test_business_endpoints_require_bearer PASSED [ 5%] +tests/test_auth.py::test_x_user_id_cannot_override_bearer_identity PASSED [ 6%] +tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 7%] +tests/test_auth.py::test_logout_twice_returns_401 PASSED [ 8%] +tests/test_auth.py::test_session_expires_after_ttl PASSED [ 9%] +tests/test_auth.py::test_token_is_stored_only_as_sha256_key PASSED [ 9%] +tests/test_auth.py::test_password_is_not_stored_in_plaintext PASSED [ 10%] +tests/test_auth.py::test_login_rejects_out_of_range_password_like_wrong_password PASSED [ 11%] +tests/test_auth.py::test_malformed_basic_credentials_return_401 PASSED [ 12%] +tests/test_auth.py::test_unknown_user_takes_comparable_time_to_wrong_password PASSED [ 13%] +tests/test_auth.py::test_invalid_basic_username_returns_401_not_500 PASSED [ 14%] +tests/test_auth.py::test_whitespace_only_password_at_login_returns_401 PASSED [ 14%] +tests/test_chats.py::test_create_and_get_chat PASSED [ 15%] +tests/test_chats.py::test_chat_is_not_visible_to_outsider PASSED [ 16%] +tests/test_chats.py::test_chat_creation_requires_creator_in_members PASSED [ 17%] +tests/test_chats.py::test_chat_payload_boundaries[payload0] PASSED [ 18%] +tests/test_chats.py::test_chat_payload_boundaries[payload1] PASSED [ 19%] +tests/test_chats.py::test_duplicate_members_are_rejected PASSED [ 19%] +tests/test_chats.py::test_unknown_member_is_rejected PASSED [ 20%] +tests/test_chats.py::test_chat_exact_title_limit_is_accepted PASSED [ 21%] +tests/test_chats.py::test_chat_list_hides_chats_without_membership PASSED [ 22%] +tests/test_chats.py::test_nonexistent_chat_returns_404 PASSED [ 23%] +tests/test_chats.py::test_failed_chat_creation_leaves_no_partial_data PASSED [ 23%] +tests/test_chats.py::test_chat_list_pagination_and_order PASSED [ 24%] +tests/test_chats.py::test_chat_endpoints_require_bearer PASSED [ 25%] +tests/test_chats.py::test_nul_in_chat_title_is_rejected PASSED [ 26%] +tests/test_concurrency.py::test_parallel_registration_of_same_username_creates_exactly_one_user PASSED [ 27%] +tests/test_concurrency.py::test_100_messages_from_10_clients PASSED [ 28%] +tests/test_concurrency.py::test_chat_creation_in_parallel_is_atomic PASSED [ 28%] +tests/test_concurrency.py::test_parallel_registration_of_distinct_usernames_all_succeed PASSED [ 29%] +tests/test_concurrency.py::test_parallel_logins_issue_distinct_valid_tokens PASSED [ 30%] +tests/test_concurrency.py::test_parallel_logout_of_same_token_succeeds_once PASSED [ 31%] +tests/test_concurrency.py::test_message_limit_is_exact_under_concurrency PASSED [ 32%] +tests/test_concurrency.py::test_failed_chat_insert_rolls_back_whole_transaction PASSED [ 33%] +tests/test_constraints.py::test_duplicate_username_is_rejected_by_database PASSED [ 33%] +tests/test_constraints.py::test_message_requires_existing_chat PASSED [ 34%] +tests/test_constraints.py::test_duplicate_membership_is_rejected_by_database PASSED [ 35%] +tests/test_errors.py::test_missing_token_returns_401 PASSED [ 36%] +tests/test_errors.py::test_garbage_token_returns_401 PASSED [ 37%] +tests/test_errors.py::test_malformed_json_returns_400 PASSED [ 38%] +tests/test_errors.py::test_not_found_returns_404 PASSED [ 38%] +tests/test_errors.py::test_method_not_allowed_preserves_405 PASSED [ 39%] +tests/test_errors.py::test_unhandled_exception_is_hidden_from_client PASSED [ 40%] +tests/test_errors.py::test_success_response_contains_request_id PASSED [ 41%] +tests/test_failures.py::test_redis_down_gives_503_for_login_and_protected_endpoints PASSED [ 42%] +tests/test_failures.py::test_redis_down_keeps_registration_working PASSED [ 42%] +tests/test_failures.py::test_database_down_with_valid_session_gives_503 PASSED [ 43%] +tests/test_failures.py::test_dependency_failure_never_authenticates_anonymous_requests PASSED [ 44%] +tests/test_health.py::test_liveness PASSED [ 45%] +tests/test_health.py::test_readiness PASSED [ 46%] +tests/test_health.py::test_readiness_is_503_when_database_is_down PASSED [ 47%] +tests/test_health.py::test_business_request_is_503_when_database_is_down PASSED [ 47%] +tests/test_history_plan.py::test_history_page_uses_composite_index PASSED [ 48%] +tests/test_limits.py::test_message_limit_rejects_without_deleting_confirmed_messages PASSED [ 49%] +tests/test_limits.py::test_in_flight_limit_returns_429_and_recovers PASSED [ 50%] +tests/test_messages.py::test_send_and_list_message PASSED [ 51%] +tests/test_messages.py::test_invalid_message_text[] PASSED [ 52%] +tests/test_messages.py::test_invalid_message_text[ ] PASSED [ 52%] +tests/test_messages.py::test_invalid_message_text[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 53%] +tests/test_messages.py::test_extra_message_field_is_rejected PASSED [ 54%] +tests/test_messages.py::test_messages_are_private_to_chat_members PASSED [ 55%] +tests/test_messages.py::test_pagination_has_no_duplicates PASSED [ 56%] +tests/test_messages.py::test_cursor_cannot_be_used_for_another_chat PASSED [ 57%] +tests/test_messages.py::test_limit_boundaries[0-400] PASSED [ 57%] +tests/test_messages.py::test_limit_boundaries[1-200] PASSED [ 58%] +tests/test_messages.py::test_limit_boundaries[100-200] PASSED [ 59%] +tests/test_messages.py::test_limit_boundaries[101-400] PASSED [ 60%] +tests/test_messages.py::test_message_exact_max_length_is_accepted PASSED [ 61%] +tests/test_messages.py::test_nonexistent_chat_messages_return_404 PASSED [ 61%] +tests/test_messages.py::test_unauthenticated_requests_are_rejected PASSED [ 62%] +tests/test_messages.py::test_messages_survive_new_login PASSED [ 63%] +tests/test_messages.py::test_history_order_is_created_at_then_id PASSED [ 64%] +tests/test_messages.py::test_nul_in_message_text_is_rejected PASSED [ 65%] +tests/test_migrations.py::test_upgrade_creates_all_tables PASSED [ 66%] +tests/test_migrations.py::test_upgrade_twice_is_idempotent_and_keeps_data PASSED [ 66%] +tests/test_migrations.py::test_models_and_migrations_are_in_sync PASSED [ 67%] +tests/test_pagination.py::test_garbage_cursor_returns_400[not-base64-%%%] PASSED [ 68%] +tests/test_pagination.py::test_garbage_cursor_returns_400[bm90IGpzb24=] PASSED [ 69%] +tests/test_pagination.py::test_garbage_cursor_returns_400[WzEsIDIsIDNd] PASSED [ 70%] +tests/test_pagination.py::test_garbage_cursor_returns_400[eyJraW5kIjogIm1lc3NhZ2VzIn0=] PASSED [ 71%] +tests/test_pagination.py::test_cursor_without_timezone_is_rejected PASSED [ 71%] +tests/test_pagination.py::test_chat_list_cursor_is_bound_to_its_owner PASSED [ 72%] +tests/test_pagination.py::test_messages_cursor_is_not_accepted_by_chat_list PASSED [ 73%] +tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 74%] +tests/test_pagination.py::test_message_sent_after_reading_last_page_is_seen_with_cursor PASSED [ 75%] +tests/test_sessions.py::test_session_key_ttl_matches_expires_at PASSED [ 76%] +tests/test_sessions.py::test_session_ttl_is_absolute_and_not_extended_by_activity PASSED [ 76%] +tests/test_sessions.py::test_logout_removes_session_key PASSED [ 77%] +tests/test_sessions.py::test_expired_session_is_rejected_everywhere PASSED [ 78%] +tests/test_sessions.py::test_relogin_after_expiry_issues_new_working_token PASSED [ 79%] +tests/test_sessions.py::test_tampered_token_is_rejected PASSED [ 80%] +tests/test_sessions.py::test_session_of_deleted_user_is_rejected PASSED [ 80%] +tests/test_sessions.py::test_redis_flush_invalidates_all_sessions PASSED [ 81%] +tests/test_sessions.py::test_sessions_survive_redis_restart PASSED [ 82%] +tests/test_users.py::test_register_does_not_leak_password PASSED [ 83%] +tests/test_users.py::test_get_current_user PASSED [ 84%] +tests/test_users.py::test_invalid_user_payloads[body0] PASSED [ 85%] +tests/test_users.py::test_invalid_user_payloads[body1] PASSED [ 85%] +tests/test_users.py::test_invalid_user_payloads[body2] PASSED [ 86%] +tests/test_users.py::test_invalid_user_payloads[body3] PASSED [ 87%] +tests/test_users.py::test_invalid_user_payloads[body4] PASSED [ 88%] +tests/test_users.py::test_invalid_user_payloads[body5] PASSED [ 89%] +tests/test_users.py::test_invalid_user_payloads[body6] PASSED [ 90%] +tests/test_users.py::test_invalid_user_payloads[body7] PASSED [ 90%] +tests/test_users.py::test_invalid_user_payloads[body8] PASSED [ 91%] +tests/test_users.py::test_invalid_user_payloads[body9] PASSED [ 92%] +tests/test_users.py::test_password_length_boundaries_are_accepted PASSED [ 93%] +tests/test_users.py::test_extra_user_field_is_rejected PASSED [ 94%] +tests/test_users.py::test_duplicate_username_returns_conflict PASSED [ 95%] +tests/test_users.py::test_user_exact_length_boundaries_are_accepted PASSED [ 95%] +tests/test_users.py::test_whitespace_only_password_is_rejected[ ] PASSED [ 96%] +tests/test_users.py::test_whitespace_only_password_is_rejected[\t\t\t\t\t\t\t\t\t\t\t\t] PASSED [ 97%] +tests/test_users.py::test_whitespace_only_password_is_rejected[ \n \n \n \n \n \n] PASSED [ 98%] +tests/test_users.py::test_nul_in_user_fields_is_rejected[display_name] PASSED [ 99%] +tests/test_users.py::test_nul_in_user_fields_is_rejected[password] PASSED [100%] +============================= 121 passed in 39.60s ============================= diff --git a/evidence/02-smoke.txt b/evidence/02-smoke.txt new file mode 100644 index 00000000..92154196 --- /dev/null +++ b/evidence/02-smoke.txt @@ -0,0 +1 @@ +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/03-persistence.txt b/evidence/03-persistence.txt new file mode 100644 index 00000000..00dc30fb --- /dev/null +++ b/evidence/03-persistence.txt @@ -0,0 +1,14 @@ +== before +SERVICE STATUS +api Up 44 seconds (healthy) +postgres Up 52 seconds (healthy) +redis Up 4 seconds (health: starting) + +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +== after (Up N seconds = container was restarted by scripts/restart.sh) +SERVICE STATUS +api Up 5 seconds (healthy) +postgres Up 12 seconds (healthy) +redis Up 15 seconds (healthy) diff --git a/evidence/04-explain-history.txt b/evidence/04-explain-history.txt new file mode 100644 index 00000000..d9c176c3 --- /dev/null +++ b/evidence/04-explain-history.txt @@ -0,0 +1,51 @@ +messages in table: 50003; target chat: 10000; noise chat: 40000 + +== first page (limit+1 = 51) +SELECT * FROM messages +WHERE chat_id = :chat_id +ORDER BY created_at, id +LIMIT :page + +Limit (actual time=0.006..0.014 rows=51 loops=1) + Buffers: shared hit=4 + -> Index Scan using ix_messages_chat_id_created_at_id on messages (actual time=0.005..0.011 rows=51 loops=1) + Index Cond: (chat_id = '5355155b-ec73-4a3a-ac44-2417871c9ddf'::uuid) + Buffers: shared hit=4 +Planning: + Buffers: shared hit=6 +Planning Time: 0.046 ms +Execution Time: 0.022 ms + +== page after cursor in the middle of history +SELECT * FROM messages +WHERE chat_id = :chat_id AND (created_at, id) > (:after_created_at, :after_id) +ORDER BY created_at, id +LIMIT :page + +Limit (actual time=0.006..0.018 rows=51 loops=1) + Buffers: shared hit=6 + -> Index Scan using ix_messages_chat_id_created_at_id on messages (actual time=0.006..0.015 rows=51 loops=1) + Index Cond: ((chat_id = '5355155b-ec73-4a3a-ac44-2417871c9ddf'::uuid) AND (ROW(created_at, id) > ROW('2026-10-28 16:00:20.004077+00'::timestamp with time zone, 'c9e332b5-5dd0-4279-a4dd-bf2e1451cc69'::uuid))) + Buffers: shared hit=6 +Planning Time: 0.037 ms +Execution Time: 0.040 ms + +== same page with index scans disabled (for comparison) +SELECT * FROM messages +WHERE chat_id = :chat_id AND (created_at, id) > (:after_created_at, :after_id) +ORDER BY created_at, id +LIMIT :page + +Limit (actual time=4.010..4.015 rows=51 loops=1) + Buffers: shared hit=618 + -> Sort (actual time=4.009..4.011 rows=51 loops=1) + Sort Key: created_at, id + Sort Method: top-N heapsort Memory: 31kB + Buffers: shared hit=618 + -> Seq Scan on messages (actual time=0.379..3.601 rows=4999 loops=1) + Filter: ((chat_id = '5355155b-ec73-4a3a-ac44-2417871c9ddf'::uuid) AND (ROW(created_at, id) > ROW('2026-10-28 16:00:20.004077+00'::timestamp with time zone, 'c9e332b5-5dd0-4279-a4dd-bf2e1451cc69'::uuid))) + Rows Removed by Filter: 45004 + Buffers: shared hit=618 +Planning Time: 0.036 ms +Execution Time: 4.024 ms + diff --git a/evidence/05-failures.txt b/evidence/05-failures.txt new file mode 100644 index 00000000..49c60a4b --- /dev/null +++ b/evidence/05-failures.txt @@ -0,0 +1,23 @@ +== baseline +GET /users/me (Bearer) -> 200 (0.003584s) + +== redis stopped +GET /users/me (Bearer) -> 503 (1.007046s) +POST /auth/sessions (Basic) -> 503 (0.659252s) +GET /users/me (no token) -> 401 (0.001931s) +GET /health/ready -> 503 (0.010165s) +GET /health/live -> 200 (0.001443s) +-- redis started again (AOF restored) +GET /users/me (same token) -> 200 (0.006832s) + +== postgres stopped +GET /users/me (Bearer) -> 503 (3.011346s) +GET /api/v1/chats (Bearer) -> 503 (3.006857s) +GET /health/ready -> 503 (1.005380s) +GET /health/live -> 200 (0.003165s) +-- postgres started again +GET /users/me (same token) -> 200 (0.016088s) + +== logout +DELETE /auth/sessions/current -> 204 (0.015784s) +GET /users/me (same token) -> 401 (0.002551s) diff --git a/evidence/06-redis-config.txt b/evidence/06-redis-config.txt new file mode 100644 index 00000000..d3d4d79d --- /dev/null +++ b/evidence/06-redis-config.txt @@ -0,0 +1,10 @@ +appendonly yes +appendfsync always +maxmemory 268435456 +maxmemory-policy noeviction +dir /data + +loading:0 +aof_enabled:1 +aof_last_bgrewrite_status:ok +aof_last_write_status:ok diff --git a/evidence/07-load-resources.txt b/evidence/07-load-resources.txt new file mode 100644 index 00000000..29dc801a --- /dev/null +++ b/evidence/07-load-resources.txt @@ -0,0 +1,4 @@ +NAME CPU % MEM USAGE / LIMIT +mtusi2026lab2-fork-test-api-1 95.15% 70.75MiB / 15.27GiB +mtusi2026lab2-fork-test-postgres-1 16.41% 39.45MiB / 15.27GiB +mtusi2026lab2-fork-test-redis-1 0.91% 8.484MiB / 15.27GiB diff --git a/evidence/07-load.json b/evidence/07-load.json new file mode 100644 index 00000000..c398f50d --- /dev/null +++ b/evidence/07-load.json @@ -0,0 +1,17 @@ +{ + "lab": 2, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.054, + "concurrency": 8, + "successful_requests": 3100, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 154.585, + "successful_latency_ms": { + "p50": 38.714, + "p95": 130.208, + "p99": 196.115 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/08-expiry.txt b/evidence/08-expiry.txt new file mode 100644 index 00000000..8d404aa1 --- /dev/null +++ b/evidence/08-expiry.txt @@ -0,0 +1,5 @@ +SESSION_TTL_SECONDS=3, login at 17:24:28Z, expires_at=2026-10-28T17:24:31.093992Z +GET /users/me right after login -> 200 +GET /users/me after 4 s -> 401 +DELETE /auth/sessions/current after -> 401 +GET /users/me with new login token -> 200 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..40a21d49 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,6 @@ +[pytest] +testpaths = 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/check_contract.py b/scripts/check_contract.py index dcb34dfa..d31b8a3f 100644 --- a/scripts/check_contract.py +++ b/scripts/check_contract.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """Offline structural checks; full schema validation is a separate target.""" + import ast import json from pathlib import Path @@ -7,48 +8,71 @@ import re ROOT = Path(__file__).resolve().parents[1] + def main(): - lab = json.loads((ROOT/'lab.json').read_text())['number'] - spec = json.loads((ROOT/'contracts/openapi.json').read_text()) - assert spec['openapi']=='3.1.0' - assert spec['info']['version']=='1.%d.0'%lab + lab = json.loads((ROOT / "lab.json").read_text())["number"] + spec = json.loads((ROOT / "contracts/openapi.json").read_text()) + assert spec["openapi"] == "3.1.0" + assert spec["info"]["version"] == "1.%d.0" % lab operation_ids = set() + def walk(value): - if isinstance(value,dict): - if '$ref' in value: - pointer = value['$ref'] - assert pointer.startswith('#/'), 'unexpected external reference' + if isinstance(value, dict): + if "$ref" in value: + pointer = value["$ref"] + assert pointer.startswith("#/"), "unexpected external reference" target = spec - for part in pointer[2:].split('/'): - target = target[part.replace('~1','/').replace('~0','~')] + for part in pointer[2:].split("/"): + target = target[part.replace("~1", "/").replace("~0", "~")] for v in value.values(): walk(v) - elif isinstance(value,list): + elif isinstance(value, list): for v in value: walk(v) + walk(spec) - for route,methods in spec['paths'].items(): - placeholders = set(re.findall(r'\{([^}]+)\}',route)) - for method,op in methods.items(): - assert op['operationId'] not in operation_ids, 'duplicate operationId' - operation_ids.add(op['operationId']) - params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')} - assert placeholders==params, 'path parameter mismatch: '+route - for response in op['responses'].values(): - assert 'X-Request-Id' in response['headers'] - if lab>=3: - assert 'X-Instance-Id' in response['headers'] - assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2) - assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4) - for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')): - ast.parse(file.read_text(),filename=str(file)) - for file in ROOT.rglob('*.json'): - if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts): + for route, methods in spec["paths"].items(): + placeholders = set(re.findall(r"\{([^}]+)\}", route)) + for method, op in methods.items(): + assert op["operationId"] not in operation_ids, "duplicate operationId" + operation_ids.add(op["operationId"]) + params = { + p["name"] + for p in op.get("parameters", []) + if p["in"] == "path" and p.get("required") + } + assert placeholders == params, "path parameter mismatch: " + route + for response in op["responses"].values(): + assert "X-Request-Id" in response["headers"] + if lab >= 3: + assert "X-Instance-Id" in response["headers"] + assert ("/api/v1/auth/sessions" in spec["paths"]) == (lab >= 2) + assert ("/api/v1/attachments/{attachment_id}" in spec["paths"]) == (lab >= 4) + for file in list((ROOT / "tests").glob("*.py")) + list( + (ROOT / "scripts").glob("*.py") + ): + ast.parse(file.read_text(), filename=str(file)) + for file in ROOT.rglob("*.json"): + if any( + part in {".git", ".venv", "node_modules", "vendor", "output"} + for part in file.parts + ): continue json.loads(file.read_text()) - for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'): - assert (ROOT/required).is_file(), 'missing '+required - print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.') + for required in ( + "README.md", + "COURSE.md", + "CONTRIBUTING.md", + "REPORT.md", + "contracts/README.md", + "tests/smoke.py", + "tests/README.md", + ): + assert (ROOT / required).is_file(), "missing " + required + print( + "PASS: offline skeleton structure, local refs and Python syntax; application NOT tested." + ) -if __name__=='__main__': + +if __name__ == "__main__": main() diff --git a/scripts/collect_evidence.sh b/scripts/collect_evidence.sh new file mode 100755 index 00000000..6cb3114c --- /dev/null +++ b/scripts/collect_evidence.sh @@ -0,0 +1,81 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." + +PYTHON="${PYTHON:-python3}" +BASE_URL="${BASE_URL:-http://localhost:8080}" +OUT=evidence + +compose() { + docker compose --env-file .env "$@" +} + +wait_ready() { + for _ in $(seq 1 90); do + curl -sf "$BASE_URL/health/ready" >/dev/null && return 0 + sleep 1 + done + echo "API $BASE_URL не готов" >&2 + exit 1 +} + +{ + echo "date: $(date -u +%Y-%m-%dT%H:%M:%SZ)" + echo "commit: $(git rev-parse --short HEAD)$(git diff --quiet HEAD -- app migrations tests scripts compose.yaml Dockerfile infra || echo ' (+ uncommitted changes)')" + echo "branch: $(git rev-parse --abbrev-ref HEAD)" + echo "cpu: $(lscpu | sed -n 's/^Model name:[[:space:]]*//p') ($(nproc) threads)" + echo "memory: $(free -h | awk '/^Mem:/ {print $2}')" + echo "kernel: $(uname -r)" + echo "docker: $(docker version --format '{{.Server.Version}}')" + echo "compose: $(docker compose version --short)" + echo "python: $($PYTHON --version 2>&1)" + echo "stand: $BASE_URL (compose project ${COMPOSE_PROJECT_NAME:-default})" + echo + compose ps --format 'table {{.Service}}\t{{.Image}}\t{{.Status}}' +} > "$OUT/00-environment.txt" + +DOCKER_TESTS=1 "$PYTHON" -m pytest tests -v -p no:cacheprovider 2>&1 \ + | grep -E "PASSED|FAILED|SKIPPED|ERROR|passed|failed" > "$OUT/01-pytest.txt" + +"$PYTHON" tests/smoke.py --base-url "$BASE_URL" > "$OUT/02-smoke.txt" 2>&1 + +{ + echo "== before" + compose ps --format 'table {{.Service}}\t{{.Status}}' + echo + "$PYTHON" tests/resilience.py --mode persistence \ + --action-script scripts/restart.sh --base-url "$BASE_URL" 2>&1 + echo + echo "== after (Up N seconds = container was restarted by scripts/restart.sh)" + compose ps --format 'table {{.Service}}\t{{.Status}}' +} > "$OUT/03-persistence.txt" + +DB_HOST=localhost DB_PORT="${DB_HOST_PORT:-5432}" PYTHONPATH=. \ + "$PYTHON" scripts/explain_history.py > "$OUT/04-explain-history.txt" + +BASE_URL="$BASE_URL" scripts/failure_demo.sh > "$OUT/05-failures.txt" 2>&1 + +{ + for key in appendonly appendfsync maxmemory maxmemory-policy dir; do + compose exec -T redis redis-cli CONFIG GET "$key" | paste -sd' ' + done + echo + compose exec -T redis redis-cli INFO persistence \ + | grep -E "^(aof_enabled|aof_last_write_status|aof_last_bgrewrite_status|loading):" +} > "$OUT/06-redis-config.txt" + +"$PYTHON" tests/load.py --base-url "$BASE_URL" --duration 20 --concurrency 8 \ + --output "$OUT/07-load.json" > /dev/null & +LOAD_PID=$! +sleep 10 +docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}' \ + $(compose ps -q) > "$OUT/07-load-resources.txt" +wait "$LOAD_PID" + +SESSION_TTL_SECONDS=3 compose up -d api >/dev/null 2>&1 +wait_ready +BASE_URL="$BASE_URL" TTL=3 scripts/expiry_demo.sh > "$OUT/08-expiry.txt" 2>&1 +compose up -d api >/dev/null 2>&1 +wait_ready + +echo "evidence written to $OUT/" 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..55ac006c --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." + +compose() { + docker compose --env-file .env "$@" +} + +wait_healthy() { + for service in "$@"; do + attempts=0 + until [ "$(compose ps --format '{{.Health}}' "$service")" = "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 diff --git a/scripts/validate_openapi.py b/scripts/validate_openapi.py index 1dfe4a98..7da2dcd0 100644 --- a/scripts/validate_openapi.py +++ b/scripts/validate_openapi.py @@ -1,23 +1,27 @@ #!/usr/bin/env python3 """Full OpenAPI and JSON Schema checks; install requirements-dev.txt first.""" + import json from pathlib import Path from openapi_spec_validator import validate from jsonschema import Draft202012Validator root = Path(__file__).resolve().parents[1] -spec = json.loads((root/'contracts/openapi.json').read_text()) +spec = json.loads((root / "contracts/openapi.json").read_text()) validate(spec) from referencing import Registry, Resource + # The base URI resolves local #/components references inside examples. -resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'}) -registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource)) -for name, example in json.loads((root/'contracts/examples.json').read_text()).items(): - schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name} +resource = dict(spec, **{"$schema": "https://json-schema.org/draft/2020-12/schema"}) +registry = Registry().with_resource( + "urn:mtusi:openapi", Resource.from_contents(resource) +) +for name, example in json.loads((root / "contracts/examples.json").read_text()).items(): + schema = {"$ref": "urn:mtusi:openapi#/components/schemas/" + name} Draft202012Validator(schema, registry=registry).validate(example) -for path in (root/'contracts').glob('*.schema.json'): +for path in (root / "contracts").glob("*.schema.json"): schema = json.loads(path.read_text()) Draft202012Validator.check_schema(schema) - for example in schema.get('examples',[]): + for example in schema.get("examples", []): Draft202012Validator(schema).validate(example) -print('PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.') +print("PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.") diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..489646b5 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,199 @@ +"""Фикстуры async-тестов на реальных PostgreSQL и Redis. + +Перед запуском: `make test-stand-up` (тестовый стенд и база _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/tests/helpers.py b/tests/helpers.py new file mode 100644 index 00000000..79a898ff --- /dev/null +++ b/tests/helpers.py @@ -0,0 +1,81 @@ +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() + + +async def send_message(client: AsyncClient, user: dict, chat: dict, text: str): + return await client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers=bearer(user), + 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/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 00000000..14b5a256 --- /dev/null +++ b/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 tests.conftest import close_app, make_client +from tests.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/tests/test_chats.py b/tests/test_chats.py new file mode 100644 index 00000000..8518f7a7 --- /dev/null +++ b/tests/test_chats.py @@ -0,0 +1,209 @@ +from urllib.parse import quote +from uuid import uuid4 + +import pytest +from httpx import AsyncClient + +from tests.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/tests/test_concurrency.py b/tests/test_concurrency.py new file mode 100644 index 00000000..c498871f --- /dev/null +++ b/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 tests.conftest import close_app, make_client +from tests.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/tests/test_constraints.py b/tests/test_constraints.py new file mode 100644 index 00000000..bb1f2a42 --- /dev/null +++ b/tests/test_constraints.py @@ -0,0 +1,46 @@ +"""Ограничения на уровне БД: приложение — не единственный защитник данных.""" + +import pytest +from sqlalchemy import text +from sqlalchemy.exc import IntegrityError + +from tests.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/tests/test_errors.py b/tests/test_errors.py new file mode 100644 index 00000000..510e9bf7 --- /dev/null +++ b/tests/test_errors.py @@ -0,0 +1,76 @@ +import json +import logging + +from httpx import AsyncClient + +from tests.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/tests/test_failures.py b/tests/test_failures.py new file mode 100644 index 00000000..7b28ee67 --- /dev/null +++ b/tests/test_failures.py @@ -0,0 +1,93 @@ +"""Недоступность Redis и PostgreSQL: ограниченный 503, без анонимного доступа.""" + +import time + +from app.main import create_app +from tests.conftest import close_app, make_client +from tests.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) diff --git a/tests/test_health.py b/tests/test_health.py new file mode 100644 index 00000000..8b426370 --- /dev/null +++ b/tests/test_health.py @@ -0,0 +1,62 @@ +from httpx import AsyncClient + +from app.main import create_app +from tests.conftest import close_app, make_client +from tests.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/tests/test_history_plan.py b/tests/test_history_plan.py new file mode 100644 index 00000000..6a8ff3cb --- /dev/null +++ b/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 tests.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/tests/test_limits.py b/tests/test_limits.py new file mode 100644 index 00000000..0f3a0661 --- /dev/null +++ b/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 tests.conftest import close_app, make_client +from tests.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/tests/test_messages.py b/tests/test_messages.py new file mode 100644 index 00000000..074acbd2 --- /dev/null +++ b/tests/test_messages.py @@ -0,0 +1,237 @@ +from urllib.parse import quote + +import pytest +from httpx import AsyncClient + +from tests.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 tests.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/tests/test_migrations.py b/tests/test_migrations.py new file mode 100644 index 00000000..8e73a66e --- /dev/null +++ b/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 tests.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/tests/test_pagination.py b/tests/test_pagination.py new file mode 100644 index 00000000..b5cbcc04 --- /dev/null +++ b/tests/test_pagination.py @@ -0,0 +1,137 @@ +"""Курсоры: непрозрачность, привязка к ресурсу и устойчивость к подделке.""" + +import base64 +import json +from urllib.parse import quote + +import pytest +from httpx import AsyncClient + +from tests.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/tests/test_sessions.py b/tests/test_sessions.py new file mode 100644 index 00000000..3210ec55 --- /dev/null +++ b/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 tests.conftest import close_app, make_client +from tests.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/tests/test_users.py b/tests/test_users.py new file mode 100644 index 00000000..4f45d4d2 --- /dev/null +++ b/tests/test_users.py @@ -0,0 +1,128 @@ +import pytest +from httpx import AsyncClient + +from tests.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"