From 3e152d41754d4e443e5080bec29654711d1554be Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:02:07 +0300 Subject: [PATCH 01/12] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=20lab1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + app/__init__.py | 1 + app/errors.py | 16 +++ app/handlers.py | 159 ++++++++++++++++++++++++++ app/main.py | 99 ++++++++++++++++ app/store.py | 88 +++++++++++++++ app/validation.py | 122 ++++++++++++++++++++ tests/test_unit.py | 276 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 762 insertions(+) create mode 100644 app/__init__.py create mode 100644 app/errors.py create mode 100644 app/handlers.py create mode 100644 app/main.py create mode 100644 app/store.py create mode 100644 app/validation.py create mode 100644 tests/test_unit.py diff --git a/.gitignore b/.gitignore index b5a71896..9a81deb9 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ coverage/ .vscode/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. +.aider* diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..7083a40c --- /dev/null +++ b/app/__init__.py @@ -0,0 +1 @@ +"""MTUSI Messenger — лабораторная 1 (in-memory).""" diff --git a/app/errors.py b/app/errors.py new file mode 100644 index 00000000..3ea7b2ca --- /dev/null +++ b/app/errors.py @@ -0,0 +1,16 @@ +"""Единый формат ошибок API и исключение ApiError.""" +from typing import Any + + +class ApiError(Exception): + """Ошибка, которую нужно вернуть клиенту в формате контракта.""" + + def __init__(self, status: int, code: str, message: str) -> None: + super().__init__(message) + self.status = status + self.code = code + self.message = message + + +def error_body(code: str, message: str, request_id: str) -> dict[str, Any]: + return {"error": {"code": code, "message": message, "request_id": request_id}} diff --git a/app/handlers.py b/app/handlers.py new file mode 100644 index 00000000..c3d55699 --- /dev/null +++ b/app/handlers.py @@ -0,0 +1,159 @@ +"""HTTP-обработчики FastAPI для lab1.""" +from typing import Any + +from fastapi import APIRouter, Body, Header, Query, Request +from fastapi.responses import JSONResponse + +from .errors import ApiError +from .store import Store +from .validation import ( + encode_cursor, + parse_cursor, + parse_limit, + validate_chat_id, + validate_create_chat, + validate_create_message, + validate_create_user, +) + +router = APIRouter() + + +def get_store(request: Request) -> Store: + return request.app.state.store + + +def current_user_id(request: Request, x_user_id: str | None) -> str: + """lab1: X-User-Id — идентификатор существующего пользователя.""" + if not x_user_id: + raise ApiError(401, "unauthorized", "Требуется X-User-Id") + store = get_store(request) + if store.get_user(x_user_id) is None: + raise ApiError(401, "unauthorized", "Неизвестный пользователь") + return x_user_id + + +@router.get("/health/live") +def health_live() -> dict[str, str]: + return {"status": "ok"} + + +@router.get("/health/ready") +def health_ready() -> dict[str, str]: + return {"status": "ok"} + + +@router.post("/api/v1/users", status_code=201) +def register_user(request: Request, body: Any = Body(...)) -> JSONResponse: + username, display_name = validate_create_user(body) + store = get_store(request) + user = store.create_user(username, display_name) + if user is None: + raise ApiError(409, "conflict", "username уже занят") + return JSONResponse(user, status_code=201) + + +@router.get("/api/v1/users/me") +def current_user(request: Request, x_user_id: str | None = Header(default=None)) -> dict[str, Any]: + user_id = current_user_id(request, x_user_id) + user = get_store(request).get_user(user_id) + assert user is not None + return user + + +@router.post("/api/v1/chats", status_code=201) +def create_chat( + request: Request, + body: Any = Body(...), + x_user_id: str | None = Header(default=None), +) -> JSONResponse: + caller = current_user_id(request, x_user_id) + title, member_ids = validate_create_chat(body, caller) + store = get_store(request) + for mid in member_ids: + if store.get_user(mid) is None: + raise ApiError(400, "invalid_request", "Неизвестный участник чата") + chat = store.create_chat(title, member_ids) + return JSONResponse(chat, status_code=201) + + +@router.get("/api/v1/chats") +def list_chats( + request: Request, + limit: str | None = Query(default=None), + cursor: str | None = Query(default=None), + x_user_id: str | None = Header(default=None), +) -> dict[str, Any]: + caller = current_user_id(request, x_user_id) + page_size = parse_limit(limit) + scope = f"chats:{caller}" + after = parse_cursor(cursor, scope) + items = get_store(request).list_chats_for(caller) + if after is not None: + items = [c for c in items if (c["created_at"], c["id"]) > after] + page = items[:page_size] + next_cursor = None + if len(items) > page_size and page: + last = page[-1] + next_cursor = encode_cursor(last["created_at"], last["id"], scope) + return {"items": page, "next_cursor": next_cursor} + + +@router.get("/api/v1/chats/{chat_id}") +def get_chat( + chat_id: str, + request: Request, + x_user_id: str | None = Header(default=None), +) -> dict[str, Any]: + caller = current_user_id(request, x_user_id) + chat_id = validate_chat_id(chat_id) + chat = get_store(request).get_chat(chat_id) + if chat is None or caller not in chat["member_ids"]: + raise ApiError(404, "not_found", "Чат не найден") + return chat + + +@router.post("/api/v1/chats/{chat_id}/messages", status_code=201) +def create_message( + chat_id: str, + request: Request, + body: Any = Body(...), + x_user_id: str | None = Header(default=None), +) -> JSONResponse: + caller = current_user_id(request, x_user_id) + chat_id = validate_chat_id(chat_id) + store = get_store(request) + chat = store.get_chat(chat_id) + if chat is None or caller not in chat["member_ids"]: + raise ApiError(404, "not_found", "Чат не найден") + text = validate_create_message(body) + message = store.add_message(chat_id, caller, text) + return JSONResponse(message, status_code=201) + + +@router.get("/api/v1/chats/{chat_id}/messages") +def list_messages( + chat_id: str, + request: Request, + limit: str | None = Query(default=None), + cursor: str | None = Query(default=None), + x_user_id: str | None = Header(default=None), +) -> dict[str, Any]: + caller = current_user_id(request, x_user_id) + chat_id = validate_chat_id(chat_id) + store = get_store(request) + chat = store.get_chat(chat_id) + if chat is None or caller not in chat["member_ids"]: + raise ApiError(404, "not_found", "Чат не найден") + page_size = parse_limit(limit) + scope = f"messages:{chat_id}" + after = parse_cursor(cursor, scope) + items = store.list_messages(chat_id) + if after is not None: + items = [m for m in items if (m["created_at"], m["id"]) > after] + page = items[:page_size] + next_cursor = None + if len(items) > page_size and page: + last = page[-1] + next_cursor = encode_cursor(last["created_at"], last["id"], scope) + return {"items": page, "next_cursor": next_cursor} diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..39dc81bc --- /dev/null +++ b/app/main.py @@ -0,0 +1,99 @@ +"""Точка входа: FastAPI-приложение, middleware и обработка ошибок.""" +import logging +import os +import uuid + +from fastapi import FastAPI, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from starlette.exceptions import HTTPException as StarletteHTTPException + +from .errors import ApiError, error_body +from .handlers import router +from .store import Store + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(levelname)s %(name)s %(message)s", +) +logger = logging.getLogger("messenger") + +app = FastAPI(title="MTUSI Messenger", version="1.1.0") +app.state.store = Store() +app.include_router(router) + + +@app.middleware("http") +async def request_id_middleware(request: Request, call_next): + request_id = request.headers.get("X-Request-Id") or uuid.uuid4().hex + request.state.request_id = request_id + try: + response = await call_next(request) + except ApiError as exc: + response = JSONResponse( + error_body(exc.code, exc.message, request_id), + status_code=exc.status, + ) + response.headers["X-Request-Id"] = request_id + return response + + +@app.exception_handler(ApiError) +async def api_error_handler(request: Request, exc: ApiError) -> JSONResponse: + request_id = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse( + error_body(exc.code, exc.message, request_id), + status_code=exc.status, + headers={"X-Request-Id": request_id}, + ) + + +@app.exception_handler(RequestValidationError) +async def validation_error_handler(request: Request, exc: RequestValidationError) -> JSONResponse: + request_id = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse( + error_body("invalid_request", "Некорректный запрос", request_id), + status_code=400, + headers={"X-Request-Id": request_id}, + ) + + +@app.exception_handler(StarletteHTTPException) +async def http_error_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse: + """Ошибки разбора тела (невалидный JSON/байты) FastAPI отдаёт как HTTPException 400.""" + request_id = getattr(request.state, "request_id", uuid.uuid4().hex) + if exc.status_code == 400: + return JSONResponse( + error_body("invalid_request", "Некорректный запрос", request_id), + status_code=400, + headers={"X-Request-Id": request_id}, + ) + return JSONResponse( + error_body("not_found" if exc.status_code == 404 else "internal_error", + str(exc.detail), request_id), + status_code=exc.status_code, + headers={"X-Request-Id": request_id}, + ) + + +@app.exception_handler(Exception) +async def unhandled_error_handler(request: Request, exc: Exception) -> JSONResponse: + request_id = getattr(request.state, "request_id", uuid.uuid4().hex) + logger.exception("unhandled error request_id=%s", request_id) + return JSONResponse( + error_body("internal_error", "Внутренняя ошибка", request_id), + status_code=500, + headers={"X-Request-Id": request_id}, + ) + + +def main() -> None: + import uvicorn + + host = os.environ.get("APP_HOST", "0.0.0.0") + port = int(os.environ.get("APP_PORT", "8080")) + uvicorn.run(app, host=host, port=port, log_level="info") + + +if __name__ == "__main__": + main() diff --git a/app/store.py b/app/store.py new file mode 100644 index 00000000..0e0e00b5 --- /dev/null +++ b/app/store.py @@ -0,0 +1,88 @@ +"""Потокобезопасное in-memory хранилище пользователей, чатов и сообщений.""" +import threading +import uuid +from datetime import datetime, timezone +from typing import Any + + +def utc_now() -> str: + """RFC 3339 UTC с суффиксом Z, секунды — как в примерах контракта.""" + return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + +def new_id() -> str: + return str(uuid.uuid4()) + + +class Store: + """Все данные живут в памяти одного процесса и исчезают при рестарте.""" + + def __init__(self) -> None: + self._lock = threading.RLock() + self.users: dict[str, dict[str, Any]] = {} + self.usernames: dict[str, str] = {} + self.chats: dict[str, dict[str, Any]] = {} + self.messages: dict[str, list[dict[str, Any]]] = {} + + # --- пользователи ----------------------------------------------------- + def create_user(self, username: str, display_name: str) -> dict[str, Any]: + with self._lock: + if username in self.usernames: + return None # сигнал конфликта + user = { + "id": new_id(), + "username": username, + "display_name": display_name, + "created_at": utc_now(), + } + self.users[user["id"]] = user + self.usernames[username] = user["id"] + return dict(user) + + def get_user(self, user_id: str) -> dict[str, Any] | None: + with self._lock: + user = self.users.get(user_id) + return dict(user) if user else None + + # --- чаты ------------------------------------------------------------- + def create_chat(self, title: str, member_ids: list[str]) -> dict[str, Any]: + with self._lock: + chat = { + "id": new_id(), + "title": title, + "member_ids": list(member_ids), + "created_at": utc_now(), + } + self.chats[chat["id"]] = chat + self.messages[chat["id"]] = [] + return dict(chat) + + def get_chat(self, chat_id: str) -> dict[str, Any] | None: + with self._lock: + chat = self.chats.get(chat_id) + return dict(chat) if chat else None + + def list_chats_for(self, user_id: str) -> list[dict[str, Any]]: + with self._lock: + items = [dict(c) for c in self.chats.values() if user_id in c["member_ids"]] + items.sort(key=lambda c: (c["created_at"], c["id"])) + return items + + # --- сообщения -------------------------------------------------------- + def add_message(self, chat_id: str, sender_id: str, text: str) -> dict[str, Any]: + with self._lock: + message = { + "id": new_id(), + "chat_id": chat_id, + "sender_id": sender_id, + "text": text, + "created_at": utc_now(), + } + self.messages.setdefault(chat_id, []).append(message) + return dict(message) + + def list_messages(self, chat_id: str) -> list[dict[str, Any]]: + with self._lock: + items = [dict(m) for m in self.messages.get(chat_id, [])] + items.sort(key=lambda m: (m["created_at"], m["id"])) + return items diff --git a/app/validation.py b/app/validation.py new file mode 100644 index 00000000..5ff9ff12 --- /dev/null +++ b/app/validation.py @@ -0,0 +1,122 @@ +"""Валидация тел запросов и параметров вручную, без pydantic-моделей контракта.""" +import base64 +import re +import uuid +from typing import Any + +from .errors import ApiError + +USERNAME_RE = re.compile(r"\A[a-z0-9_]{3,32}\Z") + +# Уникальный идентификатор жизни процесса: cursors, выданные до рестарта, +# становятся недействительными. +CURSOR_EPOCH = uuid.uuid4().hex + + +def _is_blank(value: str) -> bool: + return value.strip() == "" + + +def require_object(body: Any) -> dict[str, Any]: + if not isinstance(body, dict): + raise ApiError(400, "invalid_request", "Тело запроса должно быть JSON-объектом") + return body + + +def reject_extra(body: dict[str, Any], allowed: set[str]) -> None: + if set(body) - allowed: + raise ApiError(400, "invalid_request", "Неизвестные поля в теле запроса") + + +def validate_create_user(body: Any) -> tuple[str, str]: + body = require_object(body) + reject_extra(body, {"username", "display_name"}) + username = body.get("username") + display_name = body.get("display_name") + if not isinstance(username, str) or not USERNAME_RE.match(username): + raise ApiError(400, "invalid_request", "Некорректный username") + if not isinstance(display_name, str) or not (1 <= len(display_name) <= 100) or _is_blank(display_name): + raise ApiError(400, "invalid_request", "Некорректный display_name") + return username, display_name + + +def validate_create_chat(body: Any, caller_id: str) -> tuple[str, list[str]]: + body = require_object(body) + reject_extra(body, {"title", "member_ids"}) + title = body.get("title") + member_ids = body.get("member_ids") + if not isinstance(title, str) or not (1 <= len(title) <= 100) or _is_blank(title): + raise ApiError(400, "invalid_request", "Некорректный title") + if not isinstance(member_ids, list) or not (1 <= len(member_ids) <= 100): + raise ApiError(400, "invalid_request", "member_ids должен быть непустым массивом") + seen: set[str] = set() + for mid in member_ids: + if not isinstance(mid, str): + raise ApiError(400, "invalid_request", "member_ids содержит не строку") + try: + uuid.UUID(mid) + except ValueError: + raise ApiError(400, "invalid_request", "member_ids содержит не UUID") + if mid in seen: + raise ApiError(400, "invalid_request", "member_ids содержит дубликаты") + seen.add(mid) + if caller_id not in seen: + raise ApiError(400, "invalid_request", "Вызывающий должен входить в member_ids") + return title, list(member_ids) + + +def validate_create_message(body: Any) -> str: + body = require_object(body) + reject_extra(body, {"text"}) + text = body.get("text") + if not isinstance(text, str) or not (1 <= len(text) <= 4000) or _is_blank(text): + raise ApiError(400, "invalid_request", "Некорректный text") + return text + + +def validate_chat_id(raw: str) -> str: + """chat_id в пути должен быть UUID, иначе 400 invalid_request.""" + try: + uuid.UUID(raw) + except (ValueError, TypeError): + raise ApiError(400, "invalid_request", "Некорректный chat_id") + return raw + + +def parse_limit(raw: str | None, default: int = 50) -> int: + if raw is None: + return default + try: + value = int(raw) + except (TypeError, ValueError): + raise ApiError(400, "invalid_request", "limit должен быть целым числом") + if not (1 <= value <= 100): + raise ApiError(400, "invalid_request", "limit вне диапазона 1..100") + return value + + +def parse_cursor(raw: str | None, scope: str) -> tuple[str, str] | None: + """Cursor — непрозрачная строка base64url от 'epoch|scope|created_at|id'. + + Cursor привязан к ресурсу (scope) и к жизни процесса (epoch). + """ + if raw is None: + return None + try: + decoded = base64.urlsafe_b64decode(raw.encode("ascii")).decode("utf-8") + except Exception: + raise ApiError(400, "invalid_request", "Некорректный cursor") + parts = decoded.split("|") + if len(parts) != 4: + raise ApiError(400, "invalid_request", "Некорректный cursor") + epoch, cursor_scope, created_at, item_id = parts + if epoch != CURSOR_EPOCH or cursor_scope != scope: + raise ApiError(400, "invalid_request", "Некорректный cursor") + if not created_at or not item_id: + raise ApiError(400, "invalid_request", "Некорректный cursor") + return created_at, item_id + + +def encode_cursor(created_at: str, item_id: str, scope: str) -> str: + raw = "|".join([CURSOR_EPOCH, scope, created_at, item_id]).encode("utf-8") + return base64.urlsafe_b64encode(raw).decode("ascii") diff --git a/tests/test_unit.py b/tests/test_unit.py new file mode 100644 index 00000000..215878f8 --- /dev/null +++ b/tests/test_unit.py @@ -0,0 +1,276 @@ +#!/usr/bin/env python3 +"""Собственные тесты уровня 4: негативные сценарии и конкурентная запись. + +Запуск: python3 tests/test_unit.py +Требует запущенный сервер (по умолчанию http://localhost:8080). +""" +import argparse +import base64 +import json +import os +import sys +import threading +import unittest +import urllib.error +import urllib.request +import uuid + +_parser = argparse.ArgumentParser(description="Собственные тесты уровня 4") +_parser.add_argument("--base-url", default=None) +_args, _unknown = _parser.parse_known_args() +# unittest.main() читает sys.argv сам и не знает про --base-url — убираем его. +sys.argv = [sys.argv[0]] + _unknown + +BASE_URL = ( + _args.base_url + or os.environ.get("BASE_URL") + or "http://localhost:8080" +).rstrip("/") + + +def request(method, path, body=None, user_id=None, raw=None, content_type="application/json"): + headers = {"Accept": "application/json"} + if user_id: + headers["X-User-Id"] = user_id + data = None + if raw is not None: + data = raw + headers["Content-Type"] = content_type + elif body is not None: + data = json.dumps(body, ensure_ascii=False).encode("utf-8") + headers["Content-Type"] = "application/json" + req = urllib.request.Request(BASE_URL + path, data=data, headers=headers, method=method) + try: + with urllib.request.urlopen(req, timeout=10) as resp: + payload = resp.read() + return resp.status, json.loads(payload) if payload else None + except urllib.error.HTTPError as err: + payload = err.read() + return err.code, json.loads(payload) if payload else None + + +def make_user(label): + username = "u_" + label + "_" + uuid.uuid4().hex[:10] + status, user = request("POST", "/api/v1/users", {"username": username, "display_name": "Тест " + label}) + assert status == 201, (status, user) + return user + + +class MessengerTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.alice = make_user("alice") + cls.bob = make_user("bob") + cls.eve = make_user("eve") + status, chat = request( + "POST", + "/api/v1/chats", + {"title": "Тестовый чат", "member_ids": [cls.alice["id"], cls.bob["id"]]}, + user_id=cls.alice["id"], + ) + assert status == 201, (status, chat) + cls.chat = chat + + def test_invalid_json(self): + status, body = request( + "POST", + "/api/v1/chats/" + self.chat["id"] + "/messages", + raw=b"{not json", + user_id=self.alice["id"], + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_foreign_chat_returns_404(self): + status, body = request("GET", "/api/v1/chats/" + self.chat["id"], user_id=self.eve["id"]) + self.assertEqual(status, 404) + self.assertEqual(body["error"]["code"], "not_found") + + def test_empty_message_rejected(self): + status, body = request( + "POST", + "/api/v1/chats/" + self.chat["id"] + "/messages", + {"text": " "}, + user_id=self.alice["id"], + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_duplicate_username(self): + username = "u_dup_" + uuid.uuid4().hex[:10] + first = request("POST", "/api/v1/users", {"username": username, "display_name": "Первый"}) + self.assertEqual(first[0], 201) + second = request("POST", "/api/v1/users", {"username": username, "display_name": "Второй"}) + self.assertEqual(second[0], 409) + self.assertEqual(second[1]["error"]["code"], "conflict") + + def test_pagination_walks_all_pages(self): + chat = request( + "POST", + "/api/v1/chats", + {"title": "Пагинация", "member_ids": [self.alice["id"], self.bob["id"]]}, + user_id=self.alice["id"], + )[1] + sent = [] + for i in range(7): + status, msg = request( + "POST", + "/api/v1/chats/" + chat["id"] + "/messages", + {"text": "msg-%d" % i}, + user_id=self.alice["id"], + ) + self.assertEqual(status, 201) + sent.append(msg["id"]) + collected = [] + cursor = None + while True: + path = "/api/v1/chats/" + chat["id"] + "/messages?limit=2" + if cursor: + path += "&cursor=" + cursor + status, page = request("GET", path, user_id=self.bob["id"]) + self.assertEqual(status, 200) + collected.extend(m["id"] for m in page["items"]) + cursor = page["next_cursor"] + if not cursor: + break + self.assertEqual(collected, sent) + + def test_cursor_bound_to_resource(self): + chat_a = request( + "POST", + "/api/v1/chats", + {"title": "Cursor A", "member_ids": [self.alice["id"], self.bob["id"]]}, + user_id=self.alice["id"], + )[1] + chat_b = request( + "POST", + "/api/v1/chats", + {"title": "Cursor B", "member_ids": [self.alice["id"], self.bob["id"]]}, + user_id=self.alice["id"], + )[1] + for i in range(3): + request( + "POST", + "/api/v1/chats/" + chat_a["id"] + "/messages", + {"text": "a-%d" % i}, + user_id=self.alice["id"], + ) + status, page = request( + "GET", + "/api/v1/chats/" + chat_a["id"] + "/messages?limit=1", + user_id=self.alice["id"], + ) + self.assertEqual(status, 200) + cursor = page["next_cursor"] + self.assertIsNotNone(cursor) + status, body = request( + "GET", + "/api/v1/chats/" + chat_b["id"] + "/messages?limit=1&cursor=" + cursor, + user_id=self.alice["id"], + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_cursor_bound_to_endpoint(self): + status, page = request( + "GET", "/api/v1/chats?limit=1", user_id=self.alice["id"] + ) + self.assertEqual(status, 200) + cursor = page["next_cursor"] + self.assertIsNotNone(cursor) + status, body = request( + "GET", + "/api/v1/chats/" + self.chat["id"] + "/messages?limit=1&cursor=" + cursor, + user_id=self.alice["id"], + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_cursor_garbage_rejected(self): + garbage = base64.urlsafe_b64encode(b"x|y").decode("ascii") + status, body = request( + "GET", + "/api/v1/chats/" + self.chat["id"] + "/messages?cursor=" + garbage, + user_id=self.alice["id"], + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_invalid_chat_id_returns_400(self): + status, body = request("GET", "/api/v1/chats/not-a-uuid", user_id=self.alice["id"]) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + status, body = request( + "POST", + "/api/v1/chats/not-a-uuid/messages", + {"text": "hi"}, + user_id=self.alice["id"], + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + status, body = request( + "GET", "/api/v1/chats/not-a-uuid/messages", user_id=self.alice["id"] + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_username_with_trailing_newline_rejected(self): + status, body = request( + "POST", + "/api/v1/users", + {"username": "abc\n", "display_name": "Имя"}, + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_invalid_body_bytes_contract_error(self): + status, body = request( + "POST", + "/api/v1/users", + raw=b"\xff", + user_id=self.alice["id"], + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + self.assertIn("message", body["error"]) + self.assertIn("request_id", body["error"]) + + def test_concurrent_writes_are_unique(self): + chat = request( + "POST", + "/api/v1/chats", + {"title": "Конкурентность", "member_ids": [self.alice["id"], self.bob["id"]]}, + user_id=self.alice["id"], + )[1] + errors = [] + lock = threading.Lock() + + def worker(worker_id): + for i in range(10): + status, body = request( + "POST", + "/api/v1/chats/" + chat["id"] + "/messages", + {"text": "w%d-%d" % (worker_id, i)}, + user_id=self.alice["id"], + ) + if status != 201: + with lock: + errors.append((status, body)) + + threads = [threading.Thread(target=worker, args=(w,)) for w in range(10)] + for t in threads: + t.start() + for t in threads: + t.join() + self.assertEqual(errors, []) + status, page = request( + "GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", user_id=self.alice["id"] + ) + self.assertEqual(status, 200) + ids = [m["id"] for m in page["items"]] + self.assertEqual(len(ids), 100) + self.assertEqual(len(set(ids)), 100) + + +if __name__ == "__main__": + unittest.main(verbosity=2) -- 2.54.0 From 785fde833f4f41546353df253a5444ceac217a69 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:07:09 +0300 Subject: [PATCH 02/12] =?UTF-8?q?feat:=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B2?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B8=20API=20=D0=BD=D0=B0=20password/Basic/?= =?UTF-8?q?Bearer=20=D0=B8=20PostgreSQL=20=D1=81=20Redis-=D0=BA=D1=8D?= =?UTF-8?q?=D1=88=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- .env.example | 15 ++- Dockerfile | 15 +++ Makefile | 11 +- REPORT.md | 65 ++++++++-- app/db.py | 53 ++++++++ app/handlers.py | 174 ++++++++++++++++++++------- app/main.py | 29 ++++- app/security.py | 29 +++++ app/sessions.py | 129 ++++++++++++++++++++ app/store.py | 233 +++++++++++++++++++++++++++--------- app/validation.py | 26 ++-- compose.yaml | 50 ++++++++ migrations/001_init.sql | 32 +++++ migrations/002_sessions.sql | 9 ++ requirements.txt | 6 + scripts/explain_history.sql | 8 ++ scripts/restart.sh | 16 +++ tests/test_unit.py | 132 ++++++++++++++------ 18 files changed, 856 insertions(+), 176 deletions(-) create mode 100644 Dockerfile create mode 100644 app/db.py create mode 100644 app/security.py create mode 100644 app/sessions.py create mode 100644 compose.yaml create mode 100644 migrations/001_init.sql create mode 100644 migrations/002_sessions.sql create mode 100644 requirements.txt create mode 100644 scripts/explain_history.sql create mode 100644 scripts/restart.sh diff --git a/.env.example b/.env.example index 714c8076..becc1096 100644 --- a/.env.example +++ b/.env.example @@ -1,11 +1,14 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url +# DSN для PostgreSQL. В compose.yaml подставляется из secrets/database_url. +DATABASE_URL=postgresql://messenger:messenger@postgres:5432/messenger +# Redis используется как кэш сессий; источник истины — PostgreSQL. +CACHE_URL=redis://redis:6379/0 +# Таймауты (секунды) для БД и Redis. +DB_POOL_MIN=1 +DB_POOL_MAX=10 +DB_CONNECT_TIMEOUT=3 +REDIS_SOCKET_TIMEOUT=1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5207f7b3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY app ./app +COPY migrations ./migrations + +EXPOSE 8080 +CMD ["python", "-m", "app.main"] diff --git a/Makefile b/Makefile index 9ecb223e..738b7e98 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence +.PHONY: help check validate test load test-persistence test-unit up down help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" @@ -24,3 +24,12 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-unit: + $(PYTHON) tests/test_unit.py --base-url "$(BASE_URL)" + +up: + docker compose --env-file .env up -d --build + +down: + docker compose --env-file .env down diff --git a/REPORT.md b/REPORT.md index e42f2b05..8f2c966f 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,76 @@ # Отчёт — лабораторная 2 - Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO +- Целевая оценка: **4** (трек 5 не заявляется) - Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Использованные источники, библиотеки и помощь ИИ: официальная документация FastAPI, psycopg 3, psycopg_pool, redis-py, argon2-cffi, PostgreSQL; помощь ИИ — генерация каркаса кода и текста отчёта, проверка и адаптация вручную. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Prerequisites: Docker Engine 24+, Docker Compose v2, Python 3.9+ (для тестов), `make`. + +```sh +cp .env.example .env +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +# дождаться /health/ready (healthcheck в compose) +make test BASE_URL=http://localhost:8080 +make test-unit BASE_URL=http://localhost:8080 +chmod +x scripts/restart.sh +make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://localhost:8080 +``` + +Версии образов закреплены: `postgres:16.4-alpine`, `redis:7.4-alpine`, `python:3.12-slim`. Миграции применяются автоматически при старте API (`app/db.py::apply_migrations`), идемпотентно через таблицу `schema_migrations`. + +Безопасная остановка: `docker compose down` (volumes сохраняются). Намеренное удаление учебных данных: `docker compose down -v` — только осознанно, теряет все данные. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +- **Процессы**: `api` (FastAPI/uvicorn), `postgres` (источник истины), `redis` (кэш сессий с TTL). +- **Источник истины**: PostgreSQL. Таблицы `users`, `chats`, `chat_members`, `messages`, `sessions`, `schema_migrations`. PK/FK, `UNIQUE(username)`, составной PK `(chat_id, user_id)`. +- **Транзакционные границы**: создание чата — одна транзакция (INSERT chats + INSERT chat_members). Создание сообщения — одна транзакция. Создание сессии — одна транзакция. +- **Аутентификация**: `POST /api/v1/users` принимает `password` (12–128 символов), хранится Argon2id-хеш. `POST /api/v1/auth/sessions` — только Basic, выдаёт opaque Bearer-токен (256 бит, `secrets.token_urlsafe(32)`); в БД хранится SHA-256 от токена. `DELETE /api/v1/auth/sessions/current` отзывает сессию. `X-User-Id` игнорируется. +- **Сессии**: источник истины — `sessions` в PostgreSQL; Redis — кэш `session:` с TTL = остаток жизни сессии. При недоступности Redis — fallback на БД (не принимаем анонимов). При logout инвалидируется и БД, и кэш. +- **Пул и таймауты**: `psycopg_pool.ConnectionPool` (`DB_POOL_MIN`/`DB_POOL_MAX`), `DB_CONNECT_TIMEOUT`; Redis `socket_timeout`/`socket_connect_timeout` = `REDIS_SOCKET_TIMEOUT`. +- **Индекс истории**: `messages_chat_created_idx (chat_id, created_at, id)` под keyset-пагинацию. Проверка — `scripts/explain_history.sql`. +- **Соответствие `.env.example`**: `DATABASE_URL`, `CACHE_URL`, `SESSION_TTL_SECONDS`, `DB_POOL_MIN/MAX`, `DB_CONNECT_TIMEOUT`, `REDIS_SOCKET_TIMEOUT` — используются напрямую в `app/db.py`, `app/sessions.py`, `app/main.py`. ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke (уровень 3) | `make test` | PASS: lab2 public API scenario | вывод консоли | +| Persistence (уровень 3) | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS: persistence preserved confirmed data and existing session | вывод консоли | +| Миграции с нуля | `docker compose down -v && docker compose up -d --build` | API стартует, `schema_migrations` содержит 001/002 | `docker compose exec postgres psql -U messenger -c "select * from schema_migrations"` | +| Redis используется для сессий с TTL | `docker compose exec redis redis-cli TTL session:` | TTL > 0, уменьшается | вывод консоли | +| Параметризованные запросы | код `app/store.py`, `app/sessions.py` | все запросы через `%s` | исходники | +| Ограниченный пул и таймауты | `app/db.py`, `app/main.py` | `DB_POOL_MAX`, `DB_CONNECT_TIMEOUT`, `REDIS_SOCKET_TIMEOUT` | исходники | +| Гонка регистрации username | `tests/test_unit.py::test_duplicate_username` | 201 + 409 | `make test-unit` | +| Rollback составной операции | `app/store.py::create_chat` — `conn.transaction()` | при ошибке вставки участника чат не создаётся | исходники | +| Индекс истории + EXPLAIN | `psql "$DATABASE_URL" -f scripts/explain_history.sql` | Index Scan по `messages_chat_created_idx` | вывод EXPLAIN | +| Expiry при коротком TTL | `SESSION_TTL_SECONDS=2 docker compose up -d api` + `make test-unit` | 401 после истечения | вывод консоли | +| Logout | `tests/test_unit.py::test_logout_revokes_session` | 204, затем 401 | `make test-unit` | +| Неверный пароль | `tests/test_unit.py::test_wrong_password_returns_401` | 401 | `make test-unit` | +| Недоступность Redis | `docker compose stop redis` + `make test` | smoke проходит (fallback на БД) | вывод консоли | +| Недоступность БД | `docker compose stop postgres` + `curl /health/ready` | 503 `unavailable` | вывод консоли | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Гипотеза: при остановке Redis API продолжает обслуживать запросы (кэш — не источник истины); при остановке PostgreSQL `/health/ready` возвращает 503, бизнес-запросы — 500/503 без утечки деталей. + +Hardware: локальная машина разработчика (Windows 11, Docker Desktop). Объём данных: синтетические пользователи/чаты/сообщения из тестов. Профиль нагрузки: `tests/load.py --duration 30 --concurrency 4` (closed-loop, ограничен клиентом). | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| `docker compose restart api postgres redis` (volumes сохранены) | данные и сессии сохранены | PASS persistence | ~5–10 с, потерь нет | +| `docker compose stop redis` | API работает, сессии из БД | smoke PASS | восстановление после `start redis` | +| `docker compose stop postgres` | `/health/ready` → 503 | 503 `unavailable` | после `start postgres` — 200 | +| `SESSION_TTL_SECONDS=2` | сессия истекает | 401 после истечения | — | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Redis — кэш, не источник истины; при его недоступности latency сессий растёт (каждый запрос идёт в БД). +- Нет репликации PostgreSQL и балансировщика — уровень 3+ (failover) не заявляется. +- Нет idempotency-key для сообщений (lab3), нет вложений (lab4), нет фоновой обработки (lab5). +- `tests/load.py` — closed-loop, страдает coordinated omission; не доказывает максимальную пропускную способность. +- Трек 5 не выполнялся. diff --git a/app/db.py b/app/db.py new file mode 100644 index 00000000..a7213b6c --- /dev/null +++ b/app/db.py @@ -0,0 +1,53 @@ +"""Пул соединений PostgreSQL и применение миграций.""" +import logging +import os +from pathlib import Path + +import psycopg +from psycopg_pool import ConnectionPool + +logger = logging.getLogger("messenger.db") + +MIGRATIONS_DIR = Path(__file__).resolve().parents[1] / "migrations" + + +def make_pool() -> ConnectionPool: + dsn = os.environ.get("DATABASE_URL") + if not dsn: + raise RuntimeError("DATABASE_URL is not set") + pool = ConnectionPool( + conninfo=dsn, + min_size=int(os.environ.get("DB_POOL_MIN", "1")), + max_size=int(os.environ.get("DB_POOL_MAX", "10")), + timeout=float(os.environ.get("DB_CONNECT_TIMEOUT", "3")), + kwargs={"autocommit": False}, + open=False, + ) + pool.open(wait=True, timeout=float(os.environ.get("DB_CONNECT_TIMEOUT", "3"))) + return pool + + +def apply_migrations(pool: ConnectionPool) -> None: + """Идемпотентно применяет *.sql из migrations/ в лексикографическом порядке.""" + with pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "CREATE TABLE IF NOT EXISTS schema_migrations (" + " version TEXT PRIMARY KEY," + " applied_at TIMESTAMPTZ NOT NULL DEFAULT now())" + ) + conn.commit() + cur.execute("SELECT version FROM schema_migrations") + applied = {row[0] for row in cur.fetchall()} + for path in sorted(MIGRATIONS_DIR.glob("*.sql")): + version = path.stem + if version in applied: + continue + logger.info("applying migration %s", version) + sql = path.read_text(encoding="utf-8") + with conn.transaction(): + with conn.cursor() as cur: + cur.execute(sql) + cur.execute( + "INSERT INTO schema_migrations(version) VALUES (%s)", (version,) + ) diff --git a/app/handlers.py b/app/handlers.py index c3d55699..ad1f1d84 100644 --- a/app/handlers.py +++ b/app/handlers.py @@ -1,10 +1,15 @@ -"""HTTP-обработчики FastAPI для lab1.""" +"""HTTP-обработчики FastAPI для lab2: Basic/Bearer, PostgreSQL, Redis-кэш сессий.""" +import base64 +import binascii +import logging from typing import Any from fastapi import APIRouter, Body, Header, Query, Request from fastapi.responses import JSONResponse from .errors import ApiError +from .security import hash_password, verify_password +from .sessions import SessionStore from .store import Store from .validation import ( encode_cursor, @@ -18,19 +23,48 @@ from .validation import ( router = APIRouter() +BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"' +BEARER_CHALLENGE = 'Bearer realm="messenger"' + def get_store(request: Request) -> Store: return request.app.state.store -def current_user_id(request: Request, x_user_id: str | None) -> str: - """lab1: X-User-Id — идентификатор существующего пользователя.""" - if not x_user_id: - raise ApiError(401, "unauthorized", "Требуется X-User-Id") - store = get_store(request) - if store.get_user(x_user_id) is None: - raise ApiError(401, "unauthorized", "Неизвестный пользователь") - return x_user_id +def get_sessions(request: Request) -> SessionStore: + return request.app.state.sessions + + +def _unauthorized(message: str, challenge: str) -> ApiError: + err = ApiError(401, "unauthorized", message) + err.challenge = challenge # type: ignore[attr-defined] + return err + + +def current_user_id(request: Request, authorization: str | None) -> str: + """lab2: только Bearer. X-User-Id игнорируется.""" + if not authorization or not authorization.lower().startswith("bearer "): + raise _unauthorized("Требуется Bearer-токен", BEARER_CHALLENGE) + token = authorization[7:].strip() + if not token: + raise _unauthorized("Пустой Bearer-токен", BEARER_CHALLENGE) + user_id = get_sessions(request).resolve(token) + if user_id is None: + raise _unauthorized("Недействительная сессия", BEARER_CHALLENGE) + return user_id + + +def _parse_basic(authorization: str | None) -> tuple[str, str]: + if not authorization or not authorization.lower().startswith("basic "): + raise _unauthorized("Требуется Basic-аутентификация", BASIC_CHALLENGE) + try: + decoded = base64.b64decode(authorization[6:].strip(), validate=True).decode("utf-8") + except (binascii.Error, UnicodeDecodeError): + raise _unauthorized("Некорректный Basic", BASIC_CHALLENGE) + if ":" not in decoded: + raise _unauthorized("Некорректный Basic", BASIC_CHALLENGE) + username, password = decoded.split(":", 1) + return username, password @router.get("/health/live") @@ -39,25 +73,78 @@ def health_live() -> dict[str, str]: @router.get("/health/ready") -def health_ready() -> dict[str, str]: - return {"status": "ok"} +def health_ready(request: Request) -> JSONResponse: + """Ready, если доступна БД (обязательный синхронный путь). Redis — кэш.""" + try: + with get_store(request).pool.connection() as conn: + with conn.cursor() as cur: + cur.execute("SELECT 1") + cur.fetchone() + except Exception as exc: # noqa: BLE001 + logger_ready = logging.getLogger("messenger.ready") + logger_ready.warning("readiness failed: %s", type(exc).__name__) + raise ApiError(503, "unavailable", "Зависимость недоступна") + return JSONResponse({"status": "ok"}) @router.post("/api/v1/users", status_code=201) def register_user(request: Request, body: Any = Body(...)) -> JSONResponse: - username, display_name = validate_create_user(body) + username, display_name, password = validate_create_user(body) store = get_store(request) - user = store.create_user(username, display_name) + user = store.create_user(username, display_name, hash_password(password)) if user is None: raise ApiError(409, "conflict", "username уже занят") return JSONResponse(user, status_code=201) +@router.post("/api/v1/auth/sessions", status_code=201) +def create_session( + request: Request, + authorization: str | None = Header(default=None), +) -> JSONResponse: + username, password = _parse_basic(authorization) + store = get_store(request) + found = store.get_user_by_username(username) + if found is None: + # Одинаковый 401 для неверного username и пароля. + raise _unauthorized("Неверные учётные данные", BASIC_CHALLENGE) + user, password_hash = found + if not verify_password(password_hash, password): + raise _unauthorized("Неверные учётные данные", BASIC_CHALLENGE) + session = get_sessions(request).create(user["id"]) + return JSONResponse( + { + "token": session["token"], + "token_type": "Bearer", + "expires_at": session["expires_at"], + "user": user, + }, + status_code=201, + ) + + +@router.delete("/api/v1/auth/sessions/current", status_code=204) +def revoke_session( + request: Request, + authorization: str | None = Header(default=None), +) -> JSONResponse: + if not authorization or not authorization.lower().startswith("bearer "): + raise _unauthorized("Требуется Bearer-токен", BEARER_CHALLENGE) + token = authorization[7:].strip() + if not token or get_sessions(request).resolve(token) is None: + raise _unauthorized("Недействительная сессия", BEARER_CHALLENGE) + get_sessions(request).revoke(token) + return JSONResponse(status_code=204) + + @router.get("/api/v1/users/me") -def current_user(request: Request, x_user_id: str | None = Header(default=None)) -> dict[str, Any]: - user_id = current_user_id(request, x_user_id) +def current_user( + request: Request, authorization: str | None = Header(default=None) +) -> dict[str, Any]: + user_id = current_user_id(request, authorization) user = get_store(request).get_user(user_id) - assert user is not None + if user is None: + raise _unauthorized("Недействительная сессия", BEARER_CHALLENGE) return user @@ -65,9 +152,9 @@ def current_user(request: Request, x_user_id: str | None = Header(default=None)) def create_chat( request: Request, body: Any = Body(...), - x_user_id: str | None = Header(default=None), + authorization: str | None = Header(default=None), ) -> JSONResponse: - caller = current_user_id(request, x_user_id) + caller = current_user_id(request, authorization) title, member_ids = validate_create_chat(body, caller) store = get_store(request) for mid in member_ids: @@ -82,33 +169,33 @@ def list_chats( request: Request, limit: str | None = Query(default=None), cursor: str | None = Query(default=None), - x_user_id: str | None = Header(default=None), + authorization: str | None = Header(default=None), ) -> dict[str, Any]: - caller = current_user_id(request, x_user_id) + caller = current_user_id(request, authorization) page_size = parse_limit(limit) scope = f"chats:{caller}" after = parse_cursor(cursor, scope) - items = get_store(request).list_chats_for(caller) - if after is not None: - items = [c for c in items if (c["created_at"], c["id"]) > after] - page = items[:page_size] + items, has_more = get_store(request).list_chats_for(caller, page_size, after) next_cursor = None - if len(items) > page_size and page: - last = page[-1] + if has_more and items: + last = items[-1] next_cursor = encode_cursor(last["created_at"], last["id"], scope) - return {"items": page, "next_cursor": next_cursor} + return {"items": items, "next_cursor": next_cursor} @router.get("/api/v1/chats/{chat_id}") def get_chat( chat_id: str, request: Request, - x_user_id: str | None = Header(default=None), + authorization: str | None = Header(default=None), ) -> dict[str, Any]: - caller = current_user_id(request, x_user_id) + caller = current_user_id(request, authorization) chat_id = validate_chat_id(chat_id) - chat = get_store(request).get_chat(chat_id) - if chat is None or caller not in chat["member_ids"]: + store = get_store(request) + if not store.is_member(chat_id, caller): + raise ApiError(404, "not_found", "Чат не найден") + chat = store.get_chat(chat_id) + if chat is None: raise ApiError(404, "not_found", "Чат не найден") return chat @@ -118,13 +205,12 @@ def create_message( chat_id: str, request: Request, body: Any = Body(...), - x_user_id: str | None = Header(default=None), + authorization: str | None = Header(default=None), ) -> JSONResponse: - caller = current_user_id(request, x_user_id) + caller = current_user_id(request, authorization) chat_id = validate_chat_id(chat_id) store = get_store(request) - chat = store.get_chat(chat_id) - if chat is None or caller not in chat["member_ids"]: + if not store.is_member(chat_id, caller): raise ApiError(404, "not_found", "Чат не найден") text = validate_create_message(body) message = store.add_message(chat_id, caller, text) @@ -137,23 +223,19 @@ def list_messages( request: Request, limit: str | None = Query(default=None), cursor: str | None = Query(default=None), - x_user_id: str | None = Header(default=None), + authorization: str | None = Header(default=None), ) -> dict[str, Any]: - caller = current_user_id(request, x_user_id) + caller = current_user_id(request, authorization) chat_id = validate_chat_id(chat_id) store = get_store(request) - chat = store.get_chat(chat_id) - if chat is None or caller not in chat["member_ids"]: + if not store.is_member(chat_id, caller): raise ApiError(404, "not_found", "Чат не найден") page_size = parse_limit(limit) scope = f"messages:{chat_id}" after = parse_cursor(cursor, scope) - items = store.list_messages(chat_id) - if after is not None: - items = [m for m in items if (m["created_at"], m["id"]) > after] - page = items[:page_size] + items, has_more = store.list_messages(chat_id, page_size, after) next_cursor = None - if len(items) > page_size and page: - last = page[-1] + if has_more and items: + last = items[-1] next_cursor = encode_cursor(last["created_at"], last["id"], scope) - return {"items": page, "next_cursor": next_cursor} + return {"items": items, "next_cursor": next_cursor} diff --git a/app/main.py b/app/main.py index 39dc81bc..ab2aac61 100644 --- a/app/main.py +++ b/app/main.py @@ -3,13 +3,16 @@ import logging import os import uuid +import redis from fastapi import FastAPI, Request from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse from starlette.exceptions import HTTPException as StarletteHTTPException +from .db import apply_migrations, make_pool from .errors import ApiError, error_body from .handlers import router +from .sessions import SessionStore from .store import Store logging.basicConfig( @@ -18,8 +21,24 @@ logging.basicConfig( ) logger = logging.getLogger("messenger") -app = FastAPI(title="MTUSI Messenger", version="1.1.0") -app.state.store = Store() +app = FastAPI(title="MTUSI Messenger", version="1.2.0") + +_pool = make_pool() +apply_migrations(_pool) + +_cache: redis.Redis | None = None +_cache_url = os.environ.get("CACHE_URL") +if _cache_url: + _cache = redis.Redis.from_url( + _cache_url, + socket_timeout=float(os.environ.get("REDIS_SOCKET_TIMEOUT", "1")), + socket_connect_timeout=float(os.environ.get("REDIS_SOCKET_TIMEOUT", "1")), + decode_responses=False, + ) + +app.state.pool = _pool +app.state.store = Store(_pool) +app.state.sessions = SessionStore(_pool, _cache) app.include_router(router) @@ -41,10 +60,14 @@ async def request_id_middleware(request: Request, call_next): @app.exception_handler(ApiError) async def api_error_handler(request: Request, exc: ApiError) -> JSONResponse: request_id = getattr(request.state, "request_id", uuid.uuid4().hex) + headers = {"X-Request-Id": request_id} + challenge = getattr(exc, "challenge", None) + if challenge: + headers["WWW-Authenticate"] = challenge return JSONResponse( error_body(exc.code, exc.message, request_id), status_code=exc.status, - headers={"X-Request-Id": request_id}, + headers=headers, ) diff --git a/app/security.py b/app/security.py new file mode 100644 index 00000000..b5ff1562 --- /dev/null +++ b/app/security.py @@ -0,0 +1,29 @@ +"""Хеширование паролей (Argon2id) и генерация opaque-токенов сессий.""" +import hashlib +import secrets + +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError, VerificationError, InvalidHashError + +_hasher = PasswordHasher() + + +def hash_password(password: str) -> str: + return _hasher.hash(password) + + +def verify_password(password_hash: str, password: str) -> bool: + try: + return _hasher.verify(password_hash, password) + except (VerifyMismatchError, VerificationError, InvalidHashError): + return False + + +def new_token() -> str: + """256 бит энтропии, URL-safe.""" + return secrets.token_urlsafe(32) + + +def token_hash(token: str) -> str: + """В БД/Redis храним только SHA-256 от токена, не сам токен.""" + return hashlib.sha256(token.encode("utf-8")).hexdigest() diff --git a/app/sessions.py b/app/sessions.py new file mode 100644 index 00000000..e4e6d810 --- /dev/null +++ b/app/sessions.py @@ -0,0 +1,129 @@ +"""Сессии: PostgreSQL — источник истины, Redis — кэш с TTL. + +При недоступности Redis работаем напрямую с БД (не принимаем анонимов). +При logout инвалидируем и БД, и кэш. +""" +import logging +import os +from datetime import datetime, timedelta, timezone +from typing import Any + +import redis +from psycopg_pool import ConnectionPool + +from .security import new_token, token_hash + +logger = logging.getLogger("messenger.sessions") + + +def _now() -> datetime: + return datetime.now(timezone.utc) + + +def _ttl_seconds() -> int: + return int(os.environ.get("SESSION_TTL_SECONDS", "3600")) + + +class SessionStore: + def __init__(self, pool: ConnectionPool, cache: redis.Redis | None) -> None: + self.pool = pool + self.cache = cache + + # --- кэш -------------------------------------------------------------- + def _cache_get(self, th: str) -> dict[str, Any] | None: + if self.cache is None: + return None + try: + raw = self.cache.hgetall("session:" + th) + except redis.RedisError as exc: + logger.warning("redis get failed: %s", type(exc).__name__) + return None + if not raw: + return None + return { + "user_id": raw[b"user_id"].decode(), + "expires_at": raw[b"expires_at"].decode(), + } + + def _cache_put(self, th: str, user_id: str, expires_at: datetime) -> None: + if self.cache is None: + return + ttl = max(1, int((expires_at - _now()).total_seconds())) + try: + pipe = self.cache.pipeline() + pipe.hset( + "session:" + th, + mapping={ + "user_id": user_id, + "expires_at": expires_at.strftime("%Y-%m-%dT%H:%M:%SZ"), + }, + ) + pipe.expire("session:" + th, ttl) + pipe.execute() + except redis.RedisError as exc: + logger.warning("redis put failed: %s", type(exc).__name__) + + def _cache_del(self, th: str) -> None: + if self.cache is None: + return + try: + self.cache.delete("session:" + th) + except redis.RedisError as exc: + logger.warning("redis del failed: %s", type(exc).__name__) + + # --- публичный API ---------------------------------------------------- + def create(self, user_id: str) -> dict[str, Any]: + token = new_token() + th = token_hash(token) + expires_at = _now() + timedelta(seconds=_ttl_seconds()) + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "INSERT INTO sessions(token_hash, user_id, created_at, expires_at)" + " VALUES (%s, %s, now(), %s)", + (th, user_id, expires_at), + ) + conn.commit() + self._cache_put(th, user_id, expires_at) + return { + "token": token, + "expires_at": expires_at.strftime("%Y-%m-%dT%H:%M:%SZ"), + } + + def resolve(self, token: str) -> str | None: + """Возвращает user_id для действующей сессии или None.""" + th = token_hash(token) + cached = self._cache_get(th) + if cached is not None: + try: + exp = datetime.strptime(cached["expires_at"], "%Y-%m-%dT%H:%M:%SZ").replace( + tzinfo=timezone.utc + ) + except ValueError: + exp = None + if exp is not None and exp > _now(): + return cached["user_id"] + self._cache_del(th) + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT user_id, expires_at FROM sessions WHERE token_hash = %s", + (th,), + ) + row = cur.fetchone() + if row is None: + return None + user_id, expires_at = row + if expires_at <= _now(): + self.revoke(token) + return None + self._cache_put(th, str(user_id), expires_at) + return str(user_id) + + def revoke(self, token: str) -> None: + th = token_hash(token) + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute("DELETE FROM sessions WHERE token_hash = %s", (th,)) + conn.commit() + self._cache_del(th) diff --git a/app/store.py b/app/store.py index 0e0e00b5..65c21c02 100644 --- a/app/store.py +++ b/app/store.py @@ -1,9 +1,11 @@ -"""Потокобезопасное in-memory хранилище пользователей, чатов и сообщений.""" -import threading +"""Хранилище на PostgreSQL: пользователи, чаты, участники, сообщения.""" import uuid from datetime import datetime, timezone from typing import Any +import psycopg +from psycopg_pool import ConnectionPool + def utc_now() -> str: """RFC 3339 UTC с суффиксом Z, секунды — как в примерах контракта.""" @@ -14,75 +16,188 @@ def new_id() -> str: return str(uuid.uuid4()) -class Store: - """Все данные живут в памяти одного процесса и исчезают при рестарте.""" +def _iso(dt: datetime) -> str: + return dt.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") - def __init__(self) -> None: - self._lock = threading.RLock() - self.users: dict[str, dict[str, Any]] = {} - self.usernames: dict[str, str] = {} - self.chats: dict[str, dict[str, Any]] = {} - self.messages: dict[str, list[dict[str, Any]]] = {} + +def _user_row(row: tuple) -> dict[str, Any]: + uid, username, display_name, created_at = row + return { + "id": str(uid), + "username": username, + "display_name": display_name, + "created_at": _iso(created_at), + } + + +def _chat_row(row: tuple, member_ids: list[str]) -> dict[str, Any]: + cid, title, created_at = row + return { + "id": str(cid), + "title": title, + "member_ids": member_ids, + "created_at": _iso(created_at), + } + + +def _message_row(row: tuple) -> dict[str, Any]: + mid, chat_id, sender_id, text, created_at = row + return { + "id": str(mid), + "chat_id": str(chat_id), + "sender_id": str(sender_id), + "text": text, + "created_at": _iso(created_at), + } + + +class Store: + """Все данные в PostgreSQL; соединения берутся из пула.""" + + def __init__(self, pool: ConnectionPool) -> None: + self.pool = pool # --- пользователи ----------------------------------------------------- - def create_user(self, username: str, display_name: str) -> dict[str, Any]: - with self._lock: - if username in self.usernames: - return None # сигнал конфликта - user = { - "id": new_id(), - "username": username, - "display_name": display_name, - "created_at": utc_now(), - } - self.users[user["id"]] = user - self.usernames[username] = user["id"] - return dict(user) + def create_user( + self, username: str, display_name: str, password_hash: str + ) -> dict[str, Any] | None: + user_id = new_id() + try: + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "INSERT INTO users(id, username, display_name, password_hash, created_at)" + " VALUES (%s, %s, %s, %s, now())" + " RETURNING id, username, display_name, created_at", + (user_id, username, display_name, password_hash), + ) + row = cur.fetchone() + conn.commit() + except psycopg.errors.UniqueViolation: + return None + return _user_row(row) def get_user(self, user_id: str) -> dict[str, Any] | None: - with self._lock: - user = self.users.get(user_id) - return dict(user) if user else None + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT id, username, display_name, created_at FROM users WHERE id = %s", + (user_id,), + ) + row = cur.fetchone() + return _user_row(row) if row else None + + def get_user_by_username(self, username: str) -> tuple[dict[str, Any], str] | None: + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT id, username, display_name, created_at, password_hash" + " FROM users WHERE username = %s", + (username,), + ) + row = cur.fetchone() + if row is None: + return None + return _user_row(row[:4]), row[4] # --- чаты ------------------------------------------------------------- def create_chat(self, title: str, member_ids: list[str]) -> dict[str, Any]: - with self._lock: - chat = { - "id": new_id(), - "title": title, - "member_ids": list(member_ids), - "created_at": utc_now(), - } - self.chats[chat["id"]] = chat - self.messages[chat["id"]] = [] - return dict(chat) + chat_id = new_id() + with self.pool.connection() as conn: + with conn.transaction(): + with conn.cursor() as cur: + cur.execute( + "INSERT INTO chats(id, title, created_at) VALUES (%s, %s, now())" + " RETURNING id, title, created_at", + (chat_id, title), + ) + row = cur.fetchone() + cur.executemany( + "INSERT INTO chat_members(chat_id, user_id) VALUES (%s, %s)", + [(chat_id, mid) for mid in member_ids], + ) + return _chat_row(row, list(member_ids)) + + def _members(self, cur, chat_id: str) -> list[str]: + cur.execute( + "SELECT user_id FROM chat_members WHERE chat_id = %s ORDER BY user_id", + (chat_id,), + ) + return [str(r[0]) for r in cur.fetchall()] def get_chat(self, chat_id: str) -> dict[str, Any] | None: - with self._lock: - chat = self.chats.get(chat_id) - return dict(chat) if chat else None + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT id, title, created_at FROM chats WHERE id = %s", (chat_id,) + ) + row = cur.fetchone() + if row is None: + return None + members = self._members(cur, chat_id) + return _chat_row(row, members) - def list_chats_for(self, user_id: str) -> list[dict[str, Any]]: - with self._lock: - items = [dict(c) for c in self.chats.values() if user_id in c["member_ids"]] - items.sort(key=lambda c: (c["created_at"], c["id"])) - return items + def is_member(self, chat_id: str, user_id: str) -> bool: + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", + (chat_id, user_id), + ) + return cur.fetchone() is not None + + def list_chats_for( + self, user_id: str, limit: int, after: tuple[str, str] | None + ) -> tuple[list[dict[str, Any]], bool]: + with self.pool.connection() as conn: + with conn.cursor() as cur: + sql = ( + "SELECT c.id, c.title, c.created_at FROM chats c" + " JOIN chat_members m ON m.chat_id = c.id" + " WHERE m.user_id = %s" + ) + params: list[Any] = [user_id] + if after is not None: + sql += " AND (c.created_at, c.id) > (%s, %s)" + params.extend(after) + sql += " ORDER BY c.created_at, c.id LIMIT %s" + params.append(limit + 1) + cur.execute(sql, params) + rows = cur.fetchall() + items = [] + for row in rows[:limit]: + items.append(_chat_row(row, self._members(cur, str(row[0])))) + return items, len(rows) > limit # --- сообщения -------------------------------------------------------- def add_message(self, chat_id: str, sender_id: str, text: str) -> dict[str, Any]: - with self._lock: - message = { - "id": new_id(), - "chat_id": chat_id, - "sender_id": sender_id, - "text": text, - "created_at": utc_now(), - } - self.messages.setdefault(chat_id, []).append(message) - return dict(message) + with self.pool.connection() as conn: + with conn.cursor() as cur: + cur.execute( + "INSERT INTO messages(id, chat_id, sender_id, text, created_at)" + " VALUES (%s, %s, %s, %s, now())" + " RETURNING id, chat_id, sender_id, text, created_at", + (new_id(), chat_id, sender_id, text), + ) + row = cur.fetchone() + conn.commit() + return _message_row(row) - def list_messages(self, chat_id: str) -> list[dict[str, Any]]: - with self._lock: - items = [dict(m) for m in self.messages.get(chat_id, [])] - items.sort(key=lambda m: (m["created_at"], m["id"])) - return items + def list_messages( + self, chat_id: str, limit: int, after: tuple[str, str] | None + ) -> tuple[list[dict[str, Any]], bool]: + with self.pool.connection() as conn: + with conn.cursor() as cur: + sql = ( + "SELECT id, chat_id, sender_id, text, created_at FROM messages" + " WHERE chat_id = %s" + ) + params: list[Any] = [chat_id] + if after is not None: + sql += " AND (created_at, id) > (%s, %s)" + params.extend(after) + sql += " ORDER BY created_at, id LIMIT %s" + params.append(limit + 1) + cur.execute(sql, params) + rows = cur.fetchall() + return [_message_row(r) for r in rows[:limit]], len(rows) > limit diff --git a/app/validation.py b/app/validation.py index 5ff9ff12..cd25e3d9 100644 --- a/app/validation.py +++ b/app/validation.py @@ -8,10 +8,6 @@ from .errors import ApiError USERNAME_RE = re.compile(r"\A[a-z0-9_]{3,32}\Z") -# Уникальный идентификатор жизни процесса: cursors, выданные до рестарта, -# становятся недействительными. -CURSOR_EPOCH = uuid.uuid4().hex - def _is_blank(value: str) -> bool: return value.strip() == "" @@ -28,16 +24,19 @@ def reject_extra(body: dict[str, Any], allowed: set[str]) -> None: raise ApiError(400, "invalid_request", "Неизвестные поля в теле запроса") -def validate_create_user(body: Any) -> tuple[str, str]: +def validate_create_user(body: Any) -> tuple[str, str, str]: body = require_object(body) - reject_extra(body, {"username", "display_name"}) + reject_extra(body, {"username", "display_name", "password"}) username = body.get("username") display_name = body.get("display_name") + password = body.get("password") if not isinstance(username, str) or not USERNAME_RE.match(username): raise ApiError(400, "invalid_request", "Некорректный username") if not isinstance(display_name, str) or not (1 <= len(display_name) <= 100) or _is_blank(display_name): raise ApiError(400, "invalid_request", "Некорректный display_name") - return username, display_name + if not isinstance(password, str) or not (12 <= len(password) <= 128): + raise ApiError(400, "invalid_request", "Пароль должен быть 12..128 символов") + return username, display_name, password def validate_create_chat(body: Any, caller_id: str) -> tuple[str, list[str]]: @@ -96,9 +95,10 @@ def parse_limit(raw: str | None, default: int = 50) -> int: def parse_cursor(raw: str | None, scope: str) -> tuple[str, str] | None: - """Cursor — непрозрачная строка base64url от 'epoch|scope|created_at|id'. + """Cursor — непрозрачная строка base64url от 'scope|created_at|id'. - Cursor привязан к ресурсу (scope) и к жизни процесса (epoch). + Cursor привязан к ресурсу (scope). Значения created_at/id — из БД, + поэтому cursor переживает рестарт процесса. """ if raw is None: return None @@ -107,10 +107,10 @@ def parse_cursor(raw: str | None, scope: str) -> tuple[str, str] | None: except Exception: raise ApiError(400, "invalid_request", "Некорректный cursor") parts = decoded.split("|") - if len(parts) != 4: + if len(parts) != 3: raise ApiError(400, "invalid_request", "Некорректный cursor") - epoch, cursor_scope, created_at, item_id = parts - if epoch != CURSOR_EPOCH or cursor_scope != scope: + cursor_scope, created_at, item_id = parts + if cursor_scope != scope: raise ApiError(400, "invalid_request", "Некорректный cursor") if not created_at or not item_id: raise ApiError(400, "invalid_request", "Некорректный cursor") @@ -118,5 +118,5 @@ def parse_cursor(raw: str | None, scope: str) -> tuple[str, str] | None: def encode_cursor(created_at: str, item_id: str, scope: str) -> str: - raw = "|".join([CURSOR_EPOCH, scope, created_at, item_id]).encode("utf-8") + raw = "|".join([scope, created_at, item_id]).encode("utf-8") return base64.urlsafe_b64encode(raw).decode("ascii") diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..601ceb4e --- /dev/null +++ b/compose.yaml @@ -0,0 +1,50 @@ +services: + postgres: + image: postgres:16.4-alpine + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD: messenger + POSTGRES_DB: messenger + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 3s + timeout: 3s + retries: 20 + + redis: + image: redis:7.4-alpine + command: ["redis-server", "--appendonly", "yes"] + volumes: + - redisdata:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 3s + timeout: 3s + retries: 20 + + api: + build: . + environment: + APP_HOST: 0.0.0.0 + APP_PORT: "8080" + SESSION_TTL_SECONDS: "3600" + DATABASE_URL: postgresql://messenger:messenger@postgres:5432/messenger + CACHE_URL: redis://redis:6379/0 + ports: + - "8080:8080" + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)"] + interval: 5s + timeout: 3s + retries: 20 + +volumes: + pgdata: + redisdata: diff --git a/migrations/001_init.sql b/migrations/001_init.sql new file mode 100644 index 00000000..74bea693 --- /dev/null +++ b/migrations/001_init.sql @@ -0,0 +1,32 @@ +CREATE TABLE users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL UNIQUE, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX chat_members_user_idx ON chat_members(user_id); + +CREATE TABLE messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id), + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +-- Индекс под keyset-пагинацию истории чата: (chat_id, created_at, id). +CREATE INDEX messages_chat_created_idx ON messages(chat_id, created_at, id); diff --git a/migrations/002_sessions.sql b/migrations/002_sessions.sql new file mode 100644 index 00000000..3b470f7a --- /dev/null +++ b/migrations/002_sessions.sql @@ -0,0 +1,9 @@ +CREATE TABLE sessions ( + token_hash TEXT PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + created_at TIMESTAMPTZ NOT NULL, + expires_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX sessions_user_idx ON sessions(user_id); +CREATE INDEX sessions_expires_idx ON sessions(expires_at); diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..b38dfcb0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +fastapi==0.115.6 +uvicorn[standard]==0.32.1 +psycopg[binary]==3.2.3 +psycopg-pool==3.2.4 +redis==5.2.1 +argon2-cffi==23.1.0 diff --git a/scripts/explain_history.sql b/scripts/explain_history.sql new file mode 100644 index 00000000..0af0258d --- /dev/null +++ b/scripts/explain_history.sql @@ -0,0 +1,8 @@ +-- Ожидаем Index Scan по messages_chat_created_idx, без Seq Scan всей истории. +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = '00000000-0000-4000-8000-000000000000' + AND (created_at, id) > ('2026-01-01T00:00:00Z', '00000000-0000-4000-8000-000000000000') +ORDER BY created_at, id +LIMIT 51; diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..dc2d3d24 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -eu +# Перезапускает API и все используемые хранилища, сохраняя volumes. +# Не выполняет down -v, не flush-ит Redis, не пересоздаёт данные. +docker compose restart api postgres redis +# Ждём готовности API (тест сам ждёт /health/ready, но полезно и здесь). +i=0 +while [ "$i" -lt 60 ]; do + if docker compose exec -T api python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)" >/dev/null 2>&1; then + exit 0 + fi + i=$((i+1)) + sleep 1 +done +echo "api did not become ready" >&2 +exit 1 diff --git a/tests/test_unit.py b/tests/test_unit.py index 215878f8..d385b6c4 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -28,10 +28,15 @@ BASE_URL = ( ).rstrip("/") -def request(method, path, body=None, user_id=None, raw=None, content_type="application/json"): +def request(method, path, body=None, token=None, raw=None, content_type="application/json", + basic=None, extra_headers=None): headers = {"Accept": "application/json"} - if user_id: - headers["X-User-Id"] = user_id + if token: + headers["Authorization"] = "Bearer " + token + if basic: + headers["Authorization"] = "Basic " + basic + if extra_headers: + headers.update(extra_headers) data = None if raw is not None: data = raw @@ -51,9 +56,17 @@ def request(method, path, body=None, user_id=None, raw=None, content_type="appli def make_user(label): username = "u_" + label + "_" + uuid.uuid4().hex[:10] - status, user = request("POST", "/api/v1/users", {"username": username, "display_name": "Тест " + label}) + password = "T3st-" + uuid.uuid4().hex + status, user = request( + "POST", + "/api/v1/users", + {"username": username, "display_name": "Тест " + label, "password": password}, + ) assert status == 201, (status, user) - return user + basic = base64.b64encode((username + ":" + password).encode()).decode() + status, session = request("POST", "/api/v1/auth/sessions", basic=basic) + assert status == 201, (status, session) + return {"user": user, "token": session["token"], "basic": basic, "password": password} class MessengerTests(unittest.TestCase): @@ -65,8 +78,8 @@ class MessengerTests(unittest.TestCase): status, chat = request( "POST", "/api/v1/chats", - {"title": "Тестовый чат", "member_ids": [cls.alice["id"], cls.bob["id"]]}, - user_id=cls.alice["id"], + {"title": "Тестовый чат", "member_ids": [cls.alice["user"]["id"], cls.bob["user"]["id"]]}, + token=cls.alice["token"], ) assert status == 201, (status, chat) cls.chat = chat @@ -76,13 +89,13 @@ class MessengerTests(unittest.TestCase): "POST", "/api/v1/chats/" + self.chat["id"] + "/messages", raw=b"{not json", - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") def test_foreign_chat_returns_404(self): - status, body = request("GET", "/api/v1/chats/" + self.chat["id"], user_id=self.eve["id"]) + status, body = request("GET", "/api/v1/chats/" + self.chat["id"], token=self.eve["token"]) self.assertEqual(status, 404) self.assertEqual(body["error"]["code"], "not_found") @@ -91,25 +104,74 @@ class MessengerTests(unittest.TestCase): "POST", "/api/v1/chats/" + self.chat["id"] + "/messages", {"text": " "}, - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") def test_duplicate_username(self): username = "u_dup_" + uuid.uuid4().hex[:10] - first = request("POST", "/api/v1/users", {"username": username, "display_name": "Первый"}) + password = "T3st-" + uuid.uuid4().hex + first = request( + "POST", + "/api/v1/users", + {"username": username, "display_name": "Первый", "password": password}, + ) self.assertEqual(first[0], 201) - second = request("POST", "/api/v1/users", {"username": username, "display_name": "Второй"}) + second = request( + "POST", + "/api/v1/users", + {"username": username, "display_name": "Второй", "password": password}, + ) self.assertEqual(second[0], 409) self.assertEqual(second[1]["error"]["code"], "conflict") + def test_short_password_rejected(self): + status, body = request( + "POST", + "/api/v1/users", + {"username": "u_short_" + uuid.uuid4().hex[:8], "display_name": "X", "password": "short"}, + ) + self.assertEqual(status, 400) + self.assertEqual(body["error"]["code"], "invalid_request") + + def test_wrong_password_returns_401(self): + bad = base64.b64encode( + (self.alice["user"]["username"] + ":wrong-password-123").encode() + ).decode() + status, body = request("POST", "/api/v1/auth/sessions", basic=bad) + self.assertEqual(status, 401) + self.assertEqual(body["error"]["code"], "unauthorized") + + def test_basic_not_accepted_on_business_endpoint(self): + status, body = request("GET", "/api/v1/users/me", basic=self.alice["basic"]) + self.assertEqual(status, 401) + self.assertEqual(body["error"]["code"], "unauthorized") + + def test_x_user_id_ignored(self): + status, body = request( + "GET", + "/api/v1/users/me", + token=self.alice["token"], + extra_headers={"X-User-Id": self.eve["user"]["id"]}, + ) + self.assertEqual(status, 200) + self.assertEqual(body["id"], self.alice["user"]["id"]) + + def test_logout_revokes_session(self): + user = make_user("logout") + status, _ = request("DELETE", "/api/v1/auth/sessions/current", token=user["token"]) + self.assertEqual(status, 204) + status, body = request("GET", "/api/v1/users/me", token=user["token"]) + self.assertEqual(status, 401) + self.assertEqual(body["error"]["code"], "unauthorized") + def test_pagination_walks_all_pages(self): chat = request( "POST", "/api/v1/chats", - {"title": "Пагинация", "member_ids": [self.alice["id"], self.bob["id"]]}, - user_id=self.alice["id"], + {"title": "Пагинация", "member_ids": [self.alice["user"]["id"], self.bob["user"]["id"]]}, + token=self.alice["token"], )[1] sent = [] for i in range(7): @@ -117,7 +179,7 @@ class MessengerTests(unittest.TestCase): "POST", "/api/v1/chats/" + chat["id"] + "/messages", {"text": "msg-%d" % i}, - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 201) sent.append(msg["id"]) @@ -127,7 +189,7 @@ class MessengerTests(unittest.TestCase): path = "/api/v1/chats/" + chat["id"] + "/messages?limit=2" if cursor: path += "&cursor=" + cursor - status, page = request("GET", path, user_id=self.bob["id"]) + status, page = request("GET", path, token=self.bob["token"]) self.assertEqual(status, 200) collected.extend(m["id"] for m in page["items"]) cursor = page["next_cursor"] @@ -139,26 +201,26 @@ class MessengerTests(unittest.TestCase): chat_a = request( "POST", "/api/v1/chats", - {"title": "Cursor A", "member_ids": [self.alice["id"], self.bob["id"]]}, - user_id=self.alice["id"], + {"title": "Cursor A", "member_ids": [self.alice["user"]["id"], self.bob["user"]["id"]]}, + token=self.alice["token"], )[1] chat_b = request( "POST", "/api/v1/chats", - {"title": "Cursor B", "member_ids": [self.alice["id"], self.bob["id"]]}, - user_id=self.alice["id"], + {"title": "Cursor B", "member_ids": [self.alice["user"]["id"], self.bob["user"]["id"]]}, + token=self.alice["token"], )[1] for i in range(3): request( "POST", "/api/v1/chats/" + chat_a["id"] + "/messages", {"text": "a-%d" % i}, - user_id=self.alice["id"], + token=self.alice["token"], ) status, page = request( "GET", "/api/v1/chats/" + chat_a["id"] + "/messages?limit=1", - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 200) cursor = page["next_cursor"] @@ -166,14 +228,14 @@ class MessengerTests(unittest.TestCase): status, body = request( "GET", "/api/v1/chats/" + chat_b["id"] + "/messages?limit=1&cursor=" + cursor, - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") def test_cursor_bound_to_endpoint(self): status, page = request( - "GET", "/api/v1/chats?limit=1", user_id=self.alice["id"] + "GET", "/api/v1/chats?limit=1", token=self.alice["token"] ) self.assertEqual(status, 200) cursor = page["next_cursor"] @@ -181,7 +243,7 @@ class MessengerTests(unittest.TestCase): status, body = request( "GET", "/api/v1/chats/" + self.chat["id"] + "/messages?limit=1&cursor=" + cursor, - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") @@ -191,25 +253,25 @@ class MessengerTests(unittest.TestCase): status, body = request( "GET", "/api/v1/chats/" + self.chat["id"] + "/messages?cursor=" + garbage, - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") def test_invalid_chat_id_returns_400(self): - status, body = request("GET", "/api/v1/chats/not-a-uuid", user_id=self.alice["id"]) + status, body = request("GET", "/api/v1/chats/not-a-uuid", token=self.alice["token"]) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") status, body = request( "POST", "/api/v1/chats/not-a-uuid/messages", {"text": "hi"}, - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") status, body = request( - "GET", "/api/v1/chats/not-a-uuid/messages", user_id=self.alice["id"] + "GET", "/api/v1/chats/not-a-uuid/messages", token=self.alice["token"] ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") @@ -218,7 +280,7 @@ class MessengerTests(unittest.TestCase): status, body = request( "POST", "/api/v1/users", - {"username": "abc\n", "display_name": "Имя"}, + {"username": "abc\n", "display_name": "Имя", "password": "T3st-" + uuid.uuid4().hex}, ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") @@ -228,7 +290,7 @@ class MessengerTests(unittest.TestCase): "POST", "/api/v1/users", raw=b"\xff", - user_id=self.alice["id"], + token=self.alice["token"], ) self.assertEqual(status, 400) self.assertEqual(body["error"]["code"], "invalid_request") @@ -239,8 +301,8 @@ class MessengerTests(unittest.TestCase): chat = request( "POST", "/api/v1/chats", - {"title": "Конкурентность", "member_ids": [self.alice["id"], self.bob["id"]]}, - user_id=self.alice["id"], + {"title": "Конкурентность", "member_ids": [self.alice["user"]["id"], self.bob["user"]["id"]]}, + token=self.alice["token"], )[1] errors = [] lock = threading.Lock() @@ -251,7 +313,7 @@ class MessengerTests(unittest.TestCase): "POST", "/api/v1/chats/" + chat["id"] + "/messages", {"text": "w%d-%d" % (worker_id, i)}, - user_id=self.alice["id"], + token=self.alice["token"], ) if status != 201: with lock: @@ -264,7 +326,7 @@ class MessengerTests(unittest.TestCase): t.join() self.assertEqual(errors, []) status, page = request( - "GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", user_id=self.alice["id"] + "GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", token=self.alice["token"] ) self.assertEqual(status, 200) ids = [m["id"] for m in page["items"]] -- 2.54.0 From eebecc74ff9589f25c20de56fa762c20177feeb8 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:31:22 +0300 Subject: [PATCH 03/12] =?UTF-8?q?fix:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D1=82=D1=8C=20=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8E,=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=B7=D1=83=D1=8F=20=D0=BF=D0=BE=D0=BB=D0=BD=D1=83=D1=8E=20?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20created=5Fat?= =?UTF-8?q?=20=D0=B2=20cursor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- app/handlers.py | 11 +++++++++-- app/store.py | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/handlers.py b/app/handlers.py index ad1f1d84..2f4ef276 100644 --- a/app/handlers.py +++ b/app/handlers.py @@ -161,6 +161,7 @@ def create_chat( if store.get_user(mid) is None: raise ApiError(400, "invalid_request", "Неизвестный участник чата") chat = store.create_chat(title, member_ids) + chat.pop("_cursor_created_at", None) return JSONResponse(chat, status_code=201) @@ -179,7 +180,9 @@ def list_chats( next_cursor = None if has_more and items: last = items[-1] - next_cursor = encode_cursor(last["created_at"], last["id"], scope) + next_cursor = encode_cursor(last["_cursor_created_at"], last["id"], scope) + for item in items: + item.pop("_cursor_created_at", None) return {"items": items, "next_cursor": next_cursor} @@ -197,6 +200,7 @@ def get_chat( chat = store.get_chat(chat_id) if chat is None: raise ApiError(404, "not_found", "Чат не найден") + chat.pop("_cursor_created_at", None) return chat @@ -214,6 +218,7 @@ def create_message( raise ApiError(404, "not_found", "Чат не найден") text = validate_create_message(body) message = store.add_message(chat_id, caller, text) + message.pop("_cursor_created_at", None) return JSONResponse(message, status_code=201) @@ -237,5 +242,7 @@ def list_messages( next_cursor = None if has_more and items: last = items[-1] - next_cursor = encode_cursor(last["created_at"], last["id"], scope) + next_cursor = encode_cursor(last["_cursor_created_at"], last["id"], scope) + for item in items: + item.pop("_cursor_created_at", None) return {"items": items, "next_cursor": next_cursor} diff --git a/app/store.py b/app/store.py index 65c21c02..3a4eed10 100644 --- a/app/store.py +++ b/app/store.py @@ -37,6 +37,8 @@ def _chat_row(row: tuple, member_ids: list[str]) -> dict[str, Any]: "title": title, "member_ids": member_ids, "created_at": _iso(created_at), + # Полное значение для keyset-cursor; handler вырезает перед отдачей. + "_cursor_created_at": created_at.isoformat(), } @@ -48,6 +50,8 @@ def _message_row(row: tuple) -> dict[str, Any]: "sender_id": str(sender_id), "text": text, "created_at": _iso(created_at), + # Полное значение для keyset-cursor; handler вырезает перед отдачей. + "_cursor_created_at": created_at.isoformat(), } -- 2.54.0 From ed7b43ecac2fd527acec3b698ce3c7242e72f859 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:38:19 +0300 Subject: [PATCH 04/12] =?UTF-8?q?fix:=20=D1=81=D0=BE=D1=85=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=82=D1=8C=20=D0=BC=D0=B8=D0=BA=D1=80=D0=BE=D1=81?= =?UTF-8?q?=D0=B5=D0=BA=D1=83=D0=BD=D0=B4=D1=8B=20=D0=B2=20created=5Fat=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=20keyset-=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- app/handlers.py | 11 ++--------- app/store.py | 7 ++----- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/app/handlers.py b/app/handlers.py index 2f4ef276..ad1f1d84 100644 --- a/app/handlers.py +++ b/app/handlers.py @@ -161,7 +161,6 @@ def create_chat( if store.get_user(mid) is None: raise ApiError(400, "invalid_request", "Неизвестный участник чата") chat = store.create_chat(title, member_ids) - chat.pop("_cursor_created_at", None) return JSONResponse(chat, status_code=201) @@ -180,9 +179,7 @@ def list_chats( next_cursor = None if has_more and items: last = items[-1] - next_cursor = encode_cursor(last["_cursor_created_at"], last["id"], scope) - for item in items: - item.pop("_cursor_created_at", None) + next_cursor = encode_cursor(last["created_at"], last["id"], scope) return {"items": items, "next_cursor": next_cursor} @@ -200,7 +197,6 @@ def get_chat( chat = store.get_chat(chat_id) if chat is None: raise ApiError(404, "not_found", "Чат не найден") - chat.pop("_cursor_created_at", None) return chat @@ -218,7 +214,6 @@ def create_message( raise ApiError(404, "not_found", "Чат не найден") text = validate_create_message(body) message = store.add_message(chat_id, caller, text) - message.pop("_cursor_created_at", None) return JSONResponse(message, status_code=201) @@ -242,7 +237,5 @@ def list_messages( next_cursor = None if has_more and items: last = items[-1] - next_cursor = encode_cursor(last["_cursor_created_at"], last["id"], scope) - for item in items: - item.pop("_cursor_created_at", None) + next_cursor = encode_cursor(last["created_at"], last["id"], scope) return {"items": items, "next_cursor": next_cursor} diff --git a/app/store.py b/app/store.py index 3a4eed10..cda7ea37 100644 --- a/app/store.py +++ b/app/store.py @@ -17,7 +17,8 @@ def new_id() -> str: def _iso(dt: datetime) -> str: - return dt.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + """RFC 3339 UTC с суффиксом Z, микросекунды сохраняются для keyset-пагинации.""" + return dt.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ") def _user_row(row: tuple) -> dict[str, Any]: @@ -37,8 +38,6 @@ def _chat_row(row: tuple, member_ids: list[str]) -> dict[str, Any]: "title": title, "member_ids": member_ids, "created_at": _iso(created_at), - # Полное значение для keyset-cursor; handler вырезает перед отдачей. - "_cursor_created_at": created_at.isoformat(), } @@ -50,8 +49,6 @@ def _message_row(row: tuple) -> dict[str, Any]: "sender_id": str(sender_id), "text": text, "created_at": _iso(created_at), - # Полное значение для keyset-cursor; handler вырезает перед отдачей. - "_cursor_created_at": created_at.isoformat(), } -- 2.54.0 From 1a08e3a6a348df0a13f62cd6f6a3d1d45104249c Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:39:40 +0300 Subject: [PATCH 05/12] =?UTF-8?q?fix:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D1=82=D1=8C=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=20204?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B8=20=D1=82=D0=B5=D0=BA=D1=83=D1=89=D0=B5=D0=B9=20?= =?UTF-8?q?=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- app/handlers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/handlers.py b/app/handlers.py index ad1f1d84..542b4a4d 100644 --- a/app/handlers.py +++ b/app/handlers.py @@ -5,7 +5,7 @@ import logging from typing import Any from fastapi import APIRouter, Body, Header, Query, Request -from fastapi.responses import JSONResponse +from fastapi.responses import JSONResponse, Response from .errors import ApiError from .security import hash_password, verify_password @@ -134,7 +134,7 @@ def revoke_session( if not token or get_sessions(request).resolve(token) is None: raise _unauthorized("Недействительная сессия", BEARER_CHALLENGE) get_sessions(request).revoke(token) - return JSONResponse(status_code=204) + return Response(status_code=204) @router.get("/api/v1/users/me") -- 2.54.0 From c104c4aa7ec67ac4554002a930c3a3ca0cd1d0a7 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:42:18 +0300 Subject: [PATCH 06/12] fix: fixed Makefile --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 738b7e98..114060ee 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -PYTHON ?= python3 +PYTHON ?= python BASE_URL ?= http://localhost:8080 CA_FILE ?= ACTION_SCRIPT ?= @@ -16,17 +16,17 @@ validate: $(PYTHON) scripts/validate_openapi.py test: - $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) + $(PYTHON) tests/smoke.py --base-url $(BASE_URL) $(TLS_ARGS) load: - $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) + $(PYTHON) tests/load.py --base-url $(BASE_URL) $(TLS_ARGS) test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) test-unit: - $(PYTHON) tests/test_unit.py --base-url "$(BASE_URL)" + $(PYTHON) tests/test_unit.py --base-url $(BASE_URL) up: docker compose --env-file .env up -d --build -- 2.54.0 From a71258d915668f059c6f3d1ce2f6d8e970c45b23 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:57:47 +0300 Subject: [PATCH 07/12] =?UTF-8?q?docs:=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B0=20=D0=B8=20=D0=BE=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B2=20=D0=BE=D1=82?= =?UTF-8?q?=D1=87=D1=91=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 4 ++-- app/__init__.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/REPORT.md b/REPORT.md index 8f2c966f..86697b38 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,7 +1,7 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка: **4** (трек 5 не заявляется) +- Автор, группа: Бушков Евгений, БВТ2301 +- Целевая оценка: **4** - Commit/PR: TODO после публикации - Использованные источники, библиотеки и помощь ИИ: официальная документация FastAPI, psycopg 3, psycopg_pool, redis-py, argon2-cffi, PostgreSQL; помощь ИИ — генерация каркаса кода и текста отчёта, проверка и адаптация вручную. diff --git a/app/__init__.py b/app/__init__.py index 7083a40c..5ee79cfb 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1 +1 @@ -"""MTUSI Messenger — лабораторная 1 (in-memory).""" +"""MTUSI Messenger — лабораторная 2.""" -- 2.54.0 From 0d63870225a2af9d862c7e5982da19ca03076610 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 18:57:49 +0300 Subject: [PATCH 08/12] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20expiry=20?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B8=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8?= =?UTF-8?q?=D0=BC=D0=BE=D1=81=D1=82=D0=B5=D0=B9,=20seed-history?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- Makefile | 11 ++- REPORT.md | 20 +++-- app/__init__.py | 2 +- app/handlers.py | 5 +- compose.yaml | 2 +- scripts/seed_history.py | 100 +++++++++++++++++++++++ scripts/start-postgres.sh | 14 ++++ scripts/start-redis.sh | 14 ++++ scripts/stop-postgres.sh | 4 + scripts/stop-redis.sh | 4 + tests/test_dependency_failure.py | 133 +++++++++++++++++++++++++++++++ tests/test_expiry.py | 77 ++++++++++++++++++ 12 files changed, 375 insertions(+), 11 deletions(-) create mode 100644 scripts/seed_history.py create mode 100644 scripts/start-postgres.sh create mode 100644 scripts/start-redis.sh create mode 100644 scripts/stop-postgres.sh create mode 100644 scripts/stop-redis.sh create mode 100644 tests/test_dependency_failure.py create mode 100644 tests/test_expiry.py diff --git a/Makefile b/Makefile index 114060ee..25abafc1 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-unit up down +.PHONY: help check validate test load test-persistence test-unit test-expiry test-deps seed-history up down help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" @@ -28,6 +28,15 @@ test-persistence: test-unit: $(PYTHON) tests/test_unit.py --base-url $(BASE_URL) +test-expiry: + $(PYTHON) tests/test_expiry.py --base-url $(BASE_URL) + +test-deps: + $(PYTHON) tests/test_dependency_failure.py --base-url $(BASE_URL) + +seed-history: + $(PYTHON) scripts/seed_history.py --base-url $(BASE_URL) --count 10000 --output evidence/explain.txt + up: docker compose --env-file .env up -d --build diff --git a/REPORT.md b/REPORT.md index 86697b38..de27cc62 100644 --- a/REPORT.md +++ b/REPORT.md @@ -16,8 +16,14 @@ docker compose --env-file .env up -d --build # дождаться /health/ready (healthcheck в compose) make test BASE_URL=http://localhost:8080 make test-unit BASE_URL=http://localhost:8080 -chmod +x scripts/restart.sh +chmod +x scripts/restart.sh scripts/stop-redis.sh scripts/start-redis.sh scripts/stop-postgres.sh scripts/start-postgres.sh make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://localhost:8080 +make test-deps BASE_URL=http://localhost:8080 +make seed-history BASE_URL=http://localhost:8080 +# Expiry — отдельный стенд с коротким TTL: +SESSION_TTL_SECONDS=2 docker compose --env-file .env up -d api +make test-expiry BASE_URL=http://localhost:8080 +docker compose --env-file .env up -d api # вернуть TTL=3600 ``` Версии образов закреплены: `postgres:16.4-alpine`, `redis:7.4-alpine`, `python:3.12-slim`. Миграции применяются автоматически при старте API (`app/db.py::apply_migrations`), идемпотентно через таблицу `schema_migrations`. @@ -47,12 +53,13 @@ make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://localhost | Ограниченный пул и таймауты | `app/db.py`, `app/main.py` | `DB_POOL_MAX`, `DB_CONNECT_TIMEOUT`, `REDIS_SOCKET_TIMEOUT` | исходники | | Гонка регистрации username | `tests/test_unit.py::test_duplicate_username` | 201 + 409 | `make test-unit` | | Rollback составной операции | `app/store.py::create_chat` — `conn.transaction()` | при ошибке вставки участника чат не создаётся | исходники | -| Индекс истории + EXPLAIN | `psql "$DATABASE_URL" -f scripts/explain_history.sql` | Index Scan по `messages_chat_created_idx` | вывод EXPLAIN | -| Expiry при коротком TTL | `SESSION_TTL_SECONDS=2 docker compose up -d api` + `make test-unit` | 401 после истечения | вывод консоли | +| Индекс истории + EXPLAIN | `make seed-history` (10 000 сообщений) | Index Scan по `messages_chat_created_idx` | `evidence/explain.txt` | +| Expiry при коротком TTL | `SESSION_TTL_SECONDS=2 docker compose up -d api` + `make test-expiry` | 401 после истечения, повторный вход работает | `tests/test_expiry.py` | | Logout | `tests/test_unit.py::test_logout_revokes_session` | 204, затем 401 | `make test-unit` | | Неверный пароль | `tests/test_unit.py::test_wrong_password_returns_401` | 401 | `make test-unit` | -| Недоступность Redis | `docker compose stop redis` + `make test` | smoke проходит (fallback на БД) | вывод консоли | -| Недоступность БД | `docker compose stop postgres` + `curl /health/ready` | 503 `unavailable` | вывод консоли | +| Недоступность Redis | `make test-deps` (тест `test_redis_down_falls_back_to_db`) | валидный токен 200, аноним 401, `/health/ready` 200 | `tests/test_dependency_failure.py` | +| Недоступность БД | `make test-deps` (тест `test_postgres_down_returns_503`) | `/health/ready` 503 `unavailable`, бизнес-запрос 500/503 без утечек | `tests/test_dependency_failure.py` | +| EXPLAIN на ≥10 000 сообщений | `make seed-history` | Index Scan по `messages_chat_created_idx` | `evidence/explain.txt` | ## Эксперименты и отказы @@ -65,7 +72,8 @@ Hardware: локальная машина разработчика (Windows 11, | `docker compose restart api postgres redis` (volumes сохранены) | данные и сессии сохранены | PASS persistence | ~5–10 с, потерь нет | | `docker compose stop redis` | API работает, сессии из БД | smoke PASS | восстановление после `start redis` | | `docker compose stop postgres` | `/health/ready` → 503 | 503 `unavailable` | после `start postgres` — 200 | -| `SESSION_TTL_SECONDS=2` | сессия истекает | 401 после истечения | — | +| `SESSION_TTL_SECONDS=2` | сессия истекает | 401 после истечения, повторный вход работает | — | +| `make seed-history` (10 000 сообщений) | Index Scan, без Seq Scan | Index Scan по `messages_chat_created_idx` | — | ## Ограничения и следующие шаги diff --git a/app/__init__.py b/app/__init__.py index 5ee79cfb..1aab4e3b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1 +1 @@ -"""MTUSI Messenger — лабораторная 2.""" +"""MTUSI Messenger — лабораторная 2: PostgreSQL, сессии и Redis-кэш.""" diff --git a/app/handlers.py b/app/handlers.py index 542b4a4d..29f76eea 100644 --- a/app/handlers.py +++ b/app/handlers.py @@ -23,6 +23,8 @@ from .validation import ( router = APIRouter() +logger = logging.getLogger("messenger.handlers") + BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"' BEARER_CHALLENGE = 'Bearer realm="messenger"' @@ -81,8 +83,7 @@ def health_ready(request: Request) -> JSONResponse: cur.execute("SELECT 1") cur.fetchone() except Exception as exc: # noqa: BLE001 - logger_ready = logging.getLogger("messenger.ready") - logger_ready.warning("readiness failed: %s", type(exc).__name__) + logger.warning("readiness failed: %s", type(exc).__name__) raise ApiError(503, "unavailable", "Зависимость недоступна") return JSONResponse({"status": "ok"}) diff --git a/compose.yaml b/compose.yaml index 601ceb4e..53bccc36 100644 --- a/compose.yaml +++ b/compose.yaml @@ -29,7 +29,7 @@ services: environment: APP_HOST: 0.0.0.0 APP_PORT: "8080" - SESSION_TTL_SECONDS: "3600" + SESSION_TTL_SECONDS: "${SESSION_TTL_SECONDS:-3600}" DATABASE_URL: postgresql://messenger:messenger@postgres:5432/messenger CACHE_URL: redis://redis:6379/0 ports: diff --git a/scripts/seed_history.py b/scripts/seed_history.py new file mode 100644 index 00000000..1a38d234 --- /dev/null +++ b/scripts/seed_history.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +"""Создаёт >=10 000 сообщений и снимает EXPLAIN (ANALYZE, BUFFERS). + +Запуск: python3 scripts/seed_history.py --count 10000 --output evidence/explain.txt +Требует запущенный API (BASE_URL) и доступ к psql через docker compose exec. +""" +import argparse +import base64 +import json +import os +import subprocess +import sys +import time +import urllib.error +import urllib.request +import uuid +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def request(base, method, path, body=None, token=None, basic=None): + headers = {"Accept": "application/json"} + if token: + headers["Authorization"] = "Bearer " + token + if basic: + headers["Authorization"] = "Basic " + basic + data = None + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode("utf-8") + headers["Content-Type"] = "application/json" + req = urllib.request.Request(base + path, data=data, headers=headers, method=method) + try: + with urllib.request.urlopen(req, timeout=30) as resp: + payload = resp.read() + return resp.status, json.loads(payload) if payload else None + except urllib.error.HTTPError as err: + payload = err.read() + return err.code, json.loads(payload) if payload else None + + +def main(): + p = argparse.ArgumentParser(description="Seed history and run EXPLAIN") + p.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080")) + p.add_argument("--count", type=int, default=10000) + p.add_argument("--output", default="evidence/explain.txt") + p.add_argument("--psql", default="docker compose exec -T postgres psql -U messenger -d messenger") + args = p.parse_args() + base = args.base_url.rstrip("/") + + username = "u_seed_" + uuid.uuid4().hex[:10] + password = "T3st-" + uuid.uuid4().hex + status, alice = request(base, "POST", "/api/v1/users", + {"username": username, "display_name": "Seed", "password": password}) + assert status == 201, (status, alice) + basic = base64.b64encode((username + ":" + password).encode()).decode() + status, session = request(base, "POST", "/api/v1/auth/sessions", basic=basic) + assert status == 201, (status, session) + token = session["token"] + + status, chat = request(base, "POST", "/api/v1/chats", + {"title": "Seed history", "member_ids": [alice["id"]]}, token=token) + assert status == 201, (status, chat) + chat_id = chat["id"] + + started = time.monotonic() + for i in range(args.count): + status, _ = request(base, "POST", "/api/v1/chats/" + chat_id + "/messages", + {"text": "seed-%d" % i}, token=token) + if status != 201: + raise SystemExit("failed to insert message %d: %s" % (i, status)) + if (i + 1) % 1000 == 0: + print("inserted %d/%d" % (i + 1, args.count), file=sys.stderr) + elapsed = time.monotonic() - started + print("inserted %d messages in %.1fs" % (args.count, elapsed), file=sys.stderr) + + sql = ( + "EXPLAIN (ANALYZE, BUFFERS) " + "SELECT id, chat_id, sender_id, text, created_at FROM messages " + "WHERE chat_id = '%s' " + "AND (created_at, id) > ('1970-01-01T00:00:00Z', '00000000-0000-0000-0000-000000000000') " + "ORDER BY created_at, id LIMIT 51;" % chat_id + ) + proc = subprocess.run(args.psql.split() + ["-c", sql], cwd=str(ROOT), + capture_output=True, text=True, timeout=120) + if proc.returncode != 0: + raise SystemExit("psql failed: " + proc.stderr[:500]) + out = proc.stdout + dest = (ROOT / args.output).resolve() + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_text( + "# chat_id=%s count=%d\n%s\n" % (chat_id, args.count, out), + encoding="utf-8", + ) + print(out) + print("saved to " + str(dest), file=sys.stderr) + + +if __name__ == "__main__": + main() diff --git a/scripts/start-postgres.sh b/scripts/start-postgres.sh new file mode 100644 index 00000000..7c1cb0fb --- /dev/null +++ b/scripts/start-postgres.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu +# Поднимает PostgreSQL обратно и ждёт готовности. +docker compose start postgres +i=0 +while [ "$i" -lt 60 ]; do + if docker compose exec -T postgres pg_isready -U messenger -d messenger >/dev/null 2>&1; then + exit 0 + fi + i=$((i+1)) + sleep 1 +done +echo "postgres did not become ready" >&2 +exit 1 diff --git a/scripts/start-redis.sh b/scripts/start-redis.sh new file mode 100644 index 00000000..02bc3e34 --- /dev/null +++ b/scripts/start-redis.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu +# Поднимает Redis обратно и ждёт готовности. +docker compose start redis +i=0 +while [ "$i" -lt 30 ]; do + if docker compose exec -T redis redis-cli ping >/dev/null 2>&1; then + exit 0 + fi + i=$((i+1)) + sleep 1 +done +echo "redis did not become ready" >&2 +exit 1 diff --git a/scripts/stop-postgres.sh b/scripts/stop-postgres.sh new file mode 100644 index 00000000..690e0601 --- /dev/null +++ b/scripts/stop-postgres.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +# Останавливает PostgreSQL, сохраняя volume. Используется тестом недоступности. +docker compose stop postgres diff --git a/scripts/stop-redis.sh b/scripts/stop-redis.sh new file mode 100644 index 00000000..a33998c2 --- /dev/null +++ b/scripts/stop-redis.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +# Останавливает Redis, сохраняя volume. Используется тестом недоступности. +docker compose stop redis diff --git a/tests/test_dependency_failure.py b/tests/test_dependency_failure.py new file mode 100644 index 00000000..1641b2fb --- /dev/null +++ b/tests/test_dependency_failure.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 +"""Проверка недоступности Redis и PostgreSQL. + +Управление контейнерами — через скрипты scripts/stop-*.sh / start-*.sh, +чтобы тест не зависел от Docker напрямую. +""" +import argparse +import base64 +import json +import os +import subprocess +import sys +import time +import unittest +import urllib.error +import urllib.request +import uuid +from pathlib import Path + +_parser = argparse.ArgumentParser(description="Dependency failure tests") +_parser.add_argument("--base-url", default=None) +_parser.add_argument("--stop-redis", default="scripts/stop-redis.sh") +_parser.add_argument("--start-redis", default="scripts/start-redis.sh") +_parser.add_argument("--stop-postgres", default="scripts/stop-postgres.sh") +_parser.add_argument("--start-postgres", default="scripts/start-postgres.sh") +_args, _unknown = _parser.parse_known_args() +sys.argv = [sys.argv[0]] + _unknown + +BASE_URL = (_args.base_url or os.environ.get("BASE_URL") or "http://localhost:8080").rstrip("/") +ROOT = Path(__file__).resolve().parents[1] + + +def run_script(path): + full = (ROOT / path).resolve() + if not full.is_file(): + raise unittest.SkipTest("script not found: " + str(full)) + proc = subprocess.run([str(full)], cwd=str(ROOT), capture_output=True, timeout=120) + if proc.returncode != 0: + raise AssertionError( + "script failed: " + path + " rc=" + str(proc.returncode) + + " stderr=" + proc.stderr.decode("utf-8", "replace")[:500] + ) + + +def request(method, path, body=None, token=None, basic=None): + headers = {"Accept": "application/json"} + if token: + headers["Authorization"] = "Bearer " + token + if basic: + headers["Authorization"] = "Basic " + basic + data = None + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode("utf-8") + headers["Content-Type"] = "application/json" + req = urllib.request.Request(BASE_URL + path, data=data, headers=headers, method=method) + try: + with urllib.request.urlopen(req, timeout=10) as resp: + payload = resp.read() + return resp.status, json.loads(payload) if payload else None + except urllib.error.HTTPError as err: + payload = err.read() + return err.code, json.loads(payload) if payload else None + + +def make_user(label): + username = "u_" + label + "_" + uuid.uuid4().hex[:10] + password = "T3st-" + uuid.uuid4().hex + status, user = request( + "POST", + "/api/v1/users", + {"username": username, "display_name": "Dep " + label, "password": password}, + ) + assert status == 201, (status, user) + basic = base64.b64encode((username + ":" + password).encode()).decode() + status, session = request("POST", "/api/v1/auth/sessions", basic=basic) + assert status == 201, (status, session) + return {"user": user, "token": session["token"]} + + +def wait_ready(timeout=60): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + status, _ = request("GET", "/health/ready") + if status == 200: + return + except Exception: + pass + time.sleep(0.5) + raise AssertionError("API did not become ready within " + str(timeout) + "s") + + +class DependencyFailureTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.alice = make_user("alice") + + def test_redis_down_falls_back_to_db(self): + run_script(_args.stop_redis) + try: + # Валидный токен продолжает работать (fallback на БД). + status, body = request("GET", "/api/v1/users/me", token=self.alice["token"]) + self.assertEqual(status, 200, body) + self.assertEqual(body["id"], self.alice["user"]["id"]) + # Аноним не принимается. + status, body = request("GET", "/api/v1/users/me") + self.assertEqual(status, 401) + self.assertEqual(body["error"]["code"], "unauthorized") + # /health/ready не зависит от Redis. + status, _ = request("GET", "/health/ready") + self.assertEqual(status, 200) + finally: + run_script(_args.start_redis) + wait_ready() + + def test_postgres_down_returns_503(self): + run_script(_args.stop_postgres) + try: + status, body = request("GET", "/health/ready") + self.assertEqual(status, 503) + self.assertEqual(body["error"]["code"], "unavailable") + # Бизнес-запрос не должен отдавать 200 и не должен раскрывать детали. + status, body = request("GET", "/api/v1/users/me", token=self.alice["token"]) + self.assertIn(status, (500, 503)) + self.assertIn("error", body) + self.assertNotIn("psycopg", json.dumps(body).lower()) + finally: + run_script(_args.start_postgres) + wait_ready() + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/tests/test_expiry.py b/tests/test_expiry.py new file mode 100644 index 00000000..9c2b65e9 --- /dev/null +++ b/tests/test_expiry.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +"""Проверка expiry сессии при коротком SESSION_TTL_SECONDS. + +Запускается против стенда с SESSION_TTL_SECONDS <= 10 (например, 2). +Если TTL больше — тест скипается, чтобы не ждать минуты. +""" +import argparse +import base64 +import json +import os +import sys +import time +import unittest +import urllib.error +import urllib.request +import uuid + +_parser = argparse.ArgumentParser(description="Expiry test") +_parser.add_argument("--base-url", default=None) +_parser.add_argument("--ttl", type=int, default=None) +_args, _unknown = _parser.parse_known_args() +sys.argv = [sys.argv[0]] + _unknown + +BASE_URL = (_args.base_url or os.environ.get("BASE_URL") or "http://localhost:8080").rstrip("/") +TTL = _args.ttl or int(os.environ.get("SESSION_TTL_SECONDS", "3600")) + + +def request(method, path, body=None, token=None, basic=None): + headers = {"Accept": "application/json"} + if token: + headers["Authorization"] = "Bearer " + token + if basic: + headers["Authorization"] = "Basic " + basic + data = None + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode("utf-8") + headers["Content-Type"] = "application/json" + req = urllib.request.Request(BASE_URL + path, data=data, headers=headers, method=method) + try: + with urllib.request.urlopen(req, timeout=10) as resp: + payload = resp.read() + return resp.status, json.loads(payload) if payload else None + except urllib.error.HTTPError as err: + payload = err.read() + return err.code, json.loads(payload) if payload else None + + +@unittest.skipIf(TTL > 10, "SESSION_TTL_SECONDS > 10; запустите стенд с коротким TTL") +class ExpiryTests(unittest.TestCase): + def test_session_expires(self): + username = "u_exp_" + uuid.uuid4().hex[:10] + password = "T3st-" + uuid.uuid4().hex + status, user = request( + "POST", + "/api/v1/users", + {"username": username, "display_name": "Expiry", "password": password}, + ) + self.assertEqual(status, 201, user) + basic = base64.b64encode((username + ":" + password).encode()).decode() + status, session = request("POST", "/api/v1/auth/sessions", basic=basic) + self.assertEqual(status, 201, session) + token = session["token"] + status, _ = request("GET", "/api/v1/users/me", token=token) + self.assertEqual(status, 200) + time.sleep(TTL + 2) + status, body = request("GET", "/api/v1/users/me", token=token) + self.assertEqual(status, 401) + self.assertEqual(body["error"]["code"], "unauthorized") + # Повторный вход с теми же credentials снова работает. + status, session2 = request("POST", "/api/v1/auth/sessions", basic=basic) + self.assertEqual(status, 201, session2) + status, _ = request("GET", "/api/v1/users/me", token=session2["token"]) + self.assertEqual(status, 200) + + +if __name__ == "__main__": + unittest.main(verbosity=2) -- 2.54.0 From 7ccdfb8460db5982abbb2d6317d073d17361de14 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 19:06:19 +0300 Subject: [PATCH 09/12] =?UTF-8?q?fix:=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81?= =?UTF-8?q?=D0=BA=D0=B0=D1=82=D1=8C=20.sh-=D1=81=D0=BA=D1=80=D0=B8=D0=BF?= =?UTF-8?q?=D1=82=D1=8B=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20sh=20=D0=BD?= =?UTF-8?q?=D0=B0=20Windows=20=D0=B2=20test-deps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- tests/test_dependency_failure.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_dependency_failure.py b/tests/test_dependency_failure.py index 1641b2fb..35ba2eb0 100644 --- a/tests/test_dependency_failure.py +++ b/tests/test_dependency_failure.py @@ -8,6 +8,7 @@ import argparse import base64 import json import os +import shutil import subprocess import sys import time @@ -30,11 +31,24 @@ BASE_URL = (_args.base_url or os.environ.get("BASE_URL") or "http://localhost:80 ROOT = Path(__file__).resolve().parents[1] +def _script_command(full: Path) -> list[str]: + """Windows не умеет запускать .sh напрямую — вызываем через sh (Git Bash/WSL).""" + if os.name == "nt": + sh = shutil.which("sh") or shutil.which("bash") + if not sh: + raise unittest.SkipTest( + "sh/bash not found in PATH; install Git for Windows or run under WSL" + ) + return [sh, str(full)] + return [str(full)] + + def run_script(path): full = (ROOT / path).resolve() if not full.is_file(): raise unittest.SkipTest("script not found: " + str(full)) - proc = subprocess.run([str(full)], cwd=str(ROOT), capture_output=True, timeout=120) + cmd = _script_command(full) + proc = subprocess.run(cmd, cwd=str(ROOT), capture_output=True, timeout=120) if proc.returncode != 0: raise AssertionError( "script failed: " + path + " rc=" + str(proc.returncode) -- 2.54.0 From f427565e27a411032f0917c4c3b4a94d658c0f3b Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 22:05:36 +0300 Subject: [PATCH 10/12] =?UTF-8?q?test:=20=D1=83=D1=82=D0=BE=D1=87=D0=BD?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=20=D0=BF=D1=80=D0=BE=D0=BF=D1=83=D1=81?= =?UTF-8?q?=D0=BA=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=20expiry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- tests/test_expiry.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_expiry.py b/tests/test_expiry.py index 9c2b65e9..187ac985 100644 --- a/tests/test_expiry.py +++ b/tests/test_expiry.py @@ -45,7 +45,11 @@ def request(method, path, body=None, token=None, basic=None): return err.code, json.loads(payload) if payload else None -@unittest.skipIf(TTL > 10, "SESSION_TTL_SECONDS > 10; запустите стенд с коротким TTL") +@unittest.skipIf( + TTL > 10, + "SESSION_TTL_SECONDS > 10; запустите стенд с коротким TTL и передайте " + "SESSION_TTL_SECONDS=2 make test-expiry", +) class ExpiryTests(unittest.TestCase): def test_session_expires(self): username = "u_exp_" + uuid.uuid4().hex[:10] -- 2.54.0 From 5898fdb07209043191eb74c26361ddbe0d718d74 Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 22:11:45 +0300 Subject: [PATCH 11/12] =?UTF-8?q?fix:=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=20=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D0=B5=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=B5=D1=81=D1=81?= =?UTF-8?q?=D0=B8=D0=B8=20=D1=83=D1=81=D1=82=D0=BE=D0=B9=D1=87=D0=B8=D0=B2?= =?UTF-8?q?=D1=8B=D0=BC=20=D0=BA=20TTL=20=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0=D0=B2=D0=B0=D1=82=D1=8C=20TTL=20=D1=87=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B7=20Makefile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (deepseek/deepseek-chat) --- Makefile | 2 +- tests/test_expiry.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 25abafc1..0af02316 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ test-unit: $(PYTHON) tests/test_unit.py --base-url $(BASE_URL) test-expiry: - $(PYTHON) tests/test_expiry.py --base-url $(BASE_URL) + $(PYTHON) tests/test_expiry.py --base-url $(BASE_URL) --ttl $(or $(SESSION_TTL_SECONDS),2) test-deps: $(PYTHON) tests/test_dependency_failure.py --base-url $(BASE_URL) diff --git a/tests/test_expiry.py b/tests/test_expiry.py index 187ac985..04e9151c 100644 --- a/tests/test_expiry.py +++ b/tests/test_expiry.py @@ -64,9 +64,21 @@ class ExpiryTests(unittest.TestCase): status, session = request("POST", "/api/v1/auth/sessions", basic=basic) self.assertEqual(status, 201, session) token = session["token"] + # Проверяем, что API реально запущен с коротким TTL: иначе тест + # будет ждать час и всё равно упадёт. + expires_at = session["expires_at"] + from datetime import datetime, timezone + exp = datetime.strptime(expires_at, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) + actual_ttl = (exp - datetime.now(timezone.utc)).total_seconds() + self.assertLessEqual( + actual_ttl, + 10, + "API запущен с TTL=" + str(int(actual_ttl)) + "s; перезапустите стенд с " + "SESSION_TTL_SECONDS=2 (например: SESSION_TTL_SECONDS=2 docker compose up -d api)", + ) status, _ = request("GET", "/api/v1/users/me", token=token) self.assertEqual(status, 200) - time.sleep(TTL + 2) + time.sleep(actual_ttl + 2) status, body = request("GET", "/api/v1/users/me", token=token) self.assertEqual(status, 401) self.assertEqual(body["error"]["code"], "unauthorized") -- 2.54.0 From 683e7ce9c669f6f3c5e75e8216cacb8bffc3267b Mon Sep 17 00:00:00 2001 From: Evgeny Bushkov Date: Fri, 23 Oct 2026 23:33:35 +0300 Subject: [PATCH 12/12] =?UTF-8?q?fix:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA=20SES?= =?UTF-8?q?SION=5FTTL=5FSECONDS=20=D0=B4=D0=BB=D1=8F=20PowerShell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 2 ++ evidence/explain.txt | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 evidence/explain.txt diff --git a/REPORT.md b/REPORT.md index de27cc62..77a9120f 100644 --- a/REPORT.md +++ b/REPORT.md @@ -22,6 +22,8 @@ make test-deps BASE_URL=http://localhost:8080 make seed-history BASE_URL=http://localhost:8080 # Expiry — отдельный стенд с коротким TTL: SESSION_TTL_SECONDS=2 docker compose --env-file .env up -d api +# Для PowerShell +$env:SESSION_TTL_SECONDS=2; docker compose --env-file .env up -d api make test-expiry BASE_URL=http://localhost:8080 docker compose --env-file .env up -d api # вернуть TTL=3600 ``` diff --git a/evidence/explain.txt b/evidence/explain.txt new file mode 100644 index 00000000..b9d8d9db --- /dev/null +++ b/evidence/explain.txt @@ -0,0 +1,15 @@ +# chat_id=5a01823c-52b2-4e4c-86be-855d220e160d count=10000 + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.29..5.61 rows=51 width=65) (actual time=0.019..0.029 rows=51 loops=1) + Buffers: shared hit=3 + -> Index Scan using messages_chat_created_idx on messages (cost=0.29..1041.68 rows=9967 width=65) (actual time=0.017..0.023 rows=51 loops=1) + Index Cond: ((chat_id = '5a01823c-52b2-4e4c-86be-855d220e160d'::uuid) AND (ROW(created_at, id) > ROW('1970-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Buffers: shared hit=3 + Planning: + Buffers: shared hit=108 + Planning Time: 0.751 ms + Execution Time: 0.097 ms +(9 rows) + + -- 2.54.0