mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 23:26:23 -05:00
more s3 tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
from datetime import timedelta
|
||||
from os import getenv
|
||||
from unittest.mock import ANY, MagicMock, call, patch
|
||||
from unittest.mock import ANY, MagicMock, Mock, call, patch
|
||||
|
||||
import botocore
|
||||
import pytest
|
||||
@@ -19,6 +19,7 @@ from app.aws.s3 import (
|
||||
get_s3_client,
|
||||
get_s3_file,
|
||||
get_s3_files,
|
||||
get_s3_object,
|
||||
get_s3_resource,
|
||||
list_s3_objects,
|
||||
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")
|
||||
|
||||
|
||||
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):
|
||||
mocker.patch("app.aws.s3._get_bucket_name", return_value="Foo")
|
||||
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", "old_key")
|
||||
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}"
|
||||
)
|
||||
|
||||
@@ -159,7 +159,8 @@ def test_provider_details_history_schema_returns_user_details(
|
||||
|
||||
def test_valid_date_within_24_hours(mocker):
|
||||
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"
|
||||
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):
|
||||
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"
|
||||
try:
|
||||
@@ -179,7 +181,8 @@ def test_date_in_past(mocker):
|
||||
|
||||
def test_date_more_than_24_hours_in_future(mocker):
|
||||
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"
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user