forked from mtusi2026/mtusi2026lab2
Compare commits
8
Commits
master
...
a2e8ca2817
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2e8ca2817 | ||
|
|
62cb785c63 | ||
|
|
d33c50a087 | ||
|
|
3662e9ff5c | ||
|
|
c1200032b5 | ||
|
|
4922e68dca | ||
|
|
7f8bcfd234 | ||
|
|
789bea2f1e |
+1
-1
@@ -7,5 +7,5 @@ SESSION_TTL_SECONDS=3600
|
|||||||
# Choose credentials locally. File paths contain no secret values.
|
# Choose credentials locally. File paths contain no secret values.
|
||||||
DATABASE_URL_FILE=/run/secrets/database_url
|
DATABASE_URL_FILE=/run/secrets/database_url
|
||||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||||
SESSION_STORE=postgres
|
SESSION_STORE=redis
|
||||||
CACHE_URL_FILE=/run/secrets/cache_url
|
CACHE_URL_FILE=/run/secrets/cache_url
|
||||||
|
|||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
FROM python:3.13.15-slim
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE=1
|
||||||
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY alembic.ini .
|
||||||
|
COPY migrations ./migrations
|
||||||
|
COPY src ./src
|
||||||
|
|
||||||
|
RUN useradd --create-home --uid 10001 appuser
|
||||||
|
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||||
|
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" || exit 1
|
||||||
|
|
||||||
|
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080"]
|
||||||
@@ -1,37 +1,173 @@
|
|||||||
# Отчёт — лабораторная 2
|
# Отчёт — лабораторная 2
|
||||||
|
|
||||||
- Автор, группа: TODO
|
- **Автор, группа:** Травкин С.А., БВТ2304.
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
- **Целевая оценка; выбранный трек 5, если нужен:** 4. Трек 5 пока не заявлен.
|
||||||
- Commit/PR: TODO после публикации
|
- **Commit/PR:** указать после публикации.
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
- **Использованные источники, библиотеки и помощь ИИ:** README и API-контракт лабораторной; документация PostgreSQL, Redis, SQLAlchemy, Alembic, FastAPI и Docker Compose. Использованы Python, FastAPI, SQLAlchemy, asyncpg, Alembic, PostgreSQL 17.6, Redis 8.2.2, Argon2id. ChatGPT использовался для разбора задания, разработки и проверки реализации, тестов и оформления отчёта.
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
Необходимы Docker Engine, Docker Compose, Git и Python 3.9+ для публичных тестов. Приложение запускается в контейнерах; локально использовался Python 3.13.
|
||||||
|
|
||||||
|
После клонирования репозитория перейти в его каталог и создать локальную конфигурацию:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp .env.example .env
|
||||||
|
mkdir -p secrets
|
||||||
|
chmod 700 secrets
|
||||||
|
```
|
||||||
|
|
||||||
|
Создать согласованные файлы секретов:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 - <<'PY'
|
||||||
|
from pathlib import Path
|
||||||
|
import secrets
|
||||||
|
|
||||||
|
directory = Path("secrets")
|
||||||
|
password = secrets.token_hex(24)
|
||||||
|
|
||||||
|
values = {
|
||||||
|
"postgres_password": password,
|
||||||
|
"database_url": f"postgresql+asyncpg://lab2:{password}@postgres:5432/messenger",
|
||||||
|
"cache_url": "redis://redis:6379/0",
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, value in values.items():
|
||||||
|
path = directory / name
|
||||||
|
path.write_text(value, encoding="utf-8")
|
||||||
|
path.chmod(0o600)
|
||||||
|
|
||||||
|
print("Local secrets created.")
|
||||||
|
PY
|
||||||
|
```
|
||||||
|
|
||||||
|
Секреты создаются только при первоначальной настройке пустой базы. Пароли и строки подключения не выводятся в терминал и не добавляются в Git.
|
||||||
|
|
||||||
|
Сборка и запуск:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose --env-file .env config --quiet
|
||||||
|
docker compose --env-file .env up -d --build
|
||||||
|
```
|
||||||
|
|
||||||
|
Используются образы PostgreSQL 17.6 и Redis 8.2.2. API собирается из локального `Dockerfile`. Сервис `migrate` автоматически выполняет `alembic upgrade head` после готовности PostgreSQL. API запускается после успешного завершения миграций и готовности Redis.
|
||||||
|
|
||||||
|
Проверка:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose ps -a
|
||||||
|
curl -i http://localhost:8080/health/ready
|
||||||
|
make check
|
||||||
|
make test
|
||||||
|
```
|
||||||
|
|
||||||
|
Ожидаемый ответ `/health/ready` — HTTP 200, `{"status":"ok"}`.
|
||||||
|
|
||||||
|
Дополнительные проверки:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m venv .venv
|
||||||
|
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||||
|
make validate PYTHON=.venv/bin/python
|
||||||
|
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Собственные тесты:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python tests_student/test_auth.py
|
||||||
|
python tests_student/test_race.py
|
||||||
|
./scripts/test_short_ttl.sh
|
||||||
|
./scripts/test_failures.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Откат транзакции проверяется внутри контейнера:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose run --rm --no-deps \
|
||||||
|
-e PYTHONPATH=/app \
|
||||||
|
-v "$PWD/tests_student:/app/tests_student:ro" \
|
||||||
|
api python tests_student/test_rollback.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Безопасная остановка с сохранением данных:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose down
|
||||||
|
```
|
||||||
|
|
||||||
|
Намеренное удаление учебных данных вместе с volumes:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose down -v
|
||||||
|
```
|
||||||
|
|
||||||
## Архитектура и решения
|
## Архитектура и решения
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
Схема: **клиент → FastAPI → PostgreSQL / Redis**.
|
||||||
|
|
||||||
|
PostgreSQL — источник истины для пользователей, чатов, участников и сообщений. Redis используется для хранения сессий с TTL.
|
||||||
|
|
||||||
|
В PostgreSQL созданы таблицы `users`, `chats`, `chat_members`, `messages`. Используются UUID, первичные и внешние ключи, уникальность `username`. Для истории сообщений создан индекс `(chat_id, created_at, id)`.
|
||||||
|
|
||||||
|
Создание чата и добавление участников выполняются в одной транзакции. При ошибке изменения откатываются. Это проверено тестом `test_rollback.py`.
|
||||||
|
|
||||||
|
Из лабораторной № 1 сохранены основные операции мессенджера. Вместо хранения данных в памяти используется PostgreSQL. Добавлены пароли с Argon2id, вход через Basic, случайные Bearer-токены, TTL и logout. Заголовок `X-User-Id` больше не предоставляет доступ.
|
||||||
|
|
||||||
|
Redis хранит ключи `session:<token>` с UUID пользователя. При logout ключ удаляется, при истечении TTL становится недействительным. Включены AOF (`appendonly yes`, `appendfsync everysec`) и named volume `redis_data`. PostgreSQL использует named volume `postgres_data`.
|
||||||
|
|
||||||
|
SQL-запросы параметризованы. Пул SQLAlchemy: `pool_size=5`, `max_overflow=5`, `pool_timeout=5` секунд, таймаут подключения — 5 секунд. Для Redis установлены таймауты подключения и операций по 5 секунд.
|
||||||
|
|
||||||
|
Соответствие `.env.example` конфигурации: `APP_HOST`, `APP_PORT`, `APP_ENV`, `SESSION_TTL_SECONDS`, `DATABASE_URL_FILE`, `SESSION_STORE`, `CACHE_URL_FILE` передаются сервису API через Compose. URL подключений читаются из файлов `/run/secrets/database_url` и `/run/secrets/cache_url`. Пароль PostgreSQL читается из `/run/secrets/postgres_password`. Значение `SESSION_STORE=redis` соответствует используемому хранилищу сессий.
|
||||||
|
|
||||||
|
Миграции Alembic запускаются отдельным сервисом `migrate`. Проверен запуск на пустых volumes и создание схемы с нуля.
|
||||||
|
|
||||||
## Доказательства критериев
|
## Доказательства критериев
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| Публичный smoke | `make test` | PASS, публичный API-сценарий выполнен | `evidence/grade4_checks.txt` |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| Проверка структуры и контракта | `make check`, `make validate` | Обе проверки PASS | `evidence/grade4_checks.txt` |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| Постоянное хранение данных и сессий | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | Данные и прежний токен сохранились | `evidence/grade4_resilience.txt` |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| Перезапуск всех сервисов | `scripts/restart.sh` | `docker compose down` и `up -d` без удаления volumes; ожидание готовности PostgreSQL, Redis и API | `scripts/restart.sh` |
|
||||||
|
| PostgreSQL, ключи и ограничения | `alembic upgrade head`, запуск с пустыми volumes | Схема создана, таблицы и ограничения применены | `migrations/versions/f7c51e1e945b_initial_schema.py` |
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
| Атомарность создания чата | `test_rollback.py` | PASS, частичные изменения отсутствуют | `evidence/grade4_checks.txt` |
|
||||||
|
| Basic, Bearer, неверный пароль и logout | `test_auth.py`, `make test` | Неверный пароль — 401; logout — 204; отозванный токен — 401 | `evidence/grade4_checks.txt` |
|
||||||
|
| Redis для сессий с TTL и persistence | Перезапуск Redis в `test_failures.sh` | Прежний действующий токен работает после восстановления | `evidence/grade4_resilience.txt` |
|
||||||
|
| Ограниченный пул и таймауты | Конфигурация SQLAlchemy и Redis | Пул 5+5, таймауты заданы | `src/database.py`, `src/sessions.py` |
|
||||||
|
| Конкурентная регистрация | `test_race.py` | Одна регистрация — 201, вторая — 409 | `evidence/grade4_checks.txt` |
|
||||||
|
| Индекс истории и пагинация | `EXPLAIN (ANALYZE, BUFFERS)` на 20 000 сообщениях | Keyset: 100 строк Index Scan против 15 100 у OFFSET | `evidence/explain_history.txt` |
|
||||||
|
| Короткий TTL | `test_short_ttl.sh` | Токен истёк, получен 401 | `evidence/grade4_resilience.txt` |
|
||||||
|
| Отказ Redis | `test_failures.sh` | Readiness — 503, защищённый запрос — 503, после восстановления — 200 | `evidence/failure_timings.txt` |
|
||||||
|
| Отказ PostgreSQL | `test_failures.sh` | Readiness — 503, защищённый запрос — 503, после восстановления — 200 | `evidence/failure_timings.txt` |
|
||||||
|
| Секреты вне Git | Проверка `.gitignore` | `.env`, `secrets/`, volumes и backups исключены | `.gitignore` |
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Эксперименты и отказы
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
**Эксперимент с индексом.** Гипотеза: keyset-пагинация с составным индексом эффективнее OFFSET при чтении глубокой страницы истории. Подготовлено 20 000 синтетических сообщений одного чата. После `ANALYZE` выполнены два запроса с `EXPLAIN (ANALYZE, BUFFERS)` для получения 100 сообщений после первых 15 000.
|
||||||
|
|
||||||
|
Результаты: OFFSET — 1,908 мс, 435 shared buffer hits, 15 100 строк Index Scan; keyset — 0,053 мс, 7 shared buffer hits, 100 строк Index Scan. В данном прогоне keyset оказался примерно в 36 раз быстрее. Полный результат — `evidence/explain_history.txt`.
|
||||||
|
|
||||||
|
**Проверки отказов.** Использован локальный стенд Docker Compose в WSL. PostgreSQL 17.6, Redis 8.2.2, один API-контейнер. Создавался тестовый пользователь и действующая сессия. Зависимости останавливались по очереди, HTTP-ответы измерялись через `curl %{time_total}`. Выполнено два прогона сценария отказов.
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| TODO | TODO | TODO | TODO |
|
| Штатный перезапуск API, PostgreSQL и Redis | Сохранение данных и действующей сессии | `make test-persistence` — PASS | Потеря подтверждённых данных не обнаружена; время не измерялось |
|
||||||
|
| Остановка Redis | 503 без анонимного доступа | `/health/ready` и `/users/me` — 503; `/health/live` — 200 | Ответ `/users/me`: 4,006 и 8,000 с; после восстановления прежний токен работает; точное время восстановления не измерялось |
|
||||||
|
| Остановка PostgreSQL | 503 без необработанной ошибки | `/health/ready` и `/users/me` — 503; `/health/live` — 200 | Ответ `/users/me`: 4,006 и 4,008 с; после восстановления — 200; точное время восстановления не измерялось |
|
||||||
|
| Истечение TTL | Отказ в доступе | После TTL=2 секунды получен 401 | Сессия становится недействительной |
|
||||||
|
| Конкурентная регистрация | Уникальность username | Один 201, один 409 | Дублирования username нет |
|
||||||
|
| Ошибка составной операции | Полный rollback | Частично созданных записей нет | Частичные изменения откатились |
|
||||||
|
|
||||||
|
Время обработки отказных запросов оказалось ограниченным в проведённых тестах, но строгая максимальная задержка на уровне сервера не доказана. Отдельный нагрузочный эксперимент с RPS, p50/p95/p99, CPU/RAM, заданной интенсивностью запросов и прогревом не проводился. Поэтому такие метрики не приводятся.
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
## Ограничения и следующие шаги
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
Приложение сохраняет данные и действующие сессии после проверенного штатного перезапуска с сохранением volumes. При недоступности PostgreSQL или Redis возвращается 503, после восстановления работа продолжается. Автоматического переключения на резервные экземпляры нет.
|
||||||
|
|
||||||
|
Redis AOF с `appendfsync everysec` не гарантирует отсутствие потери последних изменений при аварийном сбое. Удаление volumes приводит к потере локальных данных. Резервное копирование и восстановление в новое хранилище пока не реализованы.
|
||||||
|
|
||||||
|
Эффективность keyset-пагинации подтверждена одним экспериментом на 20 000 сообщениях. Результаты не следует переносить на любую нагрузку без дополнительных измерений. Стоимость записи при разных индексах не исследовалась.
|
||||||
|
|
||||||
|
В дальнейшем планируется выполнить трек 2 для оценки 5: эксперимент на 100 000+ сообщениях с неравномерным распределением по чатам, сравнение индексов и стоимости вставки, backup и восстановление в новое хранилище с проверкой данных.
|
||||||
+149
@@ -0,0 +1,149 @@
|
|||||||
|
# A generic, single database configuration.
|
||||||
|
|
||||||
|
[alembic]
|
||||||
|
# path to migration scripts.
|
||||||
|
# this is typically a path given in POSIX (e.g. forward slashes)
|
||||||
|
# format, relative to the token %(here)s which refers to the location of this
|
||||||
|
# ini file
|
||||||
|
script_location = %(here)s/migrations
|
||||||
|
|
||||||
|
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
||||||
|
# Uncomment the line below if you want the files to be prepended with date and time
|
||||||
|
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
||||||
|
# for all available tokens
|
||||||
|
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
||||||
|
# Or organize into date-based subdirectories (requires recursive_version_locations = true)
|
||||||
|
# file_template = %%(year)d/%%(month).2d/%%(day).2d_%%(hour).2d%%(minute).2d_%%(second).2d_%%(rev)s_%%(slug)s
|
||||||
|
|
||||||
|
# sys.path path, will be prepended to sys.path if present.
|
||||||
|
# defaults to the current working directory. for multiple paths, the path separator
|
||||||
|
# is defined by "path_separator" below.
|
||||||
|
prepend_sys_path = .
|
||||||
|
|
||||||
|
# timezone to use when rendering the date within the migration file
|
||||||
|
# as well as the filename.
|
||||||
|
# If specified, requires the tzdata library which can be installed by adding
|
||||||
|
# `alembic[tz]` to the pip requirements.
|
||||||
|
# string value is passed to ZoneInfo()
|
||||||
|
# leave blank for localtime
|
||||||
|
# timezone =
|
||||||
|
|
||||||
|
# max length of characters to apply to the "slug" field
|
||||||
|
# truncate_slug_length = 40
|
||||||
|
|
||||||
|
# set to 'true' to run the environment during
|
||||||
|
# the 'revision' command, regardless of autogenerate
|
||||||
|
# revision_environment = false
|
||||||
|
|
||||||
|
# set to 'true' to allow .pyc and .pyo files without
|
||||||
|
# a source .py file to be detected as revisions in the
|
||||||
|
# versions/ directory
|
||||||
|
# sourceless = false
|
||||||
|
|
||||||
|
# version location specification; This defaults
|
||||||
|
# to <script_location>/versions. When using multiple version
|
||||||
|
# directories, initial revisions must be specified with --version-path.
|
||||||
|
# The path separator used here should be the separator specified by "path_separator"
|
||||||
|
# below.
|
||||||
|
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
|
||||||
|
|
||||||
|
# path_separator; This indicates what character is used to split lists of file
|
||||||
|
# paths, including version_locations and prepend_sys_path within configparser
|
||||||
|
# files such as alembic.ini.
|
||||||
|
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
|
||||||
|
# to provide os-dependent path splitting.
|
||||||
|
#
|
||||||
|
# Note that in order to support legacy alembic.ini files, this default does NOT
|
||||||
|
# take place if path_separator is not present in alembic.ini. If this
|
||||||
|
# option is omitted entirely, fallback logic is as follows:
|
||||||
|
#
|
||||||
|
# 1. Parsing of the version_locations option falls back to using the legacy
|
||||||
|
# "version_path_separator" key, which if absent then falls back to the legacy
|
||||||
|
# behavior of splitting on spaces and/or commas.
|
||||||
|
# 2. Parsing of the prepend_sys_path option falls back to the legacy
|
||||||
|
# behavior of splitting on spaces, commas, or colons.
|
||||||
|
#
|
||||||
|
# Valid values for path_separator are:
|
||||||
|
#
|
||||||
|
# path_separator = :
|
||||||
|
# path_separator = ;
|
||||||
|
# path_separator = space
|
||||||
|
# path_separator = newline
|
||||||
|
#
|
||||||
|
# Use os.pathsep. Default configuration used for new projects.
|
||||||
|
path_separator = os
|
||||||
|
|
||||||
|
|
||||||
|
# set to 'true' to search source files recursively
|
||||||
|
# in each "version_locations" directory
|
||||||
|
# new in Alembic version 1.10
|
||||||
|
# recursive_version_locations = false
|
||||||
|
|
||||||
|
# the output encoding used when revision files
|
||||||
|
# are written from script.py.mako
|
||||||
|
# output_encoding = utf-8
|
||||||
|
|
||||||
|
# database URL. This is consumed by the user-maintained env.py script only.
|
||||||
|
# other means of configuring database URLs may be customized within the env.py
|
||||||
|
# file.
|
||||||
|
sqlalchemy.url =
|
||||||
|
|
||||||
|
|
||||||
|
[post_write_hooks]
|
||||||
|
# post_write_hooks defines scripts or Python functions that are run
|
||||||
|
# on newly generated revision scripts. See the documentation for further
|
||||||
|
# detail and examples
|
||||||
|
|
||||||
|
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||||
|
# hooks = black
|
||||||
|
# black.type = console_scripts
|
||||||
|
# black.entrypoint = black
|
||||||
|
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module
|
||||||
|
# hooks = ruff
|
||||||
|
# ruff.type = module
|
||||||
|
# ruff.module = ruff
|
||||||
|
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# Alternatively, use the exec runner to execute a binary found on your PATH
|
||||||
|
# hooks = ruff
|
||||||
|
# ruff.type = exec
|
||||||
|
# ruff.executable = ruff
|
||||||
|
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# Logging configuration. This is also consumed by the user-maintained
|
||||||
|
# env.py script only.
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARNING
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARNING
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:17.6
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: messenger
|
||||||
|
POSTGRES_USER: lab2
|
||||||
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||||
|
secrets:
|
||||||
|
- postgres_password
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U lab2 -d messenger"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 10
|
||||||
|
start_period: 5s
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:8.2.2
|
||||||
|
command:
|
||||||
|
- redis-server
|
||||||
|
- --appendonly
|
||||||
|
- "yes"
|
||||||
|
- --appendfsync
|
||||||
|
- everysec
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 10
|
||||||
|
start_period: 5s
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
command: ["alembic", "upgrade", "head"]
|
||||||
|
environment:
|
||||||
|
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||||
|
secrets:
|
||||||
|
- database_url
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: "no"
|
||||||
|
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
environment:
|
||||||
|
APP_HOST: ${APP_HOST:-0.0.0.0}
|
||||||
|
APP_PORT: ${APP_PORT:-8080}
|
||||||
|
APP_ENV: ${APP_ENV:-lab}
|
||||||
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||||
|
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||||
|
SESSION_STORE: ${SESSION_STORE:-redis}
|
||||||
|
CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url}
|
||||||
|
secrets:
|
||||||
|
- database_url
|
||||||
|
- cache_url
|
||||||
|
depends_on:
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
|
||||||
|
secrets:
|
||||||
|
postgres_password:
|
||||||
|
file: ./secrets/postgres_password
|
||||||
|
|
||||||
|
database_url:
|
||||||
|
file: ./secrets/database_url
|
||||||
|
|
||||||
|
cache_url:
|
||||||
|
file: ./secrets/cache_url
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
=== Lab 2 Grade 4: message history index experiment ===
|
||||||
|
=== Preparing synthetic benchmark user ===
|
||||||
|
INSERT 0 0
|
||||||
|
=== Preparing test chat ===
|
||||||
|
INSERT 0 0
|
||||||
|
INSERT 0 0
|
||||||
|
=== Preparing 20000 messages ===
|
||||||
|
INSERT 0 0
|
||||||
|
=== Test data size ===
|
||||||
|
message_count
|
||||||
|
---------------
|
||||||
|
20000
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
=== Refreshing PostgreSQL statistics ===
|
||||||
|
ANALYZE
|
||||||
|
=== OFFSET pagination after 15000 messages ===
|
||||||
|
QUERY PLAN
|
||||||
|
--------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
Limit (cost=2129.70..2143.90 rows=100 width=82) (actual time=1.883..1.896 rows=100 loops=1)
|
||||||
|
Buffers: shared hit=435
|
||||||
|
-> Index Scan using ix_messages_chat_created_id on messages (cost=0.41..2839.47 rows=20000 width=82) (actual time=0.020..1.535 rows=15100 loops=1)
|
||||||
|
Index Cond: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid)
|
||||||
|
Buffers: shared hit=435
|
||||||
|
Planning:
|
||||||
|
Buffers: shared hit=21
|
||||||
|
Planning Time: 0.094 ms
|
||||||
|
Execution Time: 1.908 ms
|
||||||
|
(9 rows)
|
||||||
|
|
||||||
|
=== KEYSET pagination after message 15000 ===
|
||||||
|
QUERY PLAN
|
||||||
|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
Limit (cost=0.41..30.01 rows=100 width=82) (actual time=0.008..0.031 rows=100 loops=1)
|
||||||
|
Buffers: shared hit=7
|
||||||
|
-> Index Scan using ix_messages_chat_created_id on messages (cost=0.41..1481.99 rows=5006 width=82) (actual time=0.007..0.022 rows=100 loops=1)
|
||||||
|
Index Cond: ((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:15+00'::timestamp with time zone, '00003a98-0000-4000-8000-000000003a98'::uuid)))
|
||||||
|
Buffers: shared hit=7
|
||||||
|
Planning Time: 0.048 ms
|
||||||
|
Execution Time: 0.053 ms
|
||||||
|
(7 rows)
|
||||||
|
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
Waiting for API readiness before testing...
|
||||||
|
Creating a test user and session...
|
||||||
|
Test session created.
|
||||||
|
|
||||||
|
Checking baseline...
|
||||||
|
PASS: /health/live returned 200 in 0.001683s
|
||||||
|
PASS: /health/ready returned 200 in 0.003160s
|
||||||
|
PASS: /api/v1/users/me returned 200 in 0.002653s
|
||||||
|
|
||||||
|
=== Redis failure ===
|
||||||
|
Container mtusi2026lab2-redis-1 Stopping
|
||||||
|
Container mtusi2026lab2-redis-1 Stopped
|
||||||
|
PASS: /health/live returned 200 in 0.001606s
|
||||||
|
PASS: /health/ready returned 503 in 4.006407s
|
||||||
|
PASS: /api/v1/users/me returned 503 in 4.006094s
|
||||||
|
Restarting Redis...
|
||||||
|
Container mtusi2026lab2-redis-1 Starting
|
||||||
|
Container mtusi2026lab2-redis-1 Started
|
||||||
|
PASS: /health/ready returned 200 in 0.002260s
|
||||||
|
PASS: /api/v1/users/me returned 200 in 0.002382s
|
||||||
|
|
||||||
|
=== PostgreSQL failure ===
|
||||||
|
Container mtusi2026lab2-postgres-1 Stopping
|
||||||
|
Container mtusi2026lab2-postgres-1 Stopped
|
||||||
|
PASS: /health/live returned 200 in 0.001882s
|
||||||
|
PASS: /health/ready returned 503 in 4.007958s
|
||||||
|
PASS: /api/v1/users/me returned 503 in 4.006373s
|
||||||
|
Restarting PostgreSQL...
|
||||||
|
Container mtusi2026lab2-postgres-1 Starting
|
||||||
|
Container mtusi2026lab2-postgres-1 Started
|
||||||
|
PASS: /health/ready returned 200 in 0.002324s
|
||||||
|
PASS: /api/v1/users/me returned 200 in 0.012524s
|
||||||
|
|
||||||
|
PASS: dependency failure and recovery tests completed.
|
||||||
|
Restoring PostgreSQL and Redis...
|
||||||
|
Container mtusi2026lab2-postgres-1 Running
|
||||||
|
Container mtusi2026lab2-redis-1 Running
|
||||||
|
Waiting for API readiness...
|
||||||
|
Services restored.
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
=== make check ===
|
||||||
|
python3 scripts/check_contract.py
|
||||||
|
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||||
|
|
||||||
|
=== make validate ===
|
||||||
|
.venv/bin/python scripts/validate_openapi.py
|
||||||
|
PASS: full OpenAPI 3.1 and JSON Schema validation; 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.
|
||||||
|
|
||||||
|
=== test_auth ===
|
||||||
|
PASS: wrong password returned 401; valid session worked; logout revoked the token.
|
||||||
|
|
||||||
|
=== test_race ===
|
||||||
|
PASS: concurrent registration produced exactly one 201 and one 409.
|
||||||
|
|
||||||
|
=== test_rollback ===
|
||||||
|
Container mtusi2026lab2-api-run-8eb3c54add45 Creating
|
||||||
|
Container mtusi2026lab2-api-run-8eb3c54add45 Created
|
||||||
|
PASS: failed compound transaction rolled back chat and membership inserts.
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
=== persistence ===
|
||||||
|
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.
|
||||||
|
|
||||||
|
=== short TTL ===
|
||||||
|
Starting API with SESSION_TTL_SECONDS=2...
|
||||||
|
Container mtusi2026lab2-api-1 Recreate
|
||||||
|
Container mtusi2026lab2-api-1 Recreated
|
||||||
|
Container mtusi2026lab2-api-1 Starting
|
||||||
|
Container mtusi2026lab2-api-1 Started
|
||||||
|
Checking API configuration...
|
||||||
|
Waiting for API readiness...
|
||||||
|
Running short TTL test...
|
||||||
|
PASS: token works immediately after login.
|
||||||
|
PASS: expired token returned 401.
|
||||||
|
PASS: short session TTL test completed.
|
||||||
|
Restoring API with SESSION_TTL_SECONDS=3600...
|
||||||
|
Container mtusi2026lab2-api-1 Recreate
|
||||||
|
Container mtusi2026lab2-api-1 Recreated
|
||||||
|
Container mtusi2026lab2-api-1 Starting
|
||||||
|
Container mtusi2026lab2-api-1 Started
|
||||||
|
|
||||||
|
=== dependency failures ===
|
||||||
|
Waiting for API readiness before testing...
|
||||||
|
Creating a test user and session...
|
||||||
|
Test session created.
|
||||||
|
|
||||||
|
Checking baseline...
|
||||||
|
PASS: /health/live returned 200 in 0.002339s
|
||||||
|
PASS: /health/ready returned 200 in 0.003825s
|
||||||
|
PASS: /api/v1/users/me returned 200 in 0.004731s
|
||||||
|
|
||||||
|
=== Redis failure ===
|
||||||
|
Container mtusi2026lab2-redis-1 Stopping
|
||||||
|
Container mtusi2026lab2-redis-1 Stopped
|
||||||
|
PASS: /health/live returned 200 in 0.002183s
|
||||||
|
PASS: /health/ready returned 503 in 4.007097s
|
||||||
|
PASS: /api/v1/users/me returned 503 in 8.000377s
|
||||||
|
Restarting Redis...
|
||||||
|
Container mtusi2026lab2-redis-1 Starting
|
||||||
|
Container mtusi2026lab2-redis-1 Started
|
||||||
|
PASS: /health/ready returned 200 in 0.002200s
|
||||||
|
PASS: /api/v1/users/me returned 200 in 0.003519s
|
||||||
|
|
||||||
|
=== PostgreSQL failure ===
|
||||||
|
Container mtusi2026lab2-postgres-1 Stopping
|
||||||
|
Container mtusi2026lab2-postgres-1 Stopped
|
||||||
|
PASS: /health/live returned 200 in 0.002847s
|
||||||
|
PASS: /health/ready returned 503 in 4.006891s
|
||||||
|
PASS: /api/v1/users/me returned 503 in 4.008468s
|
||||||
|
Restarting PostgreSQL...
|
||||||
|
Container mtusi2026lab2-postgres-1 Starting
|
||||||
|
Container mtusi2026lab2-postgres-1 Started
|
||||||
|
PASS: /health/ready returned 200 in 0.003164s
|
||||||
|
PASS: /api/v1/users/me returned 200 in 0.006318s
|
||||||
|
|
||||||
|
PASS: dependency failure and recovery tests completed.
|
||||||
|
Restoring PostgreSQL and Redis...
|
||||||
|
Container mtusi2026lab2-postgres-1 Running
|
||||||
|
Container mtusi2026lab2-redis-1 Running
|
||||||
|
Waiting for API readiness...
|
||||||
|
Services restored.
|
||||||
|
|
||||||
|
=== final readiness ===
|
||||||
|
{"status":"ok"}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Generic single-database configuration with an async dbapi.
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import asyncio
|
||||||
|
|
||||||
|
from alembic import context
|
||||||
|
from sqlalchemy import pool
|
||||||
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||||
|
|
||||||
|
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
DATABASE_URL_FILE = "/run/secrets/database_url"
|
||||||
|
|
||||||
|
|
||||||
|
def read_database_url() -> str:
|
||||||
|
with open(DATABASE_URL_FILE, encoding="utf-8") as file:
|
||||||
|
return file.read().strip()
|
||||||
|
|
||||||
|
|
||||||
|
config.set_main_option("sqlalchemy.url", read_database_url())
|
||||||
|
|
||||||
|
target_metadata = None
|
||||||
|
|
||||||
|
|
||||||
|
def do_run_migrations(connection) -> None:
|
||||||
|
context.configure(
|
||||||
|
connection=connection,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
async def run_async_migrations() -> None:
|
||||||
|
connectable = async_engine_from_config(
|
||||||
|
config.get_section(config.config_ini_section, {}),
|
||||||
|
prefix="sqlalchemy.",
|
||||||
|
poolclass=pool.NullPool,
|
||||||
|
)
|
||||||
|
|
||||||
|
async with connectable.connect() as connection:
|
||||||
|
await connection.run_sync(do_run_migrations)
|
||||||
|
|
||||||
|
await connectable.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_online() -> None:
|
||||||
|
asyncio.run(run_async_migrations())
|
||||||
|
|
||||||
|
|
||||||
|
run_migrations_online()
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
"""${message}
|
||||||
|
|
||||||
|
Revision ID: ${up_revision}
|
||||||
|
Revises: ${down_revision | comma,n}
|
||||||
|
Create Date: ${create_date}
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
${imports if imports else ""}
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = ${repr(up_revision)}
|
||||||
|
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||||
|
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
"""Upgrade schema."""
|
||||||
|
${upgrades if upgrades else "pass"}
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
"""Downgrade schema."""
|
||||||
|
${downgrades if downgrades else "pass"}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
"""initial schema
|
||||||
|
|
||||||
|
Revision ID: f7c51e1e945b
|
||||||
|
Revises:
|
||||||
|
Create Date: 2026-10-07 18:33:37.771593
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = "f7c51e1e945b"
|
||||||
|
down_revision: Union[str, Sequence[str], None] = None
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
op.create_table(
|
||||||
|
"users",
|
||||||
|
sa.Column("id", sa.Uuid(), nullable=False),
|
||||||
|
sa.Column("username", sa.String(length=32), nullable=False),
|
||||||
|
sa.Column("display_name", sa.String(length=100), nullable=False),
|
||||||
|
sa.Column("password_hash", sa.Text(), nullable=False),
|
||||||
|
sa.Column(
|
||||||
|
"created_at",
|
||||||
|
sa.DateTime(timezone=True),
|
||||||
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||||
|
nullable=False,
|
||||||
|
),
|
||||||
|
sa.PrimaryKeyConstraint("id"),
|
||||||
|
sa.UniqueConstraint("username", name="uq_users_username"),
|
||||||
|
)
|
||||||
|
|
||||||
|
op.create_table(
|
||||||
|
"chats",
|
||||||
|
sa.Column("id", sa.Uuid(), nullable=False),
|
||||||
|
sa.Column("title", sa.String(length=100), nullable=False),
|
||||||
|
sa.Column(
|
||||||
|
"created_at",
|
||||||
|
sa.DateTime(timezone=True),
|
||||||
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||||
|
nullable=False,
|
||||||
|
),
|
||||||
|
sa.PrimaryKeyConstraint("id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
op.create_table(
|
||||||
|
"chat_members",
|
||||||
|
sa.Column("chat_id", sa.Uuid(), nullable=False),
|
||||||
|
sa.Column("user_id", sa.Uuid(), nullable=False),
|
||||||
|
sa.ForeignKeyConstraint(
|
||||||
|
["chat_id"],
|
||||||
|
["chats.id"],
|
||||||
|
ondelete="CASCADE",
|
||||||
|
),
|
||||||
|
sa.ForeignKeyConstraint(
|
||||||
|
["user_id"],
|
||||||
|
["users.id"],
|
||||||
|
ondelete="CASCADE",
|
||||||
|
),
|
||||||
|
sa.PrimaryKeyConstraint("chat_id", "user_id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
op.create_table(
|
||||||
|
"messages",
|
||||||
|
sa.Column("id", sa.Uuid(), nullable=False),
|
||||||
|
sa.Column("chat_id", sa.Uuid(), nullable=False),
|
||||||
|
sa.Column("sender_id", sa.Uuid(), nullable=False),
|
||||||
|
sa.Column("text", sa.String(length=4000), nullable=False),
|
||||||
|
sa.Column(
|
||||||
|
"created_at",
|
||||||
|
sa.DateTime(timezone=True),
|
||||||
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||||
|
nullable=False,
|
||||||
|
),
|
||||||
|
sa.ForeignKeyConstraint(
|
||||||
|
["chat_id"],
|
||||||
|
["chats.id"],
|
||||||
|
ondelete="CASCADE",
|
||||||
|
),
|
||||||
|
sa.ForeignKeyConstraint(
|
||||||
|
["sender_id"],
|
||||||
|
["users.id"],
|
||||||
|
ondelete="RESTRICT",
|
||||||
|
),
|
||||||
|
sa.PrimaryKeyConstraint("id"),
|
||||||
|
)
|
||||||
|
|
||||||
|
op.create_index(
|
||||||
|
"ix_messages_chat_created_id",
|
||||||
|
"messages",
|
||||||
|
["chat_id", "created_at", "id"],
|
||||||
|
unique=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
op.drop_index(
|
||||||
|
"ix_messages_chat_created_id",
|
||||||
|
table_name="messages",
|
||||||
|
)
|
||||||
|
op.drop_table("messages")
|
||||||
|
op.drop_table("chat_members")
|
||||||
|
op.drop_table("chats")
|
||||||
|
op.drop_table("users")
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
annotated-doc==0.0.5
|
||||||
|
annotated-types==0.8.0
|
||||||
|
anyio==4.15.1
|
||||||
|
click==8.5.0
|
||||||
|
fastapi==0.141.1
|
||||||
|
h11==0.16.0
|
||||||
|
idna==3.20
|
||||||
|
pydantic==2.13.5
|
||||||
|
pydantic_core==2.46.5
|
||||||
|
starlette==1.7.0
|
||||||
|
typing-inspection==0.4.4
|
||||||
|
typing_extensions==4.16.0
|
||||||
|
uvicorn==0.53.0
|
||||||
|
SQLAlchemy==2.1.4
|
||||||
|
asyncpg==0.32.0
|
||||||
|
alembic==1.20.0
|
||||||
|
greenlet==3.5.6
|
||||||
|
|
||||||
|
argon2-cffi==25.1.0
|
||||||
|
redis==7.0.1
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
\set ON_ERROR_STOP on
|
||||||
|
|
||||||
|
\echo '=== Lab 2 Grade 4: message history index experiment ==='
|
||||||
|
|
||||||
|
-- Fixed identifiers make the experiment repeatable.
|
||||||
|
\set chat_id '22222222-2222-4222-8222-222222222222'
|
||||||
|
\set user_id '33333333-3333-4333-8333-333333333333'
|
||||||
|
|
||||||
|
\echo '=== Preparing synthetic benchmark user ==='
|
||||||
|
|
||||||
|
INSERT INTO users (
|
||||||
|
id,
|
||||||
|
username,
|
||||||
|
display_name,
|
||||||
|
password_hash,
|
||||||
|
created_at
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
:'user_id'::uuid,
|
||||||
|
'explain_benchmark_user',
|
||||||
|
'EXPLAIN benchmark user',
|
||||||
|
'benchmark-only-not-a-real-password-hash',
|
||||||
|
CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
ON CONFLICT (id) DO NOTHING;
|
||||||
|
|
||||||
|
\echo '=== Preparing test chat ==='
|
||||||
|
|
||||||
|
INSERT INTO chats (id, title)
|
||||||
|
VALUES (
|
||||||
|
:'chat_id'::uuid,
|
||||||
|
'EXPLAIN test 20000 messages'
|
||||||
|
)
|
||||||
|
ON CONFLICT (id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO chat_members (chat_id, user_id)
|
||||||
|
VALUES (
|
||||||
|
:'chat_id'::uuid,
|
||||||
|
:'user_id'::uuid
|
||||||
|
)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
|
|
||||||
|
\echo '=== Preparing 20000 messages ==='
|
||||||
|
|
||||||
|
INSERT INTO messages (
|
||||||
|
id,
|
||||||
|
chat_id,
|
||||||
|
sender_id,
|
||||||
|
text,
|
||||||
|
created_at
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
(
|
||||||
|
lpad(to_hex(g), 8, '0') ||
|
||||||
|
'-0000-4000-8000-' ||
|
||||||
|
lpad(to_hex(g), 12, '0')
|
||||||
|
)::uuid,
|
||||||
|
:'chat_id'::uuid,
|
||||||
|
:'user_id'::uuid,
|
||||||
|
'EXPLAIN test message ' || g,
|
||||||
|
'2026-01-01 00:00:00+00'::timestamptz
|
||||||
|
+ g * interval '1 millisecond'
|
||||||
|
FROM generate_series(1, 20000) AS g
|
||||||
|
ON CONFLICT (id) DO NOTHING;
|
||||||
|
|
||||||
|
|
||||||
|
\echo '=== Test data size ==='
|
||||||
|
|
||||||
|
SELECT COUNT(*) AS message_count
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = :'chat_id'::uuid;
|
||||||
|
|
||||||
|
|
||||||
|
\echo '=== Refreshing PostgreSQL statistics ==='
|
||||||
|
|
||||||
|
ANALYZE messages;
|
||||||
|
|
||||||
|
|
||||||
|
\echo '=== OFFSET pagination after 15000 messages ==='
|
||||||
|
|
||||||
|
EXPLAIN (ANALYZE, BUFFERS)
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
chat_id,
|
||||||
|
sender_id,
|
||||||
|
text,
|
||||||
|
created_at
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = :'chat_id'::uuid
|
||||||
|
ORDER BY created_at, id
|
||||||
|
OFFSET 15000
|
||||||
|
LIMIT 100;
|
||||||
|
|
||||||
|
|
||||||
|
\echo '=== KEYSET pagination after message 15000 ==='
|
||||||
|
|
||||||
|
EXPLAIN (ANALYZE, BUFFERS)
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
chat_id,
|
||||||
|
sender_id,
|
||||||
|
text,
|
||||||
|
created_at
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = :'chat_id'::uuid
|
||||||
|
AND (created_at, id) >
|
||||||
|
(
|
||||||
|
'2026-01-01 00:00:15+00'::timestamptz,
|
||||||
|
'00003a98-0000-4000-8000-000000003a98'::uuid
|
||||||
|
)
|
||||||
|
ORDER BY created_at, id
|
||||||
|
LIMIT 100;
|
||||||
Executable
+41
@@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
echo "Stopping application and stores..."
|
||||||
|
docker compose down
|
||||||
|
|
||||||
|
echo "Starting application and stores..."
|
||||||
|
docker compose up -d
|
||||||
|
|
||||||
|
echo "Waiting for services to become healthy..."
|
||||||
|
|
||||||
|
for attempt in $(seq 1 60); do
|
||||||
|
postgres_status=$(
|
||||||
|
docker compose ps postgres --format json \
|
||||||
|
| python -c 'import sys, json; data=json.load(sys.stdin); print(data.get("Health", ""))'
|
||||||
|
)
|
||||||
|
|
||||||
|
redis_status=$(
|
||||||
|
docker compose ps redis --format json \
|
||||||
|
| python -c 'import sys, json; data=json.load(sys.stdin); print(data.get("Health", ""))'
|
||||||
|
)
|
||||||
|
|
||||||
|
api_status=$(
|
||||||
|
docker compose ps api --format json \
|
||||||
|
| python -c 'import sys, json; data=json.load(sys.stdin); print(data.get("Health", ""))'
|
||||||
|
)
|
||||||
|
|
||||||
|
if [[ "$postgres_status" == "healthy" \
|
||||||
|
&& "$redis_status" == "healthy" \
|
||||||
|
&& "$api_status" == "healthy" ]]; then
|
||||||
|
echo "All services are healthy."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Services did not become healthy in time." >&2
|
||||||
|
docker compose ps
|
||||||
|
exit 1
|
||||||
Executable
+189
@@ -0,0 +1,189 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
BASE_URL="http://localhost:8080"
|
||||||
|
TOKEN_FILE=$(mktemp)
|
||||||
|
|
||||||
|
restore_services() {
|
||||||
|
echo "Restoring PostgreSQL and Redis..."
|
||||||
|
docker compose up -d postgres redis
|
||||||
|
|
||||||
|
echo "Waiting for API readiness..."
|
||||||
|
for attempt in $(seq 1 60); do
|
||||||
|
if curl --silent --fail \
|
||||||
|
"$BASE_URL/health/ready" > /dev/null; then
|
||||||
|
rm -f "$TOKEN_FILE"
|
||||||
|
echo "Services restored."
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
rm -f "$TOKEN_FILE"
|
||||||
|
echo "WARNING: API did not become ready after recovery." >&2
|
||||||
|
}
|
||||||
|
|
||||||
|
trap restore_services EXIT
|
||||||
|
|
||||||
|
echo "Waiting for API readiness before testing..."
|
||||||
|
|
||||||
|
api_ready=false
|
||||||
|
|
||||||
|
for attempt in $(seq 1 60); do
|
||||||
|
if curl --silent --fail --max-time 3 \
|
||||||
|
"$BASE_URL/health/ready" > /dev/null; then
|
||||||
|
api_ready=true
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "$api_ready" != "true" ]]; then
|
||||||
|
echo "FAIL: API did not become ready before testing." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
check_status() {
|
||||||
|
local expected="$1"
|
||||||
|
local path="$2"
|
||||||
|
local authorization="${3:-}"
|
||||||
|
|
||||||
|
local result actual_status elapsed
|
||||||
|
|
||||||
|
if [[ -n "$authorization" ]]; then
|
||||||
|
result=$(curl --silent --output /dev/null \
|
||||||
|
--max-time 12 \
|
||||||
|
--write-out "%{http_code} %{time_total}" \
|
||||||
|
-H "Authorization: Bearer $authorization" \
|
||||||
|
"$BASE_URL$path")
|
||||||
|
else
|
||||||
|
result=$(curl --silent --output /dev/null \
|
||||||
|
--max-time 12 \
|
||||||
|
--write-out "%{http_code} %{time_total}" \
|
||||||
|
"$BASE_URL$path")
|
||||||
|
fi
|
||||||
|
|
||||||
|
read -r actual_status elapsed <<< "$result"
|
||||||
|
|
||||||
|
if [[ "$actual_status" != "$expected" ]]; then
|
||||||
|
echo "FAIL: $path expected $expected, got $actual_status" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "PASS: $path returned $actual_status in ${elapsed}s"
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Creating a test user and session..."
|
||||||
|
|
||||||
|
python - "$TOKEN_FILE" <<'PY'
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import urllib.request
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
base = "http://localhost:8080"
|
||||||
|
username = "failure_" + uuid.uuid4().hex[:16]
|
||||||
|
password = "correct-password-123"
|
||||||
|
|
||||||
|
def post(path, data=None, authorization=None):
|
||||||
|
headers = {}
|
||||||
|
body = None
|
||||||
|
|
||||||
|
if data is not None:
|
||||||
|
headers["Content-Type"] = "application/json"
|
||||||
|
body = json.dumps(data).encode("utf-8")
|
||||||
|
|
||||||
|
if authorization:
|
||||||
|
headers["Authorization"] = authorization
|
||||||
|
|
||||||
|
request = urllib.request.Request(
|
||||||
|
base + path,
|
||||||
|
data=body,
|
||||||
|
headers=headers,
|
||||||
|
method="POST",
|
||||||
|
)
|
||||||
|
|
||||||
|
with urllib.request.urlopen(request, timeout=10) as response:
|
||||||
|
assert response.status == 201
|
||||||
|
return json.load(response)
|
||||||
|
|
||||||
|
post("/api/v1/users", {
|
||||||
|
"username": username,
|
||||||
|
"display_name": "Failure Test User",
|
||||||
|
"password": password,
|
||||||
|
})
|
||||||
|
|
||||||
|
credentials = base64.b64encode(
|
||||||
|
f"{username}:{password}".encode()
|
||||||
|
).decode()
|
||||||
|
|
||||||
|
result = post(
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
authorization=f"Basic {credentials}",
|
||||||
|
)
|
||||||
|
|
||||||
|
with open(sys.argv[1], "w", encoding="utf-8") as file:
|
||||||
|
file.write(result["token"])
|
||||||
|
|
||||||
|
print("Test session created.")
|
||||||
|
PY
|
||||||
|
|
||||||
|
TOKEN=$(cat "$TOKEN_FILE")
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "Checking baseline..."
|
||||||
|
check_status 200 "/health/live"
|
||||||
|
check_status 200 "/health/ready"
|
||||||
|
check_status 200 "/api/v1/users/me" "$TOKEN"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "=== Redis failure ==="
|
||||||
|
|
||||||
|
docker compose stop redis
|
||||||
|
|
||||||
|
check_status 200 "/health/live"
|
||||||
|
check_status 503 "/health/ready"
|
||||||
|
check_status 503 "/api/v1/users/me" "$TOKEN"
|
||||||
|
|
||||||
|
echo "Restarting Redis..."
|
||||||
|
docker compose start redis
|
||||||
|
|
||||||
|
for attempt in $(seq 1 60); do
|
||||||
|
if curl --silent --fail \
|
||||||
|
"$BASE_URL/health/ready" > /dev/null; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
check_status 200 "/health/ready"
|
||||||
|
check_status 200 "/api/v1/users/me" "$TOKEN"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "=== PostgreSQL failure ==="
|
||||||
|
|
||||||
|
docker compose stop postgres
|
||||||
|
|
||||||
|
check_status 200 "/health/live"
|
||||||
|
check_status 503 "/health/ready"
|
||||||
|
check_status 503 "/api/v1/users/me" "$TOKEN"
|
||||||
|
|
||||||
|
echo "Restarting PostgreSQL..."
|
||||||
|
docker compose start postgres
|
||||||
|
|
||||||
|
for attempt in $(seq 1 60); do
|
||||||
|
if curl --silent --fail \
|
||||||
|
"$BASE_URL/health/ready" > /dev/null; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
check_status 200 "/health/ready"
|
||||||
|
check_status 200 "/api/v1/users/me" "$TOKEN"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "PASS: dependency failure and recovery tests completed."
|
||||||
Executable
+51
@@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
restore_api() {
|
||||||
|
echo "Restoring API with SESSION_TTL_SECONDS=3600..."
|
||||||
|
|
||||||
|
SESSION_TTL_SECONDS=3600 \
|
||||||
|
docker compose up -d --force-recreate --no-deps api
|
||||||
|
}
|
||||||
|
|
||||||
|
trap restore_api EXIT
|
||||||
|
|
||||||
|
echo "Starting API with SESSION_TTL_SECONDS=2..."
|
||||||
|
|
||||||
|
SESSION_TTL_SECONDS=2 \
|
||||||
|
docker compose up -d --force-recreate --no-deps api
|
||||||
|
|
||||||
|
echo "Checking API configuration..."
|
||||||
|
|
||||||
|
actual_ttl=$(docker compose exec -T api printenv SESSION_TTL_SECONDS)
|
||||||
|
|
||||||
|
if [[ "$actual_ttl" != "2" ]]; then
|
||||||
|
echo "Expected SESSION_TTL_SECONDS=2, got $actual_ttl" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Waiting for API readiness..."
|
||||||
|
|
||||||
|
ready=false
|
||||||
|
|
||||||
|
for attempt in $(seq 1 30); do
|
||||||
|
if curl --silent --fail \
|
||||||
|
http://localhost:8080/health/ready > /dev/null; then
|
||||||
|
ready=true
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "$ready" != "true" ]]; then
|
||||||
|
echo "API did not become ready." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Running short TTL test..."
|
||||||
|
|
||||||
|
python tests_student/test_short_ttl.py
|
||||||
|
|
||||||
|
echo "PASS: short session TTL test completed."
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import base64
|
||||||
|
import binascii
|
||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
|
||||||
|
def encode_cursor(
|
||||||
|
chat_id: UUID,
|
||||||
|
created_at: datetime,
|
||||||
|
message_id: UUID,
|
||||||
|
) -> str:
|
||||||
|
data = {
|
||||||
|
"chat_id": str(chat_id),
|
||||||
|
"created_at": created_at.isoformat(),
|
||||||
|
"message_id": str(message_id),
|
||||||
|
}
|
||||||
|
|
||||||
|
raw = json.dumps(
|
||||||
|
data,
|
||||||
|
separators=(",", ":"),
|
||||||
|
).encode("utf-8")
|
||||||
|
|
||||||
|
return base64.urlsafe_b64encode(raw).decode("ascii")
|
||||||
|
|
||||||
|
|
||||||
|
def decode_cursor(cursor: str) -> tuple[UUID, datetime, UUID]:
|
||||||
|
try:
|
||||||
|
raw = base64.urlsafe_b64decode(cursor.encode("ascii"))
|
||||||
|
|
||||||
|
data = json.loads(raw.decode("utf-8"))
|
||||||
|
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise ValueError("Invalid cursor")
|
||||||
|
|
||||||
|
if set(data.keys()) != {
|
||||||
|
"chat_id",
|
||||||
|
"created_at",
|
||||||
|
"message_id",
|
||||||
|
}:
|
||||||
|
raise ValueError("Invalid cursor")
|
||||||
|
|
||||||
|
chat_id = UUID(data["chat_id"])
|
||||||
|
created_at = datetime.fromisoformat(data["created_at"])
|
||||||
|
message_id = UUID(data["message_id"])
|
||||||
|
|
||||||
|
if created_at.tzinfo is None or created_at.utcoffset() is None:
|
||||||
|
raise ValueError("Invalid cursor")
|
||||||
|
|
||||||
|
return chat_id, created_at, message_id
|
||||||
|
|
||||||
|
except (
|
||||||
|
ValueError,
|
||||||
|
KeyError,
|
||||||
|
TypeError,
|
||||||
|
UnicodeError,
|
||||||
|
binascii.Error,
|
||||||
|
json.JSONDecodeError,
|
||||||
|
) as exc:
|
||||||
|
raise ValueError("Invalid cursor") from exc
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
from collections.abc import AsyncIterator
|
||||||
|
|
||||||
|
import socket
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlalchemy.ext.asyncio import (
|
||||||
|
AsyncSession,
|
||||||
|
async_sessionmaker,
|
||||||
|
create_async_engine,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
DATABASE_URL_FILE = "/run/secrets/database_url"
|
||||||
|
|
||||||
|
POOL_SIZE = 5
|
||||||
|
MAX_OVERFLOW = 5
|
||||||
|
POOL_TIMEOUT_SECONDS = 5
|
||||||
|
DB_CONNECT_TIMEOUT_SECONDS = 5
|
||||||
|
|
||||||
|
|
||||||
|
def read_database_url() -> str:
|
||||||
|
with open(DATABASE_URL_FILE, encoding="utf-8") as file:
|
||||||
|
return file.read().strip()
|
||||||
|
|
||||||
|
|
||||||
|
database_url = read_database_url()
|
||||||
|
|
||||||
|
engine = create_async_engine(
|
||||||
|
database_url,
|
||||||
|
pool_size=POOL_SIZE,
|
||||||
|
max_overflow=MAX_OVERFLOW,
|
||||||
|
pool_timeout=POOL_TIMEOUT_SECONDS,
|
||||||
|
connect_args={
|
||||||
|
"timeout": DB_CONNECT_TIMEOUT_SECONDS,
|
||||||
|
},
|
||||||
|
pool_pre_ping=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionFactory = async_sessionmaker(
|
||||||
|
bind=engine,
|
||||||
|
expire_on_commit=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_db_session() -> AsyncIterator[AsyncSession]:
|
||||||
|
async with SessionFactory() as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
|
async def ping_database() -> bool:
|
||||||
|
try:
|
||||||
|
async with engine.connect() as connection:
|
||||||
|
await connection.execute(text("SELECT 1"))
|
||||||
|
return True
|
||||||
|
except (SQLAlchemyError, OSError):
|
||||||
|
return False
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
class APIError(Exception):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
status_code: int,
|
||||||
|
code: str,
|
||||||
|
message: str,
|
||||||
|
headers: dict[str, str] | None = None,
|
||||||
|
):
|
||||||
|
self.status_code = status_code
|
||||||
|
self.code = code
|
||||||
|
self.message = message
|
||||||
|
self.headers = headers
|
||||||
+483
@@ -0,0 +1,483 @@
|
|||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
import socket
|
||||||
|
|
||||||
|
from fastapi import Depends, FastAPI, Request
|
||||||
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from fastapi.security import (
|
||||||
|
HTTPAuthorizationCredentials,
|
||||||
|
HTTPBasic,
|
||||||
|
HTTPBasicCredentials,
|
||||||
|
HTTPBearer,
|
||||||
|
)
|
||||||
|
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
|
|
||||||
|
from src.database import get_db_session, ping_database
|
||||||
|
from src.errors import APIError
|
||||||
|
from src.models import CreateUser, SessionResponse, User, CreateChat, Chat, ChatPage, CreateMessage, Message, MessagePage
|
||||||
|
from src.repositories import (
|
||||||
|
create_chat,
|
||||||
|
list_chats,
|
||||||
|
create_user as create_user_in_db,
|
||||||
|
get_user_by_id,
|
||||||
|
get_chat_by_id,
|
||||||
|
get_user_credentials_by_username,
|
||||||
|
create_message,
|
||||||
|
get_messages,
|
||||||
|
)
|
||||||
|
from src.security import verify_password
|
||||||
|
from src.sessions import (
|
||||||
|
create_session,
|
||||||
|
delete_session,
|
||||||
|
get_session_user_id,
|
||||||
|
ping_session_store,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
basic_security = HTTPBasic(auto_error=False)
|
||||||
|
bearer_security = HTTPBearer(auto_error=False)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_current_user(
|
||||||
|
credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security),
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
) -> User:
|
||||||
|
if credentials is None or credentials.scheme.lower() != "bearer":
|
||||||
|
raise APIError(
|
||||||
|
status_code=401,
|
||||||
|
code="unauthorized",
|
||||||
|
message="Unauthorized",
|
||||||
|
headers={
|
||||||
|
"WWW-Authenticate": "Bearer",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
user_id = await get_session_user_id(credentials.credentials)
|
||||||
|
|
||||||
|
if user_id is None:
|
||||||
|
raise APIError(
|
||||||
|
status_code=401,
|
||||||
|
code="unauthorized",
|
||||||
|
message="Unauthorized",
|
||||||
|
headers={
|
||||||
|
"WWW-Authenticate": "Bearer",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
user = await get_user_by_id(session, user_id)
|
||||||
|
|
||||||
|
if user is None:
|
||||||
|
raise APIError(
|
||||||
|
status_code=401,
|
||||||
|
code="unauthorized",
|
||||||
|
message="Unauthorized",
|
||||||
|
headers={
|
||||||
|
"WWW-Authenticate": "Bearer",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def request_middleware(request: Request, call_next):
|
||||||
|
request_id = str(uuid4())
|
||||||
|
request.state.request_id = request_id
|
||||||
|
|
||||||
|
if request.method in {"POST", "PUT", "PATCH"}:
|
||||||
|
content_type = request.headers.get("content-type")
|
||||||
|
|
||||||
|
if content_type is not None:
|
||||||
|
media_type = content_type.split(";", 1)[0].strip().lower()
|
||||||
|
|
||||||
|
if media_type != "application/json":
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=415,
|
||||||
|
content={
|
||||||
|
"error": {
|
||||||
|
"code": "unsupported_media_type",
|
||||||
|
"message": "Unsupported media type",
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
headers={
|
||||||
|
"X-Request-Id": request_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await call_next(request)
|
||||||
|
|
||||||
|
response.headers["X-Request-Id"] = request_id
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(RequestValidationError)
|
||||||
|
async def validation_exception_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: RequestValidationError,
|
||||||
|
):
|
||||||
|
request_id = request.state.request_id
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=400,
|
||||||
|
content={
|
||||||
|
"error": {
|
||||||
|
"code": "invalid_request",
|
||||||
|
"message": "Invalid request",
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(APIError)
|
||||||
|
async def api_error_handler(request: Request, exc: APIError):
|
||||||
|
request_id = request.state.request_id
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=exc.status_code,
|
||||||
|
content={
|
||||||
|
"error": {
|
||||||
|
"code": exc.code,
|
||||||
|
"message": exc.message,
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
headers=exc.headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(SQLAlchemyError)
|
||||||
|
async def database_exception_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: SQLAlchemyError,
|
||||||
|
):
|
||||||
|
request_id = request.state.request_id
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=503,
|
||||||
|
content={
|
||||||
|
"error": {
|
||||||
|
"code": "service_unavailable",
|
||||||
|
"message": "Database unavailable",
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(OSError)
|
||||||
|
async def database_connection_exception_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: OSError,
|
||||||
|
):
|
||||||
|
request_id = request.state.request_id
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=503,
|
||||||
|
content={
|
||||||
|
"error": {
|
||||||
|
"code": "service_unavailable",
|
||||||
|
"message": "Database unavailable",
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(StarletteHTTPException)
|
||||||
|
async def http_exception_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: StarletteHTTPException,
|
||||||
|
):
|
||||||
|
request_id = request.state.request_id
|
||||||
|
|
||||||
|
if exc.status_code == 404:
|
||||||
|
code = "not_found"
|
||||||
|
message = "Not found"
|
||||||
|
else:
|
||||||
|
code = "invalid_request"
|
||||||
|
message = "Invalid request"
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=exc.status_code,
|
||||||
|
content={
|
||||||
|
"error": {
|
||||||
|
"code": code,
|
||||||
|
"message": message,
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
headers=exc.headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def unexpected_exception_handler(
|
||||||
|
request: Request,
|
||||||
|
exc: Exception,
|
||||||
|
):
|
||||||
|
request_id = request.state.request_id
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=500,
|
||||||
|
content={
|
||||||
|
"error": {
|
||||||
|
"code": "internal_error",
|
||||||
|
"message": "Internal server error",
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/live")
|
||||||
|
async def health_live():
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/ready")
|
||||||
|
async def health_ready():
|
||||||
|
database_ready = await ping_database()
|
||||||
|
session_store_ready = await ping_session_store()
|
||||||
|
|
||||||
|
if not database_ready or not session_store_ready:
|
||||||
|
raise APIError(
|
||||||
|
status_code=503,
|
||||||
|
code="service_unavailable",
|
||||||
|
message="Service dependencies unavailable",
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/users", response_model=User, status_code=201)
|
||||||
|
async def create_user(
|
||||||
|
data: CreateUser,
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
async with session.begin():
|
||||||
|
return await create_user_in_db(session, data)
|
||||||
|
|
||||||
|
except IntegrityError as exc:
|
||||||
|
raise APIError(
|
||||||
|
status_code=409,
|
||||||
|
code="conflict",
|
||||||
|
message="Username already exists",
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/users/me", response_model=User)
|
||||||
|
async def get_me(
|
||||||
|
current_user: User = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
|
@app.post(
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
response_model=SessionResponse,
|
||||||
|
status_code=201,
|
||||||
|
)
|
||||||
|
async def create_auth_session(
|
||||||
|
credentials: HTTPBasicCredentials | None = Depends(basic_security),
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
):
|
||||||
|
if credentials is None:
|
||||||
|
raise APIError(
|
||||||
|
status_code=401,
|
||||||
|
code="unauthorized",
|
||||||
|
message="Unauthorized",
|
||||||
|
headers={
|
||||||
|
"WWW-Authenticate": 'Basic realm="messenger"',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
user_credentials = await get_user_credentials_by_username(
|
||||||
|
session,
|
||||||
|
credentials.username,
|
||||||
|
)
|
||||||
|
|
||||||
|
if user_credentials is None:
|
||||||
|
raise APIError(
|
||||||
|
status_code=401,
|
||||||
|
code="unauthorized",
|
||||||
|
message="Unauthorized",
|
||||||
|
headers={
|
||||||
|
"WWW-Authenticate": 'Basic realm="messenger"',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if not verify_password(
|
||||||
|
user_credentials.password_hash,
|
||||||
|
credentials.password,
|
||||||
|
):
|
||||||
|
raise APIError(
|
||||||
|
status_code=401,
|
||||||
|
code="unauthorized",
|
||||||
|
message="Unauthorized",
|
||||||
|
headers={
|
||||||
|
"WWW-Authenticate": 'Basic realm="messenger"',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
user = User(
|
||||||
|
id=user_credentials.id,
|
||||||
|
username=user_credentials.username,
|
||||||
|
display_name=user_credentials.display_name,
|
||||||
|
created_at=user_credentials.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
token, ttl = await create_session(user.id)
|
||||||
|
|
||||||
|
expires_at = datetime.now(timezone.utc) + timedelta(seconds=ttl)
|
||||||
|
|
||||||
|
return SessionResponse(
|
||||||
|
token=token,
|
||||||
|
token_type="Bearer",
|
||||||
|
expires_at=expires_at,
|
||||||
|
user=user,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.delete(
|
||||||
|
"/api/v1/auth/sessions/current",
|
||||||
|
status_code=204,
|
||||||
|
)
|
||||||
|
async def delete_current_session(
|
||||||
|
credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security),
|
||||||
|
current_user: User = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
await delete_session(credentials.credentials)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
response_model=Chat,
|
||||||
|
status_code=201,
|
||||||
|
)
|
||||||
|
async def create_chat_endpoint(
|
||||||
|
data: CreateChat,
|
||||||
|
current_user: User = Depends(get_current_user),
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
) -> Chat:
|
||||||
|
try:
|
||||||
|
chat = await create_chat(
|
||||||
|
session=session,
|
||||||
|
data=data,
|
||||||
|
current_user_id=current_user.id,
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
except Exception:
|
||||||
|
await session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
return chat
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/api/v1/chats",
|
||||||
|
response_model=ChatPage,
|
||||||
|
)
|
||||||
|
async def get_chats(
|
||||||
|
limit: int = 50,
|
||||||
|
current_user: User = Depends(get_current_user),
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
) -> ChatPage:
|
||||||
|
if limit < 1 or limit > 100:
|
||||||
|
raise APIError(
|
||||||
|
status_code=400,
|
||||||
|
code="invalid_request",
|
||||||
|
message="Limit must be between 1 and 100",
|
||||||
|
)
|
||||||
|
|
||||||
|
return await list_chats(
|
||||||
|
session=session,
|
||||||
|
current_user_id=current_user.id,
|
||||||
|
limit=limit,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/api/v1/chats/{chat_id}",
|
||||||
|
response_model=Chat,
|
||||||
|
)
|
||||||
|
async def get_chat_endpoint(
|
||||||
|
chat_id: UUID,
|
||||||
|
current_user: User = Depends(get_current_user),
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
) -> Chat:
|
||||||
|
chat = await get_chat_by_id(
|
||||||
|
session=session,
|
||||||
|
chat_id=chat_id,
|
||||||
|
current_user_id=current_user.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
if chat is None:
|
||||||
|
raise APIError(
|
||||||
|
status_code=404,
|
||||||
|
code="not_found",
|
||||||
|
message="Chat not found",
|
||||||
|
)
|
||||||
|
|
||||||
|
return chat
|
||||||
|
|
||||||
|
|
||||||
|
@app.post(
|
||||||
|
"/api/v1/chats/{chat_id}/messages",
|
||||||
|
response_model=Message,
|
||||||
|
status_code=201,
|
||||||
|
)
|
||||||
|
async def create_message_endpoint(
|
||||||
|
chat_id: UUID,
|
||||||
|
data: CreateMessage,
|
||||||
|
current_user: User = Depends(get_current_user),
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
) -> Message:
|
||||||
|
try:
|
||||||
|
message = await create_message(
|
||||||
|
session=session,
|
||||||
|
chat_id=chat_id,
|
||||||
|
current_user_id=current_user.id,
|
||||||
|
data=data,
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
except Exception:
|
||||||
|
await session.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/api/v1/chats/{chat_id}/messages",
|
||||||
|
response_model=MessagePage,
|
||||||
|
)
|
||||||
|
async def get_messages_endpoint(
|
||||||
|
chat_id: UUID,
|
||||||
|
limit: int = 50,
|
||||||
|
cursor: str | None = None,
|
||||||
|
current_user: User = Depends(get_current_user),
|
||||||
|
session: AsyncSession = Depends(get_db_session),
|
||||||
|
) -> MessagePage:
|
||||||
|
if limit < 1 or limit > 100:
|
||||||
|
raise APIError(
|
||||||
|
status_code=400,
|
||||||
|
code="invalid_request",
|
||||||
|
message="Limit must be between 1 and 100",
|
||||||
|
)
|
||||||
|
|
||||||
|
return await get_messages(
|
||||||
|
session=session,
|
||||||
|
chat_id=chat_id,
|
||||||
|
current_user_id=current_user.id,
|
||||||
|
limit=limit,
|
||||||
|
cursor=cursor,
|
||||||
|
)
|
||||||
+118
@@ -0,0 +1,118 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||||
|
|
||||||
|
|
||||||
|
class CreateUser(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
username: str = Field(
|
||||||
|
min_length=3,
|
||||||
|
max_length=32,
|
||||||
|
pattern=r"^[a-z0-9_]+$",
|
||||||
|
)
|
||||||
|
|
||||||
|
display_name: str = Field(
|
||||||
|
min_length=1,
|
||||||
|
max_length=100,
|
||||||
|
)
|
||||||
|
|
||||||
|
password: str = Field(
|
||||||
|
min_length=12,
|
||||||
|
max_length=128,
|
||||||
|
)
|
||||||
|
|
||||||
|
@field_validator("display_name")
|
||||||
|
@classmethod
|
||||||
|
def validate_display_name(cls, value: str) -> str:
|
||||||
|
if not value.strip():
|
||||||
|
raise ValueError("Display name must not be blank")
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class User(BaseModel):
|
||||||
|
id: UUID
|
||||||
|
username: str
|
||||||
|
display_name: str
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class SessionResponse(BaseModel):
|
||||||
|
token: str
|
||||||
|
token_type: str
|
||||||
|
expires_at: datetime
|
||||||
|
user: User
|
||||||
|
|
||||||
|
|
||||||
|
class CreateChat(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
title: str = Field(
|
||||||
|
min_length=1,
|
||||||
|
max_length=100,
|
||||||
|
)
|
||||||
|
|
||||||
|
member_ids: list[UUID] = Field(
|
||||||
|
min_length=1,
|
||||||
|
max_length=100,
|
||||||
|
)
|
||||||
|
|
||||||
|
@field_validator("title")
|
||||||
|
@classmethod
|
||||||
|
def validate_title(cls, value: str) -> str:
|
||||||
|
if not value.strip():
|
||||||
|
raise ValueError("Chat title must not be blank")
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
@field_validator("member_ids")
|
||||||
|
@classmethod
|
||||||
|
def validate_member_ids(cls, value: list[UUID]) -> list[UUID]:
|
||||||
|
if len(value) != len(set(value)):
|
||||||
|
raise ValueError("Member IDs must be unique")
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class Chat(BaseModel):
|
||||||
|
id: UUID
|
||||||
|
title: str
|
||||||
|
member_ids: list[UUID]
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class ChatPage(BaseModel):
|
||||||
|
items: list[Chat]
|
||||||
|
next_cursor: str | None
|
||||||
|
|
||||||
|
|
||||||
|
class CreateMessage(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
text: str = Field(
|
||||||
|
min_length=1,
|
||||||
|
max_length=4000,
|
||||||
|
)
|
||||||
|
|
||||||
|
@field_validator("text")
|
||||||
|
@classmethod
|
||||||
|
def validate_text(cls, value: str) -> str:
|
||||||
|
if not value.strip():
|
||||||
|
raise ValueError("Message text must not be blank")
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class Message(BaseModel):
|
||||||
|
id: UUID
|
||||||
|
chat_id: UUID
|
||||||
|
sender_id: UUID
|
||||||
|
text: str
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class MessagePage(BaseModel):
|
||||||
|
items: list[Message]
|
||||||
|
next_cursor: str | None
|
||||||
@@ -0,0 +1,535 @@
|
|||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from src.models import Chat, ChatPage, CreateChat, CreateUser, User, CreateMessage, Message, MessagePage
|
||||||
|
from src.security import hash_password
|
||||||
|
from src.errors import APIError
|
||||||
|
from src.cursor import decode_cursor, encode_cursor
|
||||||
|
|
||||||
|
|
||||||
|
async def create_user(
|
||||||
|
session: AsyncSession,
|
||||||
|
data: CreateUser,
|
||||||
|
) -> User:
|
||||||
|
user_id = uuid4()
|
||||||
|
password_hash = hash_password(data.password)
|
||||||
|
|
||||||
|
result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
INSERT INTO users (
|
||||||
|
id,
|
||||||
|
username,
|
||||||
|
display_name,
|
||||||
|
password_hash
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
:id,
|
||||||
|
:username,
|
||||||
|
:display_name,
|
||||||
|
:password_hash
|
||||||
|
)
|
||||||
|
RETURNING
|
||||||
|
id,
|
||||||
|
username,
|
||||||
|
display_name,
|
||||||
|
created_at
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"id": user_id,
|
||||||
|
"username": data.username,
|
||||||
|
"display_name": data.display_name,
|
||||||
|
"password_hash": password_hash,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
row = result.one()
|
||||||
|
|
||||||
|
return User(
|
||||||
|
id=UUID(str(row.id)),
|
||||||
|
username=row.username,
|
||||||
|
display_name=row.display_name,
|
||||||
|
created_at=row.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_user_credentials_by_username(
|
||||||
|
session: AsyncSession,
|
||||||
|
username: str,
|
||||||
|
):
|
||||||
|
result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
username,
|
||||||
|
display_name,
|
||||||
|
password_hash,
|
||||||
|
created_at
|
||||||
|
FROM users
|
||||||
|
WHERE username = :username
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"username": username,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return result.one_or_none()
|
||||||
|
|
||||||
|
|
||||||
|
async def get_user_by_id(
|
||||||
|
session: AsyncSession,
|
||||||
|
user_id: UUID,
|
||||||
|
) -> User | None:
|
||||||
|
result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
username,
|
||||||
|
display_name,
|
||||||
|
created_at
|
||||||
|
FROM users
|
||||||
|
WHERE id = :user_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
row = result.one_or_none()
|
||||||
|
|
||||||
|
if row is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return User(
|
||||||
|
id=UUID(str(row.id)),
|
||||||
|
username=row.username,
|
||||||
|
display_name=row.display_name,
|
||||||
|
created_at=row.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_chat(
|
||||||
|
session: AsyncSession,
|
||||||
|
data: CreateChat,
|
||||||
|
current_user_id: UUID,
|
||||||
|
) -> Chat:
|
||||||
|
if current_user_id not in data.member_ids:
|
||||||
|
raise APIError(
|
||||||
|
status_code=400,
|
||||||
|
code="invalid_request",
|
||||||
|
message="Current user must be a chat member",
|
||||||
|
)
|
||||||
|
|
||||||
|
users_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT id
|
||||||
|
FROM users
|
||||||
|
WHERE id = ANY(:member_ids)
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"member_ids": data.member_ids,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
existing_user_ids = {
|
||||||
|
UUID(str(row.id))
|
||||||
|
for row in users_result
|
||||||
|
}
|
||||||
|
|
||||||
|
if existing_user_ids != set(data.member_ids):
|
||||||
|
raise APIError(
|
||||||
|
status_code=400,
|
||||||
|
code="invalid_request",
|
||||||
|
message="Unknown chat member",
|
||||||
|
)
|
||||||
|
|
||||||
|
chat_id = uuid4()
|
||||||
|
|
||||||
|
chat_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
INSERT INTO chats (
|
||||||
|
id,
|
||||||
|
title
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
:id,
|
||||||
|
:title
|
||||||
|
)
|
||||||
|
RETURNING
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
created_at
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"id": chat_id,
|
||||||
|
"title": data.title,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
chat_row = chat_result.one()
|
||||||
|
|
||||||
|
for member_id in data.member_ids:
|
||||||
|
await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
INSERT INTO chat_members (
|
||||||
|
chat_id,
|
||||||
|
user_id
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
:chat_id,
|
||||||
|
:user_id
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"user_id": member_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return Chat(
|
||||||
|
id=UUID(str(chat_row.id)),
|
||||||
|
title=chat_row.title,
|
||||||
|
member_ids=data.member_ids,
|
||||||
|
created_at=chat_row.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_chat_by_id(
|
||||||
|
session: AsyncSession,
|
||||||
|
chat_id: UUID,
|
||||||
|
current_user_id: UUID,
|
||||||
|
) -> Chat | None:
|
||||||
|
chat_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
c.id,
|
||||||
|
c.title,
|
||||||
|
c.created_at
|
||||||
|
FROM chats AS c
|
||||||
|
JOIN chat_members AS cm
|
||||||
|
ON cm.chat_id = c.id
|
||||||
|
WHERE c.id = :chat_id
|
||||||
|
AND cm.user_id = :current_user_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"current_user_id": current_user_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
chat_row = chat_result.one_or_none()
|
||||||
|
|
||||||
|
if chat_row is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
members_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT user_id
|
||||||
|
FROM chat_members
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
ORDER BY user_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
member_ids = [
|
||||||
|
UUID(str(row.user_id))
|
||||||
|
for row in members_result
|
||||||
|
]
|
||||||
|
|
||||||
|
return Chat(
|
||||||
|
id=UUID(str(chat_row.id)),
|
||||||
|
title=chat_row.title,
|
||||||
|
member_ids=member_ids,
|
||||||
|
created_at=chat_row.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def list_chats(
|
||||||
|
session: AsyncSession,
|
||||||
|
current_user_id: UUID,
|
||||||
|
limit: int,
|
||||||
|
) -> ChatPage:
|
||||||
|
chats_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
c.id,
|
||||||
|
c.title,
|
||||||
|
c.created_at
|
||||||
|
FROM chats AS c
|
||||||
|
JOIN chat_members AS cm
|
||||||
|
ON cm.chat_id = c.id
|
||||||
|
WHERE cm.user_id = :current_user_id
|
||||||
|
ORDER BY c.created_at, c.id
|
||||||
|
LIMIT :limit
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"current_user_id": current_user_id,
|
||||||
|
"limit": limit,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
chat_rows = chats_result.all()
|
||||||
|
|
||||||
|
items = []
|
||||||
|
|
||||||
|
for chat_row in chat_rows:
|
||||||
|
members_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT user_id
|
||||||
|
FROM chat_members
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
ORDER BY user_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_row.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
member_ids = [
|
||||||
|
UUID(str(row.user_id))
|
||||||
|
for row in members_result
|
||||||
|
]
|
||||||
|
|
||||||
|
items.append(
|
||||||
|
Chat(
|
||||||
|
id=UUID(str(chat_row.id)),
|
||||||
|
title=chat_row.title,
|
||||||
|
member_ids=member_ids,
|
||||||
|
created_at=chat_row.created_at,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return ChatPage(
|
||||||
|
items=items,
|
||||||
|
next_cursor=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_message(
|
||||||
|
session: AsyncSession,
|
||||||
|
chat_id: UUID,
|
||||||
|
current_user_id: UUID,
|
||||||
|
data: CreateMessage,
|
||||||
|
) -> Message:
|
||||||
|
membership_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT 1
|
||||||
|
FROM chat_members
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
AND user_id = :current_user_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"current_user_id": current_user_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if membership_result.scalar_one_or_none() is None:
|
||||||
|
raise APIError(
|
||||||
|
status_code=404,
|
||||||
|
code="not_found",
|
||||||
|
message="Chat not found",
|
||||||
|
)
|
||||||
|
|
||||||
|
message_id = uuid4()
|
||||||
|
|
||||||
|
message_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
INSERT INTO messages (
|
||||||
|
id,
|
||||||
|
chat_id,
|
||||||
|
sender_id,
|
||||||
|
text
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
:id,
|
||||||
|
:chat_id,
|
||||||
|
:sender_id,
|
||||||
|
:text
|
||||||
|
)
|
||||||
|
RETURNING
|
||||||
|
id,
|
||||||
|
chat_id,
|
||||||
|
sender_id,
|
||||||
|
text,
|
||||||
|
created_at
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"id": message_id,
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"sender_id": current_user_id,
|
||||||
|
"text": data.text,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
row = message_result.one()
|
||||||
|
|
||||||
|
return Message(
|
||||||
|
id=UUID(str(row.id)),
|
||||||
|
chat_id=UUID(str(row.chat_id)),
|
||||||
|
sender_id=UUID(str(row.sender_id)),
|
||||||
|
text=row.text,
|
||||||
|
created_at=row.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_messages(
|
||||||
|
session: AsyncSession,
|
||||||
|
chat_id: UUID,
|
||||||
|
current_user_id: UUID,
|
||||||
|
limit: int,
|
||||||
|
cursor: str | None,
|
||||||
|
) -> MessagePage:
|
||||||
|
membership_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT 1
|
||||||
|
FROM chat_members
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
AND user_id = :current_user_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"current_user_id": current_user_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if membership_result.scalar_one_or_none() is None:
|
||||||
|
raise APIError(
|
||||||
|
status_code=404,
|
||||||
|
code="not_found",
|
||||||
|
message="Chat not found",
|
||||||
|
)
|
||||||
|
|
||||||
|
cursor_created_at = None
|
||||||
|
cursor_message_id = None
|
||||||
|
|
||||||
|
if cursor is not None:
|
||||||
|
try:
|
||||||
|
(
|
||||||
|
cursor_chat_id,
|
||||||
|
cursor_created_at,
|
||||||
|
cursor_message_id,
|
||||||
|
) = decode_cursor(cursor)
|
||||||
|
except ValueError:
|
||||||
|
raise APIError(
|
||||||
|
status_code=400,
|
||||||
|
code="invalid_request",
|
||||||
|
message="Invalid cursor",
|
||||||
|
)
|
||||||
|
|
||||||
|
if cursor_chat_id != chat_id:
|
||||||
|
raise APIError(
|
||||||
|
status_code=400,
|
||||||
|
code="invalid_request",
|
||||||
|
message="Cursor belongs to another chat",
|
||||||
|
)
|
||||||
|
|
||||||
|
if cursor is None:
|
||||||
|
messages_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
chat_id,
|
||||||
|
sender_id,
|
||||||
|
text,
|
||||||
|
created_at
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
ORDER BY created_at, id
|
||||||
|
LIMIT :fetch_limit
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"fetch_limit": limit + 1,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
messages_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
chat_id,
|
||||||
|
sender_id,
|
||||||
|
text,
|
||||||
|
created_at
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
AND (created_at, id) > (
|
||||||
|
:cursor_created_at,
|
||||||
|
:cursor_message_id
|
||||||
|
)
|
||||||
|
ORDER BY created_at, id
|
||||||
|
LIMIT :fetch_limit
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"cursor_created_at": cursor_created_at,
|
||||||
|
"cursor_message_id": cursor_message_id,
|
||||||
|
"fetch_limit": limit + 1,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
rows = messages_result.all()
|
||||||
|
|
||||||
|
has_more = len(rows) > limit
|
||||||
|
rows = rows[:limit]
|
||||||
|
|
||||||
|
items = [
|
||||||
|
Message(
|
||||||
|
id=UUID(str(row.id)),
|
||||||
|
chat_id=UUID(str(row.chat_id)),
|
||||||
|
sender_id=UUID(str(row.sender_id)),
|
||||||
|
text=row.text,
|
||||||
|
created_at=row.created_at,
|
||||||
|
)
|
||||||
|
for row in rows
|
||||||
|
]
|
||||||
|
|
||||||
|
next_cursor = None
|
||||||
|
|
||||||
|
if has_more:
|
||||||
|
last_message = items[-1]
|
||||||
|
|
||||||
|
next_cursor = encode_cursor(
|
||||||
|
chat_id=chat_id,
|
||||||
|
created_at=last_message.created_at,
|
||||||
|
message_id=last_message.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
return MessagePage(
|
||||||
|
items=items,
|
||||||
|
next_cursor=next_cursor,
|
||||||
|
)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
from argon2 import PasswordHasher
|
||||||
|
from argon2.exceptions import VerifyMismatchError
|
||||||
|
|
||||||
|
|
||||||
|
password_hasher = PasswordHasher()
|
||||||
|
|
||||||
|
|
||||||
|
def hash_password(password: str) -> str:
|
||||||
|
return password_hasher.hash(password)
|
||||||
|
|
||||||
|
|
||||||
|
def verify_password(password_hash: str, password: str) -> bool:
|
||||||
|
try:
|
||||||
|
return password_hasher.verify(password_hash, password)
|
||||||
|
except VerifyMismatchError:
|
||||||
|
return False
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import os
|
||||||
|
import secrets
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from redis.asyncio import Redis
|
||||||
|
from redis.exceptions import RedisError
|
||||||
|
|
||||||
|
from src.errors import APIError
|
||||||
|
|
||||||
|
|
||||||
|
CACHE_URL_FILE = "/run/secrets/cache_url"
|
||||||
|
DEFAULT_SESSION_TTL_SECONDS = 3600
|
||||||
|
|
||||||
|
|
||||||
|
def read_cache_url() -> str:
|
||||||
|
with open(CACHE_URL_FILE, encoding="utf-8") as file:
|
||||||
|
return file.read().strip()
|
||||||
|
|
||||||
|
|
||||||
|
def get_session_ttl_seconds() -> int:
|
||||||
|
return int(
|
||||||
|
os.getenv(
|
||||||
|
"SESSION_TTL_SECONDS",
|
||||||
|
str(DEFAULT_SESSION_TTL_SECONDS),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
redis_client = Redis.from_url(
|
||||||
|
read_cache_url(),
|
||||||
|
decode_responses=True,
|
||||||
|
socket_connect_timeout=5,
|
||||||
|
socket_timeout=5,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def session_store_unavailable() -> APIError:
|
||||||
|
return APIError(
|
||||||
|
status_code=503,
|
||||||
|
code="service_unavailable",
|
||||||
|
message="Session store unavailable",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def create_session(user_id: UUID) -> tuple[str, int]:
|
||||||
|
token = secrets.token_urlsafe(32)
|
||||||
|
ttl = get_session_ttl_seconds()
|
||||||
|
|
||||||
|
try:
|
||||||
|
await redis_client.set(
|
||||||
|
f"session:{token}",
|
||||||
|
str(user_id),
|
||||||
|
ex=ttl,
|
||||||
|
)
|
||||||
|
except RedisError as exc:
|
||||||
|
raise session_store_unavailable() from exc
|
||||||
|
|
||||||
|
return token, ttl
|
||||||
|
|
||||||
|
|
||||||
|
async def get_session_user_id(token: str) -> UUID | None:
|
||||||
|
try:
|
||||||
|
value = await redis_client.get(f"session:{token}")
|
||||||
|
except RedisError as exc:
|
||||||
|
raise session_store_unavailable() from exc
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
return UUID(value)
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def delete_session(token: str) -> None:
|
||||||
|
try:
|
||||||
|
await redis_client.delete(f"session:{token}")
|
||||||
|
except RedisError as exc:
|
||||||
|
raise session_store_unavailable() from exc
|
||||||
|
|
||||||
|
|
||||||
|
async def ping_session_store() -> bool:
|
||||||
|
try:
|
||||||
|
return bool(await redis_client.ping())
|
||||||
|
except RedisError:
|
||||||
|
return False
|
||||||
@@ -0,0 +1,176 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
BASE_URL = "http://localhost:8080"
|
||||||
|
PASSWORD = "correct-password-123"
|
||||||
|
|
||||||
|
|
||||||
|
def call(
|
||||||
|
method: str,
|
||||||
|
path: str,
|
||||||
|
body: dict | None = None,
|
||||||
|
headers: dict[str, str] | None = None,
|
||||||
|
) -> tuple[int, dict | None, dict]:
|
||||||
|
request_headers = dict(headers or {})
|
||||||
|
|
||||||
|
data = None
|
||||||
|
if body is not None:
|
||||||
|
data = json.dumps(body).encode("utf-8")
|
||||||
|
request_headers["Content-Type"] = "application/json"
|
||||||
|
|
||||||
|
request = urllib.request.Request(
|
||||||
|
BASE_URL + path,
|
||||||
|
data=data,
|
||||||
|
headers=request_headers,
|
||||||
|
method=method,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(request, timeout=10) as response:
|
||||||
|
raw = response.read()
|
||||||
|
response_body = json.loads(raw) if raw else None
|
||||||
|
return response.status, response_body, dict(response.headers)
|
||||||
|
except urllib.error.HTTPError as error:
|
||||||
|
raw = error.read()
|
||||||
|
response_body = json.loads(raw) if raw else None
|
||||||
|
return error.code, response_body, dict(error.headers)
|
||||||
|
|
||||||
|
|
||||||
|
def basic_header(username: str, password: str) -> str:
|
||||||
|
credentials = f"{username}:{password}".encode("utf-8")
|
||||||
|
encoded = base64.b64encode(credentials).decode("ascii")
|
||||||
|
return f"Basic {encoded}"
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
username = "auth_" + uuid.uuid4().hex[:16]
|
||||||
|
|
||||||
|
# 1. Создаём отдельного пользователя для этого теста.
|
||||||
|
status, body, _ = call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/users",
|
||||||
|
{
|
||||||
|
"username": username,
|
||||||
|
"display_name": "Auth Test User",
|
||||||
|
"password": PASSWORD,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if status != 201:
|
||||||
|
raise AssertionError(
|
||||||
|
f"registration: expected 201, got {status}: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 2. Неверный пароль не должен создавать сессию.
|
||||||
|
status, body, headers = call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
headers={
|
||||||
|
"Authorization": basic_header(
|
||||||
|
username,
|
||||||
|
"definitely-wrong-password",
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if status != 401:
|
||||||
|
raise AssertionError(
|
||||||
|
f"wrong password: expected 401, got {status}: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if body is None or body.get("error", {}).get("code") != "unauthorized":
|
||||||
|
raise AssertionError(
|
||||||
|
f"wrong password: unexpected response body: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 3. С правильным паролем получаем Bearer-сессию.
|
||||||
|
status, body, _ = call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
headers={
|
||||||
|
"Authorization": basic_header(username, PASSWORD)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if status != 201:
|
||||||
|
raise AssertionError(
|
||||||
|
f"login: expected 201, got {status}: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if body is None:
|
||||||
|
raise AssertionError("login: empty response body")
|
||||||
|
|
||||||
|
token = body.get("token")
|
||||||
|
|
||||||
|
if not token:
|
||||||
|
raise AssertionError("login: token is missing")
|
||||||
|
|
||||||
|
if body.get("token_type") != "Bearer":
|
||||||
|
raise AssertionError(
|
||||||
|
f"login: expected token_type Bearer, got {body.get('token_type')}"
|
||||||
|
)
|
||||||
|
|
||||||
|
bearer = {"Authorization": f"Bearer {token}"}
|
||||||
|
|
||||||
|
# 4. До logout токен обязан работать.
|
||||||
|
status, body, _ = call(
|
||||||
|
"GET",
|
||||||
|
"/api/v1/users/me",
|
||||||
|
headers=bearer,
|
||||||
|
)
|
||||||
|
|
||||||
|
if status != 200:
|
||||||
|
raise AssertionError(
|
||||||
|
f"token before logout: expected 200, got {status}: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 5. Отзываем текущую сессию.
|
||||||
|
status, body, _ = call(
|
||||||
|
"DELETE",
|
||||||
|
"/api/v1/auth/sessions/current",
|
||||||
|
headers=bearer,
|
||||||
|
)
|
||||||
|
|
||||||
|
if status != 204:
|
||||||
|
raise AssertionError(
|
||||||
|
f"logout: expected 204, got {status}: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 6. Тот же токен после logout больше не принимается.
|
||||||
|
status, body, _ = call(
|
||||||
|
"GET",
|
||||||
|
"/api/v1/users/me",
|
||||||
|
headers=bearer,
|
||||||
|
)
|
||||||
|
|
||||||
|
if status != 401:
|
||||||
|
raise AssertionError(
|
||||||
|
f"token after logout: expected 401, got {status}: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 7. Повторный logout с уже отозванным токеном тоже не авторизован.
|
||||||
|
status, body, _ = call(
|
||||||
|
"DELETE",
|
||||||
|
"/api/v1/auth/sessions/current",
|
||||||
|
headers=bearer,
|
||||||
|
)
|
||||||
|
|
||||||
|
if status != 401:
|
||||||
|
raise AssertionError(
|
||||||
|
f"repeated logout: expected 401, got {status}: {body}"
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
"PASS: wrong password returned 401; "
|
||||||
|
"valid session worked; logout revoked the token."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import json
|
||||||
|
import threading
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
BASE_URL = "http://localhost:8080"
|
||||||
|
|
||||||
|
|
||||||
|
def register(username: str, barrier: threading.Barrier) -> tuple[int, dict]:
|
||||||
|
body = json.dumps(
|
||||||
|
{
|
||||||
|
"username": username,
|
||||||
|
"display_name": "Race User",
|
||||||
|
"password": "correct-password-123",
|
||||||
|
}
|
||||||
|
).encode("utf-8")
|
||||||
|
|
||||||
|
request = urllib.request.Request(
|
||||||
|
BASE_URL + "/api/v1/users",
|
||||||
|
data=body,
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
method="POST",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Оба потока доходят сюда и только потом одновременно продолжают.
|
||||||
|
barrier.wait()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(request, timeout=10) as response:
|
||||||
|
return response.status, json.loads(response.read())
|
||||||
|
except urllib.error.HTTPError as error:
|
||||||
|
return error.code, json.loads(error.read())
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
username = "race_" + uuid.uuid4().hex[:16]
|
||||||
|
barrier = threading.Barrier(2)
|
||||||
|
results: list[tuple[int, dict] | None] = [None, None]
|
||||||
|
|
||||||
|
def worker(index: int) -> None:
|
||||||
|
results[index] = register(username, barrier)
|
||||||
|
|
||||||
|
threads = [
|
||||||
|
threading.Thread(target=worker, args=(0,)),
|
||||||
|
threading.Thread(target=worker, args=(1,)),
|
||||||
|
]
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
|
||||||
|
statuses = sorted(result[0] for result in results if result is not None)
|
||||||
|
|
||||||
|
if statuses != [201, 409]:
|
||||||
|
raise AssertionError(
|
||||||
|
f"expected exactly one 201 and one 409, got {statuses}"
|
||||||
|
)
|
||||||
|
|
||||||
|
created = [
|
||||||
|
result
|
||||||
|
for result in results
|
||||||
|
if result is not None and result[0] == 201
|
||||||
|
]
|
||||||
|
|
||||||
|
conflicts = [
|
||||||
|
result
|
||||||
|
for result in results
|
||||||
|
if result is not None and result[0] == 409
|
||||||
|
]
|
||||||
|
|
||||||
|
if len(created) != 1 or created[0][1]["username"] != username:
|
||||||
|
raise AssertionError("unexpected successful registration response")
|
||||||
|
|
||||||
|
if (
|
||||||
|
len(conflicts) != 1
|
||||||
|
or conflicts[0][1]["error"]["code"] != "conflict"
|
||||||
|
):
|
||||||
|
raise AssertionError("unexpected conflict response")
|
||||||
|
|
||||||
|
print(
|
||||||
|
"PASS: concurrent registration produced exactly "
|
||||||
|
"one 201 and one 409."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
|
from src.database import SessionFactory
|
||||||
|
|
||||||
|
|
||||||
|
async def main() -> None:
|
||||||
|
chat_id = uuid4()
|
||||||
|
|
||||||
|
async with SessionFactory() as session:
|
||||||
|
# Берём реального существующего пользователя.
|
||||||
|
result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT id
|
||||||
|
FROM users
|
||||||
|
ORDER BY created_at
|
||||||
|
LIMIT 1
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
user_id = result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if user_id is None:
|
||||||
|
raise AssertionError("test requires at least one existing user")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Шаг 1: успешно создаём чат.
|
||||||
|
await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
INSERT INTO chats (id, title)
|
||||||
|
VALUES (:chat_id, :title)
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"title": "Rollback automated test",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Шаг 2: успешно добавляем реального участника.
|
||||||
|
await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
INSERT INTO chat_members (chat_id, user_id)
|
||||||
|
VALUES (:chat_id, :user_id)
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Шаг 3: намеренно нарушаем внешний ключ.
|
||||||
|
await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
INSERT INTO chat_members (chat_id, user_id)
|
||||||
|
VALUES (:chat_id, :user_id)
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"user_id": uuid4(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
raise AssertionError("expected foreign key violation")
|
||||||
|
|
||||||
|
except IntegrityError:
|
||||||
|
await session.rollback()
|
||||||
|
|
||||||
|
# После rollback начинаем новую транзакцию и проверяем БД.
|
||||||
|
chat_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM chats
|
||||||
|
WHERE id = :chat_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{"chat_id": chat_id},
|
||||||
|
)
|
||||||
|
|
||||||
|
chat_count = chat_result.scalar_one()
|
||||||
|
|
||||||
|
member_result = await session.execute(
|
||||||
|
text(
|
||||||
|
"""
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM chat_members
|
||||||
|
WHERE chat_id = :chat_id
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
{"chat_id": chat_id},
|
||||||
|
)
|
||||||
|
|
||||||
|
member_count = member_result.scalar_one()
|
||||||
|
|
||||||
|
if chat_count != 0:
|
||||||
|
raise AssertionError(
|
||||||
|
f"chat survived rollback: count={chat_count}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if member_count != 0:
|
||||||
|
raise AssertionError(
|
||||||
|
f"chat members survived rollback: count={member_count}"
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
"PASS: failed compound transaction rolled back "
|
||||||
|
"chat and membership inserts."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
BASE_URL = "http://localhost:8080"
|
||||||
|
PASSWORD = "correct-password-123"
|
||||||
|
|
||||||
|
|
||||||
|
def call(method, path, body=None, headers=None):
|
||||||
|
request_headers = dict(headers or {})
|
||||||
|
data = None
|
||||||
|
|
||||||
|
if body is not None:
|
||||||
|
data = json.dumps(body).encode("utf-8")
|
||||||
|
request_headers["Content-Type"] = "application/json"
|
||||||
|
|
||||||
|
request = urllib.request.Request(
|
||||||
|
BASE_URL + path,
|
||||||
|
data=data,
|
||||||
|
headers=request_headers,
|
||||||
|
method=method,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(request, timeout=10) as response:
|
||||||
|
raw = response.read()
|
||||||
|
return response.status, json.loads(raw) if raw else None
|
||||||
|
except urllib.error.HTTPError as error:
|
||||||
|
raw = error.read()
|
||||||
|
return error.code, json.loads(raw) if raw else None
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
username = "ttl_" + uuid.uuid4().hex[:16]
|
||||||
|
|
||||||
|
status, body = call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/users",
|
||||||
|
{
|
||||||
|
"username": username,
|
||||||
|
"display_name": "TTL Test User",
|
||||||
|
"password": PASSWORD,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert status == 201, f"registration: {status}, {body}"
|
||||||
|
|
||||||
|
credentials = base64.b64encode(
|
||||||
|
f"{username}:{PASSWORD}".encode("utf-8")
|
||||||
|
).decode("ascii")
|
||||||
|
|
||||||
|
status, body = call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
headers={"Authorization": f"Basic {credentials}"},
|
||||||
|
)
|
||||||
|
assert status == 201, f"login: {status}, {body}"
|
||||||
|
|
||||||
|
token = body["token"]
|
||||||
|
bearer = {"Authorization": f"Bearer {token}"}
|
||||||
|
|
||||||
|
status, body = call("GET", "/api/v1/users/me", headers=bearer)
|
||||||
|
assert status == 200, f"before expiry: {status}, {body}"
|
||||||
|
|
||||||
|
print("PASS: token works immediately after login.")
|
||||||
|
|
||||||
|
time.sleep(4)
|
||||||
|
|
||||||
|
status, body = call("GET", "/api/v1/users/me", headers=bearer)
|
||||||
|
assert status == 401, f"after expiry: {status}, {body}"
|
||||||
|
|
||||||
|
print("PASS: expired token returned 401.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user