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

@@ -1,3 +1,5 @@
import os
import botocore
from boto3 import Session
from botocore.config import Config
@@ -29,6 +31,17 @@ def get_s3_object(
)
s3 = session.resource("s3", config=AWS_CLIENT_CONFIG)
obj = 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 obj

View File

@@ -28,6 +28,7 @@ def get_csv_upload(service_id, upload_id):
def s3upload(service_id, filedata):
upload_id = str(uuid.uuid4())
bucket_name, file_location, access_key, secret_key, region = get_csv_location(
service_id, upload_id