Files
mtusi2026lab4/student-tests/test_attachments.py
T
2026-10-30 20:08:17 +03:00

40 lines
1.8 KiB
Python

"""Attachment negative cases. Run against the Compose API at localhost:8080."""
import sys
from pathlib import Path
from uuid import uuid4
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, check
def main():
client = Client()
alice, bob, eve, chat = client.bootstrap()
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png'
image = (Path(__file__).resolve().parents[1] / 'tests' / 'fixtures' / 'sample.png').read_bytes()
client.call('POST', path, 415, actor=alice, raw=b'not an image',
headers={'Content-Type': 'image/png'})
client.call('POST', path, 415, actor=alice, raw=image,
headers={'Content-Type': 'image/jpeg'})
client.call('POST', path, 413, actor=alice, raw=b'x' * (10 * 1024 * 1024 + 1),
headers={'Content-Type': 'image/png'})
client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=../sample.png',
400, actor=alice, raw=image, headers={'Content-Type': 'image/png'})
attachment = client.call('POST', path, 201, actor=alice, raw=image,
headers={'Content-Type': 'image/png'}, schema='Attachment')['json']
client.call('GET', '/api/v1/attachments/' + attachment['id'], 404, actor=eve)
client.call('GET', '/api/v1/attachments/' + str(uuid4()), 404, actor=alice)
other = client.call('POST', '/api/v1/chats', 201, actor=alice,
body={'title': 'Other chat', 'member_ids': [alice['id']]}, schema='Chat')['json']
client.call('POST', '/api/v1/chats/' + other['id'] + '/messages', 404, actor=alice,
body={'text': '', 'attachment_ids': [attachment['id']]},
headers={'Idempotency-Key': uuid4().hex})
if __name__ == '__main__':
main()
print('PASS: attachment validation and access checks')