44 lines
2.0 KiB
Python
44 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise a running relay without third-party Python dependencies."""
|
|
import json
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
base = (sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:1337").rstrip("/")
|
|
|
|
|
|
def call(path, body=None, token=None, expected=200):
|
|
headers = {"Content-Type": "application/json"}
|
|
if token:
|
|
headers["Authorization"] = "Bearer " + token
|
|
request = urllib.request.Request(base + path, data=None if body is None else json.dumps(body).encode(), headers=headers)
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=15) as response:
|
|
status, result = response.status, json.load(response)
|
|
except urllib.error.HTTPError as error:
|
|
status, result = error.code, json.load(error)
|
|
assert status == expected, (path, status, result)
|
|
return result
|
|
|
|
|
|
assert call("/health")["status"] == "ok"
|
|
host = call("/rooms", {"name": "Smoke engineer", "role": "ENGINEER"}, expected=201)
|
|
code = host["snapshot"]["code"]
|
|
path = "/rooms/" + code
|
|
guest = call(path + "/join", {"name": "Smoke analyst", "role": "ANALYST"})
|
|
call(path, token="invalid", expected=401)
|
|
call(path + "/start", {}, guest["token"], expected=403)
|
|
call(path + "/start", {}, host["token"])
|
|
action = {"requestId": "smoke-host-1", "round": 1, "intent": "OBSERVE", "text": "Read the maintenance annotation."}
|
|
pending = call(path + "/actions", action, host["token"])
|
|
assert pending["round"] == 1 and pending["pressure"] == 12
|
|
assert pending["players"][0]["submitted"]
|
|
resolved = call(path + "/actions", {**action, "requestId": "smoke-guest-1"}, guest["token"])
|
|
assert resolved["round"] == 2 and resolved["pressure"] == 11
|
|
assert call(path + "/actions", action, host["token"]) == resolved
|
|
assert host["token"] not in json.dumps(resolved)
|
|
assert guest["token"] not in json.dumps(resolved)
|
|
call(path + "/actions", {**action, "requestId": "stale"}, host["token"], expected=409)
|
|
print("PASS: health, crew join, authorization, simultaneous resolution, replay protection, token privacy, stale-round rejection")
|