#!/usr/bin/env python3 """Attachment contract checks beyond the public smoke; creates synthetic data.""" import hashlib import sys import uuid from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, check, run ROOT = Path(__file__).resolve().parents[1] PNG = (ROOT / 'tests/fixtures/sample.png').read_bytes() def upload(c, actor, chat, content=PNG, filename='sample.png', content_type='image/png', expected=201): path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename return c.call('POST', path, expected, actor=actor, raw=content, headers={'Content-Type': content_type}, schema='Attachment') def main(): c = Client() alice, bob, eve, chat = c.bootstrap() other = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Другой чат', 'member_ids': [alice['id'], bob['id']]}, schema='Chat')['json'] notes = [] att = upload(c, alice, chat)['json'] notes.append(f"upload 201 ready, sha256={att['sha256']}, size={att['size_bytes']}") check(att['sha256'] == hashlib.sha256(PNG).hexdigest(), 'sha256 mismatch') check(att['status'] == 'ready' and att['variants'] == [] and att['error_code'] is None, 'attachment shape') jpeg_path = ROOT / 'scripts/fixtures/sample.jpg' if jpeg_path.is_file(): jpeg = jpeg_path.read_bytes() jatt = upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/jpeg')['json'] check(jatt['content_type'] == 'image/jpeg' and jatt['sha256'] == hashlib.sha256(jpeg).hexdigest(), 'jpeg metadata mismatch') got = c.call('GET', '/api/v1/attachments/' + jatt['id'] + '/content', 200, actor=bob) check(got['bytes'] == jpeg and got['headers'].get_content_type() == 'image/jpeg', 'jpeg download mismatch') upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/png', expected=415) notes.append('jpeg upload/download byte-identical; jpeg declared as png -> 415') large_path = ROOT / 'scripts/fixtures/large.png' if large_path.is_file(): large = large_path.read_bytes() latt = upload(c, alice, chat, content=large, filename='large.png')['json'] check(latt['size_bytes'] == len(large) and latt['status'] == 'ready', 'image at the pixel limit was rejected') got = c.call('GET', '/api/v1/attachments/' + latt['id'] + '/content', 200, actor=bob) check(got['bytes'] == large, 'large image download mismatch') notes.append('png with 40,000,000 pixels accepted and downloaded byte-identical') upload(c, alice, chat, filename='bad/name.png', expected=400) notes.append('filename with slash -> 400') upload(c, alice, chat, filename='bad%00name.png', expected=400) notes.append('filename with NUL -> 400') upload(c, alice, chat, filename='', expected=400) notes.append('empty filename -> 400') upload(c, alice, chat, content_type='image/jpeg', expected=415) notes.append('declared jpeg with png body -> 415') upload(c, alice, chat, content=PNG[:-30], expected=415) notes.append('truncated png -> 415') upload(c, alice, chat, content=b'\x00' * (10485761), expected=413) notes.append('10485761 bytes -> 413') upload(c, alice, chat, content_type='text/plain', expected=415) notes.append('text/plain -> 415') c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 400, actor=bob) notes.append('unknown variant -> 400') c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=original', 200, actor=bob) notes.append('variant=original -> 200') c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), 404, actor=bob) notes.append('unknown attachment id -> 404') c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=x.png', 404, actor=eve, raw=PNG, headers={'Content-Type': 'image/png'}, schema='Attachment') notes.append('upload by outsider -> 404') foreign = upload(c, alice, other)['json'] key = uuid.uuid4().hex c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice, body={'text': '', 'attachment_ids': [foreign['id']]}, headers={'Idempotency-Key': key}) notes.append('attachment from another chat -> 404') c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice, body={'text': '', 'attachment_ids': [str(uuid.uuid4())]}, headers={'Idempotency-Key': uuid.uuid4().hex}) notes.append('unknown attachment id in message -> 404') c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice, body={'text': '', 'attachment_ids': [att['id']] * 2}, headers={'Idempotency-Key': uuid.uuid4().hex}) notes.append('duplicate attachment_ids -> 400') c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice, body={'text': '', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]}, headers={'Idempotency-Key': uuid.uuid4().hex}) notes.append('eleven attachment_ids -> 400') c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice, body={'text': ''}, headers={'Idempotency-Key': uuid.uuid4().hex}) notes.append('empty text without attachments -> 400') key = uuid.uuid4().hex first = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice, body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json'] replay = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice, body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json'] check(first == replay, 'idempotent replay with attachments differs') check(first['attachment_ids'] == [att['id']], 'message lost attachment_ids') notes.append('idempotent replay with same attachment set -> same message') empty_key = uuid.uuid4().hex without = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice, body={'text': 'без вложений'}, headers={'Idempotency-Key': empty_key}, schema='Message')['json'] implicit = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice, body={'text': 'без вложений', 'attachment_ids': []}, headers={'Idempotency-Key': empty_key}, schema='Message')['json'] check(without == implicit, 'absent attachment_ids must equal an empty list') notes.append('absent attachment_ids equals [] for idempotency') c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=alice, body={'text': 'вложение', 'attachment_ids': []}, headers={'Idempotency-Key': key}) notes.append('same key with a different attachment set -> 409') history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', 200, actor=bob, schema='MessagePage')['json']['items'] check(all(isinstance(m['attachment_ids'], list) for m in history), 'attachment_ids must always be an array') check(any(m['id'] == first['id'] and m['attachment_ids'] == [att['id']] for m in history), 'history lost attachments') notes.append('history exposes attachment_ids on every message') for line in notes: print('ok:', line) print(f'PASS: {len(notes)} attachment contract checks') if __name__ == '__main__': run(main)