This commit is contained in:
Матвей
2026-10-26 15:49:31 +03:00
parent 4f6e69cfac
commit a1bb6f8bde
18 changed files with 820 additions and 195 deletions
+7 -5
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Attachment contract checks against the running lab4 stack."""
"""Attachment contract checks against the running lab5 stack."""
import hashlib
import http.client
from io import BytesIO
@@ -23,16 +23,17 @@ def main():
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename='
png = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes()
def upload(data, mime='image/png', filename='photo.png', status=201, actor=alice):
def upload(data, mime='image/png', filename='photo.png', status=202, actor=alice):
return c.call('POST', path + quote(filename, safe=''), status, actor=actor, raw=data,
headers={'Content-Type': mime}, schema='Attachment' if status == 201 else None)
headers={'Content-Type': mime}, schema='Attachment' if status == 202 else None)
for fmt, mime in [('PNG', 'image/png'), ('JPEG', 'image/jpeg')]:
buf = BytesIO()
Image.new('RGB', (32, 20), 'blue').save(buf, fmt)
data = buf.getvalue()
att = upload(data, mime)['json']
check(att['variants'] == [] and att['status'] == 'ready', 'wrong attachment state')
check(att['variants'] == [] and att['status'] == 'queued', 'wrong attachment state')
att = c.wait_ready(bob, att)
check(att['sha256'] == hashlib.sha256(data).hexdigest(), 'wrong checksum')
content_path = '/api/v1/attachments/' + att['id']
check(c.call('GET', content_path, actor=bob, schema='Attachment')['json'] == att,
@@ -41,7 +42,7 @@ def main():
c.call('GET', content_path, 404, actor=eve)
c.call('GET', content_path + '/content', 404, actor=eve)
c.call('GET', content_path + '/content', 401)
c.call('GET', content_path + '/content?variant=thumbnail', 404, actor=bob)
c.call('GET', content_path + '/content?variant=thumbnail', actor=bob)
message = c.message(bob, chat, '', attachments=[att['id']])['json']
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages',
actor=alice, schema='MessagePage')['json']['items']
@@ -98,6 +99,7 @@ def main():
# Exact limits are accepted; original bytes (including PNG trailing data) survive.
exact = png + b'\0' * (10485760 - len(png))
att = upload(exact)['json']
c.wait_ready(bob, att)
check(c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=bob)['bytes'] == exact,
'10 MiB original changed')
buf = BytesIO()