forked from mtusi2026/mtusi2026lab4
ЛР 4: S3-вложения и исследование согласованности — Майорова БВТ2301
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import concurrent.futures
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import unittest
|
||||
from urllib.parse import quote
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client
|
||||
from images import bomb, png
|
||||
|
||||
|
||||
class Attachments(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.c = Client()
|
||||
self.alice, self.bob, self.eve, self.chat = self.c.bootstrap()
|
||||
self.path = '/api/v1/chats/' + self.chat['id']
|
||||
self.image = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes()
|
||||
|
||||
def upload(self, content=None, name='photo.png', mime='image/png', status=201, actor=None):
|
||||
return self.c.call('POST', self.path + '/attachments?filename=' + quote(name, safe=''), status, actor=actor or self.alice, raw=self.image if content is None else content, headers={'Content-Type': mime}, schema='Attachment' if status == 201 else None)
|
||||
|
||||
def test_binary_roundtrip_and_metadata(self):
|
||||
att = self.upload(name='Софья 👩💻.png')['json']
|
||||
self.assertEqual(att['filename'], 'Софья 👩💻.png')
|
||||
self.assertEqual(att['variants'], [])
|
||||
self.assertIsNone(att['error_code'])
|
||||
for _ in range(4):
|
||||
metadata = self.c.call('GET', '/api/v1/attachments/' + att['id'], actor=self.bob, schema='Attachment')['json']
|
||||
self.assertEqual(metadata, att)
|
||||
response = self.c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=original', actor=self.bob)
|
||||
self.assertEqual(response['bytes'], self.image)
|
||||
self.c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 404, actor=self.bob)
|
||||
|
||||
def test_bad_images_and_mime(self):
|
||||
for raw in (b'not image', b'<svg/>', self.image[:40], self.image[:-30]):
|
||||
self.upload(raw, status=415)
|
||||
self.upload(mime='image/jpeg', status=415)
|
||||
self.upload(mime='application/octet-stream', status=415)
|
||||
self.upload(bomb(), status=413)
|
||||
self.upload(b'x' * (10 * 1024 * 1024 + 1), status=413)
|
||||
|
||||
def test_dangerous_names_and_query(self):
|
||||
for name in ('', ' ', '../a.png', 'a/b.png', 'a\\b.png', '\0.png', 'a\n.png', 'a\x7f.png', 'a' * 256):
|
||||
self.upload(name=name, status=400)
|
||||
for query in ('', '?filename=a&filename=b', '?filename=a&extra=x'):
|
||||
self.c.call('POST', self.path + '/attachments' + query, 400, actor=self.alice, raw=self.image, headers={'Content-Type': 'image/png'})
|
||||
|
||||
def test_access_and_cross_chat(self):
|
||||
att = self.upload()['json']
|
||||
for suffix in ('', '/content'):
|
||||
self.c.call('GET', '/api/v1/attachments/' + att['id'] + suffix, 404, actor=self.eve)
|
||||
self.c.call('GET', '/api/v1/attachments/' + att['id'] + suffix, 401)
|
||||
self.upload(actor=self.eve, status=404)
|
||||
other = self.c.call('POST', '/api/v1/chats', 201, actor=self.alice, body={'title': 'other', 'member_ids': [self.alice['id']]}, schema='Chat')['json']
|
||||
self.c.call('POST', '/api/v1/chats/' + other['id'] + '/messages', 404, actor=self.alice, body={'text': '', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
self.c.call('POST', self.path + '/messages', 404, actor=self.alice, body={'text': '', 'attachment_ids': [str(uuid.uuid4())]}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
|
||||
def test_attachment_message_replay(self):
|
||||
one, two = self.upload()['json'], self.upload()['json']
|
||||
key = uuid.uuid4().hex
|
||||
def send(_):
|
||||
return Client().message(self.bob, self.chat, '', key=key, attachments=[one['id'], two['id']])['json']
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
||||
replies = list(executor.map(send, range(20)))
|
||||
self.assertTrue(all(r == replies[0] for r in replies))
|
||||
self.assertEqual(replies[0]['attachment_ids'], [one['id'], two['id']])
|
||||
self.c.call('POST', self.path + '/messages', 409, actor=self.bob, body={'text': '', 'attachment_ids': [two['id'], one['id']]}, headers={'Idempotency-Key': key})
|
||||
history = self.c.call('GET', self.path + '/messages', actor=self.alice, schema='MessagePage')['json']
|
||||
self.assertEqual(history['items'], [replies[0]])
|
||||
for ids in ([one['id']] * 2, [True], 'bad', [str(uuid.uuid4()) for _ in range(11)]):
|
||||
self.c.call('POST', self.path + '/messages', 400, actor=self.alice, body={'text': '', 'attachment_ids': ids}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
|
||||
def test_valid_pixel_boundary(self):
|
||||
# Exactly 40M decoded grayscale pixels, small compressed upload.
|
||||
content = png(10000, 4000, rgb=False)
|
||||
att = self.upload(content)['json']
|
||||
response = self.c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=self.bob)
|
||||
self.assertEqual(response['bytes'], content)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
||||
Reference in New Issue
Block a user