Files
mtusi2026lab4/student-tests/attachments_negative.py
T
2026-11-11 18:55:53 +03:00

36 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Additional negative integration checks against a running lab4 stack."""
import sys
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client, arguments, run
from uuid import uuid4
def main():
args=arguments('Negative attachment checks')
c=Client(args.base_url,args.ca_file)
alice,bob,eve,chat=c.bootstrap()
prefix='/api/v1/chats/'+chat['id']+'/attachments?filename='
fixture=(Path(__file__).resolve().parents[1]/'tests/fixtures/sample.png').read_bytes()
hdr={'Content-Type':'image/png'}
c.call('POST',prefix+'../bad.png',400,actor=alice,raw=fixture,headers=hdr)
c.call('POST',prefix+'bad%5Cname.png',400,actor=alice,raw=fixture,headers=hdr)
c.call('POST',prefix+'too-large.png',413,actor=alice,raw=fixture+b'\0'*(10*1024*1024+1),headers=hdr)
c.call('POST',prefix+'corrupt.png',415,actor=alice,raw=b'not an image',headers=hdr)
c.call('POST',prefix+'spoof.png',415,actor=alice,raw=fixture,headers={'Content-Type':'image/jpeg'})
att,_=c.attachment(alice,chat)
for suffix in ('','/content'):
c.call('GET','/api/v1/attachments/'+att['id']+suffix,404,actor=eve)
other=c.call('POST','/api/v1/chats',201,actor=alice,body={
'title':'Other chat','member_ids':[alice['id'],eve['id']]},schema='Chat')['json']
c.message(alice,chat,'',attachments=[att['id']])
c.call('POST','/api/v1/chats/'+other['id']+'/messages',404,actor=alice,
body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':uuid4().hex})
c.call('POST','/api/v1/chats/'+chat['id']+'/messages',404,actor=alice,
body={'text':'','attachment_ids':[str(uuid4())]},headers={'Idempotency-Key':uuid4().hex})
print('PASS: invalid size/image/MIME/filename, outsider ACL and cross-chat bindings')
if __name__=='__main__':
run(main)