Files
mtusi2026lab4/student-tests/test_storage_bootstrap.py

39 lines
1.3 KiB
Python

"""Run inside an API container after S3 bootstrap."""
import io
import unittest
from uuid import uuid4
from urllib.error import HTTPError
from urllib.request import urlopen
from minio.error import S3Error
from src.attachments import BUCKET, storage
class StorageBootstrapTests(unittest.TestCase):
def test_private_bucket_and_scoped_api_credentials(self):
client = storage()
key = "test-bootstrap/" + uuid4().hex
data = b"private attachment fixture"
client.put_object(BUCKET, key, io.BytesIO(data), len(data), content_type="application/octet-stream")
try:
response = client.get_object(BUCKET, key)
try:
self.assertEqual(response.read(), data)
finally:
response.close()
response.release_conn()
with self.assertRaises(HTTPError) as denied:
urlopen(f"http://s3:9000/{BUCKET}/{key}", timeout=3)
self.assertEqual(denied.exception.code, 403)
with self.assertRaises(S3Error) as forbidden:
client.make_bucket("forbidden-" + uuid4().hex)
self.assertEqual(forbidden.exception.code, "AccessDenied")
finally:
client.remove_object(BUCKET, key)
if __name__ == "__main__":
unittest.main()