"""Lab5: asynchronous attachments, variants, ACL and message idempotency.""" import hashlib from pathlib import Path import struct import sys import unittest from urllib.parse import quote import uuid import zlib sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, ROOT from image_probe import jpeg_size def webp_size(data): """Read the actual WebP canvas from VP8/VP8L/VP8X, independently of API metadata.""" if data[:4] != b'RIFF' or data[8:12] != b'WEBP': raise AssertionError('optimized payload is not WebP') offset = 12 while offset + 8 <= len(data): kind = data[offset:offset + 4] length = int.from_bytes(data[offset + 4:offset + 8], 'little') payload = data[offset + 8:offset + 8 + length] if len(payload) != length: raise AssertionError('truncated WebP chunk') if kind == b'VP8X' and length >= 10: return (1 + int.from_bytes(payload[4:7], 'little'), 1 + int.from_bytes(payload[7:10], 'little')) if kind == b'VP8 ' and length >= 10 and payload[3:6] == b'\x9d\x01\x2a': return (int.from_bytes(payload[6:8], 'little') & 0x3fff, int.from_bytes(payload[8:10], 'little') & 0x3fff) if kind == b'VP8L' and length >= 5 and payload[0] == 0x2f: bits = int.from_bytes(payload[1:5], 'little') return (1 + (bits & 0x3fff), 1 + ((bits >> 14) & 0x3fff)) offset += 8 + length + (length & 1) raise AssertionError('WebP dimensions not found') def small_png(width=13, height=7): """A transparent RGBA fixture; no host imaging library is required.""" def chunk(kind, data): return (struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data))) scanlines = b''.join(b'\0' + b'\x14\x78\xdc\0' * width for _ in range(height)) return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', width, height, 8, 6, 0, 0, 0)) + chunk(b'IDAT', zlib.compress(scanlines)) + chunk(b'IEND', b'')) class AttachmentsTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.c = Client('http://127.0.0.1:8080' if not __import__('os').environ.get('BASE_URL') else __import__('os').environ['BASE_URL']) cls.a, cls.b, cls.eve, cls.chat = cls.c.bootstrap() cls.png = (ROOT / 'tests/fixtures/sample.png').read_bytes() cls.path = '/api/v1/chats/' + cls.chat['id'] + '/attachments' def upload(self, raw=None, mime='image/png', filename='photo.png', status=202, actor=None, headers=None): return self.c.call('POST', self.path + '?filename=' + quote(filename, safe=''), status, actor=actor or self.a, raw=self.png if raw is None else raw, headers={'Content-Type': mime, **(headers or {})}, schema='Attachment' if status == 202 else None) def ready(self, attachment): self.assertEqual((attachment['status'], attachment['variants'], attachment['error_code']), ('queued', [], None)) return self.c.wait_ready(self.b, attachment) def assert_variants(self, attachment, thumbnail=(256, 160), optimized=(320, 200)): variants = {item['name']: item for item in attachment['variants']} self.assertEqual(set(variants), {'thumbnail', 'crop', 'optimized'}) self.assertEqual(len(attachment['variants']), 3, 'duplicate variants') for name, expected_size, mime, probe in ( ('thumbnail', thumbnail, 'image/jpeg', jpeg_size), ('crop', (128, 128), 'image/jpeg', jpeg_size), ('optimized', optimized, 'image/webp', webp_size)): with self.subTest(variant=name): metadata = variants[name] result = self.c.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content?variant=' + name, actor=self.b) self.assertEqual(result['headers'].get_content_type(), mime) self.assertEqual(metadata['content_type'], mime) self.assertEqual((metadata['width'], metadata['height']), expected_size) self.assertEqual(probe(result['bytes']), expected_size) self.assertEqual(metadata['size_bytes'], len(result['bytes'])) self.assertEqual(metadata['sha256'], hashlib.sha256(result['bytes']).hexdigest()) def test_original_and_metadata_and_acl(self): queued = self.upload(filename='фото 👋.png')['json'] a = self.ready(queued) self.assertEqual(a['sha256'], hashlib.sha256(self.png).hexdigest()) self.assertEqual(a['size_bytes'], len(self.png)) self.assertEqual((a['status'], a['error_code']), ('ready', None)) self.assert_variants(a) path = '/api/v1/attachments/' + a['id'] self.assertEqual(self.c.call('GET', path, actor=self.b, schema='Attachment')['json'], a) result = self.c.call('GET', path + '/content?variant=original', actor=self.b) self.assertEqual(result['bytes'], self.png) self.assertEqual(result['headers'].get_content_type(), 'image/png') for suffix in ('', '/content'): self.c.call('GET', path + suffix, 404, actor=self.eve) self.c.call('GET', path + suffix, 401) self.c.call('GET', path + '/content?variant=unknown', 404, actor=self.a) def test_jpeg_original(self): jpeg = (ROOT / 'student-tests/fixtures/sample.jpg').read_bytes() a = self.ready(self.upload(raw=jpeg, mime='image/jpeg', filename='photo.jpg')['json']) got = self.c.call('GET', '/api/v1/attachments/' + a['id'] + '/content', actor=self.b) self.assertEqual(got['bytes'], jpeg) self.assertEqual(got['headers'].get_content_type(), 'image/jpeg') self.assert_variants(a) def test_small_transparent_image_does_not_upscale_thumbnail_or_optimized(self): data = small_png() a = self.ready(self.upload(raw=data, filename='transparent.png')['json']) self.assert_variants(a, thumbnail=(13, 7), optimized=(13, 7)) got = self.c.call('GET', '/api/v1/attachments/' + a['id'] + '/content', actor=self.a) self.assertEqual(got['bytes'], data) def test_large_optimized_image_is_bounded(self): data = small_png(1600, 1000) a = self.ready(self.upload(raw=data, filename='large.png')['json']) self.assert_variants(a, thumbnail=(256, 160), optimized=(1280, 800)) def test_exif_orientation_applies_to_variants_and_original_stays_identical(self): jpeg = (ROOT / 'student-tests/fixtures/sample.jpg').read_bytes() # One valid little-endian TIFF IFD with Orientation=6 (90 degrees CW). exif = (b'Exif\0\0II\x2a\0\x08\0\0\0\x01\0' + struct.pack('H', len(exif) + 2) + exif + jpeg[2:] a = self.ready(self.upload(raw=source, mime='image/jpeg', filename='rotated.jpg')['json']) self.assert_variants(a, thumbnail=(160, 256), optimized=(200, 320)) original = self.c.call('GET', '/api/v1/attachments/' + a['id'] + '/content', actor=self.a) self.assertEqual(original['bytes'], source) def test_png_requires_complete_iend_and_valid_crc(self): png = small_png() for missing in range(1, 13): with self.subTest(missing_final_bytes=missing): self.upload(raw=png[:-missing], status=415) with self.subTest(corrupt_iend_crc=True): self.upload(raw=png[:-1] + bytes([png[-1] ^ 1]), status=415) # A rejected upload must not prevent subsequent valid processing. self.ready(self.upload(raw=png)['json']) def test_corrupt_and_wrong_mime(self): for raw, mime in ((b'not an image', 'image/png'), (self.png[:40], 'image/png'), (self.png, 'image/jpeg'), (self.png, 'text/plain'), (b'', 'image/png')): with self.subTest(mime=mime, size=len(raw)): self.upload(raw=raw, mime=mime, status=415) def test_filename(self): for name in ('', ' ', '../x.png', 'a/b.png', 'a\\b.png', 'a\x00.png', 'a\n.png', 'a\x7f.png', 'я' * 256): with self.subTest(name=repr(name)): self.upload(filename=name, status=400) def test_byte_limit_before_body(self): # Большой Content-Length с одним байтом: сервер отклоняет до чтения/выделения памяти. self.upload(raw=b'x', status=413, headers={'Content-Length': '10485761'}) def test_pixel_limit_before_decode(self): ihdr = struct.pack('>IIBBBBB', 10000, 5000, 8, 2, 0, 0, 0) chunk = b'IHDR' + ihdr png = (b'\x89PNG\r\n\x1a\n' + struct.pack('>I', len(ihdr)) + chunk + struct.pack('>I', zlib.crc32(chunk)) + self.png[33:]) self.upload(raw=png, status=413) def test_foreign_chat_upload(self): self.upload(actor=self.eve, status=404) def test_attachment_only_and_idempotency(self): aid = self.upload()['json']['id'] key = uuid.uuid4().hex first = self.c.message(self.b, self.chat, '', key=key, attachments=[aid])['json'] self.assertEqual(first['attachment_ids'], [aid]) self.assertEqual(self.c.message(self.b, self.chat, '', key=key, attachments=[aid])['json'], first) path = '/api/v1/chats/' + self.chat['id'] + '/messages' self.c.call('POST', path, 409, actor=self.b, body={'text': 'другое', 'attachment_ids': [aid]}, headers={'Idempotency-Key': key}) key = uuid.uuid4().hex first = self.c.message(self.a, self.chat, 'text', key=key)['json'] self.assertEqual(self.c.message(self.a, self.chat, 'text', key=key, attachments=[])['json'], first) def test_unknown_foreign_and_duplicate_ids(self): aid = self.upload()['json']['id'] other = self.c.call('POST', '/api/v1/chats', 201, actor=self.a, body={'title': 'другой', 'member_ids': [self.a['id']]}, schema='Chat')['json'] for chat, ids, status in ((other, [aid], 404), (self.chat, [str(uuid.uuid4())], 404), (self.chat, [aid, aid], 400), (self.chat, [aid] * 11, 400), (self.chat, ['bad'], 400)): self.c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', status, actor=self.a, body={'text': '', 'attachment_ids': ids}, headers={'Idempotency-Key': uuid.uuid4().hex}) if __name__ == '__main__': unittest.main()