Lab2: starting
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
|
||||
CONTROL_TEXT = "CONTROL_SECRET_MESSAGE_MUST_NOT_BE_IN_LOGS"
|
||||
|
||||
|
||||
def request(
|
||||
base_url: str,
|
||||
method: str,
|
||||
path: str,
|
||||
payload: dict | None = None,
|
||||
user_id: str | None = None,
|
||||
) -> tuple[int, dict]:
|
||||
headers = {"Accept": "application/json"}
|
||||
data = None
|
||||
|
||||
if payload is not None:
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
|
||||
if user_id is not None:
|
||||
headers["X-User-Id"] = user_id
|
||||
|
||||
req = urllib.request.Request(
|
||||
f"{base_url.rstrip('/')}{path}",
|
||||
data=data,
|
||||
headers=headers,
|
||||
method=method,
|
||||
)
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
return response.status, json.loads(raw) if raw else {}
|
||||
except urllib.error.HTTPError as exc:
|
||||
raw = exc.read().decode("utf-8")
|
||||
return exc.code, json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def require(status: int, body: dict, expected: int, action: str) -> None:
|
||||
assert status == expected, f"{action}: expected HTTP {expected}, got {status}: {body}"
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
args = parser.parse_args()
|
||||
|
||||
suffix = uuid.uuid4().hex[:12]
|
||||
|
||||
status, user = request(
|
||||
args.base_url,
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
{
|
||||
"username": f"log_{suffix}",
|
||||
"display_name": "Log safety test",
|
||||
},
|
||||
)
|
||||
require(status, user, 201, "create user")
|
||||
|
||||
status, chat = request(
|
||||
args.base_url,
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
{
|
||||
"title": "Log safety test",
|
||||
"member_ids": [user["id"]],
|
||||
},
|
||||
user["id"],
|
||||
)
|
||||
require(status, chat, 201, "create chat")
|
||||
|
||||
status, message = request(
|
||||
args.base_url,
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
{"text": CONTROL_TEXT},
|
||||
user["id"],
|
||||
)
|
||||
require(status, message, 201, "create control message")
|
||||
|
||||
print("PASS: control message accepted with HTTP 201")
|
||||
print(f"CONTROL_TEXT={CONTROL_TEXT}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except AssertionError as exc:
|
||||
print(f"FAIL: {exc}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
except Exception as exc:
|
||||
print(f"FAIL: unexpected error: {type(exc).__name__}: {exc}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user