Files
mtusi2026lab4/scripts/upload-once.py
T

39 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Single binary upload for failure drills; asserts the expected status code."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, arguments, check, run
ROOT = Path(__file__).resolve().parents[1]
def main():
def configure(p):
p.add_argument('--expect', type=int, default=201)
p.add_argument('--file', default=str(ROOT / 'scripts/fixtures/sample.jpg'))
p.add_argument('--content-type', default='image/jpeg')
p.add_argument('--attach', action='store_true', help='also attach the upload to a new message')
args = arguments('Single upload used by compensation drills; creates synthetic data', configure)
source = Path(args.file)
check(source.is_file(), 'file not found: ' + str(source))
content = source.read_bytes()
c = Client()
alice, _, _, chat = c.bootstrap()
response = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + source.name,
args.expect, actor=alice, raw=content,
headers={'Content-Type': args.content_type}, schema='Attachment')
print(f'status={response["status"]}')
if response['status'] == 201:
attachment = response['json']
print(f"attachment={attachment['id']} size={attachment['size_bytes']}")
if args.attach:
message = c.message(alice, chat, '', attachments=[attachment['id']])['json']
print(f"message={message['id']} attachment_ids={message['attachment_ids']}")
if __name__ == '__main__':
run(main)