Files
mtusi2026lab4/student-tests/contract.py
T
ricoromaandClaude Opus 5.5 5d5d8f9d2a chore: carry lab 3 over
- carry the lab 3 solution over from mtusi2026lab3-safronov
  (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests,
  bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts,
  Dockerfile, compose, pyproject and uv lock
- merge Makefile, .gitignore and .dockerignore (the lab 3 versions only
  add lines to the skeleton, which is the same in lab 4) and
  .env.example (lab 4 skeleton names listed in the header)
- rename the compose project to mtusi-lab4 and the image to :lab4 so
  the stand does not reuse the lab 3 containers and volumes
- update AGENTS.md for lab 4: current lab, origin commit, contract and
  test files of the new skeleton

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:32:33 +03:00

222 lines
7.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Проверка ответов по `contracts/openapi.json` и форматам контракта."""
import json
import re
from collections.abc import Iterable, Mapping
from datetime import datetime, timedelta
from functools import cache
from pathlib import Path
from typing import Any
import httpx
from jsonschema import Draft202012Validator
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT202012
CONTRACT_PATH = (
Path(__file__).resolve().parents[1] / "contracts" / "openapi.json"
)
_CONTRACT_URI = "urn:mtusi:contract"
UUID_RE = re.compile(
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
)
# RFC 3339 в UTC: шесть знаков долей секунды и суффикс Z
CREATED_AT_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$")
ID_KEYS = frozenset({"id", "chat_id", "sender_id"})
TIME_KEYS = frozenset({"created_at", "expires_at"})
# WWW-Authenticate у 401 (contracts/README.md): вход и остальные запросы
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
BEARER_CHALLENGE = 'Bearer realm="messenger"'
# признаки внутренностей сервиса в error.message
LEAK_RE = re.compile(r"Traceback|File \"|\.py\b|/app/|/src/|Error\(")
# X-Instance-Id (спек api-conventions): допустим в заголовке и журнале
INSTANCE_ID_RE = re.compile(r"^[A-Za-z0-9._-]{1,64}$")
# статус -> code ошибки (спек api-conventions)
ERROR_CODES = {
400: "invalid_request",
401: "unauthorized",
404: "not_found",
405: "invalid_request",
409: "conflict",
500: "internal_error",
503: "unavailable",
}
@cache
def contract() -> dict[str, Any]:
return json.loads(CONTRACT_PATH.read_text(encoding="utf-8"))
@cache
def validator(schema: str) -> Draft202012Validator:
"""Валидатор `#/components/schemas/<schema>` внутри контракта."""
document = contract()
if schema not in document["components"]["schemas"]:
raise KeyError(f"no schema {schema!r} in {CONTRACT_PATH.name}")
resource = Resource.from_contents(
document,
default_specification=DRAFT202012,
)
registry = Registry().with_resource(_CONTRACT_URI, resource)
return Draft202012Validator(
{"$ref": f"{_CONTRACT_URI}#/components/schemas/{schema}"},
registry=registry,
)
def check_body(body: Any, schema: str) -> None:
"""Тело совпадает со схемой контракта, `id` и время — в его формате."""
errors = sorted(
validator(schema).iter_errors(body),
key=lambda error: list(map(str, error.path)),
)
assert not errors, f"{schema} schema: " + "; ".join(
f"/{'/'.join(map(str, error.path))}: {error.message}"
for error in errors
)
check_formats(body)
def check_formats(value: Any, path: str = "") -> None:
if isinstance(value, dict):
for key, item in value.items():
where = f"{path}/{key}"
if key in ID_KEYS:
assert_uuid(item, where)
elif key == "member_ids":
for number, member in enumerate(item):
assert_uuid(member, f"{where}/{number}")
elif key in TIME_KEYS:
assert_created_at(item, where)
else:
check_formats(item, where)
elif isinstance(value, list):
for number, item in enumerate(value):
check_formats(item, f"{path}/{number}")
def assert_uuid(value: Any, where: str = "id") -> None:
assert isinstance(value, str) and UUID_RE.fullmatch(value), (
f"{where}: {value!r} is not a lowercase UUID"
)
def assert_created_at(value: Any, where: str = "created_at") -> None:
assert isinstance(value, str) and CREATED_AT_RE.fullmatch(value), (
f"{where}: {value!r} is not RFC 3339 UTC with microseconds and Z"
)
parsed = datetime.fromisoformat(value)
assert parsed.utcoffset() == timedelta(0), f"{where}: {value!r} not UTC"
def assert_ok(resp: httpx.Response, status: int, schema: str) -> Any:
"""Успешный ответ: статус, JSON, `X-Request-Id`, `X-Instance-Id`, тело
по схеме."""
assert resp.status_code == status, describe(resp, f"expected {status}")
body = json_body(resp)
check_body(body, schema)
return body
def assert_error(resp: httpx.Response, status: int) -> dict[str, Any]:
"""Ошибка в формате контракта; `code` соответствует статусу."""
assert resp.status_code == status, describe(resp, f"expected {status}")
body = json_body(resp)
check_body(body, "Error")
error = body["error"]
assert error["code"] == ERROR_CODES[status], describe(
resp,
f"expected code {ERROR_CODES[status]}",
)
assert error["message"].strip(), describe(resp, "empty message")
assert not LEAK_RE.search(error["message"]), describe(
resp,
"message reveals internals",
)
assert error["request_id"] == resp.headers["X-Request-Id"], describe(
resp,
"request_id differs from X-Request-Id",
)
return error
def assert_errors(
responses: Mapping[str, httpx.Response],
status: int,
) -> None:
"""Каждый вариант запроса — ошибка `status` в формате контракта.
Статусы сверяются все разом: при падении видно, что ответил каждый
вариант, а не только первый.
"""
statuses = {name: resp.status_code for name, resp in responses.items()}
assert set(statuses.values()) == {status}, (
f"expected {status} for every variant, got {statuses}"
)
for resp in responses.values():
assert_error(resp, status)
def assert_unauthorized(
resp: httpx.Response,
challenge: str,
) -> dict[str, Any]:
"""401 в формате контракта с заданным `WWW-Authenticate`."""
error = assert_error(resp, 401)
assert resp.headers.get("WWW-Authenticate") == challenge, describe(
resp,
f"expected WWW-Authenticate {challenge}",
)
return error
def assert_instance_id(resp: httpx.Response) -> str:
"""Непустой `X-Instance-Id` допустимого формата; возвращает значение."""
instance_id = resp.headers.get("X-Instance-Id", "")
assert INSTANCE_ID_RE.fullmatch(instance_id), describe(
resp,
f"bad X-Instance-Id {instance_id!r}",
)
return instance_id
def assert_replicas(values: Iterable[str], expected: int) -> set[str]:
"""Ответы дали не меньше двух разных `X-Instance-Id`; возвращает их.
`expected` — `EXPECT_REPLICAS`: сколько реплик ждали за `BASE_URL`.
"""
seen = set(values)
assert len(seen) >= 2, (
f"saw {len(seen)} X-Instance-Id {sorted(seen)} behind BASE_URL, "
f"EXPECT_REPLICAS={expected}: no load balancing across replicas; "
"against one API process run with EXPECT_REPLICAS=1"
)
return seen
def json_body(resp: httpx.Response) -> Any:
"""JSON-тело в UTF-8, непустые `X-Request-Id` и `X-Instance-Id`."""
request_id = resp.headers.get("X-Request-Id", "")
assert request_id.strip(), describe(resp, "no X-Request-Id")
assert_instance_id(resp)
media_type, _, params = resp.headers.get("Content-Type", "").partition(
";",
)
assert media_type.strip() == "application/json", describe(
resp,
"Content-Type is not application/json",
)
charset = params.strip().lower().removeprefix("charset=")
assert charset in ("", "utf-8"), describe(resp, f"charset {charset}")
return json.loads(resp.content.decode("utf-8"))
def describe(resp: httpx.Response, problem: str) -> str:
return (
f"{resp.request.method} {resp.request.url.path}: {problem}; "
f"got {resp.status_code} {resp.text[:300]}"
)