выполнение лр2 на 4

This commit is contained in:
Ilya
2026-10-09 12:44:56 +03:00
parent e914e384fc
commit 13fa1852cb
30 changed files with 585 additions and 238 deletions
+1 -18
View File
@@ -1,13 +1,4 @@
#!/usr/bin/env python3
"""Student-written negative/edge-case tests, additional to the public tests/smoke.py.
Covers level-4 requirements from README.md:
- malformed JSON
- foreign chat access
- empty message text
- duplicate username
- multi-page pagination traversal
"""
import json
import sys
import urllib.error
@@ -17,7 +8,7 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
from client import Client, check
PASSED = 0
@@ -46,7 +37,6 @@ def main():
alice, bob, eve, chat = c.bootstrap()
path = "/api/v1/chats/" + chat["id"]
# 1. Malformed JSON body -> 400 invalid_request, not a 500.
status, body = raw_call(
base_url, "POST", "/api/v1/users",
data=b'{"username": "broken", "display_name": ',
@@ -57,24 +47,19 @@ def main():
check(parsed["error"]["code"] == "invalid_request", "malformed JSON must report invalid_request")
ok("malformed JSON -> 400 invalid_request")
# 2. Foreign chat: eve is not a member -> 404, not 403 (no existence leak).
c.call("GET", path, 404, actor=eve)
c.call("GET", path + "/messages", 404, actor=eve)
ok("foreign chat access -> 404 (no leak)")
# 3. Empty / whitespace-only message text -> 400.
c.call("POST", path + "/messages", 400, actor=alice, body={"text": ""})
c.call("POST", path + "/messages", 400, actor=alice, body={"text": " "})
ok("empty/whitespace message text -> 400")
# 4. Duplicate username -> 409 conflict, original user untouched.
c.call("POST", "/api/v1/users", 409, body=alice["registration"])
me = c.call("GET", "/api/v1/users/me", actor=alice, schema="User")["json"]
check(me == alice["user"], "existing user must be unaffected by a rejected duplicate registration")
ok("duplicate username -> 409, original user intact")
# 5. Multi-page pagination traversal: write N messages, walk every page with a small
# limit and confirm the full, ordered, duplicate-free set is recovered.
sent = [c.message(alice, chat, "page-msg-%d" % i)["json"] for i in range(7)]
collected = []
cursor = None
@@ -96,8 +81,6 @@ def main():
check(tail == sorted(tail, key=lambda m: (m["created_at"], m["id"])), "pagination must stay ordered")
ok("multi-page pagination traversal: %d pages, %d messages, no dup/loss" % (pages, len(collected)))
# 6. A cursor is bound to the chat it was issued for: reusing it on a
# different chat the same actor can also read must be rejected.
first_page = c.call("GET", path + "/messages?limit=1", actor=alice, schema="MessagePage")["json"]
check(first_page["next_cursor"], "need a non-null cursor to test cross-chat reuse")
chat2 = c.call(