"""Extra grade-4 black-box negative cases. Run from project root after smoke.""" import hashlib import sys import uuid from pathlib import Path from urllib.parse import quote ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, check, arguments, run def main(): args = arguments("Grade-4 negative attachment tests") c = Client(args.base_url, args.ca_file) alice, bob, outsider, chat = c.bootstrap() other = c.call("POST", "/api/v1/chats", 201, actor=alice, body={"title": "Other", "member_ids": [alice['id']]}, schema="Chat")['json'] path = f"/api/v1/chats/{chat['id']}/attachments?filename=photo.png" content = (ROOT / "tests/fixtures/sample.png").read_bytes() def raw(path, data, expect, actor=alice, mime="image/png"): return c.call("POST", path, expect, actor=actor, raw=data, headers={"Content-Type": mime}) # Size, corrupt image, advertised MIME mismatch, invalid filename, and ACL before upload. raw(path, b"a" * (10485760 + 1), 413) raw(path, b"this is not png", 415) raw(path, content[:50], 415) raw(path, content, 415, mime="image/jpeg") raw(f"/api/v1/chats/{chat['id']}/attachments?filename={quote('../bad.png', safe='')}", content, 400) raw(path, content, 404, actor=outsider) att = raw(path, content, 201) import json data = json.loads(att['bytes']) check(data['sha256'] == hashlib.sha256(content).hexdigest(), 'SHA mismatch') aid = data['id'] for suffix in ('', '/content'): c.call('GET', f'/api/v1/attachments/{aid}{suffix}', 404, actor=outsider) c.call('GET', f'/api/v1/attachments/{uuid.uuid4()}{suffix}', 404, actor=alice) c.message(alice, other, 'other chat', attachments=[], key=uuid.uuid4().hex) c.call('POST', f"/api/v1/chats/{other['id']}/messages", 404, actor=alice, headers={'Idempotency-Key':uuid.uuid4().hex}, body={'text':'', 'attachment_ids':[aid]}) same = c.message(alice, chat, '', attachments=[aid], key=uuid.uuid4().hex)['json'] check(same['attachment_ids'] == [aid], 'attachment link missing') fetched = c.call('GET', f'/api/v1/attachments/{aid}/content', actor=bob) check(fetched['bytes'] == content, 'download differs from upload') print('PASS: grade-4 negative upload/ACL/cross-chat cases') if __name__ == '__main__': run(main)