Skip to content

Commit

Permalink
feat: update unit tests based on new function
Browse files Browse the repository at this point in the history
  • Loading branch information
gcharest authored Apr 19, 2024
1 parent ac22087 commit 89dfd95
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions app/tests/integrations/aws/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@


@patch("integrations.aws.client.logger.error")
def test_handle_aws_api_errors_catches_botocore_error(mocked_logging_error):
@patch("integrations.aws.client.logger.info")
def test_handle_aws_api_errors_catches_botocore_error(
mocked_logging_info, mocked_logging_error
):
mock_func = MagicMock(side_effect=BotoCoreError())
mock_func.__name__ = "mock_func"
decorated_func = aws_client.handle_aws_api_errors(mock_func)
Expand All @@ -20,11 +23,40 @@ def test_handle_aws_api_errors_catches_botocore_error(mocked_logging_error):
mocked_logging_error.assert_called_once_with(
"A BotoCore error occurred in function 'mock_func': An unspecified error occurred"
)
mocked_logging_info.assert_not_called()


@patch("integrations.aws.client.logger.error")
@patch("integrations.aws.client.logger.info")
def test_handle_aws_api_errors_catches_client_error_resource_not_found(
mocked_logging_info, mocked_logging_error
):
mock_func = MagicMock(
side_effect=ClientError(
{"Error": {"Code": "ResourceNotFoundException"}}, "operation_name"
)
)
mock_func.__name__ = "mock_func"
decorated_func = aws_client.handle_aws_api_errors(mock_func)

result = decorated_func()

assert result is False
mock_func.assert_called_once()
mocked_logging_info.assert_called_once_with(
"Resource not found in function 'mock_func': An error occurred (ResourceNotFoundException) when calling the operation_name operation: Unknown"
)
mocked_logging_error.assert_not_called()


@patch("integrations.aws.client.logger.error")
def test_handle_aws_api_errors_catches_client_error(mocked_logging_error):
mock_func = MagicMock(side_effect=ClientError({"Error": {}}, "operation_name"))
@patch("integrations.aws.client.logger.info")
def test_handle_aws_api_errors_catches_client_error_other(
mocked_logging_info, mocked_logging_error
):
mock_func = MagicMock(
side_effect=ClientError({"Error": {"Code": "OtherError"}}, "operation_name")
)
mock_func.__name__ = "mock_func"
decorated_func = aws_client.handle_aws_api_errors(mock_func)

Expand All @@ -33,12 +65,16 @@ def test_handle_aws_api_errors_catches_client_error(mocked_logging_error):
assert result is None
mock_func.assert_called_once()
mocked_logging_error.assert_called_once_with(
"A ClientError occurred in function 'mock_func': An error occurred (Unknown) when calling the operation_name operation: Unknown"
"A ClientError occurred in function 'mock_func': An error occurred (OtherError) when calling the operation_name operation: Unknown"
)
mocked_logging_info.assert_not_called()


@patch("integrations.aws.client.logger.error")
def test_handle_aws_api_errors_catches_exception(mocked_logging_error):
@patch("integrations.aws.client.logger.info")
def test_handle_aws_api_errors_catches_exception(
mocked_logging_info, mocked_logging_error
):
mock_func = MagicMock(side_effect=Exception("Exception message"))
mock_func.__name__ = "mock_func"
decorated_func = aws_client.handle_aws_api_errors(mock_func)
Expand All @@ -50,6 +86,7 @@ def test_handle_aws_api_errors_catches_exception(mocked_logging_error):
mocked_logging_error.assert_called_once_with(
"An unexpected error occurred in function 'mock_func': Exception message"
)
mocked_logging_info.assert_not_called()


def test_handle_aws_api_errors_passes_through_return_value():
Expand Down Expand Up @@ -176,22 +213,6 @@ def test_assume_role_client(mock_boto3_client):
assert client == mock_service_client


@patch("boto3.client")
def test_assume_role_client_raises_exception_on_error(mock_boto3_client):
mock_sts_client = MagicMock()
mock_boto3_client.return_value = mock_sts_client

mock_sts_client.assume_role.side_effect = BotoCoreError

with pytest.raises(BotoCoreError):
aws_client.assume_role_client("test_service", "test_role_arn")

mock_boto3_client.assert_called_once_with("sts")
mock_sts_client.assume_role.assert_called_once_with(
RoleArn="test_role_arn", RoleSessionName="AssumeRoleSession1"
)


@patch.dict(os.environ, {"AWS_SSO_ROLE_ARN": "test_role_arn"})
@patch("integrations.aws.client.paginator")
@patch("integrations.aws.client.convert_kwargs_to_camel_case")
Expand Down

0 comments on commit 89dfd95

Please sign in to comment.