|
| 1 | +from typing import Any, List |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from aws_lambda_powertools.utilities.parser import ValidationError, envelopes, event_parser |
| 6 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 7 | +from tests.functional.parser.schemas import MyAdvancedSnsBusiness, MySnsBusiness |
| 8 | +from tests.functional.parser.utils import load_event |
| 9 | +from tests.functional.validator.conftest import sns_event # noqa: F401 |
| 10 | + |
| 11 | + |
| 12 | +@event_parser(model=MySnsBusiness, envelope=envelopes.SnsEnvelope) |
| 13 | +def handle_sns_json_body(event: List[MySnsBusiness], _: LambdaContext): |
| 14 | + assert len(event) == 1 |
| 15 | + assert event[0].message == "hello world" |
| 16 | + assert event[0].username == "lessa" |
| 17 | + |
| 18 | + |
| 19 | +def test_handle_sns_trigger_event_json_body(sns_event): # noqa: F811 |
| 20 | + handle_sns_json_body(sns_event, LambdaContext()) |
| 21 | + |
| 22 | + |
| 23 | +def test_validate_event_does_not_conform_with_model(): |
| 24 | + event: Any = {"invalid": "event"} |
| 25 | + |
| 26 | + with pytest.raises(ValidationError): |
| 27 | + handle_sns_json_body(event, LambdaContext()) |
| 28 | + |
| 29 | + |
| 30 | +def test_validate_event_does_not_conform_user_json_string_with_model(): |
| 31 | + event: Any = { |
| 32 | + "Records": [ |
| 33 | + { |
| 34 | + "EventVersion": "1.0", |
| 35 | + "EventSubscriptionArn": "arn:aws:sns:us-east-2:123456789012:sns-la ...", |
| 36 | + "EventSource": "aws:sns", |
| 37 | + "Sns": { |
| 38 | + "SignatureVersion": "1", |
| 39 | + "Timestamp": "2019-01-02T12:45:07.000Z", |
| 40 | + "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==", |
| 41 | + "SigningCertUrl": "https://sns.us-east-2.amazonaws.com/SimpleNotificat ...", |
| 42 | + "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", |
| 43 | + "Message": "not a valid JSON!", |
| 44 | + "MessageAttributes": {"Test": {"Type": "String", "Value": "TestString"}}, |
| 45 | + "Type": "Notification", |
| 46 | + "UnsubscribeUrl": "https://sns.us-east-2.amazonaws.com/?Action=Unsubscri ...", |
| 47 | + "TopicArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda", |
| 48 | + "Subject": "TestInvoke", |
| 49 | + }, |
| 50 | + } |
| 51 | + ] |
| 52 | + } |
| 53 | + |
| 54 | + with pytest.raises(ValidationError): |
| 55 | + handle_sns_json_body(event, LambdaContext()) |
| 56 | + |
| 57 | + |
| 58 | +@event_parser(model=MyAdvancedSnsBusiness) |
| 59 | +def handle_sns_no_envelope(event: MyAdvancedSnsBusiness, _: LambdaContext): |
| 60 | + records = event.Records |
| 61 | + record = records[0] |
| 62 | + |
| 63 | + assert len(records) == 1 |
| 64 | + assert record.EventVersion == "1.0" |
| 65 | + assert record.EventSubscriptionArn == "arn:aws:sns:us-east-2:123456789012:sns-la ..." |
| 66 | + assert record.EventSource == "aws:sns" |
| 67 | + assert record.Sns.Type == "Notification" |
| 68 | + assert record.Sns.UnsubscribeUrl.scheme == "https" |
| 69 | + assert record.Sns.UnsubscribeUrl.host == "sns.us-east-2.amazonaws.com" |
| 70 | + assert record.Sns.UnsubscribeUrl.query == "Action=Unsubscribe" |
| 71 | + assert record.Sns.TopicArn == "arn:aws:sns:us-east-2:123456789012:sns-lambda" |
| 72 | + assert record.Sns.Subject == "TestInvoke" |
| 73 | + assert record.Sns.SignatureVersion == "1" |
| 74 | + convert_time = int(round(record.Sns.Timestamp.timestamp() * 1000)) |
| 75 | + assert convert_time == 1546433107000 |
| 76 | + assert record.Sns.Signature == "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==" |
| 77 | + assert record.Sns.SigningCertUrl.host == "sns.us-east-2.amazonaws.com" |
| 78 | + assert record.Sns.SigningCertUrl.scheme == "https" |
| 79 | + assert record.Sns.SigningCertUrl.host == "sns.us-east-2.amazonaws.com" |
| 80 | + assert record.Sns.SigningCertUrl.path == "/SimpleNotification" |
| 81 | + assert record.Sns.MessageId == "95df01b4-ee98-5cb9-9903-4c221d41eb5e" |
| 82 | + assert record.Sns.Message == "Hello from SNS!" |
| 83 | + attrib_dict = record.Sns.MessageAttributes |
| 84 | + assert len(attrib_dict) == 2 |
| 85 | + assert attrib_dict["Test"].Type == "String" |
| 86 | + assert attrib_dict["Test"].Value == "TestString" |
| 87 | + assert attrib_dict["TestBinary"].Type == "Binary" |
| 88 | + assert attrib_dict["TestBinary"].Value == "TestBinary" |
| 89 | + |
| 90 | + |
| 91 | +def test_handle_sns_trigger_event_no_envelope(): |
| 92 | + event_dict = load_event("snsEvent.json") |
| 93 | + handle_sns_no_envelope(event_dict, LambdaContext()) |
0 commit comments