#!/usr/bin/env python3 """Lab4 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('Lab4 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] 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) c.call('GET','/api/v1/attachments/'+uploaded['id']+'/content?variant=thumbnail',404,actor=alice) 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: lab4 negative image, MIME, size, filename, ACL and cross-chat checks') if __name__=='__main__': run(main)