forked from mtusi2026/mtusi2026lab5
chore(migrate) moved codebase from lab4
This commit is contained in:
Executable
+168
@@ -0,0 +1,168 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Grade-4 negative checks for the attachment path.
|
||||
|
||||
The public smoke covers the happy path. This test adds the failure cases the
|
||||
assignment requires: an empty body, an oversized body, a corrupted image, a
|
||||
MIME spoof, a foreign attachment id and an attachment that belongs to another
|
||||
chat. Every client mistake must remain a 4xx response with the contract error
|
||||
envelope, not a 500.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client, check # noqa: E402
|
||||
|
||||
FIXTURE = ROOT / "tests" / "fixtures" / "sample.png"
|
||||
MAX_UPLOAD_BYTES = 10 * 1024 * 1024
|
||||
|
||||
|
||||
def upload(client, actor, chat_id, raw, filename, content_type):
|
||||
headers = {} if content_type is None else {"Content-Type": content_type}
|
||||
return client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat_id}/attachments?filename={filename}",
|
||||
expected=(201, 400, 404, 413, 415),
|
||||
actor=actor,
|
||||
raw=raw,
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--ca-file", default=None)
|
||||
args = parser.parse_args()
|
||||
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
png = FIXTURE.read_bytes()
|
||||
|
||||
# Happy path first, so the negative checks can refer to a real attachment.
|
||||
good = c.attachment(alice, chat)[0]
|
||||
check(good["status"] == "ready", "valid upload is not ready")
|
||||
check(good["error_code"] is None, "ready attachment carries an error")
|
||||
|
||||
# 1. One byte over the contract limit must be a 413, decided before decode.
|
||||
oversized = b"\x00" * (MAX_UPLOAD_BYTES + 1)
|
||||
response = upload(c, alice, chat["id"], oversized, "big.png", "image/png")
|
||||
check(response["status"] == 413, "oversized upload did not return 413")
|
||||
|
||||
# 2. A file with a valid PNG signature but a broken body must be a 415.
|
||||
corrupt = png[:8] + b"\x00" * 64
|
||||
response = upload(c, alice, chat["id"], corrupt, "broken.png", "image/png")
|
||||
check(response["status"] == 415, "corrupted image did not return 415")
|
||||
|
||||
# 3. The declared type disagrees with the actual PNG bytes: MIME spoof.
|
||||
response = upload(c, alice, chat["id"], png, "spoof.jpg", "image/jpeg")
|
||||
check(response["status"] == 415, "MIME spoof did not return 415")
|
||||
|
||||
# 4. A non-image content type is rejected without touching the decoder.
|
||||
response = upload(c, alice, chat["id"], b"hello", "file.txt", "text/plain")
|
||||
check(response["status"] == 415, "non-image Content-Type did not return 415")
|
||||
|
||||
# 4b. An empty body is rejected before validation and leaves no metadata.
|
||||
response = upload(c, alice, chat["id"], b"", "empty.png", "image/png")
|
||||
check(response["status"] == 415, "empty upload did not return 415")
|
||||
|
||||
# 5. Path separators in the display name are rejected before I/O.
|
||||
for bad_name in ("../escape.png", "dir/photo.png", "back\\slash.png"):
|
||||
response = upload(c, alice, chat["id"], png, bad_name, "image/png")
|
||||
check(response["status"] == 400, f"filename {bad_name!r} did not return 400")
|
||||
|
||||
# 6. Eve is not a chat member: both metadata and bytes are hidden as 404.
|
||||
for suffix in ("", "/content"):
|
||||
response = c.call(
|
||||
"GET", f"/api/v1/attachments/{good['id']}{suffix}", expected=404, actor=eve
|
||||
)
|
||||
check(
|
||||
response["json"]["error"]["code"] == "not_found",
|
||||
"foreign attachment error code mismatch",
|
||||
)
|
||||
|
||||
# 7. An attachment of another chat cannot be linked to this chat.
|
||||
other = c.call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
actor=alice,
|
||||
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]},
|
||||
schema="Chat",
|
||||
)["json"]
|
||||
other_attachment = c.attachment(alice, other)[0]
|
||||
c.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
404,
|
||||
actor=alice,
|
||||
body={"text": "wrong chat", "attachment_ids": [other_attachment["id"]]},
|
||||
headers={"Idempotency-Key": "wrong-chat-" + uuid.uuid4().hex},
|
||||
)
|
||||
c.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{other['id']}/messages",
|
||||
404,
|
||||
actor=alice,
|
||||
body={"text": "wrong chat", "attachment_ids": [good["id"]]},
|
||||
headers={"Idempotency-Key": "wrong-chat-" + uuid.uuid4().hex},
|
||||
)
|
||||
|
||||
# 8. Unknown ids, duplicates and an oversized list are cheap 400/404s.
|
||||
c.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
404,
|
||||
actor=alice,
|
||||
body={"text": "unknown", "attachment_ids": [str(uuid.uuid4())]},
|
||||
headers={"Idempotency-Key": "unknown-" + uuid.uuid4().hex},
|
||||
)
|
||||
c.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
400,
|
||||
actor=alice,
|
||||
body={"text": "duplicate", "attachment_ids": [good["id"], good["id"]]},
|
||||
headers={"Idempotency-Key": "duplicate-" + uuid.uuid4().hex},
|
||||
)
|
||||
c.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
400,
|
||||
actor=alice,
|
||||
body={"text": "many", "attachment_ids": [str(uuid.uuid4())] * 11},
|
||||
headers={"Idempotency-Key": "many-" + uuid.uuid4().hex},
|
||||
)
|
||||
|
||||
# 9. Uploading into a chat the caller does not belong to is a 404, and the
|
||||
# response body must not become a 500 even though authorization wins
|
||||
# before the body is read.
|
||||
response = upload(c, eve, chat["id"], png, "foreign.png", "image/png")
|
||||
check(response["status"] == 404, "foreign-chat upload did not return 404")
|
||||
|
||||
# 10. A valid image attached to a message is visible to the other member
|
||||
# with the exact original checksum, independently of the uploader.
|
||||
message = c.message(alice, chat, "", attachments=[good["id"]])["json"]
|
||||
check(message["attachment_ids"] == [good["id"]], "message lost attachment id")
|
||||
download = c.call(
|
||||
"GET", f"/api/v1/attachments/{good['id']}/content", actor=bob
|
||||
)
|
||||
check(download["bytes"] == png, "member download differs from original")
|
||||
check(
|
||||
hashlib.sha256(download["bytes"]).hexdigest() == good["sha256"],
|
||||
"member download checksum mismatch",
|
||||
)
|
||||
|
||||
print(
|
||||
"PASS: graded negative cases (413/415/404/400), no 5xx, "
|
||||
"cross-member download checksum matches"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user