def test_config_from_params(self):
     config = Config({
         'Id': 'test-id',
         'LambdaFunctionArn': 'test-arn:10',
         'Events': [],
         'Filter': {
             'Key': {
                 'FilterRules': [{
                     'Name': 'Prefix',
                     'Value': ''
                 }, {
                     'Name': 'Suffix',
                     'Value': ''
                 }]
             }
         }
     })
     config_from_params = Config.from_params(
         **{
             'event_name': 'test-id',
             'lambda_function_arn': 'test-arn',
             'lambda_version': 10,
             'events': [],
             'prefix': '',
             'suffix': ''
         })
     assert config.raw == config_from_params.raw
     assert config == config_from_params
    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(client, 'test-bucket')
        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_config(self):
        client = MagicMock()
        client.get_bucket_notification_configuration.return_value = {
            'LambdaFunctionConfigurations': []
        }

        bucket = AmazonBucket(client, 'test-bucket')
        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
 def test_apply_invalid_config(self):
     client = MagicMock()
     client.get_bucket_notification_configuration.return_value = {
         'LambdaFunctionConfigurations': []
     }
     client.put_bucket_notification_configuration.side_effect = ClientError(
         {}, '')
     bucket = AmazonBucket(client, 'test-bucket')
     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(ClientError):
         bucket.apply_config(config)