Files
mtusi2026lab6/student-tests/trace_error_setup.py
T

61 lines
1.8 KiB
Python
Executable File

import sys
import base64
import uuid
import requests
BASE = 'http://localhost:8080'
PNG = base64.b64decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==')
def fail(msg, r=None):
print(f'ERROR: {msg}', file=sys.stderr)
if r is not None:
print(f' status={r.status_code} body={r.text[:200]}', file=sys.stderr)
sys.exit(1)
uname = f'err{uuid.uuid4().hex[:6]}'
print(f'username: {uname}')
r = requests.post(f'{BASE}/api/v1/users', json={
'username': uname,
'display_name': 'ErrorTest',
'password': 'correcthorsebatterystaple',
}, timeout=10)
if r.status_code not in (200, 201):
fail('create user', r)
user_id = r.json()['id']
print(f'user_id: {user_id}')
r = requests.post(f'{BASE}/api/v1/auth/sessions',
auth=(uname, 'correcthorsebatterystaple'), timeout=10)
if r.status_code not in (200, 201):
fail('login', r)
token = r.json()['token']
print(f'token: {token[:20]}...')
r = requests.post(f'{BASE}/api/v1/chats',
json={'title': 'err-test', 'member_ids': [user_id]},
headers={'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'},
timeout=10)
if r.status_code not in (200, 201):
fail('create chat', r)
chat_id = r.json()['id']
print(f'chat_id: {chat_id}')
r = requests.post(
f'{BASE}/api/v1/chats/{chat_id}/attachments?filename=err.png',
headers={'Authorization': f'Bearer {token}',
'Content-Type': 'image/png'},
data=PNG, timeout=15)
if r.status_code not in (200, 201, 202):
fail('upload', r)
att = r.json()
print(f'attachment_id: {att["id"]}')
print(f'status: {att.get("status")}')
with open('evidence/attachment_id.txt', 'w') as f:
f.write(att['id'])
print('OK: id saved to evidence/attachment_id.txt')