Files
mtusi2026lab5/student-tests/jpeg_original.py
T

32 lines
1.2 KiB
Python

"""A valid JPEG must survive upload and download without any byte changes."""
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()
alice, bob, _, chat = client.bootstrap()
original = Path(__file__).with_name('sample.jpg').read_bytes()
attachment = client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.jpg',
202, actor=alice, raw=original, headers={'Content-Type': 'image/jpeg'},
schema='Attachment',
)['json']
check(attachment['status'] == 'queued', 'JPEG upload must start queued')
ready = client.wait_ready(bob, attachment)
result = client.call(
'GET', '/api/v1/attachments/' + ready['id'] + '/content', 200, actor=bob,
)
check(result['headers'].get_content_type() == 'image/jpeg', 'JPEG content type changed')
check(result['bytes'] == original, 'JPEG original bytes changed')
check(ready['sha256'] == hashlib.sha256(original).hexdigest(), 'JPEG checksum mismatch')
print('PASS: JPEG upload 202 -> ready -> original 200, bytes and SHA-256 match')
if __name__ == '__main__':
main()