"""Black-box grade-4 checks; creates only synthetic data on the local lab.""" from concurrent.futures import ThreadPoolExecutor import hashlib import io import json from pathlib import Path import sys import threading import uuid from PIL import Image from urllib.parse import quote sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, check c = Client(timeout=45) a,b,e,chat = c.bootstrap() base = '/api/v1/chats/' + chat['id'] att, png = c.attachment(a, chat) # Exercise the second supported format through both stores, not only Pillow. jpeg_file = io.BytesIO() Image.new('RGB', (320, 200), (20, 30, 40)).save(jpeg_file, 'JPEG') jpeg = jpeg_file.getvalue() jpeg_att = c.call('POST', base+'/attachments?filename=photo.jpg', 201, actor=a, raw=jpeg, headers={'Content-Type': 'image/jpeg'}, schema='Attachment')['json'] check(jpeg_att['sha256'] == hashlib.sha256(jpeg).hexdigest(), 'JPEG checksum differs') check(jpeg_att['size_bytes'] == len(jpeg), 'JPEG length differs') jpeg_download = c.call('GET', '/api/v1/attachments/'+jpeg_att['id']+'/content', actor=b) check(jpeg_download['bytes'] == jpeg, 'JPEG original differs') check(jpeg_download['headers'].get_content_type() == 'image/jpeg', 'JPEG MIME differs') for name in ['../x.png', 'a\\b.png', '\x00.png', '\n.png']: c.call('POST', base+'/attachments?filename='+quote(name, safe=''), 400, actor=a, raw=png, headers={'Content-Type':'image/png'}) for data,mime,status in [(b'x'*(10485760+1),'image/png',413), (b'not an image','image/png',415), (png,'image/jpeg',415), (png[:20],'image/png',415)]: c.call('POST', base+'/attachments?filename=x.png', status, actor=a, raw=data, headers={'Content-Type':mime}) other = c.call('POST','/api/v1/chats',201,actor=a, body={'title':'Other chat','member_ids':[a['id']]},schema='Chat')['json'] for target,ids,actor in [(other,[att['id']],a), (chat,[str(uuid.uuid4())],a), (chat,[att['id']],e)]: c.call('POST','/api/v1/chats/'+target['id']+'/messages',404,actor=actor, body={'text':'','attachment_ids':ids},headers={'Idempotency-Key':uuid.uuid4().hex}) for suffix in ['', '/content']: c.call('GET','/api/v1/attachments/'+att['id']+suffix,404,actor=e) # A member other than the uploader can attach and download the same file. c.message(b, chat, '', attachments=[att['id']]) key = uuid.uuid4().hex barrier = threading.Barrier(10) def replay(_): barrier.wait() return Client(timeout=45).message(a,chat,'one logical message',key=key)['json'] with ThreadPoolExecutor(max_workers=10) as pool: messages = list(pool.map(replay,range(10))) check(all(m == messages[0] for m in messages), 'concurrent replay duplicated messages') c.call('POST',base+'/messages',409,actor=a,body={'text':'different'},headers={'Idempotency-Key':key}) # Default [] and explicit [] are equivalent. c.call('POST',base+'/messages',201,actor=a,body={'text':'one logical message','attachment_ids':[]}, headers={'Idempotency-Key':key},schema='Message') print('PASS: JPEG round-trip, invalid files, ACL, cross-chat references, concurrent idempotency')