Files
mtusi2026lab5/student-tests/attachments.py
T

52 lines
2.1 KiB
Python

"""Negative checks for upload validation and chat-scoped access."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parents[1] / 'tests'))
from client import Client, check
def main():
client = Client('http://localhost:8080')
alice, bob, eve, chat = client.bootstrap()
sample = (Path(__file__).parents[1] / 'tests/fixtures/sample.png').read_bytes()
attachment = client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png',
202, actor=alice, raw=sample,
headers={'Content-Type': 'image/png'}, schema='Attachment',
)['json']
attachment_id = attachment['id']
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=broken.png',
415, actor=alice, raw=b'not an image',
headers={'Content-Type': 'image/png'},
)
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=wrong.jpg',
415, actor=alice, raw=sample,
headers={'Content-Type': 'image/jpeg'},
)
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=large.png',
413, actor=alice, raw=b'0' * (10 * 1024 * 1024 + 1),
headers={'Content-Type': 'image/png'},
)
client.call('GET', '/api/v1/attachments/' + attachment_id, 404, actor=eve)
client.call('GET', '/api/v1/attachments/' + attachment_id + '/content', 404, actor=eve)
other_chat = client.call(
'POST', '/api/v1/chats', 201, actor=alice,
body={'title': 'Другой чат', 'member_ids': [alice['id'], eve['id']]}, schema='Chat',
)['json']
client.message(alice, other_chat, 'без чужого вложения')
response = client.call(
'POST', '/api/v1/chats/' + other_chat['id'] + '/messages', 404, actor=alice,
body={'text': '', 'attachment_ids': [attachment_id]},
headers={'Idempotency-Key': 'foreign-attachment-check'},
)
check(response['status'] == 404, 'foreign attachment was accepted')
print('PASS: attachment validation and chat-scoped access')
if __name__ == '__main__':
main()