193 lines
12 KiB
Python
193 lines
12 KiB
Python
import hashlib
|
||
import http.client
|
||
import unittest
|
||
import urllib.parse
|
||
import uuid
|
||
|
||
from helpers import BASE_URL, call, new_chat, new_user
|
||
from imagegen import SAMPLE_JPG, SAMPLE_PNG, bomb, broken_crc, garbage_idat, noise_png, short_idat
|
||
|
||
MAX = 10 * 1024 * 1024
|
||
|
||
|
||
def upload(user, chat, data, content_type='image/png', filename='photo.png', token=True):
|
||
query = '' if filename is None else '?filename=' + urllib.parse.quote(filename, safe='')
|
||
return call('POST', '/api/v1/chats/%s/attachments%s' % (chat['id'], query), raw=data,
|
||
token=user['token'] if token else None, headers={'Content-Type': content_type})
|
||
|
||
|
||
def download(user, attachment_id):
|
||
target = urllib.parse.urlsplit(BASE_URL)
|
||
connection = http.client.HTTPConnection(target.hostname, target.port, timeout=30)
|
||
connection.request('GET', '/api/v1/attachments/%s/content' % attachment_id,
|
||
headers={'Authorization': 'Bearer ' + user['token']})
|
||
response = connection.getresponse()
|
||
body = response.read()
|
||
connection.close()
|
||
return response.status, response.headers, body
|
||
|
||
|
||
def send(user, chat, body, key=None):
|
||
return call('POST', '/api/v1/chats/%s/messages' % chat['id'], body, user['token'],
|
||
headers={'Idempotency-Key': key or uuid.uuid4().hex})
|
||
|
||
|
||
class UploadTest(unittest.TestCase):
|
||
def setUp(self):
|
||
self.alice, self.bob, self.eve = new_user('alice'), new_user('bob'), new_user('eve')
|
||
self.chat = new_chat(self.alice, self.bob)
|
||
|
||
def assertCode(self, result, status, code=None):
|
||
self.assertEqual(result[0], status, result[2])
|
||
if code:
|
||
self.assertEqual(result[2]['error']['code'], code)
|
||
|
||
def test_png_and_jpeg_are_stored_and_downloaded_byte_exact(self):
|
||
"""PNG и JPEG → 201 ready; второй участник скачивает те же байты с верным Content-Type"""
|
||
for data, content_type in ((SAMPLE_PNG, 'image/png'), (SAMPLE_JPG, 'image/jpeg')):
|
||
with self.subTest(content_type=content_type):
|
||
status, _, att = upload(self.alice, self.chat, data, content_type)
|
||
self.assertEqual(status, 201)
|
||
self.assertEqual((att['status'], att['variants'], att['error_code']), ('ready', [], None))
|
||
self.assertEqual((att['content_type'], att['size_bytes'], att['sha256']),
|
||
(content_type, len(data), hashlib.sha256(data).hexdigest()))
|
||
self.assertEqual((att['chat_id'], att['owner_id']), (self.chat['id'], self.alice['id']))
|
||
self.assertEqual(call('GET', '/api/v1/attachments/' + att['id'], token=self.bob['token'])[2], att)
|
||
status, headers, body = download(self.bob, att['id'])
|
||
self.assertEqual((status, headers.get_content_type(), body), (200, content_type, data))
|
||
self.assertEqual(int(headers['Content-Length']), len(data))
|
||
|
||
def test_download_through_every_replica(self):
|
||
"""Файл скачивается через любую API-реплику (байты не лежат локально у загрузившей)"""
|
||
att = upload(self.alice, self.chat, SAMPLE_PNG)[2]
|
||
replicas = set()
|
||
for _ in range(6):
|
||
status, headers, body = download(self.bob, att['id'])
|
||
self.assertEqual((status, body), (200, SAMPLE_PNG))
|
||
replicas.add(headers['X-Instance-Id'])
|
||
self.assertGreaterEqual(len(replicas), 2)
|
||
|
||
def test_filename_is_display_only_and_validated(self):
|
||
"""Unicode-имя сохраняется как есть; нет имени, пустое, /, \\, NUL, управляющий символ, >255 → 400"""
|
||
status, _, att = upload(self.alice, self.chat, SAMPLE_PNG, filename='фото отпуска 😀.png')
|
||
self.assertEqual((status, att['filename']), (201, 'фото отпуска 😀.png'))
|
||
for name in (None, '', ' ', '../etc/passwd', 'a\\b.png', 'a\x00b.png', 'bell\x07.png', 'x' * 256):
|
||
with self.subTest(name=name):
|
||
self.assertCode(upload(self.alice, self.chat, SAMPLE_PNG, filename=name), 400, 'invalid_request')
|
||
|
||
def test_wrong_declared_type(self):
|
||
"""Content-Type не image/png|jpeg (octet-stream, multipart, gif) → 415"""
|
||
for content_type in ('application/octet-stream', 'multipart/form-data; boundary=x', 'image/gif', 'text/plain'):
|
||
with self.subTest(content_type=content_type):
|
||
self.assertCode(upload(self.alice, self.chat, SAMPLE_PNG, content_type), 415, 'unsupported_media_type')
|
||
|
||
def test_mime_spoofing(self):
|
||
"""Подмена MIME: PNG как jpeg, JPEG как png, текст и GIF под видом png → 415"""
|
||
for data, content_type in ((SAMPLE_PNG, 'image/jpeg'), (SAMPLE_JPG, 'image/png'),
|
||
(b'hello, not an image', 'image/png'), (b'GIF89a' + bytes(100), 'image/png'),
|
||
(b'', 'image/png')):
|
||
with self.subTest(content_type=content_type, head=data[:6]):
|
||
self.assertCode(upload(self.alice, self.chat, data, content_type), 415, 'unsupported_media_type')
|
||
|
||
def test_corrupted_images(self):
|
||
"""Обрезанный PNG, битый CRC, мусор или нехватка пикселей в IDAT при верных CRC, обрезанный JPEG → 415"""
|
||
for data, content_type in ((SAMPLE_PNG[:5000], 'image/png'), (broken_crc(SAMPLE_PNG), 'image/png'),
|
||
(garbage_idat(SAMPLE_PNG), 'image/png'), (short_idat(SAMPLE_PNG), 'image/png'),
|
||
(SAMPLE_JPG[:3000], 'image/jpeg')):
|
||
with self.subTest(size=len(data), content_type=content_type):
|
||
self.assertCode(upload(self.alice, self.chat, data, content_type), 415, 'unsupported_media_type')
|
||
|
||
def test_too_large_file(self):
|
||
"""10 MiB + 1 байт → 413 (и по Content-Length, и chunked без длины); файл ~10 MiB принимается"""
|
||
self.assertCode(upload(self.alice, self.chat, SAMPLE_PNG + bytes(MAX + 1 - len(SAMPLE_PNG))), 413, 'payload_too_large')
|
||
target = urllib.parse.urlsplit(BASE_URL)
|
||
connection = http.client.HTTPConnection(target.hostname, target.port, timeout=60)
|
||
chunks = (SAMPLE_PNG if i == 0 else bytes(1024 * 1024) for i in range(12))
|
||
connection.request('POST', '/api/v1/chats/%s/attachments?filename=big.png' % self.chat['id'], body=chunks,
|
||
headers={'Authorization': 'Bearer ' + self.alice['token'], 'Content-Type': 'image/png'},
|
||
encode_chunked=True)
|
||
self.assertEqual(connection.getresponse().status, 413)
|
||
connection.close()
|
||
big = noise_png(MAX - 50000)
|
||
self.assertLessEqual(len(big), MAX)
|
||
status, _, att = upload(self.alice, self.chat, big)
|
||
self.assertEqual((status, att['size_bytes']), (201, len(big)))
|
||
self.assertEqual(download(self.bob, att['id'])[2], big)
|
||
|
||
def test_decompression_bomb(self):
|
||
"""PNG 10000×5000 (50 Мп) весом в десятки КБ → 413 без распаковки"""
|
||
data = bomb()
|
||
self.assertLess(len(data), 1024 * 1024)
|
||
self.assertCode(upload(self.alice, self.chat, data), 413, 'payload_too_large')
|
||
|
||
def test_access_rules(self):
|
||
"""Без токена 401; посторонний не может загрузить в чужой чат и не видит метаданные/байты (404)"""
|
||
self.assertCode(upload(self.alice, self.chat, SAMPLE_PNG, token=False), 401, 'unauthorized')
|
||
self.assertCode(upload(self.eve, self.chat, SAMPLE_PNG), 404, 'not_found')
|
||
self.assertCode(upload(self.alice, {'id': str(uuid.uuid4())}, SAMPLE_PNG), 404, 'not_found')
|
||
att = upload(self.alice, self.chat, SAMPLE_PNG)[2]
|
||
self.assertCode(call('GET', '/api/v1/attachments/' + att['id'], token=self.eve['token']), 404, 'not_found')
|
||
self.assertEqual(download(self.eve, att['id'])[0], 404)
|
||
self.assertCode(call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), token=self.bob['token']), 404)
|
||
self.assertCode(call('GET', '/api/v1/attachments/not-a-uuid', token=self.bob['token']), 400)
|
||
self.assertCode(call('GET', '/api/v1/attachments/%s/content?variant=thumbnail' % att['id'],
|
||
token=self.bob['token']), 400)
|
||
|
||
|
||
class MessageAttachmentsTest(unittest.TestCase):
|
||
def setUp(self):
|
||
self.alice, self.bob = new_user('alice'), new_user('bob')
|
||
self.chat = new_chat(self.alice, self.bob)
|
||
self.other_chat = new_chat(self.alice, title='Другой')
|
||
self.att = upload(self.alice, self.chat, SAMPLE_PNG)[2]
|
||
|
||
def history(self):
|
||
return call('GET', '/api/v1/chats/%s/messages?limit=100' % self.chat['id'], token=self.bob['token'])[2]['items']
|
||
|
||
def test_message_with_attachment_only(self):
|
||
"""text="" + одно вложение → 201; attachment_ids видны в истории; обычное сообщение получает []"""
|
||
status, _, message = send(self.alice, self.chat, {'text': '', 'attachment_ids': [self.att['id']]})
|
||
self.assertEqual((status, message['attachment_ids'], message['text']), (201, [self.att['id']], ''))
|
||
plain = send(self.alice, self.chat, {'text': 'без вложений'})[2]
|
||
self.assertEqual(plain['attachment_ids'], [])
|
||
self.assertEqual([m['attachment_ids'] for m in self.history()], [[self.att['id']], []])
|
||
|
||
def test_other_member_can_reference_attachment(self):
|
||
"""Боб прикрепляет вложение, загруженное Алисой в этот же чат"""
|
||
status, _, message = send(self.bob, self.chat, {'text': 'смотри', 'attachment_ids': [self.att['id']]})
|
||
self.assertEqual((status, message['attachment_ids']), (201, [self.att['id']]))
|
||
|
||
def test_foreign_or_unknown_attachment(self):
|
||
"""Вложение другого чата или несуществующее → 404, сообщение не создаётся"""
|
||
foreign = upload(self.alice, self.other_chat, SAMPLE_PNG)[2]
|
||
for ids in ([foreign['id']], [str(uuid.uuid4())], [self.att['id'], foreign['id']]):
|
||
with self.subTest(ids=ids):
|
||
self.assertEqual(send(self.alice, self.chat, {'text': 'x', 'attachment_ids': ids})[0], 404)
|
||
self.assertEqual(self.history(), [])
|
||
|
||
def test_invalid_attachment_lists(self):
|
||
"""Повтор id, 11 вложений, не-UUID, пустой текст без вложений, пробелы без вложений → 400"""
|
||
for body in ({'text': 'x', 'attachment_ids': [self.att['id'], self.att['id']]},
|
||
{'text': 'x', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
|
||
{'text': 'x', 'attachment_ids': ['nope']},
|
||
{'text': ''}, {'text': ' '}, {'text': '', 'attachment_ids': []}):
|
||
with self.subTest(body=body):
|
||
self.assertEqual(send(self.alice, self.chat, body)[0], 400)
|
||
|
||
def test_idempotency_includes_attachments(self):
|
||
"""Тот же ключ: те же вложения → тот же Message; другие вложения → 409; нет поля = [] для текста"""
|
||
key = uuid.uuid4().hex
|
||
first = send(self.alice, self.chat, {'text': 'фото', 'attachment_ids': [self.att['id']]}, key)
|
||
again = send(self.alice, self.chat, {'attachment_ids': [self.att['id']], 'text': 'фото'}, key)
|
||
self.assertEqual((first[0], again[0], first[2]), (201, 201, again[2]))
|
||
other = upload(self.alice, self.chat, SAMPLE_JPG, 'image/jpeg', 'b.jpg')[2]
|
||
self.assertEqual(send(self.alice, self.chat, {'text': 'фото', 'attachment_ids': [other['id']]}, key)[0], 409)
|
||
key = uuid.uuid4().hex
|
||
plain = send(self.alice, self.chat, {'text': 'текст'}, key)
|
||
same = send(self.alice, self.chat, {'text': 'текст', 'attachment_ids': []}, key)
|
||
self.assertEqual(plain[2]['id'], same[2]['id'])
|
||
|
||
|
||
if __name__ == '__main__':
|
||
unittest.main()
|