import pytest from common.src.aws.utils import get_s3_uri_from_bucket_key from .....src.aws import new_s3_client from .....src.email_composer.models import attachments from lambdas.send_email.src.config import TEST_S3_BUCKET class TestS3Attachment: _class = attachments.S3Attachment test_bucket = TEST_S3_BUCKET test_key = "__test-file.txt" test_mime_type = "plain/text" test_s3_uri = get_s3_uri_from_bucket_key(test_bucket, test_key) test_sample_bytes: bytes = b"Hello world!" @pytest.fixture(scope="class") def s3_client(self): """Create a new S3 client which will be used by all tests.""" return new_s3_client() @pytest.fixture def new_s3_test_object(self, s3_client): """Create a test object in S3.""" s3_client.put_object( Bucket=self.test_bucket, Key=self.test_key, Body=self.test_sample_bytes, ) def delete_s3_test_object(self, s3_client): """Delete the S3 test object.""" s3_client.delete_object(Bucket=self.test_bucket, Key=self.test_key) @pytest.fixture def instance(self, s3_client, new_s3_test_object): yield self._class( file_name="sample file name", mime_type="sample_mime", attachment=self.test_s3_uri, ) self.delete_s3_test_object(s3_client) def test_attachment_data(self, instance): """Assert that the attachment data is correct, i.e. is the data of the S3 object in bytes. """ assert instance.attachment_data == self.test_sample_bytes