forked from mtusi2026/mtusi2026lab4
Реализовать ЛР 4 на 3: приватное S3 и вложения — Гавриленко БПИ2303
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
"""Run in an API container: python - < student-tests/storage.py."""
|
||||
import hashlib
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
sys.path.insert(0, '/app/src')
|
||||
from db import connect
|
||||
from storage import s3, BUCKET
|
||||
|
||||
with connect() as db:
|
||||
rows = db.execute('SELECT object_key, size_bytes, sha256 FROM attachments').fetchall()
|
||||
assert rows
|
||||
for row in rows:
|
||||
response = s3.get_object(Bucket=BUCKET, Key=row['object_key'])
|
||||
with response['Body'] as stream:
|
||||
data = stream.read(10485761)
|
||||
assert len(data) == row['size_bytes'] and hashlib.sha256(data).hexdigest() == row['sha256']
|
||||
try:
|
||||
urllib.request.urlopen(s3.meta.endpoint_url + '/' + BUCKET + '/' + row['object_key'], timeout=5)
|
||||
except urllib.error.HTTPError as exc:
|
||||
assert exc.code == 403
|
||||
else:
|
||||
raise AssertionError('Anonymous download succeeded')
|
||||
print('PASS: all persisted attachments match S3 size/SHA256; anonymous GET of every known object returns 403')
|
||||
Reference in New Issue
Block a user