From 89dfd955cdbe23cfd811f77283fbe896e5cb1c51 Mon Sep 17 00:00:00 2001 From: Guillaume Charest <1690085+gcharest@users.noreply.github.com> Date: Fri, 19 Apr 2024 17:27:29 +0000 Subject: [PATCH] feat: update unit tests based on new function --- app/tests/integrations/aws/test_client.py | 63 +++++++++++++++-------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/app/tests/integrations/aws/test_client.py b/app/tests/integrations/aws/test_client.py index 2e4bce85..120ba377 100644 --- a/app/tests/integrations/aws/test_client.py +++ b/app/tests/integrations/aws/test_client.py @@ -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) @@ -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) @@ -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) @@ -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(): @@ -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")