"""Lambda test module.""" from unittest.mock import patch import pytest from src import app as index @pytest.fixture() def mock_datadog_api(): """Mock the datadog api.""" with patch('src.app.util.datadog_connection') as connection: yield connection.return_value.__enter__.return_value def test_handle_operation_failed_replication_event(mock_datadog_api): """Test handling of failed replication operation events.""" record = { 'eventName': 'Replication:OperationFailedReplication', 's3': { 'bucket': { 'name': 'test-bucket', 'ownerIdentity': { 'principalId': '123456789012' } }, 'object': { 'key': 'object-key', 'versionId': 'object-version' } }, 'replicationEventData': { 'replicationRuleId': 'rule-id', 'destinationBucket': 'destination-bucket', 's3Operation': 'PUT_OBJECT', 'failureReason': 'SrcGetObjectNotPermitted', 'requestTime': '2024-09-02T09:49:55.322Z' } } index.handler({'Records': [record]}, None) mock_datadog_api.Event.create.assert_called_with( attach_host_name=False, alert_type='error', date_happened=1725270595, source_type_name='amazon s3', tags=['aws_account:123456789012', 'bucket_name:test-bucket', 'destination_bucket:destination-bucket', 'failure_reason:SrcGetObjectNotPermitted', 'replication_rule_id:rule-id', 's3_operation:PUT_OBJECT'], text='Object object-key failed to replicate to destination-bucket. ' 'Failure reason: SrcGetObjectNotPermitted. ' 'Object version: object-version.', title='Replication failure for bucket test-bucket') def test_unsupported_event(): """Test exception is thrown when an unsupported event is received.""" record = { 'eventName': 'ObjectCreated', 's3': { 'bucket': { 'name': 'test-bucket', 'ownerIdentity': { 'principalId': '123456789012' } }, 'object': { 'key': 'object-key', 'versionId': 'object-version' } } } with pytest.raises(Exception) as e: index.handler({'Records': [record]}, None) assert str(e) == 'Event ObjectCreated not supported.'