2023-09-12 10:15:07 -07:00
|
|
|
import os
|
2023-03-03 16:01:12 -05:00
|
|
|
from datetime import datetime
|
|
|
|
|
from os import getenv
|
2021-03-10 13:55:06 +00:00
|
|
|
|
2023-09-12 10:15:07 -07:00
|
|
|
import pytest
|
|
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
|
|
|
|
|
|
from app.aws.s3 import file_exists, get_s3_file, remove_csv_object, remove_s3_object
|
2017-06-12 15:55:05 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
default_access_key = getenv("CSV_AWS_ACCESS_KEY_ID")
|
|
|
|
|
default_secret_key = getenv("CSV_AWS_SECRET_ACCESS_KEY")
|
|
|
|
|
default_region = getenv("CSV_AWS_REGION")
|
2017-06-12 15:55:05 +01:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
def single_s3_object_stub(key="foo", last_modified=None):
|
2017-06-12 15:55:05 +01:00
|
|
|
return {
|
2023-08-29 14:54:30 -07:00
|
|
|
"ETag": '"d41d8cd98f00b204e9800998ecf8427e"',
|
|
|
|
|
"Key": key,
|
|
|
|
|
"LastModified": last_modified or datetime.utcnow(),
|
2017-06-12 15:55:05 +01:00
|
|
|
}
|
2017-06-07 16:31:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_s3_file_makes_correct_call(notify_api, mocker):
|
2023-08-29 14:54:30 -07:00
|
|
|
get_s3_mock = mocker.patch("app.aws.s3.get_s3_object")
|
|
|
|
|
get_s3_file(
|
|
|
|
|
"foo-bucket",
|
|
|
|
|
"bar-file.txt",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|
2017-05-12 17:39:15 +01:00
|
|
|
|
|
|
|
|
get_s3_mock.assert_called_with(
|
2023-08-29 14:54:30 -07:00
|
|
|
"foo-bucket",
|
|
|
|
|
"bar-file.txt",
|
2022-09-26 10:56:59 -04:00
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
2017-05-12 17:39:15 +01:00
|
|
|
)
|
2023-09-12 10:15:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_csv_object(notify_api, mocker):
|
|
|
|
|
get_s3_mock = mocker.patch("app.aws.s3.get_s3_object")
|
|
|
|
|
remove_csv_object("mykey")
|
|
|
|
|
|
|
|
|
|
get_s3_mock.assert_called_once_with(
|
|
|
|
|
os.getenv("CSV_BUCKET_NAME"),
|
|
|
|
|
"mykey",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_csv_object_alternate(notify_api, mocker):
|
|
|
|
|
get_s3_mock = mocker.patch("app.aws.s3.get_s3_object")
|
|
|
|
|
remove_s3_object(
|
|
|
|
|
os.getenv("CSV_BUCKET_NAME"),
|
|
|
|
|
"mykey",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
get_s3_mock.assert_called_once_with(
|
|
|
|
|
os.getenv("CSV_BUCKET_NAME"),
|
|
|
|
|
"mykey",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_file_exists_true(notify_api, mocker):
|
|
|
|
|
get_s3_mock = mocker.patch("app.aws.s3.get_s3_object")
|
|
|
|
|
|
|
|
|
|
file_exists(
|
|
|
|
|
os.getenv("CSV_BUCKET_NAME"),
|
|
|
|
|
"mykey",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|
|
|
|
|
get_s3_mock.assert_called_once_with(
|
|
|
|
|
os.getenv("CSV_BUCKET_NAME"),
|
|
|
|
|
"mykey",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_file_exists_false(notify_api, mocker):
|
|
|
|
|
get_s3_mock = mocker.patch("app.aws.s3.get_s3_object")
|
|
|
|
|
error_response = {
|
|
|
|
|
"Error": {"Code": 500, "Message": "bogus"},
|
|
|
|
|
"ResponseMetadata": {"HTTPStatusCode": 500},
|
|
|
|
|
}
|
|
|
|
|
get_s3_mock.side_effect = ClientError(
|
|
|
|
|
error_response=error_response, operation_name="bogus"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ClientError):
|
|
|
|
|
file_exists(
|
|
|
|
|
os.getenv("CSV_BUCKET_NAME"),
|
|
|
|
|
"mykey",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
get_s3_mock.assert_called_once_with(
|
|
|
|
|
os.getenv("CSV_BUCKET_NAME"),
|
|
|
|
|
"mykey",
|
|
|
|
|
default_access_key,
|
|
|
|
|
default_secret_key,
|
|
|
|
|
default_region,
|
|
|
|
|
)
|