feat: port replicated messenger baseline into lab4
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
FROM python:3.12.12-slim-bookworm
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt && useradd --system --uid 10001 --home /app app
|
||||
COPY --chown=app:app src/ src/
|
||||
COPY --chown=app:app migrations/ migrations/
|
||||
COPY --chown=app:app scripts/migrate.py scripts/migrate.py
|
||||
USER app
|
||||
EXPOSE 8080
|
||||
CMD ["python", "-m", "src.server"]
|
||||
@@ -0,0 +1,70 @@
|
||||
name: mtusi2026lab4
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17.6-bookworm
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets: [postgres_password]
|
||||
volumes: [postgres_data:/var/lib/postgresql/data]
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
|
||||
migrate:
|
||||
build: .
|
||||
command: ["python", "scripts/migrate.py"]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
secrets: [database_url]
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
build: .
|
||||
scale: 2
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
secrets: [database_url]
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).close()"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
start_period: 5s
|
||||
stop_grace_period: 10s
|
||||
cpus: 0.5
|
||||
mem_limit: 256m
|
||||
|
||||
lb:
|
||||
image: haproxy:3.0.11-alpine
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
ports:
|
||||
- "127.0.0.1:${APP_PORT:-8080}:8080"
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
@@ -0,0 +1,26 @@
|
||||
global
|
||||
log stdout format raw local0
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
timeout connect 2s
|
||||
timeout client 15s
|
||||
timeout server 15s
|
||||
|
||||
resolvers docker
|
||||
nameserver docker 127.0.0.11:53
|
||||
resolve_retries 2
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
|
||||
frontend public
|
||||
bind *:8080
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
server-template api 1-4 api:8080 check inter 1s fall 2 rise 1 resolvers docker init-addr none
|
||||
@@ -0,0 +1,36 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE INDEX chats_history_idx ON chats (created_at, id);
|
||||
|
||||
CREATE TABLE memberships (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
position smallint NOT NULL CHECK (position BETWEEN 0 AND 99),
|
||||
PRIMARY KEY (chat_id, user_id),
|
||||
UNIQUE (chat_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX memberships_user_idx ON memberships (user_id, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL,
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
FOREIGN KEY (chat_id, sender_id) REFERENCES memberships(chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX sessions_user_idx ON sessions (user_id);
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE message_replays (
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
request_key text NOT NULL,
|
||||
request_hash bytea NOT NULL,
|
||||
message_id uuid REFERENCES messages(id),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
PRIMARY KEY (user_id, chat_id, request_key)
|
||||
);
|
||||
|
||||
CREATE INDEX message_replays_expiry_idx ON message_replays (created_at);
|
||||
@@ -0,0 +1,6 @@
|
||||
fastapi==0.118.3
|
||||
pydantic==2.12.4
|
||||
uvicorn==0.38.0
|
||||
psycopg[binary,pool]==3.2.12
|
||||
psycopg-pool==3.3.3
|
||||
argon2-cffi==25.1.0
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Apply numbered SQL migrations once, atomically and under a DB lock."""
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
import psycopg
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from src.db import database_url
|
||||
|
||||
|
||||
def main() -> None:
|
||||
migration_dir = Path(__file__).resolve().parents[1] / "migrations"
|
||||
with psycopg.connect(database_url(), connect_timeout=5, options="-c statement_timeout=30000") as conn:
|
||||
with conn.transaction():
|
||||
conn.execute("SELECT pg_advisory_xact_lock(2026, 2)")
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS schema_migrations ("
|
||||
"version integer PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT clock_timestamp())"
|
||||
)
|
||||
for path in sorted(migration_dir.glob("[0-9][0-9][0-9]_*.sql")):
|
||||
version = int(path.name.split("_", 1)[0])
|
||||
if conn.execute("SELECT 1 FROM schema_migrations WHERE version=%s", (version,)).fetchone():
|
||||
continue
|
||||
conn.execute(path.read_text(encoding="utf-8"))
|
||||
conn.execute("INSERT INTO schema_migrations (version) VALUES (%s)", (version,))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --env-file .env stop api lb postgres
|
||||
docker compose --env-file .env up -d --wait --scale api=2
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
for container in $(docker compose --env-file .env ps -q api); do
|
||||
instance=$(docker exec "$container" python -c "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2).headers['X-Instance-Id'])")
|
||||
if [ "$instance" = "$TARGET_INSTANCE_ID" ]; then
|
||||
docker stop -t 5 "$container" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
echo "target API instance not found" >&2
|
||||
exit 1
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
"""Messenger HTTP routes."""
|
||||
|
||||
import re
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
||||
|
||||
from .auth import basic_credentials, bearer_token
|
||||
from .models import CreateChat, CreateMessage, CreateUser
|
||||
from .store import store
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/health/live")
|
||||
def live():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/health/ready")
|
||||
def ready():
|
||||
return store.ready()
|
||||
|
||||
|
||||
@router.post("/api/v1/users", status_code=201)
|
||||
def register_user(body: CreateUser):
|
||||
return store.register(body.username, body.display_name, body.password)
|
||||
|
||||
|
||||
@router.post("/api/v1/auth/sessions", status_code=201)
|
||||
async def create_session(request: Request, response: Response):
|
||||
if await request.body():
|
||||
raise HTTPException(400)
|
||||
username, password = basic_credentials(request.headers.get("authorization"))
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return store.create_session(username, password)
|
||||
|
||||
|
||||
def current_user(request: Request) -> dict:
|
||||
return store.authenticated(bearer_token(request.headers.get("authorization")))
|
||||
|
||||
|
||||
def parse_limit(value: str) -> int:
|
||||
try:
|
||||
number = int(value) if value.isdecimal() else 0
|
||||
except ValueError:
|
||||
raise HTTPException(400) from None
|
||||
if not 1 <= number <= 100:
|
||||
raise HTTPException(400)
|
||||
return number
|
||||
|
||||
|
||||
@router.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
def revoke_session(request: Request):
|
||||
store.revoke_session(bearer_token(request.headers.get("authorization")))
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.get("/api/v1/users/me")
|
||||
def user_me(actor: dict = Depends(current_user)):
|
||||
return actor
|
||||
|
||||
|
||||
@router.post("/api/v1/chats", status_code=201)
|
||||
def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
|
||||
return store.create_chat(actor["id"], body.title, body.member_ids)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats")
|
||||
def list_chats(
|
||||
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
|
||||
):
|
||||
return store.list_chats(actor["id"], parse_limit(limit), cursor)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}")
|
||||
def get_chat(chat_id: UUID, actor: dict = Depends(current_user)):
|
||||
return store.chat(actor["id"], str(chat_id))
|
||||
|
||||
|
||||
@router.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
def create_message(chat_id: UUID, body: CreateMessage, request: Request, actor: dict = Depends(current_user)):
|
||||
key = request.headers.get("Idempotency-Key")
|
||||
if key is None or not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key):
|
||||
raise HTTPException(400)
|
||||
return store.create_message(actor["id"], str(chat_id), body.text, key, body.attachment_ids)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}/messages")
|
||||
def list_messages(
|
||||
chat_id: UUID,
|
||||
limit: str = Query("50"),
|
||||
cursor: str | None = Query(None, min_length=1),
|
||||
actor: dict = Depends(current_user),
|
||||
):
|
||||
return store.list_messages(actor["id"], str(chat_id), parse_limit(limit), cursor)
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
"""Parse HTTP credentials without exposing them in errors or logs."""
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import hashlib
|
||||
import re
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
USERNAME = re.compile(r"[a-z0-9_]{3,32}\Z")
|
||||
|
||||
|
||||
def unauthorized(challenge: str) -> HTTPException:
|
||||
return HTTPException(401, headers={"WWW-Authenticate": challenge})
|
||||
|
||||
|
||||
def basic_credentials(header: str | None) -> tuple[str, str]:
|
||||
if not header or not header.startswith("Basic "):
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
try:
|
||||
raw = base64.b64decode(header[6:], validate=True).decode("utf-8")
|
||||
username, password = raw.split(":", 1)
|
||||
except (binascii.Error, UnicodeError, ValueError):
|
||||
raise unauthorized(BASIC_CHALLENGE) from None
|
||||
if not USERNAME.fullmatch(username) or not 12 <= len(password) <= 128:
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
return username, password
|
||||
|
||||
|
||||
def bearer_token(header: str | None) -> str:
|
||||
if not header or not header.startswith("Bearer "):
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
token = header[7:]
|
||||
if not 32 <= len(token) <= 256 or not token.isascii() or not re.fullmatch(r"[A-Za-z0-9_-]+", token):
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
return token
|
||||
|
||||
|
||||
def token_hash(token: str) -> bytes:
|
||||
return hashlib.sha256(token.encode("ascii")).digest()
|
||||
@@ -0,0 +1,38 @@
|
||||
"""Bounded PostgreSQL pool shared by API storage and health checks."""
|
||||
|
||||
import os
|
||||
import atexit
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
import psycopg
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool
|
||||
from psycopg_pool import PoolTimeout
|
||||
|
||||
|
||||
def database_url() -> str:
|
||||
path = os.environ.get("DATABASE_URL_FILE")
|
||||
if path:
|
||||
return Path(path).read_text(encoding="utf-8").strip()
|
||||
return os.environ.get("DATABASE_URL", "postgresql:///messenger")
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def pool() -> ConnectionPool:
|
||||
result = ConnectionPool(
|
||||
database_url(), min_size=0, max_size=8, timeout=5,
|
||||
kwargs={"connect_timeout": 5, "options": "-c statement_timeout=5000", "row_factory": dict_row},
|
||||
open=True,
|
||||
)
|
||||
atexit.register(result.close)
|
||||
return result
|
||||
|
||||
|
||||
def ready() -> bool:
|
||||
try:
|
||||
with pool().connection() as conn:
|
||||
conn.execute("SELECT 1 FROM users LIMIT 0")
|
||||
return True
|
||||
except (psycopg.Error, PoolTimeout, OSError):
|
||||
return False
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
"""FastAPI entry point and contract-wide response handling."""
|
||||
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from .api import router
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(router)
|
||||
INSTANCE_ID = uuid4().hex
|
||||
|
||||
ERRORS = {
|
||||
400: ("invalid_request", "Неверный запрос"),
|
||||
401: ("unauthorized", "Требуется действительный пользователь"),
|
||||
404: ("not_found", "Объект не найден"),
|
||||
409: ("conflict", "Конфликт данных"),
|
||||
413: ("payload_too_large", "Слишком большой запрос"),
|
||||
415: ("unsupported_media_type", "Неподдерживаемый тип данных"),
|
||||
429: ("rate_limited", "Слишком много запросов"),
|
||||
503: ("unavailable", "Сервис недоступен"),
|
||||
}
|
||||
|
||||
|
||||
def error_response(request: Request, status: int, extra_headers: dict | None = None) -> JSONResponse:
|
||||
code, message = ERRORS.get(status, ("internal_error", "Внутренняя ошибка"))
|
||||
request_id = request.state.request_id
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content={"error": {"code": code, "message": message, "request_id": request_id}},
|
||||
headers={"X-Request-Id": request_id, "X-Instance-Id": INSTANCE_ID, **(extra_headers or {})},
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_contract(request: Request, call_next):
|
||||
request.state.request_id = str(uuid4())
|
||||
if request.method == "POST" and request.url.path.startswith("/api/v1/") and request.url.path != "/api/v1/auth/sessions":
|
||||
media_type, _, charset = request.headers.get("content-type", "").lower().partition(";")
|
||||
if media_type.strip() != "application/json" or (charset and charset.strip() != "charset=utf-8"):
|
||||
return error_response(request, 415)
|
||||
try:
|
||||
(await request.body()).decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return error_response(request, 400)
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return response
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_error(request: Request, exc: RequestValidationError):
|
||||
return error_response(request, 400)
|
||||
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_error(request: Request, exc: HTTPException):
|
||||
return error_response(request, exc.status_code, exc.headers)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unexpected_error(request: Request, exc: Exception):
|
||||
return error_response(request, 500)
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Request bodies from the lab 1 contract."""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
|
||||
class StrictBody(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
|
||||
class CreateUser(StrictBody):
|
||||
username: str = Field(pattern=r"^[a-z0-9_]{3,32}$")
|
||||
display_name: str = Field(min_length=1, max_length=100)
|
||||
password: str = Field(min_length=12, max_length=128)
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def nonblank_name(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("blank display name")
|
||||
return value
|
||||
|
||||
@field_validator("password")
|
||||
@classmethod
|
||||
def nonblank_password(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("blank password")
|
||||
return value
|
||||
|
||||
|
||||
class CreateChat(StrictBody):
|
||||
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 nonblank_title(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("blank title")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def valid_members(cls, value: list[str]) -> list[str]:
|
||||
members = [str(UUID(member_id)) for member_id in value]
|
||||
if len(set(members)) != len(members):
|
||||
raise ValueError("duplicate member")
|
||||
return members
|
||||
|
||||
|
||||
class CreateMessage(StrictBody):
|
||||
text: str = Field(min_length=1, max_length=4000)
|
||||
attachment_ids: list[str] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def nonblank_text(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("blank text")
|
||||
return value
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Run the ASGI worker as the container's main process."""
|
||||
|
||||
import os
|
||||
|
||||
import uvicorn
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
"src.main:app",
|
||||
host=os.environ.get("APP_HOST", "0.0.0.0"),
|
||||
port=int(os.environ.get("APP_PORT", "8080")),
|
||||
workers=1,
|
||||
)
|
||||
+275
@@ -0,0 +1,275 @@
|
||||
"""PostgreSQL-backed users, chats and message history."""
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime, timezone
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import psycopg
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
from fastapi import HTTPException
|
||||
from psycopg.errors import UniqueViolation
|
||||
from psycopg_pool import PoolTimeout
|
||||
|
||||
from . import db
|
||||
from .auth import BASIC_CHALLENGE, BEARER_CHALLENGE, token_hash, unauthorized
|
||||
|
||||
password_hasher = PasswordHasher()
|
||||
|
||||
|
||||
def timestamp(value: datetime) -> str:
|
||||
return value.astimezone(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
def encode_cursor(resource: str, item: dict) -> str:
|
||||
value = [resource, item["created_at"], item["id"]]
|
||||
return base64.urlsafe_b64encode(json.dumps(value, separators=(",", ":")).encode()).decode().rstrip("=")
|
||||
|
||||
|
||||
def decode_cursor(cursor: str | None, resource: str) -> tuple[datetime, UUID] | None:
|
||||
if cursor is None:
|
||||
return None
|
||||
try:
|
||||
raw = base64.b64decode(cursor + "=" * (-len(cursor) % 4), altchars=b"-_", validate=True)
|
||||
value = json.loads(raw)
|
||||
if not isinstance(value, list) or len(value) != 3 or value[0] != resource:
|
||||
raise ValueError
|
||||
date_text, item_id = value[1:]
|
||||
if not isinstance(date_text, str) or not date_text.endswith("Z") or not isinstance(item_id, str):
|
||||
raise ValueError
|
||||
date = datetime.fromisoformat(date_text[:-1] + "+00:00")
|
||||
if date.utcoffset().total_seconds() != 0:
|
||||
raise ValueError
|
||||
return date, UUID(item_id)
|
||||
except (binascii.Error, UnicodeError, ValueError, TypeError, AttributeError):
|
||||
raise HTTPException(400) from None
|
||||
|
||||
|
||||
@contextmanager
|
||||
def connection():
|
||||
try:
|
||||
with db.pool().connection() as conn:
|
||||
yield conn
|
||||
except (psycopg.Error, PoolTimeout, OSError):
|
||||
raise HTTPException(503) from None
|
||||
|
||||
|
||||
def user_view(row: dict) -> dict:
|
||||
return {
|
||||
"id": str(row["id"]), "username": row["username"],
|
||||
"display_name": row["display_name"], "created_at": timestamp(row["created_at"]),
|
||||
}
|
||||
|
||||
|
||||
def chat_view(row: dict) -> dict:
|
||||
return {
|
||||
"id": str(row["id"]), "title": row["title"],
|
||||
"member_ids": [str(member_id) for member_id in row["member_ids"]],
|
||||
"created_at": timestamp(row["created_at"]),
|
||||
}
|
||||
|
||||
|
||||
def message_view(row: dict) -> dict:
|
||||
return {
|
||||
"id": str(row["id"]), "chat_id": str(row["chat_id"]),
|
||||
"sender_id": str(row["sender_id"]), "text": row["text"],
|
||||
"created_at": timestamp(row["created_at"]),
|
||||
"attachment_ids": [],
|
||||
}
|
||||
|
||||
|
||||
CHAT_COLUMNS = """c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM memberships m WHERE m.chat_id=c.id ORDER BY m.position) AS member_ids"""
|
||||
|
||||
|
||||
class Store:
|
||||
def ready(self) -> dict:
|
||||
if not db.ready():
|
||||
raise HTTPException(503)
|
||||
return {"status": "ok"}
|
||||
|
||||
def register(self, username: str, display_name: str, password: str) -> dict:
|
||||
password_hash = password_hasher.hash(password)
|
||||
with connection() as conn:
|
||||
try:
|
||||
row = conn.execute(
|
||||
"INSERT INTO users (id, username, display_name, password_hash) VALUES (%s,%s,%s,%s) "
|
||||
"RETURNING id,username,display_name,created_at",
|
||||
(uuid4(), username, display_name, password_hash),
|
||||
).fetchone()
|
||||
except UniqueViolation:
|
||||
raise HTTPException(409) from None
|
||||
return user_view(row)
|
||||
|
||||
def user(self, user_id: str | None) -> dict:
|
||||
if not user_id:
|
||||
raise HTTPException(401)
|
||||
try:
|
||||
user_uuid = UUID(user_id)
|
||||
except (ValueError, TypeError):
|
||||
raise HTTPException(401) from None
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT id,username,display_name,created_at FROM users WHERE id=%s", (user_uuid,)
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise HTTPException(401)
|
||||
return user_view(row)
|
||||
|
||||
def create_session(self, username: str, password: str) -> dict:
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT id,username,display_name,created_at,password_hash FROM users WHERE username=%s",
|
||||
(username,),
|
||||
).fetchone()
|
||||
try:
|
||||
valid = row is not None and password_hasher.verify(row["password_hash"], password)
|
||||
except (VerificationError, InvalidHashError):
|
||||
valid = False
|
||||
if not valid:
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
token = secrets.token_urlsafe(32)
|
||||
ttl = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
|
||||
expiry = conn.execute(
|
||||
"INSERT INTO sessions (token_hash,user_id,expires_at) "
|
||||
"VALUES (%s,%s,clock_timestamp()+make_interval(secs => %s)) RETURNING expires_at",
|
||||
(token_hash(token), row["id"], ttl),
|
||||
).fetchone()["expires_at"]
|
||||
return {"token": token, "token_type": "Bearer", "expires_at": timestamp(expiry), "user": user_view(row)}
|
||||
|
||||
def authenticated(self, token: str) -> dict:
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT u.id,u.username,u.display_name,u.created_at FROM sessions s "
|
||||
"JOIN users u ON u.id=s.user_id "
|
||||
"WHERE s.token_hash=%s AND s.expires_at>clock_timestamp()",
|
||||
(token_hash(token),),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
return user_view(row)
|
||||
|
||||
def revoke_session(self, token: str) -> None:
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
"DELETE FROM sessions WHERE token_hash=%s AND expires_at>clock_timestamp() RETURNING token_hash",
|
||||
(token_hash(token),),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
|
||||
def create_chat(self, actor_id: str, title: str, member_ids: list[str]) -> dict:
|
||||
if actor_id not in member_ids:
|
||||
raise HTTPException(400)
|
||||
members = [UUID(member_id) for member_id in member_ids]
|
||||
with connection() as conn:
|
||||
count = conn.execute("SELECT count(*) AS n FROM users WHERE id=ANY(%s)", (members,)).fetchone()["n"]
|
||||
if count != len(members):
|
||||
raise HTTPException(400)
|
||||
row = conn.execute("INSERT INTO chats (id,title) VALUES (%s,%s) RETURNING id,title,created_at", (uuid4(), title)).fetchone()
|
||||
for position, member_id in enumerate(members):
|
||||
conn.execute(
|
||||
"INSERT INTO memberships (chat_id,user_id,position) VALUES (%s,%s,%s)",
|
||||
(row["id"], member_id, position),
|
||||
)
|
||||
return chat_view({**row, "member_ids": members})
|
||||
|
||||
def chat(self, actor_id: str, chat_id: str) -> dict:
|
||||
with connection() as conn:
|
||||
row = conn.execute(
|
||||
f"SELECT {CHAT_COLUMNS} FROM chats c "
|
||||
"JOIN memberships access ON access.chat_id=c.id "
|
||||
"WHERE access.user_id=%s AND c.id=%s",
|
||||
(UUID(actor_id), UUID(chat_id)),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise HTTPException(404)
|
||||
return chat_view(row)
|
||||
|
||||
def list_chats(self, actor_id: str, limit: int, cursor: str | None) -> dict:
|
||||
after = decode_cursor(cursor, f"chats:{actor_id}")
|
||||
query = (f"SELECT {CHAT_COLUMNS} FROM chats c "
|
||||
"JOIN memberships access ON access.chat_id=c.id WHERE access.user_id=%s")
|
||||
params = [UUID(actor_id)]
|
||||
if after:
|
||||
query += " AND (c.created_at,c.id)>(%s,%s)"
|
||||
params.extend(after)
|
||||
query += " ORDER BY c.created_at,c.id LIMIT %s"
|
||||
params.append(limit + 1)
|
||||
with connection() as conn:
|
||||
rows = conn.execute(query, params).fetchall()
|
||||
items = [chat_view(row) for row in rows[:limit]]
|
||||
return {"items": items, "next_cursor": encode_cursor(f"chats:{actor_id}", items[-1]) if len(rows) > limit else None}
|
||||
|
||||
def create_message(self, actor_id: str, chat_id: str, text: str, key: str, attachment_ids: list[str] | None = None) -> dict:
|
||||
if attachment_ids:
|
||||
raise HTTPException(404)
|
||||
chat_uuid, actor_uuid = UUID(chat_id), UUID(actor_id)
|
||||
request_hash = hashlib.sha256(json.dumps({"text": text, "attachment_ids": []}, ensure_ascii=False, sort_keys=True).encode()).digest()
|
||||
with connection() as conn:
|
||||
member = conn.execute(
|
||||
"SELECT 1 FROM memberships WHERE chat_id=%s AND user_id=%s", (chat_uuid, actor_uuid)
|
||||
).fetchone()
|
||||
if member is None:
|
||||
raise HTTPException(404)
|
||||
conn.execute(
|
||||
"DELETE FROM message_replays WHERE user_id=%s AND chat_id=%s AND request_key=%s "
|
||||
"AND created_at<clock_timestamp()-interval '24 hours'",
|
||||
(actor_uuid, chat_uuid, key),
|
||||
)
|
||||
reserved = conn.execute(
|
||||
"INSERT INTO message_replays (user_id,chat_id,request_key,request_hash) VALUES (%s,%s,%s,%s) "
|
||||
"ON CONFLICT DO NOTHING RETURNING request_key",
|
||||
(actor_uuid, chat_uuid, key, request_hash),
|
||||
).fetchone()
|
||||
if reserved is None:
|
||||
replay = conn.execute(
|
||||
"SELECT r.request_hash,m.id,m.chat_id,m.sender_id,m.text,m.created_at "
|
||||
"FROM message_replays r JOIN messages m ON m.id=r.message_id "
|
||||
"WHERE r.user_id=%s AND r.chat_id=%s AND r.request_key=%s",
|
||||
(actor_uuid, chat_uuid, key),
|
||||
).fetchone()
|
||||
if replay is None:
|
||||
raise HTTPException(503)
|
||||
if replay["request_hash"] != request_hash:
|
||||
raise HTTPException(409)
|
||||
return message_view(replay)
|
||||
row = conn.execute(
|
||||
"INSERT INTO messages (id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) "
|
||||
"RETURNING id,chat_id,sender_id,text,created_at",
|
||||
(uuid4(), chat_uuid, actor_uuid, text),
|
||||
).fetchone()
|
||||
conn.execute(
|
||||
"UPDATE message_replays SET message_id=%s WHERE user_id=%s AND chat_id=%s AND request_key=%s",
|
||||
(row["id"], actor_uuid, chat_uuid, key),
|
||||
)
|
||||
return message_view(row)
|
||||
|
||||
def list_messages(self, actor_id: str, chat_id: str, limit: int, cursor: str | None) -> dict:
|
||||
after = decode_cursor(cursor, f"messages:{chat_id}")
|
||||
chat_uuid, actor_uuid = UUID(chat_id), UUID(actor_id)
|
||||
query = "SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=%s"
|
||||
params = [chat_uuid]
|
||||
if after:
|
||||
query += " AND (created_at,id)>(%s,%s)"
|
||||
params.extend(after)
|
||||
query += " ORDER BY created_at,id LIMIT %s"
|
||||
params.append(limit + 1)
|
||||
with connection() as conn:
|
||||
member = conn.execute(
|
||||
"SELECT 1 FROM memberships WHERE chat_id=%s AND user_id=%s", (chat_uuid, actor_uuid)
|
||||
).fetchone()
|
||||
if member is None:
|
||||
raise HTTPException(404)
|
||||
rows = conn.execute(query, params).fetchall()
|
||||
items = [message_view(row) for row in rows[:limit]]
|
||||
return {"items": items, "next_cursor": encode_cursor(f"messages:{chat_id}", items[-1]) if len(rows) > limit else None}
|
||||
|
||||
|
||||
store = Store()
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Invalid pagination limits must remain client errors."""
|
||||
|
||||
import unittest
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from src.api import parse_limit
|
||||
|
||||
|
||||
class LimitTests(unittest.TestCase):
|
||||
def test_valid_and_extremely_long_limits(self):
|
||||
self.assertEqual(parse_limit("100"), 100)
|
||||
for value in ("0", "101", "-1", "x", "9" * 4301):
|
||||
with self.assertRaises(HTTPException) as failure:
|
||||
parse_limit(value)
|
||||
self.assertEqual(failure.exception.status_code, 400)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,174 @@
|
||||
"""HTTP and database checks for durable Basic/Bearer sessions."""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from uuid import uuid4
|
||||
|
||||
import psycopg
|
||||
|
||||
|
||||
URL = os.environ.get("DATABASE_URL")
|
||||
|
||||
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
|
||||
class AuthTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
subprocess.run([sys.executable, "scripts/migrate.py"], check=True)
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
cls.port = sock.getsockname()[1]
|
||||
cls.start_server()
|
||||
|
||||
@classmethod
|
||||
def start_server(cls, ttl="3600"):
|
||||
cls.instance_id = None
|
||||
env = {**os.environ, "SESSION_TTL_SECONDS": ttl}
|
||||
cls.server = subprocess.Popen(
|
||||
[sys.executable, "-m", "uvicorn", "src.main:app", "--host", "127.0.0.1", "--port", str(cls.port)],
|
||||
env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
for _ in range(100):
|
||||
if cls.server.poll() is not None:
|
||||
raise AssertionError("API server exited during startup")
|
||||
try:
|
||||
cls.call("GET", "/health/ready", expected=200)
|
||||
return
|
||||
except (AssertionError, urllib.error.URLError):
|
||||
time.sleep(0.05)
|
||||
raise AssertionError("API server did not become ready")
|
||||
|
||||
@classmethod
|
||||
def stop_server(cls):
|
||||
cls.server.terminate()
|
||||
try:
|
||||
cls.server.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
cls.server.kill()
|
||||
cls.server.wait(timeout=5)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.stop_server()
|
||||
|
||||
@classmethod
|
||||
def call(cls, method, path, *, body=None, authorization=None, user_id=None, expected=200):
|
||||
headers = {"Accept": "application/json"}
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode()
|
||||
headers["Content-Type"] = "application/json"
|
||||
if authorization is not None:
|
||||
headers["Authorization"] = authorization
|
||||
if user_id is not None:
|
||||
headers["X-User-Id"] = user_id
|
||||
request = urllib.request.Request(
|
||||
f"http://127.0.0.1:{cls.port}{path}", data=data, headers=headers, method=method,
|
||||
)
|
||||
try:
|
||||
response = urllib.request.urlopen(request, timeout=5)
|
||||
except urllib.error.HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
status = response.status
|
||||
response_headers = response.headers
|
||||
data = response.read()
|
||||
assert status == expected, (method, path, expected, status)
|
||||
request_id = response_headers.get("X-Request-Id")
|
||||
assert request_id
|
||||
instance_id = response_headers.get("X-Instance-Id")
|
||||
assert instance_id
|
||||
if cls.instance_id is None:
|
||||
cls.instance_id = instance_id
|
||||
else:
|
||||
assert instance_id == cls.instance_id
|
||||
payload = json.loads(data) if data else None
|
||||
if status >= 400:
|
||||
assert payload["error"]["request_id"] == request_id
|
||||
assert "password" not in data.decode().lower()
|
||||
assert "token" not in data.decode().lower()
|
||||
return payload, response_headers
|
||||
|
||||
def user(self, password="Пароль:очень-длинный-123"):
|
||||
username = "u_" + uuid4().hex[:16]
|
||||
body = {"username": username, "display_name": "Алиса", "password": password}
|
||||
user, _ = self.call("POST", "/api/v1/users", body=body, expected=201)
|
||||
self.assertNotIn("password", user)
|
||||
return user, password
|
||||
|
||||
def login(self, username, password, expected=201):
|
||||
encoded = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
return self.call("POST", "/api/v1/auth/sessions", authorization="Basic " + encoded, expected=expected)
|
||||
|
||||
def test_basic_bearer_logout_and_storage(self):
|
||||
alice, password = self.user()
|
||||
bob, _ = self.user()
|
||||
session, headers = self.login(alice["username"], password)
|
||||
self.assertEqual(session["token_type"], "Bearer")
|
||||
self.assertEqual(session["user"], alice)
|
||||
self.assertGreaterEqual(len(base64.urlsafe_b64decode(session["token"] + "=" * (-len(session["token"]) % 4))), 32)
|
||||
self.assertEqual(headers.get("Cache-Control"), "no-store")
|
||||
bearer = "Bearer " + session["token"]
|
||||
me, _ = self.call("GET", "/api/v1/users/me", authorization=bearer, user_id=bob["id"])
|
||||
self.assertEqual(me, alice)
|
||||
with psycopg.connect(URL) as conn:
|
||||
row = conn.execute("SELECT token_hash,expires_at FROM sessions WHERE user_id=%s", (alice["id"],)).fetchone()
|
||||
password_hash = conn.execute("SELECT password_hash FROM users WHERE id=%s", (alice["id"],)).fetchone()[0]
|
||||
self.assertEqual(row[0], hashlib.sha256(session["token"].encode()).digest())
|
||||
self.assertNotEqual(password_hash, password)
|
||||
self.assertTrue(password_hash.startswith("$argon2id$"))
|
||||
self.call("DELETE", "/api/v1/auth/sessions/current", authorization=bearer, expected=204)
|
||||
for method, path in (("GET", "/api/v1/users/me"), ("DELETE", "/api/v1/auth/sessions/current")):
|
||||
_, headers = self.call(method, path, authorization=bearer, expected=401)
|
||||
self.assertEqual(headers.get("WWW-Authenticate"), 'Bearer realm="messenger"')
|
||||
|
||||
def test_challenges_and_uniform_credentials(self):
|
||||
user, password = self.user()
|
||||
failures = []
|
||||
for username, candidate in ((user["username"], "wrong-password-123"), ("missing_user_123", password)):
|
||||
payload, headers = self.login(username, candidate, expected=401)
|
||||
failures.append((payload["error"]["code"], payload["error"]["message"]))
|
||||
self.assertEqual(headers.get("WWW-Authenticate"), 'Basic realm="messenger", charset="UTF-8"')
|
||||
self.assertEqual(failures[0], failures[1])
|
||||
encoded = base64.b64encode(f"{user['username']}:{password}".encode()).decode()
|
||||
for authorization in (None, "Basic " + encoded, "Bearer garbage"):
|
||||
_, headers = self.call("GET", "/api/v1/users/me", authorization=authorization, user_id=user["id"], expected=401)
|
||||
self.assertEqual(headers.get("WWW-Authenticate"), 'Bearer realm="messenger"')
|
||||
_, headers = self.call("POST", "/api/v1/auth/sessions", authorization="Bearer garbage", expected=401)
|
||||
self.assertEqual(headers.get("WWW-Authenticate"), 'Basic realm="messenger", charset="UTF-8"')
|
||||
self.call("POST", "/api/v1/auth/sessions", authorization="Basic " + encoded, body={"unexpected": True}, expected=400)
|
||||
|
||||
def test_z_short_ttl_survives_server_restart_without_extension(self):
|
||||
self.stop_server()
|
||||
self.start_server(ttl="2")
|
||||
try:
|
||||
user, password = self.user()
|
||||
session, _ = self.login(user["username"], password)
|
||||
bearer = "Bearer " + session["token"]
|
||||
self.call("GET", "/api/v1/users/me", authorization=bearer)
|
||||
with psycopg.connect(URL) as conn:
|
||||
before = conn.execute("SELECT expires_at FROM sessions WHERE user_id=%s", (user["id"],)).fetchone()[0]
|
||||
self.stop_server()
|
||||
self.start_server(ttl="3600")
|
||||
with psycopg.connect(URL) as conn:
|
||||
after = conn.execute("SELECT expires_at FROM sessions WHERE user_id=%s", (user["id"],)).fetchone()[0]
|
||||
self.assertEqual(before, after)
|
||||
time.sleep(2.1)
|
||||
_, headers = self.call("GET", "/api/v1/users/me", authorization=bearer, expected=401)
|
||||
self.assertEqual(headers.get("WWW-Authenticate"), 'Bearer realm="messenger"')
|
||||
finally:
|
||||
self.stop_server()
|
||||
self.start_server()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,85 @@
|
||||
"""Integration checks against a disposable PostgreSQL database."""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from uuid import uuid4
|
||||
|
||||
import psycopg
|
||||
|
||||
from src.store import Store
|
||||
|
||||
|
||||
URL = os.environ.get("DATABASE_URL")
|
||||
|
||||
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
|
||||
class DatabaseTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
for _ in range(2):
|
||||
subprocess.run([sys.executable, "scripts/migrate.py"], check=True)
|
||||
cls.store = Store()
|
||||
|
||||
def user(self):
|
||||
name = "u_" + uuid4().hex[:16]
|
||||
return self.store.register(name, "Студент", "Test-password-123")
|
||||
|
||||
def test_migration_and_unique_username_race(self):
|
||||
with psycopg.connect(URL) as conn:
|
||||
versions = conn.execute("SELECT version FROM schema_migrations").fetchall()
|
||||
self.assertEqual(versions, [(1,), (2,), (3,)])
|
||||
name = "u_" + uuid4().hex[:16]
|
||||
|
||||
def register(_):
|
||||
try:
|
||||
return self.store.register(name, "Студент", "Test-password-123")
|
||||
except Exception as exc:
|
||||
return exc
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
results = list(pool.map(register, range(2)))
|
||||
self.assertEqual(sum(isinstance(result, dict) for result in results), 1)
|
||||
self.assertEqual([result.status_code for result in results if not isinstance(result, dict)], [409])
|
||||
with psycopg.connect(URL) as conn:
|
||||
self.assertEqual(conn.execute("SELECT count(*) FROM users WHERE username=%s", (name,)).fetchone()[0], 1)
|
||||
password_hash = conn.execute("SELECT password_hash FROM users WHERE username=%s", (name,)).fetchone()[0]
|
||||
self.assertTrue(password_hash.startswith("$argon2id$"))
|
||||
self.assertNotIn("password", str(next(result for result in results if isinstance(result, dict))))
|
||||
subprocess.run([sys.executable, "scripts/migrate.py"], check=True)
|
||||
with psycopg.connect(URL) as conn:
|
||||
self.assertEqual(conn.execute("SELECT count(*) FROM users WHERE username=%s", (name,)).fetchone()[0], 1)
|
||||
|
||||
def test_chat_rollback_and_keyset_history(self):
|
||||
alice, bob, eve = self.user(), self.user(), self.user()
|
||||
with psycopg.connect(URL) as conn:
|
||||
before = conn.execute("SELECT count(*) FROM chats").fetchone()[0]
|
||||
with self.assertRaises(Exception) as failure:
|
||||
self.store.create_chat(alice["id"], "invalid", [alice["id"], str(uuid4())])
|
||||
self.assertEqual(failure.exception.status_code, 400)
|
||||
with psycopg.connect(URL) as conn:
|
||||
self.assertEqual(conn.execute("SELECT count(*) FROM chats").fetchone()[0], before)
|
||||
|
||||
chat = self.store.create_chat(alice["id"], "Valid", [alice["id"], bob["id"]])
|
||||
chat2 = self.store.create_chat(alice["id"], "Second", [alice["id"]])
|
||||
chat_page = self.store.list_chats(alice["id"], 1, None)
|
||||
self.assertEqual(chat_page["items"], [chat])
|
||||
self.assertEqual(self.store.list_chats(alice["id"], 1, chat_page["next_cursor"]), {"items": [chat2], "next_cursor": None})
|
||||
first = self.store.create_message(alice["id"], chat["id"], "первое", uuid4().hex)
|
||||
second = self.store.create_message(bob["id"], chat["id"], "второе", uuid4().hex)
|
||||
page = self.store.list_messages(alice["id"], chat["id"], 1, None)
|
||||
self.assertEqual(page["items"], [first])
|
||||
self.assertEqual(self.store.list_messages(bob["id"], chat["id"], 1, page["next_cursor"]), {"items": [second], "next_cursor": None})
|
||||
self.assertEqual(Store().chat(bob["id"], chat["id"]), chat)
|
||||
with self.assertRaises(Exception) as wrong_cursor:
|
||||
self.store.list_messages(alice["id"], chat2["id"], 1, page["next_cursor"])
|
||||
self.assertEqual(wrong_cursor.exception.status_code, 400)
|
||||
with self.assertRaises(Exception) as hidden:
|
||||
self.store.chat(eve["id"], chat["id"])
|
||||
self.assertEqual(hidden.exception.status_code, 404)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,53 @@
|
||||
"""Concurrent PostgreSQL-backed message retries create one row."""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from uuid import uuid4
|
||||
|
||||
import psycopg
|
||||
|
||||
from src.store import Store
|
||||
|
||||
|
||||
URL = os.environ.get("DATABASE_URL")
|
||||
|
||||
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
|
||||
class IdempotencyTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
subprocess.run([sys.executable, "scripts/migrate.py"], check=True)
|
||||
cls.store = Store()
|
||||
|
||||
def test_parallel_replay_conflict_and_scope(self):
|
||||
alice = self.store.register("u_" + uuid4().hex[:16], "Алиса", "Test-password-123")
|
||||
bob = self.store.register("u_" + uuid4().hex[:16], "Боб", "Test-password-123")
|
||||
chat = self.store.create_chat(alice["id"], "Test", [alice["id"], bob["id"]])
|
||||
other_chat = self.store.create_chat(alice["id"], "Other", [alice["id"]])
|
||||
key = uuid4().hex
|
||||
|
||||
def send(_):
|
||||
return Store().create_message(alice["id"], chat["id"], "same body", key, [] if _ else None)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
first, replay = list(pool.map(send, range(2)))
|
||||
self.assertEqual(first, replay)
|
||||
with psycopg.connect(URL) as conn:
|
||||
count = conn.execute(
|
||||
"SELECT count(*) FROM messages WHERE chat_id=%s AND sender_id=%s AND text=%s",
|
||||
(chat["id"], alice["id"], "same body"),
|
||||
).fetchone()[0]
|
||||
self.assertEqual(count, 1)
|
||||
|
||||
with self.assertRaises(Exception) as conflict:
|
||||
self.store.create_message(alice["id"], chat["id"], "different body", key)
|
||||
self.assertEqual(conflict.exception.status_code, 409)
|
||||
self.assertNotEqual(self.store.create_message(bob["id"], chat["id"], "same body", key)["id"], first["id"])
|
||||
self.assertNotEqual(self.store.create_message(alice["id"], other_chat["id"], "same body", key)["id"], first["id"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Every API response identifies the live replica process."""
|
||||
|
||||
import json
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
class InstanceHeaderTests(unittest.TestCase):
|
||||
def test_process_alias_on_success_and_error(self):
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
port = sock.getsockname()[1]
|
||||
server = subprocess.Popen(
|
||||
[sys.executable, "-m", "uvicorn", "src.main:app", "--host", "127.0.0.1", "--port", str(port)],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
try:
|
||||
for _ in range(100):
|
||||
try:
|
||||
with urllib.request.urlopen(f"http://127.0.0.1:{port}/health/live", timeout=1) as response:
|
||||
live_id = response.headers.get("X-Instance-Id")
|
||||
self.assertEqual(json.load(response), {"status": "ok"})
|
||||
break
|
||||
except urllib.error.URLError:
|
||||
if server.poll() is not None:
|
||||
self.fail("API exited before readiness")
|
||||
time.sleep(0.05)
|
||||
else:
|
||||
self.fail("API did not start")
|
||||
self.assertTrue(live_id)
|
||||
for path in ("/health/live", "/no-such-route"):
|
||||
try:
|
||||
response = urllib.request.urlopen(f"http://127.0.0.1:{port}{path}", timeout=1)
|
||||
except urllib.error.HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
self.assertEqual(response.headers.get("X-Instance-Id"), live_id)
|
||||
self.assertTrue(response.headers.get("X-Request-Id"))
|
||||
finally:
|
||||
server.terminate()
|
||||
try:
|
||||
server.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
server.kill()
|
||||
server.wait(timeout=5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,27 @@
|
||||
"""Text-only messages retain lab3 behavior under the lab4 wire schema."""
|
||||
|
||||
import unittest
|
||||
from datetime import datetime, timezone
|
||||
from uuid import uuid4
|
||||
|
||||
from src.models import CreateMessage
|
||||
from src.store import message_view
|
||||
|
||||
|
||||
class Lab3CompatibilityTests(unittest.TestCase):
|
||||
def test_missing_and_explicit_empty_attachments_are_equivalent(self):
|
||||
missing = CreateMessage.model_validate({"text": "hello"})
|
||||
explicit = CreateMessage.model_validate({"text": "hello", "attachment_ids": []})
|
||||
self.assertEqual(missing, explicit)
|
||||
self.assertEqual(missing.attachment_ids, [])
|
||||
|
||||
def test_text_message_response_has_empty_attachment_ids(self):
|
||||
row = {
|
||||
"id": uuid4(), "chat_id": uuid4(), "sender_id": uuid4(),
|
||||
"text": "hello", "created_at": datetime.now(timezone.utc),
|
||||
}
|
||||
self.assertEqual(message_view(row)["attachment_ids"], [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user