yurtaev - lab4
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Acceptance checks for the level-5 presigned upload track."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def presign(client, actor, chat, content, digest=None):
|
||||
response = client.call(
|
||||
"POST", "/api/v1/chats/" + chat["id"] + "/attachments/presign", 201,
|
||||
actor=actor,
|
||||
body={
|
||||
"filename": "direct.png",
|
||||
"content_type": "image/png",
|
||||
"size_bytes": len(content),
|
||||
"sha256": digest or hashlib.sha256(content).hexdigest(),
|
||||
},
|
||||
)
|
||||
return json.loads(response["bytes"])
|
||||
|
||||
|
||||
def put(ticket, content):
|
||||
request = urllib.request.Request(
|
||||
ticket["upload_url"], data=content, method="PUT",
|
||||
headers=ticket["required_headers"],
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=15) as response:
|
||||
check(response.status == 200, "presigned PUT failed")
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Lab4 direct upload track")
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, _, chat = client.bootstrap()
|
||||
content = (Path(__file__).parent / "fixtures/sample.png").read_bytes()
|
||||
|
||||
unfinished = presign(client, alice, chat, content)
|
||||
client.call(
|
||||
"POST", "/api/v1/attachments/" + unfinished["attachment_id"] + "/complete", 409,
|
||||
actor=alice, body={},
|
||||
)
|
||||
client.call(
|
||||
"POST", "/api/v1/attachments/" + unfinished["attachment_id"] + "/complete", 404,
|
||||
actor=bob, body={},
|
||||
)
|
||||
|
||||
wrong = presign(client, alice, chat, content, "0" * 64)
|
||||
put(wrong, content)
|
||||
client.call(
|
||||
"POST", "/api/v1/attachments/" + wrong["attachment_id"] + "/complete", 409,
|
||||
actor=alice, body={},
|
||||
)
|
||||
|
||||
ticket = presign(client, alice, chat, content)
|
||||
put(ticket, content)
|
||||
ready = client.call(
|
||||
"POST", "/api/v1/attachments/" + ticket["attachment_id"] + "/complete", 200,
|
||||
actor=alice, body={}, schema="Attachment",
|
||||
)["json"]
|
||||
check(ready["sha256"] == hashlib.sha256(content).hexdigest(), "checksum changed")
|
||||
downloaded = client.call(
|
||||
"GET", "/api/v1/attachments/" + ready["id"] + "/content", actor=bob,
|
||||
)["bytes"]
|
||||
check(downloaded == content, "direct-uploaded object changed")
|
||||
|
||||
parts = urlsplit(ticket["upload_url"])
|
||||
private_url = urlunsplit((parts.scheme, parts.netloc, parts.path, "", ""))
|
||||
try:
|
||||
urllib.request.urlopen(private_url, timeout=5)
|
||||
except urllib.error.HTTPError as error:
|
||||
check(error.code in (401, 403), "private bucket returned unexpected status")
|
||||
else:
|
||||
check(False, "private bucket allowed anonymous GET")
|
||||
print("PASS: presign, ownership, checksum, completion and private bucket")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user