Files
mtusi2026lab5/student-tests/variants.py
T

37 lines
1.6 KiB
Python

"""Check all level-4 image variants and their metadata."""
import hashlib
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parents[1] / 'tests'))
from client import Client, check
def main():
client = Client('http://localhost:8080')
alice, bob, _, chat = client.bootstrap()
data = (Path(__file__).parents[1] / 'tests/fixtures/sample.png').read_bytes()
attachment = client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=variants.png',
202, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment',
)['json']
ready = client.wait_ready(bob, attachment)
variants = {item['name']: item for item in ready['variants']}
check(set(variants) == {'thumbnail', 'crop', 'optimized'}, 'not all variants are ready')
check((variants['thumbnail']['width'], variants['thumbnail']['height']) == (256, 160), 'thumbnail size')
check((variants['crop']['width'], variants['crop']['height']) == (128, 128), 'crop size')
check(variants['optimized']['content_type'] == 'image/webp', 'optimized format')
for name, meta in variants.items():
response = client.call(
'GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=' + name,
200, actor=bob,
)
check(response['headers'].get_content_type() == meta['content_type'], name + ' content type')
check(len(response['bytes']) == meta['size_bytes'], name + ' size')
check(hashlib.sha256(response['bytes']).hexdigest() == meta['sha256'], name + ' checksum')
print('PASS: thumbnail, crop and optimized variants')
if __name__ == '__main__':
main()