forked from mtusi2026/mtusi2026lab3
60 lines
2.3 KiB
Python
Executable File
60 lines
2.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Black-box checks for the grade 4 registration, transaction and auth cases."""
|
|
import base64
|
|
import concurrent.futures
|
|
import sys
|
|
import threading
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
|
from client import Client, arguments, check, run # noqa: E402
|
|
|
|
|
|
def basic(username, password):
|
|
raw = (username + ":" + password).encode("utf-8")
|
|
return {"Authorization": "Basic " + base64.b64encode(raw).decode("ascii")}
|
|
|
|
|
|
def main():
|
|
args = arguments("Grade 4 API checks")
|
|
base = Client(args.base_url, args.ca_file)
|
|
suffix = uuid.uuid4().hex[:12]
|
|
username = "race_" + suffix
|
|
password = "Race-password-" + uuid.uuid4().hex
|
|
registration = {"username": username, "display_name": "Гонка регистрации", "password": password}
|
|
|
|
workers = 4
|
|
barrier = threading.Barrier(workers)
|
|
|
|
def register(_):
|
|
client = Client(args.base_url, args.ca_file)
|
|
barrier.wait()
|
|
return client.call("POST", "/api/v1/users", [201, 409], body=registration)["status"]
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as pool:
|
|
statuses = list(pool.map(register, range(workers)))
|
|
check(statuses.count(201) == 1 and statuses.count(409) == workers - 1,
|
|
"concurrent registration must create exactly one user")
|
|
|
|
base.call("POST", "/api/v1/auth/sessions", 401,
|
|
headers=basic(username, "definitely-wrong-password"))
|
|
session = base.call("POST", "/api/v1/auth/sessions", 201,
|
|
headers=basic(username, password), schema="Session")["json"]
|
|
actor = {"id": session["user"]["id"], "token": session["token"]}
|
|
|
|
title = "rollback-" + suffix
|
|
base.call("POST", "/api/v1/chats", 400, actor=actor,
|
|
body={"title": title, "member_ids": [actor["id"], str(uuid.uuid4())]})
|
|
page = base.call("GET", "/api/v1/chats?limit=100", actor=actor, schema="ChatPage")["json"]
|
|
check(all(chat["title"] != title for chat in page["items"]),
|
|
"failed composite operation left a visible chat")
|
|
|
|
base.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=actor)
|
|
base.call("GET", "/api/v1/users/me", 401, actor=actor)
|
|
print("PASS: registration race, transaction rollback, bad password and logout")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|