"""Run with .venv/bin/python against the real Compose stack.""" import hashlib import io import struct import sys import uuid import zlib from pathlib import Path from urllib.parse import quote from PIL import Image sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, arguments args = arguments('Attachment limits, formats, ACL and message retries') c = Client(args.base_url, args.ca_file) a, b, eve, chat = c.bootstrap() path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' png = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes() def upload(data, status=201, mime='image/png', filename='photo.png', actor=a): return c.call('POST', path + quote(filename, safe=''), status, actor=actor, raw=data, headers={'Content-Type': mime}, schema='Attachment' if status == 201 else None) att = upload(png, filename='Фото.png')['json'] for actor in (a, b): result = c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=actor) assert result['bytes'] == png and result['headers'].get_content_type() == 'image/png' assert hashlib.sha256(result['bytes']).hexdigest() == att['sha256'] for suffix in ('', '/content'): c.call('GET', '/api/v1/attachments/' + att['id'] + suffix, 404, actor=eve) c.call('GET', '/api/v1/attachments/' + att['id'] + suffix, 401) upload(png, 404, actor=eve) for filename in ('../a.png', 'a/b.png', 'a\\b.png', 'a\x00.png', 'a\n.png', 'a\x7f.png', '', 'a' * 256): upload(png, 400, filename=filename) upload(png, 415, mime='image/jpeg') upload(b'not an image', 415) upload(png[:50], 415) upload(b'GIF89a' + b'\0' * 100, 415) upload(png, 415, mime='application/octet-stream') # Header-only request exercises rejection before reading an oversized body. c.call('POST', path + 'huge.png', 413, actor=a, raw=b'', headers={'Content-Type': 'image/png', 'Content-Length': str(10485761)}) def chunk(name, data): return struct.pack('>I', len(data)) + name + data + struct.pack('>I', zlib.crc32(name + data)) huge = b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', 8000, 5001, 8, 2, 0, 0, 0)) + chunk(b'IDAT', b'') + chunk(b'IEND', b'') upload(huge, 413) # Both valid formats, byte and pixel boundaries; bounded compressed fixtures. for size, fmt, mime in (((40, 20), 'JPEG', 'image/jpeg'), ((8000, 5000), 'PNG', 'image/png')): output = io.BytesIO() Image.new('RGB', size).save(output, format=fmt) data = output.getvalue() item = upload(data, mime=mime)['json'] assert c.call('GET', '/api/v1/attachments/' + item['id'] + '/content', actor=b)['bytes'] == data padded = png + bytes(10485760 - len(png)) assert upload(padded)['json']['size_bytes'] == 10485760 message = c.message(a, chat, '', key='attachment-retry', attachments=[att['id']])['json'] assert c.message(a, chat, '', key='attachment-retry', attachments=[att['id']])['json'] == message assert message['attachment_ids'] == [att['id']] c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=a, body={'text': 'changed', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': 'attachment-retry'}) other = c.call('POST', '/api/v1/chats', 201, actor=a, body={'title': 'other', 'member_ids': [a['id']]}, schema='Chat')['json'] for ids, status in (([att['id']], 404), ([str(uuid.uuid4())], 404), ([att['id']] * 2, 400), (['bad'], 400)): c.call('POST', '/api/v1/chats/' + other['id'] + '/messages', status, actor=a, body={'text': '', 'attachment_ids': ids}, headers={'Idempotency-Key': uuid.uuid4().hex}) c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 404, actor=a) print('PASS: PNG/JPEG byte equality, ACL, unsafe filenames, MIME/corruption, 10 MiB and 40M pixel boundaries, attachment-only messages and retries')