Files
mtusi2026lab4/student-tests/test_storage.py
T

82 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import subprocess
import unittest
from helpers import compose, new_chat, new_user, sql
from imagegen import SAMPLE_PNG, noise_png
from test_attachments import upload
LIMITED_CLIENT = '''
import json
from botocore.exceptions import ClientError
from config import settings
from s3 import client
results = {}
for name, action in (
("put_get_delete_own_bucket", lambda: (client.put_object(Bucket=settings.s3_bucket, Key="probe/x", Body=b"x"),
client.get_object(Bucket=settings.s3_bucket, Key="probe/x"),
client.delete_object(Bucket=settings.s3_bucket, Key="probe/x"))),
("list_bucket_objects", lambda: client.list_objects_v2(Bucket=settings.s3_bucket)),
("create_bucket", lambda: client.create_bucket(Bucket="intruder")),
("delete_bucket", lambda: client.delete_bucket(Bucket=settings.s3_bucket)),
("list_all_buckets", lambda: client.list_buckets()["Buckets"])):
try:
value = action()
results[name] = "allowed" if name != "list_all_buckets" else "allowed:%d" % len(value)
except ClientError as error:
results[name] = error.response["Error"]["Code"]
print(json.dumps(results))
'''
class StorageTest(unittest.TestCase):
def setUp(self):
self.alice = new_user('alice')
self.chat = new_chat(self.alice)
def object_key(self, attachment_id):
return sql("SELECT object_key FROM attachments WHERE id = '%s'" % attachment_id)
def test_object_key_is_generated_by_server(self):
"""Ключ объекта = attachments/<uuid вложения>, filename в нём не участвует"""
att = upload(self.alice, self.chat, SAMPLE_PNG, filename='мой-файл.png')[2]
self.assertEqual(self.object_key(att['id']), 'attachments/' + att['id'])
def test_bucket_is_private_outside_api(self):
"""Анонимный GET объекта и листинг bucket напрямую в S3 (из сети backend) → 403"""
att = upload(self.alice, self.chat, SAMPLE_PNG)[2]
for path in ('/messenger-attachments/' + self.object_key(att['id']), '/messenger-attachments'):
with self.subTest(path=path):
result = compose('exec', '-T', 'lb', 'wget', '-S', '-O', '/dev/null', 'http://s3:8333' + path, check=False)
self.assertNotIn('200 OK', result)
output = compose('exec', '-T', 'lb', 'sh', '-c', 'wget -S -O /dev/null http://s3:8333%s 2>&1; true' % path)
self.assertIn('403', output)
def test_api_credentials_are_limited(self):
"""Ключи API: свои объекты читать/писать/удалять можно; листинг, создание и удаление bucket — AccessDenied"""
import json
results = json.loads(compose('exec', '-T', 'api', 'python', '-c', 'import sys; sys.path.insert(0, "src")\n' + LIMITED_CLIENT))
self.assertEqual(results['put_get_delete_own_bucket'], 'allowed')
for action in ('list_bucket_objects', 'create_bucket', 'delete_bucket'):
self.assertEqual(results[action], 'AccessDenied', results)
self.assertNotEqual(results['list_all_buckets'], 'allowed:1', 'API sees the bucket list')
def test_bootstrap_is_idempotent(self):
"""Повторный s3-init: код 0, bucket уже есть, приватность подтверждена"""
output = compose('run', '--rm', '--no-deps', 's3-init')
self.assertIn('already exists, nothing to do', output)
self.assertIn('is private', output)
def test_no_original_bytes_in_database_or_api_container(self):
"""В attachments единственный bytea — sha256 (байтов файла в БД нет); после загрузки 2 МБ в /tmp реплик API пусто"""
binary = sql("SELECT string_agg(column_name, ',') FROM information_schema.columns "
"WHERE table_name = 'attachments' AND data_type = 'bytea'")
self.assertEqual(binary, 'sha256')
self.assertEqual(upload(self.alice, self.chat, noise_png(2 * 1024 * 1024))[0], 201)
for container in compose('ps', '-q', 'api').split():
files = subprocess.run(['docker', 'exec', container, 'sh', '-c', 'find /tmp -type f | wc -l'],
capture_output=True, text=True).stdout.strip()
self.assertEqual(files, '0', container[:12])
if __name__ == '__main__':
unittest.main()