more s3 tests

This commit is contained in:
Kenneth Kehl
2024-10-28 11:55:30 -07:00
parent 0ded5fa591
commit 54cce400f4
3 changed files with 50 additions and 6 deletions

View File

@@ -209,7 +209,7 @@
"filename": "tests/app/aws/test_s3.py", "filename": "tests/app/aws/test_s3.py",
"hashed_secret": "67a74306b06d0c01624fe0d0249a570f4d093747", "hashed_secret": "67a74306b06d0c01624fe0d0249a570f4d093747",
"is_verified": false, "is_verified": false,
"line_number": 39, "line_number": 40,
"is_secret": false "is_secret": false
} }
], ],
@@ -384,5 +384,5 @@
} }
] ]
}, },
"generated_at": "2024-10-24T20:01:26Z" "generated_at": "2024-10-28T18:55:27Z"
} }

View File

@@ -1,7 +1,7 @@
import os import os
from datetime import timedelta from datetime import timedelta
from os import getenv from os import getenv
from unittest.mock import ANY, MagicMock, call, patch from unittest.mock import ANY, MagicMock, Mock, call, patch
import botocore import botocore
import pytest import pytest
@@ -19,6 +19,7 @@ from app.aws.s3 import (
get_s3_client, get_s3_client,
get_s3_file, get_s3_file,
get_s3_files, get_s3_files,
get_s3_object,
get_s3_resource, get_s3_resource,
list_s3_objects, list_s3_objects,
read_s3_file, read_s3_file,
@@ -138,6 +139,22 @@ def test_download_from_s3_no_credentials_error(mocker):
mock_logger.exception.assert_called_once_with("Credentials not found") mock_logger.exception.assert_called_once_with("Credentials not found")
def test_download_from_s3_general_exception(mocker):
mock_get_s3_client = mocker.patch("app.aws.s3.get_s3_client")
mock_current_app = mocker.patch("app.aws.s3.current_app")
mock_logger = mock_current_app.logger
mock_s3 = MagicMock()
mock_s3.download_file.side_effect = Exception()
mock_get_s3_client.return_value = mock_s3
try:
download_from_s3(
"test_bucket", "test_key", "test_file", "access_key", "secret_key", "region"
)
except Exception:
pass
mock_logger.exception.assert_called_once_with("EXCEPTION local_filename test_file")
def test_list_s3_objects(mocker): def test_list_s3_objects(mocker):
mocker.patch("app.aws.s3._get_bucket_name", return_value="Foo") mocker.patch("app.aws.s3._get_bucket_name", return_value="Foo")
mock_s3_client = mocker.Mock() mock_s3_client = mocker.Mock()
@@ -487,3 +504,27 @@ def test_get_job_and_metadata_from_s3_fallback_to_old_location(mocker):
# mock_get_s3_object.assert_any_call("bucket_name", "new_key") # mock_get_s3_object.assert_any_call("bucket_name", "new_key")
# mock_get_s3_object.assert_any_call("bucket_name", "old_key") # mock_get_s3_object.assert_any_call("bucket_name", "old_key")
assert result == ("old job data", {"old_key": "old_value"}) assert result == ("old job data", {"old_key": "old_value"})
def test_get_s3_object_client_error(mocker):
mock_get_s3_resource = mocker.patch("app.aws.s3.get_s3_resource")
mock_current_app = mocker.patch("app.aws.s3.current_app")
mock_logger = mock_current_app.logger
mock_s3 = Mock()
mock_s3.Object.side_effect = botocore.exceptions.ClientError(
error_response={"Error": {"Code": "404", "Message": "Not Found"}},
operation_name="GetObject",
)
mock_get_s3_resource.return_value = mock_s3
bucket_name = "test-bucket"
file_location = "nonexistent-file.txt"
access_key = "test-access-key"
skey = "skey"
region = "us-west-200"
result = get_s3_object(bucket_name, file_location, access_key, skey, region)
assert result is None
mock_s3.Object.assert_called_once_with(bucket_name, file_location)
mock_logger.exception.assert_called_once_with(
f"Can't retrieve S3 Object from {file_location}"
)

View File

@@ -159,7 +159,8 @@ def test_provider_details_history_schema_returns_user_details(
def test_valid_date_within_24_hours(mocker): def test_valid_date_within_24_hours(mocker):
mocker.patch( mocker.patch(
"app.schema_validation.utc_now", return_value=datetime.datetime(2024, 10, 27, 15, 0, 0) "app.schema_validation.utc_now",
return_value=datetime.datetime(2024, 10, 27, 15, 0, 0),
) )
valid_datetime = "2024-10-28T14:00:00Z" valid_datetime = "2024-10-28T14:00:00Z"
assert validate_schema_date_with_hour(valid_datetime) assert validate_schema_date_with_hour(valid_datetime)
@@ -167,7 +168,8 @@ def test_valid_date_within_24_hours(mocker):
def test_date_in_past(mocker): def test_date_in_past(mocker):
mocker.patch( mocker.patch(
"app.schema_validation.utc_now", return_value=datetime.datetime(2024, 10, 27, 15, 0, 0) "app.schema_validation.utc_now",
return_value=datetime.datetime(2024, 10, 27, 15, 0, 0),
) )
past_datetime = "2024-10-26T14:00:00Z" past_datetime = "2024-10-26T14:00:00Z"
try: try:
@@ -179,7 +181,8 @@ def test_date_in_past(mocker):
def test_date_more_than_24_hours_in_future(mocker): def test_date_more_than_24_hours_in_future(mocker):
mocker.patch( mocker.patch(
"app.schema_validation.utc_now", return_value=datetime.datetime(2024, 10, 27, 15, 0, 0) "app.schema_validation.utc_now",
return_value=datetime.datetime(2024, 10, 27, 15, 0, 0),
) )
past_datetime = "2024-10-31T14:00:00Z" past_datetime = "2024-10-31T14:00:00Z"
try: try: