|
1 |
| -from aws_lambda_powertools.utilities.data_classes import SQSEvent |
| 1 | +import json |
| 2 | + |
| 3 | +from aws_lambda_powertools.utilities.data_classes import S3Event, SQSEvent |
| 4 | +from aws_lambda_powertools.utilities.data_classes.sns_event import SNSMessage |
2 | 5 | from tests.functional.utils import load_event
|
3 | 6 |
|
4 | 7 |
|
@@ -38,3 +41,94 @@ def test_seq_trigger_event():
|
38 | 41 |
|
39 | 42 | record_2 = records[1]
|
40 | 43 | assert record_2.json_body == {"message": "foo1"}
|
| 44 | + |
| 45 | + |
| 46 | +def test_decode_nested_s3_event(): |
| 47 | + raw_event = load_event("s3SqsEvent.json") |
| 48 | + event = SQSEvent(raw_event) |
| 49 | + |
| 50 | + records = list(event.records) |
| 51 | + record = records[0] |
| 52 | + attributes = record.attributes |
| 53 | + |
| 54 | + assert len(records) == 1 |
| 55 | + assert record.message_id == raw_event["Records"][0]["messageId"] |
| 56 | + assert attributes.aws_trace_header is None |
| 57 | + raw_attributes = raw_event["Records"][0]["attributes"] |
| 58 | + assert attributes.approximate_receive_count == raw_attributes["ApproximateReceiveCount"] |
| 59 | + assert attributes.sent_timestamp == raw_attributes["SentTimestamp"] |
| 60 | + assert attributes.sender_id == raw_attributes["SenderId"] |
| 61 | + assert attributes.approximate_first_receive_timestamp == raw_attributes["ApproximateFirstReceiveTimestamp"] |
| 62 | + assert attributes.sequence_number is None |
| 63 | + assert attributes.message_group_id is None |
| 64 | + assert attributes.message_deduplication_id is None |
| 65 | + assert record.md5_of_body == raw_event["Records"][0]["md5OfBody"] |
| 66 | + assert record.event_source == raw_event["Records"][0]["eventSource"] |
| 67 | + assert record.event_source_arn == raw_event["Records"][0]["eventSourceARN"] |
| 68 | + assert record.aws_region == raw_event["Records"][0]["awsRegion"] |
| 69 | + |
| 70 | + s3_event: S3Event = record.decoded_nested_s3_event |
| 71 | + s3_record = s3_event.record |
| 72 | + raw_body = json.loads(raw_event["Records"][0]["body"]) |
| 73 | + |
| 74 | + assert s3_event.bucket_name == raw_body["Records"][0]["s3"]["bucket"]["name"] |
| 75 | + assert s3_event.object_key == raw_body["Records"][0]["s3"]["object"]["key"] |
| 76 | + raw_s3_record = raw_body["Records"][0] |
| 77 | + assert s3_record.aws_region == raw_s3_record["awsRegion"] |
| 78 | + assert s3_record.event_name == raw_s3_record["eventName"] |
| 79 | + assert s3_record.event_source == raw_s3_record["eventSource"] |
| 80 | + assert s3_record.event_time == raw_s3_record["eventTime"] |
| 81 | + assert s3_record.event_version == raw_s3_record["eventVersion"] |
| 82 | + assert s3_record.glacier_event_data is None |
| 83 | + assert s3_record.request_parameters.source_ip_address == raw_s3_record["requestParameters"]["sourceIPAddress"] |
| 84 | + assert s3_record.response_elements["x-amz-request-id"] == raw_s3_record["responseElements"]["x-amz-request-id"] |
| 85 | + assert s3_record.s3.s3_schema_version == raw_s3_record["s3"]["s3SchemaVersion"] |
| 86 | + assert s3_record.s3.bucket.arn == raw_s3_record["s3"]["bucket"]["arn"] |
| 87 | + assert s3_record.s3.bucket.name == raw_s3_record["s3"]["bucket"]["name"] |
| 88 | + assert ( |
| 89 | + s3_record.s3.bucket.owner_identity.principal_id == raw_s3_record["s3"]["bucket"]["ownerIdentity"]["principalId"] |
| 90 | + ) |
| 91 | + assert s3_record.s3.configuration_id == raw_s3_record["s3"]["configurationId"] |
| 92 | + assert s3_record.s3.get_object.etag == raw_s3_record["s3"]["object"]["eTag"] |
| 93 | + assert s3_record.s3.get_object.key == raw_s3_record["s3"]["object"]["key"] |
| 94 | + assert s3_record.s3.get_object.sequencer == raw_s3_record["s3"]["object"]["sequencer"] |
| 95 | + assert s3_record.s3.get_object.size == raw_s3_record["s3"]["object"]["size"] |
| 96 | + assert s3_record.s3.get_object.version_id == raw_s3_record["s3"]["object"]["versionId"] |
| 97 | + |
| 98 | + |
| 99 | +def test_decode_nested_sns_event(): |
| 100 | + raw_event = load_event("snsSqsEvent.json") |
| 101 | + event = SQSEvent(raw_event) |
| 102 | + |
| 103 | + records = list(event.records) |
| 104 | + record = records[0] |
| 105 | + attributes = record.attributes |
| 106 | + |
| 107 | + assert len(records) == 1 |
| 108 | + assert record.message_id == raw_event["Records"][0]["messageId"] |
| 109 | + raw_attributes = raw_event["Records"][0]["attributes"] |
| 110 | + assert attributes.aws_trace_header is None |
| 111 | + assert attributes.approximate_receive_count == raw_attributes["ApproximateReceiveCount"] |
| 112 | + assert attributes.sent_timestamp == raw_attributes["SentTimestamp"] |
| 113 | + assert attributes.sender_id == raw_attributes["SenderId"] |
| 114 | + assert attributes.approximate_first_receive_timestamp == raw_attributes["ApproximateFirstReceiveTimestamp"] |
| 115 | + assert attributes.sequence_number is None |
| 116 | + assert attributes.message_group_id is None |
| 117 | + assert attributes.message_deduplication_id is None |
| 118 | + assert record.md5_of_body == raw_event["Records"][0]["md5OfBody"] |
| 119 | + assert record.event_source == raw_event["Records"][0]["eventSource"] |
| 120 | + assert record.event_source_arn == raw_event["Records"][0]["eventSourceARN"] |
| 121 | + assert record.aws_region == raw_event["Records"][0]["awsRegion"] |
| 122 | + |
| 123 | + sns_message: SNSMessage = record.decoded_nested_sns_event |
| 124 | + raw_body = json.loads(raw_event["Records"][0]["body"]) |
| 125 | + message = json.loads(sns_message.message) |
| 126 | + |
| 127 | + assert sns_message.get_type == raw_body["Type"] |
| 128 | + assert sns_message.message_id == raw_body["MessageId"] |
| 129 | + assert sns_message.topic_arn == raw_body["TopicArn"] |
| 130 | + assert sns_message.timestamp == raw_body["Timestamp"] |
| 131 | + assert sns_message.signature_version == raw_body["SignatureVersion"] |
| 132 | + raw_message = json.loads(raw_body["Message"]) |
| 133 | + assert message["message"] == raw_message["message"] |
| 134 | + assert message["username"] == raw_message["username"] |
0 commit comments