Copy the committed lab 1 sources as-is: src/, student-tests/, bench/, Dockerfile, docker-compose.yml, pyproject.toml, uv.lock, .python-version, config_example.toml, AGENTS.md, CLAUDE.md, .claude/, openspec/, .graphifyignore and KNOWN_ISSUES.md. Lab 1 evidence stays in the lab 1 repository. Merge the new skeleton files with own sections: Makefile (lab 2 targets plus own run, docker, bench and common targets), .gitignore, .dockerignore and .env.example. Rename the image to mtusi-messenger:lab2 and point AGENTS.md at lab 2. The application is still the in-memory lab 1 implementation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
3.2 KiB
Python
121 lines
3.2 KiB
Python
"""Самопроверка `contract.py`: неверная форма ответа роняет тест.
|
|
|
|
Спек `student-tests`, сценарий «Ошибка не в формате контракта».
|
|
"""
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from api_client import Actor
|
|
from contract import assert_error, check_body
|
|
|
|
|
|
def fake_response(
|
|
status: int,
|
|
body: object,
|
|
request_id: str = "rid-1",
|
|
) -> httpx.Response:
|
|
return httpx.Response(
|
|
status,
|
|
json=body,
|
|
headers={"X-Request-Id": request_id},
|
|
request=httpx.Request("GET", "http://stand/api/v1/chats/x"),
|
|
)
|
|
|
|
|
|
def test_contract_error_passes() -> None:
|
|
body = {
|
|
"error": {
|
|
"code": "not_found",
|
|
"message": "Not found",
|
|
"request_id": "rid-1",
|
|
},
|
|
}
|
|
assert assert_error(fake_response(404, body), 404)["code"] == "not_found"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("status", "body"),
|
|
[
|
|
(404, {"detail": "Not Found"}),
|
|
(400, {"detail": [{"loc": ["body"], "msg": "bad"}]}),
|
|
(
|
|
404,
|
|
{
|
|
"error": {
|
|
"code": "not_found",
|
|
"message": "Not found",
|
|
"request_id": "other",
|
|
},
|
|
},
|
|
),
|
|
(
|
|
404,
|
|
{
|
|
"error": {
|
|
"code": "invalid_request",
|
|
"message": "Not found",
|
|
"request_id": "rid-1",
|
|
},
|
|
},
|
|
),
|
|
(
|
|
404,
|
|
{
|
|
"error": {
|
|
"code": "not_found",
|
|
"message": "Not found",
|
|
"request_id": "rid-1",
|
|
"detail": "x",
|
|
},
|
|
},
|
|
),
|
|
(
|
|
500,
|
|
{
|
|
"error": {
|
|
"code": "internal_error",
|
|
"message": 'File "/app/src/main.py", line 1',
|
|
"request_id": "rid-1",
|
|
},
|
|
},
|
|
),
|
|
],
|
|
ids=[
|
|
"fastapi-404",
|
|
"fastapi-validation",
|
|
"request-id-mismatch",
|
|
"code-not-matching-status",
|
|
"extra-field",
|
|
"leaks-internals",
|
|
],
|
|
)
|
|
def test_wrong_error_body_fails(status: int, body: object) -> None:
|
|
# статус верный: падать должна проверка тела
|
|
with pytest.raises(AssertionError):
|
|
assert_error(fake_response(status, body), status)
|
|
|
|
|
|
def test_success_body_checks(alice: Actor) -> None:
|
|
user = alice.user
|
|
check_body(user, "User")
|
|
broken = {
|
|
"extra field": {**user, "extra": 1},
|
|
"missing field": {k: v for k, v in user.items() if k != "username"},
|
|
"uppercase id": {**user, "id": user["id"].upper()},
|
|
"created_at without Z": {
|
|
**user,
|
|
"created_at": user["created_at"][:-1],
|
|
},
|
|
"created_at without microseconds": {
|
|
**user,
|
|
"created_at": user["created_at"][:19] + "Z",
|
|
},
|
|
}
|
|
for name, body in broken.items():
|
|
try:
|
|
check_body(body, "User")
|
|
except AssertionError:
|
|
continue
|
|
pytest.fail(f"accepted: {name}")
|