import base64 import json import secrets import urllib.error import urllib.request BASE_URL = "http://" + "127.0.0.1:8080" def request(method, path, body=None, token=None, basic=None): headers = {} if body is not None: headers["Content-Type"] = "application/json" if token is not None: headers["Authorization"] = "Bearer " + token if basic is not None: credentials = base64.b64encode( basic.encode("utf-8") ).decode("ascii") headers["Authorization"] = "Basic " + credentials req = urllib.request.Request( BASE_URL + path, data=json.dumps(body).encode("utf-8") if body is not None else None, headers=headers, method=method, ) try: response = urllib.request.urlopen(req, timeout=15) except urllib.error.HTTPError as error: response = error with response: raw = response.read() data = json.loads(raw) if raw else None return response.status, data def check(label, actual, expected): if actual != expected: raise AssertionError( f"{label}: expected {expected}, got {actual}" ) print(f"PASS: {label}: {actual}") password = "StrongPassword123!" suffix = secrets.token_hex(4) print("\n1. Register two users") users = [] for name in ["alice_" + suffix, "bob_" + suffix]: status, user = request( "POST", "/api/v1/users", { "username": name, "display_name": name, "password": password, }, ) check("register " + name, status, 201) users.append(user) print("\n2. Login") tokens = [] for user in users: status, result = request( "POST", "/api/v1/auth/sessions", basic=user["username"] + ":" + password, ) check("login " + user["username"], status, 201) tokens.append(result["token"]) print("\n3. Create chat") status, chat = request( "POST", "/api/v1/chats", { "title": "PostgreSQL test chat", "member_ids": [users[0]["id"], users[1]["id"]], }, token=tokens[0], ) check("create chat", status, 201) chat_id = chat["id"] print("Chat ID:", chat_id) print("\n4. Check chat visibility") for token in tokens: status, result = request( "GET", "/api/v1/chats/" + chat_id, token=token, ) check("get chat", status, 200) print("\n5. Send messages") for index in range(5): status, message = request( "POST", "/api/v1/chats/" + chat_id + "/messages", {"text": f"Message number {index + 1}"}, token=tokens[index % 2], ) check("send message", status, 201) print("\n6. Read first page") status, page1 = request( "GET", "/api/v1/chats/" + chat_id + "/messages?limit=2", token=tokens[0], ) check("first page", status, 200) assert len(page1["items"]) == 2 assert page1["next_cursor"] is not None print("\n7. Read second page") cursor = page1["next_cursor"] status, page2 = request( "GET", "/api/v1/chats/" + chat_id + "/messages?limit=2&cursor=" + cursor, token=tokens[0], ) check("second page", status, 200) assert len(page2["items"]) == 2 first_ids = {item["id"] for item in page1["items"]} second_ids = {item["id"] for item in page2["items"]} assert not first_ids.intersection(second_ids) print("\n8. Check chat list") status, result = request( "GET", "/api/v1/chats", token=tokens[0], ) check("list chats", status, 200) assert any(item["id"] == chat_id for item in result["items"]) print("\n9. Check PostgreSQL persistence") print("Created chat:", chat_id) print("Created messages: 5") print("\nALL MESSENGER CHECKS PASSED")