Files
mtusi2026lab4/student-tests/contract.py
T

275 lines
10 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", "owner_id"})
# списки UUID в ответах: участники чата и с лабы 4 вложения сообщения
ID_LIST_KEYS = frozenset({"member_ids", "attachment_ids"})
TIME_KEYS = frozenset({"created_at", "expires_at", "updated_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",
413: "payload_too_large",
415: "unsupported_media_type",
# с change upload-memory: все места загрузки процесса заняты
429: "rate_limited",
500: "internal_error",
503: "unavailable",
}
# Retry-After у 429 — положительное целое число секунд (спек
# api-conventions, «Соответствие статусов и кодов ошибок»)
RETRY_AFTER_RE = re.compile(r"^[1-9][0-9]*$")
@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 in ID_LIST_KEYS:
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",
)
if status == 429:
retry_after = resp.headers.get("Retry-After", "")
assert RETRY_AFTER_RE.fullmatch(retry_after), describe(
resp,
f"Retry-After {retry_after!r} is not a positive integer",
)
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_no_storage_traces(resp: httpx.Response) -> None:
"""Ни перенаправления, ни заголовков S3 в ответе API."""
assert "location" not in resp.headers, describe(resp, "has Location")
amz = [name for name in resp.headers if name.lower().startswith("x-amz-")]
assert not amz, describe(resp, f"S3 headers {amz}")
def assert_content(
resp: httpx.Response,
data: bytes,
content_type: str,
) -> None:
"""Байты оригинала: 200, тип без параметров, длина, `nosniff`, те же
байты, без перенаправления и следов S3."""
assert resp.status_code == 200, describe(resp, "expected 200")
assert resp.headers.get("X-Request-Id", "").strip(), describe(
resp,
"no X-Request-Id",
)
assert_instance_id(resp)
assert resp.headers.get("Content-Type") == content_type, describe(
resp,
f"Content-Type is not {content_type}",
)
assert resp.headers.get("Content-Length") == str(len(data)), describe(
resp,
f"Content-Length is not {len(data)}",
)
assert resp.headers.get("X-Content-Type-Options") == "nosniff", describe(
resp,
"no X-Content-Type-Options: nosniff",
)
assert_no_storage_traces(resp)
assert resp.content == data, (
f"downloaded {len(resp.content)} bytes differ from {len(data)} sent"
)
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]}"
)