Files
mtusi2026lab5/student-tests/lab5_grade4.py
T
2026-11-20 22:48:28 +03:00

59 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Student grade-4 checks for the asynchronous attachment pipeline."""
import hashlib
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, arguments, check, run
from image_probe import jpeg_size
def main():
args = arguments('Lab 5 grade-4 attachment pipeline')
client = Client(args.base_url, args.ca_file, timeout=10)
alice, bob, _, chat = client.bootstrap()
attachment, original = client.attachment(alice, chat)
check(attachment['status'] == 'queued', 'upload must be acknowledged as queued')
check(attachment['variants'] == [], 'queued attachment must not expose ready variants')
linked = client.message(alice, chat, '', attachments=[attachment['id']])['json']
check(linked['attachment_ids'] == [attachment['id']], 'queued attachment must be linkable')
ready = client.wait_ready(bob, attachment)
check(ready['status'] == 'ready' and ready['error_code'] is None, 'attachment did not become ready')
variants = {item['name']: item for item in ready['variants']}
check(set(variants) == {'thumbnail', 'crop', 'optimized'}, 'grade-4 variants are incomplete')
check((variants['thumbnail']['width'], variants['thumbnail']['height']) == (256, 160), 'thumbnail dimensions')
check((variants['crop']['width'], variants['crop']['height']) == (128, 128), 'crop dimensions')
check(variants['optimized']['width'] <= 1280 and variants['optimized']['height'] <= 1280, 'optimized dimensions')
original_response = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob)
check(original_response['bytes'] == original, 'original object was changed')
check(hashlib.sha256(original_response['bytes']).hexdigest() == attachment['sha256'], 'original checksum')
thumbnail = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=thumbnail', actor=bob)
check(thumbnail['headers'].get_content_type() == 'image/jpeg', 'thumbnail MIME')
check(jpeg_size(thumbnail['bytes']) == (256, 160), 'thumbnail bytes dimensions')
check(hashlib.sha256(thumbnail['bytes']).hexdigest() == variants['thumbnail']['sha256'], 'thumbnail checksum')
crop = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=crop', actor=bob)
check(crop['headers'].get_content_type() == 'image/jpeg', 'crop MIME')
check(jpeg_size(crop['bytes']) == (128, 128), 'crop bytes dimensions')
check(hashlib.sha256(crop['bytes']).hexdigest() == variants['crop']['sha256'], 'crop checksum')
optimized = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=optimized', actor=bob)
check(optimized['headers'].get_content_type() == 'image/webp', 'optimized MIME')
check(optimized['bytes'][:4] == b'RIFF' and optimized['bytes'][8:12] == b'WEBP', 'optimized is WebP')
check(hashlib.sha256(optimized['bytes']).hexdigest() == variants['optimized']['sha256'], 'optimized checksum')
# A second metadata read proves that the idempotent worker result is stable.
stable = client.call('GET', '/api/v1/attachments/' + attachment['id'], actor=bob, schema='Attachment')['json']
check(stable['variants'] == ready['variants'], 'repeated metadata read changed variants')
print('PASS: grade-4 async attachment pipeline; queue, worker, crop, WebP and idempotent metadata verified.')
if __name__ == '__main__':
run(main)