lab4: S3-вложения, presigned upload и проверки
This commit is contained in:
Executable
+107
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Negative attachment scenarios required by lab4 level 4.
|
||||
|
||||
Run against a live stand:
|
||||
python3 student-tests/attachments_negative.py --base-url http://localhost:8080
|
||||
"""
|
||||
import hashlib
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
|
||||
from client import Client, Failure, arguments, check, run # noqa: E402
|
||||
|
||||
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
|
||||
|
||||
|
||||
def upload(client, actor, chat, content, filename='sample.png', content_type='image/png', expected=201):
|
||||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + quote(filename, safe='')
|
||||
return client.call('POST', path, expected, actor=actor, raw=content, headers={'Content-Type': content_type})
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Attachment negative scenarios (lab4)')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
fixture = FIXTURE.read_bytes()
|
||||
|
||||
# Oversized body: rejected before any image parsing.
|
||||
upload(client, alice, chat, b'\x00' * (10 * 1024 * 1024 + 1), expected=413)
|
||||
|
||||
# Empty body is not a valid image.
|
||||
upload(client, alice, chat, b'', expected=415)
|
||||
|
||||
# Corrupt PNG: flip one IDAT payload byte, CRC no longer matches.
|
||||
corrupt = bytearray(fixture)
|
||||
index = corrupt.index(b'IDAT') + 8
|
||||
corrupt[index] ^= 0xFF
|
||||
upload(client, alice, chat, bytes(corrupt), expected=415)
|
||||
|
||||
# Declared JPEG but the bytes are PNG.
|
||||
upload(client, alice, chat, fixture, filename='photo.jpg', content_type='image/jpeg', expected=415)
|
||||
|
||||
# Dangerous display names.
|
||||
for name in ('../x.png', 'a/b.png', 'a\\b.png', 'a\x00.png', 'a\n.png'):
|
||||
upload(client, alice, chat, fixture, filename=name, expected=400)
|
||||
|
||||
# A member of one chat cannot read an attachment of another chat.
|
||||
other = client.call(
|
||||
'POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Другой чат', 'member_ids': [alice['id'], eve['id']]}, schema='Chat',
|
||||
)['json']
|
||||
foreign, foreign_content = client.attachment(eve, other)
|
||||
client.call('GET', '/api/v1/attachments/' + foreign['id'], 404, actor=bob)
|
||||
client.call('GET', '/api/v1/attachments/' + foreign['id'] + '/content', 404, actor=bob)
|
||||
|
||||
# Message rules: empty text without attachments, attachment from another chat.
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': ''}, headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
|
||||
body={'text': 'чужое', 'attachment_ids': [foreign['id']]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
|
||||
# Contract limits: 11 unique IDs and duplicate IDs are rejected by the schema.
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': 'много', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
duplicate = str(uuid.uuid4())
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': 'дубликат', 'attachment_ids': [duplicate, duplicate]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
|
||||
# The only variant in the lab4 contract is original; other values are 400.
|
||||
own, content = client.attachment(alice, chat)
|
||||
client.call('GET', '/api/v1/attachments/' + own['id'] + '/content?variant=thumbnail', 400, actor=alice)
|
||||
|
||||
# Attachment-only message is valid.
|
||||
client.message(alice, chat, '', attachments=[own['id']])
|
||||
|
||||
# Object keys are server-generated; anonymous direct access must be denied.
|
||||
key = 'attachments/' + chat['id'] + '/' + own['id']
|
||||
request = urllib.request.Request('http://127.0.0.1:8333/messenger-attachments/' + key, method='GET')
|
||||
try:
|
||||
urllib.request.urlopen(request, timeout=5)
|
||||
except urllib.error.HTTPError as error:
|
||||
check(error.code == 403, 'anonymous S3 GET must be 403, got ' + str(error.code))
|
||||
else:
|
||||
raise Failure('anonymous S3 GET unexpectedly succeeded')
|
||||
|
||||
check(hashlib.sha256(content).hexdigest() == own['sha256'], 'fixture checksum mismatch')
|
||||
print('PASS: lab4 attachment negative scenarios')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user