test: cover tampered cursors and UUID input forms
Add black-box tests for the strict-id-validation change: a server-issued cursor with one field changed (non-string or non-canonical item id, v=true, extra field) on both lists, member_ids with values of other JSON types, a non-array member_ids and other UUID notations, and chat_id in the path of the three chat endpoints. Sync the change's delta specs into api-conventions, chats and pagination so the coverage check knows the new scenarios. The cursor and UUID-notation tests fail on the current code by design; the fix follows in the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
форматы."""
|
||||
|
||||
import json
|
||||
import urllib.parse
|
||||
import uuid
|
||||
from collections import Counter
|
||||
from typing import Any
|
||||
|
||||
@@ -19,6 +21,7 @@ from api_client import (
|
||||
from contract import (
|
||||
assert_created_at,
|
||||
assert_error,
|
||||
assert_errors,
|
||||
assert_instance_id,
|
||||
assert_ok,
|
||||
assert_uuid,
|
||||
@@ -26,6 +29,10 @@ from contract import (
|
||||
)
|
||||
|
||||
UNICODE_TEXT = "Привет, мир 👋"
|
||||
# не UUID в пути: типы JSON, записанные строкой URL
|
||||
NOT_UUID_PATHS = ("123", "false", "[]", "{}")
|
||||
# не UUID в элементе member_ids: значения других типов JSON
|
||||
NOT_UUID_VALUES: tuple[Any, ...] = ([], {}, 123, False)
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Unicode сохраняется")
|
||||
@@ -259,3 +266,126 @@ def test_id_and_created_at_format(
|
||||
assert_uuid(member_id)
|
||||
assert_uuid(message["chat_id"])
|
||||
assert_uuid(message["sender_id"])
|
||||
|
||||
|
||||
def other_uuid_forms(value: str) -> dict[str, str]:
|
||||
"""Тот же UUID в записях, которых нет в `format: uuid` контракта."""
|
||||
return {
|
||||
"no hyphens": value.replace("-", ""),
|
||||
"urn": f"urn:uuid:{value}",
|
||||
"braces": f"{{{value}}}",
|
||||
}
|
||||
|
||||
|
||||
def chat_id_requests(
|
||||
api: Api,
|
||||
actor: Actor,
|
||||
chat_id: str,
|
||||
) -> dict[str, httpx.Response]:
|
||||
"""Три ручки с `chat_id` в пути; значение кодируется для URL явно."""
|
||||
quoted = urllib.parse.quote(chat_id, safe="")
|
||||
return {
|
||||
"GET chat": api.request("GET", f"{API}/chats/{quoted}", actor=actor),
|
||||
"GET history": api.messages_page(actor, quoted),
|
||||
"POST message": api.post_message(
|
||||
actor,
|
||||
quoted,
|
||||
json={"text": "path"},
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def post_chat(
|
||||
api: Api,
|
||||
actor: Actor,
|
||||
title: str,
|
||||
member_ids: list[Any],
|
||||
) -> httpx.Response:
|
||||
return api.request(
|
||||
"POST",
|
||||
f"{API}/chats",
|
||||
actor=actor,
|
||||
json={"title": title, "member_ids": member_ids},
|
||||
)
|
||||
|
||||
|
||||
def chat_titles(api: Api, actor: Actor) -> set[str]:
|
||||
return {
|
||||
chat["title"]
|
||||
for page in api.chat_pages(actor)
|
||||
for chat in page["items"]
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "UUID другого типа в теле")
|
||||
def test_uuid_of_other_type_in_body(api: Api, alice: Actor) -> None:
|
||||
title = f"rejected-{uuid.uuid4().hex}"
|
||||
# рядом с id Алисы: иначе 400 дала бы и проверка «вызывающий в списке»
|
||||
assert_errors(
|
||||
{
|
||||
repr(value): post_chat(api, alice, title, [alice.id, value])
|
||||
for value in NOT_UUID_VALUES
|
||||
},
|
||||
400,
|
||||
)
|
||||
assert title not in chat_titles(api, alice)
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Не UUID в пути")
|
||||
def test_not_uuid_in_path(api: Api, alice: Actor) -> None:
|
||||
assert_errors(
|
||||
{
|
||||
f"{name} {value}": resp
|
||||
for value in NOT_UUID_PATHS
|
||||
for name, resp in chat_id_requests(api, alice, value).items()
|
||||
},
|
||||
400,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Другая запись UUID")
|
||||
def test_other_uuid_forms(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
bob: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
history = api.all_messages(alice, chat["id"])
|
||||
title = f"rejected-{uuid.uuid4().hex}"
|
||||
responses = {
|
||||
f"{name} / {form}": resp
|
||||
for form, value in other_uuid_forms(chat["id"]).items()
|
||||
for name, resp in chat_id_requests(api, alice, value).items()
|
||||
}
|
||||
responses |= {
|
||||
f"member_ids / {form}": post_chat(api, alice, title, [alice.id, value])
|
||||
for form, value in other_uuid_forms(bob.id).items()
|
||||
}
|
||||
assert_errors(responses, 400)
|
||||
assert api.all_messages(alice, chat["id"]) == history
|
||||
assert title not in chat_titles(api, alice)
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "UUID в верхнем регистре в пути")
|
||||
def test_upper_case_uuid_in_path(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
upper = chat["id"].upper()
|
||||
got = api.request("GET", f"{API}/chats/{upper}", actor=alice)
|
||||
# assert_ok проверяет и формат: id и chat_id в ответах — нижний регистр
|
||||
assert assert_ok(got, 200, "Chat") == chat
|
||||
sent = assert_ok(
|
||||
api.post_message(alice, upper, json={"text": "upper"}),
|
||||
201,
|
||||
"Message",
|
||||
)
|
||||
assert sent["chat_id"] == chat["id"]
|
||||
upper_page = assert_ok(api.messages_page(alice, upper), 200, "MessagePage")
|
||||
lower_page = assert_ok(
|
||||
api.messages_page(alice, chat["id"]),
|
||||
200,
|
||||
"MessagePage",
|
||||
)
|
||||
assert upper_page == lower_page == {"items": [sent], "next_cursor": None}
|
||||
|
||||
Reference in New Issue
Block a user