From a8c6af8c7d21359e5f952d74d3f27183af59fbe4 Mon Sep 17 00:00:00 2001 From: Alexey Bodanyuk <144894236+wfa22@users.noreply.github.com> Date: Mon, 12 Oct 2026 19:34:49 +0300 Subject: [PATCH] =?UTF-8?q?test:=20=D1=81=D0=BE=D0=B1=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= =?UTF-8?q?=20lab2=20(unit,=20API,=20=D0=91=D0=94,=20=D0=BE=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=D0=B7=D1=8B,=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8,=20E?= =?UTF-8?q?XPLAIN)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Гонка регистрации (ровно 1x201 + 7x409, одна строка), rollback после первого INSERT, повтор миграций, 10x10 сообщений, отказ/пауза PostgreSQL и Valkey с действующим токеном (503 в пределах дедлайна, восстановление в finally), lock_timeout, logout при отказе Valkey, короткий TTL, SESSION_STORE=postgres, EXPLAIN поздней страницы при generic plan. Co-Authored-By: Claude Opus 5.5 --- Makefile | 35 ++++ scripts/explain_history.py | 114 ++++++++++++ student-tests/conftest.py | 199 ++++++++++++++++---- student-tests/http_concurrency.py | 134 -------------- student-tests/test_api.py | 201 +++++++++++++++++++++ student-tests/test_chats_and_messages.py | 219 ----------------------- student-tests/test_db.py | 114 ++++++++++++ student-tests/test_explain.py | 35 ++++ student-tests/test_failures.py | 99 ++++++++++ student-tests/test_sessions.py | 64 +++++++ student-tests/test_store_concurrency.py | 131 -------------- student-tests/test_users_and_errors.py | 139 -------------- student-tests/unit/test_unit.py | 134 ++++++++++++++ 13 files changed, 963 insertions(+), 655 deletions(-) create mode 100644 scripts/explain_history.py delete mode 100644 student-tests/http_concurrency.py create mode 100644 student-tests/test_api.py delete mode 100644 student-tests/test_chats_and_messages.py create mode 100644 student-tests/test_db.py create mode 100644 student-tests/test_explain.py create mode 100644 student-tests/test_failures.py create mode 100644 student-tests/test_sessions.py delete mode 100644 student-tests/test_store_concurrency.py delete mode 100644 student-tests/test_users_and_errors.py create mode 100644 student-tests/unit/test_unit.py diff --git a/Makefile b/Makefile index 9ecb223..521949a 100644 --- a/Makefile +++ b/Makefile @@ -24,3 +24,38 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +# --------------------------------------------------------------------------- +# Student solution (lab 2, Python). On Windows without make run the commands from +# REPORT.md directly (public tests there need PYTHONUTF8=1; restart hook: scripts/restart.cmd). +.PHONY: secrets up down logs ps migrate student-test unit-test explain + +secrets: + $(PYTHON) scripts/init_secrets.py + +# Build, apply migrations (service migrate) and wait until every service is healthy. +up: secrets + docker compose up -d --build --wait + +# Safe stop: containers stop, named volumes (data) stay. +down: + docker compose down + +logs: + docker compose logs --tail 50 api + +ps: + docker compose ps + +migrate: + docker compose run --rm migrate + +# Own tests: unit + black-box against the running stand (needs requirements*.txt in a venv). +student-test: + $(PYTHON) -m pytest student-tests -v + +unit-test: + $(PYTHON) -m pytest student-tests -m "not stack" -q + +explain: + $(PYTHON) scripts/explain_history.py --output evidence/explain-history.txt diff --git a/scripts/explain_history.py b/scripts/explain_history.py new file mode 100644 index 0000000..0a929ed --- /dev/null +++ b/scripts/explain_history.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 +"""EXPLAIN (ANALYZE, BUFFERS) of the history query on >= 10 000 messages (level 4). + + python scripts/explain_history.py --output evidence/explain-history.txt + +Creates (once, idempotent) a synthetic chat with 20 000 messages plus 30 000 messages in +30 other chats, then explains the EXACT statements of src/messenger/repo.py: + 1. first page (HISTORY_FIRST) + 2. late page via cursor (HISTORY_AFTER) — generic plan forced, as a prepared statement + reused by the driver would get it + 3. the rejected variant `($2 IS NULL OR (created_at, id) > ...)` — generic plan + 4. the rejected OFFSET pagination for the same late page +Runs psql inside the postgres container; no database port is published. +""" +from __future__ import annotations + +import argparse +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "src")) +from messenger.repo import HISTORY_AFTER, HISTORY_FIRST # noqa: E402 (the app's real SQL) + +CHAT = "00000000-0000-4000-8000-00000000e002" +USER = "00000000-0000-4000-8000-00000000e001" +TARGET = 20_000 +LATE = 19_950 # cursor position: the 50 newest messages remain + +SEED = f""" +\\set ON_ERROR_STOP on +BEGIN; +INSERT INTO users (id, username, display_name, password_hash) +VALUES ('{USER}', 'explain_seed', convert_to('Explain seed', 'UTF8'), 'not-a-login') ON CONFLICT DO NOTHING; +INSERT INTO chats (id, title, created_by, created_at) +SELECT ('00000000-0000-4000-8000-0000000' || lpad(to_hex(n), 5, '0'))::uuid, convert_to('explain ' || n, 'UTF8'), + '{USER}', timestamptz '2026-01-01 00:00:00+00' +FROM generate_series(57346::int, 57376::int) AS n -- e002 .. e020: target + 30 noise chats +ON CONFLICT DO NOTHING; +INSERT INTO chat_members (chat_id, user_id, position, chat_created_at) +SELECT id, '{USER}', 0, created_at FROM chats WHERE created_by = '{USER}' ON CONFLICT DO NOTHING; +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), '{CHAT}', '{USER}', convert_to('synthetic message ' || g, 'UTF8'), + timestamptz '2026-01-01 00:00:00+00' + g * interval '1 second' +FROM generate_series(1::int, {TARGET}::int) AS g +WHERE NOT EXISTS (SELECT 1 FROM messages WHERE chat_id = '{CHAT}'); +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), c.id, '{USER}', convert_to('noise ' || g, 'UTF8'), + timestamptz '2026-01-01 00:00:00+00' + g * interval '1 second' +FROM chats c CROSS JOIN generate_series(1::int, 1000::int) AS g +WHERE c.created_by = '{USER}' AND c.id <> '{CHAT}' + AND NOT EXISTS (SELECT 1 FROM messages m WHERE m.chat_id = c.id); +COMMIT; +ANALYZE messages; +""" + + +def pg(sql_param: str) -> str: + """Turn psycopg %s placeholders into $1..$n for a PREPARE statement.""" + parts = sql_param.split("%s") + return "".join(p + (f"${i + 1}" if i < len(parts) - 1 else "") for i, p in enumerate(parts)) + + +EXPLAIN = f""" +\\set ON_ERROR_STOP on +\\pset pager off +SELECT count(*) AS messages_total, count(*) FILTER (WHERE chat_id = '{CHAT}') AS messages_in_target_chat FROM messages; +SELECT created_at AS cur_ts, id AS cur_id FROM messages WHERE chat_id = '{CHAT}' +ORDER BY created_at, id OFFSET {LATE - 1} LIMIT 1 \\gset +SET plan_cache_mode = force_generic_plan; +PREPARE first_page(uuid, int) AS {pg(HISTORY_FIRST)}; +PREPARE after_page(uuid, timestamptz, uuid, int) AS {pg(HISTORY_AFTER)}; +PREPARE or_page(uuid, timestamptz, uuid, int) AS + SELECT id, sender_id, text, created_at FROM messages + WHERE chat_id = $1 AND ($2 IS NULL OR (created_at, id) > ($2, $3)) ORDER BY created_at, id LIMIT $4; +PREPARE offset_page(uuid, int, int) AS + SELECT id, sender_id, text, created_at FROM messages WHERE chat_id = $1 ORDER BY created_at, id OFFSET $2 LIMIT $3; +\\echo '=== 1. first page, app query HISTORY_FIRST, generic plan' +EXPLAIN (ANALYZE, BUFFERS, SUMMARY ON) EXECUTE first_page('{CHAT}', 51); +\\echo '=== 2. late page (cursor at {LATE}), app query HISTORY_AFTER, generic plan' +EXPLAIN (ANALYZE, BUFFERS, SUMMARY ON) EXECUTE after_page('{CHAT}', :'cur_ts', :'cur_id', 51); +\\echo '=== 3. REJECTED: one query with ($2 IS NULL OR ...), same late page, generic plan' +EXPLAIN (ANALYZE, BUFFERS, SUMMARY ON) EXECUTE or_page('{CHAT}', :'cur_ts', :'cur_id', 51); +\\echo '=== 4. REJECTED: OFFSET pagination, same late page' +EXPLAIN (ANALYZE, BUFFERS, SUMMARY ON) EXECUTE offset_page('{CHAT}', {LATE}, 51); +""" + + +def psql(script: str) -> str: + result = subprocess.run( + ["docker", "compose", "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-X", "-q", "-f", "-"], + cwd=ROOT, input=script, capture_output=True, text=True, encoding="utf-8", check=True) + return result.stdout + + +def run() -> str: + psql(SEED) + return psql(EXPLAIN) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("--output", default=None) + args = parser.parse_args() + text = run() + print(text) + if args.output: + Path(args.output).write_text(text, encoding="utf-8", newline="\n") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/student-tests/conftest.py b/student-tests/conftest.py index 14b0270..f09a6ad 100644 --- a/student-tests/conftest.py +++ b/student-tests/conftest.py @@ -1,25 +1,40 @@ -"""Shared fixtures: an isolated in-process app per test + OpenAPI schema validation.""" +"""Shared helpers for the student tests. + +* `unit/` — pure functions (validation, cursor, auth headers, Argon2id); no services. +* `test_*` in this folder — marker `stack`: black-box HTTP against the running compose + stand (BASE_URL, default http://127.0.0.1:8080) plus `docker compose` for database + checks (psql inside the postgres container) and failure injection. Every failure + test restores the stopped/paused service in `finally`. + + python -m pytest student-tests # everything (stand must be up) + python -m pytest student-tests -m "not stack" # unit only +""" from __future__ import annotations +import base64 +import contextlib import json import os +import subprocess import sys +import time import uuid from pathlib import Path +import httpx2 as httpx import pytest from jsonschema import Draft202012Validator -from starlette.testclient import TestClient ROOT = Path(__file__).resolve().parents[1] -# MESSENGER_SRC lets scripts/race_mutation_check.py run the same tests against a patched copy. -sys.path.insert(0, os.environ.get("MESSENGER_SRC", str(ROOT / "src"))) - -from messenger.app import create_app # noqa: E402 -from messenger.config import Settings # noqa: E402 +sys.path.insert(0, str(ROOT / "src")) SPEC = json.loads((ROOT / "contracts" / "openapi.json").read_text(encoding="utf-8")) -SETTINGS = Settings(host="127.0.0.1", port=8080, env="test", log_level="warning", shutdown_timeout=1.0, max_body_bytes=64 * 1024) +BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/") +PASSWORD = "Test-password-Ω-12345" # synthetic; 12+ code points, non-ASCII on purpose + + +def pytest_configure(config): + config.addinivalue_line("markers", "stack: needs the running compose stand (docker compose up -d --wait)") def schema_errors(value, name: str) -> list[str]: @@ -29,63 +44,183 @@ def schema_errors(value, name: str) -> list[str]: return [e.message for e in validator.iter_errors(value)] +def basic(username: str, password: str) -> str: + return "Basic " + base64.b64encode(f"{username}:{password}".encode()).decode() + + +# ------------------------------------------------------------------ docker / psql +def compose(*args: str, check: bool = True, timeout: float = 120) -> subprocess.CompletedProcess: + return subprocess.run(["docker", "compose", *args], cwd=ROOT, capture_output=True, text=True, + encoding="utf-8", check=check, timeout=timeout) + + +def psql(sql: str) -> str: + """Run SQL inside the postgres container (no DB port is published) and return unaligned output.""" + result = compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", + "-At", "-v", "ON_ERROR_STOP=1", "-c", sql) + return result.stdout.strip() + + +def wait_ready(timeout: float = 90) -> None: + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + if httpx.get(BASE_URL + "/health/ready", timeout=2).status_code == 200: + return + except httpx.HTTPError: + pass + time.sleep(0.3) + raise AssertionError("stand did not become ready") + + +def wait_healthy(*services: str, timeout: float = 120) -> None: + """Poll Docker health until every service is healthy again. `compose up --wait` is not used: + it gives up at once on a container that was marked unhealthy while paused.""" + deadline = time.monotonic() + timeout + pending = set(services) + while pending and time.monotonic() < deadline: + for service in list(pending): + ids = compose("ps", "-q", service, check=False).stdout.split() + if ids: + state = subprocess.run(["docker", "inspect", "-f", "{{.State.Health.Status}}", ids[0]], + capture_output=True, text=True).stdout.strip() + if state == "healthy": + pending.discard(service) + time.sleep(0.5) + assert not pending, f"not healthy: {pending}" + + +def restart_services(*services: str) -> None: + compose("restart", *services) + wait_healthy(*services) + wait_ready() + + +@contextlib.contextmanager +def service_down(service: str, how: str = "stop"): + """Stop or pause a compose service; ALWAYS bring it back and wait for readiness.""" + assert how in {"stop", "pause"} + compose(how, service) + try: + yield + finally: + compose("unpause" if how == "pause" else "start", service) + wait_healthy(service) + wait_ready() + + +@contextlib.contextmanager +def api_with_env(**overrides: str): + """Recreate the api container with environment overrides (e.g. SESSION_TTL_SECONDS=2), + then ALWAYS recreate it with the normal configuration.""" + env = {**os.environ, **overrides} + subprocess.run(["docker", "compose", "up", "-d", "--wait", "api"], cwd=ROOT, env=env, + capture_output=True, text=True, check=True, timeout=180) + wait_ready() + try: + yield + finally: + clean = {k: v for k, v in os.environ.items() if k not in overrides} + subprocess.run(["docker", "compose", "up", "-d", "--wait", "api"], cwd=ROOT, env=clean, + capture_output=True, text=True, check=True, timeout=180) + wait_ready() + + +def api_env(name: str) -> str: + """Current value of an environment variable inside the running api container.""" + return compose("exec", "-T", "api", "printenv", name).stdout.strip() + + +# ------------------------------------------------------------------ HTTP class Api: - """Thin helper over TestClient that checks the invariants every response must satisfy.""" + """HTTP helper that checks the invariants every response must satisfy.""" - def __init__(self, client: TestClient): - self.client = client + def __init__(self, base_url: str = BASE_URL): + self.client = httpx.Client(base_url=base_url, timeout=15) - def call(self, method: str, path: str, expected: int, *, user=None, json_body=None, raw=None, headers=None, schema=None): + def call(self, method: str, path: str, expected: int | tuple[int, ...], *, token: str | None = None, + json_body=None, raw: bytes | None = None, headers: dict | None = None, schema: str | None = None): h = dict(headers or {}) - if user is not None: - h["X-User-Id"] = user["id"] + if token is not None: + h["Authorization"] = "Bearer " + token if json_body is not None: raw = json.dumps(json_body, ensure_ascii=False).encode("utf-8") h.setdefault("Content-Type", "application/json") response = self.client.request(method, path, content=raw, headers=h) - assert response.status_code == expected, (method, path, response.status_code, response.text) + allowed = (expected,) if isinstance(expected, int) else expected + assert response.status_code in allowed, (method, path, response.status_code, response.text[:300]) request_id = response.headers.get("x-request-id") assert request_id, "every response must carry X-Request-Id" if response.status_code >= 400: body = response.json() - assert schema_errors(body, "Error") == [] + assert schema_errors(body, "Error") == [], response.text assert body["error"]["request_id"] == request_id - assert "Traceback" not in response.text + assert "Traceback" not in response.text and "psycopg" not in response.text + if response.status_code == 401: + challenge = "basic" if path == "/api/v1/auth/sessions" else "bearer" + assert response.headers.get("www-authenticate", "").lower().startswith(challenge) elif schema: assert schema_errors(response.json(), schema) == [], response.text return response - def user(self, label: str = "u", display_name: str | None = None) -> dict: - body = {"username": f"{label}_{uuid.uuid4().hex[:10]}", "display_name": display_name or f"Студент {label}"} - return self.call("POST", "/api/v1/users", 201, json_body=body, schema="User").json() + def register(self, label: str = "u", *, password: str = PASSWORD, display_name: str | None = None) -> dict: + username = f"{label}_{uuid.uuid4().hex[:12]}"[:32] + body = {"username": username, "display_name": display_name or f"Студент {label}", "password": password} + user = self.call("POST", "/api/v1/users", 201, json_body=body, schema="User").json() + return {"id": user["id"], "user": user, "username": username, "password": password} + + def login(self, actor: dict) -> str: + session = self.call("POST", "/api/v1/auth/sessions", 201, + headers={"Authorization": basic(actor["username"], actor["password"])}, + schema="Session").json() + assert session["user"] == actor["user"] and session["token_type"] == "Bearer" + return session["token"] + + def user(self, label: str = "u", **kwargs) -> dict: + actor = self.register(label, **kwargs) + actor["token"] = self.login(actor) + return actor def chat(self, owner: dict, *members: dict, title: str = "Тестовый чат") -> dict: ids = [owner["id"], *(m["id"] for m in members)] - return self.call("POST", "/api/v1/chats", 201, user=owner, json_body={"title": title, "member_ids": ids}, schema="Chat").json() + return self.call("POST", "/api/v1/chats", 201, token=owner["token"], + json_body={"title": title, "member_ids": ids}, schema="Chat").json() - def send(self, user: dict, chat: dict, text: str) -> dict: - return self.call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, user=user, json_body={"text": text}, schema="Message").json() + def send(self, actor: dict, chat: dict, text: str) -> dict: + return self.call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, token=actor["token"], + json_body={"text": text}, schema="Message").json() - def history(self, user: dict, chat: dict, limit: int) -> tuple[list[dict], int]: + def history(self, actor: dict, chat: dict, limit: int) -> tuple[list[dict], int]: """Walk all pages; returns (items, number_of_pages).""" items, pages, cursor = [], 0, None while True: query = f"?limit={limit}" + (f"&cursor={cursor}" if cursor else "") - page = self.call("GET", f"/api/v1/chats/{chat['id']}/messages{query}", 200, user=user, schema="MessagePage").json() + page = self.call("GET", f"/api/v1/chats/{chat['id']}/messages{query}", 200, + token=actor["token"], schema="MessagePage").json() pages += 1 items.extend(page["items"]) cursor = page["next_cursor"] if cursor is None: return items, pages assert len(page["items"]) == limit, "a non-final page must be full" + assert len(items) < 100_000, "cursor loop" -@pytest.fixture -def app(): - return create_app(SETTINGS) +def _stand_is_up() -> bool: + try: + return httpx.get(BASE_URL + "/health/ready", timeout=3).status_code == 200 + except httpx.HTTPError: + return False -@pytest.fixture -def api(app) -> Api: - with TestClient(app, raise_server_exceptions=False) as client: - yield Api(client) +def pytest_collection_modifyitems(config, items): + if any("stack" in item.keywords for item in items) and not _stand_is_up(): + skip = pytest.mark.skip(reason=f"stand is not running at {BASE_URL}: docker compose up -d --wait") + for item in items: + if "stack" in item.keywords: + item.add_marker(skip) + + +@pytest.fixture(scope="session") +def api() -> Api: + return Api() diff --git a/student-tests/http_concurrency.py b/student-tests/http_concurrency.py deleted file mode 100644 index 19c3a21..0000000 --- a/student-tests/http_concurrency.py +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/env python3 -"""Black-box race check against a RUNNING server (level 4 criterion). - -10 clients (10 different users, one thread each) send 10 messages each into one -chat at the same moment. Then the whole history is read page by page through -the cursor. PASS requires: 100 confirmed writes, exactly 100 unique messages, -nothing lost, strict (created_at, id) order and per-client order preserved. - - python student-tests/http_concurrency.py --base-url http://127.0.0.1:8080 \ - --output evidence/concurrency.json - -Standard library only (Python 3.9+). Creates synthetic users; run on a lab stand. -""" -from __future__ import annotations - -import argparse -import json -import sys -import threading -import time -import urllib.error -import urllib.request -import uuid -from pathlib import Path - - -def call(base: str, method: str, path: str, body=None, user_id=None, expected=200): - data = None if body is None else json.dumps(body, ensure_ascii=False).encode("utf-8") - headers = {"Accept": "application/json"} - if data is not None: - headers["Content-Type"] = "application/json" - if user_id: - headers["X-User-Id"] = user_id - request = urllib.request.Request(base + path, data=data, headers=headers, method=method) - try: - with urllib.request.urlopen(request, timeout=10) as response: - status, payload = response.status, response.read() - except urllib.error.HTTPError as error: - status, payload = error.code, error.read() - if status != expected: - raise AssertionError(f"{method} {path.split('?')[0]}: expected {expected}, got {status}") - return json.loads(payload) - - -def main() -> int: - parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) - parser.add_argument("--base-url", default="http://127.0.0.1:8080") - parser.add_argument("--clients", type=int, default=10) - parser.add_argument("--messages", type=int, default=10, help="messages per client") - parser.add_argument("--page-limit", type=int, default=7, help="small limit => many cursor pages") - parser.add_argument("--output", default=None, help="optional JSON summary (no secrets inside)") - args = parser.parse_args() - base = args.base_url.rstrip("/") - total = args.clients * args.messages - - run = uuid.uuid4().hex[:8] - users = [call(base, "POST", "/api/v1/users", {"username": f"race_{run}_{i}", "display_name": f"Клиент {i}"}, expected=201) - for i in range(args.clients)] - chat = call(base, "POST", "/api/v1/chats", {"title": f"race {run}", "member_ids": [u["id"] for u in users]}, - user_id=users[0]["id"], expected=201) - - barrier = threading.Barrier(args.clients) - confirmed: list[dict] = [] - errors: list[str] = [] - lock = threading.Lock() - - def client(index: int) -> None: - barrier.wait() # all clients start sending at the same moment - for j in range(args.messages): - try: - message = call(base, "POST", f"/api/v1/chats/{chat['id']}/messages", - {"text": f"c{index}-m{j} 🧵"}, user_id=users[index]["id"], expected=201) - with lock: - confirmed.append(message) - except Exception as exc: # noqa: BLE001 - reported below - with lock: - errors.append(f"client {index}: {exc}") - - started = time.monotonic() - threads = [threading.Thread(target=client, args=(i,)) for i in range(args.clients)] - for t in threads: - t.start() - for t in threads: - t.join() - elapsed = time.monotonic() - started - - history, pages, cursor = [], 0, None - while True: - query = f"?limit={args.page_limit}" + (f"&cursor={cursor}" if cursor else "") - page = call(base, "GET", f"/api/v1/chats/{chat['id']}/messages{query}", user_id=users[-1]["id"]) - pages += 1 - history.extend(page["items"]) - cursor = page["next_cursor"] - if cursor is None or len(history) > 10 * total: - break - - ids = [m["id"] for m in history] - keys = [(m["created_at"], uuid.UUID(m["id"]).int) for m in history] - per_client_ok = all( - [m["text"] for m in history if m["sender_id"] == u["id"]] == [f"c{i}-m{j} 🧵" for j in range(args.messages)] - for i, u in enumerate(users) - ) - checks = { - "all_writes_confirmed_201": len(confirmed) == total and not errors, - "history_count_equals_sent": len(history) == total, - "ids_unique": len(set(ids)) == len(ids), - "no_confirmed_message_lost": {m["id"] for m in confirmed} <= set(ids), - "strict_created_at_id_order": all(a < b for a, b in zip(keys, keys[1:])), - "per_client_order_preserved": per_client_ok, - } - summary = { - "clients": args.clients, - "messages_per_client": args.messages, - "sent": total, - "confirmed_201": len(confirmed), - "errors": errors[:5], - "history_items": len(history), - "unique_ids": len(set(ids)), - "pages_read": pages, - "page_limit": args.page_limit, - "write_phase_seconds": round(elapsed, 3), - "checks": checks, - "result": "PASS" if all(checks.values()) else "FAIL", - } - text = json.dumps(summary, ensure_ascii=False, indent=2) - print(text) - if args.output: - Path(args.output).parent.mkdir(parents=True, exist_ok=True) - Path(args.output).write_text(text + "\n", encoding="utf-8") - return 0 if summary["result"] == "PASS" else 1 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 0000000..d99077e --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,201 @@ +"""Contract behaviour over real HTTP (stand must be running). Lab 1 checks moved to Bearer + new lab 2 auth.""" +from __future__ import annotations + +import base64 +import uuid + +import pytest + +from conftest import PASSWORD, basic + +pytestmark = pytest.mark.stack + + +def key(item: dict) -> tuple[str, int]: + return item["created_at"], uuid.UUID(item["id"]).int + + +# ================================================================== registration & login +def test_register_never_returns_password_and_login_returns_bearer(api): + actor = api.register("alice", display_name="Алиса 👩‍💻") + assert set(actor["user"]) == {"id", "username", "display_name", "created_at"} + session = api.call("POST", "/api/v1/auth/sessions", 201, + headers={"Authorization": basic(actor["username"], PASSWORD)}, schema="Session").json() + assert session["token_type"] == "Bearer" and len(session["token"]) >= 43 + assert actor["username"] not in session["token"] and actor["id"] not in session["token"] + assert api.call("GET", "/api/v1/users/me", 200, token=session["token"], schema="User").json() == actor["user"] + + +def test_duplicate_username_is_409(api): + actor = api.register("dup") + body = {"username": actor["username"], "display_name": "Другой", "password": PASSWORD} + assert api.call("POST", "/api/v1/users", 409, json_body=body).json()["error"]["code"] == "conflict" + + +@pytest.mark.parametrize("password", ["short-11ch", " " * 12, " \t \n " * 3, "я" * 129]) +def test_bad_password_on_registration_is_400(api, password): + body = {"username": "pw_" + uuid.uuid4().hex[:10], "display_name": "x", "password": password} + api.call("POST", "/api/v1/users", 400, json_body=body) + + +@pytest.mark.parametrize("password", ["я" * 128, "with:colons:inside", "Twelve chars"]) +def test_edge_passwords_register_and_login(api, password): + actor = api.user("edge", password=password) + api.call("GET", "/api/v1/users/me", 200, token=actor["token"]) + + +def test_wrong_password_and_unknown_user_give_the_same_401(api): + actor = api.register("victim") + wrong = api.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": basic(actor["username"], "Wrong-password-1")}) + unknown = api.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": basic("nobody_" + uuid.uuid4().hex[:8], PASSWORD)}) + assert wrong.json()["error"]["message"] == unknown.json()["error"]["message"] + assert wrong.headers["www-authenticate"] == 'Basic realm="messenger", charset="UTF-8"' + + +@pytest.mark.parametrize("header", ["Basic é", "Basic !!!", "Basic " + base64.b64encode(b"no_colon").decode(), + "Basic " + base64.b64encode(b"\xff\xfe:x").decode(), "Bearer " + "a" * 43, ""]) +def test_malformed_basic_is_401_not_500(api, header): + # Raw UTF-8 bytes, as curl would send them (httpx refuses non-ASCII str headers). + api.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": header.encode("utf-8")} if header else {}) + + +def test_logout_revokes_only_this_token(api): + actor = api.user("logout") + second = api.login(actor) + api.call("DELETE", "/api/v1/auth/sessions/current", 204, token=actor["token"]) + api.call("GET", "/api/v1/users/me", 401, token=actor["token"]) + api.call("DELETE", "/api/v1/auth/sessions/current", 401, token=actor["token"]) + api.call("GET", "/api/v1/users/me", 200, token=second) # other session of the same user survives + + +def test_x_user_id_basic_and_garbage_do_not_authenticate(api): + actor = api.user("hdr") + for headers in ({"X-User-Id": actor["id"]}, + {"Authorization": basic(actor["username"], PASSWORD)}, + {"Authorization": "Bearer " + "A" * 43}, + {"Authorization": "Bearer"}): + api.call("GET", "/api/v1/users/me", 401, headers=headers) + me = api.call("GET", "/api/v1/users/me", 200, token=actor["token"], headers={"X-User-Id": str(uuid.uuid4())}) + assert me.json()["id"] == actor["id"] + + +# ================================================================== validation / errors (from lab 1) +@pytest.mark.parametrize("raw", [b"{", b"", b"[]", b"null", b'{"username": "abc_def", "display_name": NaN, "password": "Twelve-chars"}', + b'{"username": "abc_def", "display_name": "x", "password": "Twelve-chars"}{"a": 1}', + b'{"username": "abc_def", "display_name": "\\ud800", "password": "Twelve-chars"}']) +def test_malformed_json_is_400(api, raw): + api.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": "application/json"}) + + +@pytest.mark.parametrize("body", [ + {"username": "abc_def", "display_name": "x"}, # no password + {"username": "abc_def", "display_name": "x", "password": "Twelve-chars", "extra": 1}, + {"username": "Abc_def", "display_name": "x", "password": "Twelve-chars"}, + {"username": "abc_def", "display_name": " ", "password": "Twelve-chars"}, + {"username": "abc_def", "display_name": "x" * 101, "password": "Twelve-chars"}, + {"username": 5, "display_name": "x", "password": "Twelve-chars"}, +]) +def test_invalid_registration_fields_are_400(api, body): + api.call("POST", "/api/v1/users", 400, json_body=body) + + +def test_unicode_limits_are_code_points_and_nul_is_stored(api): + actor = api.user("uni", display_name="😀" * 100) + assert actor["user"]["display_name"] == "😀" * 100 + nul = api.user("nul", display_name="A\u0000B") + assert api.call("GET", "/api/v1/users/me", 200, token=nul["token"]).json()["display_name"] == "A\u0000B" + chat = api.chat(actor, title="T\u0000itle") + assert chat["title"] == "T\u0000itle" + assert api.send(actor, chat, "hello\u0000world")["text"] == "hello\u0000world" + assert api.send(actor, chat, "🙂" * 4000)["text"] == "🙂" * 4000 + api.call("POST", f"/api/v1/chats/{chat['id']}/messages", 400, token=actor["token"], json_body={"text": "я" * 4001}) + texts = [m["text"] for m in api.history(actor, chat, 100)[0]] + assert texts == ["hello\u0000world", "🙂" * 4000] + + +def test_oversized_body_and_wrong_content_type(api): + api.call("POST", "/api/v1/users", 413, json_body={"username": "big_body", "display_name": "x" * 70_000, "password": PASSWORD}) + api.call("POST", "/api/v1/users", 400, raw=b'{"username":"abc_def"}', headers={"Content-Type": "text/plain"}) + + +# ================================================================== chats & membership +def test_chat_validation(api): + alice, bob = api.user("alice"), api.user("bob") + for body in ({"title": "x", "member_ids": [alice["id"], bob["id"], bob["id"]]}, + {"title": "x", "member_ids": [alice["id"], str(uuid.uuid4())]}, + {"title": "x", "member_ids": [bob["id"]]}, + {"title": " ", "member_ids": [alice["id"]]}, + {"title": "x", "member_ids": [alice["id"], "bob"]}): + api.call("POST", "/api/v1/chats", 400, token=alice["token"], json_body=body) + + +def test_foreign_chat_is_indistinguishable_from_missing(api): + alice, bob, eve = api.user("alice"), api.user("bob"), api.user("eve") + chat = api.chat(alice, bob) + for chat_id in (chat["id"], str(uuid.uuid4())): + base = f"/api/v1/chats/{chat_id}" + errors = [api.call("GET", base, 404, token=eve["token"]).json(), + api.call("GET", base + "/messages", 404, token=eve["token"]).json(), + api.call("POST", base + "/messages", 404, token=eve["token"], json_body={"text": "чужое"}).json(), + api.call("GET", base + "/messages?cursor=garbage", 404, token=eve["token"]).json()] + assert len({e["error"]["message"] for e in errors}) == 1 + api.call("GET", "/api/v1/chats/not-a-uuid", 401) + api.call("GET", "/api/v1/chats/not-a-uuid", 400, token=alice["token"]) + api.call("GET", "/api/v1/chats/NOT-A-UUID/messages", 400, token=alice["token"]) + + +def test_chat_list_order_pagination_and_isolation(api): + alice, bob, eve = api.user("alice"), api.user("bob"), api.user("eve") + created = [api.chat(alice, bob, title=f"Чат {i}") for i in range(5)] + api.chat(eve, title="Чат Евы") + walked, cursor = [], None + while True: + query = "?limit=2" + (f"&cursor={cursor}" if cursor else "") + page = api.call("GET", "/api/v1/chats" + query, 200, token=bob["token"], schema="ChatPage").json() + walked += page["items"] + cursor = page["next_cursor"] + if not cursor: + break + assert walked == created + first = api.call("GET", "/api/v1/chats?limit=1", 200, token=alice["token"]).json()["next_cursor"] + api.call("GET", f"/api/v1/chats?limit=1&cursor={first}", 400, token=bob["token"]) # cursor of another user + api.call("GET", "/api/v1/chats?limit=0", 400, token=bob["token"]) + + +# ================================================================== messages & keyset pagination +def test_history_pages_without_loss_or_duplicates(api): + alice, bob = api.user("alice"), api.user("bob") + chat = api.chat(alice, bob) + sent = [api.send(alice if i % 2 else bob, chat, f"сообщение {i}") for i in range(25)] + items, pages = api.history(bob, chat, 7) + assert pages == 4 and items == sent and items == sorted(items, key=key) + assert sent[0]["sender_id"] == bob["id"] and sent[1]["sender_id"] == alice["id"] + + +@pytest.mark.parametrize("query", ["limit=0", "limit=101", "limit=abc", "limit=1&limit=2", "cursor=", "cursor=garbage", + "cursor=djF8bXx4fDF8Mg", "cursor=" + base64.urlsafe_b64encode(b"v1|m|x|99999999999999999999|y").decode()]) +def test_bad_limit_or_cursor_is_400(api, query): + alice = api.user("alice") + chat = api.chat(alice) + api.call("GET", f"/api/v1/chats/{chat['id']}/messages?{query}", 400, token=alice["token"]) + + +def test_cursor_bound_to_chat(api): + alice = api.user("alice") + a, b = api.chat(alice, title="A"), api.chat(alice, title="B") + for chat in (a, b): + api.send(alice, chat, "1") + api.send(alice, chat, "2") + cursor = api.call("GET", f"/api/v1/chats/{a['id']}/messages?limit=1", 200, token=alice["token"]).json()["next_cursor"] + api.call("GET", f"/api/v1/chats/{b['id']}/messages?cursor={cursor}", 400, token=alice["token"]) + empty = api.chat(alice, title="Empty") + api.call("GET", f"/api/v1/chats/{empty['id']}/messages?cursor=garbage", 400, token=alice["token"]) + + +def test_read_after_write(api): + alice, bob = api.user("alice"), api.user("bob") + chat = api.chat(alice, bob) + for i in range(5): + message = api.send(alice, chat, f"m{i}") + last = api.call("GET", f"/api/v1/chats/{chat['id']}/messages?limit=100", 200, token=bob["token"]).json()["items"][-1] + assert last == message diff --git a/student-tests/test_chats_and_messages.py b/student-tests/test_chats_and_messages.py deleted file mode 100644 index 83e36db..0000000 --- a/student-tests/test_chats_and_messages.py +++ /dev/null @@ -1,219 +0,0 @@ -"""Chats, membership (foreign chat -> 404), messages and keyset pagination.""" -from __future__ import annotations - -import uuid - -import pytest - - -def key(item: dict) -> tuple[str, int]: - # Fixed-width RFC 3339 strings compare like timestamps; ids compare as UUID integers. - return item["created_at"], uuid.UUID(item["id"]).int - - -# ---------------------------------------------------------------- chats -def test_create_chat_and_read_it(api): - alice, bob = api.user("alice"), api.user("bob") - chat = api.chat(alice, bob, title="Проект по ВНП 🚀") - assert chat["member_ids"] == [alice["id"], bob["id"]] - assert chat["title"] == "Проект по ВНП 🚀" - assert api.call("GET", f"/api/v1/chats/{chat['id']}", 200, user=bob, schema="Chat").json() == chat - - -@pytest.mark.parametrize("case", ["duplicate", "unknown", "without_caller", "empty", "too_many", "not_uuid", "blank_title", "long_title", "extra_field", "ids_not_list"]) -def test_invalid_chat_is_400(api, case): - alice, bob = api.user("alice"), api.user("bob") - body = {"title": "Чат", "member_ids": [alice["id"], bob["id"]]} - if case == "duplicate": - body["member_ids"] = [alice["id"], bob["id"], bob["id"].upper()] - elif case == "unknown": - body["member_ids"] = [alice["id"], str(uuid.uuid4())] - elif case == "without_caller": - body["member_ids"] = [bob["id"]] - elif case == "empty": - body["member_ids"] = [] - elif case == "too_many": - body["member_ids"] = [alice["id"]] + [str(uuid.uuid4()) for _ in range(100)] - elif case == "not_uuid": - body["member_ids"] = [alice["id"], "bob"] - elif case == "blank_title": - body["title"] = " " - elif case == "long_title": - body["title"] = "т" * 101 - elif case == "extra_field": - body["owner_id"] = alice["id"] - elif case == "ids_not_list": - body["member_ids"] = alice["id"] - api.call("POST", "/api/v1/chats", 400, user=alice, json_body=body) - - -def test_chat_with_only_the_caller_is_allowed(api): - alice = api.user("alice") - assert api.chat(alice)["member_ids"] == [alice["id"]] - - -def test_chat_list_contains_only_own_chats_in_order(api): - alice, bob, eve = api.user("alice"), api.user("bob"), api.user("eve") - created = [api.chat(alice, bob, title=f"Чат {i}") for i in range(5)] - api.chat(eve, title="Чат Евы") - listed = api.call("GET", "/api/v1/chats?limit=100", 200, user=bob, schema="ChatPage").json() - assert listed == {"items": created, "next_cursor": None} - assert api.call("GET", "/api/v1/chats", 200, user=eve).json()["items"][0]["title"] == "Чат Евы" - assert len(api.call("GET", "/api/v1/chats", 200, user=eve).json()["items"]) == 1 - - -def test_chat_list_pagination(api): - alice = api.user("alice") - created = [api.chat(alice, title=f"Чат {i}") for i in range(5)] - first = api.call("GET", "/api/v1/chats?limit=2", 200, user=alice).json() - second = api.call("GET", f"/api/v1/chats?limit=2&cursor={first['next_cursor']}", 200, user=alice).json() - third = api.call("GET", f"/api/v1/chats?limit=2&cursor={second['next_cursor']}", 200, user=alice).json() - assert first["items"] + second["items"] + third["items"] == created - assert third["next_cursor"] is None - - -def test_chat_cursor_of_another_user_is_400(api): - alice, bob = api.user("alice"), api.user("bob") - for i in range(3): - api.chat(alice, bob, title=f"Чат {i}") - cursor = api.call("GET", "/api/v1/chats?limit=1", 200, user=alice).json()["next_cursor"] - api.call("GET", f"/api/v1/chats?limit=1&cursor={cursor}", 400, user=bob) - - -# ------------------------------------------------------ foreign chat -> 404 -def test_foreign_chat_is_indistinguishable_from_missing_chat(api): - alice, bob, eve = api.user("alice"), api.user("bob"), api.user("eve") - chat = api.chat(alice, bob) - api.send(alice, chat, "секрет") - missing = str(uuid.uuid4()) - for chat_id in (chat["id"], missing): - base = f"/api/v1/chats/{chat_id}" - errors = [ - api.call("GET", base, 404, user=eve).json(), - api.call("GET", base + "/messages", 404, user=eve).json(), - api.call("POST", base + "/messages", 404, user=eve, json_body={"text": "чужое"}).json(), - # Even an invalid body or cursor does not reveal that the chat exists. - api.call("POST", base + "/messages", 404, user=eve, json_body={"text": ""}).json(), - api.call("GET", base + "/messages?cursor=garbage", 404, user=eve).json(), - ] - assert all(e["error"]["code"] == "not_found" for e in errors) - assert all(e["error"]["message"] == errors[0]["error"]["message"] for e in errors) - - -def test_invalid_chat_id_is_400_after_authentication(api): - alice = api.user("alice") - api.call("GET", "/api/v1/chats/not-a-uuid", 401) - api.call("GET", "/api/v1/chats/not-a-uuid", 400, user=alice) - api.call("GET", "/api/v1/chats/not-a-uuid/messages", 400, user=alice) - - -# -------------------------------------------------------------- messages -def test_message_sender_is_derived_from_identity(api): - alice, bob = api.user("alice"), api.user("bob") - chat = api.chat(alice, bob) - first = api.send(alice, chat, "Привет, Боб! 👋") - second = api.send(bob, chat, "Привет, Алиса") - assert (first["sender_id"], second["sender_id"]) == (alice["id"], bob["id"]) - assert first["chat_id"] == second["chat_id"] == chat["id"] - assert first["text"] == "Привет, Боб! 👋" - assert key(first) < key(second) - - -@pytest.mark.parametrize("body", [ - {"text": ""}, - {"text": " \n\t "}, - {"text": "я" * 4001}, - {"text": None}, - {"text": 42}, - {}, - {"text": "ok", "sender_id": str(uuid.uuid4())}, # sender cannot be chosen by the client - {"text": "ok", "attachment_ids": []}, # attachments appear only in lab4 -]) -def test_invalid_message_is_400(api, body): - alice = api.user("alice") - chat = api.chat(alice) - error = api.call("POST", f"/api/v1/chats/{chat['id']}/messages", 400, user=alice, json_body=body).json() - assert error["error"]["code"] == "invalid_request" - assert api.call("GET", f"/api/v1/chats/{chat['id']}/messages", 200, user=alice).json()["items"] == [] - - -def test_message_limit_is_4000_code_points(api): - alice = api.user("alice") - chat = api.chat(alice) - text = "🙂" * 4000 # 4000 code points, 16000 UTF-8 bytes - assert api.send(alice, chat, text)["text"] == text - assert api.send(alice, chat, " отступы и\nпереносы сохраняются ")["text"] == " отступы и\nпереносы сохраняются " - - -# ------------------------------------------------------------ pagination -def test_history_walks_several_pages_without_loss_or_duplicates(api): - alice, bob = api.user("alice"), api.user("bob") - chat = api.chat(alice, bob) - sent = [api.send(alice if i % 2 else bob, chat, f"сообщение {i}") for i in range(25)] - items, pages = api.history(bob, chat, limit=7) - assert pages == 4 # 7 + 7 + 7 + 4 - assert items == sent # same order, nothing lost or repeated - assert items == sorted(items, key=key) - assert len({m["id"] for m in items}) == 25 - - -def test_limit_may_change_between_pages_and_default_is_50(api): - alice = api.user("alice") - chat = api.chat(alice) - sent = [api.send(alice, chat, f"m{i}") for i in range(60)] - base = f"/api/v1/chats/{chat['id']}/messages" - first = api.call("GET", base, 200, user=alice).json() - assert len(first["items"]) == 50 and first["next_cursor"] - rest = api.call("GET", f"{base}?limit=100&cursor={first['next_cursor']}", 200, user=alice).json() - assert first["items"] + rest["items"] == sent and rest["next_cursor"] is None - - -def test_exact_page_boundary_has_null_cursor(api): - alice = api.user("alice") - chat = api.chat(alice) - for i in range(3): - api.send(alice, chat, f"m{i}") - page = api.call("GET", f"/api/v1/chats/{chat['id']}/messages?limit=3", 200, user=alice).json() - assert len(page["items"]) == 3 and page["next_cursor"] is None - - -def test_new_message_after_last_page_is_reachable_from_cursor(api): - alice = api.user("alice") - chat = api.chat(alice) - api.send(alice, chat, "m0") - api.send(alice, chat, "m1") - base = f"/api/v1/chats/{chat['id']}/messages" - page = api.call("GET", f"{base}?limit=1", 200, user=alice).json() - newer = api.send(alice, chat, "m2") - tail = api.call("GET", f"{base}?limit=10&cursor={page['next_cursor']}", 200, user=alice).json() - assert [m["text"] for m in tail["items"]] == ["m1", "m2"] and tail["items"][-1] == newer - - -@pytest.mark.parametrize("query", ["limit=0", "limit=101", "limit=-1", "limit=abc", "limit=1.5", "limit=", "limit=%D9%A3", - "limit=1&limit=2", "cursor=", "cursor=garbage", "cursor=%%%", "cursor=djF8bXx4fDF8Mg"]) -def test_invalid_limit_or_cursor_is_400(api, query): - alice = api.user("alice") - chat = api.chat(alice) - api.send(alice, chat, "m") - api.call("GET", f"/api/v1/chats/{chat['id']}/messages?{query}", 400, user=alice) - - -def test_cursor_from_another_chat_is_400(api): - alice = api.user("alice") - chat_a, chat_b = api.chat(alice, title="A"), api.chat(alice, title="B") - for chat in (chat_a, chat_b): - api.send(alice, chat, "1") - api.send(alice, chat, "2") - cursor = api.call("GET", f"/api/v1/chats/{chat_a['id']}/messages?limit=1", 200, user=alice).json()["next_cursor"] - api.call("GET", f"/api/v1/chats/{chat_b['id']}/messages?cursor={cursor}", 400, user=alice) - # A cursor of the chat list is not a message cursor either. - api.chat(alice, title="C") - chats_cursor = api.call("GET", "/api/v1/chats?limit=1", 200, user=alice).json()["next_cursor"] - api.call("GET", f"/api/v1/chats/{chat_a['id']}/messages?cursor={chats_cursor}", 400, user=alice) - - -def test_invalid_cursor_is_400_even_for_empty_history(api): - alice = api.user("alice") - chat = api.chat(alice) # no messages at all - api.call("GET", f"/api/v1/chats/{chat['id']}/messages?cursor=garbage", 400, user=alice) - api.call("GET", "/api/v1/chats?cursor=garbage", 400, user=api.user("bob")) # user without chats diff --git a/student-tests/test_db.py b/student-tests/test_db.py new file mode 100644 index 0000000..fd065e1 --- /dev/null +++ b/student-tests/test_db.py @@ -0,0 +1,114 @@ +"""Database-level guarantees, checked over HTTP and verified directly in PostgreSQL / Valkey.""" +from __future__ import annotations + +import threading +import time +import uuid + +import pytest + +from conftest import PASSWORD, Api, compose, psql + +pytestmark = pytest.mark.stack + + +def run_concurrently(workers: int, target) -> list: + barrier = threading.Barrier(workers) + results: list = [None] * workers + + def body(index: int) -> None: + local = Api() # one HTTP client (connection) per worker + barrier.wait() # all workers fire at the same moment + try: + results[index] = target(local, index) + except BaseException as exc: # noqa: BLE001 - reported by the test + results[index] = exc + + threads = [threading.Thread(target=body, args=(i,)) for i in range(workers)] + for t in threads: + t.start() + for t in threads: + t.join() + return results + + +def test_registration_race_exactly_one_201_and_seven_409(): + username = "race_" + uuid.uuid4().hex[:10] + + def register(client: Api, _index: int) -> int: + body = {"username": username, "display_name": "Гонка", "password": PASSWORD} + response = client.client.post("/api/v1/users", json=body) + return response.status_code + + statuses = run_concurrently(8, register) + # Real HTTP codes are compared: a timeout, 500 or exception fails the test instead of counting as 409. + assert sorted(statuses, key=str) == [201] + [409] * 7, statuses + assert psql(f"SELECT count(*) FROM users WHERE username = '{username}'") == "1" + + +def _ins_counter() -> int: + return int(psql("SELECT n_tup_ins FROM pg_stat_user_tables WHERE relname = 'chats'")) + + +def test_create_chat_rolls_back_after_the_chat_row_was_inserted(api): + """The chat INSERT runs first; the unknown member then breaks the membership INSERT (FK). + + Proof that the first INSERT really happened and was rolled back: PostgreSQL counts + attempted inserts in pg_stat_user_tables.n_tup_ins even for aborted transactions, + while the table contains no such chat and no new membership rows. + """ + alice = api.user("rollback") + title = "rollback-" + uuid.uuid4().hex + members_before = int(psql("SELECT count(*) FROM chat_members")) + attempts_before = _ins_counter() + error = api.call("POST", "/api/v1/chats", 400, token=alice["token"], + json_body={"title": title, "member_ids": [alice["id"], str(uuid.uuid4())]}).json() + assert "несуществующих" in error["error"]["message"] + deadline = time.monotonic() + 20 # statistics are flushed by the API backend asynchronously + while _ins_counter() == attempts_before and time.monotonic() < deadline: + time.sleep(0.5) + assert _ins_counter() > attempts_before, "the chat INSERT was never attempted" + assert psql(f"SELECT count(*) FROM chats WHERE title = convert_to('{title}', 'UTF8')") == "0" + assert int(psql("SELECT count(*) FROM chat_members")) == members_before + + +def test_migrations_rerun_is_a_noop(): + users_before = psql("SELECT count(*) FROM users") + result = compose("run", "--rm", "--no-deps", "migrate") + assert "0001_init: already applied, skipped" in result.stdout + assert psql("SELECT count(*) FROM schema_migrations") == "1" + assert psql("SELECT count(*) FROM users") == users_before + + +def test_100_messages_from_10_clients_keep_order_and_cursor_walk(api): + clients = [api.user(f"w{i}") for i in range(10)] + chat = api.chat(clients[0], *clients[1:]) + + def writer(client: Api, index: int) -> list[dict]: + return [client.send(clients[index], chat, f"c{index}-m{j}") for j in range(10)] + + results = run_concurrently(10, writer) + assert all(isinstance(r, list) for r in results), results + confirmed = [m for batch in results for m in batch] + history, pages = api.history(clients[5], chat, 7) + ids = [m["id"] for m in history] + assert len(confirmed) == 100 and len(history) == 100 and len(set(ids)) == 100 and pages == 15 + assert set(ids) == {m["id"] for m in confirmed} + keys = [(m["created_at"], uuid.UUID(m["id"]).int) for m in history] + assert all(a < b for a, b in zip(keys, keys[1:])), "history is not strictly ordered" + stamps = [m["created_at"] for m in history] + assert len(set(stamps)) == 100, "per-chat clock must give unique timestamps" + for i, client in enumerate(clients): + own = [m["text"] for m in history if m["sender_id"] == client["id"]] + assert own == [f"c{i}-m{j}" for j in range(10)] + + +def test_secrets_are_not_stored_in_plain_text(api): + actor = api.user("store") + stored = psql(f"SELECT password_hash FROM users WHERE id = '{actor['id']}'") + assert stored.startswith("$argon2id$v=19$m=19456,t=2,p=1$") and PASSWORD not in stored + token_hex = actor["token"].encode().hex() + keys = compose("exec", "-T", "valkey", "sh", "-c", + 'REDISCLI_AUTH="$(cat /run/secrets/valkey_password)" valkey-cli --scan --pattern "session:*"').stdout.split() + assert keys and all(len(k) == len("session:") + 64 for k in keys) + assert not any(actor["token"] in k or token_hex in k for k in keys) diff --git a/student-tests/test_explain.py b/student-tests/test_explain.py new file mode 100644 index 0000000..a2503a5 --- /dev/null +++ b/student-tests/test_explain.py @@ -0,0 +1,35 @@ +"""The history index really bounds the work of a late page (>= 10 000 messages, generic plan).""" +from __future__ import annotations + +import re +import sys + +import pytest + +from conftest import ROOT + +pytestmark = pytest.mark.stack + +sys.path.insert(0, str(ROOT / "scripts")) +import explain_history # noqa: E402 + + +def section(text: str, number: int) -> str: + parts = re.split(r"^=== ", text, flags=re.M) + return next(p for p in parts if p.startswith(f"{number}.")) + + +def buffers(plan: str) -> int: + return int(re.search(r"Buffers: shared hit=(\d+)", plan).group(1)) + + +def test_late_page_uses_index_range_not_filter(): + text = explain_history.run() + assert "20000" in text # the target chat holds 20 000 messages + app_late = section(text, 2) + assert "Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))" in app_late + assert "Rows Removed by Filter" not in app_late and "Sort" not in app_late + rejected = section(text, 3) + assert "Rows Removed by Filter: 19950" in rejected + # The app query touches a handful of pages; the rejected variants touch the whole prefix. + assert buffers(app_late) <= 10 < buffers(rejected) and buffers(app_late) <= 10 < buffers(section(text, 4)) diff --git a/student-tests/test_failures.py b/student-tests/test_failures.py new file mode 100644 index 0000000..0dce65a --- /dev/null +++ b/student-tests/test_failures.py @@ -0,0 +1,99 @@ +"""Dependency failures with a VALID session: bounded 503, never anonymous access, recovery afterwards. + +Every test stops/pauses a real container and restores it in `finally` (service_down). +Timing bounds: DB_TIMEOUT_SECONDS=3 (client deadline), CACHE_TIMEOUT_SECONDS=0.5. +""" +from __future__ import annotations + +import subprocess +import time + +import pytest + +from conftest import ROOT, restart_services, service_down + +pytestmark = pytest.mark.stack + +DB_BOUND = 3.6 # DB_TIMEOUT_SECONDS + network/HTTP overhead +CACHE_BOUND = 1.6 # 2 x CACHE_TIMEOUT_SECONDS + overhead + + +def timed(api, *args, **kwargs): + started = time.monotonic() + response = api.call(*args, **kwargs) + return response, time.monotonic() - started + + +@pytest.fixture +def prepared(api): + alice, bob = api.user("alice"), api.user("bob") + chat = api.chat(alice, bob) + message = api.send(alice, chat, "до отказа") + return alice, bob, chat, message + + +def check_intact(api, alice, bob, chat, message): + """After recovery the SAME tokens work and confirmed data is unchanged.""" + assert api.call("GET", "/api/v1/users/me", 200, token=alice["token"]).json() == alice["user"] + items = api.call("GET", f"/api/v1/chats/{chat['id']}/messages", 200, token=bob["token"]).json()["items"] + assert items == [message] + + +@pytest.mark.parametrize("how", ["stop", "pause"]) +def test_valkey_failure(api, prepared, how): + alice, bob, chat, message = prepared + with service_down("valkey", how): + response, took = timed(api, "GET", "/api/v1/users/me", 503, token=alice["token"]) + assert response.json()["error"]["code"] == "unavailable" and took < CACHE_BOUND, took + api.call("GET", f"/api/v1/chats/{chat['id']}/messages", 503, token=bob["token"]) + api.call("GET", "/api/v1/users/me", 401) # anonymous is still rejected + api.call("GET", "/health/ready", 503) + api.call("GET", "/health/live", 200) + check_intact(api, alice, bob, chat, message) + + +@pytest.mark.parametrize("how", ["stop", "pause"]) +def test_postgres_failure(api, prepared, how): + alice, bob, chat, message = prepared + with service_down("postgres", how): + for path in ("/api/v1/users/me", f"/api/v1/chats/{chat['id']}/messages", "/api/v1/chats"): + response, took = timed(api, "GET", path, 503, token=alice["token"]) + assert response.json()["error"]["code"] == "unavailable", path + assert took < DB_BOUND, (path, took) # bounded even when PostgreSQL is frozen (pause) + api.call("GET", "/api/v1/users/me", 401) # anonymous is still rejected + api.call("GET", "/health/ready", 503) + api.call("GET", "/health/live", 200) + check_intact(api, alice, bob, chat, message) + + +def test_locked_table_gives_bounded_503(api, prepared): + """A statement stuck behind a lock is stopped by lock_timeout, not after minutes.""" + alice, bob, chat, message = prepared + locker = subprocess.Popen( + ["docker", "compose", "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c", + "BEGIN; LOCK TABLE messages IN ACCESS EXCLUSIVE MODE; SELECT pg_sleep(6); COMMIT;"], + cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + try: + time.sleep(1.5) # let the lock be taken + response, took = timed(api, "GET", f"/api/v1/chats/{chat['id']}/messages", 503, token=bob["token"]) + assert response.json()["error"]["code"] == "unavailable" and took < DB_BOUND, took + finally: + locker.wait(timeout=30) + check_intact(api, alice, bob, chat, message) + + +def test_logout_during_valkey_outage_is_never_lost(api, prepared): + """Valkey is the single source of truth for sessions, so a logout cannot be 'forgotten': + during the outage logout answers 503 (nothing revoked, the client knows it failed); + after recovery logout works, and the revocation survives a Valkey and API restart (AOF).""" + alice, bob, chat, message = prepared + with service_down("valkey", "stop"): + api.call("DELETE", "/api/v1/auth/sessions/current", 503, token=alice["token"]) + api.call("GET", "/api/v1/users/me", 200, token=alice["token"]) # logout did not happen + api.call("DELETE", "/api/v1/auth/sessions/current", 204, token=alice["token"]) + api.call("GET", "/api/v1/users/me", 401, token=alice["token"]) + restart_services("valkey", "api") + for _ in range(10): # repeated checks: the revoked token must never come back + api.call("GET", "/api/v1/users/me", 401, token=alice["token"]) + api.call("POST", f"/api/v1/chats/{chat['id']}/messages", 401, token=alice["token"], json_body={"text": "x"}) + api.call("GET", "/api/v1/users/me", 200, token=bob["token"]) # other sessions survived the restart diff --git a/student-tests/test_sessions.py b/student-tests/test_sessions.py new file mode 100644 index 0000000..245db76 --- /dev/null +++ b/student-tests/test_sessions.py @@ -0,0 +1,64 @@ +"""Session lifetime: expiry with a short SESSION_TTL_SECONDS, no extension by restarts, +and the alternative SESSION_STORE=postgres. The api container is recreated with the +override and ALWAYS recreated back with the normal configuration (api_with_env).""" +from __future__ import annotations + +import datetime as dt +import time + +import pytest + +from conftest import api_env, api_with_env, restart_services, service_down + +pytestmark = pytest.mark.stack + + +def seconds_left(expires_at: str) -> float: + moment = dt.datetime.fromisoformat(expires_at.replace("Z", "+00:00")) + return (moment - dt.datetime.now(dt.timezone.utc)).total_seconds() + + +def login_session(api, actor) -> dict: + from conftest import basic + return api.call("POST", "/api/v1/auth/sessions", 201, + headers={"Authorization": basic(actor["username"], actor["password"])}, schema="Session").json() + + +@pytest.mark.parametrize("store", ["valkey", "postgres"]) +def test_short_ttl_expires(api, store): + with api_with_env(SESSION_TTL_SECONDS="2", SESSION_STORE=store): + assert api_env("SESSION_TTL_SECONDS") == "2" # the contract variable reaches the container + actor = api.register("ttl") + session = login_session(api, actor) + assert 1.0 < seconds_left(session["expires_at"]) <= 2.1 + api.call("GET", "/api/v1/users/me", 200, token=session["token"]) + time.sleep(max(0.0, seconds_left(session["expires_at"])) + 0.3) + api.call("GET", "/api/v1/users/me", 401, token=session["token"]) + api.call("DELETE", "/api/v1/auth/sessions/current", 401, token=session["token"]) + + +def test_restart_does_not_extend_ttl(api): + with api_with_env(SESSION_TTL_SECONDS="12"): + actor = api.register("ttlrestart") + session = login_session(api, actor) + restart_services("valkey", "api") + assert seconds_left(session["expires_at"]) > 0, "restart took longer than the TTL; increase it" + api.call("GET", "/api/v1/users/me", 200, token=session["token"]) + time.sleep(max(0.0, seconds_left(session["expires_at"])) + 0.3) + api.call("GET", "/api/v1/users/me", 401, token=session["token"]) + + +def test_postgres_session_store(api): + """SESSION_STORE=postgres: sessions survive an API restart, logout revokes, and a DB outage + is 503 for a valid token (not 401 and not anonymous access).""" + with api_with_env(SESSION_STORE="postgres"): + assert api_env("SESSION_STORE") == "postgres" + actor = api.user("pgstore") + restart_services("api") + api.call("GET", "/api/v1/users/me", 200, token=actor["token"]) + with service_down("postgres", "stop"): + assert api.call("GET", "/api/v1/users/me", 503, token=actor["token"]).json()["error"]["code"] == "unavailable" + api.call("GET", "/api/v1/users/me", 401) + api.call("GET", "/api/v1/users/me", 200, token=actor["token"]) + api.call("DELETE", "/api/v1/auth/sessions/current", 204, token=actor["token"]) + api.call("GET", "/api/v1/users/me", 401, token=actor["token"]) diff --git a/student-tests/test_store_concurrency.py b/student-tests/test_store_concurrency.py deleted file mode 100644 index 5b80795..0000000 --- a/student-tests/test_store_concurrency.py +++ /dev/null @@ -1,131 +0,0 @@ -"""Race protection of the in-memory store with real OS threads. - -The HTTP server itself runs on one event loop; these tests hit the Store from -10 threads at once to prove the lock and the "timestamp under the lock" rule: -no lost/duplicated messages AND the history order equals (created_at, id). -""" -from __future__ import annotations - -import sys -import threading - -import pytest - -from messenger.errors import Conflict -from messenger.store import Store - -CLIENTS = 10 -PER_CLIENT = 10 - - -@pytest.fixture(autouse=True) -def frequent_thread_switches(): - """Force the interpreter to switch threads every 0.1 ms instead of every 5 ms. - - Without this the GIL rarely interrupts a short critical section and a race - such as "timestamp taken before the lock" would - pass unnoticed. With it the broken variant fails reliably. - """ - previous = sys.getswitchinterval() - sys.setswitchinterval(1e-4) - yield - sys.setswitchinterval(previous) - - -def run_concurrently(workers: int, target) -> list: - barrier = threading.Barrier(workers) - results: list = [None] * workers - errors: list[BaseException] = [] - - def body(index: int) -> None: - barrier.wait() # release all threads at the same moment - try: - results[index] = target(index) - except BaseException as exc: # noqa: BLE001 - collected and re-raised in the test - errors.append(exc) - - threads = [threading.Thread(target=body, args=(i,)) for i in range(workers)] - for t in threads: - t.start() - for t in threads: - t.join() - if errors: - raise errors[0] - return results - - -def walk(store: Store, user_id: str, chat_id: str, limit: int, max_items: int = 1000) -> list: - """Cursor traversal like a client does it. If the stored order were broken, - a cursor could point back to an already returned page and the walk would - never end, so the number of returned items is bounded.""" - items, after = [], None - while True: - page, has_more = store.list_messages(user_id, chat_id, after, limit) - items.extend(page) - assert len(items) <= max_items, "cursor traversal repeats messages (order is broken)" - if not has_more: - return items - after = page[-1].key - - -@pytest.mark.parametrize("round_", range(20)) -def test_100_messages_from_10_clients_are_unique_and_ordered(round_): - store = Store() - users = [store.create_user(f"client_{i}", f"Клиент {i}") for i in range(CLIENTS)] - chat = store.create_chat(users[0].id, "Нагрузка", [u.id for u in users]) - - def client(index: int): - return [store.add_message(users[index].id, chat.id, f"c{index}-m{j}") for j in range(PER_CLIENT)] - - confirmed = [m for batch in run_concurrently(CLIENTS, client) for m in batch] - history = walk(store, users[3].id, chat.id, limit=7) # multi-page cursor traversal - - # Compact messages on purpose: pytest's diff of 100 dataclasses is slow and unreadable. - assert len(confirmed) == 100, f"confirmed {len(confirmed)} writes" - ids = [m.id for m in history] - assert len(ids) == 100, f"history has {len(ids)} messages" - assert set(ids) == {m.id for m in confirmed}, "a confirmed message is missing" - assert len(set(ids)) == 100, "duplicated message in history" - keys = [m.key for m in history] - inversions = sum(1 for a, b in zip(keys, keys[1:]) if not a < b) - assert inversions == 0, f"{inversions} neighbours violate strict (created_at, id) order" - for i in range(CLIENTS): - own = [m.text for m in history if m.sender_id == users[i].id] - assert own == [f"c{i}-m{j}" for j in range(PER_CLIENT)], f"client {i} order changed" - - -def test_timestamps_increase_even_if_wall_clock_goes_back(): - ticks = iter([5_000_000_000, 4_000_000_000, 4_000_000_000, 1_000_000_000] + [0] * 10) - store = Store(clock_ns=lambda: next(ticks)) - user = store.create_user("clock_user", "Часы") - chat = store.create_chat(user.id, "Часы", [user.id]) - messages = [store.add_message(user.id, chat.id, str(i)) for i in range(5)] - stamps = [user.ts, chat.ts, *(m.ts for m in messages)] - assert stamps == sorted(stamps) and len(set(stamps)) == len(stamps) - assert [m.created_at for m in messages] == sorted(m.created_at for m in messages) - - -def test_concurrent_registration_of_same_username_creates_one_user(): - store = Store() - - def register(_index: int): - try: - return store.create_user("same_name", "Гонка") - except Conflict: - return None - - created = [u for u in run_concurrently(20, register) if u is not None] - assert len(created) == 1 - assert store.counts()["users"] == 1 - - -def test_concurrent_chat_creation_keeps_indexes_consistent(): - store = Store() - users = [store.create_user(f"member_{i}", "Участник") for i in range(CLIENTS)] - ids = [u.id for u in users] - chats = run_concurrently(CLIENTS, lambda i: store.create_chat(ids[i], f"Чат {i}", ids)) - for user in users: - listed, has_more = store.list_chats(user.id, None, 100) - assert not has_more - assert {c.id for c in listed} == {c.id for c in chats} - assert [c.key for c in listed] == sorted(c.key for c in listed) diff --git a/student-tests/test_users_and_errors.py b/student-tests/test_users_and_errors.py deleted file mode 100644 index b1550ba..0000000 --- a/student-tests/test_users_and_errors.py +++ /dev/null @@ -1,139 +0,0 @@ -"""Registration, identification (X-User-Id), strict JSON validation and the error format.""" -from __future__ import annotations - -import datetime as dt -import uuid - -import pytest - - -def test_health_endpoints(api): - assert api.call("GET", "/health/live", 200, schema="Health").json() == {"status": "ok"} - assert api.call("GET", "/health/ready", 200, schema="Health").json() == {"status": "ok"} - - -def test_readiness_is_503_while_draining(api, app): - app.state.draining = True - body = api.call("GET", "/health/ready", 503).json() - assert body["error"]["code"] == "unavailable" - assert api.call("GET", "/health/live", 200).json() == {"status": "ok"} - - -def test_register_returns_user_with_server_fields_and_unicode(api): - user = api.user("alice", display_name="Алиса 👩‍💻 Ωμέγα") - assert user["display_name"] == "Алиса 👩‍💻 Ωμέγα" - assert uuid.UUID(user["id"]).version == 4 - assert user["created_at"].endswith("Z") - dt.datetime.fromisoformat(user["created_at"].replace("Z", "+00:00")) - assert set(user) == {"id", "username", "display_name", "created_at"} - - -def test_duplicate_username_is_409(api): - body = {"username": "dup_" + uuid.uuid4().hex[:8], "display_name": "Первый"} - api.call("POST", "/api/v1/users", 201, json_body=body) - error = api.call("POST", "/api/v1/users", 409, json_body={**body, "display_name": "Второй"}).json() - assert error["error"]["code"] == "conflict" - - -def test_display_name_limit_is_in_code_points(api): - api.call("POST", "/api/v1/users", 201, json_body={"username": "emoji_ok", "display_name": "😀" * 100}) - api.call("POST", "/api/v1/users", 400, json_body={"username": "emoji_bad", "display_name": "😀" * 101}) - - -@pytest.mark.parametrize("raw", [ - b"{", # malformed JSON - b"", # empty body - b"[]", # not an object - b'"alice"', - b"null", - b'{"username": "abc_def", "display_name": NaN}', # NaN is not JSON - b'{"username": "abc_def", "display_name": "\xff\xfe"}', # invalid UTF-8 - b'{"username": "abc_def", "display_name": "\\ud800"}', # lone surrogate - b'{"username": "abc_def", "display_name": "x"}{"a": 1}', # two objects glued together -]) -def test_malformed_json_is_400(api, raw): - error = api.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": "application/json"}).json() - assert error["error"]["code"] == "invalid_request" - - -@pytest.mark.parametrize("body", [ - {"username": "abc_def"}, # missing field - {"username": "abc_def", "display_name": "x", "extra": 1}, # unknown field - {"username": "abc_def", "display_name": "x", "password": "p"}, # lab2 field is not accepted yet - {"username": 123, "display_name": "x"}, # wrong type - {"username": "abc_def", "display_name": None}, - {"username": "abc_def", "display_name": ["x"]}, - {"username": "abc_def", "display_name": ""}, - {"username": "abc_def", "display_name": " \t\n "}, # only whitespace - {"username": "ab", "display_name": "x"}, # too short - {"username": "a" * 33, "display_name": "x"}, # too long - {"username": "Alice", "display_name": "x"}, # upper case - {"username": "алиса", "display_name": "x"}, # non-ASCII - {"username": "abc-def", "display_name": "x"}, - {"username": "abc_def\n", "display_name": "x"}, # `$` trap in regex -]) -def test_invalid_user_fields_are_400(api, body): - api.call("POST", "/api/v1/users", 400, json_body=body) - - -@pytest.mark.parametrize("content_type", ["text/plain", "application/x-www-form-urlencoded", "application/json; charset=latin-1"]) -def test_wrong_content_type_is_400(api, content_type): - raw = b'{"username": "abc_def", "display_name": "x"}' - api.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": content_type}) - - -def test_json_with_utf8_charset_is_accepted(api): - raw = '{"username": "charset_ok", "display_name": "Юникод"}'.encode() - api.call("POST", "/api/v1/users", 201, raw=raw, headers={"Content-Type": "application/json; charset=UTF-8"}) - - -def test_oversized_body_is_413(api): - body = {"username": "big_body", "display_name": "x" * 70_000} - assert api.call("POST", "/api/v1/users", 413, json_body=body).json()["error"]["code"] == "payload_too_large" - - -@pytest.mark.parametrize("header", [None, "", "not-a-uuid", str(uuid.uuid4()), "11111111-1111-4111-8111-11111111111"]) -def test_missing_or_unknown_identity_is_401(api, header): - headers = {} if header is None else {"X-User-Id": header} - for method, path in [("GET", "/api/v1/users/me"), ("GET", "/api/v1/chats"), ("GET", f"/api/v1/chats/{uuid.uuid4()}")]: - error = api.call(method, path, 401, headers=headers).json() - assert error["error"]["code"] == "unauthorized" - - -def test_me_returns_selected_user(api): - alice, bob = api.user("alice"), api.user("bob") - assert api.call("GET", "/api/v1/users/me", 200, user=alice, schema="User").json() == alice - assert api.call("GET", "/api/v1/users/me", 200, user=bob).json() == bob - upper = api.call("GET", "/api/v1/users/me", 200, headers={"X-User-Id": alice["id"].upper()}).json() - assert upper == alice - - -def test_unknown_route_and_method_use_error_format(api): - assert api.call("GET", "/api/v1/nope", 404).json()["error"]["code"] == "not_found" - api.call("DELETE", "/api/v1/users", 405) - - -def test_request_id_is_unique_and_propagated(api): - first = api.call("GET", "/health/live", 200).headers["x-request-id"] - second = api.call("GET", "/health/live", 200).headers["x-request-id"] - assert first != second - assert api.call("GET", "/health/live", 200, headers={"X-Request-Id": "trace-42"}).headers["x-request-id"] == "trace-42" - - -def test_unexpected_exception_is_500_without_details(api, app, monkeypatch): - def boom(*_args, **_kwargs): - raise RuntimeError("secret internal detail") - - monkeypatch.setattr(app.state.store, "get_user", boom) - response = api.call("GET", "/api/v1/users/me", 500, headers={"X-User-Id": str(uuid.uuid4())}) - assert response.json()["error"]["code"] == "internal_error" - assert "secret" not in response.text - - -def test_swagger_ui_and_contract_are_served(api): - page = api.call("GET", "/docs", 200) - assert "swagger-ui" in page.text - spec = api.call("GET", "/openapi.json", 200).json() - assert spec["info"]["title"].startswith("MTUSI Messenger") - assert spec["servers"] == [{"url": "/", "description": "этот сервер"}] - assert "/api/v1/chats/{chat_id}/messages" in spec["paths"] diff --git a/student-tests/unit/test_unit.py b/student-tests/unit/test_unit.py new file mode 100644 index 0000000..3461286 --- /dev/null +++ b/student-tests/unit/test_unit.py @@ -0,0 +1,134 @@ +"""Pure functions, no services: validation, cursor, Authorization parsing, Argon2id, config.""" +from __future__ import annotations + +import asyncio +import base64 +import datetime as dt + +import pytest + +from messenger import authhdr, cursor, passwords, validation +from messenger.config import ConfigError, Settings +from messenger.errors import InvalidRequest, Unauthorized +from messenger.timefmt import UTC, from_us, rfc3339, to_us + + +def b64(raw: bytes) -> str: + return base64.b64encode(raw).decode() + + +# ------------------------------------------------------------------ password rules +@pytest.mark.parametrize("password", ["Twelve-chars", "я" * 128, "пароль:с:двоеточиями", "😀" * 12, "a b c d e f g"]) +def test_valid_passwords(password): + assert validation.password_field({"password": password}) == password + + +@pytest.mark.parametrize("password", ["short-11ch", "я" * 129, " " * 12, "\t\n " * 5, "\ud800" * 12, None, 123456789012]) +def test_invalid_passwords(password): + with pytest.raises(InvalidRequest): + validation.password_field({"password": password}) + + +def test_text_may_contain_nul_but_not_lone_surrogates(): + assert validation.text_field({"t": "a\u0000b"}, "t", 10) == "a\u0000b" + with pytest.raises(InvalidRequest): + validation.text_field({"t": "a\ud800"}, "t", 10) + + +# ------------------------------------------------------------------ cursor +CHAT = "3f2c7a5e-0b7d-4a35-9a43-8d1f0f3b2c11" + + +def test_cursor_roundtrip_and_binding(): + key = (to_us(dt.datetime(2026, 10, 12, 12, 0, 0, 123456, tzinfo=UTC)), "11111111-1111-4111-8111-111111111111") + token = cursor.encode(cursor.MESSAGES, CHAT, key) + assert cursor.decode(token, cursor.MESSAGES, CHAT) == key + with pytest.raises(InvalidRequest): + cursor.decode(token, cursor.MESSAGES, "00000000-0000-4000-8000-000000000000") # other chat + with pytest.raises(InvalidRequest): + cursor.decode(token, cursor.CHATS, CHAT) # other resource + + +@pytest.mark.parametrize("payload", [ + b"v1|m|" + CHAT.encode() + b"|253402300800000000|11111111-1111-4111-8111-111111111111", # 1 us after 9999-12-31 + b"v1|m|" + CHAT.encode() + b"|9999999999999999999|11111111-1111-4111-8111-111111111111", # int64 edge + b"v1|m|" + CHAT.encode() + b"|-1|11111111-1111-4111-8111-111111111111", + b"v1|m|" + CHAT.encode() + b"|1|not-a-uuid", + b"v1|m|" + CHAT.encode() + b"|1|11111111-1111-4111-8111-11111111111G", + b"v2|m|" + CHAT.encode() + b"|1|11111111-1111-4111-8111-111111111111", +]) +def test_broken_cursors_are_400_not_500(payload): + token = base64.urlsafe_b64encode(payload).rstrip(b"=").decode() + with pytest.raises(InvalidRequest): + cursor.decode(token, cursor.MESSAGES, CHAT) + + +def test_time_helpers_are_exact(): + moment = dt.datetime(2026, 2, 28, 23, 59, 59, 999999, tzinfo=UTC) + assert from_us(to_us(moment)) == moment + assert rfc3339(moment) == "2026-02-28T23:59:59.999999Z" + assert from_us(cursor.MAX_TS_US).year == 9999 + + +# ------------------------------------------------------------------ Authorization header +def test_basic_is_parsed_and_password_may_contain_colons(): + assert authhdr.parse_basic("Basic " + b64("alice:pa:ss:wörd".encode())) == ("alice", "pa:ss:wörd") + assert authhdr.parse_basic("basic " + b64(b"bob_1:x" * 1)) == ("bob_1", "x") + + +@pytest.mark.parametrize("header", [ + None, "", "Basic", "Basic é", "Basic !!!!", "Basic " + b64(b"no-colon"), "Basic " + b64(b"\xff\xfe:pw"), + "Basic " + b64(b":password"), "Basic " + b64(b"alice:"), "Basic " + b64("Алиса:pw".encode()), + "Bearer " + "a" * 43, "Basic " + "A" * 2000, +]) +def test_bad_basic_is_401_with_basic_challenge(header): + with pytest.raises(Unauthorized) as info: + authhdr.parse_basic(header) + assert info.value.headers["WWW-Authenticate"].startswith("Basic realm=\"messenger\"") + + +@pytest.mark.parametrize("header", [None, "", "Bearer", "Bearer short", "Bearer " + "a" * 43 + "!", + "Basic " + b64(b"alice:password-123"), "Token " + "a" * 43, "Bearer " + "é" * 43]) +def test_bad_bearer_is_401_with_bearer_challenge(header): + with pytest.raises(Unauthorized) as info: + authhdr.parse_bearer(header) + assert info.value.headers["WWW-Authenticate"] == 'Bearer realm="messenger"' + + +def test_bearer_ok(): + token = "A_b-" + "x" * 39 + assert authhdr.parse_bearer("Bearer " + token) == token + + +# ------------------------------------------------------------------ Argon2id +def test_argon2id_hash_and_verify(): + async def scenario(): + stored = await passwords.hash_password("я" * 128) + assert stored.startswith("$argon2id$v=19$m=19456,t=2,p=1$") + assert await passwords.verify_password(stored, "я" * 128) + assert not await passwords.verify_password(stored, "я" * 127) + assert not await passwords.verify_password(None, "anything") # unknown user, dummy hash + assert not await passwords.verify_password("not-a-hash", "anything") + assert await passwords.hash_password("same password!") != await passwords.hash_password("same password!") # salt + asyncio.run(scenario()) + + +# ------------------------------------------------------------------ config +def test_settings_read_secret_files_and_contract_names(tmp_path, monkeypatch): + url_file = tmp_path / "database_url" + url_file.write_text("postgresql://u:p@db/x\n", encoding="utf-8") + monkeypatch.setenv("DATABASE_URL_FILE", str(url_file)) + monkeypatch.setenv("SESSION_STORE", "redis") + monkeypatch.setenv("CACHE_URL", "redis://:p@valkey:6379/0") + monkeypatch.setenv("SESSION_TTL_SECONDS", "2") + settings = Settings.from_env() + assert settings.database_url == "postgresql://u:p@db/x" + assert settings.session_store == "valkey" and settings.session_ttl_seconds == 2 + monkeypatch.setenv("SESSION_TTL_SECONDS", "0") + with pytest.raises(ConfigError): + Settings.from_env() + monkeypatch.setenv("SESSION_TTL_SECONDS", "2") + monkeypatch.setenv("DATABASE_URL_FILE", str(tmp_path / "missing")) + with pytest.raises(ConfigError) as info: + Settings.from_env() + assert "u:p" not in str(info.value)