forked from mtusi2026/mtusi2026lab5
49 lines
3.0 KiB
Python
49 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Lab5 negative API tests with isolated synthetic users (requires live stack)."""
|
|
from urllib.parse import quote
|
|
import uuid
|
|
from client import Client, arguments, run, check
|
|
from pathlib import Path
|
|
|
|
|
|
def main():
|
|
args = arguments('Lab5 negative file/ACL/security tests')
|
|
c = Client(args.base_url, args.ca_file)
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
base = '/api/v1/chats/' + chat['id'] + '/attachments?filename='
|
|
data = (Path(__file__).parent / 'fixtures/sample.png').read_bytes()
|
|
c.call('POST', base+'big.png', 413, actor=alice, raw=data+b'\x00'*(10*1024*1024+1-len(data)), headers={'Content-Type':'image/png'})
|
|
c.call('POST', base+'bad.png', 415, actor=alice, raw=b'not-an-image', headers={'Content-Type':'image/png'})
|
|
c.call('POST', base+'fake.jpg', 415, actor=alice, raw=data, headers={'Content-Type':'image/jpeg'})
|
|
c.call('POST', base+quote('../escape.png',safe=''), 400, actor=alice, raw=data, headers={'Content-Type':'image/png'})
|
|
c.call('POST', base+'x.png', 404, actor=eve, raw=data, headers={'Content-Type':'image/png'})
|
|
c.call('POST', base+'x.png', 401, raw=data, headers={'Content-Type':'image/png'})
|
|
uploaded = c.attachment(alice,chat)[0]
|
|
ready = c.wait_ready(alice, uploaded)
|
|
unknown = str(uuid.uuid4())
|
|
c.call('GET','/api/v1/attachments/'+unknown,404,actor=alice)
|
|
c.call('GET','/api/v1/attachments/'+uploaded['id'],404,actor=eve)
|
|
c.call('GET','/api/v1/attachments/'+uploaded['id']+'/content',404,actor=eve)
|
|
# A ready Lab5 attachment has a downloadable thumbnail for chat members.
|
|
# A non-member still gets 404, and an unknown variant must not resolve.
|
|
c.call('GET','/api/v1/attachments/'+uploaded['id']+'/content?variant=thumbnail',404,actor=eve)
|
|
c.call('GET','/api/v1/attachments/'+uploaded['id']+'/content?variant=nonexistent',404,actor=alice)
|
|
check(any(v['name'] == 'thumbnail' for v in ready['variants']), 'missing thumbnail variant')
|
|
thumb = c.call('GET','/api/v1/attachments/'+uploaded['id']+'/content?variant=thumbnail',200,actor=alice)
|
|
check(thumb['headers'].get_content_type() == 'image/jpeg', 'wrong thumbnail MIME')
|
|
other_chat = c.call('POST','/api/v1/chats',201,actor=alice,
|
|
body={'title':'Другой чат','member_ids':[alice['id'],bob['id']]},schema='Chat')['json']
|
|
path = '/api/v1/chats/'+other_chat['id']+'/messages'
|
|
c.call('POST',path,404,actor=alice,
|
|
body={'text':'cross-chat','attachment_ids':[uploaded['id']]},
|
|
headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
c.call('POST','/api/v1/chats/'+chat['id']+'/messages',404,actor=alice,
|
|
body={'text':'unknown','attachment_ids':[unknown]},
|
|
headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
ok = c.message(bob,chat,'',attachments=[uploaded['id']])['json']
|
|
check(ok['attachment_ids']==[uploaded['id']], 'valid attachment not linked')
|
|
print('PASS: lab5 negative image, MIME, size, filename, ACL, variants and cross-chat checks')
|
|
|
|
if __name__=='__main__':
|
|
run(main)
|