forked from mtusi2026/mtusi2026lab4
89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
import json
|
|
import os
|
|
import sys
|
|
from hashlib import sha256
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
sys.path[:0] = [str(ROOT / "scripts"), str(ROOT / "student-tests")]
|
|
import lab4_seed as seed
|
|
import pictures
|
|
import reconcile_track as track
|
|
|
|
out = Path(__file__).with_name("png-roundtrip.json")
|
|
assert not out.exists()
|
|
api = seed.Http(os.environ["BASE_URL"], 30)
|
|
alice, token = seed.login(api, "png_alice")
|
|
bob, bob_token = seed.login(api, "png_bob")
|
|
status, _, data = api.call(
|
|
"POST", "/api/v1/chats", {"title": "PNG regression", "member_ids": [alice, bob]}, token=token
|
|
)
|
|
assert status == 201
|
|
chat = json.loads(data)["id"]
|
|
before = track.oracle()
|
|
results = []
|
|
for name, body in (
|
|
("broken-idat", pictures.BROKEN_PNG),
|
|
("duplicate-ihdr", pictures.DUPLICATE_IHDR_PNG),
|
|
):
|
|
status, headers, data = api.call(
|
|
"POST",
|
|
f"/api/v1/chats/{chat}/attachments?filename={name}.png",
|
|
body,
|
|
{"Content-Type": "image/png"},
|
|
token,
|
|
)
|
|
results.append(
|
|
{
|
|
"case": name,
|
|
"bytes": len(body),
|
|
"sha256": sha256(body).hexdigest(),
|
|
"status": status,
|
|
"headers": headers,
|
|
"response": json.loads(data),
|
|
}
|
|
)
|
|
after = track.oracle()
|
|
positive = []
|
|
for kind, body in (("image/png", pictures.SAMPLE), ("image/jpeg", pictures.JPEG)):
|
|
status, headers, data = api.call(
|
|
"POST",
|
|
f"/api/v1/chats/{chat}/attachments?filename=valid-image",
|
|
body,
|
|
{"Content-Type": kind},
|
|
token,
|
|
)
|
|
attachment = json.loads(data)
|
|
item = {"kind": kind, "status": status, "response": attachment}
|
|
if status == 201:
|
|
code, rh, downloaded = api.call(
|
|
"GET", f"/api/v1/attachments/{attachment['id']}/content", token=bob_token
|
|
)
|
|
item.update(
|
|
download_status=code,
|
|
content_type=rh.get("content-type"),
|
|
bytes_match=downloaded == body,
|
|
)
|
|
positive.append(item)
|
|
report = {
|
|
"commit": track.head(),
|
|
"negative": results,
|
|
"before": {k: before[k] for k in ("objects", "rows", "counts")},
|
|
"after": {k: after[k] for k in ("objects", "rows", "counts")},
|
|
"positive": positive,
|
|
}
|
|
report["ok"] = (
|
|
report["before"] == report["after"]
|
|
and all(
|
|
r["status"] == 415 and r["response"]["error"]["code"] == "unsupported_media_type"
|
|
for r in results
|
|
)
|
|
and all(
|
|
r["status"] == 201 and r.get("bytes_match") and r["content_type"] == r["kind"]
|
|
for r in positive
|
|
)
|
|
)
|
|
out.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
|
print(json.dumps(report, ensure_ascii=False, indent=2))
|
|
sys.exit(0 if report["ok"] else 1)
|