Files
mtusi2026lab4-implementation/scripts/upload-once.py
T

35 lines
1.3 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')
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 __name__ == '__main__':
run(main)