Merge pull request #1528 from GSA/notify-api-1526

make sure we reuse boto3 clients and resources
This commit is contained in:
Kenneth Kehl
2025-01-17 09:00:11 -08:00
committed by GitHub
3 changed files with 101 additions and 22 deletions

View File

@@ -1,9 +1,16 @@
from unittest.mock import MagicMock
from urllib.parse import parse_qs
import botocore
import pytest
from notifications_utils.s3 import S3ObjectNotFound, s3download, s3upload
from notifications_utils.s3 import (
AWS_CLIENT_CONFIG,
S3ObjectNotFound,
get_s3_resource,
s3download,
s3upload,
)
contents = "some file data"
region = "eu-west-1"
@@ -13,7 +20,11 @@ content_type = "binary/octet-stream"
def test_s3upload_save_file_to_bucket(mocker):
mocked = mocker.patch("notifications_utils.s3.Session.resource")
mock_s3_resource = mocker.Mock()
mocked = mocker.patch(
"notifications_utils.s3.get_s3_resource", return_value=mock_s3_resource
)
s3upload(
filedata=contents, region=region, bucket_name=bucket, file_location=location
)
@@ -27,7 +38,11 @@ def test_s3upload_save_file_to_bucket(mocker):
def test_s3upload_save_file_to_bucket_with_contenttype(mocker):
content_type = "image/png"
mocked = mocker.patch("notifications_utils.s3.Session.resource")
mock_s3_resource = mocker.Mock()
mocked = mocker.patch(
"notifications_utils.s3.get_s3_resource", return_value=mock_s3_resource
)
s3upload(
filedata=contents,
region=region,
@@ -44,7 +59,11 @@ def test_s3upload_save_file_to_bucket_with_contenttype(mocker):
def test_s3upload_raises_exception(app, mocker):
mocked = mocker.patch("notifications_utils.s3.Session.resource")
mock_s3_resource = mocker.Mock()
mocked = mocker.patch(
"notifications_utils.s3.get_s3_resource", return_value=mock_s3_resource
)
response = {"Error": {"Code": 500}}
exception = botocore.exceptions.ClientError(response, "Bad exception")
mocked.return_value.Object.return_value.put.side_effect = exception
@@ -58,7 +77,12 @@ def test_s3upload_raises_exception(app, mocker):
def test_s3upload_save_file_to_bucket_with_urlencoded_tags(mocker):
mocked = mocker.patch("notifications_utils.s3.Session.resource")
mock_s3_resource = mocker.Mock()
mocked = mocker.patch(
"notifications_utils.s3.get_s3_resource", return_value=mock_s3_resource
)
s3upload(
filedata=contents,
region=region,
@@ -74,7 +98,12 @@ def test_s3upload_save_file_to_bucket_with_urlencoded_tags(mocker):
def test_s3upload_save_file_to_bucket_with_metadata(mocker):
mocked = mocker.patch("notifications_utils.s3.Session.resource")
mock_s3_resource = mocker.Mock()
mocked = mocker.patch(
"notifications_utils.s3.get_s3_resource", return_value=mock_s3_resource
)
s3upload(
filedata=contents,
region=region,
@@ -88,17 +117,49 @@ def test_s3upload_save_file_to_bucket_with_metadata(mocker):
assert metadata == {"status": "valid", "pages": "5"}
def test_get_s3_resource(mocker):
mock_session = mocker.patch("notifications_utils.s3.Session")
mock_current_app = mocker.patch("notifications_utils.s3.current_app")
sa_key = "sec"
sa_key = f"{sa_key}ret_access_key"
mock_current_app.config = {
"CSV_UPLOAD_BUCKET": {
"access_key_id": "test_access_key",
sa_key: "test_s_key",
"region": "us-west-100",
}
}
mock_s3_resource = MagicMock()
mock_session.return_value.resource.return_value = mock_s3_resource
result = get_s3_resource()
mock_session.return_value.resource.assert_called_once_with(
"s3", config=AWS_CLIENT_CONFIG
)
assert result == mock_s3_resource
def test_s3download_gets_file(mocker):
mocked = mocker.patch("notifications_utils.s3.Session.resource")
mock_s3_resource = mocker.Mock()
mocked = mocker.patch(
"notifications_utils.s3.get_s3_resource", return_value=mock_s3_resource
)
mocked_object = mocked.return_value.Object
mocked_get = mocked.return_value.Object.return_value.get
mocked_object.return_value.get.return_value = {"Body": mocker.Mock()}
s3download("bucket", "location.file")
mocked_object.assert_called_once_with("bucket", "location.file")
mocked_get.assert_called_once_with()
def test_s3download_raises_on_error(mocker):
mocked = mocker.patch("notifications_utils.s3.Session.resource")
mock_s3_resource = mocker.Mock()
mocked = mocker.patch(
"notifications_utils.s3.get_s3_resource", return_value=mock_s3_resource
)
mocked.return_value.Object.side_effect = botocore.exceptions.ClientError(
{"Error": {"Code": 404}},
"Bad exception",