forked from mtusi2026/mtusi2026lab5
66 lines
3.2 KiB
Python
66 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Negative black-box checks for lab4; uses only synthetic users and files."""
|
|
import argparse
|
|
import sys
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
|
|
from client import Client, Failure, check, run # noqa: E402
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--base-url', default='http://localhost:8080')
|
|
parser.add_argument('--expect-s3-unavailable', action='store_true')
|
|
args = parser.parse_args()
|
|
|
|
client = Client(args.base_url, timeout=20)
|
|
alice, bob, eve, chat = client.bootstrap()
|
|
png = (ROOT / 'tests' / 'fixtures' / 'sample.png').read_bytes()
|
|
upload_path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png'
|
|
|
|
if args.expect_s3_unavailable:
|
|
response = client.call('POST', upload_path, 503, actor=alice, raw=png,
|
|
headers={'Content-Type': 'image/png'})
|
|
check(response['json']['error']['code'] == 'unavailable', 'S3 outage must return unavailable')
|
|
print('PASS: unavailable S3 returns contract JSON 503')
|
|
return
|
|
|
|
attachment, original = client.attachment(alice, chat)
|
|
client.call('GET', '/api/v1/attachments/' + attachment['id'], 404, actor=eve)
|
|
client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', 404, actor=eve)
|
|
|
|
other_chat = client.call('POST', '/api/v1/chats', 201, actor=alice,
|
|
body={'title': 'Другой чат', 'member_ids': [alice['id'], eve['id']]},
|
|
schema='Chat')['json']
|
|
client.call('POST', '/api/v1/chats/' + other_chat['id'] + '/messages', 404,
|
|
actor=alice, headers={'Idempotency-Key': uuid.uuid4().hex},
|
|
body={'text': '', 'attachment_ids': [attachment['id']]})
|
|
client.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404,
|
|
actor=alice, headers={'Idempotency-Key': uuid.uuid4().hex},
|
|
body={'text': '', 'attachment_ids': [str(uuid.uuid4())]})
|
|
client.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400,
|
|
actor=alice, headers={'Idempotency-Key': uuid.uuid4().hex},
|
|
body={'text': '', 'attachment_ids': [attachment['id'], attachment['id']]})
|
|
|
|
client.call('POST', upload_path, 415, actor=alice, raw=png,
|
|
headers={'Content-Type': 'image/jpeg'})
|
|
client.call('POST', upload_path, 415, actor=alice, raw=b'\x89PNG\r\n\x1a\ntruncated',
|
|
headers={'Content-Type': 'image/png'})
|
|
client.call('POST', upload_path, 413, actor=alice, raw=b'x' * (10_485_760 + 1),
|
|
headers={'Content-Type': 'image/png'})
|
|
client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=../escape.png',
|
|
400, actor=alice, raw=png, headers={'Content-Type': 'image/png'})
|
|
|
|
downloaded = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content',
|
|
actor=bob)
|
|
check(downloaded['bytes'] == original, 'authorized download changed bytes')
|
|
print('PASS: unknown/foreign attachment, foreign chat/user, MIME spoof, corruption, byte limit and filename checks')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|