Merge pull request #1570 from GSA/notify-admin-1041

use moto to mock s3
This commit is contained in:
Carlo Costino
2024-05-31 16:12:06 -04:00
committed by GitHub
9 changed files with 277 additions and 87 deletions

View File

@@ -38,6 +38,17 @@ def s3upload(
region_name=region,
)
_s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
# This 'proves' that use of moto in the relevant tests in test_send.py
# mocks everything related to S3. What you will see in the logs is:
# Exception: CREATED AT <MagicMock name='resource().Bucket().creation_date' id='4665562448'>
#
# raise Exception(f"CREATED AT {_s3.Bucket(bucket_name).creation_date}")
if os.getenv("NOTIFY_ENVIRONMENT") == "test":
teststr = str(_s3.Bucket(bucket_name).creation_date).lower()
if "magicmock" not in teststr:
raise Exception(
"Test is not mocked, use @mock_aws or the relevant mocker.patch to avoid accessing S3"
)
key = _s3.Object(bucket_name, file_location)
@@ -82,6 +93,17 @@ def s3download(
)
s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
key = s3.Object(bucket_name, filename)
# This 'proves' that use of moto in the relevant tests in test_send.py
# mocks everything related to S3. What you will see in the logs is:
# Exception: CREATED AT <MagicMock name='resource().Bucket().creation_date' id='4665562448'>
#
# raise Exception(f"CREATED AT {_s3.Bucket(bucket_name).creation_date}")
if os.getenv("NOTIFY_ENVIRONMENT") == "test":
teststr = str(s3.Bucket(bucket_name).creation_date).lower()
if "magicmock" not in teststr:
raise Exception(
"Test is not mocked, use @mock_aws or the relevant mocker.patch to avoid accessing S3"
)
return key.get()["Body"]
except botocore.exceptions.ClientError as error:
raise S3ObjectNotFound(error.response, error.operation_name)