#!/usr/bin/env python3 """Helper script to populate synthetic demo data for Lab 2 defense in Swagger UI.""" import base64 import json import sys import urllib.request import uuid BASE_URL = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080" def req(method, path, body=None, headers=None): h = {"Accept": "application/json"} data = None if body is not None: data = json.dumps(body).encode("utf-8") h["Content-Type"] = "application/json" if headers: h.update(headers) r = urllib.request.Request(BASE_URL + path, data=data, headers=h, method=method) with urllib.request.urlopen(r, timeout=10) as resp: raw = resp.read() return json.loads(raw.decode("utf-8")) if raw else None def main(): suffix = uuid.uuid4().hex[:6] alice_username = f"alice_{suffix}" bob_username = f"bob_{suffix}" password = "DemoPassword-2026!" alice = req("POST", "/api/v1/users", { "username": alice_username, "display_name": "Alice Zamorskaya", "password": password, }) bob = req("POST", "/api/v1/users", { "username": bob_username, "display_name": "Bob Bobrov", "password": password, }) alice_basic = base64.b64encode(f"{alice_username}:{password}".encode("utf-8")).decode("ascii") bob_basic = base64.b64encode(f"{bob_username}:{password}".encode("utf-8")).decode("ascii") alice_sess = req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {alice_basic}"}) bob_sess = req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {bob_basic}"}) alice_bearer = {"Authorization": f"Bearer {alice_sess['token']}"} bob_bearer = {"Authorization": f"Bearer {bob_sess['token']}"} chat = req("POST", "/api/v1/chats", { "title": "Lab 2 Defense Chat", "member_ids": [alice["id"], bob["id"]], }, headers=alice_bearer) req("POST", f"/api/v1/chats/{chat['id']}/messages", { "text": "Hello Bob! Stored in PostgreSQL 18 and cached in Redis 7.4.", }, headers=alice_bearer) req("POST", f"/api/v1/chats/{chat['id']}/messages", { "text": "Hi Alice! Authenticated via Bearer session token.", }, headers=bob_bearer) print("=== LAB 2 DEMO DATA CREATED ===") print(f"Swagger UI: {BASE_URL}/docs") print(f"Password: {password}") print(f"Alice Username: {alice_username}") print(f"Alice User ID: {alice['id']}") print(f"Alice Token: {alice_sess['token']}") print(f"Bob Username: {bob_username}") print(f"Bob User ID: {bob['id']}") print(f"Bob Token: {bob_sess['token']}") print(f"Chat ID: {chat['id']}") if __name__ == "__main__": main()