def test_delete_fail_delete_call( mocked_operations_delete: mock.MagicMock, exists_lambda_event, _mocked_urllib3_pool_manager, _mocked_json_dumps, ): """ GIVEN mocked operations.delete and delete Cloudformation request with fail physical resource id WHEN lambda_handler is called with the request THEN delete is not called. """ event = { **exists_lambda_event, **{ "RequestType": "Delete", "ResourceProperties": { "key": "value" }, "PhysicalResourceId": "[FAIL]physical resource id 1", }, } index.lambda_handler(event, mock.MagicMock()) mocked_operations_delete.assert_not_called()
def test_delete_fail_put(exists_lambda_event, mocked_urllib3_pool_manager: mock.MagicMock): """ GIVEN mocked operations.delete that returns success response and urllib3.PoolManager and delete Cloudformation request with fail physical resource id WHEN lambda_handler is called with the request THEN PoolManager.request PUT is called with the ResponseURL from the request with the correct body. """ event = { **exists_lambda_event, **{ "RequestType": "Delete", "ResponseURL": "response url 1", "StackId": "stack id 1", "RequestId": "request id 1", "LogicalResourceId": "logical resource id 1", "PhysicalResourceId": "[FAIL]physical resource id 1", }, } index.lambda_handler(event, mock.MagicMock()) mocked_urllib3_pool_manager.return_value.request.assert_called_once_with( "PUT", "response url 1", body=json.dumps({ "StackId": "stack id 1", "RequestId": "request id 1", "LogicalResourceId": "logical resource id 1", "PhysicalResourceId": "[FAIL]physical resource id 1", "Status": "SUCCESS", }).encode("utf-8"), )
def test_delete_delete_call( mocked_operations_delete: mock.MagicMock, exists_lambda_event, _mocked_urllib3_pool_manager, _mocked_json_dumps, ): """ GIVEN mocked operations.delete and delete Cloudformation request WHEN lambda_handler is called with the request THEN delete is called with the body and physical resource id from the request. """ event = { **exists_lambda_event, **{ "RequestType": "Delete", "ResourceProperties": { "key": "value" }, "PhysicalResourceId": "physical resource id 1", }, } index.lambda_handler(event, mock.MagicMock()) mocked_operations_delete.assert_called_once_with( body={"key": "value"}, physical_name="physical resource id 1")
def test_lambda_handler_malformed_event(event, _mocked_operations_create): """ GIVEN event dictionary that is not as expected WHEN lambda_handler is called with the event THEN MalformedEventError is raised. """ with pytest.raises(exceptions.MalformedEventError): index.lambda_handler(event, mock.MagicMock())
def test_delete_physical_resource_id_missing(create_lambda_event): """ GIVEN CloudFormation delete request without physical resource id WHEN lambda_handler is called with the request THEN MalformedEventError is raised. """ event = {**create_lambda_event, "RequestType": "Delete"} with pytest.raises(exceptions.MalformedEventError): index.lambda_handler(event, mock.MagicMock())
def test_delete_put_failure( mocked_operations_delete: mock.MagicMock, exists_lambda_event, mocked_urllib3_pool_manager: mock.MagicMock, ): """ GIVEN mocked operations.delete that returns failure response and urllib3.PoolManager and delete Cloudformation request WHEN lambda_handler is called with the request THEN PoolManager.request PUT is called with the ResponseURL from the request with the correct body. """ mocked_operations_delete.return_value = operations.ExistsReturn( "FAILURE", "reason 1") event = { **exists_lambda_event, **{ "RequestType": "Delete", "ResponseURL": "response url 1", "StackId": "stack id 1", "RequestId": "request id 1", "LogicalResourceId": "logical resource id 1", "PhysicalResourceId": "physical resource id 1", }, } index.lambda_handler(event, mock.MagicMock()) mocked_urllib3_pool_manager.return_value.request.assert_called_once_with( "PUT", "response url 1", body=json.dumps({ "StackId": "stack id 1", "RequestId": "request id 1", "LogicalResourceId": "logical resource id 1", "PhysicalResourceId": "physical resource id 1", "Status": "FAILURE", "Reason": "reason 1", }).encode("utf-8"), )
def test_create_put_success( mocked_operations_create: mock.MagicMock, create_lambda_event, mocked_urllib3_pool_manager: mock.MagicMock, ): """ GIVEN mocked operations.create that returns success response and urllib3.PoolManager and create Cloudformation request WHEN lambda_handler is called with the request THEN PoolManager.request PUT is called with the ResponseURL from the request with the correct body. """ mocked_operations_create.return_value = operations.CreateReturn( "SUCCESS", None, "physical name 1") event = { **create_lambda_event, **{ "RequestType": "Create", "ResponseURL": "response url 1", "StackId": "stack id 1", "RequestId": "request id 1", "LogicalResourceId": "logical resource id 1", }, } index.lambda_handler(event, mock.MagicMock()) mocked_urllib3_pool_manager.return_value.request.assert_called_once_with( "PUT", "response url 1", body=json.dumps({ "StackId": "stack id 1", "RequestId": "request id 1", "LogicalResourceId": "logical resource id 1", "Status": "SUCCESS", "PhysicalResourceId": "physical name 1", }).encode("utf-8"), )
def test_create_create_call( mocked_operations_create: mock.MagicMock, create_lambda_event, _mocked_urllib3_pool_manager, _mocked_json_dumps, ): """ GIVEN mocked operations.create and create Cloudformation request WHEN lambda_handler is called with the request THEN create is called with the body from the request. """ event = { **create_lambda_event, **{ "RequestType": "Create", "ResourceProperties": { "key": "value" } }, } index.lambda_handler(event, mock.MagicMock()) mocked_operations_create.assert_called_once_with(body={"key": "value"})