From 5893d051c1febb0eb1362fca08683c7114aac50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=D1=81?= Date: Thu, 22 Oct 2026 22:45:34 +0300 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80?= =?UTF-8?q?=D1=8B=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=D0=B7=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Dockerfile | 27 ++++ app/__init__.py | 1 + app/config.py | 6 + app/dependencies.py | 26 ++++ app/errors.py | 131 ++++++++++++++++ app/main.py | 54 +++++++ app/middleware.py | 59 +++++++ app/routes/__init__.py | 0 app/routes/chats.py | 64 ++++++++ app/routes/health.py | 21 +++ app/routes/messages.py | 66 ++++++++ app/routes/users.py | 26 ++++ app/schemas.py | 149 ++++++++++++++++++ app/storage.py | 241 ++++++++++++++++++++++++++++ pytest.ini | 4 + requirements.txt | 6 + tests_student/conftest.py | 18 +++ tests_student/tests.py | 320 ++++++++++++++++++++++++++++++++++++++ 19 files changed, 1220 insertions(+) create mode 100644 Dockerfile create mode 100644 app/__init__.py create mode 100644 app/config.py create mode 100644 app/dependencies.py create mode 100644 app/errors.py create mode 100644 app/main.py create mode 100644 app/middleware.py create mode 100644 app/routes/__init__.py create mode 100644 app/routes/chats.py create mode 100644 app/routes/health.py create mode 100644 app/routes/messages.py create mode 100644 app/routes/users.py create mode 100644 app/schemas.py create mode 100644 app/storage.py create mode 100644 pytest.ini create mode 100644 requirements.txt create mode 100644 tests_student/conftest.py create mode 100644 tests_student/tests.py diff --git a/.gitignore b/.gitignore index b5a71896..e6a53fe9 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ bin/ obj/ coverage/ *.log +!evidence/*.log .DS_Store .idea/ .vscode/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..87010244 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + APP_HOST=0.0.0.0 \ + APP_PORT=8080 + +RUN groupadd -g 10001 appuser && \ + useradd -u 10001 -g appuser -s /sbin/nologin -M appuser + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY app/ ./app/ + +RUN chown -R appuser:appuser /app + +USER appuser + +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/live')" || exit 1 + +CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "10"] diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..6e6e089e --- /dev/null +++ b/app/__init__.py @@ -0,0 +1 @@ +"""Messenger application package.""" diff --git a/app/config.py b/app/config.py new file mode 100644 index 00000000..77c5f4bb --- /dev/null +++ b/app/config.py @@ -0,0 +1,6 @@ +import os + +APP_HOST = os.getenv("APP_HOST", "0.0.0.0") +APP_PORT = int(os.getenv("APP_PORT", "8080")) +APP_ENV = os.getenv("APP_ENV", "lab") +LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO") diff --git a/app/dependencies.py b/app/dependencies.py new file mode 100644 index 00000000..4b3eae08 --- /dev/null +++ b/app/dependencies.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +from uuid import UUID + +from fastapi import Header, Request + +from app.schemas import UserResponse +from app.storage import UnauthorizedError, storage + + +async def get_current_user( + request: Request, + x_user_id: str | None = Header(default=None, alias="X-User-Id"), +) -> UserResponse: + if not x_user_id or not x_user_id.strip(): + raise UnauthorizedError("Отсутствует заголовок X-User-Id") + + # валидация формата UUID + try: + UUID(x_user_id) + except ValueError: + raise UnauthorizedError("Неверный формат заголовка X-User-Id") + + # проверка существования пользователя + user = await storage.get_user_by_id(x_user_id) + return user diff --git a/app/errors.py b/app/errors.py new file mode 100644 index 00000000..6966c8cb --- /dev/null +++ b/app/errors.py @@ -0,0 +1,131 @@ +from __future__ import annotations + +import logging +import uuid + +from fastapi import FastAPI, Request, status +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from starlette.exceptions import HTTPException as StarletteHTTPException + +from app.schemas import ErrorCode, ErrorDetail, ErrorResponse +from app.storage import ( + ConflictError, + InvalidRequestError, + NotFoundError, + UnauthorizedError, +) + +logger = logging.getLogger("app.errors") + + +def get_request_id(request: Request) -> str: + req_id = getattr(request.state, "request_id", None) + if not req_id: + req_id = request.headers.get("X-Request-Id") or str(uuid.uuid4()) + return req_id + + +def make_error_response( + status_code: int, code: ErrorCode, message: str, request_id: str +) -> JSONResponse: + payload = ErrorResponse( + error=ErrorDetail(code=code, message=message, request_id=request_id) + ).model_dump() + return JSONResponse( + status_code=status_code, + content=payload, + headers={"X-Request-Id": request_id, "Content-Type": "application/json"}, + ) + + +def register_error_handlers(app: FastAPI) -> None: + @app.exception_handler(InvalidRequestError) + async def invalid_request_handler( + request: Request, exc: InvalidRequestError + ) -> JSONResponse: + req_id = get_request_id(request) + return make_error_response( + status.HTTP_400_BAD_REQUEST, ErrorCode.INVALID_REQUEST, str(exc), req_id + ) + + @app.exception_handler(UnauthorizedError) + async def unauthorized_handler( + request: Request, exc: UnauthorizedError + ) -> JSONResponse: + req_id = get_request_id(request) + return make_error_response( + status.HTTP_401_UNAUTHORIZED, ErrorCode.UNAUTHORIZED, str(exc), req_id + ) + + @app.exception_handler(NotFoundError) + async def not_found_handler(request: Request, exc: NotFoundError) -> JSONResponse: + req_id = get_request_id(request) + return make_error_response( + status.HTTP_404_NOT_FOUND, ErrorCode.NOT_FOUND, str(exc), req_id + ) + + @app.exception_handler(ConflictError) + async def conflict_handler(request: Request, exc: ConflictError) -> JSONResponse: + req_id = get_request_id(request) + return make_error_response( + status.HTTP_409_CONFLICT, ErrorCode.CONFLICT, str(exc), req_id + ) + + @app.exception_handler(RequestValidationError) + async def validation_error_handler( + request: Request, exc: RequestValidationError + ) -> JSONResponse: + req_id = get_request_id(request) + # Ошибки валидации входного JSON маппятся на 400 + errors = exc.errors() + messages = [] + for err in errors: + loc = ".".join(str(x) for x in err.get("loc", []) if x != "body") + msg = err.get("msg", "invalid") + messages.append(f"{loc}: {msg}" if loc else msg) + detail_msg = "; ".join(messages) or "Некорректное тело запроса или параметры" + return make_error_response( + status.HTTP_400_BAD_REQUEST, ErrorCode.INVALID_REQUEST, detail_msg, req_id + ) + + @app.exception_handler(StarletteHTTPException) + async def http_exception_handler( + request: Request, exc: StarletteHTTPException + ) -> JSONResponse: + req_id = get_request_id(request) + code = ErrorCode.INTERNAL_ERROR + if exc.status_code == 400: + code = ErrorCode.INVALID_REQUEST + elif exc.status_code == 401: + code = ErrorCode.UNAUTHORIZED + elif exc.status_code == 404: + code = ErrorCode.NOT_FOUND + elif exc.status_code == 409: + code = ErrorCode.CONFLICT + elif exc.status_code == 413: + code = ErrorCode.PAYLOAD_TOO_LARGE + elif exc.status_code == 415: + code = ErrorCode.UNSUPPORTED_MEDIA_TYPE + elif exc.status_code == 429: + code = ErrorCode.RATE_LIMITED + elif exc.status_code == 503: + code = ErrorCode.UNAVAILABLE + + msg = exc.detail if isinstance(exc.detail, str) else "HTTP ошибка" + return make_error_response(exc.status_code, code, msg, req_id) + + @app.exception_handler(Exception) + async def unhandled_exception_handler( + request: Request, exc: Exception + ) -> JSONResponse: + req_id = get_request_id(request) + logger.error( + "Unhandled exception for request %s: %s", req_id, exc, exc_info=True + ) + return make_error_response( + status.HTTP_500_INTERNAL_SERVER_ERROR, + ErrorCode.INTERNAL_ERROR, + "Внутренняя ошибка сервера", + req_id, + ) diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..96edbf8b --- /dev/null +++ b/app/main.py @@ -0,0 +1,54 @@ +from __future__ import annotations + +import logging +from contextlib import asynccontextmanager + +import uvicorn +from fastapi import FastAPI + +from app.config import APP_HOST, APP_PORT, LOG_LEVEL +from app.errors import register_error_handlers +from app.middleware import RequestIdAndLoggingMiddleware +from app.routes.chats import router as chats_router +from app.routes.health import router as health_router +from app.routes.messages import router as messages_router +from app.routes.users import router as users_router + +logger = logging.getLogger("app.lifecycle") + + +@asynccontextmanager +async def lifespan(app: FastAPI): + logger.info( + "Starting messenger server on %s:%d (env: %s)", APP_HOST, APP_PORT, LOG_LEVEL + ) + yield + logger.info("Graceful shutdown: closing connections and cleaning up...") + + +app = FastAPI( + title="highload-messenger", + version="1.1.0", + docs_url="/docs", + redoc_url=None, + lifespan=lifespan, +) + +app.add_middleware(RequestIdAndLoggingMiddleware) + +register_error_handlers(app) + +app.include_router(health_router) +app.include_router(users_router) +app.include_router(chats_router) +app.include_router(messages_router) + + +if __name__ == "__main__": + uvicorn.run( + "app.main:app", + host=APP_HOST, + port=APP_PORT, + log_level=LOG_LEVEL.lower(), + timeout_graceful_shutdown=10, + ) diff --git a/app/middleware.py b/app/middleware.py new file mode 100644 index 00000000..765092a3 --- /dev/null +++ b/app/middleware.py @@ -0,0 +1,59 @@ +from __future__ import annotations + +import logging +import sys +import time +import uuid + +from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint +from starlette.requests import Request +from starlette.responses import Response + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", + stream=sys.stdout, +) +logger = logging.getLogger("app.access") + + +class RequestIdAndLoggingMiddleware(BaseHTTPMiddleware): + async def dispatch( + self, request: Request, call_next: RequestResponseEndpoint + ) -> Response: + # Получаем X-Request-Id или генерируем новый + request_id = request.headers.get("X-Request-Id") + if not request_id or not request_id.strip(): + request_id = str(uuid.uuid4()) + + request.state.request_id = request_id + start_time = time.monotonic() + + try: + response = await call_next(request) + except Exception: + # Исключение перехватится exception_handler + duration_ms = (time.monotonic() - start_time) * 1000 + logger.error( + "%s %s -> 500 error | duration=%.2fms | request_id=%s", + request.method, + request.url.path, + duration_ms, + request_id, + ) + raise + + duration_ms = (time.monotonic() - start_time) * 1000 + response.headers["X-Request-Id"] = request_id + + # лог без логирования тела запросов и текстов сообщений + logger.info( + "%s %s -> %d | duration=%.2fms | request_id=%s", + request.method, + request.url.path, + response.status_code, + duration_ms, + request_id, + ) + + return response diff --git a/app/routes/__init__.py b/app/routes/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/routes/chats.py b/app/routes/chats.py new file mode 100644 index 00000000..d59927fc --- /dev/null +++ b/app/routes/chats.py @@ -0,0 +1,64 @@ +from __future__ import annotations + +from uuid import UUID + +from fastapi import APIRouter, Depends, Query, status + +from app.dependencies import get_current_user +from app.schemas import ( + ChatPageResponse, + ChatResponse, + CreateChatRequest, + UserResponse, +) +from app.storage import InvalidRequestError, storage + +router = APIRouter(prefix="/api/v1/chats", tags=["chats"]) + + +def validate_uuid(value: str, field_name: str = "id") -> str: + try: + UUID(value) + return value + except ValueError as exc: + raise InvalidRequestError(f"Поле '{field_name}' не является валидным UUID") from exc + + +@router.post("", response_model=ChatResponse, status_code=status.HTTP_201_CREATED) +async def create_chat( + body: CreateChatRequest, + current_user: UserResponse = Depends(get_current_user), +) -> ChatResponse: + """Создание нового чата.""" + chat = await storage.create_chat( + caller_id=current_user.id, + title=body.title, + member_ids=body.member_ids, + ) + return chat + + +@router.get("", response_model=ChatPageResponse, status_code=status.HTTP_200_OK) +async def list_chats( + limit: int = Query(default=50, ge=1, le=100), + cursor: str | None = Query(default=None), + current_user: UserResponse = Depends(get_current_user), +) -> ChatPageResponse: + """Список чатов, в которых состоит текущий пользователь.""" + items, next_cursor = await storage.list_chats( + caller_id=current_user.id, + limit=limit, + cursor=cursor, + ) + return ChatPageResponse(items=items, next_cursor=next_cursor) + + +@router.get("/{chat_id}", response_model=ChatResponse, status_code=status.HTTP_200_OK) +async def get_chat( + chat_id: str, + current_user: UserResponse = Depends(get_current_user), +) -> ChatResponse: + """Получение информации о чате по ID.""" + valid_id = validate_uuid(chat_id, "chat_id") + chat = await storage.get_chat(caller_id=current_user.id, chat_id=valid_id) + return chat diff --git a/app/routes/health.py b/app/routes/health.py new file mode 100644 index 00000000..b807f23e --- /dev/null +++ b/app/routes/health.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +from fastapi import APIRouter, status + +from app.schemas import HealthResponse + +router = APIRouter(tags=["health"]) + + +@router.get( + "/health/live", response_model=HealthResponse, status_code=status.HTTP_200_OK +) +async def health_live() -> HealthResponse: + return HealthResponse(status="ok") + + +@router.get( + "/health/ready", response_model=HealthResponse, status_code=status.HTTP_200_OK +) +async def health_ready() -> HealthResponse: + return HealthResponse(status="ok") diff --git a/app/routes/messages.py b/app/routes/messages.py new file mode 100644 index 00000000..695b4a3f --- /dev/null +++ b/app/routes/messages.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +from uuid import UUID + +from fastapi import APIRouter, Depends, Query, status + +from app.dependencies import get_current_user +from app.schemas import ( + CreateMessageRequest, + MessagePageResponse, + MessageResponse, + UserResponse, +) +from app.storage import InvalidRequestError, storage + +router = APIRouter(prefix="/api/v1/chats", tags=["messages"]) + + +def validate_uuid(value: str, field_name: str = "id") -> str: + try: + UUID(value) + return value + except ValueError as exc: + raise InvalidRequestError(f"Поле '{field_name}' не является валидным UUID") from exc + + +@router.post( + "/{chat_id}/messages", + response_model=MessageResponse, + status_code=status.HTTP_201_CREATED, +) +async def create_message( + chat_id: str, + body: CreateMessageRequest, + current_user: UserResponse = Depends(get_current_user), +) -> MessageResponse: + """Отправка сообщения в чат.""" + valid_id = validate_uuid(chat_id, "chat_id") + msg = await storage.create_message( + caller_id=current_user.id, + chat_id=valid_id, + text=body.text, + ) + return msg + + +@router.get( + "/{chat_id}/messages", + response_model=MessagePageResponse, + status_code=status.HTTP_200_OK, +) +async def list_messages( + chat_id: str, + limit: int = Query(default=50, ge=1, le=100), + cursor: str | None = Query(default=None), + current_user: UserResponse = Depends(get_current_user), +) -> MessagePageResponse: + """Получение истории сообщений чата с keyset-пагинацией.""" + valid_id = validate_uuid(chat_id, "chat_id") + items, next_cursor = await storage.list_messages( + caller_id=current_user.id, + chat_id=valid_id, + limit=limit, + cursor=cursor, + ) + return MessagePageResponse(items=items, next_cursor=next_cursor) diff --git a/app/routes/users.py b/app/routes/users.py new file mode 100644 index 00000000..61ee0d0a --- /dev/null +++ b/app/routes/users.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +from fastapi import APIRouter, Depends, status + +from app.dependencies import get_current_user +from app.schemas import CreateUserRequest, UserResponse +from app.storage import storage + +router = APIRouter(prefix="/api/v1/users", tags=["users"]) + + +@router.post("", response_model=UserResponse, status_code=status.HTTP_201_CREATED) +async def create_user(body: CreateUserRequest) -> UserResponse: + """Регистрация нового пользователя.""" + user = await storage.create_user( + username=body.username, display_name=body.display_name + ) + return user + + +@router.get("/me", response_model=UserResponse, status_code=status.HTTP_200_OK) +async def get_me( + current_user: UserResponse = Depends(get_current_user), +) -> UserResponse: + """Получение профиля текущего пользователя.""" + return current_user diff --git a/app/schemas.py b/app/schemas.py new file mode 100644 index 00000000..2fe32687 --- /dev/null +++ b/app/schemas.py @@ -0,0 +1,149 @@ +from __future__ import annotations + +import datetime as dt +import re +from enum import Enum +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +def now_utc_iso() -> str: + """Возвращает текущее время в формате RFC 3339 UTC с суффиксом Z.""" + return dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ") + + +class ErrorCode(str, Enum): + INVALID_REQUEST = "invalid_request" + UNAUTHORIZED = "unauthorized" + NOT_FOUND = "not_found" + CONFLICT = "conflict" + PAYLOAD_TOO_LARGE = "payload_too_large" + UNSUPPORTED_MEDIA_TYPE = "unsupported_media_type" + RATE_LIMITED = "rate_limited" + UNAVAILABLE = "unavailable" + INTERNAL_ERROR = "internal_error" + + +class ErrorDetail(BaseModel): + model_config = ConfigDict(extra="forbid") + + code: ErrorCode + message: str = Field(min_length=1) + request_id: str = Field(min_length=1) + + +class ErrorResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + error: ErrorDetail + + +class HealthResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + status: str = Field(default="ok") + + +class CreateUserRequest(BaseModel): + model_config = ConfigDict(extra="forbid") + + username: str = Field(min_length=3, max_length=32) + display_name: str = Field(min_length=1, max_length=100) + + @field_validator("username") + @classmethod + def validate_username(cls, v: str) -> str: + if not re.match(r"^[a-z0-9_]{3,32}\Z", v): + raise ValueError("Имя пользователя должно соответствовать шаблону ^[a-z0-9_]{3,32}$") + return v + + @field_validator("display_name") + @classmethod + def validate_display_name(cls, v: str) -> str: + if not v.strip(): + raise ValueError("Отображаемое имя не может состоять только из пробелов") + return v + + +class UserResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + id: str + username: str + display_name: str + created_at: str + + +class CreateChatRequest(BaseModel): + model_config = ConfigDict(extra="forbid") + + title: str = Field(min_length=1, max_length=100) + member_ids: list[str] = Field(min_length=1, max_length=100) + + @field_validator("title") + @classmethod + def validate_title(cls, v: str) -> str: + if not v.strip(): + raise ValueError("Название не может состоять только из пробелов") + return v + + @field_validator("member_ids") + @classmethod + def validate_member_ids(cls, v: list[str]) -> list[str]: + # Проверка уникальности + if len(v) != len(set(v)): + raise ValueError("Идентификаторы участников должны быть уникальными") + # Проверка формата каждого UUID + for member_id in v: + try: + UUID(member_id) + except ValueError: + raise ValueError(f"Идентификатор участника '{member_id}' не является валидным UUID") + return v + + +class ChatResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + id: str + title: str + member_ids: list[str] + created_at: str + + +class ChatPageResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + items: list[ChatResponse] + next_cursor: str | None = None + + +class CreateMessageRequest(BaseModel): + model_config = ConfigDict(extra="forbid") + + text: str = Field(min_length=1, max_length=4000) + + @field_validator("text") + @classmethod + def validate_text(cls, v: str) -> str: + if not v.strip(): + raise ValueError("Текст сообщения не может состоять только из пробелов") + return v + + +class MessageResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + id: str + chat_id: str + sender_id: str + text: str + created_at: str + + +class MessagePageResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + items: list[MessageResponse] + next_cursor: str | None = None diff --git a/app/storage.py b/app/storage.py new file mode 100644 index 00000000..84fbd214 --- /dev/null +++ b/app/storage.py @@ -0,0 +1,241 @@ +from __future__ import annotations + +import asyncio +import base64 +import json +import uuid + +from app.schemas import ( + ChatResponse, + MessageResponse, + UserResponse, + now_utc_iso, +) + + +class StorageError(Exception): + pass + + +class InvalidRequestError(StorageError): + pass + + +class NotFoundError(StorageError): + pass + + +class ConflictError(StorageError): + pass + + +class UnauthorizedError(StorageError): + pass + + +def encode_cursor(data: dict) -> str: + raw = json.dumps(data, separators=(",", ":")).encode("utf-8") + return base64.urlsafe_b64encode(raw).decode("utf-8").rstrip("=") + + +def decode_cursor(cursor_str: str) -> dict: + if not isinstance(cursor_str, str) or not cursor_str.strip(): + raise InvalidRequestError("Некорректный курсор") + try: + padded = cursor_str + "=" * (-len(cursor_str) % 4) + raw = base64.urlsafe_b64decode(padded.encode("utf-8")) + data = json.loads(raw.decode("utf-8")) + if not isinstance(data, dict): + raise InvalidRequestError("Некорректный курсор") + return data + except InvalidRequestError: + raise + except Exception as exc: + raise InvalidRequestError("Некорректный курсор") from exc + + +class InMemoryStorage: + def __init__(self) -> None: + self._lock = asyncio.Lock() + self._users_by_id: dict[str, UserResponse] = {} + self._users_by_username: dict[str, UserResponse] = {} + self._chats_by_id: dict[str, ChatResponse] = {} + self._messages_by_chat: dict[str, list[MessageResponse]] = {} + + async def create_user(self, username: str, display_name: str) -> UserResponse: + async with self._lock: + if username in self._users_by_username: + raise ConflictError("Имя пользователя уже занято") + + user_id = str(uuid.uuid4()) + created_at = now_utc_iso() + user = UserResponse( + id=user_id, + username=username, + display_name=display_name, + created_at=created_at, + ) + self._users_by_id[user_id] = user + self._users_by_username[username] = user + return user + + async def get_user_by_id(self, user_id: str) -> UserResponse: + async with self._lock: + user = self._users_by_id.get(user_id) + if not user: + raise UnauthorizedError("Пользователь не найден или не авторизован") + return user + + async def create_chat( + self, caller_id: str, title: str, member_ids: list[str] + ) -> ChatResponse: + async with self._lock: + # Вызывающий обязан быть в списке участников + if caller_id not in member_ids: + raise InvalidRequestError("Создатель чата должен быть в списке участников member_ids") + + # Все участники обязаны существовать + for mid in member_ids: + if mid not in self._users_by_id: + raise InvalidRequestError(f"Пользователь {mid} не существует") + + chat_id = str(uuid.uuid4()) + created_at = now_utc_iso() + chat = ChatResponse( + id=chat_id, + title=title, + member_ids=list(member_ids), + created_at=created_at, + ) + self._chats_by_id[chat_id] = chat + self._messages_by_chat[chat_id] = [] + return chat + + async def get_chat(self, caller_id: str, chat_id: str) -> ChatResponse: + async with self._lock: + chat = self._chats_by_id.get(chat_id) + # Если чата нет или вызывающий не участник — строго 404 + if not chat or caller_id not in chat.member_ids: + raise NotFoundError("Чат не найден") + return chat + + async def list_chats( + self, caller_id: str, limit: int = 50, cursor: str | None = None + ) -> tuple[list[ChatResponse], str | None]: + async with self._lock: + # Фильтруем только чаты, где пользователь — участник + user_chats = [ + chat + for chat in self._chats_by_id.values() + if caller_id in chat.member_ids + ] + # Сортировка по (created_at, id) по возрастанию + user_chats.sort(key=lambda x: (x.created_at, x.id)) + + start_idx = 0 + if cursor is not None: + c_data = decode_cursor(cursor) + if c_data.get("type") != "chats": + raise InvalidRequestError("Курсор недействителен для списка чатов") + c_created = c_data.get("created_at") + c_id = c_data.get("id") + if ( + not isinstance(c_created, str) + or not isinstance(c_id, str) + or not c_created.strip() + or not c_id.strip() + ): + raise InvalidRequestError("Поврежденный курсор") + + # Ищем первый элемент, строго больший курсора + start_idx = len(user_chats) + for idx, chat in enumerate(user_chats): + if (chat.created_at, chat.id) > (c_created, c_id): + start_idx = idx + break + + items = user_chats[start_idx : start_idx + limit] + has_next = (start_idx + limit) < len(user_chats) + next_cursor = None + if has_next and items: + last = items[-1] + next_cursor = encode_cursor( + {"type": "chats", "created_at": last.created_at, "id": last.id} + ) + + return items, next_cursor + + async def create_message( + self, caller_id: str, chat_id: str, text: str + ) -> MessageResponse: + async with self._lock: + chat = self._chats_by_id.get(chat_id) + if not chat or caller_id not in chat.member_ids: + raise NotFoundError("Чат не найден") + + message_id = str(uuid.uuid4()) + created_at = now_utc_iso() + msg = MessageResponse( + id=message_id, + chat_id=chat_id, + sender_id=caller_id, + text=text, + created_at=created_at, + ) + self._messages_by_chat[chat_id].append(msg) + return msg + + async def list_messages( + self, caller_id: str, chat_id: str, limit: int = 50, cursor: str | None = None + ) -> tuple[list[MessageResponse], str | None]: + async with self._lock: + chat = self._chats_by_id.get(chat_id) + if not chat or caller_id not in chat.member_ids: + raise NotFoundError("Чат не найден") + + all_msgs = self._messages_by_chat.get(chat_id, []) + # Сортировка по (created_at, id) по возрастанию + sorted_msgs = sorted(all_msgs, key=lambda x: (x.created_at, x.id)) + + start_idx = 0 + if cursor is not None: + c_data = decode_cursor(cursor) + if c_data.get("type") != "messages" or c_data.get("chat_id") != chat_id: + raise InvalidRequestError( + "Курсор принадлежит другому чату или недействителен" + ) + c_created = c_data.get("created_at") + c_id = c_data.get("id") + if ( + not isinstance(c_created, str) + or not isinstance(c_id, str) + or not c_created.strip() + or not c_id.strip() + ): + raise InvalidRequestError("Поврежденный курсор") + + start_idx = len(sorted_msgs) + for idx, msg in enumerate(sorted_msgs): + if (msg.created_at, msg.id) > (c_created, c_id): + start_idx = idx + break + + items = sorted_msgs[start_idx : start_idx + limit] + has_next = (start_idx + limit) < len(sorted_msgs) + next_cursor = None + if has_next and items: + last = items[-1] + next_cursor = encode_cursor( + { + "type": "messages", + "chat_id": chat_id, + "created_at": last.created_at, + "id": last.id, + } + ) + + return items, next_cursor + + +# Глобальный экземпляр хранилища для процесса +storage = InMemoryStorage() diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..e279b86b --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +pythonpath = . +asyncio_mode = auto +python_files = test_*.py *_test.py tests.py test.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..b5285407 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +fastapi==0.141.1 +uvicorn[standard]==0.52.4 +pydantic==2.13.5 +pytest==9.1.1 +pytest-asyncio==1.4.0 +httpx==0.28.1 diff --git a/tests_student/conftest.py b/tests_student/conftest.py new file mode 100644 index 00000000..82d1cb23 --- /dev/null +++ b/tests_student/conftest.py @@ -0,0 +1,18 @@ +import os + +import pytest +from httpx import ASGITransport, AsyncClient + +from app.main import app + + +@pytest.fixture +async def client(): + base_url = os.getenv("BASE_URL", "http://testserver") + if base_url.startswith("http://testserver"): + transport = ASGITransport(app=app) + async with AsyncClient(transport=transport, base_url="http://testserver") as ac: + yield ac + else: + async with AsyncClient(base_url=base_url) as ac: + yield ac diff --git a/tests_student/tests.py b/tests_student/tests.py new file mode 100644 index 00000000..ba79c1e6 --- /dev/null +++ b/tests_student/tests.py @@ -0,0 +1,320 @@ +import asyncio +import uuid + +import pytest +from httpx import AsyncClient + + +@pytest.mark.asyncio +async def test_invalid_json_and_extra_fields(client: AsyncClient): + """Проверка невалидного JSON и запрета лишних полей (400 invalid_request).""" + # 1. Невалидный синтаксис JSON + res_malformed = await client.post( + "/api/v1/users", + content="not a valid json {", + headers={"Content-Type": "application/json"}, + ) + assert res_malformed.status_code == 400 + err_malformed = res_malformed.json() + assert err_malformed["error"]["code"] == "invalid_request" + assert "request_id" in err_malformed["error"] + assert ( + res_malformed.headers.get("X-Request-Id") + == err_malformed["error"]["request_id"] + ) + + # 2. Лишние поля (additionalProperties: false) + res_extra = await client.post( + "/api/v1/users", + json={ + "username": f"u_{uuid.uuid4().hex[:8]}", + "display_name": "test_user", + "unexpected_field": "hacker_payload", + }, + ) + assert res_extra.status_code == 400 + err_extra = res_extra.json() + assert err_extra["error"]["code"] == "invalid_request" + + # 3. Запрет завершающего перевода строки в username ("abc\n") + res_newline = await client.post( + "/api/v1/users", + json={"username": "abc\n", "display_name": "test_newline"}, + ) + assert res_newline.status_code == 400 + assert res_newline.json()["error"]["code"] == "invalid_request" + + # 4. Валидация курсора: не-dict JSON (W10= -> []) и пустой cursor= + user_res = ( + await client.post( + "/api/v1/users", + json={"username": f"u_{uuid.uuid4().hex[:8]}", "display_name": "test_cursor_val"}, + ) + ).json() + headers = {"X-User-Id": user_res["id"]} + + # cursor=W10= (декодируется в list [], а не dict) + res_w10 = await client.get("/api/v1/chats?cursor=W10=", headers=headers) + assert res_w10.status_code == 400 + assert res_w10.json()["error"]["code"] == "invalid_request" + + # пустой cursor= + res_empty_cursor = await client.get("/api/v1/chats?cursor=", headers=headers) + assert res_empty_cursor.status_code == 400 + assert res_empty_cursor.json()["error"]["code"] == "invalid_request" + + +@pytest.mark.asyncio +async def test_outsider_chat_access_is_404(client: AsyncClient): + """Проверка того, что доступ к чужому чату возвращает строго 404 (а не 403).""" + # Создаём двух участников чата + test_user1 = ( + await client.post( + "/api/v1/users", + json={"username": f"u1_{uuid.uuid4().hex[:8]}", "display_name": "test_user1"}, + ) + ).json() + test_user2 = ( + await client.post( + "/api/v1/users", + json={"username": f"u2_{uuid.uuid4().hex[:8]}", "display_name": "test_user2"}, + ) + ).json() + # Создаём стороннего пользователя + test_user3 = ( + await client.post( + "/api/v1/users", + json={"username": f"u3_{uuid.uuid4().hex[:8]}", "display_name": "test_user3"}, + ) + ).json() + + # Первый пользователь создаёт чат со вторым + chat = ( + await client.post( + "/api/v1/chats", + headers={"X-User-Id": test_user1["id"]}, + json={"title": "Private Chat", "member_ids": [test_user1["id"], test_user2["id"]]}, + ) + ).json() + + chat_id = chat["id"] + + # Сторонний пользователь пытается получить чат по ID -> 404 + res_chat = await client.get( + f"/api/v1/chats/{chat_id}", headers={"X-User-Id": test_user3["id"]} + ) + assert res_chat.status_code == 404 + assert res_chat.json()["error"]["code"] == "not_found" + + # Сторонний пользователь пытается отправить сообщение в чужой чат -> 404 + res_post = await client.post( + f"/api/v1/chats/{chat_id}/messages", + headers={"X-User-Id": test_user3["id"]}, + json={"text": "Привет извне"}, + ) + assert res_post.status_code == 404 + assert res_post.json()["error"]["code"] == "not_found" + + # Сторонний пользователь пытается прочитать сообщения чужого чата -> 404 + res_msgs = await client.get( + f"/api/v1/chats/{chat_id}/messages", headers={"X-User-Id": test_user3["id"]} + ) + assert res_msgs.status_code == 404 + assert res_msgs.json()["error"]["code"] == "not_found" + + +@pytest.mark.asyncio +async def test_empty_and_whitespace_message(client: AsyncClient): + """Проверка отправки пустого сообщения или сообщения только из пробелов (400).""" + user = ( + await client.post( + "/api/v1/users", + json={"username": f"u_{uuid.uuid4().hex[:8]}", "display_name": "test_user"}, + ) + ).json() + chat = ( + await client.post( + "/api/v1/chats", + headers={"X-User-Id": user["id"]}, + json={"title": "Chat", "member_ids": [user["id"]]}, + ) + ).json() + + # Пустая строка + res_empty = await client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers={"X-User-Id": user["id"]}, + json={"text": ""}, + ) + assert res_empty.status_code == 400 + assert res_empty.json()["error"]["code"] == "invalid_request" + + # Только пробелы и переводы строк + res_spaces = await client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers={"X-User-Id": user["id"]}, + json={"text": " \t \n "}, + ) + assert res_spaces.status_code == 400 + assert res_spaces.json()["error"]["code"] == "invalid_request" + + +@pytest.mark.asyncio +async def test_duplicate_username_conflict(client: AsyncClient): + """Проверка регистрации пользователя с уже занятым username (409).""" + uname = f"unique_{uuid.uuid4().hex[:8]}" + res1 = await client.post( + "/api/v1/users", json={"username": uname, "display_name": "test_user1"} + ) + assert res1.status_code == 201 + + res2 = await client.post( + "/api/v1/users", json={"username": uname, "display_name": "test_user2"} + ) + assert res2.status_code == 409 + err = res2.json() + assert err["error"]["code"] == "conflict" + + +@pytest.mark.asyncio +async def test_multi_page_cursor_pagination(client: AsyncClient): + """Проверка обхода нескольких страниц по keyset-курсору.""" + user = ( + await client.post( + "/api/v1/users", + json={"username": f"pag_{uuid.uuid4().hex[:8]}", "display_name": "test_user"}, + ) + ).json() + chat = ( + await client.post( + "/api/v1/chats", + headers={"X-User-Id": user["id"]}, + json={"title": "Pagination Chat", "member_ids": [user["id"]]}, + ) + ).json() + chat_id = chat["id"] + + # Отправляем 5 сообщений + sent_texts = [f"Message #{i}" for i in range(1, 6)] + created_messages = [] + for txt in sent_texts: + res = await client.post( + f"/api/v1/chats/{chat_id}/messages", + headers={"X-User-Id": user["id"]}, + json={"text": txt}, + ) + assert res.status_code == 201 + created_messages.append(res.json()) + + # Страница 1: limit=2 + p1 = ( + await client.get( + f"/api/v1/chats/{chat_id}/messages?limit=2", + headers={"X-User-Id": user["id"]}, + ) + ).json() + assert len(p1["items"]) == 2 + assert p1["next_cursor"] is not None + + # Страница 2: limit=2 с курсором со страницы 1 + p2 = ( + await client.get( + f"/api/v1/chats/{chat_id}/messages?limit=2&cursor={p1['next_cursor']}", + headers={"X-User-Id": user["id"]}, + ) + ).json() + assert len(p2["items"]) == 2 + assert p2["next_cursor"] is not None + + # Страница 3: limit=2 с курсором со страницы 2 -> остался 1 элемент, next_cursor == None + p3 = ( + await client.get( + f"/api/v1/chats/{chat_id}/messages?limit=2&cursor={p2['next_cursor']}", + headers={"X-User-Id": user["id"]}, + ) + ).json() + assert len(p3["items"]) == 1 + assert p3["next_cursor"] is None + + # Проверяем, что все 5 сообщений собраны без повторов и потерь + all_retrieved = p1["items"] + p2["items"] + p3["items"] + assert len(all_retrieved) == 5 + assert [m["id"] for m in all_retrieved] == [m["id"] for m in created_messages] + + +@pytest.mark.asyncio +async def test_concurrency_race_condition(client: AsyncClient): + """ + Защита состояния от гонок: + Параллельно отправляем 100 сообщений от 10 клиентов, + затем получаем ровно 100 уникальных сообщений без повреждений. + """ + # 1. Создаём 10 клиентов + users = [] + for i in range(10): + u = ( + await client.post( + "/api/v1/users", + json={ + "username": f"conc_{i}_{uuid.uuid4().hex[:6]}", + "display_name": f"test_user_{i}", + }, + ) + ).json() + users.append(u) + + user_ids = [u["id"] for u in users] + + # 2. Создаём чат со всеми 10 участниками + chat = ( + await client.post( + "/api/v1/chats", + headers={"X-User-Id": user_ids[0]}, + json={"title": "Highload Concurrency Chat", "member_ids": user_ids}, + ) + ).json() + chat_id = chat["id"] + + # 3. Каждый из 10 клиентов отправляет по 10 сообщений параллельно (итого 100 сообщений) + async def send_batch(user, client_idx): + tasks = [] + for msg_idx in range(10): + text = f"client-{client_idx}-msg-{msg_idx}" + tasks.append( + client.post( + f"/api/v1/chats/{chat_id}/messages", + headers={"X-User-Id": user["id"]}, + json={"text": text}, + ) + ) + return await asyncio.gather(*tasks) + + # Запускаем отправку от всех 10 клиентов одновременно + batch_results = await asyncio.gather(*[send_batch(users[i], i) for i in range(10)]) + + # Проверяем, что все 100 запросов завершились со статусом 201 Created + all_responses = [resp for batch in batch_results for resp in batch] + assert len(all_responses) == 100 + for resp in all_responses: + assert resp.status_code == 201 + + # 4. Читаем все сообщения из чата (limit=100) + history_res = await client.get( + f"/api/v1/chats/{chat_id}/messages?limit=100", + headers={"X-User-Id": user_ids[0]}, + ) + assert history_res.status_code == 200 + history = history_res.json() + items = history["items"] + + # параллельно отправим 100 сообщений от 10 клиентов, затем получаем ровно 100 уникальных сообщений + assert len(items) == 100, f"Expected 100 messages, received {len(items)}" + unique_ids = {m["id"] for m in items} + assert len(unique_ids) == 100, "Found duplicate message IDs" + + # Проверяем монотонную сортировку (created_at, id) + for idx in range(len(items) - 1): + assert (items[idx]["created_at"], items[idx]["id"]) <= ( + items[idx + 1]["created_at"], + items[idx + 1]["id"], + ) -- 2.54.0 From 9ddb2244bc07d62d9208bd36ea457f4938d52f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=D1=81?= Date: Fri, 23 Oct 2026 12:45:58 +0300 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=D0=9B=D0=A02=20-=20PostgreSQL,=20R?= =?UTF-8?q?edis=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8=20=D0=B8=20keyset-?= =?UTF-8?q?=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 24 +- REPORT.md | 119 ++++++- app/auth.py | 27 ++ app/config.py | 37 ++- app/dependencies.py | 38 ++- app/errors.py | 61 +++- app/main.py | 9 +- app/middleware.py | 15 + app/routes/auth.py | 63 ++++ app/routes/health.py | 10 + app/routes/messages.py | 2 +- app/routes/users.py | 4 +- app/schemas.py | 32 +- app/storage.py | 599 +++++++++++++++++++++++++--------- compose.yaml | 66 ++++ evidence/docker_services.log | 4 + evidence/explain_index.log | 13 + evidence/persistence_test.log | 3 + evidence/smoke_test.log | 2 + evidence/student_tests.log | 28 ++ infra/init.sql | 35 ++ requirements.txt | 3 + scripts/restart.sh | 5 + tests_student/conftest.py | 9 +- tests_student/tests.py | 547 ++++++++++++++++++++----------- 25 files changed, 1332 insertions(+), 423 deletions(-) create mode 100644 app/auth.py create mode 100644 app/routes/auth.py create mode 100644 compose.yaml create mode 100644 evidence/docker_services.log create mode 100644 evidence/explain_index.log create mode 100644 evidence/persistence_test.log create mode 100644 evidence/smoke_test.log create mode 100644 evidence/student_tests.log create mode 100644 infra/init.sql create mode 100755 scripts/restart.sh diff --git a/Makefile b/Makefile index 9ecb223e..faa1577e 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,15 @@ PYTHON ?= python3 BASE_URL ?= http://localhost:8080 CA_FILE ?= -ACTION_SCRIPT ?= +ACTION_SCRIPT ?= scripts/restart.sh 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-student up down restart help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "test-student: run student tests (pytest); test-persistence: test stack restart with data preservation" + @echo "up: build and start docker compose stack; down: stop stack; restart: restart stack" check: $(PYTHON) scripts/check_contract.py @@ -16,11 +18,23 @@ validate: $(PYTHON) scripts/validate_openapi.py test: - $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) + no_proxy=localhost,127.0.0.1 NO_PROXY=localhost,127.0.0.1 $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) load: - $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) + no_proxy=localhost,127.0.0.1 NO_PROXY=localhost,127.0.0.1 $(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) + no_proxy=localhost,127.0.0.1 NO_PROXY=localhost,127.0.0.1 $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-student: + no_proxy=localhost,127.0.0.1 NO_PROXY=localhost,127.0.0.1 BASE_URL="$(BASE_URL)" $(PYTHON) -m pytest tests_student/ -v + +up: + docker compose up -d --build + +down: + docker compose down + +restart: + docker compose restart diff --git a/REPORT.md b/REPORT.md index e42f2b05..b3d0175a 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,122 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Герейханов Ильяс, БВТ2301 +- Целевая оценка: 4 +- Commit/PR: ветка `solution/bvt2301-Gereykhanov`. PR: `ЛР 2 — БВТ2301 — Герейханов — на 4` +- Использованные источники, библиотеки и помощь ИИ: Python 3.12, FastAPI, Uvicorn, PostgreSQL 16, Redis 7, asyncpg, argon2-cffi, Pydantic, Pytest, Docker Compose; документация PostgreSQL, Redis, RFC 7617 (Basic Auth), RFC 6750 (Bearer Token). ИИ использовался для генерации кода тестов pytest, отладки конфигурации Docker Compose и формулирования сценариев проверки персистентности данных. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +1. **Версии runtime и базовых образов:** + - Локальный runtime: Linux, Docker 29.9.1, Docker Compose 5.6.0 + - API контейнер: `python:3.12-slim` + - PostgreSQL: `postgres:16-alpine` + - Redis: `redis:7-alpine` + +2. **Подготовка окружения и запуск стека:** + ```sh + git checkout solution/bvt2301-Gereykhanov + cp .env.example .env + + # Сборка и запуск всех сервисов + make up + ``` + +3. **Ожидание готовности и запуск тестов:** + ```sh + # Проверка smoke теста + make test + + # Проверка сценария сохранности данных и сессий при рестарте + make test-persistence ACTION_SCRIPT=scripts/restart.sh + + # Запуск собственных расширенных тестов + make test-student + ``` + +4. **Остановка и удаление контейнеров:** + ```sh + # Остановка контейнеров с сохранением постоянных volumes + make down + + # Остановка контейнеров с удалением volumes + docker compose down -v + ``` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +- **Схема процессов и хранилищ:** + - Клиент $\to$ HTTP REST API (FastAPI, порт 8080) $\to$ PostgreSQL 16 (данные пользователей, чатов, сообщений) + Redis 7 (сессии пользователей). + - Вся инфраструктура разворачивается через `compose.yaml`. +- **Источник истины:** + - **PostgreSQL 16**: постоянное хранение состояния на именованном Docker volume `pg_data`. + - **Redis 7**: хранение токенов сессий с TTL на Docker volume `redis_data` с флагом `--appendonly yes` (AOF-журналирование обеспечивает сохранность сессий при падении или перезапуске). +- **Безопасность и аутентификация:** + - Пароли хэшируются KDF **Argon2id** (`argon2-cffi`). + - Вход в систему выполняется через `POST /api/v1/auth/sessions` с заголовком `Authorization: Basic `. + - Сессионный токен генерируется с энтропией 256 бит (`secrets.token_urlsafe(32)`). + - Бизнес-эндпоинты защищены через `Authorization: Bearer `. Заголовок `X-User-Id` из первой лабы игнорируется и не даёт доступа. + - Логаут (`DELETE /api/v1/auth/sessions/current`) отзывает токен из Redis (повторные запросы с этим токеном получают 401). + - При ошибке 401 сервер возвращает заголовок `WWW-Authenticate`: для эндпоинта логина `Basic realm="messenger", charset="UTF-8"`, для остальных эндпоинтов `Bearer realm="messenger"`. +- **Курсорная пагинация и индексы:** + - Порядок истории строго детерминирован составным ключом `(created_at, id)` по возрастанию. + - В PostgreSQL создан составной B-Tree индекс: + `CREATE INDEX idx_messages_cursor ON messages (chat_id, created_at ASC, id ASC);` + - Поиск страницы выполняется через SQL-условие `WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at ASC, id ASC LIMIT $4`. База делает `Index Scan` без полного перебора таблицы. +- **Транзакции и работа с БД:** + - Создание чата и добавление участников выполняется атомарно внутри транзакции `async with conn.transaction():`. Если хотя бы один участник не найден, происходит rollback, пустые или повреждённые чаты в базу не попадают. + - Подключение к PostgreSQL управляется пулом `asyncpg` (от 2 до 20 соединений). Все запросы параметризованы через `$1, $2`. +- **Что изменилось относительно ЛР1:** + - Из первой лабы перенесена базовая структура роутов FastAPI, схемы Pydantic и Dockerfile. + - Хранилище в оперативной памяти Python полностью заменено на постоянные PostgreSQL 16 и Redis 7. + - Заголовок `X-User-Id` удалён и заменён на полноценную авторизацию по сессиям. ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test` | `PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.` | `evidence/smoke_test.log` | +| Сохранение данных и сессий при рестарте | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | `PASS: persistence preserved confirmed data and existing session.` | `evidence/persistence_test.log` | +| Негативные тесты | `make test-student` | 16 тестов проходят: пустые сообщения, пароли из пробелов, запрет тела в auth sessions, Basic Auth битый base64, чужой курсор 400, дубликаты, чужой чат 404, неверный пароль, отзыв сессии, гонка 100 сообщений | `evidence/student_tests.log` | +| B-Tree Index Scan на 17 000+ сообщениях | `EXPLAIN (ANALYZE, BUFFERS)` в PostgreSQL | `Index Scan using idx_messages_cursor on messages`. Execution Time: 0.109 ms, без `Seq Scan` | `evidence/explain_index.log` | +| Redis сессии с AOF-персистентностью | `docker exec highload-redis-lab2 redis-cli config get appendonly` | Сессии сохраняются на `redis_data` volume и выдерживают рестарт контейнеров | `evidence/docker_services.log` | +| Защита от гонок: 100 сообщений от 10 клиентов | `pytest tests_student/ -k test_concurrency_race_condition` | Все 100 сообщений успешно записаны и прочитаны, 100 уникальных ID, строгий порядок | `evidence/student_tests.log` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +- **Профиль нагрузки:** скрипт `tests/load.py`, закрытый цикл параллельных POST-запросов отправки сообщений. +- **Параметры:** длительность 30 секунд, 4 параллельных потока +- **Результаты нагрузки:** + ```json + { + "lab": 2, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30, + "elapsed_seconds": 30.008, + "concurrency": 4, + "successful_requests": 17657, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 588.401, + "successful_latency_ms": { + "p50": 6.535, + "p95": 8.599, + "p99": 10.335 + } + } + ``` -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +| Воздействие | Что ожидаем | Что наблюдали | Время восстановления | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Рестарт стека `docker compose restart` во время активной сессии | Сохранение подтверждённых данных в Postgres и токенов сессий в Redis; восстановление API $\le$ 90 сек | Контейнеры перезапустились за 2.3 сек, токен юзера сохранил валидность, история сообщений не повреждена | Восстановление за 2.3 сек, потеря подтверждённых данных: 0 | +| Обращение с отозванным токеном после logout | HTTP 401 с заголовком `WWW-Authenticate: Bearer` | 401 Unauthorized | Потеря данных отсутствует, токен недействителен | +| Неверный пароль при входе | HTTP 401 с заголовком `WWW-Authenticate: Basic` | 401 Unauthorized, токен не выпускается | Безопасность соблюдена | -## Ограничения и следующие шаги +## Ограничения -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- **Какие отказы выдерживаются и какие нет:** + 1. *Выдерживаются:* перезапуск любого или всех контейнеров (`docker compose restart`). Все подтверждённые данные в Postgres и токены сессий в Redis выживают благодаря томам `pg_data` и `redis_data`. Конкурентная нагрузка и параллельная запись сообщений без гонок. + 2. *Не выдерживаются:* удаление Docker volumes через `docker compose down -v`, физический выход диска из строя, падение хоста во время активной записи без репликации. +- **Учебные упрощения:** + 1. *Одиночные инстансы:* Postgres, Redis и API запущены в одном экземпляре без репликации и кластеризации. + 2. *Конфигурация:* учетные данные базы зафиксированы в `compose.yaml` для простоты воспроизведения проверки. diff --git a/app/auth.py b/app/auth.py new file mode 100644 index 00000000..3035feeb --- /dev/null +++ b/app/auth.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +import secrets +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError + +_hasher = PasswordHasher() + + +def hash_password(password: str) -> str: + """Хэширует пароль с использованием Argon2id.""" + return _hasher.hash(password) + + +def verify_password(hash_str: str, password: str) -> bool: + """Проверяет пароль на соответствие хэшу Argon2id.""" + try: + return _hasher.verify(hash_str, password) + except VerifyMismatchError: + return False + except Exception: + return False + + +def generate_session_token() -> str: + """Генерирует криптографически стойкий непрозрачный токен сессии (256 бит энтропии).""" + return secrets.token_urlsafe(32) diff --git a/app/config.py b/app/config.py index 77c5f4bb..96df5c0e 100644 --- a/app/config.py +++ b/app/config.py @@ -1,6 +1,33 @@ -import os +from __future__ import annotations -APP_HOST = os.getenv("APP_HOST", "0.0.0.0") -APP_PORT = int(os.getenv("APP_PORT", "8080")) -APP_ENV = os.getenv("APP_ENV", "lab") -LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO") +import os +from pathlib import Path + + +def _get_val(env_name: str, file_env_name: str | None = None, default: str = "") -> str: + if file_env_name and file_env_name in os.environ: + file_path = Path(os.environ[file_env_name]) + if file_path.is_file(): + return file_path.read_text(encoding="utf-8").strip() + return os.getenv(env_name, default) + + +APP_HOST: str = os.getenv("APP_HOST", "0.0.0.0") +APP_PORT: int = int(os.getenv("APP_PORT", "8080")) +APP_ENV: str = os.getenv("APP_ENV", "lab") +LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO") + +SESSION_TTL_SECONDS: int = int(os.getenv("SESSION_TTL_SECONDS", "3600")) +SESSION_STORE: str = os.getenv("SESSION_STORE", "redis").lower() + +DATABASE_URL: str = _get_val( + "DATABASE_URL", + "DATABASE_URL_FILE", + "postgresql://postgres:postgres@postgres:5432/messenger", +) + +CACHE_URL: str = _get_val( + "CACHE_URL", + "CACHE_URL_FILE", + "redis://redis:6379/0", +) diff --git a/app/dependencies.py b/app/dependencies.py index 4b3eae08..53563c58 100644 --- a/app/dependencies.py +++ b/app/dependencies.py @@ -1,26 +1,30 @@ from __future__ import annotations -from uuid import UUID - -from fastapi import Header, Request +from fastapi import Depends, Header, Request +from app.errors import UnauthorizedError from app.schemas import UserResponse -from app.storage import UnauthorizedError, storage +from app.storage import storage -async def get_current_user( - request: Request, - x_user_id: str | None = Header(default=None, alias="X-User-Id"), -) -> UserResponse: - if not x_user_id or not x_user_id.strip(): - raise UnauthorizedError("Отсутствует заголовок X-User-Id") +async def get_current_token( + authorization: str | None = Header(default=None, alias="Authorization"), +) -> str: + if not authorization or not authorization.strip(): + raise UnauthorizedError("Отсутствует заголовок Authorization") - # валидация формата UUID - try: - UUID(x_user_id) - except ValueError: - raise UnauthorizedError("Неверный формат заголовка X-User-Id") + parts = authorization.strip().split(maxsplit=1) + if len(parts) != 2 or parts[0].lower() != "bearer": + raise UnauthorizedError("Неверная схема авторизации (ожидается Bearer)") - # проверка существования пользователя - user = await storage.get_user_by_id(x_user_id) + token = parts[1].strip() + if not token: + raise UnauthorizedError("Пустой Bearer токен") + return token + + +async def get_current_user(token: str = Depends(get_current_token)) -> UserResponse: + user = await storage.get_session_user(token) + if not user: + raise UnauthorizedError("Недействительная или истекшая сессия") return user diff --git a/app/errors.py b/app/errors.py index 6966c8cb..46f84598 100644 --- a/app/errors.py +++ b/app/errors.py @@ -9,12 +9,27 @@ from fastapi.responses import JSONResponse from starlette.exceptions import HTTPException as StarletteHTTPException from app.schemas import ErrorCode, ErrorDetail, ErrorResponse -from app.storage import ( - ConflictError, - InvalidRequestError, - NotFoundError, - UnauthorizedError, -) + + +class StorageError(Exception): + pass + + +class InvalidRequestError(StorageError): + pass + + +class NotFoundError(StorageError): + pass + + +class ConflictError(StorageError): + pass + + +class UnauthorizedError(StorageError): + pass + logger = logging.getLogger("app.errors") @@ -27,15 +42,22 @@ def get_request_id(request: Request) -> str: def make_error_response( - status_code: int, code: ErrorCode, message: str, request_id: str + status_code: int, + code: ErrorCode, + message: str, + request_id: str, + headers: dict[str, str] | None = None, ) -> JSONResponse: payload = ErrorResponse( error=ErrorDetail(code=code, message=message, request_id=request_id) ).model_dump() + resp_headers = {"X-Request-Id": request_id, "Content-Type": "application/json"} + if headers: + resp_headers.update(headers) return JSONResponse( status_code=status_code, content=payload, - headers={"X-Request-Id": request_id, "Content-Type": "application/json"}, + headers=resp_headers, ) @@ -54,8 +76,17 @@ def register_error_handlers(app: FastAPI) -> None: request: Request, exc: UnauthorizedError ) -> JSONResponse: req_id = get_request_id(request) + scheme = ( + 'Basic realm="messenger", charset="UTF-8"' + if request.url.path == "/api/v1/auth/sessions" + else 'Bearer realm="messenger"' + ) return make_error_response( - status.HTTP_401_UNAUTHORIZED, ErrorCode.UNAUTHORIZED, str(exc), req_id + status.HTTP_401_UNAUTHORIZED, + ErrorCode.UNAUTHORIZED, + str(exc), + req_id, + headers={"WWW-Authenticate": scheme}, ) @app.exception_handler(NotFoundError) @@ -77,7 +108,6 @@ def register_error_handlers(app: FastAPI) -> None: request: Request, exc: RequestValidationError ) -> JSONResponse: req_id = get_request_id(request) - # Ошибки валидации входного JSON маппятся на 400 errors = exc.errors() messages = [] for err in errors: @@ -95,10 +125,17 @@ def register_error_handlers(app: FastAPI) -> None: ) -> JSONResponse: req_id = get_request_id(request) code = ErrorCode.INTERNAL_ERROR + extra_headers: dict[str, str] = {} if exc.status_code == 400: code = ErrorCode.INVALID_REQUEST elif exc.status_code == 401: code = ErrorCode.UNAUTHORIZED + scheme = ( + 'Basic realm="messenger", charset="UTF-8"' + if request.url.path == "/api/v1/auth/sessions" + else 'Bearer realm="messenger"' + ) + extra_headers["WWW-Authenticate"] = scheme elif exc.status_code == 404: code = ErrorCode.NOT_FOUND elif exc.status_code == 409: @@ -113,7 +150,9 @@ def register_error_handlers(app: FastAPI) -> None: code = ErrorCode.UNAVAILABLE msg = exc.detail if isinstance(exc.detail, str) else "HTTP ошибка" - return make_error_response(exc.status_code, code, msg, req_id) + return make_error_response( + exc.status_code, code, msg, req_id, headers=extra_headers + ) @app.exception_handler(Exception) async def unhandled_exception_handler( diff --git a/app/main.py b/app/main.py index 96edbf8b..87686eb1 100644 --- a/app/main.py +++ b/app/main.py @@ -9,10 +9,12 @@ from fastapi import FastAPI from app.config import APP_HOST, APP_PORT, LOG_LEVEL from app.errors import register_error_handlers from app.middleware import RequestIdAndLoggingMiddleware +from app.routes.auth import router as auth_router from app.routes.chats import router as chats_router from app.routes.health import router as health_router from app.routes.messages import router as messages_router from app.routes.users import router as users_router +from app.storage import storage logger = logging.getLogger("app.lifecycle") @@ -22,13 +24,15 @@ async def lifespan(app: FastAPI): logger.info( "Starting messenger server on %s:%d (env: %s)", APP_HOST, APP_PORT, LOG_LEVEL ) + await storage.connect() yield - logger.info("Graceful shutdown: closing connections and cleaning up...") + logger.info("Graceful shutdown: closing connections...") + await storage.disconnect() app = FastAPI( title="highload-messenger", - version="1.1.0", + version="1.2.0", docs_url="/docs", redoc_url=None, lifespan=lifespan, @@ -39,6 +43,7 @@ app.add_middleware(RequestIdAndLoggingMiddleware) register_error_handlers(app) app.include_router(health_router) +app.include_router(auth_router) app.include_router(users_router) app.include_router(chats_router) app.include_router(messages_router) diff --git a/app/middleware.py b/app/middleware.py index 765092a3..bb0746b9 100644 --- a/app/middleware.py +++ b/app/middleware.py @@ -29,6 +29,21 @@ class RequestIdAndLoggingMiddleware(BaseHTTPMiddleware): request.state.request_id = request_id start_time = time.monotonic() + if request.method in ("POST", "PUT", "PATCH") and request.url.path != "/api/v1/auth/sessions": + ct = request.headers.get("content-type") + if ct: + ct_clean = ct.split(";")[0].strip().lower() + if ct_clean != "application/json": + from fastapi import status + from app.errors import make_error_response + from app.schemas import ErrorCode + return make_error_response( + status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, + ErrorCode.UNSUPPORTED_MEDIA_TYPE, + f"Неподдерживаемый тип содержимого '{ct}'. Ожидается application/json", + request_id, + ) + try: response = await call_next(request) except Exception: diff --git a/app/routes/auth.py b/app/routes/auth.py new file mode 100644 index 00000000..9c60f36d --- /dev/null +++ b/app/routes/auth.py @@ -0,0 +1,63 @@ +from __future__ import annotations + +import base64 +from fastapi import APIRouter, Depends, Header, Request, Response, status + +from app.dependencies import get_current_token +from app.errors import InvalidRequestError, UnauthorizedError +from app.schemas import SessionResponse +from app.storage import storage + +router = APIRouter(prefix="/api/v1/auth/sessions", tags=["auth"]) + + +@router.post("", response_model=SessionResponse, status_code=status.HTTP_201_CREATED) +async def create_session( + request: Request, + authorization: str | None = Header(default=None, alias="Authorization"), +) -> SessionResponse: + """Аутентификация пользователя через Basic Auth и создание новой сессии.""" + body = await request.body() + if body and body.strip(): + raise InvalidRequestError("Тело запроса для создания сессии должно быть пустым") + + if not authorization or not authorization.strip(): + raise UnauthorizedError("Отсутствует заголовок Authorization") + + parts = authorization.strip().split(maxsplit=1) + if len(parts) != 2 or parts[0].lower() != "basic": + raise UnauthorizedError("Неверная схема авторизации (ожидается Basic)") + + try: + raw_bytes = base64.b64decode(parts[1].strip()) + raw_str = raw_bytes.decode("utf-8") + if ":" not in raw_str: + raise UnauthorizedError("Некорректный формат Basic credentials") + username, password = raw_str.split(":", 1) + except Exception: + raise UnauthorizedError("Некорректный заголовок Basic авторизации") + + user = await storage.authenticate_user(username, password) + if not user: + raise UnauthorizedError("Неверное имя пользователя или пароль") + + session = await storage.create_session(user) + return session + + +@router.delete("/current", status_code=status.HTTP_204_NO_CONTENT) +async def delete_current_session( + request: Request, + token: str = Depends(get_current_token), +) -> Response: + """Отзыв текущей сессии.""" + body = await request.body() + if body and body.strip(): + raise InvalidRequestError("Тело запроса для отзыва сессии должно быть пустым") + + user = await storage.get_session_user(token) + if not user: + raise UnauthorizedError("Недействительная или уже отозванная сессия") + + await storage.delete_session(token) + return Response(status_code=status.HTTP_204_NO_CONTENT) diff --git a/app/routes/health.py b/app/routes/health.py index b807f23e..3e475270 100644 --- a/app/routes/health.py +++ b/app/routes/health.py @@ -1,8 +1,10 @@ from __future__ import annotations from fastapi import APIRouter, status +from starlette.exceptions import HTTPException as StarletteHTTPException from app.schemas import HealthResponse +from app.storage import storage router = APIRouter(tags=["health"]) @@ -11,6 +13,7 @@ router = APIRouter(tags=["health"]) "/health/live", response_model=HealthResponse, status_code=status.HTTP_200_OK ) async def health_live() -> HealthResponse: + """Проверка жизни процесса (Liveness).""" return HealthResponse(status="ok") @@ -18,4 +21,11 @@ async def health_live() -> HealthResponse: "/health/ready", response_model=HealthResponse, status_code=status.HTTP_200_OK ) async def health_ready() -> HealthResponse: + """Проверка готовности сервиса и его зависимостей (Readiness).""" + is_ready = await storage.check_health() + if not is_ready: + raise StarletteHTTPException( + status_code=status.HTTP_503_SERVICE_UNAVAILABLE, + detail="Сервис или его хранилища недоступны", + ) return HealthResponse(status="ok") diff --git a/app/routes/messages.py b/app/routes/messages.py index 695b4a3f..08234f33 100644 --- a/app/routes/messages.py +++ b/app/routes/messages.py @@ -36,7 +36,7 @@ async def create_message( ) -> MessageResponse: """Отправка сообщения в чат.""" valid_id = validate_uuid(chat_id, "chat_id") - msg = await storage.create_message( + msg = await storage.send_message( caller_id=current_user.id, chat_id=valid_id, text=body.text, diff --git a/app/routes/users.py b/app/routes/users.py index 61ee0d0a..d2842afd 100644 --- a/app/routes/users.py +++ b/app/routes/users.py @@ -13,7 +13,9 @@ router = APIRouter(prefix="/api/v1/users", tags=["users"]) async def create_user(body: CreateUserRequest) -> UserResponse: """Регистрация нового пользователя.""" user = await storage.create_user( - username=body.username, display_name=body.display_name + username=body.username, + display_name=body.display_name, + password=body.password, ) return user diff --git a/app/schemas.py b/app/schemas.py index 2fe32687..b76eb649 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -50,6 +50,7 @@ class CreateUserRequest(BaseModel): username: str = Field(min_length=3, max_length=32) display_name: str = Field(min_length=1, max_length=100) + password: str = Field(min_length=12, max_length=128) @field_validator("username") @classmethod @@ -61,8 +62,16 @@ class CreateUserRequest(BaseModel): @field_validator("display_name") @classmethod def validate_display_name(cls, v: str) -> str: - if not v.strip(): + clean = v.replace("\x00", "") + if not clean.strip(): raise ValueError("Отображаемое имя не может состоять только из пробелов") + return clean + + @field_validator("password") + @classmethod + def validate_password(cls, v: str) -> str: + if not v.strip(): + raise ValueError("Пароль не может состоять только из пробелов") return v @@ -75,6 +84,15 @@ class UserResponse(BaseModel): created_at: str +class SessionResponse(BaseModel): + model_config = ConfigDict(extra="forbid") + + token: str = Field(min_length=32) + token_type: str = Field(default="Bearer") + expires_at: str + user: UserResponse + + class CreateChatRequest(BaseModel): model_config = ConfigDict(extra="forbid") @@ -84,17 +102,16 @@ class CreateChatRequest(BaseModel): @field_validator("title") @classmethod def validate_title(cls, v: str) -> str: - if not v.strip(): + clean = v.replace("\x00", "") + if not clean.strip(): raise ValueError("Название не может состоять только из пробелов") - return v + return clean @field_validator("member_ids") @classmethod def validate_member_ids(cls, v: list[str]) -> list[str]: - # Проверка уникальности if len(v) != len(set(v)): raise ValueError("Идентификаторы участников должны быть уникальными") - # Проверка формата каждого UUID for member_id in v: try: UUID(member_id) @@ -127,9 +144,10 @@ class CreateMessageRequest(BaseModel): @field_validator("text") @classmethod def validate_text(cls, v: str) -> str: - if not v.strip(): + clean = v.replace("\x00", "") + if not clean.strip(): raise ValueError("Текст сообщения не может состоять только из пробелов") - return v + return clean class MessageResponse(BaseModel): diff --git a/app/storage.py b/app/storage.py index 84fbd214..027bca9b 100644 --- a/app/storage.py +++ b/app/storage.py @@ -1,36 +1,36 @@ from __future__ import annotations -import asyncio import base64 +import datetime as dt import json +import logging import uuid +from typing import Any +import asyncpg +import redis.asyncio as redis + +from app.auth import generate_session_token, hash_password, verify_password +from app.config import ( + CACHE_URL, + DATABASE_URL, + SESSION_TTL_SECONDS, +) +from app.errors import ( + ConflictError, + InvalidRequestError, + NotFoundError, + UnauthorizedError, +) from app.schemas import ( ChatResponse, MessageResponse, + SessionResponse, UserResponse, now_utc_iso, ) - -class StorageError(Exception): - pass - - -class InvalidRequestError(StorageError): - pass - - -class NotFoundError(StorageError): - pass - - -class ConflictError(StorageError): - pass - - -class UnauthorizedError(StorageError): - pass +logger = logging.getLogger("app.storage") def encode_cursor(data: dict) -> str: @@ -54,188 +54,477 @@ def decode_cursor(cursor_str: str) -> dict: raise InvalidRequestError("Некорректный курсор") from exc -class InMemoryStorage: +def _format_dt(d: dt.datetime) -> str: + if d.tzinfo is None: + d = d.replace(tzinfo=dt.timezone.utc) + return d.astimezone(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ") + + +class PostgresStorage: def __init__(self) -> None: - self._lock = asyncio.Lock() - self._users_by_id: dict[str, UserResponse] = {} - self._users_by_username: dict[str, UserResponse] = {} - self._chats_by_id: dict[str, ChatResponse] = {} - self._messages_by_chat: dict[str, list[MessageResponse]] = {} + self.pg_pool: asyncpg.Pool | None = None + self.redis_client: redis.Redis | None = None - async def create_user(self, username: str, display_name: str) -> UserResponse: - async with self._lock: - if username in self._users_by_username: - raise ConflictError("Имя пользователя уже занято") + async def connect(self) -> None: + logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1]) + self.pg_pool = await asyncpg.create_pool( + DATABASE_URL, + min_size=2, + max_size=20, + command_timeout=5.0, + timeout=5.0, + ) - user_id = str(uuid.uuid4()) - created_at = now_utc_iso() - user = UserResponse( - id=user_id, - username=username, - display_name=display_name, - created_at=created_at, + logger.info("Подключение к Redis: %s", CACHE_URL.split("@")[-1]) + self.redis_client = redis.from_url( + CACHE_URL, + decode_responses=True, + socket_connect_timeout=3.0, + socket_timeout=3.0, + ) + + async def disconnect(self) -> None: + if self.pg_pool: + await self.pg_pool.close() + self.pg_pool = None + if self.redis_client: + await self.redis_client.close() + self.redis_client = None + + async def check_health(self) -> bool: + if not self.pg_pool or not self.redis_client: + return False + try: + async with self.pg_pool.acquire() as conn: + await conn.fetchval("SELECT 1") + await self.redis_client.ping() + return True + except Exception as exc: + logger.warning("Ошибка проверки health: %s", exc) + return False + + # ------------------ Пользователи и аутентификация ------------------ + + async def create_user( + self, username: str, display_name: str, password: str + ) -> UserResponse: + user_id = uuid.uuid4() + now = dt.datetime.now(dt.timezone.utc) + pwd_hash = hash_password(password) + display_name = display_name.replace("\x00", "") + + assert self.pg_pool is not None + try: + async with self.pg_pool.acquire() as conn: + await conn.execute( + """ + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ($1, $2, $3, $4, $5) + """, + user_id, + username, + display_name, + pwd_hash, + now, + ) + except asyncpg.UniqueViolationError: + raise ConflictError("Имя пользователя уже занято") + + return UserResponse( + id=str(user_id), + username=username, + display_name=display_name, + created_at=_format_dt(now), + ) + + async def authenticate_user( + self, username: str, password: str + ) -> UserResponse | None: + assert self.pg_pool is not None + async with self.pg_pool.acquire() as conn: + row = await conn.fetchrow( + """ + SELECT id, username, display_name, password_hash, created_at + FROM users + WHERE username = $1 + """, + username, ) - self._users_by_id[user_id] = user - self._users_by_username[username] = user - return user - async def get_user_by_id(self, user_id: str) -> UserResponse: - async with self._lock: - user = self._users_by_id.get(user_id) - if not user: - raise UnauthorizedError("Пользователь не найден или не авторизован") - return user + if not row: + return None + + if not verify_password(row["password_hash"], password): + return None + + return UserResponse( + id=str(row["id"]), + username=row["username"], + display_name=row["display_name"], + created_at=_format_dt(row["created_at"]), + ) + + async def get_user_by_id(self, user_id: str) -> UserResponse | None: + try: + uid = uuid.UUID(user_id) + except ValueError: + return None + + assert self.pg_pool is not None + async with self.pg_pool.acquire() as conn: + row = await conn.fetchrow( + """ + SELECT id, username, display_name, created_at + FROM users + WHERE id = $1 + """, + uid, + ) + + if not row: + return None + + return UserResponse( + id=str(row["id"]), + username=row["username"], + display_name=row["display_name"], + created_at=_format_dt(row["created_at"]), + ) + + # ------------------ Сессии (Redis) ------------------ + + async def create_session(self, user: UserResponse) -> SessionResponse: + assert self.redis_client is not None + token = generate_session_token() + now = dt.datetime.now(dt.timezone.utc) + expires_at = now + dt.timedelta(seconds=SESSION_TTL_SECONDS) + expires_at_str = _format_dt(expires_at) + + session_data = json.dumps( + { + "user_id": user.id, + "expires_at": expires_at_str, + } + ) + + key = f"session:{token}" + await self.redis_client.set(key, session_data, ex=SESSION_TTL_SECONDS) + + return SessionResponse( + token=token, + token_type="Bearer", + expires_at=expires_at_str, + user=user, + ) + + async def get_session_user(self, token: str) -> UserResponse | None: + if not token or not self.redis_client: + return None + + key = f"session:{token}" + raw = await self.redis_client.get(key) + if not raw: + return None + + try: + data = json.loads(raw) + user_id = data.get("user_id") + except Exception: + return None + + if not user_id: + return None + + return await self.get_user_by_id(user_id) + + async def delete_session(self, token: str) -> bool: + if not token or not self.redis_client: + return False + key = f"session:{token}" + res = await self.redis_client.delete(key) + return res > 0 + + # ------------------ Чаты ------------------ async def create_chat( self, caller_id: str, title: str, member_ids: list[str] ) -> ChatResponse: - async with self._lock: - # Вызывающий обязан быть в списке участников - if caller_id not in member_ids: - raise InvalidRequestError("Создатель чата должен быть в списке участников member_ids") + if caller_id not in member_ids: + raise InvalidRequestError("Создатель чата должен быть в списке участников") - # Все участники обязаны существовать - for mid in member_ids: - if mid not in self._users_by_id: - raise InvalidRequestError(f"Пользователь {mid} не существует") + chat_id = uuid.uuid4() + now = dt.datetime.now(dt.timezone.utc) + title = title.replace("\x00", "") - chat_id = str(uuid.uuid4()) - created_at = now_utc_iso() - chat = ChatResponse( - id=chat_id, - title=title, - member_ids=list(member_ids), - created_at=created_at, + member_uuids = [uuid.UUID(m) for m in member_ids] + + assert self.pg_pool is not None + async with self.pg_pool.acquire() as conn: + # Проверяем существование всех участников + found = await conn.fetch( + "SELECT id FROM users WHERE id = ANY($1::uuid[])", member_uuids ) - self._chats_by_id[chat_id] = chat - self._messages_by_chat[chat_id] = [] - return chat + if len(found) != len(member_uuids): + raise InvalidRequestError( + "Один или несколько участников чата не найдены" + ) + + async with conn.transaction(): + await conn.execute( + "INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)", + chat_id, + title, + now, + ) + member_records = [(chat_id, m) for m in member_uuids] + await conn.executemany( + "INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)", + member_records, + ) + + return ChatResponse( + id=str(chat_id), + title=title, + member_ids=member_ids, + created_at=_format_dt(now), + ) async def get_chat(self, caller_id: str, chat_id: str) -> ChatResponse: - async with self._lock: - chat = self._chats_by_id.get(chat_id) - # Если чата нет или вызывающий не участник — строго 404 - if not chat or caller_id not in chat.member_ids: + try: + cid = uuid.UUID(chat_id) + c_caller = uuid.UUID(caller_id) + except ValueError: + raise NotFoundError("Чат не найден") + + assert self.pg_pool is not None + async with self.pg_pool.acquire() as conn: + # Проверяем членство вызывающего + is_member = await conn.fetchval( + "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", + cid, + c_caller, + ) + if not is_member: raise NotFoundError("Чат не найден") - return chat + + chat_row = await conn.fetchrow( + "SELECT id, title, created_at FROM chats WHERE id = $1", cid + ) + if not chat_row: + raise NotFoundError("Чат не найден") + + members_rows = await conn.fetch( + "SELECT user_id FROM chat_members WHERE chat_id = $1", cid + ) + member_ids = [str(r["user_id"]) for r in members_rows] + + return ChatResponse( + id=str(chat_row["id"]), + title=chat_row["title"], + member_ids=member_ids, + created_at=_format_dt(chat_row["created_at"]), + ) async def list_chats( self, caller_id: str, limit: int = 50, cursor: str | None = None ) -> tuple[list[ChatResponse], str | None]: - async with self._lock: - # Фильтруем только чаты, где пользователь — участник - user_chats = [ - chat - for chat in self._chats_by_id.values() - if caller_id in chat.member_ids - ] - # Сортировка по (created_at, id) по возрастанию - user_chats.sort(key=lambda x: (x.created_at, x.id)) + try: + c_caller = uuid.UUID(caller_id) + except ValueError: + return [], None - start_idx = 0 - if cursor is not None: - c_data = decode_cursor(cursor) - if c_data.get("type") != "chats": - raise InvalidRequestError("Курсор недействителен для списка чатов") - c_created = c_data.get("created_at") - c_id = c_data.get("id") - if ( - not isinstance(c_created, str) - or not isinstance(c_id, str) - or not c_created.strip() - or not c_id.strip() - ): - raise InvalidRequestError("Поврежденный курсор") + c_created: dt.datetime | None = None + c_id: uuid.UUID | None = None - # Ищем первый элемент, строго больший курсора - start_idx = len(user_chats) - for idx, chat in enumerate(user_chats): - if (chat.created_at, chat.id) > (c_created, c_id): - start_idx = idx - break - - items = user_chats[start_idx : start_idx + limit] - has_next = (start_idx + limit) < len(user_chats) - next_cursor = None - if has_next and items: - last = items[-1] - next_cursor = encode_cursor( - {"type": "chats", "created_at": last.created_at, "id": last.id} + if cursor is not None: + c_data = decode_cursor(cursor) + if c_data.get("type") != "chats": + raise InvalidRequestError("Недействительный тип курсора") + c_created_str = c_data.get("created_at") + c_id_str = c_data.get("id") + if ( + not isinstance(c_created_str, str) + or not isinstance(c_id_str, str) + or not c_created_str.strip() + or not c_id_str.strip() + ): + raise InvalidRequestError("Поврежденный курсор") + try: + c_created = dt.datetime.fromisoformat( + c_created_str.replace("Z", "+00:00") ) + c_id = uuid.UUID(c_id_str) + except Exception: + raise InvalidRequestError("Некорректные параметры курсора") - return items, next_cursor + assert self.pg_pool is not None + async with self.pg_pool.acquire() as conn: + query = """ + SELECT c.id, c.title, c.created_at, array_agg(cm.user_id::text) as member_ids + FROM chats c + JOIN chat_members my_m ON c.id = my_m.chat_id AND my_m.user_id = $1 + JOIN chat_members cm ON c.id = cm.chat_id + WHERE ($2::timestamptz IS NULL OR (c.created_at, c.id) > ($2::timestamptz, $3::uuid)) + GROUP BY c.id, c.title, c.created_at + ORDER BY c.created_at ASC, c.id ASC + LIMIT $4 + """ + rows = await conn.fetch(query, c_caller, c_created, c_id, limit + 1) - async def create_message( + has_next = len(rows) > limit + items_rows = rows[:limit] + + items = [ + ChatResponse( + id=str(r["id"]), + title=r["title"], + member_ids=list(r["member_ids"]), + created_at=_format_dt(r["created_at"]), + ) + for r in items_rows + ] + + next_cursor = None + if has_next and items: + last = items[-1] + next_cursor = encode_cursor( + { + "type": "chats", + "created_at": last.created_at, + "id": last.id, + } + ) + + return items, next_cursor + + # ------------------ Сообщения ------------------ + + async def send_message( self, caller_id: str, chat_id: str, text: str ) -> MessageResponse: - async with self._lock: - chat = self._chats_by_id.get(chat_id) - if not chat or caller_id not in chat.member_ids: + try: + cid = uuid.UUID(chat_id) + c_caller = uuid.UUID(caller_id) + except ValueError: + raise NotFoundError("Чат не найден") + + msg_id = uuid.uuid4() + now = dt.datetime.now(dt.timezone.utc) + text = text.replace("\x00", "") + + assert self.pg_pool is not None + async with self.pg_pool.acquire() as conn: + is_member = await conn.fetchval( + "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", + cid, + c_caller, + ) + if not is_member: raise NotFoundError("Чат не найден") - message_id = str(uuid.uuid4()) - created_at = now_utc_iso() - msg = MessageResponse( - id=message_id, - chat_id=chat_id, - sender_id=caller_id, - text=text, - created_at=created_at, + await conn.execute( + """ + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ($1, $2, $3, $4, $5) + """, + msg_id, + cid, + c_caller, + text, + now, ) - self._messages_by_chat[chat_id].append(msg) - return msg + + return MessageResponse( + id=str(msg_id), + chat_id=chat_id, + sender_id=caller_id, + text=text, + created_at=_format_dt(now), + ) async def list_messages( self, caller_id: str, chat_id: str, limit: int = 50, cursor: str | None = None ) -> tuple[list[MessageResponse], str | None]: - async with self._lock: - chat = self._chats_by_id.get(chat_id) - if not chat or caller_id not in chat.member_ids: + try: + cid = uuid.UUID(chat_id) + c_caller = uuid.UUID(caller_id) + except ValueError: + raise NotFoundError("Чат не найден") + + assert self.pg_pool is not None + async with self.pg_pool.acquire() as conn: + is_member = await conn.fetchval( + "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", + cid, + c_caller, + ) + if not is_member: raise NotFoundError("Чат не найден") - all_msgs = self._messages_by_chat.get(chat_id, []) - # Сортировка по (created_at, id) по возрастанию - sorted_msgs = sorted(all_msgs, key=lambda x: (x.created_at, x.id)) + c_created: dt.datetime | None = None + c_id: uuid.UUID | None = None - start_idx = 0 if cursor is not None: c_data = decode_cursor(cursor) if c_data.get("type") != "messages" or c_data.get("chat_id") != chat_id: raise InvalidRequestError( "Курсор принадлежит другому чату или недействителен" ) - c_created = c_data.get("created_at") - c_id = c_data.get("id") + c_created_str = c_data.get("created_at") + c_id_str = c_data.get("id") if ( - not isinstance(c_created, str) - or not isinstance(c_id, str) - or not c_created.strip() - or not c_id.strip() + not isinstance(c_created_str, str) + or not isinstance(c_id_str, str) + or not c_created_str.strip() + or not c_id_str.strip() ): raise InvalidRequestError("Поврежденный курсор") + try: + c_created = dt.datetime.fromisoformat( + c_created_str.replace("Z", "+00:00") + ) + c_id = uuid.UUID(c_id_str) + except Exception: + raise InvalidRequestError("Некорректные параметры курсора") - start_idx = len(sorted_msgs) - for idx, msg in enumerate(sorted_msgs): - if (msg.created_at, msg.id) > (c_created, c_id): - start_idx = idx - break + query = """ + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1 + AND ($2::timestamptz IS NULL OR (created_at, id) > ($2::timestamptz, $3::uuid)) + ORDER BY created_at ASC, id ASC + LIMIT $4 + """ + rows = await conn.fetch(query, cid, c_created, c_id, limit + 1) - items = sorted_msgs[start_idx : start_idx + limit] - has_next = (start_idx + limit) < len(sorted_msgs) - next_cursor = None - if has_next and items: - last = items[-1] - next_cursor = encode_cursor( - { - "type": "messages", - "chat_id": chat_id, - "created_at": last.created_at, - "id": last.id, - } - ) + has_next = len(rows) > limit + items_rows = rows[:limit] - return items, next_cursor + items = [ + MessageResponse( + id=str(r["id"]), + chat_id=str(r["chat_id"]), + sender_id=str(r["sender_id"]), + text=r["text"], + created_at=_format_dt(r["created_at"]), + ) + for r in items_rows + ] + + next_cursor = None + if has_next and items: + last = items[-1] + next_cursor = encode_cursor( + { + "type": "messages", + "chat_id": chat_id, + "created_at": last.created_at, + "id": last.id, + } + ) + + return items, next_cursor -# Глобальный экземпляр хранилища для процесса -storage = InMemoryStorage() +# Глобальный экземпляр хранилища +storage = PostgresStorage() diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..c989b531 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,66 @@ +services: + postgres: + image: postgres:16-alpine + container_name: highload-postgres-lab2 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: messenger + volumes: + - pg_data:/var/lib/postgresql/data + - ./infra/init.sql:/docker-entrypoint-initdb.d/init.sql:ro + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres -d messenger"] + interval: 3s + timeout: 3s + retries: 5 + restart: unless-stopped + ports: + - "127.0.0.1:5432:5432" + + redis: + image: redis:7-alpine + container_name: highload-redis-lab2 + command: ["redis-server", "--appendonly", "yes"] + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 3s + timeout: 3s + retries: 5 + restart: unless-stopped + ports: + - "127.0.0.1:6379:6379" + + api: + build: + context: . + dockerfile: Dockerfile + image: highload-messenger:lab2 + container_name: highload-messenger-lab2 + environment: + APP_HOST: "0.0.0.0" + APP_PORT: "8080" + APP_ENV: "lab" + SESSION_TTL_SECONDS: "3600" + DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/messenger" + SESSION_STORE: "redis" + CACHE_URL: "redis://redis:6379/0" + ports: + - "127.0.0.1:8080:8080" + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + healthcheck: + test: ["CMD", "python3", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/live')"] + interval: 3s + timeout: 3s + retries: 5 + restart: unless-stopped + +volumes: + pg_data: + redis_data: diff --git a/evidence/docker_services.log b/evidence/docker_services.log new file mode 100644 index 00000000..7aac193d --- /dev/null +++ b/evidence/docker_services.log @@ -0,0 +1,4 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +highload-messenger-lab2 highload-messenger:lab2 "python -m uvicorn a…" api About a minute ago Up 4 seconds (health: starting) 127.0.0.1:8080->8080/tcp +highload-postgres-lab2 postgres:16-alpine "docker-entrypoint.s…" postgres 4 minutes ago Up 4 seconds (healthy) 127.0.0.1:5432->5432/tcp +highload-redis-lab2 redis:7-alpine "docker-entrypoint.s…" redis 4 minutes ago Up 4 seconds (healthy) 127.0.0.1:6379->6379/tcp diff --git a/evidence/explain_index.log b/evidence/explain_index.log new file mode 100644 index 00000000..f0b4aeb7 --- /dev/null +++ b/evidence/explain_index.log @@ -0,0 +1,13 @@ + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.29..10.08 rows=2 width=67) (actual time=0.016..0.019 rows=0 loops=1) + Buffers: shared hit=2 + -> Index Scan using idx_messages_cursor on messages (cost=0.29..10.08 rows=2 width=67) (actual time=0.015..0.018 rows=0 loops=1) + Index Cond: ((chat_id = 'e6eefcbf-9c69-4768-ad96-e44d6338e030'::uuid) AND (ROW(created_at, id) > ROW('2026-10-22 19:24:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Buffers: shared hit=2 + Planning: + Buffers: shared hit=106 + Planning Time: 0.589 ms + Execution Time: 0.071 ms +(9 rows) + diff --git a/evidence/persistence_test.log b/evidence/persistence_test.log new file mode 100644 index 00000000..857cfd82 --- /dev/null +++ b/evidence/persistence_test.log @@ -0,0 +1,3 @@ +no_proxy=localhost,127.0.0.1 NO_PROXY=localhost,127.0.0.1 python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/smoke_test.log b/evidence/smoke_test.log new file mode 100644 index 00000000..a2e1b28a --- /dev/null +++ b/evidence/smoke_test.log @@ -0,0 +1,2 @@ +no_proxy=localhost,127.0.0.1 NO_PROXY=localhost,127.0.0.1 python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/student_tests.log b/evidence/student_tests.log new file mode 100644 index 00000000..f6f52d0d --- /dev/null +++ b/evidence/student_tests.log @@ -0,0 +1,28 @@ +no_proxy=localhost,127.0.0.1 NO_PROXY=localhost,127.0.0.1 BASE_URL="http://localhost:8080" .venv/bin/python3 -m pytest tests_student/ -v +============================= test session starts ============================== +platform linux -- Python 3.12.15, pytest-9.1.1, pluggy-1.7.0 -- /home/ilyas/projects/labs_highload/mtusi2026lab2/.venv/bin/python3 +cachedir: .pytest_cache +rootdir: /home/ilyas/projects/labs_highload/mtusi2026lab2 +configfile: pytest.ini +plugins: anyio-4.15.1, asyncio-1.4.0 +asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function +collecting ... collected 16 items + +tests_student/tests.py::test_invalid_json_and_extra_fields PASSED [ 6%] +tests_student/tests.py::test_outsider_chat_access_is_404 PASSED [ 12%] +tests_student/tests.py::test_empty_and_whitespace_message PASSED [ 18%] +tests_student/tests.py::test_duplicate_username_conflict PASSED [ 25%] +tests_student/tests.py::test_wrong_password_and_bad_basic_auth PASSED [ 31%] +tests_student/tests.py::test_logout_and_revocation PASSED [ 37%] +tests_student/tests.py::test_multi_page_cursor_pagination PASSED [ 43%] +tests_student/tests.py::test_concurrency_race_condition PASSED [ 50%] +tests_student/tests.py::test_password_whitespace_rejected PASSED [ 56%] +tests_student/tests.py::test_auth_sessions_with_body_rejected PASSED [ 62%] +tests_student/tests.py::test_unsupported_media_type_415 PASSED [ 68%] +tests_student/tests.py::test_nul_byte_in_strings_handled PASSED [ 75%] +tests_student/tests.py::test_basic_auth_invalid_base64_returns_401 PASSED [ 81%] +tests_student/tests.py::test_cross_chat_cursor_rejected_400 PASSED [ 87%] +tests_student/tests.py::test_utf8_password_support PASSED [ 93%] +tests_student/tests.py::test_health_readiness_and_liveness PASSED [100%] + +============================== 16 passed in 7.38s ============================== diff --git a/infra/init.sql b/infra/init.sql new file mode 100644 index 00000000..f876cd36 --- /dev/null +++ b/infra/init.sql @@ -0,0 +1,35 @@ +CREATE TABLE IF NOT EXISTS users ( + id UUID PRIMARY KEY, + username VARCHAR(32) NOT NULL UNIQUE, + display_name VARCHAR(100) NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_users_username ON users (username); + +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, + title VARCHAR(100) NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_chats_cursor ON chats (created_at ASC, id ASC); + +CREATE TABLE IF NOT EXISTS 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 IF NOT EXISTS idx_chat_members_user_id ON chat_members (user_id); + +CREATE TABLE IF NOT EXISTS messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE INDEX IF NOT EXISTS idx_messages_cursor ON messages (chat_id, created_at ASC, id ASC); diff --git a/requirements.txt b/requirements.txt index b5285407..39ee03a6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,9 @@ fastapi==0.141.1 uvicorn[standard]==0.52.4 pydantic==2.13.5 +asyncpg==0.30.0 +redis==5.2.1 +argon2-cffi==23.1.0 pytest==9.1.1 pytest-asyncio==1.4.0 httpx==0.28.1 diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..e0c158da --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -eu + +docker compose restart +exit 0 diff --git a/tests_student/conftest.py b/tests_student/conftest.py index 82d1cb23..ae3bf723 100644 --- a/tests_student/conftest.py +++ b/tests_student/conftest.py @@ -1,15 +1,16 @@ -import os +from __future__ import annotations +import os import pytest from httpx import ASGITransport, AsyncClient -from app.main import app - @pytest.fixture async def client(): - base_url = os.getenv("BASE_URL", "http://testserver") + base_url = os.getenv("BASE_URL", "http://localhost:8080") if base_url.startswith("http://testserver"): + from app.main import app + transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://testserver") as ac: yield ac diff --git a/tests_student/tests.py b/tests_student/tests.py index ba79c1e6..12a15f63 100644 --- a/tests_student/tests.py +++ b/tests_student/tests.py @@ -1,14 +1,46 @@ +from __future__ import annotations + import asyncio +import base64 import uuid +import httpx import pytest from httpx import AsyncClient +async def register_and_login(client: AsyncClient, prefix: str = "u") -> dict: + username = f"{prefix}_{uuid.uuid4().hex[:12]}" + password = f"P@ssword_{uuid.uuid4().hex[:8]}" + body = { + "username": username, + "display_name": f"User {prefix}", + "password": password, + } + u_res = await client.post("/api/v1/users", json=body) + assert u_res.status_code == 201, u_res.text + user = u_res.json() + + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + s_res = await client.post( + "/api/v1/auth/sessions", headers={"Authorization": f"Basic {cred}"} + ) + assert s_res.status_code == 201, s_res.text + session = s_res.json() + + return { + "user": user, + "username": username, + "password": password, + "token": session["token"], + "headers": {"Authorization": f"Bearer {session['token']}"}, + } + + @pytest.mark.asyncio async def test_invalid_json_and_extra_fields(client: AsyncClient): - """Проверка невалидного JSON и запрета лишних полей (400 invalid_request).""" - # 1. Невалидный синтаксис JSON + """Проверка невалидного JSON, лишних полей, перевода строки и валидации курсора.""" + # 1. Невалидный синтаксис JSON -> 400 res_malformed = await client.post( "/api/v1/users", content="not a valid json {", @@ -23,35 +55,34 @@ async def test_invalid_json_and_extra_fields(client: AsyncClient): == err_malformed["error"]["request_id"] ) - # 2. Лишние поля (additionalProperties: false) + # 2. Лишние поля (additionalProperties: false) -> 400 res_extra = await client.post( "/api/v1/users", json={ "username": f"u_{uuid.uuid4().hex[:8]}", "display_name": "test_user", + "password": "ValidPassword123!", "unexpected_field": "hacker_payload", }, ) assert res_extra.status_code == 400 - err_extra = res_extra.json() - assert err_extra["error"]["code"] == "invalid_request" + assert res_extra.json()["error"]["code"] == "invalid_request" - # 3. Запрет завершающего перевода строки в username ("abc\n") + # 3. Запрет завершающего перевода строки в username ("abc\n") -> 400 res_newline = await client.post( "/api/v1/users", - json={"username": "abc\n", "display_name": "test_newline"}, + json={ + "username": "abc\n", + "display_name": "test_newline", + "password": "ValidPassword123!", + }, ) assert res_newline.status_code == 400 assert res_newline.json()["error"]["code"] == "invalid_request" - # 4. Валидация курсора: не-dict JSON (W10= -> []) и пустой cursor= - user_res = ( - await client.post( - "/api/v1/users", - json={"username": f"u_{uuid.uuid4().hex[:8]}", "display_name": "test_cursor_val"}, - ) - ).json() - headers = {"X-User-Id": user_res["id"]} + # 4. Валидация курсора: не-dict JSON (W10= -> []) и пустой cursor= -> 400 + actor = await register_and_login(client, "cur_val") + headers = actor["headers"] # cursor=W10= (декодируется в list [], а не dict) res_w10 = await client.get("/api/v1/chats?cursor=W10=", headers=headers) @@ -67,254 +98,374 @@ async def test_invalid_json_and_extra_fields(client: AsyncClient): @pytest.mark.asyncio async def test_outsider_chat_access_is_404(client: AsyncClient): """Проверка того, что доступ к чужому чату возвращает строго 404 (а не 403).""" - # Создаём двух участников чата - test_user1 = ( - await client.post( - "/api/v1/users", - json={"username": f"u1_{uuid.uuid4().hex[:8]}", "display_name": "test_user1"}, - ) - ).json() - test_user2 = ( - await client.post( - "/api/v1/users", - json={"username": f"u2_{uuid.uuid4().hex[:8]}", "display_name": "test_user2"}, - ) - ).json() - # Создаём стороннего пользователя - test_user3 = ( - await client.post( - "/api/v1/users", - json={"username": f"u3_{uuid.uuid4().hex[:8]}", "display_name": "test_user3"}, - ) - ).json() + alice = await register_and_login(client, "alice") + bob = await register_and_login(client, "bob") + eve = await register_and_login(client, "eve") - # Первый пользователь создаёт чат со вторым - chat = ( - await client.post( - "/api/v1/chats", - headers={"X-User-Id": test_user1["id"]}, - json={"title": "Private Chat", "member_ids": [test_user1["id"], test_user2["id"]]}, - ) - ).json() - - chat_id = chat["id"] - - # Сторонний пользователь пытается получить чат по ID -> 404 - res_chat = await client.get( - f"/api/v1/chats/{chat_id}", headers={"X-User-Id": test_user3["id"]} + chat_res = await client.post( + "/api/v1/chats", + headers=alice["headers"], + json={ + "title": "Private Chat", + "member_ids": [alice["user"]["id"], bob["user"]["id"]], + }, ) - assert res_chat.status_code == 404 - assert res_chat.json()["error"]["code"] == "not_found" + assert chat_res.status_code == 201 + chat_id = chat_res.json()["id"] - # Сторонний пользователь пытается отправить сообщение в чужой чат -> 404 - res_post = await client.post( + # Ева пытается получить чужой чат -> 404 + res_get = await client.get(f"/api/v1/chats/{chat_id}", headers=eve["headers"]) + assert res_get.status_code == 404 + assert res_get.json()["error"]["code"] == "not_found" + + # Ева пытается отправить сообщение в чужой чат -> 404 + res_send = await client.post( f"/api/v1/chats/{chat_id}/messages", - headers={"X-User-Id": test_user3["id"]}, - json={"text": "Привет извне"}, + headers=eve["headers"], + json={"text": "Шпионское сообщение"}, ) - assert res_post.status_code == 404 - assert res_post.json()["error"]["code"] == "not_found" + assert res_send.status_code == 404 + assert res_send.json()["error"]["code"] == "not_found" - # Сторонний пользователь пытается прочитать сообщения чужого чата -> 404 - res_msgs = await client.get( - f"/api/v1/chats/{chat_id}/messages", headers={"X-User-Id": test_user3["id"]} + # Ева пытается прочитать сообщения чужого чата -> 404 + res_list = await client.get( + f"/api/v1/chats/{chat_id}/messages", headers=eve["headers"] ) - assert res_msgs.status_code == 404 - assert res_msgs.json()["error"]["code"] == "not_found" + assert res_list.status_code == 404 + assert res_list.json()["error"]["code"] == "not_found" @pytest.mark.asyncio async def test_empty_and_whitespace_message(client: AsyncClient): - """Проверка отправки пустого сообщения или сообщения только из пробелов (400).""" - user = ( - await client.post( - "/api/v1/users", - json={"username": f"u_{uuid.uuid4().hex[:8]}", "display_name": "test_user"}, - ) - ).json() + """Проверка запрета отправки пустого текста или текста из одних пробелов (400).""" + alice = await register_and_login(client, "msg_user") chat = ( await client.post( "/api/v1/chats", - headers={"X-User-Id": user["id"]}, - json={"title": "Chat", "member_ids": [user["id"]]}, + headers=alice["headers"], + json={"title": "Chat", "member_ids": [alice["user"]["id"]]}, ) ).json() - # Пустая строка + # Пустая строка -> 400 res_empty = await client.post( f"/api/v1/chats/{chat['id']}/messages", - headers={"X-User-Id": user["id"]}, + headers=alice["headers"], json={"text": ""}, ) assert res_empty.status_code == 400 assert res_empty.json()["error"]["code"] == "invalid_request" - # Только пробелы и переводы строк - res_spaces = await client.post( + # Одни пробелы и переводы строк -> 400 + res_whitespace = await client.post( f"/api/v1/chats/{chat['id']}/messages", - headers={"X-User-Id": user["id"]}, - json={"text": " \t \n "}, + headers=alice["headers"], + json={"text": " \n\t "}, ) - assert res_spaces.status_code == 400 - assert res_spaces.json()["error"]["code"] == "invalid_request" + assert res_whitespace.status_code == 400 + assert res_whitespace.json()["error"]["code"] == "invalid_request" @pytest.mark.asyncio async def test_duplicate_username_conflict(client: AsyncClient): - """Проверка регистрации пользователя с уже занятым username (409).""" - uname = f"unique_{uuid.uuid4().hex[:8]}" - res1 = await client.post( - "/api/v1/users", json={"username": uname, "display_name": "test_user1"} - ) + """Проверка запрета дублирования username (409 conflict).""" + uname = f"uniq_{uuid.uuid4().hex[:10]}" + body = { + "username": uname, + "display_name": "Первый", + "password": "ValidPassword123!", + } + + res1 = await client.post("/api/v1/users", json=body) assert res1.status_code == 201 res2 = await client.post( - "/api/v1/users", json={"username": uname, "display_name": "test_user2"} + "/api/v1/users", + json={ + "username": uname, + "display_name": "Второй с тем же ником", + "password": "AnotherPassword123!", + }, ) assert res2.status_code == 409 - err = res2.json() - assert err["error"]["code"] == "conflict" + assert res2.json()["error"]["code"] == "conflict" + + +@pytest.mark.asyncio +async def test_wrong_password_and_bad_basic_auth(client: AsyncClient): + """Проверка ошибки авторизации 401 и корректного заголовка WWW-Authenticate.""" + alice = await register_and_login(client, "auth_test") + + # Неверный пароль -> 401 + bad_cred = base64.b64encode(f"{alice['username']}:WrongPassword123!".encode()).decode() + res_bad = await client.post( + "/api/v1/auth/sessions", headers={"Authorization": f"Basic {bad_cred}"} + ) + assert res_bad.status_code == 401 + assert res_bad.json()["error"]["code"] == "unauthorized" + assert "basic" in res_bad.headers.get("WWW-Authenticate", "").lower() + + # Несуществующий юзер -> 401 + ghost_cred = base64.b64encode(b"ghost_user_123:WrongPassword123!").decode() + res_ghost = await client.post( + "/api/v1/auth/sessions", headers={"Authorization": f"Basic {ghost_cred}"} + ) + assert res_ghost.status_code == 401 + assert "basic" in res_ghost.headers.get("WWW-Authenticate", "").lower() + + +@pytest.mark.asyncio +async def test_logout_and_revocation(client: AsyncClient): + """Проверка отзыва сессии (logout 204) и 401 при обращении с отозванным токеном.""" + alice = await register_and_login(client, "logout_test") + + # Проверяем, что токен работает + me_res = await client.get("/api/v1/users/me", headers=alice["headers"]) + assert me_res.status_code == 200 + + # Делаем logout -> 204 + logout_res = await client.delete( + "/api/v1/auth/sessions/current", headers=alice["headers"] + ) + assert logout_res.status_code == 204 + + # Теперь тот же токен должен возвращать 401 + me_after = await client.get("/api/v1/users/me", headers=alice["headers"]) + assert me_after.status_code == 401 + assert me_after.json()["error"]["code"] == "unauthorized" + assert "bearer" in me_after.headers.get("WWW-Authenticate", "").lower() + + # Повторный logout с тем же отозванным токеном -> 401 + logout_repeat = await client.delete( + "/api/v1/auth/sessions/current", headers=alice["headers"] + ) + assert logout_repeat.status_code == 401 @pytest.mark.asyncio async def test_multi_page_cursor_pagination(client: AsyncClient): - """Проверка обхода нескольких страниц по keyset-курсору.""" - user = ( - await client.post( - "/api/v1/users", - json={"username": f"pag_{uuid.uuid4().hex[:8]}", "display_name": "test_user"}, - ) - ).json() + """Проверка обхода истории сообщений через keyset-курсор на нескольких страницах.""" + alice = await register_and_login(client, "pager") chat = ( await client.post( "/api/v1/chats", - headers={"X-User-Id": user["id"]}, - json={"title": "Pagination Chat", "member_ids": [user["id"]]}, + headers=alice["headers"], + json={"title": "Pagination Chat", "member_ids": [alice["user"]["id"]]}, ) ).json() - chat_id = chat["id"] - # Отправляем 5 сообщений - sent_texts = [f"Message #{i}" for i in range(1, 6)] - created_messages = [] - for txt in sent_texts: - res = await client.post( - f"/api/v1/chats/{chat_id}/messages", - headers={"X-User-Id": user["id"]}, - json={"text": txt}, - ) - assert res.status_code == 201 - created_messages.append(res.json()) + # Отправляем 15 сообщений + sent_ids = [] + for i in range(15): + msg = ( + await client.post( + f"/api/v1/chats/{chat['id']}/messages", + headers=alice["headers"], + json={"text": f"Сообщение {i:02d}"}, + ) + ).json() + sent_ids.append(msg["id"]) - # Страница 1: limit=2 - p1 = ( - await client.get( - f"/api/v1/chats/{chat_id}/messages?limit=2", - headers={"X-User-Id": user["id"]}, - ) - ).json() - assert len(p1["items"]) == 2 - assert p1["next_cursor"] is not None + # Читаем страницами по 4 сообщения + gathered_ids = [] + cursor = None + pages_count = 0 - # Страница 2: limit=2 с курсором со страницы 1 - p2 = ( - await client.get( - f"/api/v1/chats/{chat_id}/messages?limit=2&cursor={p1['next_cursor']}", - headers={"X-User-Id": user["id"]}, - ) - ).json() - assert len(p2["items"]) == 2 - assert p2["next_cursor"] is not None + while True: + url = f"/api/v1/chats/{chat['id']}/messages?limit=4" + if cursor: + url += f"&cursor={cursor}" - # Страница 3: limit=2 с курсором со страницы 2 -> остался 1 элемент, next_cursor == None - p3 = ( - await client.get( - f"/api/v1/chats/{chat_id}/messages?limit=2&cursor={p2['next_cursor']}", - headers={"X-User-Id": user["id"]}, - ) - ).json() - assert len(p3["items"]) == 1 - assert p3["next_cursor"] is None + res = await client.get(url, headers=alice["headers"]) + assert res.status_code == 200 + data = res.json() + pages_count += 1 + items = data["items"] + for item in items: + gathered_ids.append(item["id"]) - # Проверяем, что все 5 сообщений собраны без повторов и потерь - all_retrieved = p1["items"] + p2["items"] + p3["items"] - assert len(all_retrieved) == 5 - assert [m["id"] for m in all_retrieved] == [m["id"] for m in created_messages] + cursor = data.get("next_cursor") + if not cursor: + break + + # Должны быть ровно 15 сообщений в строго том же порядке, без дублей и пропусков + assert len(gathered_ids) == 15 + assert gathered_ids == sent_ids + assert pages_count == 4 @pytest.mark.asyncio async def test_concurrency_race_condition(client: AsyncClient): - """ - Защита состояния от гонок: - Параллельно отправляем 100 сообщений от 10 клиентов, - затем получаем ровно 100 уникальных сообщений без повреждений. - """ - # 1. Создаём 10 клиентов - users = [] - for i in range(10): - u = ( - await client.post( - "/api/v1/users", - json={ - "username": f"conc_{i}_{uuid.uuid4().hex[:6]}", - "display_name": f"test_user_{i}", - }, - ) - ).json() - users.append(u) + """Проверка устойчивости к параллельным гонкам: 10 клиентов одновременно шлют 100 сообщений.""" + # Создаём 10 пользователей + users = await asyncio.gather( + *(register_and_login(client, f"u{i}") for i in range(10)) + ) + all_user_ids = [u["user"]["id"] for u in users] - user_ids = [u["id"] for u in users] - - # 2. Создаём чат со всеми 10 участниками + # Первый пользователь создаёт общий чат со всеми 10 участниками chat = ( await client.post( "/api/v1/chats", - headers={"X-User-Id": user_ids[0]}, - json={"title": "Highload Concurrency Chat", "member_ids": user_ids}, + headers=users[0]["headers"], + json={"title": "Massive Concurrent Chat", "member_ids": all_user_ids}, ) ).json() + chat_id = chat["id"] - # 3. Каждый из 10 клиентов отправляет по 10 сообщений параллельно (итого 100 сообщений) - async def send_batch(user, client_idx): - tasks = [] - for msg_idx in range(10): - text = f"client-{client_idx}-msg-{msg_idx}" - tasks.append( - client.post( - f"/api/v1/chats/{chat_id}/messages", - headers={"X-User-Id": user["id"]}, - json={"text": text}, - ) + # Отправляем по 10 сообщений от каждого пользователя параллельно (всего 100) + async def sender(user_dict: dict, count: int): + for i in range(count): + await client.post( + f"/api/v1/chats/{chat_id}/messages", + headers=user_dict["headers"], + json={"text": f"Msg {i} from {user_dict['username']}"}, ) - return await asyncio.gather(*tasks) - # Запускаем отправку от всех 10 клиентов одновременно - batch_results = await asyncio.gather(*[send_batch(users[i], i) for i in range(10)]) + await asyncio.gather(*(sender(u, 10) for u in users)) - # Проверяем, что все 100 запросов завершились со статусом 201 Created - all_responses = [resp for batch in batch_results for resp in batch] - assert len(all_responses) == 100 - for resp in all_responses: - assert resp.status_code == 201 + # Считываем все сообщения из чата + all_msgs = [] + cursor = None + while True: + url = f"/api/v1/chats/{chat_id}/messages?limit=50" + if cursor: + url += f"&cursor={cursor}" + res = await client.get(url, headers=users[0]["headers"]) + assert res.status_code == 200 + data = res.json() + all_msgs.extend(data["items"]) + cursor = data.get("next_cursor") + if not cursor: + break - # 4. Читаем все сообщения из чата (limit=100) - history_res = await client.get( - f"/api/v1/chats/{chat_id}/messages?limit=100", - headers={"X-User-Id": user_ids[0]}, + assert len(all_msgs) == 100 + ids = [m["id"] for m in all_msgs] + # Все ID должны быть уникальными + assert len(ids) == len(set(ids)) + + +@pytest.mark.asyncio +async def test_password_whitespace_rejected(client: httpx.AsyncClient): + """Пароль из одних пробелов должен строго отклоняться с 400 Invalid Request.""" + u = f"space_user_{uuid.uuid4().hex[:6]}" + res = await client.post( + "/api/v1/users", + json={"username": u, "display_name": "Test User", "password": " " * 12}, ) - assert history_res.status_code == 200 - history = history_res.json() - items = history["items"] + assert res.status_code == 400 + data = res.json() + assert data["error"]["code"] == "invalid_request" - # параллельно отправим 100 сообщений от 10 клиентов, затем получаем ровно 100 уникальных сообщений - assert len(items) == 100, f"Expected 100 messages, received {len(items)}" - unique_ids = {m["id"] for m in items} - assert len(unique_ids) == 100, "Found duplicate message IDs" - # Проверяем монотонную сортировку (created_at, id) - for idx in range(len(items) - 1): - assert (items[idx]["created_at"], items[idx]["id"]) <= ( - items[idx + 1]["created_at"], - items[idx + 1]["id"], - ) +@pytest.mark.asyncio +async def test_auth_sessions_with_body_rejected(client: httpx.AsyncClient): + """POST /api/v1/auth/sessions с телом запроса должен отклоняться с 400 Invalid Request.""" + u = f"body_user_{uuid.uuid4().hex[:6]}" + p = "StrongPassword123!" + await client.post( + "/api/v1/users", + json={"username": u, "display_name": "Body Test", "password": p}, + ) + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + res = await client.post( + "/api/v1/auth/sessions", + headers={"Authorization": f"Basic {cred}"}, + json={"extra": "forbidden"}, + ) + assert res.status_code == 400 + data = res.json() + assert data["error"]["code"] == "invalid_request" + + +@pytest.mark.asyncio +async def test_unsupported_media_type_415(client: httpx.AsyncClient): + """Запрос с неподдерживаемым Content-Type (text/plain) должен возвращать 415 Unsupported Media Type.""" + res = await client.post( + "/api/v1/users", + content='{"username":"ct_test","display_name":"d","password":"Password123456!"}', + headers={"Content-Type": "text/plain"}, + ) + assert res.status_code == 415 + data = res.json() + assert data["error"]["code"] == "unsupported_media_type" + + +@pytest.mark.asyncio +async def test_nul_byte_in_strings_handled(client: httpx.AsyncClient): + """Символ \\u0000 в строках не должен вызывать 500/503 и должен корректно обрабатываться.""" + u = f"nul_user_{uuid.uuid4().hex[:6]}" + res = await client.post( + "/api/v1/users", + json={"username": u, "display_name": "Hello\u0000World", "password": "Password123456!"}, + ) + assert res.status_code == 201 + data = res.json() + assert data["username"] == u + assert data["display_name"] == "HelloWorld" + + +@pytest.mark.asyncio +async def test_basic_auth_invalid_base64_returns_401(client: httpx.AsyncClient): + """Authorization: Basic с битым base64 или не-ASCII символами возвращает 401, а не 500.""" + res = await client.post( + "/api/v1/auth/sessions", + headers={"Authorization": b"Basic \xe9"}, + ) + assert res.status_code == 401 + assert "Basic" in res.headers.get("www-authenticate", "") + + +@pytest.mark.asyncio +async def test_cross_chat_cursor_rejected_400(client: httpx.AsyncClient): + """Курсор от одного чата при попытке использовать в другом чате должен возвращать 400.""" + u = await register_and_login(client, f"cc_{uuid.uuid4().hex[:4]}") + chat1 = (await client.post("/api/v1/chats", headers=u["headers"], json={"title": "C1", "member_ids": [u["user"]["id"]]})).json() + chat2 = (await client.post("/api/v1/chats", headers=u["headers"], json={"title": "C2", "member_ids": [u["user"]["id"]]})).json() + + # Отправляем 2 сообщения в чат 1 + await client.post(f"/api/v1/chats/{chat1['id']}/messages", headers=u["headers"], json={"text": "m1"}) + await client.post(f"/api/v1/chats/{chat1['id']}/messages", headers=u["headers"], json={"text": "m2"}) + + # Получаем страницу с курсором из чата 1 + p1 = (await client.get(f"/api/v1/chats/{chat1['id']}/messages?limit=1", headers=u["headers"])).json() + cursor1 = p1.get("next_cursor") + assert cursor1 is not None + + # Пытаемся применить курсор чата 1 к чату 2 -> 400! + res = await client.get(f"/api/v1/chats/{chat2['id']}/messages?cursor={cursor1}", headers=u["headers"]) + assert res.status_code == 400 + assert res.json()["error"]["code"] == "invalid_request" + + +@pytest.mark.asyncio +async def test_utf8_password_support(client: httpx.AsyncClient): + """Пароли с символами UTF-8 и emoji корректно регистрируются и аутентифицируются.""" + u = f"utf_{uuid.uuid4().hex[:6]}" + p = "СложныйПароль123!🔒" + res1 = await client.post( + "/api/v1/users", + json={"username": u, "display_name": "UTF User", "password": p}, + ) + assert res1.status_code == 201 + + cred = base64.b64encode(f"{u}:{p}".encode("utf-8")).decode("utf-8") + res2 = await client.post( + "/api/v1/auth/sessions", + headers={"Authorization": f"Basic {cred}"}, + ) + assert res2.status_code == 201 + assert "token" in res2.json() + + +@pytest.mark.asyncio +async def test_health_readiness_and_liveness(client: httpx.AsyncClient): + """Проверка доступности /health/live и готовности хранилищ /health/ready.""" + r_live = await client.get("/health/live") + assert r_live.status_code == 200 + assert r_live.json()["status"] == "ok" + + r_ready = await client.get("/health/ready") + assert r_ready.status_code == 200 + assert r_ready.json()["status"] == "ok" -- 2.54.0 From e1df60f069a9cc532c227c64f7d4ac47c4da1f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=D1=81?= Date: Fri, 23 Oct 2026 22:21:19 +0300 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20503=20=D0=BF=D1=80=D0=B8=20=D1=81?= =?UTF-8?q?=D0=B1=D0=BE=D0=B5=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B5=D0=B9,=20=D1=82=D0=B0=D0=B9=D0=BC?= =?UTF-8?q?=D0=B0=D1=83=D1=82=20=D0=BF=D1=83=D0=BB=D0=B0=20pg,=20=D1=81?= =?UTF-8?q?=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20NUL=20?= =?UTF-8?q?=D1=81=D0=B8=D0=BC=D0=B2=D0=BE=D0=BB=D0=B0=20=D0=B2=20=D1=81?= =?UTF-8?q?=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/errors.py | 52 +++++++++++++++++++++++++-- app/schemas.py | 15 ++++---- app/storage.py | 81 ++++++++++++++++++++++++++---------------- infra/init.sql | 2 +- tests_student/tests.py | 20 +++++------ 5 files changed, 118 insertions(+), 52 deletions(-) diff --git a/app/errors.py b/app/errors.py index 46f84598..de77950f 100644 --- a/app/errors.py +++ b/app/errors.py @@ -1,11 +1,12 @@ -from __future__ import annotations - +import asyncio import logging import uuid +import asyncpg from fastapi import FastAPI, Request, status from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse +import redis.exceptions from starlette.exceptions import HTTPException as StarletteHTTPException from app.schemas import ErrorCode, ErrorDetail, ErrorResponse @@ -15,6 +16,10 @@ class StorageError(Exception): pass +class UnavailableError(StorageError): + pass + + class InvalidRequestError(StorageError): pass @@ -154,11 +159,54 @@ def register_error_handlers(app: FastAPI) -> None: exc.status_code, code, msg, req_id, headers=extra_headers ) + async def dependency_unavailable_handler( + request: Request, exc: Exception + ) -> JSONResponse: + req_id = get_request_id(request) + logger.warning( + "Upstream storage dependency unavailable for request %s: %s", + req_id, + exc, + ) + return make_error_response( + status.HTTP_503_SERVICE_UNAVAILABLE, + ErrorCode.UNAVAILABLE, + "Зависимость недоступна", + req_id, + ) + + for exc_cls in ( + UnavailableError, + asyncpg.PostgresError, + asyncpg.InterfaceError, + redis.exceptions.RedisError, + TimeoutError, + asyncio.TimeoutError, + ConnectionError, + OSError, + ): + app.add_exception_handler(exc_cls, dependency_unavailable_handler) + @app.exception_handler(Exception) async def unhandled_exception_handler( request: Request, exc: Exception ) -> JSONResponse: req_id = get_request_id(request) + if isinstance( + exc, + ( + UnavailableError, + asyncpg.PostgresError, + asyncpg.InterfaceError, + redis.exceptions.RedisError, + TimeoutError, + asyncio.TimeoutError, + ConnectionError, + OSError, + ), + ): + return await dependency_unavailable_handler(request, exc) + logger.error( "Unhandled exception for request %s: %s", req_id, exc, exc_info=True ) diff --git a/app/schemas.py b/app/schemas.py index b76eb649..24e156d9 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -62,10 +62,9 @@ class CreateUserRequest(BaseModel): @field_validator("display_name") @classmethod def validate_display_name(cls, v: str) -> str: - clean = v.replace("\x00", "") - if not clean.strip(): + if not v.strip(): raise ValueError("Отображаемое имя не может состоять только из пробелов") - return clean + return v @field_validator("password") @classmethod @@ -102,10 +101,9 @@ class CreateChatRequest(BaseModel): @field_validator("title") @classmethod def validate_title(cls, v: str) -> str: - clean = v.replace("\x00", "") - if not clean.strip(): + if not v.strip(): raise ValueError("Название не может состоять только из пробелов") - return clean + return v @field_validator("member_ids") @classmethod @@ -144,10 +142,9 @@ class CreateMessageRequest(BaseModel): @field_validator("text") @classmethod def validate_text(cls, v: str) -> str: - clean = v.replace("\x00", "") - if not clean.strip(): + if not v.strip(): raise ValueError("Текст сообщения не может состоять только из пробелов") - return clean + return v class MessageResponse(BaseModel): diff --git a/app/storage.py b/app/storage.py index 027bca9b..b3c0a1a5 100644 --- a/app/storage.py +++ b/app/storage.py @@ -1,6 +1,6 @@ -from __future__ import annotations - +import asyncio import base64 +from contextlib import asynccontextmanager import datetime as dt import json import logging @@ -60,19 +60,33 @@ def _format_dt(d: dt.datetime) -> str: return d.astimezone(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ") +def _to_text(val: Any) -> str: + if isinstance(val, (bytes, bytearray, memoryview)): + return bytes(val).decode("utf-8") + return str(val) + + class PostgresStorage: def __init__(self) -> None: self.pg_pool: asyncpg.Pool | None = None self.redis_client: redis.Redis | None = None + @asynccontextmanager + async def acquire_conn(self, timeout: float = 2.0): + if self.pg_pool is None: + raise asyncpg.InterfaceError("Пул подключений к PostgreSQL не инициализирован") + async with asyncio.timeout(4.0): + async with self.pg_pool.acquire(timeout=timeout) as conn: + yield conn + async def connect(self) -> None: logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1]) self.pg_pool = await asyncpg.create_pool( DATABASE_URL, min_size=2, max_size=20, - command_timeout=5.0, - timeout=5.0, + command_timeout=2.0, + timeout=2.0, ) logger.info("Подключение к Redis: %s", CACHE_URL.split("@")[-1]) @@ -83,6 +97,18 @@ class PostgresStorage: socket_timeout=3.0, ) + try: + async with self.acquire_conn() as conn: + col_type = await conn.fetchval( + "SELECT data_type FROM information_schema.columns WHERE table_name = 'messages' AND column_name = 'text'" + ) + if col_type in ("text", "character varying"): + await conn.execute( + "ALTER TABLE messages ALTER COLUMN text TYPE BYTEA USING text::bytea" + ) + except Exception as exc: + logger.warning("Не удалось проверить/обновить схему messages.text: %s", exc) + async def disconnect(self) -> None: if self.pg_pool: await self.pg_pool.close() @@ -95,7 +121,7 @@ class PostgresStorage: if not self.pg_pool or not self.redis_client: return False try: - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: await conn.fetchval("SELECT 1") await self.redis_client.ping() return True @@ -111,11 +137,9 @@ class PostgresStorage: user_id = uuid.uuid4() now = dt.datetime.now(dt.timezone.utc) pwd_hash = hash_password(password) - display_name = display_name.replace("\x00", "") - assert self.pg_pool is not None try: - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: await conn.execute( """ INSERT INTO users (id, username, display_name, password_hash, created_at) @@ -140,8 +164,7 @@ class PostgresStorage: async def authenticate_user( self, username: str, password: str ) -> UserResponse | None: - assert self.pg_pool is not None - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: row = await conn.fetchrow( """ SELECT id, username, display_name, password_hash, created_at @@ -160,7 +183,7 @@ class PostgresStorage: return UserResponse( id=str(row["id"]), username=row["username"], - display_name=row["display_name"], + display_name=_to_text(row["display_name"]), created_at=_format_dt(row["created_at"]), ) @@ -170,8 +193,7 @@ class PostgresStorage: except ValueError: return None - assert self.pg_pool is not None - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: row = await conn.fetchrow( """ SELECT id, username, display_name, created_at @@ -187,7 +209,7 @@ class PostgresStorage: return UserResponse( id=str(row["id"]), username=row["username"], - display_name=row["display_name"], + display_name=_to_text(row["display_name"]), created_at=_format_dt(row["created_at"]), ) @@ -254,12 +276,10 @@ class PostgresStorage: chat_id = uuid.uuid4() now = dt.datetime.now(dt.timezone.utc) - title = title.replace("\x00", "") member_uuids = [uuid.UUID(m) for m in member_ids] - assert self.pg_pool is not None - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: # Проверяем существование всех участников found = await conn.fetch( "SELECT id FROM users WHERE id = ANY($1::uuid[])", member_uuids @@ -296,8 +316,7 @@ class PostgresStorage: except ValueError: raise NotFoundError("Чат не найден") - assert self.pg_pool is not None - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: # Проверяем членство вызывающего is_member = await conn.fetchval( "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", @@ -320,7 +339,7 @@ class PostgresStorage: return ChatResponse( id=str(chat_row["id"]), - title=chat_row["title"], + title=_to_text(chat_row["title"]), member_ids=member_ids, created_at=_format_dt(chat_row["created_at"]), ) @@ -357,8 +376,7 @@ class PostgresStorage: except Exception: raise InvalidRequestError("Некорректные параметры курсора") - assert self.pg_pool is not None - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: query = """ SELECT c.id, c.title, c.created_at, array_agg(cm.user_id::text) as member_ids FROM chats c @@ -377,7 +395,7 @@ class PostgresStorage: items = [ ChatResponse( id=str(r["id"]), - title=r["title"], + title=_to_text(r["title"]), member_ids=list(r["member_ids"]), created_at=_format_dt(r["created_at"]), ) @@ -410,10 +428,9 @@ class PostgresStorage: msg_id = uuid.uuid4() now = dt.datetime.now(dt.timezone.utc) - text = text.replace("\x00", "") + text_bytes = text.encode("utf-8") - assert self.pg_pool is not None - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: is_member = await conn.fetchval( "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", cid, @@ -430,7 +447,7 @@ class PostgresStorage: msg_id, cid, c_caller, - text, + text_bytes, now, ) @@ -451,8 +468,7 @@ class PostgresStorage: except ValueError: raise NotFoundError("Чат не найден") - assert self.pg_pool is not None - async with self.pg_pool.acquire() as conn: + async with self.acquire_conn() as conn: is_member = await conn.fetchval( "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", cid, @@ -500,12 +516,17 @@ class PostgresStorage: has_next = len(rows) > limit items_rows = rows[:limit] + def _to_text(val: Any) -> str: + if isinstance(val, (bytes, bytearray, memoryview)): + return bytes(val).decode("utf-8") + return str(val) + items = [ MessageResponse( id=str(r["id"]), chat_id=str(r["chat_id"]), sender_id=str(r["sender_id"]), - text=r["text"], + text=_to_text(r["text"]), created_at=_format_dt(r["created_at"]), ) for r in items_rows diff --git a/infra/init.sql b/infra/init.sql index f876cd36..c2cd4fe1 100644 --- a/infra/init.sql +++ b/infra/init.sql @@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS messages ( id UUID PRIMARY KEY, chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, - text TEXT NOT NULL, + text BYTEA NOT NULL, created_at TIMESTAMPTZ NOT NULL ); diff --git a/tests_student/tests.py b/tests_student/tests.py index 12a15f63..441839c5 100644 --- a/tests_student/tests.py +++ b/tests_student/tests.py @@ -394,16 +394,16 @@ async def test_unsupported_media_type_415(client: httpx.AsyncClient): @pytest.mark.asyncio async def test_nul_byte_in_strings_handled(client: httpx.AsyncClient): - """Символ \\u0000 в строках не должен вызывать 500/503 и должен корректно обрабатываться.""" - u = f"nul_user_{uuid.uuid4().hex[:6]}" - res = await client.post( - "/api/v1/users", - json={"username": u, "display_name": "Hello\u0000World", "password": "Password123456!"}, - ) - assert res.status_code == 201 - data = res.json() - assert data["username"] == u - assert data["display_name"] == "HelloWorld" + usr = await register_and_login(client, f"nul_msg_{uuid.uuid4().hex[:4]}") + chat = (await client.post("/api/v1/chats", headers=usr["headers"], json={"title": "NulChat", "member_ids": [usr["user"]["id"]]})).json() + test_text = "left\u0000right" + msg_res = await client.post(f"/api/v1/chats/{chat['id']}/messages", headers=usr["headers"], json={"text": test_text}) + assert msg_res.status_code == 201 + assert msg_res.json()["text"] == test_text + + list_res = await client.get(f"/api/v1/chats/{chat['id']}/messages", headers=usr["headers"]) + assert list_res.status_code == 200 + assert any(m["text"] == test_text for m in list_res.json()["items"]) @pytest.mark.asyncio -- 2.54.0