forked from mtusi2026/mtusi2026lab2
ЛР 2: завершить трек 2 на оценку 5
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Contract-based lab1 JSON edge cases; all data is synthetic.
|
||||
"""Contract-based lab2 JSON edge cases; all data is synthetic.
|
||||
|
||||
Uses raw HTTP bodies for byte encoding and field-name checks. Expectations
|
||||
come from contracts/README.md and CreateUser/CreateChat/CreateMessage schemas.
|
||||
@@ -39,14 +39,15 @@ def cases_for(client):
|
||||
body={"title": "Validation audit", "member_ids": [owner["id"]]},
|
||||
schema="Chat")["json"]
|
||||
endpoints = (
|
||||
("users", "/api/v1/users", None, "User", ("username", "display_name")),
|
||||
("users", "/api/v1/users", None, "User", ("username", "display_name", "password")),
|
||||
("chats", "/api/v1/chats", owner, "Chat", ("title", "member_ids")),
|
||||
("messages", "/api/v1/chats/" + chat["id"] + "/messages", owner, "Message", ("text",)),
|
||||
)
|
||||
|
||||
def body_for(endpoint):
|
||||
if endpoint == "users":
|
||||
return {"username": "edge_" + uuid.uuid4().hex[:16], "display_name": "Probe"}
|
||||
return {"username": "edge_" + uuid.uuid4().hex[:16], "display_name": "Probe",
|
||||
"password": "Synthetic:validation-password"}
|
||||
if endpoint == "chats":
|
||||
return {"title": "Probe", "member_ids": [owner["id"]]}
|
||||
return {"text": "CASE_PROBE"}
|
||||
@@ -152,8 +153,8 @@ def run(client, groups=None, verbose=True):
|
||||
if groups is not None and case["group"] not in groups:
|
||||
continue
|
||||
result = {key: case[key] for key in ("name", "group", "path", "expected")}
|
||||
if case["group"] in ("utf8", "observations"):
|
||||
result["request_hex"] = case["raw"].hex()
|
||||
# Request bodies may contain a registration password; do not put them
|
||||
# in evidence, even as hex. Tokens remain only in the teacher client.
|
||||
try:
|
||||
response = client.call("POST", case["path"], expected=list(range(200, 600)),
|
||||
actor=case["actor"], raw=case["raw"],
|
||||
@@ -171,7 +172,10 @@ def run(client, groups=None, verbose=True):
|
||||
elif response["status"] == 201:
|
||||
payload = json.loads(response["bytes"])
|
||||
shape(payload, case["schema"])
|
||||
check(all(payload.get(k) == v for k, v in case["echo"].items()), "valid fields changed")
|
||||
check(all(payload.get(k) == v for k, v in case["echo"].items()
|
||||
if k != "password"), "valid fields changed")
|
||||
check("password" not in payload and "password_hash" not in payload,
|
||||
"response exposed password material")
|
||||
result["status"] = "PASS"
|
||||
except (Failure, OSError, ValueError) as exc:
|
||||
result.update(status="FAIL", details=str(exc))
|
||||
@@ -182,11 +186,12 @@ def run(client, groups=None, verbose=True):
|
||||
return results
|
||||
|
||||
|
||||
def acceptance(client, group):
|
||||
results = run(client, {group}, verbose=False)
|
||||
def acceptance(client, group=None):
|
||||
results = run(client, {group} if group else None, verbose=False)
|
||||
check(bool(results), "validation group has no cases")
|
||||
failures = [x for x in results if x["status"] == "FAIL"]
|
||||
check(not failures, "; ".join(x["name"] + ": " + x["details"] for x in failures))
|
||||
return {"json_cases": len(results)}
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user