Files

52 lines
2.9 KiB
Python

"""Actual variant geometry, orientation, alpha, encoder metadata and hashes."""
import hashlib
import io
import json
from pathlib import Path
import sys
from PIL import Image
sys.path.insert(0,str(Path(__file__).resolve().parent))
from client import Client,check
def main():
c=Client('http://lb:8080');alice,bob,eve,chat=c.bootstrap();rows=[]
cases=[]
cases.append(('small_alpha',Image.new('RGBA',(80,40),(0,0,0,0)),'PNG',{},(80,40)))
cases.append(('exif_orientation',Image.new('RGB',(400,100),'red'),'JPEG',{'exif':Image.Exif()},(64,256)))
cases[-1][3]['exif'][274]=6
center=Image.new('RGB',(400,100),'red');center.paste('blue',(150,0,250,100))
cases.append(('center_crop',center,'PNG',{},(256,64)))
cases.append(('large_fit',Image.new('RGB',(1600,800),'green'),'PNG',{},(256,128)))
for name,image,fmt,options,expected in cases:
data=io.BytesIO();image.save(data,fmt,**options);payload=data.getvalue()
mime='image/png' if fmt=='PNG' else 'image/jpeg'
att=c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename='+name,202,actor=alice,
raw=payload,headers={'Content-Type':mime},schema='Attachment')['json']
ready=c.wait_ready(alice,att)
check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)['bytes']==payload,'original changed')
dimensions={}
for variant in ready['variants']:
result=c.call('GET','/api/v1/attachments/'+att['id']+'/content?variant='+variant['name'],actor=bob)
check(hashlib.sha256(result['bytes']).hexdigest()==variant['sha256'],'false checksum')
check(len(result['bytes'])==variant['size_bytes'],'false variant size')
with Image.open(io.BytesIO(result['bytes'])) as output:
check(not output.getexif(),'EXIF leaked')
check(output.size==(variant['width'],variant['height']),'false dimensions')
dimensions[variant['name']]=output.size
check(output.format==('WEBP' if variant['name']=='optimized' else 'JPEG'),'wrong encoder')
if variant['name']=='thumbnail':
check(output.size==expected,'thumbnail orientation/scale incorrect')
if name=='small_alpha':check(min(output.getpixel((10,10)))>245,'alpha not composited on white')
if variant['name']=='crop':
check(output.size==(128,128),'crop dimensions wrong')
if name=='center_crop':check(output.getpixel((64,64))[2]>240,'crop not centered')
if variant['name']=='optimized':
check(max(output.size)<=1280,'optimized too large')
if name=='large_fit':check(output.size==(1280,640),'optimized ratio wrong')
rows.append(dict(case=name,status='PASS',dimensions=dimensions,original_sha256=hashlib.sha256(payload).hexdigest()))
print(json.dumps({'status':'PASS','rows':rows},indent=2))
if __name__=='__main__':main()