Add files via upload
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
from app.server import build_server
|
||||
|
||||
|
||||
def call(method: str, url: str, payload: dict | None = None, headers: dict | None = None) -> tuple[int, dict]:
|
||||
data = None
|
||||
req_headers = {"Content-Type": "application/json"}
|
||||
if headers:
|
||||
req_headers.update(headers)
|
||||
if payload is not None:
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
|
||||
request = urllib.request.Request(url, data=data, method=method, headers=req_headers)
|
||||
try:
|
||||
with urllib.request.urlopen(request) as response:
|
||||
return response.status, json.loads(response.read().decode("utf-8"))
|
||||
except urllib.error.HTTPError as exc:
|
||||
return exc.code, json.loads(exc.read().decode("utf-8"))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
server = build_server("127.0.0.1", 18765)
|
||||
thread = threading.Thread(target=server.serve_forever, daemon=True)
|
||||
thread.start()
|
||||
time.sleep(0.2)
|
||||
|
||||
base = "http://127.0.0.1:18765"
|
||||
status, payload = call(
|
||||
"POST",
|
||||
f"{base}/register",
|
||||
{"email": "host@example.com", "password": "123456", "name": "Host"},
|
||||
)
|
||||
assert status in {201, 409}, (status, payload)
|
||||
|
||||
status, payload = call(
|
||||
"POST",
|
||||
f"{base}/login",
|
||||
{"email": "host@example.com", "password": "123456"},
|
||||
)
|
||||
assert status == 200, (status, payload)
|
||||
token = payload["access_token"]
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
status, payload = call("GET", f"{base}/me", headers=headers)
|
||||
assert status == 200, (status, payload)
|
||||
|
||||
status, payload = call(
|
||||
"POST",
|
||||
f"{base}/1.0.0/games",
|
||||
{
|
||||
"GameId": "test-game-001",
|
||||
"GameStartConfiguration": {"NumberOfPlayersAsInt": 4, "IsPrivate": False},
|
||||
},
|
||||
headers=headers,
|
||||
)
|
||||
assert status in {201, 409}, (status, payload)
|
||||
|
||||
status, payload = call("GET", f"{base}/1.0.0/games?perPage=20", headers=headers)
|
||||
assert status == 200 and isinstance(payload.get("data"), list), (status, payload)
|
||||
|
||||
status, payload = call(
|
||||
"POST",
|
||||
f"{base}/1.0.0/games/test-game-001/events",
|
||||
{"events": [{"$type": "GameStarted"}]},
|
||||
headers=headers,
|
||||
)
|
||||
assert status == 200 and isinstance(payload, dict), (status, payload)
|
||||
|
||||
status, payload = call(
|
||||
"GET",
|
||||
f"{base}/1.0.0/games/test-game-001/events-and-metadata",
|
||||
headers=headers,
|
||||
)
|
||||
assert status == 200, (status, payload)
|
||||
|
||||
server.shutdown()
|
||||
print("local test passed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user