#!/usr/bin/env python3 import concurrent.futures import json import os import sys import uuid import urllib.error import urllib.parse import urllib.request from datetime import datetime import base64 BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/") PASSWORD = "correct-horse-battery" def request(method, path, expected, body=None, headers=None, raw_body=None): url = BASE_URL + path request_headers = dict(headers or {}) if raw_body is not None: data = raw_body elif body is not None: data = json.dumps(body, ensure_ascii=False).encode("utf-8") else: data = None if body is not None or raw_body is not None: request_headers.setdefault("Content-Type", "application/json") req = urllib.request.Request( url, data=data, method=method, headers=request_headers, ) try: with urllib.request.urlopen(req, timeout=10) as response: status = response.status response_headers = response.headers response_body = response.read() except urllib.error.HTTPError as exc: status = exc.code response_headers = exc.headers response_body = exc.read() if status != expected: raise AssertionError( f"{method} {path}: expected HTTP {expected}, got {status}. " f"Body={response_body!r}" ) request_id = response_headers.get("X-Request-Id") if not request_id: raise AssertionError( f"{method} {path}: missing X-Request-Id" ) parsed = None if response_body: content_type = response_headers.get_content_type() if content_type == "application/json": parsed = json.loads(response_body.decode("utf-8")) if status >= 400: codes = { 400: "invalid_request", 401: "unauthorized", 404: "not_found", 409: "conflict", 413: "payload_too_large", 415: "unsupported_media_type", 429: "rate_limited", 500: "internal_error", 503: "unavailable", } if not isinstance(parsed, dict) or set(parsed) != {"error"}: raise AssertionError(f"Invalid error response: {parsed!r}") error = parsed["error"] if not isinstance(error, dict) or set(error) != {"code", "message", "request_id"}: raise AssertionError(f"Invalid error fields: {error!r}") if error["code"] != codes[status] or error["request_id"] != request_id: raise AssertionError(f"Wrong error code or request ID: {error!r}") if not isinstance(error["message"], str) or not error["message"].strip(): raise AssertionError("Error message must be a nonempty string") return { "status": status, "headers": response_headers, "json": parsed, "bytes": response_body, } def unique_username(prefix): # username должен соответствовать [a-z0-9_]{3,32} suffix = uuid.uuid4().hex[:12] return f"{prefix}_{suffix}" def basic(username, password=PASSWORD): raw = f"{username}:{password}".encode("utf-8") return "Basic " + base64.b64encode(raw).decode() def create_user(prefix="usr", login=True): user = request("POST", "/api/v1/users", 201, body={ "username": unique_username(prefix), "display_name": f"Test {prefix}", "password": PASSWORD, })["json"] if login: session = request("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": basic(user["username"])})["json"] user["token"] = session["token"] return user def auth(user): return {"Authorization": "Bearer " + user["token"]} def create_chat(owner, members, title="Student test chat"): result = request( "POST", "/api/v1/chats", 201, headers=auth(owner), body={ "title": title, "member_ids": [u["id"] for u in members], }, ) return result["json"] def create_message(user, chat, text): return request( "POST", f"/api/v1/chats/{chat['id']}/messages", 201, headers=auth(user), body={"text": text}, )["json"] def test_malformed_json(): for body in (b'{"username": "broken",', b"\xff"): request("POST", "/api/v1/users", 400, raw_body=body) print("PASS malformed JSON and invalid UTF-8 -> 400 with error envelope") def test_duplicate_username(): username = unique_username("dup") body = { "username": username, "display_name": "First", "password": PASSWORD, } request( "POST", "/api/v1/users", 201, body=body, ) request( "POST", "/api/v1/users", 409, body={ "username": username, "display_name": "Second", "password": PASSWORD, }, ) print("PASS duplicate username -> 409") def test_outsider_chat(): alice = create_user("alice") bob = create_user("bob") eve = create_user("eve") chat = create_chat( alice, [alice, bob], "Private chat", ) request( "GET", f"/api/v1/chats/{chat['id']}", 404, headers=auth(eve), ) request( "GET", f"/api/v1/chats/{chat['id']}/messages", 404, headers=auth(eve), ) request( "POST", f"/api/v1/chats/{chat['id']}/messages", 404, headers=auth(eve), body={"text": "Not allowed"}, ) page = request("GET", "/api/v1/chats", 200, headers=auth(eve))["json"] if page != {"items": [], "next_cursor": None}: raise AssertionError("Outsider can see a private chat in the list") print("PASS outsider cannot access chat -> 404") def test_empty_and_whitespace_message(): alice = create_user("emptya") bob = create_user("emptyb") chat = create_chat( alice, [alice, bob], "Empty message test", ) request( "POST", f"/api/v1/chats/{chat['id']}/messages", 400, headers=auth(alice), body={"text": ""}, ) request( "POST", f"/api/v1/chats/{chat['id']}/messages", 400, headers=auth(alice), body={"text": " \t\n"}, ) print("PASS empty/whitespace message -> 400") def test_extra_json_field(): request( "POST", "/api/v1/users", 400, body={ "username": unique_username("extra"), "display_name": "Extra", "password": PASSWORD, "admin": True, }, ) print("PASS extra JSON field -> 400") def test_multi_page_history(): alice = create_user("pagea") bob = create_user("pageb") chat = create_chat( alice, [alice, bob], "Pagination test", ) expected = [] for i in range(7): message = create_message( alice, chat, f"pagination-message-{i}", ) expected.append(message) expected.sort( key=lambda message: ( datetime.fromisoformat( message["created_at"].replace("Z", "+00:00") ), uuid.UUID(message["id"]).int, ) ) expected_ids = [ message["id"] for message in expected ] collected = [] cursor = None for _ in range(len(expected) + 1): path = ( f"/api/v1/chats/{chat['id']}/messages" f"?limit=2" ) if cursor is not None: path += "&cursor=" + urllib.parse.quote( cursor, safe="", ) page = request( "GET", path, 200, headers=auth(bob), )["json"] ids = [item["id"] for item in page["items"]] collected.extend(ids) cursor = page["next_cursor"] if cursor is None: break else: raise AssertionError("Pagination did not reach the last page") if collected != expected_ids: raise AssertionError( "Pagination lost, duplicated or reordered messages.\n" f"Expected: {expected_ids}\n" f"Received: {collected}" ) if len(collected) != len(set(collected)): raise AssertionError( "Pagination returned duplicate messages" ) print( f"PASS pagination: {len(collected)} messages " "read through multiple pages" ) def test_concurrent_100_messages(): users = [ create_user(f"c{i}") for i in range(10) ] chat = create_chat( users[0], users, "Concurrency test", ) def worker(client_index): sent = [] for message_index in range(10): text = ( f"client-{client_index}-" f"message-{message_index}" ) result = create_message( users[client_index], chat, text, ) sent.append(result) return sent all_sent = [] with concurrent.futures.ThreadPoolExecutor( max_workers=10 ) as executor: futures = [ executor.submit(worker, i) for i in range(10) ] for future in concurrent.futures.as_completed( futures ): all_sent.extend(future.result()) if len(all_sent) != 100: raise AssertionError( f"Expected 100 successful POSTs, got {len(all_sent)}" ) page = request( "GET", f"/api/v1/chats/{chat['id']}/messages?limit=100", 200, headers=auth(users[0]), )["json"] messages = page["items"] if len(messages) != 100: raise AssertionError( f"Expected exactly 100 stored messages, " f"got {len(messages)}" ) ids = [message["id"] for message in messages] if len(set(ids)) != 100: raise AssertionError( "Expected 100 unique message IDs" ) if set(ids) != {message["id"] for message in all_sent}: raise AssertionError("Stored messages differ from successful POST responses") texts = [message["text"] for message in messages] if len(set(texts)) != 100: raise AssertionError( "Expected 100 unique message texts" ) if page["next_cursor"] is not None: raise AssertionError( "100 messages with limit=100 should fit in one page" ) print( "PASS concurrency: 10 clients sent 100 messages; " "exactly 100 unique messages stored" ) def test_invalid_chat_ids(): user = create_user("uuid") for chat_id, expected in (("not-a-uuid", 400), (str(uuid.uuid4()), 404)): path = f"/api/v1/chats/{chat_id}" request("GET", path, expected, headers=auth(user)) request("GET", path + "/messages", expected, headers=auth(user)) request("POST", path + "/messages", expected, headers=auth(user), body={"text": "Test"}) print("PASS invalid chat UUID -> 400; unknown valid UUID -> 404") def test_chat_member_limits(): users = [create_user("member", login=(i == 0)) for i in range(101)] owner = users[0] for members in ([], users, [owner, owner], users[1:3]): request("POST", "/api/v1/chats", 400, headers=auth(owner), body={ "title": "Invalid members", "member_ids": [user["id"] for user in members], }) request("POST", "/api/v1/chats", 400, headers=auth(owner), body={ "title": "Unknown member", "member_ids": [owner["id"], str(uuid.uuid4())], }) chat = create_chat(owner, users[:100]) if len(chat["member_ids"]) != 100: raise AssertionError("Chat must accept exactly 100 members") print("PASS chat members: 1..100, unique, existing, caller included") def test_invalid_pagination(): user = create_user("cursor") chat = create_chat(user, [user]) other_chat = create_chat(user, [user]) other_message = create_message(user, other_chat, "Other history") paths = ("/api/v1/chats", f"/api/v1/chats/{chat['id']}/messages") for path in paths: for query in ("cursor=", "cursor=unknown", "limit=0", "limit=101", "limit=abc", "limit=1.5"): request("GET", path + "?" + query, 400, headers=auth(user)) request("GET", paths[1] + "?cursor=" + other_message["id"], 400, headers=auth(user)) print("PASS invalid cursor/limit and cursor from another chat -> 400") def test_multi_page_chats(): user = create_user("chatpage") chats = [create_chat(user, [user], f"Chat {i}") for i in range(7)] chats.sort(key=lambda chat: (chat["created_at"], uuid.UUID(chat["id"]).int)) collected = [] cursor = None for page_number in range(len(chats) + 1): query = {"limit": 2 if page_number == 0 else 3} if cursor is not None: query["cursor"] = cursor page = request("GET", "/api/v1/chats?" + urllib.parse.urlencode(query), 200, headers=auth(user))["json"] collected.extend(chat["id"] for chat in page["items"]) cursor = page["next_cursor"] if cursor is None: break else: raise AssertionError("Chat pagination did not reach the last page") if collected != [chat["id"] for chat in chats]: raise AssertionError("Chat pagination lost, duplicated or reordered chats") print("PASS pagination: 7 chats, changing limit between pages") def test_validation_and_unicode(): for display_name in ("", " \t", "x" * 101, 123, None): request("POST", "/api/v1/users", 400, body={ "username": unique_username("invalid"), "display_name": display_name, "password": PASSWORD, }) request("POST", "/api/v1/users", 400, body={"username": True, "display_name": "Test", "password": PASSWORD}) for password in ("short", "x" * 129, 12345678901234, None): request("POST", "/api/v1/users", 400, body={ "username": unique_username("badpw"), "display_name": "Test", "password": password, }) request("POST", "/api/v1/users", 400, body={"username": unique_username("nopw"), "display_name": "Test"}) unicode_password = "\U00020000" * 12 # пароль не из ASCII проходит через Basic/UTF-8 и KDF user = request("POST", "/api/v1/users", 201, body={ "username": unique_username("unicode"), "display_name": "\U00020000" * 100, "password": unicode_password, })["json"] session = request("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": basic(user["username"], unicode_password)})["json"] user["token"] = session["token"] chat = create_chat(user, [user]) path = f"/api/v1/chats/{chat['id']}/messages" text = "\U00020000" * 4000 message = create_message(user, chat, text) if message["text"] != text: raise AssertionError("Unicode message was altered") for invalid in (text + "x", 123, None): request("POST", path, 400, headers=auth(user), body={"text": invalid}) request("GET", "/api/v1/users/me", 401) request("GET", "/api/v1/users/me", 401, headers={"X-User-Id": user["id"]}) request("GET", "/api/v1/users/me", 401, headers={"Authorization": basic(user["username"], unicode_password)}) print("PASS strict types, Unicode, password rules, Bearer-only identification") def main(): tests = [ test_malformed_json, test_duplicate_username, test_outsider_chat, test_empty_and_whitespace_message, test_extra_json_field, test_multi_page_history, test_concurrent_100_messages, test_invalid_chat_ids, test_chat_member_limits, test_invalid_pagination, test_multi_page_chats, test_validation_and_unicode, ] print(f"Testing {BASE_URL}") for test in tests: test() print() print( f"PASS: {len(tests)} student test scenarios completed" ) if __name__ == "__main__": try: main() except Exception as exc: print( f"FAIL: {type(exc).__name__}: {exc}", file=sys.stderr, ) sys.exit(1)