Example #1
0
def test_fail_to_deserialize_sns_message_without_required_attribute_set_to_null(attribute_name):
    data = build_sns_notification_data()
    data[attribute_name] = None

    with pytest.raises(SnsSchemaError) as error:
        deserialize_sns_message(data)

    assert str(error.value) == "Could not deserialize SNS message: " \
                               "{{'{0}': ['Field may not be null.']}}".format(attribute_name)
Example #2
0
def test_fail_to_deserialize_sns_message_without_required_attribute(attribute_name):
    data = build_sns_notification_data()
    del data[attribute_name]

    with pytest.raises(SnsSchemaError) as error:
        deserialize_sns_message(data)

    assert str(error.value) == "Could not deserialize SNS message: " \
                               "{{'{0}': ['Missing data for required field.']}}".format(attribute_name)
Example #3
0
def test_fail_to_deserialize_sns_message_with_unknown_type():
    data = build_sns_notification_data(Type='FooBar')

    with pytest.raises(SnsSchemaError) as error:
        deserialize_sns_message(data)

    assert str(error.value) == (
        "Could not deserialize SNS message: "
        "{'Type': ['Must be one of: Notification, SubscriptionConfirmation, UnsubscribeConfirmation.']}"
    )
Example #4
0
 def __build_sqs_message_with_embedded_sns_message(data, payload):
     sns_message = deserialize_sns_message(payload)
     return SqsMessage(message_id=data['message_id'],
                       receipt_handle=data['receipt_handle'],
                       payload=sns_message.payload,
                       payload_type=sns_message.payload_type,
                       embedded_message=sns_message)
Example #5
0
def test_deserialize_sns_notification_without_subject():
    data = build_sns_notification_data()
    del data['Subject']

    message = deserialize_sns_message(data)

    assert_is_sns_notification(message)
    assert message.subject is None
Example #6
0
def test_deserialize_sns_notification_with_mega_payload_over_plaintext_json():
    mega_data = build_mega_payload_data()
    mega_json = json.dumps(mega_data)
    data = build_sns_notification_data(Message=mega_json)

    message = deserialize_sns_message(data)

    assert_is_sns_notification(message)
    assert_sns_notification_matches_data_attributes(message, data)
    assert_has_mega_payload(message)
    assert_mega_payload_matches_data(message.payload, mega_data)
Example #7
0
def test_deserialize_sns_notification_with_plaintext_payload():
    plaintext = 'Hello world!'
    data = build_sns_notification_data(Message=plaintext)

    message = deserialize_sns_message(data)

    assert_is_sns_notification(message)
    assert_sns_notification_matches_data_attributes(message, data)

    assert message.payload == plaintext
    assert message.payload_type == PayloadType.PLAINTEXT
Example #8
0
def test_deserialize_sns_notification_with_mega_payload_over_binary_bson():
    mega_data = build_mega_payload_data()
    mega_bson = bson.dumps(mega_data)
    mega_blob = b64encode(mega_bson).decode()
    data = build_sns_notification_data(Message=mega_blob)

    message = deserialize_sns_message(data)

    assert_is_sns_notification(message)
    assert_sns_notification_matches_data_attributes(message, data)
    assert_has_mega_payload(message)
    assert_mega_payload_matches_data(message.payload, mega_data)
Example #9
0
def test_deserialize_sns_notification_with_generic_json_payload():
    payload_data = build_generic_json_data()
    payload_json = json.dumps(payload_data)
    data = build_sns_notification_data(Message=payload_json)

    message = deserialize_sns_message(data)

    assert_is_sns_notification(message)
    assert_sns_notification_matches_data_attributes(message, data)

    assert message.payload_type == PayloadType.DATA
    assert message.payload == payload_data
Example #10
0
def test_deserialize_sns_notification_with_base64_encoded_bson_payload():
    payload_data = build_generic_json_data()
    payload_blob = bson.dumps(payload_data)
    data = build_sns_notification_data(Message=b64encode(payload_blob).decode())

    message = deserialize_sns_message(data)

    assert_is_sns_notification(message)
    assert_sns_notification_matches_data_attributes(message, data)

    assert message.payload_type == PayloadType.DATA
    assert message.payload == payload_data
Example #11
0
def test_deserialize_sns_notification_with_base64_encoded_binary_payload():
    blob = (
        b"\x01\x02\x03\x00xQ\xc4\xf2QF\xbfw~W\x1b\xf1\xf1Nq\xff\xc0\x94\x84ov\x9a0\x1dC\xcf\xd2\x06r4\n\xe7m\x01\nQ{"
        b"wb6X\x9cz\xab\xb5\x04\x97\x8e\xdf^cr\x81\xb1\x83s\xf2\xb0\xa1[2\xd0\x9f|V\xb0\xc3"
    )
    data = build_sns_notification_data(Message=b64encode(blob).decode())

    message = deserialize_sns_message(data)

    assert_is_sns_notification(message)
    assert_sns_notification_matches_data_attributes(message, data)

    assert message.payload == blob
    assert message.payload_type == PayloadType.BINARY
Example #12
0
def test_deserialize_sns_unsubscribe_confirmation():
    data = build_sns_unsubscribe_confirmation_data()

    message = deserialize_sns_message(data)

    assert message is not None
    assert isinstance(message, SnsUnsubscribeConfirmation)
    assert message.message_type == MessageType.SNS
    assert message.sns_message_type == SnsMessageType.UNSUBSCRIBE_CONFIRMATION

    assert_sns_message_matches_data_attributes(message, data)

    assert message.payload is None
    assert message.payload_type == PayloadType.NONE
    assert message.token == data['Token']
    assert message.subscribe_url == data['SubscribeURL']
    assert message.raw_message == data['Message']