Best Python code snippet using localstack_python
test_sns.py
Source:test_sns.py
...21class TestSns:22 def test_create_sns_message_body_raw_message_delivery(self, subscriber):23 subscriber["RawMessageDelivery"] = "true"24 action = {"Message": ["msg"]}25 result = create_sns_message_body(subscriber, action)26 assert "msg" == result27 def test_create_sns_message_body(self, subscriber):28 action = {"Message": ["msg"]}29 result_str = create_sns_message_body(subscriber, action, str(uuid.uuid4()))30 result = json.loads(result_str)31 try:32 uuid.UUID(result.pop("MessageId"))33 except KeyError:34 assert False, "MessageId missing in SNS response message body"35 except ValueError:36 assert False, "SNS response MessageId not a valid UUID"37 try:38 dateutil.parser.parse(result.pop("Timestamp"))39 except KeyError:40 assert False, "Timestamp missing in SNS response message body"41 except ValueError:42 assert False, "SNS response Timestamp not a valid ISO 8601 date"43 expected_sns_body = {44 "Message": "msg",45 "Signature": "EXAMPLEpH+..",46 "SignatureVersion": "1",47 "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem",48 "TopicArn": "arn",49 "Type": "Notification",50 "UnsubscribeURL": "http://localhost:4566/?Action=Unsubscribe&SubscriptionArn=arn",51 }52 assert expected_sns_body == result53 # Now add a subject and message attributes54 action = {55 "Message": ["msg"],56 "Subject": ["subject"],57 }58 message_attributes = {59 "attr1": {60 "DataType": "String",61 "StringValue": "value1",62 },63 "attr2": {64 "DataType": "Binary",65 "BinaryValue": b"\x02\x03\x04",66 },67 }68 result_str = create_sns_message_body(69 subscriber, action, str(uuid.uuid4()), message_attributes70 )71 result = json.loads(result_str)72 del result["MessageId"]73 del result["Timestamp"]74 msg = {75 "Message": "msg",76 "Subject": "subject",77 "Signature": "EXAMPLEpH+..",78 "SignatureVersion": "1",79 "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem",80 "TopicArn": "arn",81 "Type": "Notification",82 "UnsubscribeURL": "http://localhost:4566/?Action=Unsubscribe&SubscriptionArn=arn",83 "MessageAttributes": {84 "attr1": {85 "Type": "String",86 "Value": "value1",87 },88 "attr2": {89 "Type": "Binary",90 "Value": b64encode(b"\x02\x03\x04").decode("utf-8"),91 },92 },93 }94 assert msg == result95 def test_create_sns_message_body_json_structure(self, subscriber):96 action = {97 "Message": ['{"default": {"message": "abc"}}'],98 "MessageStructure": ["json"],99 }100 result_str = create_sns_message_body(subscriber, action)101 result = json.loads(result_str)102 assert {"message": "abc"} == result["Message"]103 def test_create_sns_message_body_json_structure_raw_delivery(self, subscriber):104 subscriber["RawMessageDelivery"] = "true"105 action = {106 "Message": ['{"default": {"message": "abc"}}'],107 "MessageStructure": ["json"],108 }109 result = create_sns_message_body(subscriber, action)110 assert {"message": "abc"} == result111 def test_create_sns_message_body_json_structure_without_default_key(self, subscriber):112 action = {"Message": ['{"message": "abc"}'], "MessageStructure": ["json"]}113 with pytest.raises(Exception) as exc:114 create_sns_message_body(subscriber, action)115 assert "Unable to find 'default' key in message payload" == str(exc.value)116 def test_create_sns_message_body_json_structure_sqs_protocol(self, subscriber):117 action = {118 "Message": ['{"default": "default message", "sqs": "sqs message"}'],119 "MessageStructure": ["json"],120 }121 result_str = create_sns_message_body(subscriber, action)122 result = json.loads(result_str)123 assert "sqs message" == result["Message"]124 def test_create_sns_message_body_json_structure_raw_delivery_sqs_protocol(self, subscriber):125 subscriber["RawMessageDelivery"] = "true"126 action = {127 "Message": [128 '{"default": {"message": "default version"}, "sqs": {"message": "sqs version"}}'129 ],130 "MessageStructure": ["json"],131 }132 result = create_sns_message_body(subscriber, action)133 assert {"message": "sqs version"} == result134 def test_create_sns_message_timestamp_millis(self, subscriber):135 action = {"Message": ["msg"]}136 result_str = create_sns_message_body(subscriber, action)137 result = json.loads(result_str)138 timestamp = result.pop("Timestamp")139 end = timestamp[-5:]140 matcher = re.compile(r"\.[0-9]{3}Z")141 match = matcher.match(end)142 assert match143 def test_filter_policy(self):144 test_data = [145 ("no filter with no attributes", {}, {}, True),146 (147 "no filter with attributes",148 {},149 {"filter": {"Type": "String", "Value": "type1"}},150 True,...
test_sns_listener.py
Source:test_sns_listener.py
...7 }8 action = {9 'Message': ['msg']10 }11 result = sns_listener.create_sns_message_body(subscriber, action)12 assert (result == 'msg')13def test_create_sns_message_body():14 subscriber = {15 'TopicArn': 'arn',16 'RawMessageDelivery': 'false',17 }18 action = {19 'Message': ['msg']20 }21 result = sns_listener.create_sns_message_body(subscriber, action)22 assert_equal(json.loads(result), {'Message': 'msg', 'Type': 'Notification', 'TopicArn': 'arn'})23 # Now add a subject24 action = {25 'Message': ['msg'],26 'Subject': ['subject'],27 'MessageAttributes.entry.1.Name': ['attr1'],28 'MessageAttributes.entry.1.Value.DataType': ['String'],29 'MessageAttributes.entry.1.Value.StringValue': ['value1'],30 'MessageAttributes.entry.1.Value.BinaryValue': ['value1'],31 'MessageAttributes.entry.2.Name': ['attr2'],32 'MessageAttributes.entry.2.Value.DataType': ['String'],33 'MessageAttributes.entry.2.Value.StringValue': ['value2'],34 'MessageAttributes.entry.2.Value.BinaryValue': ['value2'],35 }36 result = sns_listener.create_sns_message_body(subscriber, action)37 expected = json.dumps({'Message': 'msg',38 'TopicArn': 'arn',39 'Type': 'Notification',40 'Subject': 'subject',41 'MessageAttributes': {42 'attr1': {43 'Type': 'String',44 'Value': 'value1',45 }, 'attr2': {46 'Type': 'String',47 'Value': 'value2',48 }49 }})50 assert_equal(json.loads(result), json.loads(expected))
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!