def test_apply_config_add_event(self): api_config = { 'Id': 'test-id', 'LambdaFunctionArn': 'test-arn', 'Events': ['s3:ObjectRemoved:*'], 'Filter': { 'Key': { 'FilterRules': [{ 'Name': 'Prefix', 'Value': '' }, { 'Name': 'Suffix', 'Value': '' }] } } } client = MagicMock() client.get_bucket_notification_configuration.return_value = { 'LambdaFunctionConfigurations': [api_config] } bucket = AmazonBucket(self.module, client) config = Config.from_params( **{ 'event_name': 'test-id', 'lambda_function_arn': 'test-arn', 'lambda_version': 1, 'events': ['s3:ObjectRemoved:*', 's3:ObjectCreated:*'], 'prefix': '', 'suffix': '' }) bucket.apply_config(config) assert client.get_bucket_notification_configuration.call_count == 1 assert client.put_bucket_notification_configuration.call_count == 1 client.put_bucket_notification_configuration.assert_called_with( Bucket='test-bucket', NotificationConfiguration={ 'LambdaFunctionConfigurations': [{ 'Id': 'test-id', 'LambdaFunctionArn': 'test-arn:1', 'Events': ['s3:ObjectCreated:*', 's3:ObjectRemoved:*'], 'Filter': { 'Key': { 'FilterRules': [{ 'Name': 'Prefix', 'Value': '' }, { 'Name': 'Suffix', 'Value': '' }] } } }] })
def test_apply_invalid_config(self): client = MagicMock() client.get_bucket_notification_configuration.return_value = { 'LambdaFunctionConfigurations': [] } client.put_bucket_notification_configuration.side_effect = AnsibleFailJson( {}, '') bucket = AmazonBucket(self.module, client) config = Config.from_params( **{ 'event_name': 'test_event', 'lambda_function_arn': 'lambda_arn', 'lambda_version': 1, 'events': ['s3:ObjectRemoved:*', 's3:ObjectCreated:*'], 'prefix': '', 'suffix': '' }) with pytest.raises(AnsibleFailJson): bucket.apply_config(config)
def test_apply_config(self): client = MagicMock() client.get_bucket_notification_configuration.return_value = { 'LambdaFunctionConfigurations': [] } bucket = AmazonBucket(self.module, client) config = Config.from_params( **{ 'event_name': 'test_event', 'lambda_function_arn': 'lambda_arn', 'lambda_version': 1, 'events': ['s3:ObjectRemoved:*', 's3:ObjectCreated:*'], 'prefix': '', 'suffix': '' }) bucket.apply_config(config) assert client.get_bucket_notification_configuration.call_count == 1 assert client.put_bucket_notification_configuration.call_count == 1