diff --git a/.dockerignore b/.dockerignore index bc16e556..7c415953 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,16 @@ __pycache__ node_modules evidence .DS_Store +.pytest_cache +*.py[cod] +tests +student-tests +contracts +scripts +examples +docs +*.md +Makefile +Dockerfile* +requirements-dev.txt +lab.json diff --git a/.env.example b/.env.example index 714c8076..4f95f6a2 100644 --- a/.env.example +++ b/.env.example @@ -3,9 +3,27 @@ APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab +LOG_LEVEL=INFO +SHUTDOWN_TIMEOUT_SEC=5 + +DB_USER=test +DB_PASS=test +DB_NAME=test +DB_HOST=localhost +DB_PORT=5432 +DB_POOL_MIN_SIZE=2 +DB_POOL_MAX_SIZE=10 +DB_CONNECT_TIMEOUT_SEC=3 +DB_COMMAND_TIMEOUT_SEC=5 +DB_STARTUP_TIMEOUT_SEC=30 + +VALKEY_HOST=localhost +VALKEY_PORT=6379 +VALKEY_PASS=test +VALKEY_POOL_MAX_SIZE=20 +VALKEY_TIMEOUT_SEC=0.5 +VALKEY_RETRY_AFTER_SEC=5 + 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_CACHE_TTL_SECONDS=60 +PASSWORD_HASH_WORKERS=4 diff --git a/.gitignore b/.gitignore index b5a71896..ed8abe81 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ coverage/ .vscode/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. +.pytest_cache/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..91783354 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.12.12-slim-bookworm + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + POETRY_VIRTUALENVS_CREATE=false + +WORKDIR /app +COPY poetry.lock pyproject.toml ./ +RUN pip install --no-cache-dir "poetry==2.2.1" \ + && poetry install --only main --no-root --no-interaction +COPY migrations ./migrations +COPY src ./src + +# Процесс работает не от root +RUN useradd --system --uid 10001 --no-create-home app +USER app + +EXPOSE 8080 +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD ["python", "src/healthcheck.py"] +CMD ["python", "src/gateway.py"] diff --git a/Makefile b/Makefile index 9ecb223e..b0393aed 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,12 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence +.PHONY: help check validate test load test-persistence install up down restart ps logs wait-ready destroy-data student-test concurrency-test test-expiry test-failures test-migrations explain sigterm-check help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "up/down/restart/ps/logs: compose stack; destroy-data: DELETE volumes intentionally" + @echo "student-test: own tests; test-migrations: from empty volume; test-expiry: short SESSION_TTL; test-failures: stop Valkey/PostgreSQL; explain: EXPLAIN history" check: $(PYTHON) scripts/check_contract.py @@ -24,3 +26,56 @@ 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) + +install: + poetry install --no-root + +up: + docker compose up -d --build --wait + +down: + docker compose --profile expiry down + +restart: + ./scripts/restart.sh + @$(MAKE) --no-print-directory wait-ready + +ps: + docker compose ps + +logs: + docker compose logs -f api + +wait-ready: + @for i in $$(seq 1 60); do \ + curl -fsS "$(BASE_URL)/health/ready" >/dev/null 2>&1 && echo "ready: $(BASE_URL)" && exit 0; \ + sleep 1; \ + done; echo "API is not ready after 60s"; exit 1 + +destroy-data: + docker compose --profile expiry down -v + +student-test: + BASE_URL="$(BASE_URL)" poetry run pytest + +concurrency-test: + BASE_URL="$(BASE_URL)" poetry run pytest student-tests/test_concurrency.py -s + +test-expiry: + docker compose --profile expiry up -d --build --wait api-short-ttl + EXPIRY_BASE_URL=http://localhost:8081 poetry run pytest student-tests/test_expiry.py -s; \ + status=$$?; docker compose --profile expiry stop api-short-ttl; exit $$status + +test-failures: + BASE_URL="$(BASE_URL)" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_dependency_failures.py -s + +test-migrations: + ./scripts/check_migrations.sh + +explain: + docker compose exec -T postgres sh -c 'psql -X -q -v ON_ERROR_STOP=1 -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"' \ + < scripts/explain_history.sql > evidence/explain_history.txt + @echo "saved: evidence/explain_history.txt" + +sigterm-check: + $(PYTHON) scripts/sigterm_check.py --base-url "$(BASE_URL)" diff --git a/REPORT.md b/REPORT.md index e42f2b05..18316638 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,168 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO +- Автор, группа: Саватеев Артём, БВТ2302 +- Целевая оценка: **4** (все пункты уровней 3 и 4) - Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Использованные источники, библиотеки и помощь ИИ: FastAPI, Pydantic, Uvicorn, loguru, asyncpg, redis-py, argon2-cffi; для тестов pytest, httpx; документация PostgreSQL (transactions, EXPLAIN, advisory locks, `uuidv7`) и Valkey ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Версии: Docker Engine 29.0.1, Docker Compose v2.40.3 (проверено на Docker Desktop, linux/arm64). Образы закреплены явными тегами: `python:3.12.12-slim`, `postgres:18.22`, `valkey/valkey:8.1.4`. Python-зависимости закреплены в `pyproject.toml` (точные версии) и `poetry.lock`. + +Prerequisites: Docker с Compose, make, curl, Python ≥ 3.9 для публичных тестов. Для собственных тестов дополнительно Python 3.12 и Poetry 2.2.1. + +```sh +# Запуск +cp .env.example .env +make up # docker compose up -d --build --wait; миграции API применяет сам при старте + +# Проверки +make check # структура skeleton +make test # публичный smoke +make test-persistence ACTION_SCRIPT=scripts/restart.sh # рестарт api, postgres, valkey с сохранением volume +make install # dev-окружение Poetry (один раз) +make student-test # собственные тесты, одна команда +make test-migrations # миграции на пустом volume отдельного compose-проекта +make test-expiry # второй API с SESSION_TTL_SECONDS=2 на :8081 +make test-failures # останавливает и запускает valkey и postgres +make explain # сид ≥10 000 сообщений и планы запроса истории + +# Безопасная остановка без потери данных +make down # = docker compose --profile expiry down +``` + +Намеренное удаление учебных данных — `make destroy-data` (`docker compose down -v`): volume `postgres_data` будет стёрт. Остальные команды volume не удаляют. + +Операторских действий кроме создания `.env` не требуется. `make up` ждёт healthcheck всех сервисов, готовность API — `GET /health/ready`. `.env` в образ не копируется (`.dockerignore`), конфигурация передаётся через `env_file` в `compose.yaml`. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +``` +Клиент ──HTTP :8080──▶ api (FastAPI, uvicorn) ──asyncpg pool 2..10──▶ postgres источник истины, volume postgres_data + │ + └──redis-py pool ≤20, timeout 0.5 s──▶ valkey read-through кэш сессий, без persistence +сети: default (только api, 127.0.0.1:8080) + backend (internal: api, postgres, valkey) +``` + +Слои в `src/`: + +| Каталог | Ответственность | +| --- | --- | +| `config/` | чтение окружения через envparse, логирование loguru в stdout | +| `domain/` | Pydantic-схемы запросов/ответов, сессии и учётные данные, коды и исключения ошибок | +| `interfaces/` | ABC-репозитории и кэш сессий: сервисы зависят от абстракций, а не от PostgreSQL/Valkey | +| `infrastructure/` | репозитории PostgreSQL, кэш Valkey, сервисы, Argon2id и токены (`security/`), пул БД и кэша, пагинация/cursor, `ServiceCreator` для DI | +| `application/api/` | роутеры, Basic/Bearer зависимости, middleware, обработчики ошибок | +| `gateway.py` | сборка приложения, lifespan (пул, миграции, кэш), запуск uvicorn | + +Миграции — нумерованные SQL-файлы в `migrations/`. + +Роли и сервисы `compose.yaml`: + +| Роль | Сервис | Версия | Health / readiness | Volume | Сеть / порт | Credentials | +| --- | --- | --- | --- | --- | --- | --- | +| API | `api` | сборка из `Dockerfile` | `src/healthcheck.py` → `/health/ready` | нет | default + backend, `127.0.0.1:8080` | `DB_*`, `VALKEY_*` из `.env` (env_file) | +| PostgreSQL | `postgres` | `postgres:18.2-alpine3.22` | `pg_isready` | `postgres_data:/var/lib/postgresql` | backend (internal), не публикуется | `POSTGRES_PASSWORD` ← `DB_PASS` | +| Valkey | `valkey` | `valkey/valkey:8.1.4-alpine3.22` | `valkey-cli ping` | нет (чистый кэш) | backend (internal), не публикуется | `--requirepass` ← `VALKEY_PASS` | +| API с коротким TTL | `api-short-ttl` (profile `expiry`) | тот же образ | как у API | нет | `127.0.0.1:8081` | как у API, `SESSION_TTL_SECONDS=2` | + +**Что перенесено из lab1.** Слои `application/domain/interfaces/infrastructure`, контракт ошибок, `X-Request-Id`, keyset-cursor, graceful shutdown, Dockerfile с пользователем не root. In-memory хранилище удалено, вместо него репозитории `Postgres*Repository` и `ValkeySessionCache` за теми же интерфейсами. + +**Переходы API по COURSE.md.** Регистрация требует `password` (12–128 code points). Вход только через `POST /api/v1/auth/sessions` с Basic. Все бизнес-endpoint требуют Bearer. Basic на бизнес-endpoint и `X-User-Id` возвращают 401 и не влияют на владельца сессии. + +**Источник истины** — PostgreSQL. Схема — `migrations/0001_init.sql`: + +| Таблица | Ключи и ограничения | +| --- | --- | +| `users` | PK `id uuid DEFAULT uuidv7()`, `UNIQUE (username)`, CHECK формата username и длины display_name, `password_hash` (PHC Argon2id) | +| `sessions` | PK `token_hash bytea` (SHA-256 токена, 32 байта), FK `user_id → users`, `expires_at`, `revoked_at`, CHECK `expires_at > created_at` | +| `chats` | PK uuid v7, FK `created_by → users`, CHECK длины title | +| `chat_members` | PK `(chat_id, user_id)`, FK на chats и users, `position` (порядок `member_ids`), `UNIQUE (chat_id, position)`, индекс `(user_id, chat_id)` | +| `messages` | PK uuid v7, FK `(chat_id, sender_id) → chat_members`: отправитель обязан быть участником; CHECK длины text; индекс `(chat_id, created_at, id)` | + +UUID v7 упорядочен по времени, поэтому вставки в B-tree PK идут в правую часть, как у bigint, а наружу отдаётся UUID, как требует контракт. + +**Регистрация** — один `INSERT`. Гонку одинаковых username разрешает `UNIQUE`, `UniqueViolationError` превращается в 409. Проверки «сначала SELECT» нет. + +**Вход.** `SELECT` пользователя, проверка Argon2id в отдельном пуле потоков, затем `INSERT sessions` с `expires_at = clock_timestamp() + TTL` по часам БД. Токен — 32 случайных байта (256 бит), в БД хранится только его SHA-256. Для неизвестного username проверяется dummy-hash, поэтому ответ и время одинаковы. + +**Создание чата** — `BEGIN` → `INSERT chats` → `INSERT chat_members` (unnest) → `COMMIT`. Неизвестный участник нарушает FK, транзакция откатывается целиком, клиент получает 400. Предварительной проверки участников нет: она была бы гонкой и маскировала бы откат. + +**Сообщение** — `BEGIN` → `pg_advisory_xact_lock(2, hashtext(chat_id))` → `INSERT … created_at = GREATEST(clock_timestamp(), MAX(created_at) + 1 µs)` → `COMMIT`. Внутри чата `created_at` строго возрастает, и порядок коммитов совпадает с порядком `(created_at, id)`. Поэтому читатель с cursor не пропустит сообщение, вставленное «задним числом» конкурентной транзакцией. Это замена `asyncio.Lock` и `MonotonicClock` из lab1, корректная для нескольких процессов. Записи в один чат сериализуются, записи в разные чаты нет. + +**Logout** — `UPDATE sessions SET revoked_at` для действующей сессии (0 строк → 401), затем `DEL` ключа в Valkey. + +**Valkey.** Read-through кэш сессий: ключ `session:`, значение `{user, expires_at}`. При попадании в кэш аутентификация выполняется без SQL. Источник истины — таблица `sessions`, Valkey хранит только копию. TTL записи — `min(остаток жизни сессии, SESSION_CACHE_TTL_SECONDS=60)`, приложение дополнительно сверяет `expires_at`. Logout удаляет ключ после фиксации отзыва в БД; если Valkey в этот момент недоступен, запись может остаться не дольше 60 с, это логируется как warning. + +**Persistence Valkey выключена** (`--save "" --appendonly no`). Valkey не единственное хранилище сессий: после рестарта или flush кэш пуст и заполняется из PostgreSQL, сессии не теряются. Заодно запись, не удалённая при logout во время сбоя, не «воскресает» после рестарта Valkey. + +**Отказ Valkey.** Ошибка (таймаут 0.5 с, без встроенных повторов) переводит кэш в режим обхода на `VALKEY_RETRY_AFTER_SEC=5` с: все запросы идут в PostgreSQL без сетевых попыток к Valkey, сервис работает, `/health/ready` = 200. + +**Readiness.** Критичен только PostgreSQL: `/health/ready` выполняет `SELECT 1`, без него ответ — `503 unavailable` без деталей SQL. Анонимный запрос — 401. Неизвестный токен при недоступной БД — 503, а не 200: доверие без проверки невозможно. Valkey не критичен. + +**Пул и таймауты.** asyncpg: `min_size=2`, `max_size=10`, connect/acquire timeout 3 с, `command_timeout` 5 с и серверный `statement_timeout` 5 с. Ошибки соединения превращаются в `UnavailableError` (503); первая такая ошибка вызывает `pool.expire_connections()`, чтобы после рестарта PostgreSQL 503 получил один запрос, а не все соединения пула по очереди. Запросы только параметризованные (`$1…$n`), SQL написан в методах репозиториев, значения в текст запроса не подставляются. + +| Переменная | Где используется | Значение в примере | +| --- | --- | --- | +| `APP_HOST` | адрес bind uvicorn; в контейнере обязательно `0.0.0.0` | `0.0.0.0` | +| `APP_PORT` | порт uvicorn, healthcheck, публикация порта в `compose.yaml` | `8080` | +| `APP_ENV` | имя окружения в логе запуска приложения | `lab` | +| `LOG_LEVEL` | уровень loguru | `INFO` | +| `SHUTDOWN_TIMEOUT_SEC` | `timeout_graceful_shutdown` uvicorn | `5` | +| `DB_USER`, `DB_PASS`, `DB_NAME`, `DB_HOST`, `DB_PORT` | подключение asyncpg; учётные данные сервиса `postgres`. В контейнере API `compose.yaml` подставляет `DB_HOST=postgres` | `test`, `test`, `test`, `localhost`, `5432` | +| `DB_POOL_MIN_SIZE`, `DB_POOL_MAX_SIZE` | границы пула asyncpg | `2`, `10` | +| `DB_CONNECT_TIMEOUT_SEC`, `DB_COMMAND_TIMEOUT_SEC` | таймаут подключения/ожидания соединения и выполнения запроса | `3`, `5` | +| `DB_STARTUP_TIMEOUT_SEC` | сколько ждать БД при старте API | `30` | +| `VALKEY_HOST`, `VALKEY_PORT`, `VALKEY_PASS` | подключение к Valkey; пароль сервиса `valkey`. В контейнере API `compose.yaml` подставляет `VALKEY_HOST=valkey` | `localhost`, `6379`, `test` | +| `VALKEY_POOL_MAX_SIZE`, `VALKEY_TIMEOUT_SEC`, `VALKEY_RETRY_AFTER_SEC` | пул redis-py, таймаут операции, пауза обхода кэша после ошибки | `20`, `0.5`, `5` | +| `SESSION_TTL_SECONDS` | срок жизни сессии | `3600` | +| `SESSION_CACHE_TTL_SECONDS` | верхняя граница TTL записи в Valkey | `60` | +| `PASSWORD_HASH_WORKERS` | потоки Argon2id: ограничение CPU/RAM на одновременные входы | `4` | + +Переменные skeleton: `DATABASE_URL_FILE` заменён на `DB_*`, `CACHE_URL_FILE` — на `VALKEY_*`; `SESSION_STORE=postgres` соответствует реализации: сессии в PostgreSQL, Valkey — кэш. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Полный вывод команд сохранён в каталоге `evidence/`, имя файла указано в скобках. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Критерий README | Команда/сценарий | Наблюдаемый результат | +| --- | --- | --- | +| 3: публичный smoke | `make check && make test` | `PASS: lab2 public API scenario` (smoke.txt) | +| 3: данные и действующие сессии переживают рестарт API и хранилищ | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | `PASS`; `StartedAt` у api, postgres, valkey изменился, volume сохранён, токен до рестарта работает после (persistence.txt) | +| 3: PK/FK, уникальность username, индексы | `\d+ messages`, `\di` | ограничения и индексы из миграции (migrations.txt) | +| 3: миграции с нуля и повторно без дублирования | `make test-migrations` | пустой volume: 0 таблиц → запуск 1 применяет `0001_init` → запуски 2–3 ничего не меняют (migrations_from_zero.txt) | +| 3: атомарное создание чата | `test_failed_chat_creation_is_rolled_back` | 400; в БД 0 строк `chats` с этим title и 0 участников-сирот (race_rollback.txt) | +| 3: Basic только на `POST /auth/sessions`, Bearer на остальных, X-User-Id не даёт доступа | `test_auth.py`, `test_users.py`, smoke | 401 с `WWW-Authenticate: Basic …` / `Bearer …`; Basic на бизнес-endpoint — 401; `X-User-Id` игнорируется (student_tests.txt) | +| 3: KDF паролей | `test_password_is_not_stored_in_plaintext` | в `users.password_hash` строка `$argon2id$…`, пароля нет (student_tests.txt) | +| 3: секреты и volumes не в Git | `git check-ignore .env`, `.gitignore` | `.env`, `volumes/`, `data/`, `secrets/` игнорируются | +| 4: Valkey реально используется для сессий с TTL | `test_valkey_down_cache_is_bypassed`, раздел «Valkey» | read-through кэш, TTL ≤ min(сессия, 60 с), DEL при logout (dependency_failures.txt) | +| 4: параметризованные запросы, ограниченный пул и таймауты | `infrastructure/repositories/*/postgres.py`, `dependencies/database.py`, `dependencies/cache.py` | только `$n`; пул 2..10; таймауты 3/5 с; Valkey ≤20, 0.5 с | +| 4: гонка регистрации одинакового username | `test_same_username_race_creates_exactly_one_user` | 20 параллельных запросов → 1×201, 19×409, 1 строка в БД (race_rollback.txt) | +| 4: rollback составной операции | `test_failed_chat_creation_is_rolled_back` | откат проверен по содержимому БД, а не только по ответу (race_rollback.txt) | +| 4: индекс истории и `EXPLAIN (ANALYZE, BUFFERS)` на ≥10 000 сообщениях | `make explain` | 138 769 сообщений, в чате 20 000. Страница из середины: `Index Scan … Index Cond: chat_id AND ROW(created_at,id) > ROW(…)`, 5 буферов, 0.036 мс; generic plan (как в asyncpg) — тот же Index Scan. Без индекса — `Parallel Seq Scan`, 128 768 строк отброшено, 1 792 буфера, 7.28 мс (explain_history.txt) | +| 4: expiry при коротком `SESSION_TTL_SECONDS` | `make test-expiry` | токен работает и попадает в кэш, через 3 с — 401 на `/users/me`, `/chats`, logout (expiry.txt) | +| 4: logout, неверный пароль | `test_logout_revokes_only_current_session`, `test_wrong_password_and_unknown_user_are_indistinguishable` | 204 без тела → 401 → повторный logout 401; другая сессия жива; неверный пароль и неизвестный логин дают одинаковый 401 (student_tests.txt) | +| 4: недоступность Valkey/БД | `make test-failures` | без Valkey вход, запись и logout работают, ответ < 3 с. Без PostgreSQL — `/health/ready` 503, аноним 401, неизвестный токен 503, регистрация/вход 503 без деталей. После рестарта PostgreSQL прежний токен работает (dependency_failures.txt) | + +Сводка собственных тестов: `make student-test` — 87 passed, 6 skipped (их запускают `make test-expiry` и `make test-failures`). ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. - | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| `scripts/restart.sh`: рестарт postgres, valkey, api | данные и сессии на месте | PASS, прежний токен читает те же сообщения | ~2 с до ready; потерь нет | +| `docker compose stop valkey` | кэш обходится, API работает | вход, Bearer, запись, logout — OK, ready 200; один warning, далее 5 с без попыток | мгновенно; потерь нет (кэш) | +| logout при остановленном valkey, затем его старт | отозванный токен не принимается | 401 после подъёма: кэш без persistence пуст | — | +| `docker compose stop postgres` | ограниченный 503, без доверия анониму | ready 503, аноним 401, неизвестный токен 503, ответ < 3 с | — | +| `docker compose restart postgres` при живом API | API переподключается сам | после ready прежний токен работает | до ~2 с; данные на volume | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Один Docker-host: отказ хоста, диска или единственного экземпляра PostgreSQL не переживается, реплик и бэкапов нет. HA для Valkey тоже нет, но он не критичен. +- `COMMIT` подтверждается после записи WAL на диск (`synchronous_commit=on`, `fsync=on`); гарантия ограничена честностью fsync в Docker Desktop VM и одним диском. +- Если Valkey недоступен ровно в момент logout, но не перезапускался (например, сетевой разрыв), отозванная сессия может приниматься ещё до 60 с. Граница задаётся `SESSION_CACHE_TTL_SECONDS`. +- Запись в один чат сериализуется advisory lock: пропускная способность одного «горячего» чата ограничена. Коллизии `hashtext` изредка сериализуют разные чаты. +- Символ U+0000 в `text`/`title`/`display_name` отклоняется с 400: тип `text` в PostgreSQL его не хранит. Тест lab1 с `\u0000` заменён на отдельный тест отказа. +- Список чатов пользователя сортируется по `(created_at, id)` после соединения `chat_members` → `chats`, отдельного keyset-индекса нет. Для пользователя с тысячами чатов понадобится денормализация `created_at` в `chat_members`. +- Устаревшие строки `sessions` не удаляются: фоновой очистки пока нет. +- `expire_connections` срабатывает на первой ошибке соединения; запрос, который на неё попал, получает 503 и не повторяется прозрачно — автоматических повторов POST нет намеренно. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..02860763 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,88 @@ +name: messenger-lab2 + +x-api: &api + build: . + image: mtusi-messenger:lab2 + restart: unless-stopped + env_file: + - .env + environment: + DB_HOST: postgres + VALKEY_HOST: valkey + depends_on: + postgres: + condition: service_healthy + valkey: + condition: service_started + networks: + - backend + - default + stop_grace_period: 10s + +services: + postgres: + image: postgres:18.2-alpine3.22 + restart: unless-stopped + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASS} + POSTGRES_DB: ${DB_NAME} + volumes: + - postgres_data:/var/lib/postgresql + healthcheck: + test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""] + interval: 2s + timeout: 3s + retries: 15 + start_period: 10s + networks: + - backend + stop_grace_period: 30s + + valkey: + image: valkey/valkey:8.1.4-alpine3.22 + restart: unless-stopped + environment: + VALKEY_PASS: ${VALKEY_PASS} + command: + - valkey-server + - --requirepass + - ${VALKEY_PASS} + - --save + - "" + - --appendonly + - "no" + - --maxmemory + - 64mb + - --maxmemory-policy + - allkeys-lru + healthcheck: + test: ["CMD-SHELL", "valkey-cli --no-auth-warning -a \"$$VALKEY_PASS\" ping | grep -q PONG"] + interval: 2s + timeout: 3s + retries: 15 + networks: + - backend + + api: + <<: *api + ports: + - "127.0.0.1:${APP_PORT}:${APP_PORT}" + + api-short-ttl: + <<: *api + profiles: + - expiry + environment: + DB_HOST: postgres + VALKEY_HOST: valkey + SESSION_TTL_SECONDS: "2" + ports: + - "127.0.0.1:8081:${APP_PORT}" + +networks: + backend: + internal: true + +volumes: + postgres_data: diff --git a/evidence/dependency_failures.txt b/evidence/dependency_failures.txt new file mode 100644 index 00000000..82120ce3 --- /dev/null +++ b/evidence/dependency_failures.txt @@ -0,0 +1,27 @@ +$ make test-failures +BASE_URL="http://localhost:8080" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_dependency_failures.py -s +/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 + warnings.warn( +============================= test session starts ============================== +platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-TbR4RJXC-py3.12/bin/python +cachedir: .pytest_cache +rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab2 +configfile: pyproject.toml +plugins: anyio-4.15.1 +collecting ... collected 4 items + +student-tests/test_dependency_failures.py::test_valkey_down_cache_is_bypassed PASSED +student-tests/test_dependency_failures.py::test_logout_during_valkey_outage_is_not_resurrected PASSED +student-tests/test_dependency_failures.py::test_postgres_down_returns_bounded_503_and_no_anonymous_access PASSED +student-tests/test_dependency_failures.py::test_session_survives_postgres_restart PASSED + +============================== 4 passed in 11.94s ============================== + +$ docker compose logs --no-log-prefix --since 2026-10-11T14:41:19Z api | grep WARNING +2026-10-11 14:41:20.418 | WARNING | infrastructure.dependencies.cache:mark_failed:69 - Valkey недоступен (ConnectionError): кэш сессий обходится 5.0 с, запросы идут в PostgreSQL +2026-10-11 14:41:20.434 | WARNING | infrastructure.services.auth_service.auth_service:logout:88 - Сессия отозвана в PostgreSQL, но запись кэша не удалена: она истечёт по TTL кэша +2026-10-11 14:41:23.079 | WARNING | infrastructure.services.auth_service.auth_service:logout:88 - Сессия отозвана в PostgreSQL, но запись кэша не удалена: она истечёт по TTL кэша +2026-10-11 14:41:25.665 | WARNING | infrastructure.utils.repository_utils:wrapper:34 - Request to DATABASE || Object: PostgresSessionRepository || Function: get_active || failed || Error: gaierror +2026-10-11 14:41:25.712 | WARNING | infrastructure.utils.repository_utils:wrapper:34 - Request to DATABASE || Object: PostgresUserRepository || Function: create || failed || Error: gaierror +2026-10-11 14:41:25.719 | WARNING | infrastructure.utils.repository_utils:wrapper:34 - Request to DATABASE || Object: PostgresUserRepository || Function: get_credentials || failed || Error: gaierror + diff --git a/evidence/expiry.txt b/evidence/expiry.txt new file mode 100644 index 00000000..dfcc4ccf --- /dev/null +++ b/evidence/expiry.txt @@ -0,0 +1,89 @@ +$ make test-expiry +docker compose --profile expiry up -d --build --wait api-short-ttl +#1 [internal] load local bake definitions +#1 reading from stdin 597B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 642B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.12.12-slim-bookworm +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 328B done +#4 DONE 0.0s + +#5 [1/7] FROM docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c +#5 resolve docker.io/library/python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c done +#5 DONE 0.0s + +#6 [internal] load build context +#6 transferring context: 117.69kB 0.0s done +#6 DONE 0.0s + +#7 [3/7] COPY poetry.lock pyproject.toml ./ +#7 CACHED + +#8 [2/7] WORKDIR /app +#8 CACHED + +#9 [5/7] COPY migrations ./migrations +#9 CACHED + +#10 [4/7] RUN pip install --no-cache-dir "poetry==2.2.1" && poetry install --only main --no-root --no-interaction +#10 CACHED + +#11 [6/7] COPY src ./src +#11 CACHED + +#12 [7/7] RUN useradd --system --uid 10001 --no-create-home app +#12 CACHED + +#13 exporting to image +#13 exporting layers done +#13 exporting manifest sha256:38c2dfc7141796ce2219a46fa351316f15c1c687a9a09ec702887d0f351165ce done +#13 exporting config sha256:1ab109ba0df31f1aa4ef7fca42802a75b11b456dfa73dcb4ec54117b4909cbc0 done +#13 exporting attestation manifest sha256:0131ba669988c7b2d51762370da624bd434ef64b4ea09b95b4a6e12458b0a1f8 done +#13 exporting manifest list sha256:95d9d58148c7c6842d3fbb4b4531522da5e203f013ddedd1e566fcd118a6c71c done +#13 naming to docker.io/library/mtusi-messenger:lab2 done +#13 unpacking to docker.io/library/mtusi-messenger:lab2 done +#13 DONE 0.0s + +#14 resolving provenance for metadata file +#14 DONE 0.0s + mtusi-messenger:lab2 Built + Container messenger-lab2-valkey-1 Running + Container messenger-lab2-postgres-1 Running + Container messenger-lab2-api-short-ttl-1 Recreate + Container messenger-lab2-api-short-ttl-1 Recreated + Container messenger-lab2-postgres-1 Waiting + Container messenger-lab2-postgres-1 Healthy + Container messenger-lab2-api-short-ttl-1 Starting + Container messenger-lab2-api-short-ttl-1 Started + Container messenger-lab2-valkey-1 Waiting + Container messenger-lab2-api-short-ttl-1 Waiting + Container messenger-lab2-postgres-1 Waiting + Container messenger-lab2-valkey-1 Healthy + Container messenger-lab2-postgres-1 Healthy + Container messenger-lab2-api-short-ttl-1 Healthy +EXPIRY_BASE_URL=http://localhost:8081 poetry run pytest student-tests/test_expiry.py -s; \ + status=$?; docker compose --profile expiry stop api-short-ttl; exit $status +/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 + warnings.warn( +============================= test session starts ============================== +platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-TbR4RJXC-py3.12/bin/python +cachedir: .pytest_cache +rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab2 +configfile: pyproject.toml +plugins: anyio-4.15.1 +collecting ... collected 2 items + +student-tests/test_expiry.py::test_session_expires_after_short_ttl PASSED +student-tests/test_expiry.py::test_expires_at_matches_short_ttl PASSED + +============================== 2 passed in 3.26s =============================== + Container messenger-lab2-api-short-ttl-1 Stopping + Container messenger-lab2-api-short-ttl-1 Stopped + diff --git a/evidence/explain_history.txt b/evidence/explain_history.txt new file mode 100644 index 00000000..d2a44aaf --- /dev/null +++ b/evidence/explain_history.txt @@ -0,0 +1,96 @@ +== Объём данных + messages_total | messages_in_big_chat | messages_heap | history_index +----------------+----------------------+---------------+--------------- + 138769 | 20000 | 14 MB | 8000 kB +(1 row) + + +== 1. Первая страница истории (limit 50 + 1), как list_for_chat + QUERY PLAN +--------------------------------------------------------------------------------------------------------------------- + Limit (actual time=0.008..0.012 rows=51.00 loops=1) + Buffers: shared hit=4 + -> Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.009 rows=51.00 loops=1) + Index Cond: (chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid) + Index Searches: 1 + Buffers: shared hit=4 + Planning: + Buffers: shared hit=6 + Planning Time: 0.092 ms + Execution Time: 0.026 ms +(10 rows) + + +== 2. Страница из середины по cursor (после 10 000-го), как list_for_chat_after + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (actual time=0.007..0.012 rows=51.00 loops=1) + Buffers: shared hit=5 + -> Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.010 rows=51.00 loops=1) + Index Cond: ((chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, '01a10705-59eb-73d8-aa15-9a1a496ee297'::uuid))) + Index Searches: 1 + Buffers: shared hit=5 + Planning Time: 0.044 ms + Execution Time: 0.036 ms +(8 rows) + + +== 3. Тот же запрос как generic plan prepared statement (так его исполняет asyncpg) + QUERY PLAN +--------------------------------------------------------------------------------------------------------------------- + Limit (actual time=0.008..0.012 rows=51.00 loops=1) + Buffers: shared hit=5 + -> Index Scan using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.009 rows=51.00 loops=1) + Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3))) + Index Searches: 1 + Buffers: shared hit=5 + Planning Time: 0.041 ms + Execution Time: 0.022 ms +(8 rows) + + +== 4. MAX(created_at) при вставке сообщения (монотонное время чата) + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------ + Result (actual time=0.008..0.008 rows=1.00 loops=1) + Buffers: shared hit=4 + InitPlan 1 + -> Limit (actual time=0.007..0.007 rows=1.00 loops=1) + Buffers: shared hit=4 + -> Index Only Scan Backward using messages_chat_id_created_at_id_idx on messages (actual time=0.007..0.007 rows=1.00 loops=1) + Index Cond: (chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid) + Heap Fetches: 0 + Index Searches: 1 + Buffers: shared hit=4 + Planning: + Buffers: shared hit=3 + Planning Time: 0.041 ms + Execution Time: 0.015 ms +(14 rows) + + +== 5. Для сравнения: тот же запрос из середины БЕЗ индекса истории + (DROP INDEX внутри транзакции с ROLLBACK — индекс не теряется) + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (actual time=6.360..7.265 rows=51.00 loops=1) + Buffers: shared hit=1792 + -> Gather Merge (actual time=6.360..7.262 rows=51.00 loops=1) + Workers Planned: 1 + Workers Launched: 1 + Buffers: shared hit=1792 + -> Sort (actual time=2.069..2.073 rows=25.50 loops=2) + Sort Key: created_at, id + Sort Method: top-N heapsort Memory: 31kB + Buffers: shared hit=1792 + Worker 0: Sort Method: quicksort Memory: 25kB + -> Parallel Seq Scan on messages (actual time=0.183..1.840 rows=5000.00 loops=2) + Filter: ((chat_id = '01a10705-59d7-70b2-aa32-64965b148b34'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 02:46:40+00'::timestamp with time zone, '01a10705-59eb-73d8-aa15-9a1a496ee297'::uuid))) + Rows Removed by Filter: 64384 + Buffers: shared hit=1777 + Planning: + Buffers: shared hit=4 + Planning Time: 0.045 ms + Execution Time: 7.283 ms +(19 rows) + diff --git a/evidence/migrations.txt b/evidence/migrations.txt new file mode 100644 index 00000000..092ac77e --- /dev/null +++ b/evidence/migrations.txt @@ -0,0 +1,54 @@ +$ docker compose logs --no-log-prefix api | grep -E 'миграци|Схема БД' +2026-10-11 14:40:39.798 | INFO | infrastructure.dependencies.database:apply_migrations:156 - Схема БД в актуальной версии +2026-10-11 14:40:48.046 | INFO | infrastructure.dependencies.database:apply_migrations:156 - Схема БД в актуальной версии + +$ docker compose exec -T postgres sh -c 'psql -X -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT * FROM schema_migrations"' + version | name | applied_at +---------+-----------+------------------------------- + 1 | 0001_init | 2026-10-04 13:01:32.330387+00 +(1 row) + + +$ docker compose exec -T postgres sh -c 'psql -X -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "\d+ messages"' + Table "public.messages" + Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description +------------+--------------------------+-----------+----------+----------+----------+-------------+--------------+------------- + id | uuid | | not null | uuidv7() | plain | | | + chat_id | uuid | | not null | | plain | | | + sender_id | uuid | | not null | | plain | | | + text | text | | not null | | extended | | | + created_at | timestamp with time zone | | not null | | plain | | | +Indexes: + "messages_pkey" PRIMARY KEY, btree (id) + "messages_chat_id_created_at_id_idx" btree (chat_id, created_at, id) +Check constraints: + "messages_text_len" CHECK (char_length(text) >= 1 AND char_length(text) <= 4000) +Foreign-key constraints: + "messages_sender_member_fkey" FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id) ON DELETE CASCADE +Not-null constraints: + "messages_id_not_null" NOT NULL "id" + "messages_chat_id_not_null" NOT NULL "chat_id" + "messages_sender_id_not_null" NOT NULL "sender_id" + "messages_text_not_null" NOT NULL "text" + "messages_created_at_not_null" NOT NULL "created_at" +Access method: heap + + +$ docker compose exec -T postgres sh -c 'psql -X -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "\di"' + List of indexes + Schema | Name | Type | Owner | Table +--------+------------------------------------+-------+-----------+------------------- + public | chat_members_pkey | index | messenger | chat_members + public | chat_members_position_key | index | messenger | chat_members + public | chat_members_user_id_chat_id_idx | index | messenger | chat_members + public | chats_pkey | index | messenger | chats + public | messages_chat_id_created_at_id_idx | index | messenger | messages + public | messages_pkey | index | messenger | messages + public | schema_migrations_pkey | index | messenger | schema_migrations + public | sessions_pkey | index | messenger | sessions + public | sessions_user_id_idx | index | messenger | sessions + public | users_pkey | index | messenger | users + public | users_username_key | index | messenger | users +(11 rows) + + diff --git a/evidence/migrations_from_zero.txt b/evidence/migrations_from_zero.txt new file mode 100644 index 00000000..1804678a --- /dev/null +++ b/evidence/migrations_from_zero.txt @@ -0,0 +1,15 @@ +$ make test-migrations +./scripts/check_migrations.sh + Network messenger-lab2-migrations-check_default Creating + Network messenger-lab2-migrations-check_default Created +2026-10-11 14:40:55.908 | INFO | infrastructure.dependencies.database:create_pool:69 - Установка соединения с базой данных psql +2026-10-11 14:40:55.938 | INFO | infrastructure.dependencies.database:create_pool:99 - Соединение с базой данных psql установлено. DB_NAME: messenger, pool: 2..10 +пустой volume: таблиц в public = 0 +2026-10-11 14:40:55.960 | INFO | infrastructure.dependencies.database:apply_migrations:151 - Применена миграция 0001_init.sql +2026-10-11 14:40:55.961 | INFO | infrastructure.dependencies.database:apply_migrations:156 - Схема БД в актуальной версии +запуск 1: schema_migrations = [(1, '0001_init')], таблиц = 6 +2026-10-11 14:40:55.962 | INFO | infrastructure.dependencies.database:apply_migrations:156 - Схема БД в актуальной версии +запуск 2: schema_migrations = [(1, '0001_init')], таблиц = 6 +2026-10-11 14:40:55.963 | INFO | infrastructure.dependencies.database:apply_migrations:156 - Схема БД в актуальной версии +запуск 3: schema_migrations = [(1, '0001_init')], таблиц = 6 + diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..9a54679a --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,23 @@ +$ docker inspect -f '{{.Name}} {{.State.StartedAt}}' messenger-lab2-api-1 messenger-lab2-postgres-1 messenger-lab2-valkey-1 +/messenger-lab2-api-1 2026-10-11T14:40:38.947015458Z +/messenger-lab2-postgres-1 2026-10-11T12:57:22.164526672Z +/messenger-lab2-valkey-1 2026-10-11T12:57:22.228188756Z + +$ docker volume ls --filter name=messenger-lab2 +DRIVER VOLUME NAME +local messenger-lab2_postgres_data + +$ make test-persistence ACTION_SCRIPT=scripts/restart.sh +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +$ docker inspect -f '{{.Name}} {{.State.StartedAt}}' messenger-lab2-api-1 messenger-lab2-postgres-1 messenger-lab2-valkey-1 +/messenger-lab2-api-1 2026-10-11T14:40:47.267238587Z +/messenger-lab2-postgres-1 2026-10-11T14:40:46.679260379Z +/messenger-lab2-valkey-1 2026-10-11T14:40:46.639763379Z + +$ docker volume ls --filter name=messenger-lab2 +DRIVER VOLUME NAME +local messenger-lab2_postgres_data + diff --git a/evidence/race_rollback.txt b/evidence/race_rollback.txt new file mode 100644 index 00000000..f4199fb7 --- /dev/null +++ b/evidence/race_rollback.txt @@ -0,0 +1,22 @@ +$ poetry run pytest student-tests/test_transactions.py student-tests/test_concurrency.py -s +/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 + warnings.warn( +============================= test session starts ============================== +platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-TbR4RJXC-py3.12/bin/python +cachedir: .pytest_cache +rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab2 +configfile: pyproject.toml +plugins: anyio-4.15.1 +collecting ... collected 4 items + +student-tests/test_transactions.py::test_same_username_race_creates_exactly_one_user +REGISTRATION RACE: {"requests": 20, "status_201": 1, "status_409": 19, "rows_in_db": 1} +PASSED +student-tests/test_transactions.py::test_failed_chat_creation_is_rolled_back PASSED +student-tests/test_transactions.py::test_successful_chat_creation_commits_all_members PASSED +student-tests/test_concurrency.py::test_parallel_messages_are_not_lost_or_duplicated +CONCURRENCY RESULT: {"clients": 10, "messages_sent": 100, "status_201": 100, "messages_read": 100, "unique_ids": 100, "unique_texts": 100, "pages_read": 4, "send_seconds": 0.275} +PASSED + +============================== 4 passed in 1.95s =============================== + diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..8703eb2f --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,8 @@ +$ make check +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. + +$ make test +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. + diff --git a/evidence/student_tests.txt b/evidence/student_tests.txt new file mode 100644 index 00000000..52434f7e --- /dev/null +++ b/evidence/student_tests.txt @@ -0,0 +1,109 @@ +$ make student-test +BASE_URL="http://localhost:8080" poetry run pytest +/Users/enterxx/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 + warnings.warn( +============================= test session starts ============================== +platform darwin -- Python 3.12.12, pytest-9.1.1, pluggy-1.6.0 -- /Users/enterxx/Library/Caches/pypoetry/virtualenvs/max2-TbR4RJXC-py3.12/bin/python +cachedir: .pytest_cache +rootdir: /Users/enterxx/Desktop/university/4course/mtusi2026lab2 +configfile: pyproject.toml +testpaths: student-tests +plugins: anyio-4.15.1 +collecting ... collected 93 items + +student-tests/test_auth.py::test_login_returns_opaque_bearer_session PASSED [ 1%] +student-tests/test_auth.py::test_wrong_password_and_unknown_user_are_indistinguishable PASSED [ 2%] +student-tests/test_auth.py::test_malformed_basic_is_unauthorized[None] PASSED [ 3%] +student-tests/test_auth.py::test_malformed_basic_is_unauthorized[] PASSED [ 4%] +student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic] PASSED [ 5%] +student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic !!!not-base64!!!] PASSED [ 6%] +student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic bm8tY29sb24taGVyZQ==] PASSED [ 7%] +student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Basic //46cGFzc3dvcmQ=] PASSED [ 8%] +student-tests/test_auth.py::test_malformed_basic_is_unauthorized[Bearer some-token] PASSED [ 9%] +student-tests/test_auth.py::test_utf8_password_with_colon PASSED [ 10%] +student-tests/test_auth.py::test_basic_is_rejected_on_business_endpoints PASSED [ 11%] +student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 12%] +student-tests/test_auth.py::test_logout_without_token PASSED [ 13%] +student-tests/test_auth.py::test_relogin_after_logout PASSED [ 15%] +student-tests/test_auth.py::test_password_is_not_stored_in_plaintext PASSED [ 16%] +student-tests/test_concurrency.py::test_parallel_messages_are_not_lost_or_duplicated PASSED [ 17%] +student-tests/test_dependency_failures.py::test_valkey_down_cache_is_bypassed SKIPPED [ 18%] +student-tests/test_dependency_failures.py::test_logout_during_valkey_outage_is_not_resurrected SKIPPED [ 19%] +student-tests/test_dependency_failures.py::test_postgres_down_returns_bounded_503_and_no_anonymous_access SKIPPED [ 20%] +student-tests/test_dependency_failures.py::test_session_survives_postgres_restart SKIPPED [ 21%] +student-tests/test_expiry.py::test_session_expires_after_short_ttl SKIPPED [ 22%] +student-tests/test_expiry.py::test_expires_at_matches_short_ttl SKIPPED [ 23%] +student-tests/test_foreign_chat.py::test_outsider_cannot_access_chat PASSED [ 24%] +student-tests/test_foreign_chat.py::test_foreign_and_missing_chat_are_indistinguishable PASSED [ 25%] +student-tests/test_foreign_chat.py::test_chat_list_contains_only_own_chats PASSED [ 26%] +student-tests/test_health.py::test_health_endpoints_have_request_id PASSED [ 27%] +student-tests/test_health.py::test_unknown_route_uses_error_format PASSED [ 29%] +student-tests/test_invalid_json.py::test_malformed_json_on_register[{"username": "abc",] PASSED [ 30%] +student-tests/test_invalid_json.py::test_malformed_json_on_register[not json at all] PASSED [ 31%] +student-tests/test_invalid_json.py::test_malformed_json_on_register[{"text": "\xff\xfe"}] PASSED [ 32%] +student-tests/test_invalid_json.py::test_malformed_json_on_register[] PASSED [ 33%] +student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[{"username": "abc",] PASSED [ 34%] +student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[not json at all] PASSED [ 35%] +student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[{"text": "\xff\xfe"}] PASSED [ 36%] +student-tests/test_invalid_json.py::test_malformed_json_on_create_chat_and_message[] PASSED [ 37%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body0] PASSED [ 38%] +student-tests/test_invalid_json.py::test_invalid_user_payload[alice] PASSED [ 39%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body2] PASSED [ 40%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body3] PASSED [ 41%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body4] PASSED [ 43%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body5] PASSED [ 44%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body6] PASSED [ 45%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body7] PASSED [ 46%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body8] PASSED [ 47%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body9] PASSED [ 48%] +student-tests/test_invalid_json.py::test_invalid_user_payload[body10] PASSED [ 49%] +student-tests/test_invalid_json.py::test_invalid_message_payload[body0] PASSED [ 50%] +student-tests/test_invalid_json.py::test_invalid_message_payload[body1] PASSED [ 51%] +student-tests/test_invalid_json.py::test_invalid_message_payload[body2] PASSED [ 52%] +student-tests/test_invalid_json.py::test_invalid_chat_payload PASSED [ 53%] +student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[] PASSED [ 54%] +student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[ ] PASSED [ 55%] +student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[ \n\t ] PASSED [ 56%] +student-tests/test_messages.py::test_empty_or_blank_message_is_rejected[\u3000] PASSED [ 58%] +student-tests/test_messages.py::test_message_length_is_counted_in_code_points PASSED [ 59%] +student-tests/test_messages.py::test_unicode_is_preserved PASSED [ 60%] +student-tests/test_messages.py::test_nul_character_is_rejected PASSED [ 61%] +student-tests/test_messages.py::test_sender_is_taken_from_session PASSED [ 62%] +student-tests/test_pagination.py::test_walk_message_pages[7-3-3] PASSED [ 63%] +student-tests/test_pagination.py::test_walk_message_pages[6-3-2] PASSED [ 64%] +student-tests/test_pagination.py::test_walk_message_pages[5-1-5] PASSED [ 65%] +student-tests/test_pagination.py::test_walk_message_pages[3-100-1] PASSED [ 66%] +student-tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 67%] +student-tests/test_pagination.py::test_new_message_appears_after_cursor PASSED [ 68%] +student-tests/test_pagination.py::test_cursor_is_bound_to_chat PASSED [ 69%] +student-tests/test_pagination.py::test_invalid_limit_or_cursor[params0] PASSED [ 70%] +student-tests/test_pagination.py::test_invalid_limit_or_cursor[params1] PASSED [ 72%] +student-tests/test_pagination.py::test_invalid_limit_or_cursor[params2] PASSED [ 73%] +student-tests/test_pagination.py::test_invalid_limit_or_cursor[params3] PASSED [ 74%] +student-tests/test_pagination.py::test_invalid_limit_or_cursor[params4] PASSED [ 75%] +student-tests/test_pagination.py::test_invalid_limit_or_cursor[params5] PASSED [ 76%] +student-tests/test_pagination.py::test_walk_chat_list_pages PASSED [ 77%] +student-tests/test_pagination.py::test_chat_cursor_is_bound_to_user PASSED [ 78%] +student-tests/test_transactions.py::test_same_username_race_creates_exactly_one_user PASSED [ 79%] +student-tests/test_transactions.py::test_failed_chat_creation_is_rolled_back PASSED [ 80%] +student-tests/test_transactions.py::test_successful_chat_creation_commits_all_members PASSED [ 81%] +student-tests/test_users.py::test_duplicate_username_is_conflict PASSED [ 82%] +student-tests/test_users.py::test_registration_never_returns_password PASSED [ 83%] +student-tests/test_users.py::test_invalid_password_is_rejected[None] PASSED [ 84%] +student-tests/test_users.py::test_invalid_password_is_rejected[] PASSED [ 86%] +student-tests/test_users.py::test_invalid_password_is_rejected[\u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439] PASSED [ 87%] +student-tests/test_users.py::test_invalid_password_is_rejected[xxxxxxxxxxx] PASSED [ 88%] +student-tests/test_users.py::test_invalid_password_is_rejected[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 89%] +student-tests/test_users.py::test_invalid_password_is_rejected[123456789012] PASSED [ 90%] +student-tests/test_users.py::test_password_length_is_counted_in_code_points PASSED [ 91%] +student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers0] PASSED [ 92%] +student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers1] PASSED [ 93%] +student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers2] PASSED [ 94%] +student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers3] PASSED [ 95%] +student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers4] PASSED [ 96%] +student-tests/test_users.py::test_missing_or_unknown_token_is_unauthorized[headers5] PASSED [ 97%] +student-tests/test_users.py::test_x_user_id_gives_no_access PASSED [ 98%] +student-tests/test_users.py::test_current_user PASSED [100%] + +======================== 87 passed, 6 skipped in 7.11s ========================= + diff --git a/migrations/0001_init.sql b/migrations/0001_init.sql new file mode 100644 index 00000000..e9a91a3a --- /dev/null +++ b/migrations/0001_init.sql @@ -0,0 +1,60 @@ +CREATE TABLE users ( + id uuid PRIMARY KEY DEFAULT uuidv7(), + username text NOT NULL, + display_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + CONSTRAINT users_username_key UNIQUE (username), + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100) +); + +-- Источник истины для сессий; токен хранится только как SHA-256 +CREATE TABLE sessions ( + token_hash bytea PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + expires_at timestamptz NOT NULL, + revoked_at timestamptz, + CONSTRAINT sessions_token_hash_len CHECK (octet_length(token_hash) = 32), + CONSTRAINT sessions_expiry_order CHECK (expires_at > created_at) +); + +CREATE INDEX sessions_user_id_idx ON sessions (user_id); + +CREATE TABLE chats ( + id uuid PRIMARY KEY DEFAULT uuidv7(), + title text NOT NULL, + created_by uuid NOT NULL REFERENCES users (id), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100) +); + +-- position сохраняет порядок member_ids из запроса создания +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users (id), + position smallint NOT NULL, + PRIMARY KEY (chat_id, user_id), + CONSTRAINT chat_members_position_key UNIQUE (chat_id, position) +); + +-- Список чатов пользователя: WHERE user_id = $1 +CREATE INDEX chat_members_user_id_chat_id_idx ON chat_members (user_id, chat_id); + +-- created_at назначает приложение под блокировкой чата (строго возрастает в чате) +CREATE TABLE messages ( + id uuid PRIMARY KEY DEFAULT uuidv7(), + chat_id uuid NOT NULL, + sender_id uuid NOT NULL, + text text NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000), + -- отправитель обязан быть участником этого чата + CONSTRAINT messages_sender_member_fkey FOREIGN KEY (chat_id, sender_id) + REFERENCES chat_members (chat_id, user_id) ON DELETE CASCADE +); + +-- Keyset-пагинация истории: WHERE chat_id = $1 AND (created_at, id) > ($2, $3) +-- ORDER BY created_at, id LIMIT $4 — Index Scan без сортировки и полного прохода +CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id); diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 00000000..d7cbba00 --- /dev/null +++ b/poetry.lock @@ -0,0 +1,829 @@ +# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand. + +[[package]] +name = "annotated-doc" +version = "0.0.5" +description = "Document parameters, class attributes, return types, and variables inline, with Annotated." +optional = false +python-versions = ">=3.9" +groups = ["main"] +files = [ + {file = "annotated_doc-0.0.5-py3-none-any.whl", hash = "sha256:117bac03a25ede5df5440e855b32d556049ca169ead221505badf432fed4b101"}, + {file = "annotated_doc-0.0.5.tar.gz", hash = "sha256:c7e58ce09192557605d8bbd92836d7e1d520ac9580096042c0bfd197efacf1bb"}, +] + +[[package]] +name = "annotated-types" +version = "0.8.0" +description = "Reusable constraint types to use with typing.Annotated" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "annotated_types-0.8.0-py3-none-any.whl", hash = "sha256:f072f4d804ea359e4eaf198b1af7a8b0943881a87f31bb764f8bf219bb9419e0"}, + {file = "annotated_types-0.8.0.tar.gz", hash = "sha256:13b2beaad985e05e2d6407ee4c4f35590b11f8d693a258a561055cac8f64cab7"}, +] + +[[package]] +name = "anyio" +version = "4.15.1" +description = "High-level concurrency and networking framework on top of asyncio or Trio" +optional = false +python-versions = ">=3.10" +groups = ["main", "dev"] +files = [ + {file = "anyio-4.15.1-py3-none-any.whl", hash = "sha256:6152fdbbf9a77fdec97731721bebf7c4c44f7c29b424b0065826173efc7ed101"}, + {file = "anyio-4.15.1.tar.gz", hash = "sha256:9f28306018cbd6d329e64a36d58256edff76dd996fe423bc957326e578b82a94"}, +] + +[package.dependencies] +idna = ">=2.8" +typing_extensions = {version = ">=4.16.0", markers = "python_version < \"3.15\""} + +[package.extras] +trio = ["trio (>=0.32.0)"] + +[[package]] +name = "argon2-cffi" +version = "25.1.0" +description = "Argon2 for Python" +optional = false +python-versions = ">=3.8" +groups = ["main"] +files = [ + {file = "argon2_cffi-25.1.0-py3-none-any.whl", hash = "sha256:fdc8b074db390fccb6eb4a3604ae7231f219aa669a2652e0f20e16ba513d5741"}, + {file = "argon2_cffi-25.1.0.tar.gz", hash = "sha256:694ae5cc8a42f4c4e2bf2ca0e64e51e23a040c6a517a85074683d3959e1346c1"}, +] + +[package.dependencies] +argon2-cffi-bindings = "*" + +[[package]] +name = "argon2-cffi-bindings" +version = "26.1.0" +description = "Low-level CFFI bindings for Argon2" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:21ca0396fe5ec995dd54431c32698189666f9224810acfa752e50d2bd94d9df2"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:78de2d65e0b9ea7ce9d1b1c3e87297b2d7305a02c266ee2a2d6910daddd7ee69"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:27f1821903e2ceadcb88ec2b45ef190897b7682449c772f4d9b53e42c520cf29"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:d88e5f7e60f28ae0b0cc6b2f16c43e87cd642a196a86f85e0d8bb6fe016fc16d"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:34b7d9c24a4165a2c61cc8ae11d44d48c9ce2830fb536cb7914e11fdd9962728"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-musllinux_1_2_riscv64.whl", hash = "sha256:224865cbbcb7a2bd1356741dff12b0134df726b6d44bb7b500df8e303cbd9e81"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:ffff613aaa9ce6236766e2fc6dc560bb5abde7a2e2416e3db1f9ae395a2b4dd4"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-win32.whl", hash = "sha256:a86c069c91a747a2c4e5c51473590aeb48172fff9b2130d23729a42d98665ecb"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-win_amd64.whl", hash = "sha256:2c36ff87b5dfaa477d0bd51e9d7f6abdae7c8955d2983c97419085d842154b3e"}, + {file = "argon2_cffi_bindings-26.1.0-cp310-abi3-win_arm64.whl", hash = "sha256:f9c4420a7a864fe1b86ce35befc95b8e39fb852493b81cf798671ddc265de638"}, + {file = "argon2_cffi_bindings-26.1.0-cp313-cp313-pyemscripten_2025_0_wasm32.whl", hash = "sha256:af11ac37a7c53dc16cb7950a6190851b0870fe218b6c60c0bb7ac355234e3083"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314-pyemscripten_2026_0_wasm32.whl", hash = "sha256:db0fcd827ca61622a01b220aadfbece01939acf53888f2cb98cd93e9b1e2c97e"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:28524438cd3e723f25412f63d4fd516ff5bae9ae5aa56acbe2a1404398a0cf31"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ac82fc756a446b6ccd7139ce70efa9d8bbe541e7ad579a12dcb52764b7175c5f"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:6a4e68eed961a8de6928d1c17ff3dc2a547e0e923c17f8f1cd79fb7bc9502f98"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:151dfaad9de753f4af2a7854e707e4784f2acc434340ade64239c5b104b2d605"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:061a6919145bbf282ebf1f9c59d3135d4833c25313c8595c0d68cf7712ddfce2"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-musllinux_1_2_riscv64.whl", hash = "sha256:62ff20cd130c956c7c9144d5fe35228f98b51c579b2439e988b27ef93e16c02a"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:19423e5d7ac1cc354baab59eaabf18db2ec04ef6593b5abe5a34f323c4a8f87a"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-win32.whl", hash = "sha256:4f84cdd868978d7b7350a566c254042d44216d9e37f241f3a6d3b1dfebeede35"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-win_amd64.whl", hash = "sha256:2b741888c93147444fdfc851abd81cc207f37f7f7da42062a00deb3888e57da8"}, + {file = "argon2_cffi_bindings-26.1.0-cp314-cp314t-win_arm64.whl", hash = "sha256:6ab674f668d5962a3a4136ae0812519b0f1586874263723a32181d60d64137e1"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:1d98e33bd8bd67d7206c124e200bf2229c4cfa8c9c19f7b44a897f0fc71837eb"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ccaf0a46cbb380f1fd102a874e32aa629fd3cb0c0e94f4943fa1f6d5edc5dac6"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f0c3103fcff20183e593459cfea6e012281c0e76ae3ed8b5565ad1b92eac3990"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-manylinux_2_34_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:c49e853a3bef9dd10329f31f702e7fa9b5c58229ff9c2ff6d069efaf09177c08"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:6376d4b3aca039375ca8bf92f770da0ec424a1ce3a37077a8d3c557411aa56ca"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-musllinux_1_2_riscv64.whl", hash = "sha256:9bacedc04b0402837586a17f0919e3dfdd95291f441f1f56bd80ec274c2840a1"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:76ae29acace5d33355344612844d588e19deaaba4639d8bb01601e4b1418ef36"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-win32.whl", hash = "sha256:df612391feca41c44d20118f3b88d1b86419465cd1f5496859f715ca60ec2210"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-win_amd64.whl", hash = "sha256:1a0a29ed86960e44eaace7e081bdfab4f08b012fd96ec8edba71e2ad020939e4"}, + {file = "argon2_cffi_bindings-26.1.0-cp315-cp315t-win_arm64.whl", hash = "sha256:d157ddfab1e8b21f2f1dedda9c09645d98b5ed0b667b0626be600a345d426440"}, + {file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-macosx_11_0_arm64.whl", hash = "sha256:7014ab7e6f5d8511af92544667a0346ea6dfc314ea9a7cad1dba9fdb5c9a6e33"}, + {file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:242bb0cda2ae3650764fc194593d9ea45fc9e72729acd89778c7cfe184cec2a5"}, + {file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b70225b5fd1e0d2ef4f7fd30d24658454535f0924dff0caca5dc08efbbbadfbb"}, + {file = "argon2_cffi_bindings-26.1.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:1af817e84578ef8b7295ad17de0f9896e4c8520dbf2233c7aa5aa3d487256fc4"}, + {file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:19b562b1de4b9052ef1214a2821c44b6e6f22945daa102c32ae4eff929d8b6d8"}, + {file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:49d525938467d52c923a890153c99087c9d5a937d1f6b585dbdba34ec82e397a"}, + {file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1b0bcac4d490a237e18cf91f57352920c29f77f2fa39efd0813fb81298bf17ba"}, + {file = "argon2_cffi_bindings-26.1.0-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:0cc40f7b4050bb93eb67de95d2d759322fc7ce4930b9d645581ecf4913ec651e"}, + {file = "argon2_cffi_bindings-26.1.0.tar.gz", hash = "sha256:63505c71542a44b68b1e38060450fb006404170da375feb31af153e7f9c6205d"}, +] + +[package.dependencies] +cffi = {version = ">=1.0.1", markers = "python_version < \"3.14\""} + +[[package]] +name = "asyncpg" +version = "0.31.0" +description = "An asyncio PostgreSQL driver" +optional = false +python-versions = ">=3.9.0" +groups = ["main"] +files = [ + {file = "asyncpg-0.31.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:831712dd3cf117eec68575a9b50da711893fd63ebe277fc155ecae1c6c9f0f61"}, + {file = "asyncpg-0.31.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:0b17c89312c2f4ccea222a3a6571f7df65d4ba2c0e803339bfc7bed46a96d3be"}, + {file = "asyncpg-0.31.0-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3faa62f997db0c9add34504a68ac2c342cfee4d57a0c3062fcf0d86c7f9cb1e8"}, + {file = "asyncpg-0.31.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8ea599d45c361dfbf398cb67da7fd052affa556a401482d3ff1ee99bd68808a1"}, + {file = "asyncpg-0.31.0-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:795416369c3d284e1837461909f58418ad22b305f955e625a4b3a2521d80a5f3"}, + {file = "asyncpg-0.31.0-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:a8d758dac9d2e723e173d286ef5e574f0b350ec00e9186fce84d0fc5f6a8e6b8"}, + {file = "asyncpg-0.31.0-cp310-cp310-win32.whl", hash = "sha256:2d076d42eb583601179efa246c5d7ae44614b4144bc1c7a683ad1222814ed095"}, + {file = "asyncpg-0.31.0-cp310-cp310-win_amd64.whl", hash = "sha256:9ea33213ac044171f4cac23740bed9a3805abae10e7025314cfbd725ec670540"}, + {file = "asyncpg-0.31.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:eee690960e8ab85063ba93af2ce128c0f52fd655fdff9fdb1a28df01329f031d"}, + {file = "asyncpg-0.31.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:2657204552b75f8288de08ca60faf4a99a65deef3a71d1467454123205a88fab"}, + {file = "asyncpg-0.31.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:a429e842a3a4b4ea240ea52d7fe3f82d5149853249306f7ff166cb9948faa46c"}, + {file = "asyncpg-0.31.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c0807be46c32c963ae40d329b3a686356e417f674c976c07fa49f1b30303f109"}, + {file = "asyncpg-0.31.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:e5d5098f63beeae93512ee513d4c0c53dc12e9aa2b7a1af5a81cddf93fe4e4da"}, + {file = "asyncpg-0.31.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:37fc6c00a814e18eef51833545d1891cac9aa69140598bb076b4cd29b3e010b9"}, + {file = "asyncpg-0.31.0-cp311-cp311-win32.whl", hash = "sha256:5a4af56edf82a701aece93190cc4e094d2df7d33f6e915c222fb09efbb5afc24"}, + {file = "asyncpg-0.31.0-cp311-cp311-win_amd64.whl", hash = "sha256:480c4befbdf079c14c9ca43c8c5e1fe8b6296c96f1f927158d4f1e750aacc047"}, + {file = "asyncpg-0.31.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:b44c31e1efc1c15188ef183f287c728e2046abb1d26af4d20858215d50d91fad"}, + {file = "asyncpg-0.31.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:0c89ccf741c067614c9b5fc7f1fc6f3b61ab05ae4aaa966e6fd6b93097c7d20d"}, + {file = "asyncpg-0.31.0-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:12b3b2e39dc5470abd5e98c8d3373e4b1d1234d9fbdedf538798b2c13c64460a"}, + {file = "asyncpg-0.31.0-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:aad7a33913fb8bcb5454313377cc330fbb19a0cd5faa7272407d8a0c4257b671"}, + {file = "asyncpg-0.31.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:3df118d94f46d85b2e434fd62c84cb66d5834d5a890725fe625f498e72e4d5ec"}, + {file = "asyncpg-0.31.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:bd5b6efff3c17c3202d4b37189969acf8927438a238c6257f66be3c426beba20"}, + {file = "asyncpg-0.31.0-cp312-cp312-win32.whl", hash = "sha256:027eaa61361ec735926566f995d959ade4796f6a49d3bde17e5134b9964f9ba8"}, + {file = "asyncpg-0.31.0-cp312-cp312-win_amd64.whl", hash = "sha256:72d6bdcbc93d608a1158f17932de2321f68b1a967a13e014998db87a72ed3186"}, + {file = "asyncpg-0.31.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:c204fab1b91e08b0f47e90a75d1b3c62174dab21f670ad6c5d0f243a228f015b"}, + {file = "asyncpg-0.31.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:54a64f91839ba59008eccf7aad2e93d6e3de688d796f35803235ea1c4898ae1e"}, + {file = "asyncpg-0.31.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c0e0822b1038dc7253b337b0f3f676cadc4ac31b126c5d42691c39691962e403"}, + {file = "asyncpg-0.31.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:bef056aa502ee34204c161c72ca1f3c274917596877f825968368b2c33f585f4"}, + {file = "asyncpg-0.31.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:0bfbcc5b7ffcd9b75ab1558f00db2ae07db9c80637ad1b2469c43df79d7a5ae2"}, + {file = "asyncpg-0.31.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:22bc525ebbdc24d1261ecbf6f504998244d4e3be1721784b5f64664d61fbe602"}, + {file = "asyncpg-0.31.0-cp313-cp313-win32.whl", hash = "sha256:f890de5e1e4f7e14023619399a471ce4b71f5418cd67a51853b9910fdfa73696"}, + {file = "asyncpg-0.31.0-cp313-cp313-win_amd64.whl", hash = "sha256:dc5f2fa9916f292e5c5c8b2ac2813763bcd7f58e130055b4ad8a0531314201ab"}, + {file = "asyncpg-0.31.0-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:f6b56b91bb0ffc328c4e3ed113136cddd9deefdf5f79ab448598b9772831df44"}, + {file = "asyncpg-0.31.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:334dec28cf20d7f5bb9e45b39546ddf247f8042a690bff9b9573d00086e69cb5"}, + {file = "asyncpg-0.31.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:98cc158c53f46de7bb677fd20c417e264fc02b36d901cc2a43bd6cb0dc6dbfd2"}, + {file = "asyncpg-0.31.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9322b563e2661a52e3cdbc93eed3be7748b289f792e0011cb2720d278b366ce2"}, + {file = "asyncpg-0.31.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:19857a358fc811d82227449b7ca40afb46e75b33eb8897240c3839dd8b744218"}, + {file = "asyncpg-0.31.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:ba5f8886e850882ff2c2ace5732300e99193823e8107e2c53ef01c1ebfa1e85d"}, + {file = "asyncpg-0.31.0-cp314-cp314-win32.whl", hash = "sha256:cea3a0b2a14f95834cee29432e4ddc399b95700eb1d51bbc5bfee8f31fa07b2b"}, + {file = "asyncpg-0.31.0-cp314-cp314-win_amd64.whl", hash = "sha256:04d19392716af6b029411a0264d92093b6e5e8285ae97a39957b9a9c14ea72be"}, + {file = "asyncpg-0.31.0-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:bdb957706da132e982cc6856bb2f7b740603472b54c3ebc77fe60ea3e57e1bd2"}, + {file = "asyncpg-0.31.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:6d11b198111a72f47154fa03b85799f9be63701e068b43f84ac25da0bda9cb31"}, + {file = "asyncpg-0.31.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:18c83b03bc0d1b23e6230f5bf8d4f217dc9bc08644ce0502a9d91dc9e634a9c7"}, + {file = "asyncpg-0.31.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e009abc333464ff18b8f6fd146addffd9aaf63e79aa3bb40ab7a4c332d0c5e9e"}, + {file = "asyncpg-0.31.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:3b1fbcb0e396a5ca435a8826a87e5c2c2cc0c8c68eb6fadf82168056b0e53a8c"}, + {file = "asyncpg-0.31.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:8df714dba348efcc162d2adf02d213e5fab1bd9f557e1305633e851a61814a7a"}, + {file = "asyncpg-0.31.0-cp314-cp314t-win32.whl", hash = "sha256:1b41f1afb1033f2b44f3234993b15096ddc9cd71b21a42dbd87fc6a57b43d65d"}, + {file = "asyncpg-0.31.0-cp314-cp314t-win_amd64.whl", hash = "sha256:bd4107bb7cdd0e9e65fae66a62afd3a249663b844fa34d479f6d5b3bef9c04c3"}, + {file = "asyncpg-0.31.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:ebb3cde58321a1f89ce41812be3f2a98dddedc1e76d0838aba1d724f1e4e1a95"}, + {file = "asyncpg-0.31.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:e6974f36eb9a224d8fb428bcf66bd411aa12cf57c2967463178149e73d4de366"}, + {file = "asyncpg-0.31.0-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:bc2b685f400ceae428f79f78b58110470d7b4466929a7f78d455964b17ad1008"}, + {file = "asyncpg-0.31.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:bb223567dea5f47c45d347f2bde5486be8d9f40339f27217adb3fb1c3be51298"}, + {file = "asyncpg-0.31.0-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:22be6e02381bab3101cd502d9297ac71e2f966c86e20e78caead9934c98a8af6"}, + {file = "asyncpg-0.31.0-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:37a58919cfef2448a920df00d1b2f821762d17194d0dbf355d6dde8d952c04f9"}, + {file = "asyncpg-0.31.0-cp39-cp39-win32.whl", hash = "sha256:c1a9c5b71d2371a2290bc93336cd05ba4ec781683cab292adbddc084f89443c6"}, + {file = "asyncpg-0.31.0-cp39-cp39-win_amd64.whl", hash = "sha256:c1e1ab5bc65373d92dd749d7308c5b26fb2dc0fbe5d3bf68a32b676aa3bcd24a"}, + {file = "asyncpg-0.31.0.tar.gz", hash = "sha256:c989386c83940bfbd787180f2b1519415e2d3d6277a70d9d0f0145ac73500735"}, +] + +[package.extras] +gssauth = ["gssapi ; platform_system != \"Windows\"", "sspilib ; platform_system == \"Windows\""] + +[[package]] +name = "certifi" +version = "2026.7.22" +description = "Python package for providing Mozilla's CA Bundle." +optional = false +python-versions = ">=3.7" +groups = ["dev"] +files = [ + {file = "certifi-2026.7.22-py3-none-any.whl", hash = "sha256:62f22742b58a1a33014a2b6b706588a8d7e2a88ae7bd1a6ebe8c992928483775"}, + {file = "certifi-2026.7.22.tar.gz", hash = "sha256:741e2c3b351ddf169a738da9f2c048608ff7f2c5cc02f1ebc6b118bb090d5d55"}, +] + +[[package]] +name = "cffi" +version = "2.1.1" +description = "Foreign Function Interface for Python calling C code." +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "cffi-2.1.1-cp310-cp310-macosx_10_15_x86_64.whl", hash = "sha256:baed1e86cc735622097354b9d1281406caf42ff42a886d29faa8e8d1630333be"}, + {file = "cffi-2.1.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ca82be1a1d406ecfe1d25dc16cb33488e5a16bf4438c9fb590484ea29d92478b"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:42e2f76b9455f5a9a844f770bf3e200ed3da0e15f5df3db9c31fe80b04b3d004"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:5a59cc1c4442bc3d5c703bf720b51138d0bfc173618807c9ee2490a7541dd3d9"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:9f8d177621de5cb38ee3e731eda45d421db093ec0739f46a5594babda7987a98"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:75f80557d1389eddbd0de2681f6a390a0c5338c31ddaa821381c203fc3fd50d9"}, + {file = "cffi-2.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:194cffa889098ced9976c3fc6340305e43f6303657d298da55366907c05c22d6"}, + {file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:5bb4e7ea95dcd6a014a6fef62e62467d67d8e582326443f3d68e71d6320a9fcf"}, + {file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:3d22a20b1fb1632cc72c22f95f7b0d2961c3e1c235f245ba4c606c4771035659"}, + {file = "cffi-2.1.1-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:1dea0e4d7d4f11f619fe8c1d76caf49e24405b4b5743c0e3be16a500ecd930c9"}, + {file = "cffi-2.1.1-cp310-cp310-win32.whl", hash = "sha256:7ce713ace7c0e4520535b42b77eaa742c16dab813978064913e5a3cf82973b41"}, + {file = "cffi-2.1.1-cp310-cp310-win_amd64.whl", hash = "sha256:a48d62ab9d6f4f98c983223a547af44be6ca3691074c31cecced6facd3ba2dc1"}, + {file = "cffi-2.1.1-cp311-cp311-macosx_10_15_x86_64.whl", hash = "sha256:c8d2c9fd1f2d16f780d15127abb050d13d1a76c03a4bd87d7e4980e45e511e12"}, + {file = "cffi-2.1.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:398aff33cee2767e3e781d2554c54bd0dff386bb437581e0d8011fde1a942ec1"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:154852545011f779917b11c78db2358d095da62a9a172b78ad0a583ee5adc0d0"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:3311ed60d36f83378794e1009ac6258bafbf81f7888b4caa7b35a521e3f95813"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:6e192623c49c94421616a5778fba35cf0d5a8d000650c1967ef4448ee5cdd990"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a6e721d4b0e45d5b65e87534470e67b18dcd092c83f68fba09f152b9cbc061af"}, + {file = "cffi-2.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:34e261f78cb6ceaaa36f42f2613f4380d94d9c759a9c73c769ee6e0247364632"}, + {file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:7225e4514edb64eb6740324353e0da0711954fd8d7da4576755b1c6e09b697cd"}, + {file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:df913725b79db7bcf03448f36b7bf8815363417d5b58deecf9305e3e30f0f21a"}, + {file = "cffi-2.1.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:f5cfbc5fe74540d335175b656c725d74d90e3730c626d92575eea35029d9afaa"}, + {file = "cffi-2.1.1-cp311-cp311-win32.whl", hash = "sha256:f8ec5e643a9a937f64e1999eb9f75d072263751912dc5cd06d3c85f8f44be7c3"}, + {file = "cffi-2.1.1-cp311-cp311-win_amd64.whl", hash = "sha256:42f6930c31dc7f50732c9ae793c2786c7b6b044195967bbdde40bb9be81c4cc0"}, + {file = "cffi-2.1.1-cp311-cp311-win_arm64.whl", hash = "sha256:c7659f22557c5a0bc4855cd635f55edec690cc008a40768527762cb9fb263455"}, + {file = "cffi-2.1.1-cp312-cp312-macosx_10_15_x86_64.whl", hash = "sha256:c8c69575568085ba0b1b10c0249d779a214aea6f6522e949a0fc9fb0fcb449d0"}, + {file = "cffi-2.1.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f81b3b8f3d4e343550fa4baa0e479bba9f2d29ce9c2e9b51d1ce1718d7442fcf"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:811bd1e21d32de12efca32393a0ab3f5133b54fce9bd44b8bd77ab07da14bf6a"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:68e62fe11f30d5ca8289242866f0a5291402d8529ca2178ab8afc5c9694ae890"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:4a7c934f7360e8cd64fe9efadcbd10c7c6364f531e432b9a4bf5ccbc9e0e8b50"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:3143d81e29e1e20a9ce10901ec369012947876596f75a222235965f2b7ae832e"}, + {file = "cffi-2.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:c1453022f490d2459a11819d83ad1d586e9ff65a12ac3e705ffebd46d3685dcf"}, + {file = "cffi-2.1.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:208f941bb9d18e768138677f0a6d2ce01f590df56043dda1df1535ac57c88517"}, + {file = "cffi-2.1.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:210019b6c7cf07f081b4c54635c8cf744377001350e29cc0f81c4377b4797735"}, + {file = "cffi-2.1.1-cp312-cp312-win32.whl", hash = "sha256:046bfc24911b37851ee1b51aab8bffe713d89c68c6a057b09484ce9fd5f69b4e"}, + {file = "cffi-2.1.1-cp312-cp312-win_amd64.whl", hash = "sha256:f53e442b08449d42821fa4a4fba000095af9f62742a500f978a9f557ec44339a"}, + {file = "cffi-2.1.1-cp312-cp312-win_arm64.whl", hash = "sha256:7bde5e4cc5c10140859842b9d383af292b22639a4dffb725314baf45968cef80"}, + {file = "cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphoneos.whl", hash = "sha256:b5bdfd1c873d4e093aabc0ca84c4ca6dbc4f752afb5c86f146d9742580c9da2e"}, + {file = "cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:31348097ff5bbe827ccc41795d4dd099d9f0625e7def00ee653c137a490c2a6c"}, + {file = "cffi-2.1.1-cp313-cp313-macosx_10_15_x86_64.whl", hash = "sha256:9d2055050ea716bd38b7f7f1579c275386646b4894c155a3e2f3cd62ed41b7c6"}, + {file = "cffi-2.1.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:19ee6127ee34de7d83ce3d371ebc5ed91addbdcc39f9ab15ce4eb35a4e534971"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:6a8dddef476fab96d066d578fc88526767b836ab5ab21754e1d5bf3879c31c7c"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:f16c709686a78c727bbbf059f92b0bf41c6fc60deec706d2dc19f529175a6125"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:fcd22650c908d7b7da162bbfaab594a1227a15d1643a98c68b122ac642fa2264"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:aa9511c62d14da7aacc9b4bf51f3f697a621e83b2d6919008243c3aad168eea3"}, + {file = "cffi-2.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:a931079504ecc49efed7744c476a5c343a92fabf66dec2db95edb1b2fdc770e2"}, + {file = "cffi-2.1.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a2d7755bef5a12ed488f4ef1f1b69ee9191d7396083b755a5d2295f6edb4768b"}, + {file = "cffi-2.1.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:e0bcb7e0f677f543555d2adff3bf19c05f66cdb4796e5ff602442ab2fe3c4ef7"}, + {file = "cffi-2.1.1-cp313-cp313-win32.whl", hash = "sha256:334644fbac4eff73d985a17a91226df55d0f394160c4cfb880e084c8f7161cac"}, + {file = "cffi-2.1.1-cp313-cp313-win_amd64.whl", hash = "sha256:1aa5645c30469b09530c4ebca77ebf8f17618293c58f8549cb1a543a50236e7d"}, + {file = "cffi-2.1.1-cp313-cp313-win_arm64.whl", hash = "sha256:63bbfd5ded17c4840ac07cd8f1c21ba9d9708141f840b324f422f41b207e3973"}, + {file = "cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphoneos.whl", hash = "sha256:7dbb61fe3a7699468030f71bbe5f8a0e326a151daa91beb11a6fc1f980c55e1c"}, + {file = "cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:f24fb43132a4c6b4cb4eb029492919b2db645be6808d738f244fd146c03c32cb"}, + {file = "cffi-2.1.1-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:d28630f5854ab07ab1fd4aba756de52326c82e6be15d414b12793f1975048b54"}, + {file = "cffi-2.1.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:661c298b4821edebead0c91edd2b00374d67ad7c5a1f7a91d4442633b79d6a72"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:58acb8ab8e295e6c5ea12f888cbb13cf21511ef2a3303a23f4325c29d17fe5c1"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:456a61fa52d579ebf9df2e9552ead5129855dbaff6c1e5a9b1bc408809bdc062"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a4f00aa42f75d6e4595e8866e748cc1705adc0cddfeb2ca86d0d03993d63ba03"}, + {file = "cffi-2.1.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:b0431303acaea1089ad4b3e9ce4e6518193def1118d4073ca848635ee4ea2e96"}, + {file = "cffi-2.1.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:64faea20f4e2613363a1a9b9c7dd73058f3ecd00133a511e72ad7c511658f527"}, + {file = "cffi-2.1.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5c58fe613dc5e5336357eff555824a314d8e43282600435c8d1cb6a7a2fedd13"}, + {file = "cffi-2.1.1-cp314-cp314-win32.whl", hash = "sha256:1a18a57b58cfb21fc28d72e876acf10eaed67a1ed96226f92af4df681d571c4c"}, + {file = "cffi-2.1.1-cp314-cp314-win_amd64.whl", hash = "sha256:3222ba5d678f80a030e6afbcc33dc1ae5cb45facabb61cee2c7016b8432fde48"}, + {file = "cffi-2.1.1-cp314-cp314-win_arm64.whl", hash = "sha256:ab36d55f9ed2d067327667c2fea18dda018eb628dd6347aa01dda6cf1f5d3836"}, + {file = "cffi-2.1.1-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:7750c6449dff7864bb9bb27ddfb0267756189201a3afc911d82b3caacd70dfc3"}, + {file = "cffi-2.1.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:0beceaabe56af686895136a2de78db54ecd8e4046b236b8fd6d6cb61389e9bf2"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:49cbc70e6542d4ccccb936558d1064a8012541e78f821f955cff24e357776c94"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:e2d65b31f36619cda3999b78b2aa9632e76b78448e7a56fc4240824200e7c4fc"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:28907ab9bfb6aa13184cfc17c6b8e1023c5ab6fd7076d8c20a35e59fe04f8f29"}, + {file = "cffi-2.1.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:51b31d1c98274844cfd7838ce00bfc27c7423a4dc00fc0772fc3331c2cc90676"}, + {file = "cffi-2.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:5e7cecbaadb83884793e05828cee59b210b24583b9c7425d0ba6a754fe22eb4e"}, + {file = "cffi-2.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:25792eac27877609e7bb06d42ff88278a6624fff2ba9bbb523c09616b117e80f"}, + {file = "cffi-2.1.1-cp314-cp314t-win32.whl", hash = "sha256:8ef53b2de9bcb9197d31854256575d59dbac0cba72ac627bb291ef5eceb74be4"}, + {file = "cffi-2.1.1-cp314-cp314t-win_amd64.whl", hash = "sha256:616f097f2fe415bc92a247f02e11f634e1f9e9a83d327e3c915c15089c87869e"}, + {file = "cffi-2.1.1-cp314-cp314t-win_arm64.whl", hash = "sha256:ad2c86c495b899d862ea0f4b42891b8713a3bd45dd4105c7fd51c2a72f39f3a5"}, + {file = "cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:dddad92b554513a31f272570678ba307fb9f618f05e3d4a5eacafff9eae03e1d"}, + {file = "cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:da0e573f9f97159390c89d9f1a9e41908b66d408cc5b58d08cf3847d844c531b"}, + {file = "cffi-2.1.1-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:fb92203a88b3d3053034db775110081c49d28be6551923805e039924093761e4"}, + {file = "cffi-2.1.1-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:2ae64be792b8966f2c69538199728b290e34726562896df1e5dc8ffd8d8188e8"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:507a24c282e0f42f8ed737cf048572cbf580468da5555764a8331735e9c736b6"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:246fa40ce8645a614ff682e0b70f37134e460eaf93a775e0cbe3cca585a67a80"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:471cee653ae88de62096552e6d24ccb4a5adb8c8c9f10b5054d0122c15bf2779"}, + {file = "cffi-2.1.1-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:aeae0e330c9f6acd681f647d46cefd30c29f93e3392882e792e82080c9691399"}, + {file = "cffi-2.1.1-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:42a494cee34437f05546455144f2b5d9ac09b1face62bcfce597d2e521066688"}, + {file = "cffi-2.1.1-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:cc572dace3f60ef98d7b12ff411d20f5362feb31a0439eab0085bbfd349982d7"}, + {file = "cffi-2.1.1-cp315-cp315-win32.whl", hash = "sha256:4f42141fc14250de6dde5ee7ea4432be017252d91f19c5ad043c084cea629cac"}, + {file = "cffi-2.1.1-cp315-cp315-win_amd64.whl", hash = "sha256:e6e8cff14d6fb0be70a09c0bdc58096f501952d04624ebf867e0e56da2df8960"}, + {file = "cffi-2.1.1-cp315-cp315-win_arm64.whl", hash = "sha256:27350daa11d4f10c540e6e89dada4c54feb7256ad03e9a4dc075ebad7ba360d1"}, + {file = "cffi-2.1.1-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:c26608d2222fb1e94487e4a387d85f13eb55d5ed725cb25a0c589ac4ee60e7bc"}, + {file = "cffi-2.1.1-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:4be96343e422f2dfcd12ab5c9f5aebe03f82f737c6bffeca6830b3875cb44aab"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:937c0052c05a31ca1daf18de3158eed4dbfcb9cc107adbea227728d647be701e"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:df423d40ee8654634421812bc3b196da3f9bd7d32929da813f8394c4348a5358"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a730a083190634c65cca36ba5f489531576ebd79bcd5c8e172130f6453127231"}, + {file = "cffi-2.1.1-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:363e05fa78e15116c3c32c210ee36884fd6b9afa6d440e47112c3bd511d64cb6"}, + {file = "cffi-2.1.1-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:770de9db11e84213beec501cfcaa013b019820ca881e03344dea5844f7876d94"}, + {file = "cffi-2.1.1-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:7da0c5eff80f0197f3b3d1232ec5a682a9325f4ae9016a78f5f5ca35f9ced1f5"}, + {file = "cffi-2.1.1-cp315-cp315t-win32.whl", hash = "sha256:06c72bb76605a4b0cd0aad6930b69d4baf7dd5d806cfc409b824191099700e66"}, + {file = "cffi-2.1.1-cp315-cp315t-win_amd64.whl", hash = "sha256:d9c275eaacd24aa73f94ffd6de08fc3f932424d8b6c376f4bed7cde376fe7bc3"}, + {file = "cffi-2.1.1-cp315-cp315t-win_arm64.whl", hash = "sha256:d18e5ac0f2f03f4f518d3e23db0f0cad7faa1da8620e9c09461d443bbf6e6692"}, + {file = "cffi-2.1.1.tar.gz", hash = "sha256:dd31f52ea1086513bb9df30f8fcee9b8918323ae067a3d5b78bc826a000712be"}, +] + +[package.dependencies] +pycparser = {version = "*", markers = "implementation_name != \"PyPy\""} + +[[package]] +name = "click" +version = "8.5.0" +description = "Composable command line interface toolkit" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "click-8.5.0-py3-none-any.whl", hash = "sha256:255bc9599cf7748b4b1a446ccc735421bd08a2ae529a8b88597d3de5664ee360"}, + {file = "click-8.5.0.tar.gz", hash = "sha256:ba0d2089de75ea0310e2dde03160e6ca10009947fb95a182f9b54021bb272e34"}, +] + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +groups = ["main", "dev"] +markers = "sys_platform == \"win32\"" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "envparse" +version = "0.2.0" +description = "Simple environment variable parsing" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "envparse-0.2.0.tar.gz", hash = "sha256:4f3b9a27bb55d27f124eb4adf006fec05e4588891c9a054a183a112645056eb7"}, +] + +[package.extras] +testing = ["pytest"] + +[[package]] +name = "fastapi" +version = "0.142.2" +description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "fastapi-0.142.2-py3-none-any.whl", hash = "sha256:bd5f4d81f1e93a88bcd77caf4dfe3c2dbffc3805407a0007e9a114c18b3a670b"}, + {file = "fastapi-0.142.2.tar.gz", hash = "sha256:06366626f2e70576367714d9ab2fe8472e6c8456dba69b399f9f797ab5e92570"}, +] + +[package.dependencies] +annotated-doc = ">=0.0.2" +opentelemetry-api = ">=1.44.0" +pydantic = ">=2.9.0" +starlette = ">=0.46.0" +typing-extensions = ">=4.8.0" +typing-inspection = ">=0.4.2" + +[package.extras] +all = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.32)", "httpx (>=0.23.0,<1.0.0)", "itsdangerous (>=1.1.0)", "jinja2 (>=3.1.5)", "opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "pyyaml (>=5.3.1)", "uvicorn[standard] (>=0.12.0)"] +opentelemetry = ["opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)"] +standard = ["email-validator (>=2.0.0)", "fastapi-cli[standard] (>=0.0.32)", "fastar (>=0.9.0)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"] +standard-no-fastapi-cloud-cli = ["email-validator (>=2.0.0)", "fastapi-cli[standard-no-fastapi-cloud-cli] (>=0.0.32)", "httpx (>=0.23.0,<1.0.0)", "jinja2 (>=3.1.5)", "opentelemetry-exporter-otlp-proto-http (>=1.44.0)", "opentelemetry-sdk (>=1.44.0)", "pydantic-extra-types (>=2.0.0)", "pydantic-settings (>=2.0.0)", "python-multipart (>=0.0.18)", "uvicorn[standard] (>=0.12.0)"] + +[[package]] +name = "h11" +version = "0.16.0" +description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1" +optional = false +python-versions = ">=3.8" +groups = ["main", "dev"] +files = [ + {file = "h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86"}, + {file = "h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1"}, +] + +[[package]] +name = "httpcore" +version = "1.0.9" +description = "A minimal low-level HTTP client." +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55"}, + {file = "httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8"}, +] + +[package.dependencies] +certifi = "*" +h11 = ">=0.16" + +[package.extras] +asyncio = ["anyio (>=4.0,<5.0)"] +http2 = ["h2 (>=3,<5)"] +socks = ["socksio (==1.*)"] +trio = ["trio (>=0.22.0,<1.0)"] + +[[package]] +name = "httpx" +version = "0.28.1" +description = "The next generation HTTP client." +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad"}, + {file = "httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc"}, +] + +[package.dependencies] +anyio = "*" +certifi = "*" +httpcore = "==1.*" +idna = "*" + +[package.extras] +brotli = ["brotli ; platform_python_implementation == \"CPython\"", "brotlicffi ; platform_python_implementation != \"CPython\""] +cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"] +http2 = ["h2 (>=3,<5)"] +socks = ["socksio (==1.*)"] +zstd = ["zstandard (>=0.18.0)"] + +[[package]] +name = "idna" +version = "3.20" +description = "Internationalized Domain Names in Applications (IDNA)" +optional = false +python-versions = ">=3.9" +groups = ["main", "dev"] +files = [ + {file = "idna-3.20-py3-none-any.whl", hash = "sha256:ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c"}, + {file = "idna-3.20.tar.gz", hash = "sha256:a7db850025b95ded1eae8a46181a1a6c56c92c96f0e2b005d9ff8dc0210cab44"}, +] + +[package.extras] +all = ["coverage (>=7.10.0)", "hypothesis (>=6.141.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.16.0)", "ty (>=0.0.37)"] + +[[package]] +name = "iniconfig" +version = "2.3.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "iniconfig-2.3.0-py3-none-any.whl", hash = "sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12"}, + {file = "iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730"}, +] + +[[package]] +name = "loguru" +version = "0.7.3" +description = "Python logging made (stupidly) simple" +optional = false +python-versions = "<4.0,>=3.5" +groups = ["main"] +files = [ + {file = "loguru-0.7.3-py3-none-any.whl", hash = "sha256:31a33c10c8e1e10422bfd431aeb5d351c7cf7fa671e3c4df004162264b28220c"}, + {file = "loguru-0.7.3.tar.gz", hash = "sha256:19480589e77d47b8d85b2c827ad95d49bf31b0dcde16593892eb51dd18706eb6"}, +] + +[package.dependencies] +colorama = {version = ">=0.3.4", markers = "sys_platform == \"win32\""} +win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""} + +[package.extras] +dev = ["Sphinx (==8.1.3) ; python_version >= \"3.11\"", "build (==1.2.2) ; python_version >= \"3.11\"", "colorama (==0.4.5) ; python_version < \"3.8\"", "colorama (==0.4.6) ; python_version >= \"3.8\"", "exceptiongroup (==1.1.3) ; python_version >= \"3.7\" and python_version < \"3.11\"", "freezegun (==1.1.0) ; python_version < \"3.8\"", "freezegun (==1.5.0) ; python_version >= \"3.8\"", "mypy (==v0.910) ; python_version < \"3.6\"", "mypy (==v0.971) ; python_version == \"3.6\"", "mypy (==v1.13.0) ; python_version >= \"3.8\"", "mypy (==v1.4.1) ; python_version == \"3.7\"", "myst-parser (==4.0.0) ; python_version >= \"3.11\"", "pre-commit (==4.0.1) ; python_version >= \"3.9\"", "pytest (==6.1.2) ; python_version < \"3.8\"", "pytest (==8.3.2) ; python_version >= \"3.8\"", "pytest-cov (==2.12.1) ; python_version < \"3.8\"", "pytest-cov (==5.0.0) ; python_version == \"3.8\"", "pytest-cov (==6.0.0) ; python_version >= \"3.9\"", "pytest-mypy-plugins (==1.9.3) ; python_version >= \"3.6\" and python_version < \"3.8\"", "pytest-mypy-plugins (==3.1.0) ; python_version >= \"3.8\"", "sphinx-rtd-theme (==3.0.2) ; python_version >= \"3.11\"", "tox (==3.27.1) ; python_version < \"3.8\"", "tox (==4.23.2) ; python_version >= \"3.8\"", "twine (==6.0.1) ; python_version >= \"3.11\""] + +[[package]] +name = "opentelemetry-api" +version = "1.45.0" +description = "OpenTelemetry Python API" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "opentelemetry_api-1.45.0-py3-none-any.whl", hash = "sha256:80e068aba7cd56c8b58512d6a36f8d25cb1dfaa0c0a4cc1c938ccf9f362d9cb3"}, + {file = "opentelemetry_api-1.45.0.tar.gz", hash = "sha256:711ede81773c8025c2c03dac0450bc89f3d30aea6eabcc815c570d4e35a963f7"}, +] + +[package.dependencies] +typing-extensions = ">=4.5.0" + +[[package]] +name = "packaging" +version = "26.3" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "packaging-26.3-py3-none-any.whl", hash = "sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c"}, + {file = "packaging-26.3.tar.gz", hash = "sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79"}, +] + +[[package]] +name = "pluggy" +version = "1.6.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746"}, + {file = "pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3"}, +] + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["coverage", "pytest", "pytest-benchmark"] + +[[package]] +name = "pycparser" +version = "3.0" +description = "C parser in Python" +optional = false +python-versions = ">=3.10" +groups = ["main"] +markers = "implementation_name != \"PyPy\"" +files = [ + {file = "pycparser-3.0-py3-none-any.whl", hash = "sha256:b727414169a36b7d524c1c3e31839a521725078d7b2ff038656844266160a992"}, + {file = "pycparser-3.0.tar.gz", hash = "sha256:600f49d217304a5902ac3c37e1281c9fe94e4d0489de643a9504c5cdfdfc6b29"}, +] + +[[package]] +name = "pydantic" +version = "2.13.5" +description = "Data validation using Python type hints" +optional = false +python-versions = ">=3.9" +groups = ["main"] +files = [ + {file = "pydantic-2.13.5-py3-none-any.whl", hash = "sha256:346a034f080da3755d8e9cb5e00e8b07de1d39e4f6e2c87d8ab7cafa0b269a73"}, + {file = "pydantic-2.13.5.tar.gz", hash = "sha256:51a9c5f7b2f8e636f04c6cada605d9b6a3bf1348fdf945a3d8869b19bba0ee08"}, +] + +[package.dependencies] +annotated-types = ">=0.6.0" +pydantic-core = "2.46.5" +typing-extensions = ">=4.14.1" +typing-inspection = ">=0.4.2" + +[package.extras] +email = ["email-validator (>=2.0.0)"] +timezone = ["tzdata ; python_version >= \"3.9\" and platform_system == \"Windows\""] + +[[package]] +name = "pydantic-core" +version = "2.46.5" +description = "Core functionality for Pydantic validation and serialization" +optional = false +python-versions = ">=3.9" +groups = ["main"] +files = [ + {file = "pydantic_core-2.46.5-cp310-cp310-macosx_10_12_x86_64.whl", hash = "sha256:657b40d6240c0a7b6a64b30f22d1e3aa631c7e846c621b0c0f6d1d75e2e15ea6"}, + {file = "pydantic_core-2.46.5-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ecb42011e12ee19cafbc312887cbf3546959fe02fbad44f272d4be5baa997615"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4dedce55295becb61921e386b99d4f2706045306e7fa52249a33004c837379fb"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9f47b8a949e60f027f0aa0a6f6c7b7e9c55cbf4380d10b344e282fa4e7ab1e1b"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:200aa3dc9f8d54f0754f43247c0bad0999fdcfbfd2488384dd44f37279271fe6"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:6d30e1a4f138b8951063e9a394752a9179b51da288ffa507b1e659222f4c1793"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:850a08d167dde16db8702c274f320c7be9d7da6f6dff2b58b18f9e815bd94f5b"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_31_riscv64.whl", hash = "sha256:c3471e5c4a949c26ec00a77f01df59096aa9495877de76fd60a980f8ee6be461"}, + {file = "pydantic_core-2.46.5-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:3a3e26b6a8274211bddee2d0e4d0d42778f17a34510f49d2ec44b58abfc41736"}, + {file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:fc5d783bd4a2387e97b8a2d5ec781cfb92b3d893bf82370548e99db5915935d3"}, + {file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_armv7l.whl", hash = "sha256:356c8368cbc321050b169595683a2e1d63413b1e0e2868b330af9fc14c616d3f"}, + {file = "pydantic_core-2.46.5-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:eb7d8d0e5886a89a55d2eef490e272fa965a9d57c6b29a5b5088a7997ec2cad1"}, + {file = "pydantic_core-2.46.5-cp310-cp310-win32.whl", hash = "sha256:4d44cf99ddebf875f9b68cc267aa684c99b7b44fe63ee1cac4ec163807290069"}, + {file = "pydantic_core-2.46.5-cp310-cp310-win_amd64.whl", hash = "sha256:1e5aad1220a1192c42341c8fd4a8686657e73ab2a920c970bdc4de334fe3193d"}, + {file = "pydantic_core-2.46.5-cp311-cp311-macosx_10_12_x86_64.whl", hash = "sha256:a1dee1b804ff4d11c663636cf15d2ea47e9f79cd56c033fb1cbf08924842a48f"}, + {file = "pydantic_core-2.46.5-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:d625a186a65201c23a9e3b8ed9c47e90a026e03256608cc91851c6709096844f"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4f8507560a9284e1370bb048ed4282012fbef4e8d109875b95e884d228552061"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:5f93c5fe914d75fbec9a49209b00da5f08e9e467d69da2b1510c81940cfd10be"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:aca6c767f552b21b10f774aeac128e828eafb796adfa1b666a18bf6321453c3a"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:701b2e04b560eeb4bddf7a25ab8ca476176e34fdbd9a0e18196f0d12d4685f0b"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:49776eab08766a08dfff7012f8b422dcd7e25e43b316eedf0477c24fcfa84b7c"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_31_riscv64.whl", hash = "sha256:a2468d93d181667a7abd66e1b64bb9f76f361b0fef8faddf687456453576f5ee"}, + {file = "pydantic_core-2.46.5-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:53feb344243bb9510a9dec7bf3cf1b64d88a98af5dc7872a5160465f8b198c8e"}, + {file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:cd5214352ae68f3b5e9af7768bdc5253695ee069675db3480518420b3be881f2"}, + {file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_armv7l.whl", hash = "sha256:9432f3598db432cb51c5b37fdbf29a60fcccc79e30d37a05022776a6bc4ab689"}, + {file = "pydantic_core-2.46.5-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:8feeac04b5794e513e710af2f9c87d49f31a6dc47967bb264a1fed61a8989bec"}, + {file = "pydantic_core-2.46.5-cp311-cp311-win32.whl", hash = "sha256:892a881d5f68c2b9ea304b7a6c2c60d9343df578a311b0f86b94bc8f1ffe8129"}, + {file = "pydantic_core-2.46.5-cp311-cp311-win_amd64.whl", hash = "sha256:40375c2d05acec10323e45dfe2077ac44bc74659008614af5069034e2cfc781c"}, + {file = "pydantic_core-2.46.5-cp311-cp311-win_arm64.whl", hash = "sha256:28a6a556cd3b6066bea827857f9d9cce027c96f776e512f544a581f9e42161f8"}, + {file = "pydantic_core-2.46.5-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:b9fe6fb92520e3fd61f2e49000b6911b188824f089b75973ea06d6267f0b476d"}, + {file = "pydantic_core-2.46.5-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:a39ac25a9a2fa4072efdb429833c4a4c8009a51ff9eea3eeae131713cd27991e"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4fdc8b93a41521988916eeaa271173fcca7fa0803d62f87675aac8dcec1c8e29"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:b98134087d9de723658d17a42c7d0da8d6e2ef08015dee7dc93889047315f5e4"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e652ab17569c94bff5475520f907b7148b8c24036a8ebbe5cf7cf7493d28579a"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:d925f3d9afd05a8c0fb3a1031463a8d59ebe5e2afad297e29c78be19e13b4e62"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0fc5be0abd4a407e200d844b404e33639a554e7bd0d448e7b9ae181be4789ac2"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_31_riscv64.whl", hash = "sha256:816ff0a6550ffc06c098ccd2e0698600f9aa7da192a79eaa6f9af504a35db869"}, + {file = "pydantic_core-2.46.5-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:c7ea57fc63aa7da93a1bd2d644e6577befae10c52c4e36377635eea1056a74f5"}, + {file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:efd62a42486f1bda5d24cb4f63d15a3c7768375fe83d36f9417b4ad7a2fb20b3"}, + {file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_armv7l.whl", hash = "sha256:2bc9419666990c06d7397831f2126a1ecc3594aaa3ff7de5bf2d066802f4e07b"}, + {file = "pydantic_core-2.46.5-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:18a09e1e1011b462f2e32774f25859ef1223d5c2b0546a633cf56654710721e0"}, + {file = "pydantic_core-2.46.5-cp312-cp312-win32.whl", hash = "sha256:5cb482e9e84c851f4e623fe4acc1ced89168cf1fe18f7089db4548c8f5bbb65b"}, + {file = "pydantic_core-2.46.5-cp312-cp312-win_amd64.whl", hash = "sha256:5e81740c09e310f5aa5cbd3e434a01c154d4bef93241c7877b39f211d2b78ba8"}, + {file = "pydantic_core-2.46.5-cp312-cp312-win_arm64.whl", hash = "sha256:f7b0ec93a2893de856652154d73b7ba622f26fa97726487dcac373de5f4c6084"}, + {file = "pydantic_core-2.46.5-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:b7ca9034437b6022f941f4857459562ee00a560b97e7cce8a0ec5a74fc6766e0"}, + {file = "pydantic_core-2.46.5-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f332f0e72a5a0400141f830744e141bf9f97917878dbe968669e8a7fefea78ff"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:193375f3548919d3f0b60936ca113ada3e38f264f91b9b8e0508efaad57be931"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:79bdfa52f843137045b2d081cc05c120ba6665d29b7559c2c47690906f39279f"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:24922243639cbdac66c75fcb6fd6495a9cb52b213d62f9a0d16f0310b1ff8038"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c76fe65e607be28c7fd4d56fc3c42b1583aa058ce3408b7ad0fd540171d31f9f"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6f7b393a8b3da82f5c1fc0751e6d01ac6c55b93c18226a60bdfba4a724efafd1"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_31_riscv64.whl", hash = "sha256:7ac031912d54f3d83ef3b3eb98dfabc1608802e2202263d25957eeed40b94761"}, + {file = "pydantic_core-2.46.5-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:837b396ca3d7b74091ca623f6cbd8351bd42d670a79c2683e79fb089f06a2de5"}, + {file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:5ee239d575f80b08eca11f6e20f90c4c695de7825c67eefe6091fbf20dda648e"}, + {file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_armv7l.whl", hash = "sha256:e80675d75ae2cd14372cb65cad5400d9347a3d3f6c13000183f22dfd027283ed"}, + {file = "pydantic_core-2.46.5-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:9c4b71f10dd532fb7a5cbc8f58707779e64f03a258c2bf8bfbaecfcd9970b519"}, + {file = "pydantic_core-2.46.5-cp313-cp313-win32.whl", hash = "sha256:97bf8de4d541598c94a59344eeb988a94c08ff76b5723c41f6567ec18c7892ea"}, + {file = "pydantic_core-2.46.5-cp313-cp313-win_amd64.whl", hash = "sha256:15f4a94963c95accac15b7b657bb177d3ad82bb90b0d0526d9a9b85079925db5"}, + {file = "pydantic_core-2.46.5-cp313-cp313-win_arm64.whl", hash = "sha256:d22a945598fb91236b4dd793a6e42e4f3dd7740bb5aace5ebd7d4c08d13bb575"}, + {file = "pydantic_core-2.46.5-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:c1c43ad4339643d70ebb8124e1305a7dab423001eff58bb41a0f731adbc98355"}, + {file = "pydantic_core-2.46.5-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:1a353f84de772f423b5ffb11d7ae352fbbef0f446f3c0b0af0f8236d7233606e"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5086029a57366b8cf81b130a43908738095c270c21a8d7f0e8bdfdb89718e2f3"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:46c25dda9d092a06c08db76ffe0a197107904d0dfac653f7d5306bbcd6d6119c"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:37ea7b83c935e5b0d68c9449b82651accf78a10828b2c02b2f2d9e9496446c21"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e64e88d5585bea9ce95861079de72006c7fa6d3df4e3a3b65ba31eb979c15c9f"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:54d510bac3ee52247af28ed4bb18a1e799f040ac60fd2bf5ccd4c92f1fbe786f"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_31_riscv64.whl", hash = "sha256:a2a5e1d0ff29adddc9f6d6821a66302e4493f8ca898b715b6b1182c2c201ea0a"}, + {file = "pydantic_core-2.46.5-cp314-cp314-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:03b9666e41e35d8909852ba191a0607520f81b74eaf12ccf8737005dbb313821"}, + {file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_aarch64.whl", hash = "sha256:a91c17edf6eea2402cb5457b4c89e99bc5ed1004aa34c4adf1d4258c1a5c22c2"}, + {file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_armv7l.whl", hash = "sha256:b49924c73a235e969511bf2aabdff3beebf9820931f646c80274d5d780010c47"}, + {file = "pydantic_core-2.46.5-cp314-cp314-musllinux_1_1_x86_64.whl", hash = "sha256:2cbd9a5eff05e51c447c34dfa4632145b26b09120cf04bd0c871e44c1a5e1c9a"}, + {file = "pydantic_core-2.46.5-cp314-cp314-win32.whl", hash = "sha256:2d5d76654becf5efd62c9e51c3756c67b49498b0c9a40884934c40807adbd074"}, + {file = "pydantic_core-2.46.5-cp314-cp314-win_amd64.whl", hash = "sha256:fa10ef4112775900e7a0661068635eb67b2ab824fbde764de6e0e21982a93db0"}, + {file = "pydantic_core-2.46.5-cp314-cp314-win_arm64.whl", hash = "sha256:045ab3b6d308439e32b81cc173bba5b9018bc6ed896afd0c65b3b009b1699af5"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:8816f3d218beb4b787de5c9759c259b8fa61f9dec42dc7811f320a33771778b7"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:bce57638e08ac148e5778cce7feb968307a727d66f8e2274a543d0cf0c9ad6a3"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:976e1128455aa595ea04c79ccfedff1aaeab96ee013fcc916bed120c4f0ad94f"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e7b891faeedeafba41b2983e5001a81b6a915b69544c7e7570d1989ce1c36ac7"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:5f194189415698233dd1114a093a9b56e61e2c57e11b469be3b0506f46f0771c"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:82a36973cf8a2ef5406f4fe2edbf8ed0c99629535d959e0b100c76a32535a111"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cdbb78909f52b981d3b2d56b97328d71eb0b974c36bd77c920123a7ebb192829"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_31_riscv64.whl", hash = "sha256:52e24eacdb536cade636aa90fb851835222becff8484b7001fdc78cb0290f2aa"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:37ae34309d7bd8c0d61ab839668058f2a7962ea1fc51d105d2db228fe0618034"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_aarch64.whl", hash = "sha256:0cdbada856a1c69a7624a64d3d9aefe79300bd6ef827b43a4f265010b9b55184"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_armv7l.whl", hash = "sha256:545f26c504b27c3758439a5e6d9349931f0a04f855668d5fe323c89e82300a38"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-musllinux_1_1_x86_64.whl", hash = "sha256:ff218293c9c806138dca139765e3b067621be52bcd93cdc14c7711be7ddc90a9"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-win32.whl", hash = "sha256:97cf3eb53a8cccacf9d46686a0926186c9bfb5574f2ed66d3639d5fe117cd3a9"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-win_amd64.whl", hash = "sha256:d2f9fc07a8042a8f95925b35c4f04f469707c981fc33245b6ca187cf5d2dd290"}, + {file = "pydantic_core-2.46.5-cp314-cp314t-win_arm64.whl", hash = "sha256:acf8a67ba51f4ca9ddbd0e6b3000a65ac51ab734661778b3e7ba64d99a710f2f"}, + {file = "pydantic_core-2.46.5-cp39-cp39-macosx_10_12_x86_64.whl", hash = "sha256:c583b927a8838dab890706a6fa7573fbb8b70e24000ef9f7238e2d6f6435a5ed"}, + {file = "pydantic_core-2.46.5-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:cdc8b74ecc48c0cb1e9607a05ec4e9e88db60a19ffcc9a1d5f9088ede40c8dc0"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8b10e3e8fd7ddc2bd915848a2768e44c15b22936f1cc54c462ad1164deb02655"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:f077d0b97ab11fa7dcc633fca53515f290bca8a8a633e966d5b6d1879d9ed01a"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7b0fc826b16c55e561e5d2a0c5c77b051ba1d92808118c4e4b5390f5e0cf191d"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ef3fbbf161dc9351a2fe0422e51b129f9e97e42385bd0320b309c15f7d287dd8"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:978e7b97d4824b5be09c69fb70507cbde3b0323fc147332ca40a94d9a6a0ebbf"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_31_riscv64.whl", hash = "sha256:9b68938dd5b0c783d88ff8e2dcc69451b5eb936fe212d516b21b9d5567f6d464"}, + {file = "pydantic_core-2.46.5-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:771cf63ae0b1b50dd22e5f3e3549fab5f3f4ff1635d352a9e1a97fe01c7b2e64"}, + {file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:7c6be839a5a8312626b32029a415644a0846b420bc8b52b95b28cd92da162168"}, + {file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_armv7l.whl", hash = "sha256:895395f8918627b04efb1ad2a4cf605387143300ba03304cd1dfa6d03f5e095e"}, + {file = "pydantic_core-2.46.5-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:fc8515076c11f3cfdf4fb142dcca0fe384b1230a3b5415458ac84f3e0903ec13"}, + {file = "pydantic_core-2.46.5-cp39-cp39-win32.whl", hash = "sha256:3d2652072b2d774947ba5cf78a9e59644ac62ee572daf6dd2e1dfe905e15b2b7"}, + {file = "pydantic_core-2.46.5-cp39-cp39-win_amd64.whl", hash = "sha256:3aa166e99c4f2985407fb8714aebede877ecb5455cf321b606adca926d30d5a0"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-macosx_10_12_x86_64.whl", hash = "sha256:c14ad3bdc85ee7f318742c457ca3968a92126d144b15721c759033bfb06296c2"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-macosx_11_0_arm64.whl", hash = "sha256:0bddb4020d8f04175865ccd17eff3040874fc11fb593f424edb452653b4b947c"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2471fd51c61c610e1dcf7de44d7299283661654d11264ab4802b303368d69c47"}, + {file = "pydantic_core-2.46.5-graalpy311-graalpy242_311_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b10ec717381bdbfafef34607824db4c91de69ff085e4fca3b2af91b4fa17e68a"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-macosx_10_12_x86_64.whl", hash = "sha256:013d6f3483d81e02e7c328831808f336c8596ee33b4bd4026b9ffb1e960b8942"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-macosx_11_0_arm64.whl", hash = "sha256:e9c134bb666dd54b778b9fc0d2b50cbb7f979b9e3716f26a88c9ab3b6fc1dd0f"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:347ec774390c87326a2e4929d58d3f7e8763a104d5d35f4cd595a4c952366433"}, + {file = "pydantic_core-2.46.5-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8e24d8f05fa2d28513d94e877e9c75ad66175376209b3977f916e240e623193c"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-macosx_10_12_x86_64.whl", hash = "sha256:ab4b66edffb32d9e951efb3814bd104b8367a7501b81b955cacb5726d897389f"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-macosx_11_0_arm64.whl", hash = "sha256:337639ba62a11acde6ef3aeb08c8ea755f8ef1fe5e513356c0f36a2b0d7568b0"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:413a717a410d0c817ef5b786a059415550b3794e1d0c2abffd9efb93a3d9f7b4"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:1e449def1945a462c464331254e5a44fca7c3b4f9aedf59ec2f50f8066dd8e25"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_aarch64.whl", hash = "sha256:a445486499897b88a7d6c310c88ed64dd37b1b59bfd7ae9107490bbb362f47d6"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_armv7l.whl", hash = "sha256:2d330aaba8621b1edcec8ae2c4050f63b84ccf6d98723a8f212e9684713abf0e"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-musllinux_1_1_x86_64.whl", hash = "sha256:b6acfb46a814762367fb7ba0828b0a17d441b92ce249a0e007474c9072662dda"}, + {file = "pydantic_core-2.46.5-pp311-pypy311_pp73-win_amd64.whl", hash = "sha256:d0a24b40877af2de4950252be9d21eaf7fb07660f3c2cae1f56c6b599ada5266"}, + {file = "pydantic_core-2.46.5.tar.gz", hash = "sha256:10416c15b8839ecc4ef4d0885da76da6fd0f67333a0eb8aff6d93c4b8f2910fc"}, +] + +[package.dependencies] +typing-extensions = ">=4.14.1" + +[[package]] +name = "pygments" +version = "2.21.0" +description = "Pygments is a syntax highlighting package written in Python." +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "pygments-2.21.0-py3-none-any.whl", hash = "sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9"}, + {file = "pygments-2.21.0.tar.gz", hash = "sha256:610ca751c9bc2492b38eb9a38a7fbc93edbbb2d7182edaf34e66ae493dee5c8c"}, +] + +[package.extras] +windows-terminal = ["colorama (>=0.4.6)"] + +[[package]] +name = "pytest" +version = "9.1.1" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "pytest-9.1.1-py3-none-any.whl", hash = "sha256:37a86b45efb9a47a61a36449063e8e18d0cab3161329fc099eb21783169c4f0c"}, + {file = "pytest-9.1.1.tar.gz", hash = "sha256:1088fbde8f2b49d95a549a195707afa7a76a3ce9bcadc26b6d71f0ffda5fe313"}, +] + +[package.dependencies] +colorama = {version = ">=0.4", markers = "sys_platform == \"win32\""} +iniconfig = ">=1.0.1" +packaging = ">=22" +pluggy = ">=1.5,<2" +pygments = ">=2.7.2" + +[package.extras] +dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "requests", "setuptools", "xmlschema"] + +[[package]] +name = "redis" +version = "6.4.0" +description = "Python client for Redis database and key-value store" +optional = false +python-versions = ">=3.9" +groups = ["main"] +files = [ + {file = "redis-6.4.0-py3-none-any.whl", hash = "sha256:f0544fa9604264e9464cdf4814e7d4830f74b165d52f2a330a760a88dd248b7f"}, + {file = "redis-6.4.0.tar.gz", hash = "sha256:b01bc7282b8444e28ec36b261df5375183bb47a07eb9c603f284e89cbc5ef010"}, +] + +[package.extras] +hiredis = ["hiredis (>=3.2.0)"] +jwt = ["pyjwt (>=2.9.0)"] +ocsp = ["cryptography (>=36.0.1)", "pyopenssl (>=20.0.1)", "requests (>=2.31.0)"] + +[[package]] +name = "starlette" +version = "1.7.0" +description = "The little ASGI library that shines." +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "starlette-1.7.0-py3-none-any.whl", hash = "sha256:67f8e99895493dd2911a03f11314af6ceebeae4e704bb9f43dfc6a9db151c93e"}, + {file = "starlette-1.7.0.tar.gz", hash = "sha256:c79f74ea63cff761804fbbfb182f1e0b440c2d07b164d24700c5a1bab5d6ff5d"}, +] + +[package.dependencies] +anyio = ">=4.0.0,<5" +typing-extensions = {version = ">=4.10.0", markers = "python_version < \"3.13\""} + +[package.extras] +full = ["httpx (>=0.27.0,<0.29.0)", "httpx2 (>=2.0.0)", "itsdangerous", "jinja2", "opentelemetry-api", "python-multipart (>=0.0.18)", "pyyaml"] + +[[package]] +name = "typing-extensions" +version = "4.16.0" +description = "Backported and Experimental Type Hints for Python 3.9+" +optional = false +python-versions = ">=3.9" +groups = ["main", "dev"] +files = [ + {file = "typing_extensions-4.16.0-py3-none-any.whl", hash = "sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8"}, + {file = "typing_extensions-4.16.0.tar.gz", hash = "sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5"}, +] + +[[package]] +name = "typing-inspection" +version = "0.4.4" +description = "Runtime typing introspection tools" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "typing_inspection-0.4.4-py3-none-any.whl", hash = "sha256:65b8397ba37ccbce054456aaccddfc91e6e3083c92824df348d96ca832f3f147"}, + {file = "typing_inspection-0.4.4.tar.gz", hash = "sha256:547274fa6b0a561ccf549cc9524b999a578e737d015d8709d021f9d0d13bea47"}, +] + +[package.dependencies] +typing-extensions = ">=4.15.0" + +[[package]] +name = "uvicorn" +version = "0.54.0" +description = "The lightning-fast ASGI server." +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "uvicorn-0.54.0-py3-none-any.whl", hash = "sha256:505bdb0f318731d45f1f712071fc781a8981f6847a31c902c9f5e652d4f67faf"}, + {file = "uvicorn-0.54.0.tar.gz", hash = "sha256:a2e33cbfaa0306f8e6b0c13e0cb89d7d7a2da3e62b90c66e18c33d9807b28620"}, +] + +[package.dependencies] +click = ">=7.0" +h11 = ">=0.8" + +[package.extras] +standard = ["httptools (>=0.8.0)", "python-dotenv (>=0.13)", "pyyaml (>=5.1)", "uvloop (>=0.15.1) ; sys_platform != \"win32\" and sys_platform != \"cygwin\" and platform_python_implementation != \"PyPy\"", "watchfiles (>=0.20)", "websockets (>=13.0)"] + +[[package]] +name = "win32-setctime" +version = "1.2.0" +description = "A small Python utility to set file creation time on Windows" +optional = false +python-versions = ">=3.5" +groups = ["main"] +markers = "sys_platform == \"win32\"" +files = [ + {file = "win32_setctime-1.2.0-py3-none-any.whl", hash = "sha256:95d644c4e708aba81dc3704a116d8cbc974d70b3bdb8be1d150e36be6e9d1390"}, + {file = "win32_setctime-1.2.0.tar.gz", hash = "sha256:ae1fdf948f5640aae05c511ade119313fb6a30d7eabe25fef9764dca5873c4c0"}, +] + +[package.extras] +dev = ["black (>=19.3b0) ; python_version >= \"3.6\"", "pytest (>=4.6.2)"] + +[metadata] +lock-version = "2.1" +python-versions = ">=3.12,<3.13" +content-hash = "69e25ec2435541d7cdd8ffcea503313332dda8958ff07fcbe81fe125f1521346" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..7dcaf3fc --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,29 @@ +[tool.poetry] +name = "max2" +version = "1.2.0" +description = "In-memory мессенджер, лабораторная 2" +authors = ["Саватеев Артём"] +package-mode = false + +[tool.poetry.dependencies] +python = ">=3.12,<3.13" +fastapi = "0.142.2" +uvicorn = "0.54.0" +pydantic = "2.13.5" +envparse = "0.2.0" +loguru = "0.7.3" +asyncpg = "0.31.0" +redis = "6.4.0" +argon2-cffi = "25.1.0" + +[tool.poetry.group.dev.dependencies] +pytest = "9.1.1" +httpx = "0.28.1" + +[build-system] +requires = ["poetry-core>=2.0.0,<3.0.0"] +build-backend = "poetry.core.masonry.api" + +[tool.pytest.ini_options] +testpaths = ["student-tests"] +addopts = "-v" diff --git a/scripts/check_migrations.sh b/scripts/check_migrations.sh new file mode 100755 index 00000000..df625d13 --- /dev/null +++ b/scripts/check_migrations.sh @@ -0,0 +1,45 @@ +#!/bin/sh +set -eu +# Миграции с нуля и повторно: отдельный compose-проект с собственным пустым +# volume. Рабочий стенд и его данные не затрагиваются; временный volume +# этого проекта удаляется в конце. +cd "$(dirname "$0")/.." +PROJECT=messenger-lab2-migrations-check +COMPOSE="docker compose --env-file ${ENV_FILE:-.env} -p $PROJECT" +trap '$COMPOSE down -v --remove-orphans >/dev/null 2>&1' EXIT + +$COMPOSE up -d --wait postgres >/dev/null 2>&1 +$COMPOSE run --rm --no-deps -T api python - <<'EOF' +import asyncio +import sys + +sys.path.insert(0, "src") + +from infrastructure.dependencies.database import database_pool_manager as db + + +async def main() -> None: + await db.create_pool(30) + async with db.acquire() as conn: + tables = await conn.fetchval( + "SELECT count(*) FROM pg_tables WHERE schemaname = 'public';" + ) + print(f"пустой volume: таблиц в public = {tables}") + for attempt in (1, 2, 3): + await db.apply_migrations() + async with db.acquire() as conn: + versions = await conn.fetch( + "SELECT version, name FROM schema_migrations ORDER BY version;" + ) + tables = await conn.fetchval( + "SELECT count(*) FROM pg_tables WHERE schemaname = 'public';" + ) + print( + f"запуск {attempt}: schema_migrations = " + f"{[(r['version'], r['name']) for r in versions]}, таблиц = {tables}" + ) + await db.close() + + +asyncio.run(main()) +EOF diff --git a/scripts/explain_history.sql b/scripts/explain_history.sql new file mode 100644 index 00000000..d48d1b95 --- /dev/null +++ b/scripts/explain_history.sql @@ -0,0 +1,111 @@ +-- Обоснование индекса истории: EXPLAIN (ANALYZE, BUFFERS) на ≥10 000 сообщений. +-- Запуск: make explain → evidence/explain_history.txt +-- Сид синтетический и идемпотентный: повторный запуск данных не дублирует. +-- Пользователь explain_seed не может войти: password_hash не является hash Argon2. + +\pset pager off +\timing off + +DO $$ +DECLARE + seed_user uuid; + big_chat uuid; + noise uuid; +BEGIN + IF EXISTS (SELECT 1 FROM chats WHERE title = 'explain-big') THEN + RETURN; + END IF; + + INSERT INTO users (username, display_name, password_hash) + VALUES ('explain_seed', 'EXPLAIN seed', 'not-an-argon2-hash') + ON CONFLICT (username) DO UPDATE SET display_name = EXCLUDED.display_name + RETURNING id INTO seed_user; + + -- Большой чат: 20 000 сообщений + INSERT INTO chats (title, created_by) VALUES ('explain-big', seed_user) + RETURNING id INTO big_chat; + INSERT INTO chat_members (chat_id, user_id, position) VALUES (big_chat, seed_user, 0); + INSERT INTO messages (chat_id, sender_id, text, created_at) + SELECT big_chat, seed_user, 'synthetic message #' || n, + timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second' + FROM generate_series(1, 20000) AS n; + + -- Шум: 50 чатов по 1 000 сообщений, чтобы таблица была больше одного чата + FOR i IN 1..50 LOOP + INSERT INTO chats (title, created_by) VALUES ('explain-noise-' || i, seed_user) + RETURNING id INTO noise; + INSERT INTO chat_members (chat_id, user_id, position) VALUES (noise, seed_user, 0); + INSERT INTO messages (chat_id, sender_id, text, created_at) + SELECT noise, seed_user, 'noise #' || n, + timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second' + FROM generate_series(1, 1000) AS n; + END LOOP; +END $$; + +ANALYZE users, chats, chat_members, messages; + +SELECT id AS big_chat FROM chats WHERE title = 'explain-big' \gset + +-- Cursor из середины истории: ключ 10 000-го сообщения +SELECT created_at AS cursor_created_at, id AS cursor_id +FROM messages WHERE chat_id = :'big_chat' +ORDER BY created_at, id OFFSET 9999 LIMIT 1 \gset + +\echo '== Объём данных' +SELECT + (SELECT count(*) FROM messages) AS messages_total, + (SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS messages_in_big_chat, + pg_size_pretty(pg_relation_size('messages')) AS messages_heap, + pg_size_pretty(pg_relation_size('messages_chat_id_created_at_id_idx')) AS history_index; + +\echo '' +\echo '== 1. Первая страница истории (limit 50 + 1), как list_for_chat' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = :'big_chat' +ORDER BY created_at, id +LIMIT 51; + +\echo '' +\echo '== 2. Страница из середины по cursor (после 10 000-го), как list_for_chat_after' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = :'big_chat' + AND (created_at, id) > (:'cursor_created_at'::timestamptz, :'cursor_id'::uuid) +ORDER BY created_at, id +LIMIT 51; + +\echo '' +\echo '== 3. Тот же запрос как generic plan prepared statement (так его исполняет asyncpg)' +SET plan_cache_mode = force_generic_plan; +PREPARE history_page(uuid, timestamptz, uuid, int) AS +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = $1 AND (created_at, id) > ($2, $3) +ORDER BY created_at, id +LIMIT $4; +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +EXECUTE history_page(:'big_chat', :'cursor_created_at', :'cursor_id', 51); +DEALLOCATE history_page; +RESET plan_cache_mode; + +\echo '' +\echo '== 4. MAX(created_at) при вставке сообщения (монотонное время чата)' +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT max(created_at) FROM messages WHERE chat_id = :'big_chat'; + +\echo '' +\echo '== 5. Для сравнения: тот же запрос из середины БЕЗ индекса истории' +\echo ' (DROP INDEX внутри транзакции с ROLLBACK — индекс не теряется)' +BEGIN; +DROP INDEX messages_chat_id_created_at_id_idx; +EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = :'big_chat' + AND (created_at, id) > (:'cursor_created_at'::timestamptz, :'cursor_id'::uuid) +ORDER BY created_at, id +LIMIT 51; +ROLLBACK; diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..a80b1276 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -eu +# Перезапуск API и всех используемых хранилищ (PostgreSQL, Valkey) с сохранением +# volumes: никаких down -v, flush или повторного seed. Готовность ждёт тест. +cd "$(dirname "$0")/.." +ENV_FILE="${ENV_FILE:-.env}" + +docker compose --env-file "$ENV_FILE" restart postgres valkey +docker compose --env-file "$ENV_FILE" restart api diff --git a/scripts/sigterm_check.py b/scripts/sigterm_check.py new file mode 100644 index 00000000..06618e5b --- /dev/null +++ b/scripts/sigterm_check.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +"""SIGTERM: процесс перестаёт принимать запросы и завершается не дольше чем за 10 секунд. + +Во время проверки фоновый клиент непрерывно шлёт GET /health/live: до сигнала +ответы 200, после — отказ в соединении; ответов 5xx быть не должно. +""" + +import argparse +import json +import os +import subprocess +import threading +import time +import urllib.error +import urllib.request +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +DEFAULT_BASE_URL = json.loads((ROOT / "lab.json").read_text())["default_base_url"] + +SHUTDOWN_LIMIT_SEC = 10 +PROBE_INTERVAL_SEC = 0.02 + + +class Prober(threading.Thread): + """Фоновый клиент: каждое обращение — новое соединение""" + + def __init__(self, url: str) -> None: + super().__init__(daemon=True) + self.url = url + self.stop = threading.Event() + self.ok: list[float] = [] + self.refused: list[float] = [] + self.server_errors = 0 + + def run(self) -> None: + while not self.stop.is_set(): + try: + with urllib.request.urlopen(self.url, timeout=2) as response: + if response.status == 200: + self.ok.append(time.monotonic()) + except urllib.error.HTTPError as error: + if error.code >= 500: + self.server_errors += 1 + except OSError: + self.refused.append(time.monotonic()) + time.sleep(PROBE_INTERVAL_SEC) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--base-url", default=os.environ.get("BASE_URL") or DEFAULT_BASE_URL + ) + parser.add_argument( + "--container", default=os.environ.get("CONTAINER", "messenger-lab2-api-1") + ) + args = parser.parse_args() + + logs = subprocess.Popen( + ["docker", "logs", "--follow", "--since", "0s", args.container], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + ) + waiter = subprocess.Popen( + ["docker", "wait", args.container], stdout=subprocess.PIPE, text=True + ) + prober = Prober(args.base_url.rstrip("/") + "/health/live") + prober.start() + time.sleep(1) + if not prober.ok: + raise SystemExit("FAIL: API не отвечает до отправки SIGTERM") + + sent_at = time.monotonic() + subprocess.run( + ["docker", "kill", "--signal", "SIGTERM", args.container], + check=True, + capture_output=True, + ) + exit_code = int(waiter.communicate(timeout=SHUTDOWN_LIMIT_SEC + 5)[0].strip()) + exited_at = time.monotonic() + time.sleep(0.5) + prober.stop.set() + prober.join() + shutdown_log = logs.communicate(timeout=5)[0] + + shutdown_sec = exited_at - sent_at + ok_after = [t for t in prober.ok if t > sent_at] + first_refused = min((t for t in prober.refused if t > sent_at), default=None) + print("Логи контейнера после SIGTERM:") + print( + "\n".join(line for line in shutdown_log.splitlines() if "/health/" not in line) + ) + print( + json.dumps( + { + "container": args.container, + "exit_code": exit_code, + "shutdown_seconds": round(shutdown_sec, 3), + "limit_seconds": SHUTDOWN_LIMIT_SEC, + "probes_ok_before_sigterm": len(prober.ok) - len(ok_after), + "probes_ok_after_sigterm": len(ok_after), + "first_refused_after_sigterm_seconds": None + if first_refused is None + else round(first_refused - sent_at, 3), + "probes_refused_after_sigterm": len(prober.refused), + "server_errors_5xx": prober.server_errors, + }, + indent=2, + ) + ) + if ( + exit_code != 0 + or shutdown_sec > SHUTDOWN_LIMIT_SEC + or prober.server_errors + or first_refused is None + ): + raise SystemExit("FAIL: завершение по SIGTERM не соответствует требованиям") + print( + "PASS: SIGTERM завершил процесс за %.2f с, новые соединения отклоняются" + % shutdown_sec + ) + + +if __name__ == "__main__": + main() diff --git a/src/application/__init__.py b/src/application/__init__.py new file mode 100644 index 00000000..75143767 --- /dev/null +++ b/src/application/__init__.py @@ -0,0 +1,4 @@ +from .api import app as api_endpoints +from .api import health_router as health_endpoints + +__all__ = ["api_endpoints", "health_endpoints"] diff --git a/src/application/api/__init__.py b/src/application/api/__init__.py new file mode 100644 index 00000000..48c9b632 --- /dev/null +++ b/src/application/api/__init__.py @@ -0,0 +1,10 @@ +from fastapi import APIRouter + +from .health import health_router +from .routers import app as v1_endpoints + +app = APIRouter(prefix="/api") + +app.include_router(v1_endpoints) + +__all__ = ["app", "health_router"] diff --git a/src/application/api/dependencies/__init__.py b/src/application/api/dependencies/__init__.py new file mode 100644 index 00000000..93fec71b --- /dev/null +++ b/src/application/api/dependencies/__init__.py @@ -0,0 +1,4 @@ +from .auth import get_current_user +from .security import basic_credentials, bearer_token + +__all__ = ["get_current_user", "basic_credentials", "bearer_token"] diff --git a/src/application/api/dependencies/auth.py b/src/application/api/dependencies/auth.py new file mode 100644 index 00000000..8bfdfe87 --- /dev/null +++ b/src/application/api/dependencies/auth.py @@ -0,0 +1,15 @@ +from fastapi import Depends + +from domain import UserSchema +from infrastructure.dependencies import ServiceCreator +from infrastructure.services import AuthService + +from .security import bearer_token + + +async def get_current_user( + token: str | None = Depends(bearer_token), + service: AuthService = Depends(ServiceCreator.get_auth_service), +) -> UserSchema: + """Владелец действующей Bearer-сессии; X-User-Id не учитывается; иначе 401""" + return await service.authenticate(token) diff --git a/src/application/api/dependencies/security.py b/src/application/api/dependencies/security.py new file mode 100644 index 00000000..44a66b1f --- /dev/null +++ b/src/application/api/dependencies/security.py @@ -0,0 +1,52 @@ +import base64 +import binascii + +from fastapi import Depends +from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer +from fastapi.security.http import HTTPBase + +from domain import BasicCredentialsSchema +from domain.errors import InvalidCredentialsError + +BASIC_SCHEME = "basic" + +basic_scheme = HTTPBase( + scheme=BASIC_SCHEME, + scheme_name="basicAuth", + description="Только для POST /auth/sessions", + auto_error=False, +) + +bearer_scheme = HTTPBearer( + scheme_name="bearerAuth", + bearerFormat="opaque session token", + auto_error=False, +) + + +async def basic_credentials( + authorization: HTTPAuthorizationCredentials | None = Depends(basic_scheme), +) -> BasicCredentialsSchema: + """ + Authorization: Basic base64(username:password) + + username — до первого двоеточия, остальное — пароль + Любая ошибка формата — тот же 401, что и неверный пароль. + """ + if authorization is None or authorization.scheme.lower() != BASIC_SCHEME: + raise InvalidCredentialsError() + try: + decoded = base64.b64decode(authorization.credentials, validate=True) + username, separator, password = decoded.decode("utf-8").partition(":") + except (binascii.Error, UnicodeDecodeError): + raise InvalidCredentialsError() from None + if not separator or not username.isascii(): + raise InvalidCredentialsError() + return BasicCredentialsSchema(username=username, password=password) + + +async def bearer_token( + authorization: HTTPAuthorizationCredentials | None = Depends(bearer_scheme), +) -> str | None: + """Токен из Authorization: Bearer; другие схемы (Basic) — как отсутствие""" + return authorization.credentials if authorization is not None else None diff --git a/src/application/api/handlers/__init__.py b/src/application/api/handlers/__init__.py new file mode 100644 index 00000000..59f2c3fe --- /dev/null +++ b/src/application/api/handlers/__init__.py @@ -0,0 +1,3 @@ +from .exceptions import register_exception_handlers + +__all__ = ["register_exception_handlers"] diff --git a/src/application/api/handlers/exceptions.py b/src/application/api/handlers/exceptions.py new file mode 100644 index 00000000..e00f47a2 --- /dev/null +++ b/src/application/api/handlers/exceptions.py @@ -0,0 +1,61 @@ +from http import HTTPStatus + +from fastapi import FastAPI, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from starlette.exceptions import HTTPException as StarletteHTTPException + +from application.api.utils import error_response, get_request_id +from domain.errors import AppError, ErrorCode, InvalidRequestError, NotFoundError + + +async def app_error_handler(request: Request, error: Exception) -> JSONResponse: + """Доменные ошибки сервисов и репозиториев""" + assert isinstance(error, AppError) + return error_response( + get_request_id(request), + error.status_code, + error.code, + error.message, + error.headers, + ) + + +async def validation_error_handler(request: Request, error: Exception) -> JSONResponse: + """Malformed JSON, лишние поля, неверные типы, uuid и limit — 400""" + assert isinstance(error, RequestValidationError) + return error_response( + get_request_id(request), + InvalidRequestError.status_code, + InvalidRequestError.code, + _describe_validation_error(error), + ) + + +async def http_error_handler(request: Request, error: Exception) -> JSONResponse: + """Ошибки маршрутизации неизвестный путь, неподдерживаемый метод""" + assert isinstance(error, StarletteHTTPException) + if error.status_code == HTTPStatus.NOT_FOUND: + code, message = NotFoundError.code, NotFoundError.default_message + elif error.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR: + code, message = AppError.code, AppError.default_message + else: + code, message = ErrorCode.INVALID_REQUEST, HTTPStatus(error.status_code).phrase + return error_response( + get_request_id(request), error.status_code, code, message, error.headers + ) + + +def _describe_validation_error(error: RequestValidationError) -> str: + """Безопасное описание: только тип и путь поля, без присланного значения""" + first = error.errors()[0] + if first.get("type") == "json_invalid": + return "Некорректный JSON в теле запроса" + location = ".".join(str(part) for part in first.get("loc", ())) + return f"Некорректное значение: {location}" + + +def register_exception_handlers(app: FastAPI) -> None: + app.add_exception_handler(AppError, app_error_handler) + app.add_exception_handler(RequestValidationError, validation_error_handler) + app.add_exception_handler(StarletteHTTPException, http_error_handler) diff --git a/src/application/api/health/__init__.py b/src/application/api/health/__init__.py new file mode 100644 index 00000000..400c3b13 --- /dev/null +++ b/src/application/api/health/__init__.py @@ -0,0 +1,3 @@ +from .endpoints import health_router + +__all__ = ["health_router"] diff --git a/src/application/api/health/endpoints.py b/src/application/api/health/endpoints.py new file mode 100644 index 00000000..ccc48a7b --- /dev/null +++ b/src/application/api/health/endpoints.py @@ -0,0 +1,36 @@ +from fastapi import APIRouter, Depends, Request, status + +from domain import HealthSchema +from domain.errors import UnavailableError +from infrastructure.dependencies import DatabasePoolManager, get_database + +health_router = APIRouter(prefix="/health", tags=["Health"]) + + +@health_router.get( + "/live", + status_code=status.HTTP_200_OK, + response_model=HealthSchema, +) +async def liveness() -> HealthSchema: + """Процесс жив и обрабатывает хттп""" + return HealthSchema() + + +@health_router.get( + "/ready", + status_code=status.HTTP_200_OK, + response_model=HealthSchema, +) +async def readiness( + request: Request, database: DatabasePoolManager = Depends(get_database) +) -> HealthSchema: + """ + Приложение не завершается и PostgreSQL отвечает. Valkey не критичен: + без него сессии читаются из PostgreSQL, поэтому он не влияет на готовность. + """ + if not getattr(request.app.state, "is_ready", False): + raise UnavailableError() + if not await database.check(): + raise UnavailableError("бд недоступна") + return HealthSchema() diff --git a/src/application/api/middlewares/__init__.py b/src/application/api/middlewares/__init__.py new file mode 100644 index 00000000..6005e31e --- /dev/null +++ b/src/application/api/middlewares/__init__.py @@ -0,0 +1,3 @@ +from .request_context import RequestContextMiddleware + +__all__ = ["RequestContextMiddleware"] diff --git a/src/application/api/middlewares/request_context.py b/src/application/api/middlewares/request_context.py new file mode 100644 index 00000000..4ae099c0 --- /dev/null +++ b/src/application/api/middlewares/request_context.py @@ -0,0 +1,55 @@ +import time +import uuid + +from fastapi import Request, Response +from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint + +from application.api.utils import REQUEST_ID_HEADER, error_response +from config import LOGGER +from domain.errors import AppError + +HEALTH_PATH_PREFIX = "/health/" + + +class RequestContextMiddleware(BaseHTTPMiddleware): + """ + X-Request-Id в каждом ответе, журнал запросов и защита от необработанных ошибок. + + В лог попадают метод, путь, статус и длительность — без тел запросов, + query-параметров и текста сообщений. + """ + + async def dispatch( + self, request: Request, call_next: RequestResponseEndpoint + ) -> Response: + request_id = uuid.uuid4().hex + request.state.request_id = request_id + started = time.perf_counter() + try: + response = await call_next(request) + except Exception: + LOGGER.exception(f"Необработанная ошибка | request_id={request_id}") + error = AppError() + response = error_response( + request_id, error.status_code, error.code, error.message + ) + response.headers[REQUEST_ID_HEADER] = request_id + self._log_request(request, response, request_id, started) + return response + + @staticmethod + def _log_request( + request: Request, response: Response, request_id: str, started: float + ) -> None: + duration_ms = (time.perf_counter() - started) * 1000 + if response.status_code >= 500: + level = "ERROR" + elif request.url.path.startswith(HEALTH_PATH_PREFIX): + level = "DEBUG" + else: + level = "INFO" + LOGGER.log( + level, + f"{request.method} {request.url.path} -> {response.status_code} " + f"| {duration_ms:.1f} ms | request_id={request_id}", + ) diff --git a/src/application/api/routers/__init__.py b/src/application/api/routers/__init__.py new file mode 100644 index 00000000..8ec37015 --- /dev/null +++ b/src/application/api/routers/__init__.py @@ -0,0 +1,15 @@ +from fastapi import APIRouter + +from .auth import auth_router +from .chats import chats_router +from .messages import messages_router +from .users import users_router + +app = APIRouter(prefix="/v1") + +app.include_router(users_router) +app.include_router(auth_router) +app.include_router(chats_router) +app.include_router(messages_router) + +__all__ = ["app"] diff --git a/src/application/api/routers/auth/__init__.py b/src/application/api/routers/auth/__init__.py new file mode 100644 index 00000000..b5957682 --- /dev/null +++ b/src/application/api/routers/auth/__init__.py @@ -0,0 +1,3 @@ +from .endpoints import auth_router + +__all__ = ["auth_router"] diff --git a/src/application/api/routers/auth/endpoints.py b/src/application/api/routers/auth/endpoints.py new file mode 100644 index 00000000..0e57edd5 --- /dev/null +++ b/src/application/api/routers/auth/endpoints.py @@ -0,0 +1,35 @@ +from fastapi import APIRouter, Depends, Response, status + +from application.api.dependencies import basic_credentials, bearer_token +from domain import BasicCredentialsSchema, SessionSchema +from infrastructure.dependencies import ServiceCreator +from infrastructure.services import AuthService + +auth_router = APIRouter(prefix="/auth/sessions", tags=["Auth"]) + + +@auth_router.post( + "", + status_code=status.HTTP_201_CREATED, + response_model=SessionSchema, +) +async def create_session( + credentials: BasicCredentialsSchema = Depends(basic_credentials), + service: AuthService = Depends(ServiceCreator.get_auth_service), +) -> SessionSchema: + """Вход по Basic, без JSON body; возвращает Bearer-токен""" + return await service.login(credentials) + + +@auth_router.delete( + "/current", + status_code=status.HTTP_204_NO_CONTENT, + response_class=Response, +) +async def revoke_session( + token: str | None = Depends(bearer_token), + service: AuthService = Depends(ServiceCreator.get_auth_service), +) -> Response: + """Logout: текущий токен перестаёт действовать; повтор — 401""" + await service.logout(token) + return Response(status_code=status.HTTP_204_NO_CONTENT) diff --git a/src/application/api/routers/chats/__init__.py b/src/application/api/routers/chats/__init__.py new file mode 100644 index 00000000..7364c40f --- /dev/null +++ b/src/application/api/routers/chats/__init__.py @@ -0,0 +1,3 @@ +from .endpoints import chats_router + +__all__ = ["chats_router"] diff --git a/src/application/api/routers/chats/endpoints.py b/src/application/api/routers/chats/endpoints.py new file mode 100644 index 00000000..4556db55 --- /dev/null +++ b/src/application/api/routers/chats/endpoints.py @@ -0,0 +1,58 @@ +from typing import Annotated +from uuid import UUID + +from fastapi import APIRouter, Body, Depends, Query, status + +from application.api.dependencies import get_current_user +from domain import ( + ChatPageSchema, + ChatSchema, + CreateChatSchema, + UserSchema, +) +from infrastructure.dependencies import ServiceCreator +from infrastructure.services import ChatService + +chats_router = APIRouter(prefix="/chats", tags=["Chats"]) + + +@chats_router.post( + "", + status_code=status.HTTP_201_CREATED, + response_model=ChatSchema, +) +async def create_chat( + body: Annotated[CreateChatSchema, Body(...)], + current_user: UserSchema = Depends(get_current_user), + service: ChatService = Depends(ServiceCreator.get_chat_service), +) -> ChatSchema: + """Вызывающий должен входить в member_ids""" + return await service.create_chat(current_user, body) + + +@chats_router.get( + "", + status_code=status.HTTP_200_OK, + response_model=ChatPageSchema, +) +async def list_chats( + limit: Annotated[int, Query(ge=1, le=100)] = 50, + cursor: Annotated[str | None, Query(min_length=1)] = None, + current_user: UserSchema = Depends(get_current_user), + service: ChatService = Depends(ServiceCreator.get_chat_service), +) -> ChatPageSchema: + """Чаты текущего пользователя по (created_at, id) по возрастанию""" + return await service.list_chats(current_user, limit, cursor) + + +@chats_router.get( + "/{chat_id}", + status_code=status.HTTP_200_OK, + response_model=ChatSchema, +) +async def get_chat( + chat_id: UUID, + current_user: UserSchema = Depends(get_current_user), + service: ChatService = Depends(ServiceCreator.get_chat_service), +) -> ChatSchema: + return await service.get_chat(current_user, chat_id) diff --git a/src/application/api/routers/messages/__init__.py b/src/application/api/routers/messages/__init__.py new file mode 100644 index 00000000..8573c3bc --- /dev/null +++ b/src/application/api/routers/messages/__init__.py @@ -0,0 +1,3 @@ +from .endpoints import messages_router + +__all__ = ["messages_router"] diff --git a/src/application/api/routers/messages/endpoints.py b/src/application/api/routers/messages/endpoints.py new file mode 100644 index 00000000..0353fd96 --- /dev/null +++ b/src/application/api/routers/messages/endpoints.py @@ -0,0 +1,48 @@ +from typing import Annotated +from uuid import UUID + +from fastapi import APIRouter, Body, Depends, Query, status + +from application.api.dependencies import get_current_user +from domain import ( + CreateMessageSchema, + MessagePageSchema, + MessageSchema, + UserSchema, +) +from infrastructure.dependencies import ServiceCreator +from infrastructure.services import MessageService + +messages_router = APIRouter( + prefix="/chats/{chat_id}/messages", tags=["Messages"] +) + + +@messages_router.post( + "", + status_code=status.HTTP_201_CREATED, + response_model=MessageSchema, +) +async def send_message( + chat_id: UUID, + body: Annotated[CreateMessageSchema, Body(...)], + current_user: UserSchema = Depends(get_current_user), + service: MessageService = Depends(ServiceCreator.get_message_service), +) -> MessageSchema: + return await service.send_message(current_user, chat_id, body) + + +@messages_router.get( + "", + status_code=status.HTTP_200_OK, + response_model=MessagePageSchema, +) +async def list_messages( + chat_id: UUID, + limit: Annotated[int, Query(ge=1, le=100)] = 50, + cursor: Annotated[str | None, Query(min_length=1)] = None, + current_user: UserSchema = Depends(get_current_user), + service: MessageService = Depends(ServiceCreator.get_message_service), +) -> MessagePageSchema: + """История чата: keyset cursor по (created_at, id) по возрастанию""" + return await service.list_messages(current_user, chat_id, limit, cursor) diff --git a/src/application/api/routers/users/__init__.py b/src/application/api/routers/users/__init__.py new file mode 100644 index 00000000..ca28ba5a --- /dev/null +++ b/src/application/api/routers/users/__init__.py @@ -0,0 +1,3 @@ +from .endpoints import users_router + +__all__ = ["users_router"] diff --git a/src/application/api/routers/users/endpoints.py b/src/application/api/routers/users/endpoints.py new file mode 100644 index 00000000..f91c9503 --- /dev/null +++ b/src/application/api/routers/users/endpoints.py @@ -0,0 +1,33 @@ +from typing import Annotated + +from fastapi import APIRouter, Body, Depends, status + +from application.api.dependencies import get_current_user +from domain import CreateUserSchema, UserSchema +from infrastructure.dependencies import ServiceCreator +from infrastructure.services import UserService + +users_router = APIRouter(prefix="/users", tags=["Users"]) + + +@users_router.post( + "", + status_code=status.HTTP_201_CREATED, + response_model=UserSchema, + openapi_extra={"security": []}, +) +async def register_user( + body: Annotated[CreateUserSchema, Body(...)], + service: UserService = Depends(ServiceCreator.get_user_service), +) -> UserSchema: + """Публичная регистрация; username уникален""" + return await service.register(body) + + +@users_router.get( + "/me", + status_code=status.HTTP_200_OK, + response_model=UserSchema, +) +async def get_me(current_user: UserSchema = Depends(get_current_user)) -> UserSchema: + return current_user diff --git a/src/application/api/utils/__init__.py b/src/application/api/utils/__init__.py new file mode 100644 index 00000000..0e10f357 --- /dev/null +++ b/src/application/api/utils/__init__.py @@ -0,0 +1,3 @@ +from .errors import REQUEST_ID_HEADER, error_response, get_request_id + +__all__ = ["REQUEST_ID_HEADER", "error_response", "get_request_id"] diff --git a/src/application/api/utils/errors.py b/src/application/api/utils/errors.py new file mode 100644 index 00000000..5c2fc3ce --- /dev/null +++ b/src/application/api/utils/errors.py @@ -0,0 +1,32 @@ +from collections.abc import Mapping + +from fastapi import Request +from fastapi.responses import JSONResponse + +from domain.errors import ErrorBodySchema, ErrorCode, ErrorSchema + +REQUEST_ID_HEADER = "X-Request-Id" + + +def get_request_id(request: Request) -> str: + """ID запроса, назначенный RequestContextMiddleware""" + request_id: str = request.state.request_id + return request_id + + +def error_response( + request_id: str, + status_code: int, + code: ErrorCode, + message: str, + headers: Mapping[str, str] | None = None, +) -> JSONResponse: + """Ответ в едином формате ошибки контракта""" + body = ErrorSchema( + error=ErrorBodySchema(code=code, message=message, request_id=request_id) + ) + return JSONResponse( + status_code=status_code, + content=body.model_dump(mode="json"), + headers={**(headers or {}), REQUEST_ID_HEADER: request_id}, + ) diff --git a/src/config/__init__.py b/src/config/__init__.py new file mode 100644 index 00000000..5a74d2de --- /dev/null +++ b/src/config/__init__.py @@ -0,0 +1,55 @@ +from .log_config import LOGGER +from .settings import ( + APP_ENV, + APP_HOST, + APP_PORT, + DB_COMMAND_TIMEOUT_SEC, + DB_CONNECT_TIMEOUT_SEC, + DB_HOST, + DB_NAME, + DB_PASS, + DB_POOL_MAX_SIZE, + DB_POOL_MIN_SIZE, + DB_PORT, + DB_STARTUP_TIMEOUT_SEC, + DB_USER, + LOG_LEVEL, + PASSWORD_HASH_WORKERS, + SESSION_CACHE_TTL_SECONDS, + SESSION_TTL_SECONDS, + SHUTDOWN_TIMEOUT_SEC, + VALKEY_HOST, + VALKEY_PASS, + VALKEY_POOL_MAX_SIZE, + VALKEY_PORT, + VALKEY_RETRY_AFTER_SEC, + VALKEY_TIMEOUT_SEC, +) + +__all__ = [ + "LOGGER", + "APP_HOST", + "APP_PORT", + "APP_ENV", + "LOG_LEVEL", + "SHUTDOWN_TIMEOUT_SEC", + "DB_USER", + "DB_PASS", + "DB_NAME", + "DB_HOST", + "DB_PORT", + "DB_POOL_MIN_SIZE", + "DB_POOL_MAX_SIZE", + "DB_CONNECT_TIMEOUT_SEC", + "DB_COMMAND_TIMEOUT_SEC", + "DB_STARTUP_TIMEOUT_SEC", + "VALKEY_HOST", + "VALKEY_PORT", + "VALKEY_PASS", + "VALKEY_POOL_MAX_SIZE", + "VALKEY_TIMEOUT_SEC", + "VALKEY_RETRY_AFTER_SEC", + "SESSION_TTL_SECONDS", + "SESSION_CACHE_TTL_SECONDS", + "PASSWORD_HASH_WORKERS", +] diff --git a/src/config/log_config.py b/src/config/log_config.py new file mode 100644 index 00000000..cce05f11 --- /dev/null +++ b/src/config/log_config.py @@ -0,0 +1,12 @@ +"""Сборщик логов""" + +import sys + +from loguru import logger + +from .settings import LOG_LEVEL + +LOGGER = logger + +LOGGER.remove() +LOGGER.add(sys.stdout, level=LOG_LEVEL, diagnose=False) diff --git a/src/config/settings.py b/src/config/settings.py new file mode 100644 index 00000000..45717ec5 --- /dev/null +++ b/src/config/settings.py @@ -0,0 +1,39 @@ +import os +from pathlib import Path + +from envparse import Env + +env = Env() + +ENV_FILE = Path(os.getenv("ENV_FILE", ".env")) +if ENV_FILE.is_file(): + env.read_envfile(path=str(ENV_FILE)) + +APP_HOST = env.str("APP_HOST") +APP_PORT = env.int("APP_PORT") +APP_ENV = env.str("APP_ENV") + +LOG_LEVEL = env.str("LOG_LEVEL") +SHUTDOWN_TIMEOUT_SEC = env.int("SHUTDOWN_TIMEOUT_SEC") + +DB_USER = env.str("DB_USER") +DB_PASS = env.str("DB_PASS") +DB_NAME = env.str("DB_NAME") +DB_HOST = env.str("DB_HOST") +DB_PORT = env.int("DB_PORT") +DB_POOL_MIN_SIZE = env.int("DB_POOL_MIN_SIZE") +DB_POOL_MAX_SIZE = env.int("DB_POOL_MAX_SIZE") +DB_CONNECT_TIMEOUT_SEC = env.float("DB_CONNECT_TIMEOUT_SEC") +DB_COMMAND_TIMEOUT_SEC = env.float("DB_COMMAND_TIMEOUT_SEC") +DB_STARTUP_TIMEOUT_SEC = env.float("DB_STARTUP_TIMEOUT_SEC") + +VALKEY_HOST = env.str("VALKEY_HOST") +VALKEY_PORT = env.int("VALKEY_PORT") +VALKEY_PASS = env.str("VALKEY_PASS") +VALKEY_POOL_MAX_SIZE = env.int("VALKEY_POOL_MAX_SIZE") +VALKEY_TIMEOUT_SEC = env.float("VALKEY_TIMEOUT_SEC") +VALKEY_RETRY_AFTER_SEC = env.float("VALKEY_RETRY_AFTER_SEC") + +SESSION_TTL_SECONDS = env.int("SESSION_TTL_SECONDS") +SESSION_CACHE_TTL_SECONDS = env.int("SESSION_CACHE_TTL_SECONDS") +PASSWORD_HASH_WORKERS = env.int("PASSWORD_HASH_WORKERS") diff --git a/src/domain/__init__.py b/src/domain/__init__.py new file mode 100644 index 00000000..c5bd3a15 --- /dev/null +++ b/src/domain/__init__.py @@ -0,0 +1,24 @@ +from .auth import ActiveSessionSchema, BasicCredentialsSchema, SessionSchema +from .chats import ChatPageSchema, ChatSchema, CreateChatSchema +from .health import HealthSchema +from .messages import CreateMessageSchema, MessagePageSchema, MessageSchema +from .pagination import PageCursorSchema, PageKey +from .users import CreateUserSchema, UserCredentialsSchema, UserSchema + +__all__ = [ + "CreateUserSchema", + "UserSchema", + "UserCredentialsSchema", + "BasicCredentialsSchema", + "SessionSchema", + "ActiveSessionSchema", + "CreateChatSchema", + "ChatSchema", + "ChatPageSchema", + "CreateMessageSchema", + "MessageSchema", + "MessagePageSchema", + "HealthSchema", + "PageKey", + "PageCursorSchema", +] diff --git a/src/domain/auth/__init__.py b/src/domain/auth/__init__.py new file mode 100644 index 00000000..aaf57594 --- /dev/null +++ b/src/domain/auth/__init__.py @@ -0,0 +1,7 @@ +from .schemas import ActiveSessionSchema, BasicCredentialsSchema, SessionSchema + +__all__ = [ + "BasicCredentialsSchema", + "SessionSchema", + "ActiveSessionSchema", +] diff --git a/src/domain/auth/schemas.py b/src/domain/auth/schemas.py new file mode 100644 index 00000000..c2c81b84 --- /dev/null +++ b/src/domain/auth/schemas.py @@ -0,0 +1,35 @@ +from typing import Literal + +from pydantic import BaseModel, ConfigDict + +from domain.users import UserSchema +from domain.utils import UtcDateTime + + +class BasicCredentialsSchema(BaseModel): + """Разобранный заголовок Authorization: Basic""" + + model_config = ConfigDict(frozen=True) + + username: str + password: str + + +class SessionSchema(BaseModel): + """Ответ входа: непрозрачный токен показывается клиенту один раз""" + + model_config = ConfigDict(frozen=True) + + token: str + token_type: Literal["Bearer"] = "Bearer" + expires_at: UtcDateTime + user: UserSchema + + +class ActiveSessionSchema(BaseModel): + """Действующая серверная сессия: владелец и срок; так же лежит в кэше""" + + model_config = ConfigDict(frozen=True) + + user: UserSchema + expires_at: UtcDateTime diff --git a/src/domain/chats/__init__.py b/src/domain/chats/__init__.py new file mode 100644 index 00000000..14dca213 --- /dev/null +++ b/src/domain/chats/__init__.py @@ -0,0 +1,7 @@ +from .schemas import ChatPageSchema, ChatSchema, CreateChatSchema + +__all__ = [ + "CreateChatSchema", + "ChatSchema", + "ChatPageSchema", +] diff --git a/src/domain/chats/schemas.py b/src/domain/chats/schemas.py new file mode 100644 index 00000000..17914795 --- /dev/null +++ b/src/domain/chats/schemas.py @@ -0,0 +1,42 @@ +from typing import Annotated +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + +from domain.utils import NotBlankStr, UtcDateTime + +ChatTitle = Annotated[NotBlankStr, Field(min_length=1, max_length=100)] + + +class CreateChatSchema(BaseModel): + """Запрос создания чата: участники задаются явно, включая вызывающего""" + + model_config = ConfigDict(extra="forbid") + + title: ChatTitle + member_ids: list[UUID] = Field(min_length=1, max_length=100) + + @field_validator("member_ids") + @classmethod + def validate_unique_members(cls, value: list[UUID]) -> list[UUID]: + if len(set(value)) != len(value): + raise ValueError("member_ids не должен содержать дубликаты") + return value + + +class ChatSchema(BaseModel): + """Чат""" + + model_config = ConfigDict(frozen=True) + + id: UUID + title: str + member_ids: list[UUID] + created_at: UtcDateTime + + +class ChatPageSchema(BaseModel): + """Страница чатов текущего пользователя""" + + items: list[ChatSchema] + next_cursor: str | None diff --git a/src/domain/errors/__init__.py b/src/domain/errors/__init__.py new file mode 100644 index 00000000..6f3b717c --- /dev/null +++ b/src/domain/errors/__init__.py @@ -0,0 +1,24 @@ +from .codes import ErrorCode +from .exceptions import ( + AppError, + ConflictError, + InvalidCredentialsError, + InvalidRequestError, + NotFoundError, + UnauthorizedError, + UnavailableError, +) +from .schemas import ErrorBodySchema, ErrorSchema + +__all__ = [ + "ErrorCode", + "AppError", + "InvalidRequestError", + "UnauthorizedError", + "InvalidCredentialsError", + "NotFoundError", + "ConflictError", + "UnavailableError", + "ErrorBodySchema", + "ErrorSchema", +] diff --git a/src/domain/errors/codes.py b/src/domain/errors/codes.py new file mode 100644 index 00000000..f17eebe3 --- /dev/null +++ b/src/domain/errors/codes.py @@ -0,0 +1,15 @@ +from enum import StrEnum + + +class ErrorCode(StrEnum): + """коды ошибок""" + + INVALID_REQUEST = "invalid_request" + UNAUTHORIZED = "unauthorized" + NOT_FOUND = "not_found" + CONFLICT = "conflict" + PAYLOAD_TOO_LARGE = "payload_too_large" + UNSUPPORTED_MEDIA_TYPE = "unsupported_media_type" + RATE_LIMITED = "rate_limited" + UNAVAILABLE = "unavailable" + INTERNAL_ERROR = "internal_error" diff --git a/src/domain/errors/exceptions.py b/src/domain/errors/exceptions.py new file mode 100644 index 00000000..04d1b579 --- /dev/null +++ b/src/domain/errors/exceptions.py @@ -0,0 +1,64 @@ +from http import HTTPStatus + +from .codes import ErrorCode + + +class AppError(Exception): + """Базовая ошибка приложения: статус, машинный код и безопасное сообщение""" + + status_code: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR + code: ErrorCode = ErrorCode.INTERNAL_ERROR + default_message: str = "Внутренняя ошибка сервера" + headers: dict[str, str] = {} + + def __init__(self, message: str | None = None) -> None: + self.message = message or self.default_message + super().__init__(self.message) + + +class InvalidRequestError(AppError): + """Неверные поля, UUID, cursor или limit""" + + status_code = HTTPStatus.BAD_REQUEST + code = ErrorCode.INVALID_REQUEST + default_message = "Некорректный запрос" + + +class UnauthorizedError(AppError): + """Нет действительной Bearer-сессии: токена нет, он истёк, отозван или неизвестен""" + + status_code = HTTPStatus.UNAUTHORIZED + code = ErrorCode.UNAUTHORIZED + default_message = "Требуется действительная сессия: Authorization: Bearer " + headers = {"WWW-Authenticate": 'Bearer realm="messenger"'} + + +class InvalidCredentialsError(UnauthorizedError): + """Неверные Basic-credentials: одинаковый ответ для неизвестного логина и пароля""" + + default_message = "Неверное имя пользователя или пароль" + headers = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'} + + +class NotFoundError(AppError): + """Объекта нет или он чужой: существование чужого объекта не раскрывается""" + + status_code = HTTPStatus.NOT_FOUND + code = ErrorCode.NOT_FOUND + default_message = "Объект не найден" + + +class ConflictError(AppError): + """Конфликт состояния, например занятый username""" + + status_code = HTTPStatus.CONFLICT + code = ErrorCode.CONFLICT + default_message = "Конфликт состояния" + + +class UnavailableError(AppError): + """Приложение не готово обслуживать запросы""" + + status_code = HTTPStatus.SERVICE_UNAVAILABLE + code = ErrorCode.UNAVAILABLE + default_message = "Сервис временно недоступен" diff --git a/src/domain/errors/schemas.py b/src/domain/errors/schemas.py new file mode 100644 index 00000000..29fcc615 --- /dev/null +++ b/src/domain/errors/schemas.py @@ -0,0 +1,21 @@ +from pydantic import BaseModel, ConfigDict + +from .codes import ErrorCode + + +class ErrorBodySchema(BaseModel): + """Тело ошибки""" + + model_config = ConfigDict(extra="forbid") + + code: ErrorCode + message: str + request_id: str + + +class ErrorSchema(BaseModel): + """Единый формат ошибки: {"error": {...}}""" + + model_config = ConfigDict(extra="forbid") + + error: ErrorBodySchema diff --git a/src/domain/health/__init__.py b/src/domain/health/__init__.py new file mode 100644 index 00000000..d9f144b5 --- /dev/null +++ b/src/domain/health/__init__.py @@ -0,0 +1,3 @@ +from .schemas import HealthSchema + +__all__ = ["HealthSchema"] diff --git a/src/domain/health/schemas.py b/src/domain/health/schemas.py new file mode 100644 index 00000000..81993212 --- /dev/null +++ b/src/domain/health/schemas.py @@ -0,0 +1,9 @@ +from typing import Literal + +from pydantic import BaseModel + + +class HealthSchema(BaseModel): + """Ответ health-проверок""" + + status: Literal["ok"] = "ok" diff --git a/src/domain/messages/__init__.py b/src/domain/messages/__init__.py new file mode 100644 index 00000000..2fa9747b --- /dev/null +++ b/src/domain/messages/__init__.py @@ -0,0 +1,7 @@ +from .schemas import CreateMessageSchema, MessagePageSchema, MessageSchema + +__all__ = [ + "CreateMessageSchema", + "MessageSchema", + "MessagePageSchema", +] diff --git a/src/domain/messages/schemas.py b/src/domain/messages/schemas.py new file mode 100644 index 00000000..a2adfbc2 --- /dev/null +++ b/src/domain/messages/schemas.py @@ -0,0 +1,35 @@ +from typing import Annotated +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field + +from domain.utils import NotBlankStr, UtcDateTime + +MessageText = Annotated[NotBlankStr, Field(min_length=1, max_length=4000)] + + +class CreateMessageSchema(BaseModel): + """Запрос отправки сообщения: отправитель берётся из текущей сессии""" + + model_config = ConfigDict(extra="forbid") + + text: MessageText + + +class MessageSchema(BaseModel): + """Сообщение""" + + model_config = ConfigDict(frozen=True) + + id: UUID + chat_id: UUID + sender_id: UUID + text: str + created_at: UtcDateTime + + +class MessagePageSchema(BaseModel): + """Страница истории чата""" + + items: list[MessageSchema] + next_cursor: str | None diff --git a/src/domain/pagination/__init__.py b/src/domain/pagination/__init__.py new file mode 100644 index 00000000..cb7ef9c7 --- /dev/null +++ b/src/domain/pagination/__init__.py @@ -0,0 +1,6 @@ +from .schemas import PageCursorSchema, PageKey + +__all__ = [ + "PageKey", + "PageCursorSchema", +] diff --git a/src/domain/pagination/schemas.py b/src/domain/pagination/schemas.py new file mode 100644 index 00000000..f5be98be --- /dev/null +++ b/src/domain/pagination/schemas.py @@ -0,0 +1,26 @@ +from datetime import datetime +from typing import NamedTuple +from uuid import UUID + +from pydantic import BaseModel, ConfigDict + + +class PageKey(NamedTuple): + """Ключ keyset-пагинации: порядок (created_at, id) по возрастанию""" + + created_at: datetime + id: UUID + + +class PageCursorSchema(BaseModel): + """Содержимое непрозрачного cursor: ключ последнего элемента и его ресурс""" + + model_config = ConfigDict(extra="forbid", frozen=True) + + scope: str + created_at: datetime + id: UUID + + @property + def key(self) -> PageKey: + return PageKey(self.created_at, self.id) diff --git a/src/domain/users/__init__.py b/src/domain/users/__init__.py new file mode 100644 index 00000000..e483acc3 --- /dev/null +++ b/src/domain/users/__init__.py @@ -0,0 +1,7 @@ +from .schemas import CreateUserSchema, UserCredentialsSchema, UserSchema + +__all__ = [ + "CreateUserSchema", + "UserSchema", + "UserCredentialsSchema", +] diff --git a/src/domain/users/schemas.py b/src/domain/users/schemas.py new file mode 100644 index 00000000..f771fc1d --- /dev/null +++ b/src/domain/users/schemas.py @@ -0,0 +1,40 @@ +from typing import Annotated +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, StrictStr + +from domain.utils import NotBlankStr, UtcDateTime + +Username = Annotated[StrictStr, Field(pattern=r"^[a-z0-9_]{3,32}$")] +DisplayName = Annotated[NotBlankStr, Field(min_length=1, max_length=100)] +Password = Annotated[StrictStr, Field(min_length=12, max_length=128)] + + +class CreateUserSchema(BaseModel): + """Запрос регистрации пользователя; длина пароля — в Unicode code points""" + + model_config = ConfigDict(extra="forbid") + + username: Username + display_name: DisplayName + password: Password = Field(json_schema_extra={"writeOnly": True}) + + +class UserSchema(BaseModel): + """Пользователь: пароль и его hash сюда никогда не попадают""" + + model_config = ConfigDict(frozen=True) + + id: UUID + username: str + display_name: str + created_at: UtcDateTime + + +class UserCredentialsSchema(BaseModel): + """Пользователь вместе с hash пароля — только для проверки входа""" + + model_config = ConfigDict(frozen=True) + + user: UserSchema + password_hash: str diff --git a/src/domain/utils/__init__.py b/src/domain/utils/__init__.py new file mode 100644 index 00000000..de0c0c1f --- /dev/null +++ b/src/domain/utils/__init__.py @@ -0,0 +1,15 @@ +from .types import ( + NotBlankStr, + UtcDateTime, + ensure_not_blank, + ensure_storable, + to_rfc3339_utc, +) + +__all__ = [ + "NotBlankStr", + "UtcDateTime", + "ensure_not_blank", + "ensure_storable", + "to_rfc3339_utc", +] diff --git a/src/domain/utils/types.py b/src/domain/utils/types.py new file mode 100644 index 00000000..c2e88a6c --- /dev/null +++ b/src/domain/utils/types.py @@ -0,0 +1,36 @@ +from datetime import UTC, datetime +from typing import Annotated + +from pydantic import AfterValidator, PlainSerializer, StrictStr + +NUL_CHARACTER = "\x00" + + +def ensure_not_blank(value: str) -> str: + """Обязательная строка не может состоять только из пробельных символов""" + if not value.strip(): + raise ValueError("Строка не может быть пустой или пробельной") + return value + + +def ensure_storable(value: str) -> str: + """Тип text в потсгре""" + if NUL_CHARACTER in value: + raise ValueError("Строка не может содержать символ NUL") + return value + + +def to_rfc3339_utc(value: datetime) -> str: + """RFC 3339 в UTC с суффиксом Z и микросекундами""" + return ( + value.astimezone(UTC).isoformat(timespec="microseconds").replace("+00:00", "Z") + ) + + +NotBlankStr = Annotated[ + StrictStr, AfterValidator(ensure_not_blank), AfterValidator(ensure_storable) +] + +UtcDateTime = Annotated[ + datetime, PlainSerializer(to_rfc3339_utc, return_type=str, when_used="json") +] diff --git a/src/gateway.py b/src/gateway.py new file mode 100644 index 00000000..535ed6c3 --- /dev/null +++ b/src/gateway.py @@ -0,0 +1,71 @@ +from collections.abc import AsyncIterator +from contextlib import asynccontextmanager + +import uvicorn +from fastapi import FastAPI + +from application import api_endpoints, health_endpoints +from application.api.handlers import register_exception_handlers +from application.api.middlewares import RequestContextMiddleware +from config import ( + APP_ENV, + APP_HOST, + APP_PORT, + DB_STARTUP_TIMEOUT_SEC, + LOGGER, + PASSWORD_HASH_WORKERS, + SESSION_TTL_SECONDS, + SHUTDOWN_TIMEOUT_SEC, +) +from infrastructure import PasswordHasher, cache_manager, database_pool_manager + + +@asynccontextmanager +async def lifespan(app: FastAPI) -> AsyncIterator[None]: + await database_pool_manager.create_pool(DB_STARTUP_TIMEOUT_SEC) + await database_pool_manager.apply_migrations() + cache_manager.create_client() + password_hasher = PasswordHasher(PASSWORD_HASH_WORKERS) + app.state.database = database_pool_manager + app.state.cache = cache_manager + app.state.password_hasher = password_hasher + app.state.is_ready = True + LOGGER.info( + f"Application started, env={APP_ENV}, session ttl={SESSION_TTL_SECONDS} s" + ) + yield + app.state.is_ready = False + await cache_manager.close() + await database_pool_manager.close() + password_hasher.close() + LOGGER.info("Application stopped, connections closed") + + +app = FastAPI( + title="mtuci-labs-messanger", + description="мессенджер-конкурент-максу", + version="1.2.0", + lifespan=lifespan, +) + +app.add_middleware(RequestContextMiddleware) +register_exception_handlers(app) + +app.include_router(health_endpoints) +app.include_router(api_endpoints) + + +def main(host: str, port: int, shutdown_timeout: int) -> None: + """Функция для запуска api""" + uvicorn.run( + app, + host=host, + port=port, + timeout_graceful_shutdown=shutdown_timeout, + access_log=False, + server_header=False, + ) + + +if __name__ == "__main__": + main(host=APP_HOST, port=APP_PORT, shutdown_timeout=SHUTDOWN_TIMEOUT_SEC) diff --git a/src/healthcheck.py b/src/healthcheck.py new file mode 100644 index 00000000..66d7ed89 --- /dev/null +++ b/src/healthcheck.py @@ -0,0 +1,20 @@ +import sys +import urllib.request + +from config import APP_PORT + +READY_URL = f"http://127.0.0.1:{APP_PORT}/health/ready" + +REQUEST_TIMEOUT_SEC = 2 + + +def main() -> int: + try: + with urllib.request.urlopen(READY_URL, timeout=REQUEST_TIMEOUT_SEC) as response: + return 0 if response.status == 200 else 1 + except OSError: + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/infrastructure/__init__.py b/src/infrastructure/__init__.py new file mode 100644 index 00000000..58bcd4d7 --- /dev/null +++ b/src/infrastructure/__init__.py @@ -0,0 +1,4 @@ +from .dependencies import cache_manager, database_pool_manager +from .security import PasswordHasher + +__all__ = ["database_pool_manager", "cache_manager", "PasswordHasher"] diff --git a/src/infrastructure/dependencies/__init__.py b/src/infrastructure/dependencies/__init__.py new file mode 100644 index 00000000..0e85bb76 --- /dev/null +++ b/src/infrastructure/dependencies/__init__.py @@ -0,0 +1,13 @@ +from .cache import CacheManager, cache_manager, get_cache +from .database import DatabasePoolManager, database_pool_manager, get_database +from .service_creator import ServiceCreator + +__all__ = [ + "ServiceCreator", + "DatabasePoolManager", + "database_pool_manager", + "get_database", + "CacheManager", + "cache_manager", + "get_cache", +] diff --git a/src/infrastructure/dependencies/cache.py b/src/infrastructure/dependencies/cache.py new file mode 100644 index 00000000..0fa840b9 --- /dev/null +++ b/src/infrastructure/dependencies/cache.py @@ -0,0 +1,94 @@ +import time + +from redis.asyncio import BlockingConnectionPool, Redis +from redis.asyncio.retry import Retry +from redis.backoff import NoBackoff +from starlette.requests import HTTPConnection + +from config import ( + LOGGER, + VALKEY_HOST, + VALKEY_PASS, + VALKEY_POOL_MAX_SIZE, + VALKEY_PORT, + VALKEY_RETRY_AFTER_SEC, + VALKEY_TIMEOUT_SEC, +) + + +class CacheManager: + """ + Клиент Valkey: ограниченный пул, короткие таймауты, без встроенных повторов. + """ + + def __init__( + self, + host: str, + port: int, + password: str, + max_size: int, + timeout: float, + retry_after: float, + ) -> None: + self._host = host + self._port = port + self._password = password + self.max_size = max_size + self.timeout = timeout + self.retry_after = retry_after + self.client: Redis | None = None + self._disabled_until = 0.0 + + def create_client(self) -> Redis: + """Создание клиента; соединения открываются лениво при первом запросе""" + if self.client is None: + pool = BlockingConnectionPool( + host=self._host, + port=self._port, + password=self._password, + max_connections=self.max_size, + timeout=self.timeout, + socket_timeout=self.timeout, + socket_connect_timeout=self.timeout, + retry=Retry(NoBackoff(), 0), + ) + self.client = Redis(connection_pool=pool) + LOGGER.info( + f"Клиент Valkey создан: {self._host}:{self._port}, " + f"pool: {self.max_size}, timeout: {self.timeout} с" + ) + return self.client + + @property + def is_available(self) -> bool: + return time.monotonic() >= self._disabled_until + + def mark_failed(self, error: Exception) -> None: + """Обход кэша на retry_after секунд после ошибки Valkey""" + if self.is_available: + LOGGER.warning( + f"Valkey недоступен ({type(error).__name__}): кэш сессий " + f"обходится {self.retry_after} с, запросы идут в PostgreSQL" + ) + self._disabled_until = time.monotonic() + self.retry_after + + async def close(self) -> None: + if self.client is not None: + await self.client.aclose() + self.client = None + + +def get_cache(connection: HTTPConnection) -> CacheManager: + """Получение менеджера кэша, созданного в lifespan.""" + cache: CacheManager = connection.app.state.cache + return cache + + +cache_manager = CacheManager( + host=VALKEY_HOST, + port=VALKEY_PORT, + password=VALKEY_PASS, + max_size=VALKEY_POOL_MAX_SIZE, + timeout=VALKEY_TIMEOUT_SEC, + retry_after=VALKEY_RETRY_AFTER_SEC, +) diff --git a/src/infrastructure/dependencies/database.py b/src/infrastructure/dependencies/database.py new file mode 100644 index 00000000..86ba2376 --- /dev/null +++ b/src/infrastructure/dependencies/database.py @@ -0,0 +1,206 @@ +import asyncio +import time +from collections.abc import AsyncIterator +from contextlib import asynccontextmanager +from pathlib import Path + +import asyncpg +from starlette.requests import HTTPConnection + +from config import ( + DB_COMMAND_TIMEOUT_SEC, + DB_CONNECT_TIMEOUT_SEC, + DB_HOST, + DB_NAME, + DB_PASS, + DB_POOL_MAX_SIZE, + DB_POOL_MIN_SIZE, + DB_PORT, + DB_USER, + LOGGER, +) +from infrastructure.utils import DATABASE_UNAVAILABLE_ERRORS + +MIGRATIONS_DIR = Path(__file__).resolve().parents[3] / "migrations" + +MIGRATIONS_LOCK_NAMESPACE = 1 + +STARTUP_RETRY_INTERVAL_SEC = 1 + +BROKEN_CONNECTION_ERRORS: tuple[type[Exception], ...] = ( + asyncpg.PostgresConnectionError, + asyncpg.InterfaceError, + asyncpg.exceptions.OperatorInterventionError, +) + +MIGRATIONS_TIMEOUT_SEC = 300 + + +class DatabasePoolManager: + """Класс для управления пулом соединений и версионируемыми миграциями.""" + + def __init__( + self, + user: str, + password: str, + db: str, + host: str, + port: int, + min_size: int, + max_size: int, + connect_timeout: float, + command_timeout: float, + ) -> None: + self._user = user + self._password = password + self._host = host + self._port = port + self._database = db + self.min_size = min_size + self.max_size = max_size + self.connect_timeout = connect_timeout + self.command_timeout = command_timeout + self.pool: asyncpg.Pool | None = None + + async def create_pool(self, startup_timeout: float) -> asyncpg.Pool: + """ + Создание ограниченного пула соединений. + """ + LOGGER.info("Установка соединения с базой данных psql") + deadline = time.monotonic() + startup_timeout + while self.pool is None: + try: + self.pool = await asyncpg.create_pool( + user=self._user, + password=self._password, + host=self._host, + port=self._port, + database=self._database, + min_size=self.min_size, + max_size=self.max_size, + timeout=self.connect_timeout, + command_timeout=self.command_timeout, + server_settings={ + "application_name": "messenger-api", + "statement_timeout": str(int(self.command_timeout * 1000)), + }, + ) + except (*DATABASE_UNAVAILABLE_ERRORS, asyncpg.PostgresError) as error: + if time.monotonic() >= deadline: + LOGGER.error( + "Не удалось подключиться к psql за " + f"{startup_timeout} с: {type(error).__name__}" + ) + raise + LOGGER.warning( + f"PostgreSQL ещё недоступен: {type(error).__name__}, повтор" + ) + await asyncio.sleep(STARTUP_RETRY_INTERVAL_SEC) + LOGGER.info( + f"Соединение с базой данных psql установлено. DB_NAME: " + f"{self._database}, pool: {self.min_size}..{self.max_size}" + ) + return self.pool + + async def apply_migrations(self, directory: Path = MIGRATIONS_DIR) -> None: + """ + Применение миграций NNNN_name.sql по порядку версий. + + Каждая миграция и запись о ней в schema_migrations — одна транзакция. + Advisory lock не даёт двум процессам API применять миграции одновременно; + уже применённые версии пропускаются, поэтому повторный запуск безопасен. + """ + async with self.acquire() as conn: + await conn.execute("SET statement_timeout = 0;") + await conn.execute( + "SELECT pg_advisory_lock($1, 0);", + MIGRATIONS_LOCK_NAMESPACE, + timeout=MIGRATIONS_TIMEOUT_SEC, + ) + try: + await conn.execute( + """ + CREATE TABLE IF NOT EXISTS schema_migrations ( + version integer PRIMARY KEY, + name text NOT NULL, + applied_at timestamptz NOT NULL DEFAULT clock_timestamp() + ); + """ + ) + applied = { + record["version"] + for record in await conn.fetch( + "SELECT version FROM schema_migrations;" + ) + } + for path in sorted(directory.glob("*.sql")): + version = int(path.name.split("_", 1)[0]) + if version in applied: + continue + async with conn.transaction(): + await conn.execute( + path.read_text(encoding="utf-8"), + timeout=MIGRATIONS_TIMEOUT_SEC, + ) + await conn.execute( + "INSERT INTO schema_migrations (version, name) " + "VALUES ($1, $2);", + version, + path.stem, + ) + LOGGER.info(f"Применена миграция {path.name}") + finally: + await conn.execute( + "SELECT pg_advisory_unlock($1, 0);", MIGRATIONS_LOCK_NAMESPACE + ) + LOGGER.info("Схема БД в актуальной версии") + + @asynccontextmanager + async def acquire(self) -> AsyncIterator[asyncpg.Connection]: + """ + Получение соединения из пула с ограниченным ожиданием свободного. + + После рестарта PostgreSQL все соединения пула мертвы. На первой же + ошибке соединения пул помечает их на переподключение, чтобы 503 + получил один запрос, а не каждый из max_size следующих. + """ + assert self.pool is not None + try: + async with self.pool.acquire(timeout=self.connect_timeout) as connection: + yield connection + except BROKEN_CONNECTION_ERRORS: + await self.pool.expire_connections() + raise + + async def check(self) -> bool: + """PostgreSQL отвечает на запрос — для readiness""" + try: + async with self.acquire() as conn: + await conn.fetchval("SELECT 1;", timeout=self.connect_timeout) + except DATABASE_UNAVAILABLE_ERRORS: + return False + return True + + async def close(self) -> None: + if self.pool: + await self.pool.close() + self.pool = None + + +def get_database(connection: HTTPConnection) -> DatabasePoolManager: + """Получение менеджера пула соединений, созданного в lifespan.""" + database: DatabasePoolManager = connection.app.state.database + return database + + +database_pool_manager = DatabasePoolManager( + user=DB_USER, + password=DB_PASS, + db=DB_NAME, + host=DB_HOST, + port=DB_PORT, + min_size=DB_POOL_MIN_SIZE, + max_size=DB_POOL_MAX_SIZE, + connect_timeout=DB_CONNECT_TIMEOUT_SEC, + command_timeout=DB_COMMAND_TIMEOUT_SEC, +) diff --git a/src/infrastructure/dependencies/service_creator.py b/src/infrastructure/dependencies/service_creator.py new file mode 100644 index 00000000..7e42b54e --- /dev/null +++ b/src/infrastructure/dependencies/service_creator.py @@ -0,0 +1,93 @@ +from fastapi import Depends + +from config import SESSION_CACHE_TTL_SECONDS, SESSION_TTL_SECONDS +from infrastructure.dependencies.cache import CacheManager, get_cache +from infrastructure.dependencies.database import DatabasePoolManager, get_database +from infrastructure.repositories import ( + PostgresChatRepository, + PostgresMessageRepository, + PostgresSessionRepository, + PostgresUserRepository, + ValkeySessionCache, +) +from infrastructure.security import PasswordHasher, get_password_hasher +from infrastructure.services import ( + AuthService, + ChatService, + MessageService, + UserService, +) +from interfaces import ( + ChatRepository, + MessageRepository, + SessionCache, + SessionRepository, + UserRepository, +) + + +class ServiceCreator: + @staticmethod + def _get_user_repository( + database: DatabasePoolManager = Depends(get_database), + ) -> UserRepository: + return PostgresUserRepository(database) + + @staticmethod + def _get_chat_repository( + database: DatabasePoolManager = Depends(get_database), + ) -> ChatRepository: + return PostgresChatRepository(database) + + @staticmethod + def _get_message_repository( + database: DatabasePoolManager = Depends(get_database), + ) -> MessageRepository: + return PostgresMessageRepository(database) + + @staticmethod + def _get_session_repository( + database: DatabasePoolManager = Depends(get_database), + ) -> SessionRepository: + return PostgresSessionRepository(database) + + @staticmethod + def _get_session_cache( + cache: CacheManager = Depends(get_cache), + ) -> SessionCache: + return ValkeySessionCache(cache, SESSION_CACHE_TTL_SECONDS) + + @staticmethod + def get_user_service( + repository: UserRepository = Depends(_get_user_repository), + hasher: PasswordHasher = Depends(get_password_hasher), + ) -> UserService: + return UserService(repository, hasher) + + @staticmethod + def get_auth_service( + user_repository: UserRepository = Depends(_get_user_repository), + session_repository: SessionRepository = Depends(_get_session_repository), + session_cache: SessionCache = Depends(_get_session_cache), + hasher: PasswordHasher = Depends(get_password_hasher), + ) -> AuthService: + return AuthService( + user_repository, + session_repository, + session_cache, + hasher, + SESSION_TTL_SECONDS, + ) + + @staticmethod + def get_chat_service( + chat_repository: ChatRepository = Depends(_get_chat_repository), + ) -> ChatService: + return ChatService(chat_repository) + + @staticmethod + def get_message_service( + chat_repository: ChatRepository = Depends(_get_chat_repository), + message_repository: MessageRepository = Depends(_get_message_repository), + ) -> MessageService: + return MessageService(chat_repository, message_repository) diff --git a/src/infrastructure/repositories/__init__.py b/src/infrastructure/repositories/__init__.py new file mode 100644 index 00000000..2a845b1c --- /dev/null +++ b/src/infrastructure/repositories/__init__.py @@ -0,0 +1,13 @@ +from .chat_repository import PostgresChatRepository +from .message_repository import PostgresMessageRepository +from .session_cache import ValkeySessionCache +from .session_repository import PostgresSessionRepository +from .user_repository import PostgresUserRepository + +__all__ = [ + "PostgresUserRepository", + "PostgresChatRepository", + "PostgresMessageRepository", + "PostgresSessionRepository", + "ValkeySessionCache", +] diff --git a/src/infrastructure/repositories/chat_repository/__init__.py b/src/infrastructure/repositories/chat_repository/__init__.py new file mode 100644 index 00000000..822c590b --- /dev/null +++ b/src/infrastructure/repositories/chat_repository/__init__.py @@ -0,0 +1,3 @@ +from .postgres import PostgresChatRepository + +__all__ = ["PostgresChatRepository"] diff --git a/src/infrastructure/repositories/chat_repository/postgres.py b/src/infrastructure/repositories/chat_repository/postgres.py new file mode 100644 index 00000000..455fd442 --- /dev/null +++ b/src/infrastructure/repositories/chat_repository/postgres.py @@ -0,0 +1,150 @@ +from uuid import UUID + +import asyncpg + +from domain import ChatSchema, PageKey +from domain.errors import InvalidRequestError +from infrastructure.utils import Pool, repository_logs, transaction +from interfaces import ChatRepository + + +class PostgresChatRepository(ChatRepository): + def __init__(self, pool: Pool) -> None: + self._pool = pool + + @repository_logs + async def create( + self, title: str, created_by: UUID, member_ids: list[UUID] + ) -> ChatSchema: + """ + Чат и участники — одна транзакция. Неизвестный участник нарушает FK + на users, транзакция откатывается целиком, строка chats не остаётся. + Участники вставляются одним запросом; position — порядок в member_ids. + """ + chat_query = """ + INSERT INTO chats + ( + title, + created_by + ) + VALUES + ( + $1, + $2 + ) + RETURNING id, title, created_at; + """ + members_query = """ + INSERT INTO chat_members + ( + chat_id, + user_id, + position + ) + SELECT + $1, + member.user_id, + member.ordinality - 1 + FROM unnest($2::uuid[]) WITH ORDINALITY AS member(user_id, ordinality); + """ + try: + async with transaction(self._pool) as conn: + row = await conn.fetchrow(chat_query, title, created_by) + assert row is not None + await conn.execute(members_query, row["id"], member_ids) + except asyncpg.ForeignKeyViolationError: + raise InvalidRequestError( + "member_ids содержит неизвестных пользователей" + ) from None + return ChatSchema( + id=row["id"], + title=row["title"], + member_ids=member_ids, + created_at=row["created_at"], + ) + + @repository_logs + async def get_for_member(self, chat_id: UUID, member_id: UUID) -> ChatSchema | None: + query = """ + SELECT + chat.id, + chat.title, + chat.created_at, + ARRAY( + SELECT member.user_id + FROM chat_members member + WHERE member.chat_id = chat.id + ORDER BY member.position + ) AS member_ids + FROM chats chat + WHERE + chat.id = $1 + AND EXISTS + ( + SELECT 1 + FROM chat_members caller + WHERE caller.chat_id = chat.id AND caller.user_id = $2 + ); + """ + async with self._pool.acquire() as conn: + row = await conn.fetchrow(query, chat_id, member_id) + return self._row_to_chat(row) if row is not None else None + + @repository_logs + async def list_for_member( + self, member_id: UUID, after: PageKey | None, limit: int + ) -> list[ChatSchema]: + """Чаты пользователя по (created_at, id); со второй страницы — после ключа""" + first_page_query = """ + SELECT + chat.id, + chat.title, + chat.created_at, + ARRAY( + SELECT member.user_id + FROM chat_members member + WHERE member.chat_id = chat.id + ORDER BY member.position + ) AS member_ids + FROM chat_members caller + JOIN chats chat ON chat.id = caller.chat_id + WHERE caller.user_id = $1 + ORDER BY chat.created_at, chat.id + LIMIT $2; + """ + next_page_query = """ + SELECT + chat.id, + chat.title, + chat.created_at, + ARRAY( + SELECT member.user_id + FROM chat_members member + WHERE member.chat_id = chat.id + ORDER BY member.position + ) AS member_ids + FROM chat_members caller + JOIN chats chat ON chat.id = caller.chat_id + WHERE + caller.user_id = $1 + AND (chat.created_at, chat.id) > ($2, $3) + ORDER BY chat.created_at, chat.id + LIMIT $4; + """ + async with self._pool.acquire() as conn: + if after is None: + rows = await conn.fetch(first_page_query, member_id, limit) + else: + rows = await conn.fetch( + next_page_query, member_id, after.created_at, after.id, limit + ) + return [self._row_to_chat(row) for row in rows] + + @staticmethod + def _row_to_chat(row: asyncpg.Record) -> ChatSchema: + return ChatSchema( + id=row["id"], + title=row["title"], + member_ids=row["member_ids"], + created_at=row["created_at"], + ) diff --git a/src/infrastructure/repositories/message_repository/__init__.py b/src/infrastructure/repositories/message_repository/__init__.py new file mode 100644 index 00000000..586dcf44 --- /dev/null +++ b/src/infrastructure/repositories/message_repository/__init__.py @@ -0,0 +1,3 @@ +from .postgres import PostgresMessageRepository + +__all__ = ["PostgresMessageRepository"] diff --git a/src/infrastructure/repositories/message_repository/postgres.py b/src/infrastructure/repositories/message_repository/postgres.py new file mode 100644 index 00000000..f8aa343c --- /dev/null +++ b/src/infrastructure/repositories/message_repository/postgres.py @@ -0,0 +1,106 @@ +from uuid import UUID + +import asyncpg + +from domain import MessageSchema, PageKey +from infrastructure.utils import Pool, repository_logs, transaction +from interfaces import MessageRepository + +# Пространство ключей advisory lock: (namespace, hashtext(chat_id)) +CHAT_LOCK_NAMESPACE = 2 + + +class PostgresMessageRepository(MessageRepository): + def __init__(self, pool: Pool) -> None: + self._pool = pool + + @repository_logs + async def create(self, chat_id: UUID, sender_id: UUID, text: str) -> MessageSchema: + """ + Блокировка чата и вставка — одна транзакция; lock снимается на COMMIT. + + Запись в один чат сериализуется, записи в разные чаты не мешают друг другу. + created_at строго больше последнего в чате, даже если часы ушли назад: + порядок коммитов совпадает с порядком (created_at, id), и читатель + с cursor не пропустит сообщение. MAX берётся из индекса + (chat_id, created_at, id) — Index Only Scan Backward, а не проход по истории. + """ + lock_query = """ + SELECT pg_advisory_xact_lock($1, hashtext($2::uuid::text)); + """ + insert_query = """ + INSERT INTO messages + ( + chat_id, + sender_id, + text, + created_at + ) + SELECT + $1, + $2, + $3, + GREATEST(clock_timestamp(), MAX(created_at) + interval '1 microsecond') + FROM messages + WHERE chat_id = $1 + RETURNING id, chat_id, sender_id, text, created_at; + """ + async with transaction(self._pool) as conn: + await conn.execute(lock_query, CHAT_LOCK_NAMESPACE, chat_id) + row = await conn.fetchrow(insert_query, chat_id, sender_id, text) + assert row is not None + return self._row_to_message(row) + + @repository_logs + async def list_for_chat( + self, chat_id: UUID, after: PageKey | None, limit: int + ) -> list[MessageSchema]: + """ + История по (created_at, id). Со второй страницы Index Cond по + (chat_id, created_at, id) начинает чтение сразу после cursor + и останавливается после LIMIT строк. + """ + first_page_query = """ + SELECT + id, + chat_id, + sender_id, + text, + created_at + FROM messages + WHERE chat_id = $1 + ORDER BY created_at, id + LIMIT $2; + """ + next_page_query = """ + SELECT + id, + chat_id, + sender_id, + text, + created_at + FROM messages + WHERE + chat_id = $1 + AND (created_at, id) > ($2, $3) + ORDER BY created_at, id + LIMIT $4; + """ + async with self._pool.acquire() as conn: + if after is None: + rows = await conn.fetch(first_page_query, chat_id, limit) + else: + rows = await conn.fetch( + next_page_query, chat_id, after.created_at, after.id, limit + ) + return [self._row_to_message(row) for row in rows] + + @staticmethod + def _row_to_message(row: asyncpg.Record) -> MessageSchema: + return MessageSchema( + id=row["id"], + chat_id=row["chat_id"], + sender_id=row["sender_id"], + text=row["text"], + created_at=row["created_at"], + ) diff --git a/src/infrastructure/repositories/session_cache/__init__.py b/src/infrastructure/repositories/session_cache/__init__.py new file mode 100644 index 00000000..737a87e7 --- /dev/null +++ b/src/infrastructure/repositories/session_cache/__init__.py @@ -0,0 +1,3 @@ +from .valkey import ValkeySessionCache + +__all__ = ["ValkeySessionCache"] diff --git a/src/infrastructure/repositories/session_cache/valkey.py b/src/infrastructure/repositories/session_cache/valkey.py new file mode 100644 index 00000000..a5e232e5 --- /dev/null +++ b/src/infrastructure/repositories/session_cache/valkey.py @@ -0,0 +1,87 @@ +import math +from datetime import UTC, datetime +from typing import TYPE_CHECKING + +from pydantic import ValidationError +from redis.exceptions import RedisError + +from config import LOGGER +from domain import ActiveSessionSchema +from interfaces import SessionCache + +if TYPE_CHECKING: + from infrastructure.dependencies.cache import CacheManager + +KEY_PREFIX = "session:" + +CACHE_UNAVAILABLE_ERRORS: tuple[type[Exception], ...] = ( + RedisError, + OSError, + TimeoutError, +) + + +class ValkeySessionCache(SessionCache): + """ + Read-through кэш сессий в Valkey. + + Ключ session:, значение — JSON ActiveSessionSchema. + TTL = min(остаток жизни сессии, max_ttl): запись исчезает не позже сессии, + а max_ttl ограничивает окно устаревания, если инвалидация не дошла. + """ + + def __init__(self, cache: "CacheManager", max_ttl_seconds: int) -> None: + self._cache = cache + self._max_ttl_ms = max_ttl_seconds * 1000 + + async def get(self, token_hash: bytes) -> ActiveSessionSchema | None: + if not self._cache.is_available or self._cache.client is None: + return None + try: + raw = await self._cache.client.get(self._key(token_hash)) + except CACHE_UNAVAILABLE_ERRORS as error: + self._cache.mark_failed(error) + return None + if raw is None: + return None + try: + return ActiveSessionSchema.model_validate_json(raw) + except ValidationError: + LOGGER.warning("Повреждённая запись кэша сессии проигнорирована") + return None + + async def set(self, token_hash: bytes, session: ActiveSessionSchema) -> None: + if not self._cache.is_available or self._cache.client is None: + return + remaining = session.expires_at - datetime.now(UTC) + ttl_ms = min(self._max_ttl_ms, math.floor(remaining.total_seconds() * 1000)) + if ttl_ms <= 0: + return + try: + await self._cache.client.set( + self._key(token_hash), session.model_dump_json(), px=ttl_ms + ) + except CACHE_UNAVAILABLE_ERRORS as error: + self._cache.mark_failed(error) + + async def delete(self, token_hash: bytes) -> bool: + if self._cache.client is None: + return False + try: + await self._cache.client.delete(self._key(token_hash)) + except CACHE_UNAVAILABLE_ERRORS as error: + self._cache.mark_failed(error) + return False + return True + + async def ping(self) -> bool: + if self._cache.client is None: + return False + try: + return bool(await self._cache.client.ping()) + except CACHE_UNAVAILABLE_ERRORS: + return False + + @staticmethod + def _key(token_hash: bytes) -> str: + return KEY_PREFIX + token_hash.hex() diff --git a/src/infrastructure/repositories/session_repository/__init__.py b/src/infrastructure/repositories/session_repository/__init__.py new file mode 100644 index 00000000..c3887458 --- /dev/null +++ b/src/infrastructure/repositories/session_repository/__init__.py @@ -0,0 +1,3 @@ +from .postgres import PostgresSessionRepository + +__all__ = ["PostgresSessionRepository"] diff --git a/src/infrastructure/repositories/session_repository/postgres.py b/src/infrastructure/repositories/session_repository/postgres.py new file mode 100644 index 00000000..1bbf1537 --- /dev/null +++ b/src/infrastructure/repositories/session_repository/postgres.py @@ -0,0 +1,85 @@ +from datetime import datetime +from uuid import UUID + +from domain import ActiveSessionSchema, UserSchema +from infrastructure.utils import Pool, repository_logs +from interfaces import SessionRepository + + +class PostgresSessionRepository(SessionRepository): + def __init__(self, pool: Pool) -> None: + self._pool = pool + + @repository_logs + async def create( + self, token_hash: bytes, user_id: UUID, ttl_seconds: int + ) -> datetime: + query = """ + INSERT INTO sessions + ( + token_hash, + user_id, + expires_at + ) + VALUES + ( + $1, + $2, + clock_timestamp() + make_interval(secs => $3::integer) + ) + RETURNING expires_at; + """ + async with self._pool.acquire() as conn: + expires_at: datetime = await conn.fetchval( + query, token_hash, user_id, ttl_seconds + ) + return expires_at + + @repository_logs + async def get_active(self, token_hash: bytes) -> ActiveSessionSchema | None: + """Поиск по PK; истёкшая или отозванная сессия не возвращается""" + query = """ + SELECT + usr.id, + usr.username, + usr.display_name, + usr.created_at, + session.expires_at + FROM sessions session + JOIN users usr ON usr.id = session.user_id + WHERE + session.token_hash = $1 + AND session.revoked_at IS NULL + AND session.expires_at > clock_timestamp(); + """ + async with self._pool.acquire() as conn: + row = await conn.fetchrow(query, token_hash) + if row is None: + return None + return ActiveSessionSchema( + user=UserSchema( + id=row["id"], + username=row["username"], + display_name=row["display_name"], + created_at=row["created_at"], + ), + expires_at=row["expires_at"], + ) + + @repository_logs + async def revoke(self, token_hash: bytes) -> bool: + query = """ + UPDATE sessions + SET revoked_at = clock_timestamp() + WHERE + token_hash = $1 + AND revoked_at IS NULL + AND expires_at > clock_timestamp(); + """ + async with self._pool.acquire() as conn: + status = await conn.execute(query, token_hash) + return self._parse_affected(status) > 0 + + @staticmethod + def _parse_affected(status: str) -> int: + return int(status.split()[-1]) diff --git a/src/infrastructure/repositories/user_repository/__init__.py b/src/infrastructure/repositories/user_repository/__init__.py new file mode 100644 index 00000000..96a76e43 --- /dev/null +++ b/src/infrastructure/repositories/user_repository/__init__.py @@ -0,0 +1,3 @@ +from .postgres import PostgresUserRepository + +__all__ = ["PostgresUserRepository"] diff --git a/src/infrastructure/repositories/user_repository/postgres.py b/src/infrastructure/repositories/user_repository/postgres.py new file mode 100644 index 00000000..2dd15290 --- /dev/null +++ b/src/infrastructure/repositories/user_repository/postgres.py @@ -0,0 +1,67 @@ +import asyncpg + +from domain import UserCredentialsSchema, UserSchema +from domain.errors import ConflictError +from infrastructure.utils import Pool, repository_logs +from interfaces import UserRepository + + +class PostgresUserRepository(UserRepository): + def __init__(self, pool: Pool) -> None: + self._pool = pool + + @repository_logs + async def create( + self, username: str, display_name: str, password_hash: str + ) -> UserSchema: + query = """ + INSERT INTO users + ( + username, + display_name, + password_hash + ) + VALUES + ( + $1, + $2, + $3 + ) + RETURNING id, username, display_name, created_at; + """ + async with self._pool.acquire() as conn: + try: + row = await conn.fetchrow(query, username, display_name, password_hash) + except asyncpg.UniqueViolationError: + raise ConflictError("Имя пользователя уже занято") from None + assert row is not None + return self._row_to_user(row) + + @repository_logs + async def get_credentials(self, username: str) -> UserCredentialsSchema | None: + query = """ + SELECT + id, + username, + display_name, + created_at, + password_hash + FROM users + WHERE username = $1; + """ + async with self._pool.acquire() as conn: + row = await conn.fetchrow(query, username) + if row is None: + return None + return UserCredentialsSchema( + user=self._row_to_user(row), password_hash=row["password_hash"] + ) + + @staticmethod + def _row_to_user(row: asyncpg.Record) -> UserSchema: + return UserSchema( + id=row["id"], + username=row["username"], + display_name=row["display_name"], + created_at=row["created_at"], + ) diff --git a/src/infrastructure/security/__init__.py b/src/infrastructure/security/__init__.py new file mode 100644 index 00000000..0e01aecd --- /dev/null +++ b/src/infrastructure/security/__init__.py @@ -0,0 +1,4 @@ +from .password_hasher import PasswordHasher, get_password_hasher +from .tokens import generate_token, hash_token + +__all__ = ["PasswordHasher", "get_password_hasher", "generate_token", "hash_token"] diff --git a/src/infrastructure/security/password_hasher.py b/src/infrastructure/security/password_hasher.py new file mode 100644 index 00000000..9a3ca576 --- /dev/null +++ b/src/infrastructure/security/password_hasher.py @@ -0,0 +1,50 @@ +import asyncio +import secrets +from concurrent.futures import ThreadPoolExecutor + +from argon2 import PasswordHasher as Argon2Hasher +from argon2.exceptions import InvalidHashError, VerificationError +from argon2.profiles import RFC_9106_LOW_MEMORY +from starlette.requests import HTTPConnection + + +class PasswordHasher: + """ + Хеширование паролей с использованием Argon2id. + """ + + def __init__(self, workers: int) -> None: + self._hasher = Argon2Hasher.from_parameters(RFC_9106_LOW_MEMORY) + self._executor = ThreadPoolExecutor( + max_workers=workers, thread_name_prefix="argon2" + ) + self._dummy_hash = self._hasher.hash(secrets.token_urlsafe()) + + async def hash(self, password: str) -> str: + loop = asyncio.get_running_loop() + return await loop.run_in_executor(self._executor, self._hasher.hash, password) + + async def verify(self, password_hash: str, password: str) -> bool: + loop = asyncio.get_running_loop() + return await loop.run_in_executor( + self._executor, self._verify, password_hash, password + ) + + async def verify_dummy(self, password: str) -> None: + """Та же работа, что у verify, для выравнивания времени ответа""" + await self.verify(self._dummy_hash, password) + + def close(self) -> None: + self._executor.shutdown(wait=False, cancel_futures=True) + + def _verify(self, password_hash: str, password: str) -> bool: + try: + return self._hasher.verify(password_hash, password) + except (VerificationError, InvalidHashError): + return False + + +def get_password_hasher(connection: HTTPConnection) -> PasswordHasher: + """Получение hasher, созданного в lifespan.""" + hasher: PasswordHasher = connection.app.state.password_hasher + return hasher diff --git a/src/infrastructure/security/tokens.py b/src/infrastructure/security/tokens.py new file mode 100644 index 00000000..b7f7d6ba --- /dev/null +++ b/src/infrastructure/security/tokens.py @@ -0,0 +1,17 @@ +import hashlib +import secrets + + +def generate_token() -> str: + """Непрозрачный токен сессии: 256 бит из CSPRNG, base64url""" + return secrets.token_urlsafe(32) + + +def hash_token(token: str) -> bytes: + """ + SHA-256 токена для хранения и поиска. + + Токен уже имеет 256 бит энтропии, перебор невозможен, поэтому быстрый hash + достаточен; медленный KDF нужен паролям, у которых энтропия низкая. + """ + return hashlib.sha256(token.encode()).digest() diff --git a/src/infrastructure/services/__init__.py b/src/infrastructure/services/__init__.py new file mode 100644 index 00000000..15239f75 --- /dev/null +++ b/src/infrastructure/services/__init__.py @@ -0,0 +1,6 @@ +from .auth_service import AuthService +from .chat_service import ChatService +from .message_service import MessageService +from .user_service import UserService + +__all__ = ["UserService", "AuthService", "ChatService", "MessageService"] diff --git a/src/infrastructure/services/auth_service/__init__.py b/src/infrastructure/services/auth_service/__init__.py new file mode 100644 index 00000000..3bde19dc --- /dev/null +++ b/src/infrastructure/services/auth_service/__init__.py @@ -0,0 +1,3 @@ +from .auth_service import AuthService + +__all__ = ["AuthService"] diff --git a/src/infrastructure/services/auth_service/auth_service.py b/src/infrastructure/services/auth_service/auth_service.py new file mode 100644 index 00000000..8fd4627b --- /dev/null +++ b/src/infrastructure/services/auth_service/auth_service.py @@ -0,0 +1,92 @@ +from datetime import UTC, datetime + +from config import LOGGER +from domain import BasicCredentialsSchema, SessionSchema, UserSchema +from domain.errors import InvalidCredentialsError, UnauthorizedError +from infrastructure.security import PasswordHasher, generate_token, hash_token +from interfaces import SessionCache, SessionRepository, UserRepository + +TOKEN_MAX_LENGTH = 256 + + +class AuthService: + """ + Basic → opaque Bearer-сессия. + + Источник истины — SessionRepository (PostgreSQL), SessionCache (Valkey) — + read-through кэш: при промахе или недоступности кэша сессия читается из БД. + """ + + def __init__( + self, + user_repository: UserRepository, + session_repository: SessionRepository, + session_cache: SessionCache, + hasher: PasswordHasher, + session_ttl_seconds: int, + ) -> None: + self._users = user_repository + self._sessions = session_repository + self._cache = session_cache + self._hasher = hasher + self._session_ttl = session_ttl_seconds + + async def login(self, credentials: BasicCredentialsSchema) -> SessionSchema: + """ + Создание сессии по username и паролю. + + Raises: + InvalidCredentialsError: неизвестный username или неверный пароль — + одинаковый ответ и сопоставимое время + """ + found = await self._users.get_credentials(credentials.username) + if found is None: + await self._hasher.verify_dummy(credentials.password) + raise InvalidCredentialsError() + if not await self._hasher.verify(found.password_hash, credentials.password): + raise InvalidCredentialsError() + token = generate_token() + expires_at = await self._sessions.create( + hash_token(token), found.user.id, self._session_ttl + ) + LOGGER.info(f"Создана сессия пользователя {found.user.id}") + return SessionSchema(token=token, expires_at=expires_at, user=found.user) + + async def authenticate(self, token: str | None) -> UserSchema: + """ + Владелец действующей сессии. + + Raises: + UnauthorizedError: токена нет, он неизвестен, истёк или отозван + UnavailableError: кэш промахнулся, а PostgreSQL недоступен + """ + if not token or len(token) > TOKEN_MAX_LENGTH: + raise UnauthorizedError() + token_hash = hash_token(token) + session = await self._cache.get(token_hash) + if session is not None and session.expires_at > datetime.now(UTC): + return session.user + session = await self._sessions.get_active(token_hash) + if session is None: + raise UnauthorizedError() + await self._cache.set(token_hash, session) + return session.user + + async def logout(self, token: str | None) -> None: + """ + Отзыв в источнике истины, затем инвалидация кэша. + + Raises: + UnauthorizedError: действующей сессии с таким токеном нет + """ + if not token or len(token) > TOKEN_MAX_LENGTH: + raise UnauthorizedError() + token_hash = hash_token(token) + if not await self._sessions.revoke(token_hash): + raise UnauthorizedError() + if not await self._cache.delete(token_hash): + LOGGER.warning( + "Сессия отозвана в PostgreSQL, но запись кэша не удалена: " + "она истечёт по TTL кэша" + ) + LOGGER.info("Сессия отозвана") diff --git a/src/infrastructure/services/chat_service/__init__.py b/src/infrastructure/services/chat_service/__init__.py new file mode 100644 index 00000000..800f4e1e --- /dev/null +++ b/src/infrastructure/services/chat_service/__init__.py @@ -0,0 +1,3 @@ +from .chat_service import ChatService + +__all__ = ["ChatService"] diff --git a/src/infrastructure/services/chat_service/chat_service.py b/src/infrastructure/services/chat_service/chat_service.py new file mode 100644 index 00000000..8cc6b884 --- /dev/null +++ b/src/infrastructure/services/chat_service/chat_service.py @@ -0,0 +1,46 @@ +from uuid import UUID + +from config import LOGGER +from domain import ChatPageSchema, ChatSchema, CreateChatSchema, UserSchema +from domain.errors import InvalidRequestError, NotFoundError +from infrastructure.utils import build_page, decode_cursor +from interfaces import ChatRepository + + +class ChatService: + def __init__(self, chat_repository: ChatRepository) -> None: + self._chats = chat_repository + + async def create_chat( + self, current_user: UserSchema, data: CreateChatSchema + ) -> ChatSchema: + """ + Чат создаётся из явного списка существующих участников, включая создателя. + Существование участников проверяет FK в транзакции создания: отдельная + предварительная проверка была бы гонкой и не нужна для отката. + """ + if current_user.id not in data.member_ids: + raise InvalidRequestError( + "member_ids должен содержать текущего пользователя" + ) + chat = await self._chats.create(data.title, current_user.id, data.member_ids) + LOGGER.info( + f"Создан чат {chat.id} пользователем {current_user.id}, " + f"участников: {len(chat.member_ids)}" + ) + return chat + + async def get_chat(self, current_user: UserSchema, chat_id: UUID) -> ChatSchema: + chat = await self._chats.get_for_member(chat_id, current_user.id) + if chat is None: + raise NotFoundError() + return chat + + async def list_chats( + self, current_user: UserSchema, limit: int, cursor: str | None + ) -> ChatPageSchema: + scope = f"chats:{current_user.id}" + after = decode_cursor(cursor, scope) + fetched = await self._chats.list_for_member(current_user.id, after, limit + 1) + items, next_cursor = build_page(fetched, limit, scope) + return ChatPageSchema(items=items, next_cursor=next_cursor) diff --git a/src/infrastructure/services/message_service/__init__.py b/src/infrastructure/services/message_service/__init__.py new file mode 100644 index 00000000..d834d91d --- /dev/null +++ b/src/infrastructure/services/message_service/__init__.py @@ -0,0 +1,3 @@ +from .message_service import MessageService + +__all__ = ["MessageService"] diff --git a/src/infrastructure/services/message_service/message_service.py b/src/infrastructure/services/message_service/message_service.py new file mode 100644 index 00000000..3b2061a3 --- /dev/null +++ b/src/infrastructure/services/message_service/message_service.py @@ -0,0 +1,42 @@ +from uuid import UUID + +from config import LOGGER +from domain import CreateMessageSchema, MessagePageSchema, MessageSchema, UserSchema +from domain.errors import NotFoundError +from infrastructure.utils import build_page, decode_cursor +from interfaces import ChatRepository, MessageRepository + + +class MessageService: + def __init__( + self, chat_repository: ChatRepository, message_repository: MessageRepository + ) -> None: + self._chats = chat_repository + self._messages = message_repository + + async def send_message( + self, current_user: UserSchema, chat_id: UUID, data: CreateMessageSchema + ) -> MessageSchema: + """Отправитель берётся из текущей идентификации; текст в логи не пишется""" + await self._ensure_member(current_user, chat_id) + message = await self._messages.create(chat_id, current_user.id, data.text) + LOGGER.info( + f"Сообщение {message.id} отправлено в чат {chat_id} " + f"пользователем {current_user.id}" + ) + return message + + async def list_messages( + self, current_user: UserSchema, chat_id: UUID, limit: int, cursor: str | None + ) -> MessagePageSchema: + await self._ensure_member(current_user, chat_id) + scope = f"messages:{chat_id}" + after = decode_cursor(cursor, scope) + fetched = await self._messages.list_for_chat(chat_id, after, limit + 1) + items, next_cursor = build_page(fetched, limit, scope) + return MessagePageSchema(items=items, next_cursor=next_cursor) + + async def _ensure_member(self, current_user: UserSchema, chat_id: UUID) -> None: + """Посторонний получает 404: существование чужого чата не раскрывается""" + if await self._chats.get_for_member(chat_id, current_user.id) is None: + raise NotFoundError() diff --git a/src/infrastructure/services/user_service/__init__.py b/src/infrastructure/services/user_service/__init__.py new file mode 100644 index 00000000..3b69507f --- /dev/null +++ b/src/infrastructure/services/user_service/__init__.py @@ -0,0 +1,3 @@ +from .user_service import UserService + +__all__ = ["UserService"] diff --git a/src/infrastructure/services/user_service/user_service.py b/src/infrastructure/services/user_service/user_service.py new file mode 100644 index 00000000..36b0f0b4 --- /dev/null +++ b/src/infrastructure/services/user_service/user_service.py @@ -0,0 +1,17 @@ +from config import LOGGER +from domain import CreateUserSchema, UserSchema +from infrastructure.security import PasswordHasher +from interfaces import UserRepository + + +class UserService: + def __init__(self, repository: UserRepository, hasher: PasswordHasher) -> None: + self._repo = repository + self._hasher = hasher + + async def register(self, data: CreateUserSchema) -> UserSchema: + """Пароль сохраняется только как hash Argon2id""" + password_hash = await self._hasher.hash(data.password) + user = await self._repo.create(data.username, data.display_name, password_hash) + LOGGER.info(f"Зарегистрирован пользователь {user.id}") + return user diff --git a/src/infrastructure/utils/__init__.py b/src/infrastructure/utils/__init__.py new file mode 100644 index 00000000..206e65c3 --- /dev/null +++ b/src/infrastructure/utils/__init__.py @@ -0,0 +1,15 @@ +from .pagination import build_page, decode_cursor, encode_cursor, page_key +from .repository_utils import DATABASE_UNAVAILABLE_ERRORS, repository_logs +from .transaction import ConnectionPool, Pool, transaction + +__all__ = [ + "page_key", + "encode_cursor", + "decode_cursor", + "build_page", + "repository_logs", + "DATABASE_UNAVAILABLE_ERRORS", + "ConnectionPool", + "Pool", + "transaction", +] diff --git a/src/infrastructure/utils/pagination.py b/src/infrastructure/utils/pagination.py new file mode 100644 index 00000000..91c13c30 --- /dev/null +++ b/src/infrastructure/utils/pagination.py @@ -0,0 +1,44 @@ +import base64 +import binascii + +from pydantic import ValidationError + +from domain import ChatSchema, MessageSchema, PageCursorSchema, PageKey +from domain.errors import InvalidRequestError + +PageItem = ChatSchema | MessageSchema + + +def page_key(item: PageItem) -> PageKey: + return PageKey(item.created_at, item.id) + + +def encode_cursor(scope: str, key: PageKey) -> str: + """Непрозрачный cursor: base64url от JSON с ресурсом и ключом последнего элемента""" + payload = PageCursorSchema(scope=scope, created_at=key.created_at, id=key.id) + encoded = base64.urlsafe_b64encode(payload.model_dump_json().encode()) + return encoded.decode().rstrip("=") + + +def decode_cursor(raw: str | None, scope: str) -> PageKey | None: + """Разбор cursor, привязанного к ресурсу.""" + if raw is None: + return None + try: + decoded = base64.urlsafe_b64decode(raw + "=" * (-len(raw) % 4)) + cursor = PageCursorSchema.model_validate_json(decoded) + except (binascii.Error, ValueError, ValidationError): + raise InvalidRequestError("Некорректный cursor") from None + if cursor.scope != scope: + raise InvalidRequestError("cursor выдан для другого ресурса") + return cursor.key + + +def build_page( + fetched: list[PageItem], limit: int, scope: str +) -> tuple[list[PageItem], str | None]: + """Отрезает страницу из limit + 1 полученных элементов.""" + items = fetched[:limit] + if len(fetched) <= limit: + return items, None + return items, encode_cursor(scope, page_key(items[-1])) diff --git a/src/infrastructure/utils/repository_utils.py b/src/infrastructure/utils/repository_utils.py new file mode 100644 index 00000000..e450afe1 --- /dev/null +++ b/src/infrastructure/utils/repository_utils.py @@ -0,0 +1,39 @@ +from collections.abc import Callable +from functools import wraps +from typing import Any + +import asyncpg + +from config import LOGGER +from domain.errors import UnavailableError + +DATABASE_UNAVAILABLE_ERRORS: tuple[type[Exception], ...] = ( + OSError, + TimeoutError, + asyncpg.PostgresConnectionError, + asyncpg.InterfaceError, + asyncpg.exceptions.OperatorInterventionError, + asyncpg.exceptions.InsufficientResourcesError, +) + + +def repository_logs(func: Callable) -> Callable: + """ + Decorator for logging database requests. + """ + + @wraps(func) + async def wrapper(*args: Any, **kwargs: Any) -> Any: + log_text = ( + f"Request to DATABASE || Object: {args[0].__class__.__name__} " + f"|| Function: {func.__name__} || " + ) + try: + result = await func(*args, **kwargs) + except DATABASE_UNAVAILABLE_ERRORS as error: + LOGGER.warning(log_text + f"failed || Error: {type(error).__name__}") + raise UnavailableError() from None + LOGGER.debug(log_text + "completed") + return result + + return wrapper diff --git a/src/infrastructure/utils/transaction.py b/src/infrastructure/utils/transaction.py new file mode 100644 index 00000000..e4d18efa --- /dev/null +++ b/src/infrastructure/utils/transaction.py @@ -0,0 +1,29 @@ +from collections.abc import AsyncIterator +from contextlib import AbstractAsyncContextManager, asynccontextmanager +from typing import Protocol + +import asyncpg + + +class Pool(Protocol): + """Источник соединений: DatabasePoolManager или одно соединение транзакции""" + + def acquire(self) -> AbstractAsyncContextManager[asyncpg.Connection]: ... + + +class ConnectionPool: + """Pool-подобная обёртка над одним соединением.""" + + def __init__(self, conn: asyncpg.Connection) -> None: + self.connection = conn + + @asynccontextmanager + async def acquire(self) -> AsyncIterator[asyncpg.Connection]: + yield self.connection + + +@asynccontextmanager +async def transaction(pool: Pool) -> AsyncIterator[asyncpg.Connection]: + async with pool.acquire() as conn: + async with conn.transaction(): + yield conn diff --git a/src/interfaces/__init__.py b/src/interfaces/__init__.py new file mode 100644 index 00000000..623dfd53 --- /dev/null +++ b/src/interfaces/__init__.py @@ -0,0 +1,13 @@ +from .chat_repository import ChatRepository +from .message_repository import MessageRepository +from .session_cache import SessionCache +from .session_repository import SessionRepository +from .user_repository import UserRepository + +__all__ = [ + "UserRepository", + "ChatRepository", + "MessageRepository", + "SessionRepository", + "SessionCache", +] diff --git a/src/interfaces/chat_repository.py b/src/interfaces/chat_repository.py new file mode 100644 index 00000000..1821c8d7 --- /dev/null +++ b/src/interfaces/chat_repository.py @@ -0,0 +1,34 @@ +from abc import ABC, abstractmethod +from uuid import UUID + +from domain import ChatSchema, PageKey + + +class ChatRepository(ABC): + """Слой абстракции хранилища чатов""" + + @abstractmethod + async def create( + self, title: str, created_by: UUID, member_ids: list[UUID] + ) -> ChatSchema: + """ + создание чата вместе с участниками: при любой ошибке + не остаётся ни чата, ни части участников. + """ + pass + + @abstractmethod + async def get_for_member(self, chat_id: UUID, member_id: UUID) -> ChatSchema | None: + """ + Получение чата, только если пользователь является его участником. + """ + pass + + @abstractmethod + async def list_for_member( + self, member_id: UUID, after: PageKey | None, limit: int + ) -> list[ChatSchema]: + """ + Чаты пользователя по (created_at, id) по возрастанию. + """ + pass diff --git a/src/interfaces/message_repository.py b/src/interfaces/message_repository.py new file mode 100644 index 00000000..1b878c83 --- /dev/null +++ b/src/interfaces/message_repository.py @@ -0,0 +1,26 @@ +from abc import ABC, abstractmethod +from uuid import UUID + +from domain import MessageSchema, PageKey + + +class MessageRepository(ABC): + """Слой абстракции хранилища сообщений""" + + @abstractmethod + async def create(self, chat_id: UUID, sender_id: UUID, text: str) -> MessageSchema: + """ + Добавление сообщения в конец истории чата. id и created_at назначает + хранилище; created_at строго возрастает внутри чата, поэтому сообщение + не может появиться перед уже выданным cursor. + """ + pass + + @abstractmethod + async def list_for_chat( + self, chat_id: UUID, after: PageKey | None, limit: int + ) -> list[MessageSchema]: + """ + История чата по (created_at, id) по возрастанию. + """ + pass diff --git a/src/interfaces/session_cache.py b/src/interfaces/session_cache.py new file mode 100644 index 00000000..3cab29cf --- /dev/null +++ b/src/interfaces/session_cache.py @@ -0,0 +1,35 @@ +from abc import ABC, abstractmethod + +from domain import ActiveSessionSchema + + +class SessionCache(ABC): + """ + Слой абстракции кэша сессий. Кэш не источник истины: любой метод при + недоступности кэша не бросает исключение, а сообщает промах/пропуск, + и вызывающий идёт в SessionRepository. + """ + + @abstractmethod + async def get(self, token_hash: bytes) -> ActiveSessionSchema | None: + """ + Сессия из кэша. + """ + pass + + @abstractmethod + async def set(self, token_hash: bytes, session: ActiveSessionSchema) -> None: + """Запись сессии с TTL не дольше её оставшейся жизни""" + pass + + @abstractmethod + async def delete(self, token_hash: bytes) -> bool: + """ + Инвалидация записи после отзыва сессии. + """ + pass + + @abstractmethod + async def ping(self) -> bool: + """Доступность кэша для readiness и диагностики""" + pass diff --git a/src/interfaces/session_repository.py b/src/interfaces/session_repository.py new file mode 100644 index 00000000..4fe05af6 --- /dev/null +++ b/src/interfaces/session_repository.py @@ -0,0 +1,36 @@ +from abc import ABC, abstractmethod +from datetime import datetime +from uuid import UUID + +from domain import ActiveSessionSchema + + +class SessionRepository(ABC): + """ + Слой абстракции хранилища сессий — источника истины. + Хранится только hash токена, сам токен знает лишь клиент. + """ + + @abstractmethod + async def create( + self, token_hash: bytes, user_id: UUID, ttl_seconds: int + ) -> datetime: + """ + Сохранение новой сессии. Срок считается по часам хранилища — тем же, + по которым потом проверяется истечение. + """ + pass + + @abstractmethod + async def get_active(self, token_hash: bytes) -> ActiveSessionSchema | None: + """ + Действующая сессия: не истекла и не отозвана. + """ + pass + + @abstractmethod + async def revoke(self, token_hash: bytes) -> bool: + """ + Отзыв действующей сессии (logout). + """ + pass diff --git a/src/interfaces/user_repository.py b/src/interfaces/user_repository.py new file mode 100644 index 00000000..5e6afb24 --- /dev/null +++ b/src/interfaces/user_repository.py @@ -0,0 +1,24 @@ +from abc import ABC, abstractmethod + +from domain import UserCredentialsSchema, UserSchema + + +class UserRepository(ABC): + """Слой абстракции хранилища пользователей""" + + @abstractmethod + async def create( + self, username: str, display_name: str, password_hash: str + ) -> UserSchema: + """ + Сохранение пользователя. Уникальность username гарантирует хранилище, + поэтому одновременные регистрации одного имени не создают дубликат. + """ + pass + + @abstractmethod + async def get_credentials(self, username: str) -> UserCredentialsSchema | None: + """ + Пользователь и hash его пароля для проверки входа. + """ + pass diff --git a/student-tests/conftest.py b/student-tests/conftest.py new file mode 100644 index 00000000..b22e64e0 --- /dev/null +++ b/student-tests/conftest.py @@ -0,0 +1,76 @@ +import json +import os +from collections.abc import Callable, Iterator +from pathlib import Path + +import httpx +import pytest +from helpers import auth, basic, unique_password, unique_username + +ROOT = Path(__file__).resolve().parents[1] +DEFAULT_BASE_URL = json.loads((ROOT / "lab.json").read_text())["default_base_url"] + +REQUEST_TIMEOUT_SEC = 10 + + +@pytest.fixture(scope="session") +def base_url() -> str: + return os.environ.get("BASE_URL") or DEFAULT_BASE_URL + + +@pytest.fixture(scope="session") +def api(base_url: str) -> Iterator[httpx.Client]: + with httpx.Client(base_url=base_url, timeout=REQUEST_TIMEOUT_SEC) as client: + yield client + + +def register_and_login( + client: httpx.Client, label: str, display_name: str | None = None +) -> dict: + """Регистрация и вход; в словаре User плюс token и password для тестов""" + username = unique_username(label) + password = unique_password() + body = { + "username": username, + "display_name": display_name or f"Студент {label}", + "password": password, + } + response = client.post("/api/v1/users", json=body) + assert response.status_code == 201, response.text + user = response.json() + session = client.post("/api/v1/auth/sessions", headers=basic(username, password)) + assert session.status_code == 201, session.text + return {**user, "token": session.json()["token"], "password": password} + + +@pytest.fixture +def make_user(api: httpx.Client) -> Callable[..., dict]: + def create(label: str = "user", display_name: str | None = None) -> dict: + return register_and_login(api, label, display_name) + + return create + + +@pytest.fixture +def make_chat(api: httpx.Client) -> Callable[..., dict]: + def create(owner: dict, members: list[dict], title: str = "Тестовый чат") -> dict: + body = {"title": title, "member_ids": [member["id"] for member in members]} + response = api.post("/api/v1/chats", json=body, headers=auth(owner)) + assert response.status_code == 201, response.text + return response.json() + + return create + + +@pytest.fixture +def send_message(api: httpx.Client) -> Callable[..., dict]: + def send(user: dict, chat: dict, text: str) -> dict: + response = api.post( + f"/api/v1/chats/{chat['id']}/messages", + json={"text": text}, + headers=auth(user), + ) + assert response.status_code == 201, response.text + return response.json() + + return send diff --git a/student-tests/helpers.py b/student-tests/helpers.py new file mode 100644 index 00000000..fc4ecda4 --- /dev/null +++ b/student-tests/helpers.py @@ -0,0 +1,98 @@ +"""Общие проверки для собственных black-box тестов""" + +import base64 +import subprocess +import uuid +from pathlib import Path + +import httpx + +ROOT = Path(__file__).resolve().parents[1] + +USER_FIELDS = ("id", "username", "display_name", "created_at") + + +def unique_username(label: str) -> str: + return f"st_{label}_{uuid.uuid4().hex[:12]}" + + +def unique_password() -> str: + return f"Pw-{uuid.uuid4().hex}" + + +def basic(username: str, password: str) -> dict[str, str]: + """Authorization: Basic; пароль кодируется в UTF-8""" + raw = f"{username}:{password}".encode() + return {"Authorization": "Basic " + base64.b64encode(raw).decode()} + + +def auth(user: dict) -> dict[str, str]: + return {"Authorization": f"Bearer {user['token']}"} + + +def public_user(user: dict) -> dict: + """User из ответа API без тестовых полей token/password""" + return {field: user[field] for field in USER_FIELDS} + + +def assert_error(response: httpx.Response, status: int, code: str) -> None: + """Код ответа и единый формат ошибки: request_id совпадает с заголовком""" + assert response.status_code == status, response.text + assert response.headers["content-type"].startswith("application/json") + error = response.json()["error"] + assert error["code"] == code + assert error["message"] + assert error["request_id"] == response.headers["X-Request-Id"] + + +def assert_unauthorized(response: httpx.Response, scheme: str = "Bearer") -> None: + """401 с challenge нужной схемы""" + assert_error(response, 401, "unauthorized") + assert response.headers["WWW-Authenticate"].startswith(scheme) + + +def read_all_messages( + api: httpx.Client, user: dict, chat_id: str, limit: int +) -> tuple[list[dict], int]: + """Обход истории по next_cursor; возвращает сообщения и число страниц""" + items: list[dict] = [] + pages = 0 + params: dict[str, str | int] = {"limit": limit} + while True: + response = api.get( + f"/api/v1/chats/{chat_id}/messages", params=params, headers=auth(user) + ) + assert response.status_code == 200, response.text + page = response.json() + items.extend(page["items"]) + pages += 1 + if page["next_cursor"] is None: + return items, pages + params = {"limit": limit, "cursor": page["next_cursor"]} + + +def compose(*args: str, stdin: str | None = None) -> str: + """docker compose из корня репозитория; вывод не печатается (может быть секретен)""" + result = subprocess.run( + ["docker", "compose", "--env-file", ".env", *args], + cwd=ROOT, + input=stdin, + capture_output=True, + text=True, + timeout=120, + check=True, + ) + return result.stdout + + +def psql(sql: str) -> str: + """Запрос в PostgreSQL стенда изнутри контейнера; порт БД наружу не открыт""" + return compose( + "exec", + "-T", + "postgres", + "sh", + "-c", + 'psql -X -q -tA -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"', + stdin=sql, + ).strip() diff --git a/student-tests/test_auth.py b/student-tests/test_auth.py new file mode 100644 index 00000000..919f24d2 --- /dev/null +++ b/student-tests/test_auth.py @@ -0,0 +1,131 @@ +"""Вход по Basic, opaque Bearer-сессия, неверный пароль и logout""" + +import base64 +from datetime import UTC, datetime + +import httpx +import pytest +from helpers import ( + assert_unauthorized, + auth, + basic, + psql, + public_user, + unique_username, +) + +SESSIONS_PATH = "/api/v1/auth/sessions" +CURRENT_SESSION_PATH = "/api/v1/auth/sessions/current" + + +def test_login_returns_opaque_bearer_session(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + response = api.post( + SESSIONS_PATH, headers=basic(alice["username"], alice["password"]) + ) + assert response.status_code == 201, response.text + session = response.json() + assert set(session) == {"token", "token_type", "expires_at", "user"} + assert session["token_type"] == "Bearer" + assert session["user"] == public_user(alice) + assert len(session["token"]) >= 43 # 32 байта в base64url + assert alice["id"] not in session["token"] + assert alice["username"] not in session["token"] + assert session["token"] != alice["token"] + expires_at = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00")) + assert expires_at > datetime.now(UTC) + + +def test_wrong_password_and_unknown_user_are_indistinguishable( + api: httpx.Client, make_user +) -> None: + alice = make_user("alice") + wrong = api.post(SESSIONS_PATH, headers=basic(alice["username"], "Wrong-pass-123")) + unknown = api.post( + SESSIONS_PATH, headers=basic(unique_username("ghost"), "Wrong-pass-123") + ) + for response in (wrong, unknown): + assert_unauthorized(response, scheme="Basic") + assert 'realm="messenger"' in response.headers["WWW-Authenticate"] + assert wrong.json()["error"]["message"] == unknown.json()["error"]["message"] + + +@pytest.mark.parametrize( + "authorization", + [ + None, + "", + "Basic", + "Basic !!!not-base64!!!", + "Basic " + base64.b64encode(b"no-colon-here").decode(), + "Basic " + base64.b64encode(b"\xff\xfe:password").decode(), + "Bearer some-token", + ], +) +def test_malformed_basic_is_unauthorized( + api: httpx.Client, authorization: str | None +) -> None: + headers = {} if authorization is None else {"Authorization": authorization} + assert_unauthorized(api.post(SESSIONS_PATH, headers=headers), scheme="Basic") + + +def test_utf8_password_with_colon(api: httpx.Client) -> None: + username = unique_username("utf") + password = "пароль:с:двоеточием 🔐" + body = {"username": username, "display_name": "x", "password": password} + assert api.post("/api/v1/users", json=body).status_code == 201 + assert api.post(SESSIONS_PATH, headers=basic(username, password)).status_code == 201 + assert_unauthorized( + api.post(SESSIONS_PATH, headers=basic(username, "пароль:с:двоеточием")), + scheme="Basic", + ) + + +def test_basic_is_rejected_on_business_endpoints(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + headers = basic(alice["username"], alice["password"]) + assert_unauthorized(api.get("/api/v1/users/me", headers=headers)) + assert_unauthorized(api.get("/api/v1/chats", headers=headers)) + + +def test_logout_revokes_only_current_session(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + second = api.post( + SESSIONS_PATH, headers=basic(alice["username"], alice["password"]) + ) + other_session = {**alice, "token": second.json()["token"]} + + assert api.get("/api/v1/users/me", headers=auth(alice)).status_code == 200 + response = api.delete(CURRENT_SESSION_PATH, headers=auth(alice)) + assert response.status_code == 204 + assert response.content == b"" + + assert_unauthorized(api.get("/api/v1/users/me", headers=auth(alice))) + assert_unauthorized(api.delete(CURRENT_SESSION_PATH, headers=auth(alice))) + assert api.get("/api/v1/users/me", headers=auth(other_session)).status_code == 200 + + +def test_logout_without_token(api: httpx.Client) -> None: + assert_unauthorized(api.delete(CURRENT_SESSION_PATH)) + assert_unauthorized( + api.delete(CURRENT_SESSION_PATH, headers={"Authorization": "Bearer unknown"}) + ) + + +def test_relogin_after_logout(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + api.delete(CURRENT_SESSION_PATH, headers=auth(alice)) + again = api.post(SESSIONS_PATH, headers=basic(alice["username"], alice["password"])) + assert again.status_code == 201 + renewed = {**alice, "token": again.json()["token"]} + assert api.get("/api/v1/users/me", headers=auth(renewed)).status_code == 200 + + +def test_password_is_not_stored_in_plaintext(make_user) -> None: + """Проверка в БД стенда: в users лежит PHC-строка Argon2id, а не пароль""" + alice = make_user("alice") + stored = psql( + f"SELECT password_hash FROM users WHERE username = '{alice['username']}';" + ) + assert stored.startswith("$argon2id$") + assert alice["password"] not in stored diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..19524415 --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,77 @@ +"""Защита состояния от гонок: 100 сообщений от 10 параллельных клиентов""" + +import asyncio +import json +import time + +import httpx +from helpers import auth, read_all_messages + +CLIENTS = 10 +MESSAGES_PER_CLIENT = 10 +TOTAL_MESSAGES = CLIENTS * MESSAGES_PER_CLIENT +PAGE_LIMIT = 30 + + +async def _send_concurrently( + base_url: str, chat_id: str, users: list[dict] +) -> list[int]: + """Каждый клиент — отдельный пользователь и пул соединений; старт по сигналу""" + start = asyncio.Event() + + async def client_worker(user: dict) -> list[int]: + async with httpx.AsyncClient( + base_url=base_url, headers=auth(user), timeout=30 + ) as client: + await start.wait() + responses = await asyncio.gather( + *( + client.post( + f"/api/v1/chats/{chat_id}/messages", + json={"text": f"{user['username']}#{n}"}, + ) + for n in range(MESSAGES_PER_CLIENT) + ) + ) + return [response.status_code for response in responses] + + workers = [asyncio.create_task(client_worker(user)) for user in users] + await asyncio.sleep(0) + start.set() + results = await asyncio.gather(*workers) + return [status for statuses in results for status in statuses] + + +def test_parallel_messages_are_not_lost_or_duplicated( + api: httpx.Client, base_url: str, make_user, make_chat +) -> None: + users = [make_user(f"c{n}") for n in range(CLIENTS)] + chat = make_chat(users[0], users) + + started = time.perf_counter() + statuses = asyncio.run(_send_concurrently(base_url, chat["id"], users)) + elapsed = time.perf_counter() - started + + items, pages = read_all_messages(api, users[-1], chat["id"], PAGE_LIMIT) + expected_texts = { + f"{user['username']}#{n}" for user in users for n in range(MESSAGES_PER_CLIENT) + } + summary = { + "clients": CLIENTS, + "messages_sent": len(statuses), + "status_201": statuses.count(201), + "messages_read": len(items), + "unique_ids": len({item["id"] for item in items}), + "unique_texts": len({item["text"] for item in items}), + "pages_read": pages, + "send_seconds": round(elapsed, 3), + } + print("\nCONCURRENCY RESULT:", json.dumps(summary, ensure_ascii=False)) + + assert statuses == [201] * TOTAL_MESSAGES + assert len(items) == TOTAL_MESSAGES + assert summary["unique_ids"] == TOTAL_MESSAGES + assert {item["text"] for item in items} == expected_texts + keys = [(item["created_at"], item["id"]) for item in items] + assert keys == sorted(keys) + assert len(set(keys)) == TOTAL_MESSAGES diff --git a/student-tests/test_dependency_failures.py b/student-tests/test_dependency_failures.py new file mode 100644 index 00000000..f935ae3d --- /dev/null +++ b/student-tests/test_dependency_failures.py @@ -0,0 +1,129 @@ +""" +Отказы зависимостей: Valkey и PostgreSQL останавливаются через docker compose. + +Тесты меняют состояние стенда (stop/start контейнеров, volumes сохраняются), +поэтому запускаются только явно: make test-failures (RUN_FAILURE_TESTS=1). +""" + +import os +import time +from collections.abc import Iterator + +import httpx +import pytest +from helpers import assert_error, assert_unauthorized, auth, basic, compose + +pytestmark = pytest.mark.skipif( + os.environ.get("RUN_FAILURE_TESTS") != "1", + reason="останавливает контейнеры стенда: запускайте через make test-failures", +) + +READY_TIMEOUT_SEC = 60 +# Ответ при упавшей зависимости должен быть быстрым, а не висеть до таймаута клиента +BOUNDED_RESPONSE_SEC = 3 + + +def wait_ready(api: httpx.Client) -> None: + deadline = time.monotonic() + READY_TIMEOUT_SEC + while time.monotonic() < deadline: + try: + if api.get("/health/ready").status_code == 200: + return + except httpx.TransportError: + pass + time.sleep(0.5) + raise AssertionError("API не стал готов после восстановления зависимости") + + +@pytest.fixture +def valkey_stopped(api: httpx.Client) -> Iterator[None]: + compose("stop", "valkey") + try: + yield + finally: + compose("start", "valkey") + compose("up", "-d", "--wait", "valkey") + wait_ready(api) + + +@pytest.fixture +def postgres_stopped(api: httpx.Client) -> Iterator[None]: + compose("stop", "postgres") + try: + yield + finally: + compose("start", "postgres") + compose("up", "-d", "--wait", "postgres") + wait_ready(api) + + +def test_valkey_down_cache_is_bypassed( + api: httpx.Client, make_user, make_chat, send_message, valkey_stopped +) -> None: + """Без Valkey всё работает через PostgreSQL: вход, Bearer, запись, logout""" + alice = make_user("alice") + started = time.monotonic() + me = api.get("/api/v1/users/me", headers=auth(alice)) + assert me.status_code == 200, me.text + assert time.monotonic() - started < BOUNDED_RESPONSE_SEC + + chat = make_chat(alice, [alice]) + send_message(alice, chat, "без кэша") + assert api.get("/health/ready").status_code == 200 + + assert ( + api.delete("/api/v1/auth/sessions/current", headers=auth(alice)).status_code + == 204 + ) + assert_unauthorized(api.get("/api/v1/users/me", headers=auth(alice))) + assert_unauthorized(api.get("/api/v1/users/me")) + + +def test_logout_during_valkey_outage_is_not_resurrected( + api: httpx.Client, make_user +) -> None: + """ + Сессия попала в кэш, затем Valkey упал и пользователь вышел. После подъёма + Valkey (без persistence — пустой) отозванный токен не принимается. + """ + alice = make_user("alice") + assert api.get("/api/v1/users/me", headers=auth(alice)).status_code == 200 + compose("stop", "valkey") + try: + response = api.delete("/api/v1/auth/sessions/current", headers=auth(alice)) + assert response.status_code == 204 + finally: + compose("start", "valkey") + compose("up", "-d", "--wait", "valkey") + wait_ready(api) + assert_unauthorized(api.get("/api/v1/users/me", headers=auth(alice))) + + +def test_postgres_down_returns_bounded_503_and_no_anonymous_access( + api: httpx.Client, make_user, postgres_stopped +) -> None: + alice_token_unused = {"token": "x" * 43} + + started = time.monotonic() + ready = api.get("/health/ready") + assert_error(ready, 503, "unavailable") + assert time.monotonic() - started < BOUNDED_RESPONSE_SEC + + # Анонимный запрос остаётся 401, неизвестный токен не принимается как доверенный + assert_unauthorized(api.get("/api/v1/users/me")) + unknown = api.get("/api/v1/users/me", headers=auth(alice_token_unused)) + assert_error(unknown, 503, "unavailable") + assert "asyncpg" not in unknown.text and "postgres" not in unknown.text.lower() + + body = {"username": "st_pg_down", "display_name": "x", "password": "P" * 12} + assert_error(api.post("/api/v1/users", json=body), 503, "unavailable") + login = api.post("/api/v1/auth/sessions", headers=basic("st_pg_down", "P" * 12)) + assert_error(login, 503, "unavailable") + + +def test_session_survives_postgres_restart(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + compose("restart", "postgres") + wait_ready(api) + chats = api.get("/api/v1/chats", headers=auth(alice)) + assert chats.status_code == 200, chats.text diff --git a/student-tests/test_expiry.py b/student-tests/test_expiry.py new file mode 100644 index 00000000..707426de --- /dev/null +++ b/student-tests/test_expiry.py @@ -0,0 +1,55 @@ +""" +Истечение сессии при коротком SESSION_TTL_SECONDS. + +Нужен отдельный API с SESSION_TTL_SECONDS=2: make test-expiry поднимает +api-short-ttl на :8081 поверх тех же PostgreSQL и Valkey. +""" + +import os +import time +from datetime import UTC, datetime + +import httpx +import pytest +from conftest import register_and_login +from helpers import assert_unauthorized, auth, basic + +EXPIRY_BASE_URL = os.environ.get("EXPIRY_BASE_URL") +SHORT_TTL_SEC = 2 + +pytestmark = pytest.mark.skipif( + not EXPIRY_BASE_URL, reason="EXPIRY_BASE_URL не задан: запустите make test-expiry" +) + + +@pytest.fixture +def short_api() -> httpx.Client: + assert EXPIRY_BASE_URL is not None + with httpx.Client(base_url=EXPIRY_BASE_URL, timeout=10) as client: + yield client + + +def test_session_expires_after_short_ttl(short_api: httpx.Client) -> None: + alice = register_and_login(short_api, "ttl") + me = short_api.get("/api/v1/users/me", headers=auth(alice)) + assert me.status_code == 200, me.text + + time.sleep(SHORT_TTL_SEC + 1) + + # Сессия уже лежала в кэше после первого запроса: истечь должна и там + assert_unauthorized(short_api.get("/api/v1/users/me", headers=auth(alice))) + assert_unauthorized(short_api.get("/api/v1/chats", headers=auth(alice))) + assert_unauthorized( + short_api.delete("/api/v1/auth/sessions/current", headers=auth(alice)) + ) + + +def test_expires_at_matches_short_ttl(short_api: httpx.Client) -> None: + before = datetime.now(UTC) + alice = register_and_login(short_api, "ttl") + session = short_api.post( + "/api/v1/auth/sessions", headers=basic(alice["username"], alice["password"]) + ).json() + expires_at = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00")) + lifetime = (expires_at - before).total_seconds() + assert 0 < lifetime <= SHORT_TTL_SEC + 1 diff --git a/student-tests/test_foreign_chat.py b/student-tests/test_foreign_chat.py new file mode 100644 index 00000000..e720cf9e --- /dev/null +++ b/student-tests/test_foreign_chat.py @@ -0,0 +1,49 @@ +"""Чужой чат: посторонний получает 404, существование чата не раскрывается""" + +import uuid + +import httpx +from helpers import assert_error, auth + + +def test_outsider_cannot_access_chat( + api: httpx.Client, make_user, make_chat, send_message +) -> None: + alice = make_user("alice") + bob = make_user("bob") + eve = make_user("eve") + chat = make_chat(alice, [alice, bob]) + send_message(alice, chat, "секрет") + path = f"/api/v1/chats/{chat['id']}" + + assert_error(api.get(path, headers=auth(eve)), 404, "not_found") + assert_error(api.get(f"{path}/messages", headers=auth(eve)), 404, "not_found") + response = api.post(f"{path}/messages", json={"text": "чужое"}, headers=auth(eve)) + assert_error(response, 404, "not_found") + + history = api.get(f"{path}/messages", headers=auth(bob)).json() + assert [item["text"] for item in history["items"]] == ["секрет"] + + +def test_foreign_and_missing_chat_are_indistinguishable( + api: httpx.Client, make_user, make_chat +) -> None: + alice = make_user("alice") + eve = make_user("eve") + chat = make_chat(alice, [alice]) + foreign = api.get(f"/api/v1/chats/{chat['id']}", headers=auth(eve)).json() + missing = api.get(f"/api/v1/chats/{uuid.uuid4()}", headers=auth(eve)).json() + assert foreign["error"]["code"] == missing["error"]["code"] == "not_found" + assert foreign["error"]["message"] == missing["error"]["message"] + + +def test_chat_list_contains_only_own_chats( + api: httpx.Client, make_user, make_chat +) -> None: + alice = make_user("alice") + eve = make_user("eve") + chat = make_chat(alice, [alice]) + own = api.get("/api/v1/chats", headers=auth(eve)).json() + assert own == {"items": [], "next_cursor": None} + listed = api.get("/api/v1/chats", headers=auth(alice)).json() + assert [item["id"] for item in listed["items"]] == [chat["id"]] diff --git a/student-tests/test_health.py b/student-tests/test_health.py new file mode 100644 index 00000000..a8b1dbbe --- /dev/null +++ b/student-tests/test_health.py @@ -0,0 +1,15 @@ +import httpx +from helpers import assert_error + + +def test_health_endpoints_have_request_id(api: httpx.Client) -> None: + for path in ("/health/live", "/health/ready"): + response = api.get(path) + assert response.status_code == 200 + assert response.json() == {"status": "ok"} + assert response.headers["X-Request-Id"] + + +def test_unknown_route_uses_error_format(api: httpx.Client) -> None: + assert_error(api.get("/api/v1/unknown"), 404, "not_found") + diff --git a/student-tests/test_invalid_json.py b/student-tests/test_invalid_json.py new file mode 100644 index 00000000..df887973 --- /dev/null +++ b/student-tests/test_invalid_json.py @@ -0,0 +1,102 @@ +"""Неверный JSON и неверная структура тела — 400 invalid_request""" + +import httpx +import pytest +from helpers import assert_error, auth + +PASSWORD = "Valid-password-123" + +MALFORMED_BODIES = [ + b'{"username": "abc",', + b"not json at all", + b'{"text": "\xff\xfe"}', + b"", +] + + +@pytest.mark.parametrize("raw", MALFORMED_BODIES) +def test_malformed_json_on_register(api: httpx.Client, raw: bytes) -> None: + response = api.post( + "/api/v1/users", content=raw, headers={"Content-Type": "application/json"} + ) + assert_error(response, 400, "invalid_request") + + +@pytest.mark.parametrize("raw", MALFORMED_BODIES) +def test_malformed_json_on_create_chat_and_message( + api: httpx.Client, make_user, make_chat, raw: bytes +) -> None: + alice = make_user("alice") + chat = make_chat(alice, [alice]) + headers = {**auth(alice), "Content-Type": "application/json"} + assert_error( + api.post("/api/v1/chats", content=raw, headers=headers), 400, "invalid_request" + ) + assert_error( + api.post(f"/api/v1/chats/{chat['id']}/messages", content=raw, headers=headers), + 400, + "invalid_request", + ) + + +@pytest.mark.parametrize( + "body", + [ + [], + "alice", + {"username": "valid_name", "password": PASSWORD}, + {"username": 123, "display_name": "x", "password": PASSWORD}, + {"username": "valid_name", "display_name": None, "password": PASSWORD}, + { + "username": "valid_name", + "display_name": "x", + "password": PASSWORD, + "is_admin": True, + }, + {"username": "Upper_Case", "display_name": "x", "password": PASSWORD}, + {"username": "ab", "display_name": "x", "password": PASSWORD}, + {"username": "valid_name", "display_name": " ", "password": PASSWORD}, + {"username": "valid_name", "display_name": "я" * 101, "password": PASSWORD}, + {"username": "valid_name", "display_name": "a\u0000b", "password": PASSWORD}, + ], +) +def test_invalid_user_payload(api: httpx.Client, body: object) -> None: + assert_error(api.post("/api/v1/users", json=body), 400, "invalid_request") + + +@pytest.mark.parametrize( + "body", + [ + {"text": 42}, + {"text": "привет", "sender_id": "11111111-1111-4111-8111-111111111111"}, + {}, + ], +) +def test_invalid_message_payload( + api: httpx.Client, make_user, make_chat, body: dict +) -> None: + alice = make_user("alice") + chat = make_chat(alice, [alice]) + response = api.post( + f"/api/v1/chats/{chat['id']}/messages", json=body, headers=auth(alice) + ) + assert_error(response, 400, "invalid_request") + + +def test_invalid_chat_payload(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + bob = make_user("bob") + cases = [ + {"title": "t", "member_ids": [bob["id"]]}, + {"title": "t", "member_ids": [alice["id"], alice["id"]]}, + { + "title": "t", + "member_ids": [alice["id"], "11111111-1111-4111-8111-111111111111"], + }, + {"title": "t", "member_ids": [alice["id"], "not-a-uuid"]}, + {"title": "t", "member_ids": []}, + {"title": " ", "member_ids": [alice["id"]]}, + ] + for body in cases: + response = api.post("/api/v1/chats", json=body, headers=auth(alice)) + assert_error(response, 400, "invalid_request") diff --git a/student-tests/test_messages.py b/student-tests/test_messages.py new file mode 100644 index 00000000..0319354f --- /dev/null +++ b/student-tests/test_messages.py @@ -0,0 +1,77 @@ +"""Пустые сообщения, границы длины и Unicode""" + +import httpx +import pytest +from helpers import assert_error, auth + + +@pytest.mark.parametrize("text", ["", " ", " \n\t ", " "]) +def test_empty_or_blank_message_is_rejected( + api: httpx.Client, make_user, make_chat, text: str +) -> None: + alice = make_user("alice") + chat = make_chat(alice, [alice]) + response = api.post( + f"/api/v1/chats/{chat['id']}/messages", json={"text": text}, headers=auth(alice) + ) + assert_error(response, 400, "invalid_request") + history = api.get(f"/api/v1/chats/{chat['id']}/messages", headers=auth(alice)) + assert history.json()["items"] == [] + + +def test_message_length_is_counted_in_code_points( + api: httpx.Client, make_user, make_chat, send_message +) -> None: + alice = make_user("alice") + chat = make_chat(alice, [alice]) + longest = "😀" * 4000 + assert send_message(alice, chat, longest)["text"] == longest + response = api.post( + f"/api/v1/chats/{chat['id']}/messages", + json={"text": "😀" * 4001}, + headers=auth(alice), + ) + assert_error(response, 400, "invalid_request") + + +def test_unicode_is_preserved( + api: httpx.Client, make_user, make_chat, send_message +) -> None: + name = "Алиса 👩‍💻 é" + alice = make_user("alice", display_name=name) + assert alice["display_name"] == name + chat = make_chat(alice, [alice], title="Чат 🚀 中文") + assert chat["title"] == "Чат 🚀 中文" + text = "Привет, мир 👋 — «кавычки» \t\n и 𝔘𝔫𝔦𝔠𝔬𝔡𝔢" + message = send_message(alice, chat, text) + assert message["text"] == text + page = api.get(f"/api/v1/chats/{chat['id']}/messages", headers=auth(alice)).json() + assert page["items"][0]["text"] == text + + +def test_nul_character_is_rejected(api: httpx.Client, make_user, make_chat) -> None: + """PostgreSQL text не хранит U+0000: отказ 400 до записи, а не 500""" + alice = make_user("alice") + chat = make_chat(alice, [alice]) + response = api.post( + f"/api/v1/chats/{chat['id']}/messages", + json={"text": "до\u0000после"}, + headers=auth(alice), + ) + assert_error(response, 400, "invalid_request") + response = api.post( + "/api/v1/chats", + json={"title": "a\u0000b", "member_ids": [alice["id"]]}, + headers=auth(alice), + ) + assert_error(response, 400, "invalid_request") + + +def test_sender_is_taken_from_session(make_user, make_chat, send_message) -> None: + alice = make_user("alice") + bob = make_user("bob") + chat = make_chat(alice, [alice, bob]) + message = send_message(bob, chat, "от Боба") + assert message["sender_id"] == bob["id"] + assert message["chat_id"] == chat["id"] + assert message["created_at"].endswith("Z") diff --git a/student-tests/test_pagination.py b/student-tests/test_pagination.py new file mode 100644 index 00000000..25154348 --- /dev/null +++ b/student-tests/test_pagination.py @@ -0,0 +1,138 @@ +"""Обход нескольких страниц истории и списка чатов по cursor""" + +import httpx +import pytest +from helpers import assert_error, auth, read_all_messages + + +@pytest.mark.parametrize( + ("total", "limit", "expected_pages"), + [(7, 3, 3), (6, 3, 2), (5, 1, 5), (3, 100, 1)], +) +def test_walk_message_pages( + api: httpx.Client, + make_user, + make_chat, + send_message, + total: int, + limit: int, + expected_pages: int, +) -> None: + alice = make_user("alice") + bob = make_user("bob") + chat = make_chat(alice, [alice, bob]) + sent = [send_message(alice if n % 2 else bob, chat, f"m{n}") for n in range(total)] + + items, pages = read_all_messages(api, bob, chat["id"], limit) + + assert pages == expected_pages + assert items == sent + assert len({item["id"] for item in items}) == total + keys = [(item["created_at"], item["id"]) for item in items] + assert keys == sorted(keys) + + +def test_limit_can_change_between_pages( + api: httpx.Client, make_user, make_chat, send_message +) -> None: + alice = make_user("alice") + chat = make_chat(alice, [alice]) + sent = [send_message(alice, chat, f"m{n}") for n in range(6)] + path = f"/api/v1/chats/{chat['id']}/messages" + + first = api.get(path, params={"limit": 2}, headers=auth(alice)).json() + second = api.get( + path, params={"limit": 4, "cursor": first["next_cursor"]}, headers=auth(alice) + ).json() + + assert first["items"] + second["items"] == sent + assert second["next_cursor"] is None + + +def test_new_message_appears_after_cursor( + api: httpx.Client, make_user, make_chat, send_message +) -> None: + alice = make_user("alice") + chat = make_chat(alice, [alice]) + send_message(alice, chat, "m0") + send_message(alice, chat, "m1") + path = f"/api/v1/chats/{chat['id']}/messages" + first = api.get(path, params={"limit": 1}, headers=auth(alice)).json() + late = send_message(alice, chat, "m2") + rest = api.get( + path, params={"limit": 10, "cursor": first["next_cursor"]}, headers=auth(alice) + ).json() + assert [item["text"] for item in rest["items"]] == ["m1", "m2"] + assert rest["items"][-1] == late + + +def test_cursor_is_bound_to_chat( + api: httpx.Client, make_user, make_chat, send_message +) -> None: + alice = make_user("alice") + chat_a = make_chat(alice, [alice], title="A") + chat_b = make_chat(alice, [alice], title="B") + for n in range(3): + send_message(alice, chat_a, f"a{n}") + cursor = api.get( + f"/api/v1/chats/{chat_a['id']}/messages", + params={"limit": 1}, + headers=auth(alice), + ).json()["next_cursor"] + response = api.get( + f"/api/v1/chats/{chat_b['id']}/messages", + params={"cursor": cursor}, + headers=auth(alice), + ) + assert_error(response, 400, "invalid_request") + + +@pytest.mark.parametrize( + "params", + [ + {"limit": 0}, + {"limit": 101}, + {"limit": "abc"}, + {"cursor": ""}, + {"cursor": "not-a-cursor"}, + {"cursor": "eyJzY29wZSI6IDF9"}, + ], +) +def test_invalid_limit_or_cursor( + api: httpx.Client, make_user, make_chat, params: dict +) -> None: + alice = make_user("alice") + chat = make_chat(alice, [alice]) + response = api.get( + f"/api/v1/chats/{chat['id']}/messages", params=params, headers=auth(alice) + ) + assert_error(response, 400, "invalid_request") + + +def test_walk_chat_list_pages(api: httpx.Client, make_user, make_chat) -> None: + alice = make_user("alice") + created = [make_chat(alice, [alice], title=f"chat {n}") for n in range(5)] + listed: list[dict] = [] + params: dict[str, str | int] = {"limit": 2} + pages = 0 + while True: + page = api.get("/api/v1/chats", params=params, headers=auth(alice)).json() + listed.extend(page["items"]) + pages += 1 + if page["next_cursor"] is None: + break + params = {"limit": 2, "cursor": page["next_cursor"]} + assert pages == 3 + assert listed == created + + +def test_chat_cursor_is_bound_to_user(api: httpx.Client, make_user, make_chat) -> None: + alice = make_user("alice") + bob = make_user("bob") + for n in range(2): + make_chat(alice, [alice, bob], title=f"chat {n}") + cursor = api.get("/api/v1/chats", params={"limit": 1}, headers=auth(alice)).json()[ + "next_cursor" + ] + response = api.get("/api/v1/chats", params={"cursor": cursor}, headers=auth(bob)) + assert_error(response, 400, "invalid_request") diff --git a/student-tests/test_transactions.py b/student-tests/test_transactions.py new file mode 100644 index 00000000..86085d4e --- /dev/null +++ b/student-tests/test_transactions.py @@ -0,0 +1,95 @@ +"""Гонка регистрации одинакового username и rollback создания чата""" + +import asyncio +import json +import uuid + +import httpx +from helpers import assert_error, auth, psql, unique_password, unique_username + +PARALLEL_REGISTRATIONS = 20 + + +async def _register_concurrently(base_url: str, username: str) -> list[int]: + """Все запросы стартуют по одному сигналу, у каждого свой пароль""" + start = asyncio.Event() + + async def register(client: httpx.AsyncClient) -> int: + await start.wait() + body = { + "username": username, + "display_name": "Гонка", + "password": unique_password(), + } + response = await client.post("/api/v1/users", json=body) + return response.status_code + + limits = httpx.Limits(max_connections=PARALLEL_REGISTRATIONS) + async with httpx.AsyncClient( + base_url=base_url, timeout=30, limits=limits + ) as client: + tasks = [ + asyncio.create_task(register(client)) for _ in range(PARALLEL_REGISTRATIONS) + ] + await asyncio.sleep(0) + start.set() + return list(await asyncio.gather(*tasks)) + + +def test_same_username_race_creates_exactly_one_user(base_url: str) -> None: + username = unique_username("race") + statuses = asyncio.run(_register_concurrently(base_url, username)) + stored = psql(f"SELECT count(*) FROM users WHERE username = '{username}';") + print( + "\nREGISTRATION RACE:", + json.dumps( + { + "requests": len(statuses), + "status_201": statuses.count(201), + "status_409": statuses.count(409), + "rows_in_db": int(stored), + } + ), + ) + assert statuses.count(201) == 1 + assert statuses.count(409) == PARALLEL_REGISTRATIONS - 1 + assert stored == "1" + + +def test_failed_chat_creation_is_rolled_back(api: httpx.Client, make_user) -> None: + """ + Строка chats вставляется первой, затем участники нарушают FK (неизвестный + пользователь в конце списка). После 400 в БД не должно остаться ни чата + с этим title, ни его участников: откатилась вся транзакция. + """ + alice = make_user("alice") + bob = make_user("bob") + title = f"rollback-{uuid.uuid4().hex}" + body = {"title": title, "member_ids": [alice["id"], bob["id"], str(uuid.uuid4())]} + + response = api.post("/api/v1/chats", json=body, headers=auth(alice)) + + assert_error(response, 400, "invalid_request") + assert psql(f"SELECT count(*) FROM chats WHERE title = '{title}';") == "0" + orphan_members = psql( + "SELECT count(*) FROM chat_members m " + "LEFT JOIN chats c ON c.id = m.chat_id WHERE c.id IS NULL;" + ) + assert orphan_members == "0" + listed = api.get("/api/v1/chats", params={"limit": 100}, headers=auth(alice)) + assert all(chat["title"] != title for chat in listed.json()["items"]) + + +def test_successful_chat_creation_commits_all_members( + api: httpx.Client, make_user +) -> None: + alice = make_user("alice") + bob = make_user("bob") + title = f"commit-{uuid.uuid4().hex}" + body = {"title": title, "member_ids": [bob["id"], alice["id"]]} + chat = api.post("/api/v1/chats", json=body, headers=auth(alice)).json() + assert chat["member_ids"] == [bob["id"], alice["id"]] + members = psql(f"SELECT count(*) FROM chat_members WHERE chat_id = '{chat['id']}';") + assert members == "2" + again = api.get(f"/api/v1/chats/{chat['id']}", headers=auth(bob)).json() + assert again == chat diff --git a/student-tests/test_users.py b/student-tests/test_users.py new file mode 100644 index 00000000..e452885e --- /dev/null +++ b/student-tests/test_users.py @@ -0,0 +1,95 @@ +"""Регистрация, дубликат username и идентификация только через Bearer""" + +import uuid + +import httpx +import pytest +from helpers import ( + assert_error, + assert_unauthorized, + auth, + public_user, + unique_password, + unique_username, +) + + +def test_duplicate_username_is_conflict(api: httpx.Client, make_user) -> None: + alice = make_user("dup") + body = { + "username": alice["username"], + "display_name": "B", + "password": unique_password(), + } + assert_error(api.post("/api/v1/users", json=body), 409, "conflict") + me = api.get("/api/v1/users/me", headers=auth(alice)) + assert me.json()["display_name"] == alice["display_name"] + + +def test_registration_never_returns_password(api: httpx.Client) -> None: + password = unique_password() + body = { + "username": unique_username("pw"), + "display_name": "x", + "password": password, + } + response = api.post("/api/v1/users", json=body) + assert response.status_code == 201 + assert set(response.json()) == {"id", "username", "display_name", "created_at"} + assert password not in response.text + + +@pytest.mark.parametrize( + "password", + [None, "", "короткий", "x" * 11, "x" * 129, 123456789012], +) +def test_invalid_password_is_rejected(api: httpx.Client, password: object) -> None: + body: dict[str, object] = {"username": unique_username("pw"), "display_name": "x"} + if password is not None: + body["password"] = password + assert_error(api.post("/api/v1/users", json=body), 400, "invalid_request") + + +def test_password_length_is_counted_in_code_points(api: httpx.Client) -> None: + for password in ("😀" * 12, "😀" * 128): + body = { + "username": unique_username("pw"), + "display_name": "x", + "password": password, + } + assert api.post("/api/v1/users", json=body).status_code == 201 + + +@pytest.mark.parametrize( + "headers", + [ + {}, + {"Authorization": ""}, + {"Authorization": "Bearer"}, + {"Authorization": "Bearer not-a-real-token"}, + {"Authorization": "Bearer " + "x" * 5000}, + {"Authorization": "Token abc"}, + ], +) +def test_missing_or_unknown_token_is_unauthorized( + api: httpx.Client, headers: dict +) -> None: + assert_unauthorized(api.get("/api/v1/users/me", headers=headers)) + assert_unauthorized(api.get("/api/v1/chats", headers=headers)) + + +def test_x_user_id_gives_no_access(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + eve = make_user("eve") + assert_unauthorized(api.get("/api/v1/users/me", headers={"X-User-Id": alice["id"]})) + me = api.get("/api/v1/users/me", headers={**auth(alice), "X-User-Id": eve["id"]}) + assert me.json()["id"] == alice["id"] + unknown = {"X-User-Id": str(uuid.uuid4())} + assert_unauthorized(api.get("/api/v1/chats", headers=unknown)) + + +def test_current_user(api: httpx.Client, make_user) -> None: + alice = make_user("alice") + me = api.get("/api/v1/users/me", headers=auth(alice)) + assert me.status_code == 200 + assert me.json() == public_user(alice)