def test_create_sns_message_timestamp_millis(self, subscriber): action = {"Message": ["msg"]} result_str = create_sns_message_body(subscriber, action) result = json.loads(result_str) timestamp = result.pop("Timestamp") end = timestamp[-5:] matcher = re.compile(r"\.[0-9]{3}Z") match = matcher.match(end) assert match
def test_create_sns_message_body_json_structure_sqs_protocol(self, subscriber): action = { "Message": ['{"default": "default message", "sqs": "sqs message"}'], "MessageStructure": ["json"], } result_str = create_sns_message_body(subscriber, action) result = json.loads(result_str) assert "sqs message" == result["Message"]
def test_create_sns_message_body_json_structure_raw_delivery(self, subscriber): subscriber["RawMessageDelivery"] = "true" action = { "Message": ['{"default": {"message": "abc"}}'], "MessageStructure": ["json"], } result = create_sns_message_body(subscriber, action) assert {"message": "abc"} == result
def test_create_sns_message_body_json_structure(self, subscriber): action = { "Message": ['{"default": {"message": "abc"}}'], "MessageStructure": ["json"], } result_str = create_sns_message_body(subscriber, action) result = json.loads(result_str) assert {"message": "abc"} == result["Message"]
def test_create_sns_message_body(self, subscriber): action = {"Message": ["msg"]} result_str = create_sns_message_body(subscriber, action, str(uuid.uuid4())) result = json.loads(result_str) try: uuid.UUID(result.pop("MessageId")) except KeyError: assert False, "MessageId missing in SNS response message body" except ValueError: assert False, "SNS response MessageId not a valid UUID" try: dateutil.parser.parse(result.pop("Timestamp")) except KeyError: assert False, "Timestamp missing in SNS response message body" except ValueError: assert False, "SNS response Timestamp not a valid ISO 8601 date" expected_sns_body = { "Message": "msg", "Signature": "EXAMPLEpH+..", "SignatureVersion": "1", "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem", "TopicArn": "arn", "Type": "Notification", "UnsubscribeURL": "http://localhost:4566/?Action=Unsubscribe&SubscriptionArn=arn", } assert expected_sns_body == result # Now add a subject and message attributes action = { "Message": ["msg"], "Subject": ["subject"], } message_attributes = { "attr1": { "DataType": "String", "StringValue": "value1", }, "attr2": { "DataType": "Binary", "BinaryValue": b"\x02\x03\x04", }, } result_str = create_sns_message_body( subscriber, action, str(uuid.uuid4()), message_attributes ) result = json.loads(result_str) del result["MessageId"] del result["Timestamp"] msg = { "Message": "msg", "Subject": "subject", "Signature": "EXAMPLEpH+..", "SignatureVersion": "1", "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem", "TopicArn": "arn", "Type": "Notification", "UnsubscribeURL": "http://localhost:4566/?Action=Unsubscribe&SubscriptionArn=arn", "MessageAttributes": { "attr1": { "Type": "String", "Value": "value1", }, "attr2": { "Type": "Binary", "Value": b64encode(b"\x02\x03\x04").decode("utf-8"), }, }, } assert msg == result
def test_create_sns_message_body_raw_message_delivery(self, subscriber): subscriber["RawMessageDelivery"] = "true" action = {"Message": ["msg"]} result = create_sns_message_body(subscriber, action) assert "msg" == result
def test_create_sns_message_body_json_structure_without_default_key(self, subscriber): action = {"Message": ['{"message": "abc"}'], "MessageStructure": ["json"]} with pytest.raises(Exception) as exc: create_sns_message_body(subscriber, action) assert "Unable to find 'default' key in message payload" == str(exc.value)