예제 #1
0
def test_fail_to_deserialize_sns_message_without_required_attribute_set_to_null(
        attribute_name):
    data = build_sqs_message_data()
    data[attribute_name] = None

    with pytest.raises(SqsSchemaError) as error:
        deserialize_sqs_message(data)

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

    with pytest.raises(SqsSchemaError) as error:
        deserialize_sqs_message(data)

    assert str(error.value) == "Could not deserialize SQS message: " \
                               "{{'{0}': ['Missing data for required field.']}}".format(attribute_name)
예제 #3
0
def test_receive_many_messages_with_mixed_payloads(queue_url):
    sqs = SqsReceiver(queue_url=queue_url, max_number_of_messages=10)

    with vcr.use_cassette(
            'receive_many_messages_with_mixed_payloads') as cassette:
        messages = sqs.receive_messages()

    assert cassette.all_played
    response = get_sqs_receive_message_response_data(cassette)

    assert len(messages) == len(response['Message']) == 7

    for i, message in enumerate(messages):
        assert_is_sqs_message(message)

        message_from_response = response['Message'][i]

        assert message.message_id == message_from_response['MessageId']
        assert message.receipt_handle == message_from_response['ReceiptHandle']

        deserialized_response = deserialize_sqs_message(message_from_response)
        assert message.payload == deserialized_response.payload
        assert message.payload_type == deserialized_response.payload_type

    assert_request_match_sqs_receiver_attributes(cassette, sqs)
예제 #4
0
def test_deserialize_sqs_message_with_binary_payload():
    blob = b"\x9cz\xab\xb5\x04\x97\x8e\xdf^cr\x81\xb1\x83s\xf2\xb0\xa1[2\xd0\x9f|V\xb0\xc3"

    sqs_data = build_sqs_message_data(Body=b64encode(blob).decode())
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    assert sqs_message.payload_type == PayloadType.BINARY
    assert sqs_message.payload == blob
    assert sqs_message.embedded_message is None
예제 #5
0
def test_deserialize_sqs_message_with_plaintext_payload():
    plaintext = '{"the quick brown fox jumps overt the lazy dog!"}'

    sqs_data = build_sqs_message_data(Body=plaintext)
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    assert sqs_message.payload_type == PayloadType.PLAINTEXT
    assert sqs_message.payload == plaintext
    assert sqs_message.embedded_message is None
예제 #6
0
def test_deserialize_sqs_message_with_mega_payload_encoded_as_binary_bson():
    mega_payload = build_mega_payload_data()
    blob = bson.dumps(mega_payload)

    sqs_data = build_sqs_message_data(Body=b64encode(blob).decode())
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    assert sqs_message.payload_type == PayloadType.MEGA
    assert sqs_message.payload == deserialize_payload(mega_payload)
    assert sqs_message.embedded_message is None
예제 #7
0
def test_deserialize_sqs_message_with_mega_payload_encoded_as_plaintext_json():
    mega_payload = build_mega_payload_data()
    plaintext = json.dumps(mega_payload)

    sqs_data = build_sqs_message_data(Body=plaintext)
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    assert sqs_message.payload_type == PayloadType.MEGA
    assert sqs_message.payload == deserialize_payload(mega_payload)
    assert sqs_message.embedded_message is None
예제 #8
0
def test_deserialize_sqs_message_with_data_payload_encoded_as_plaintext_json():
    payload_data = build_generic_json_data()
    plaintext = json.dumps(payload_data)

    sqs_data = build_sqs_message_data(Body=plaintext)
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    assert sqs_message.payload_type == PayloadType.DATA
    assert sqs_message.payload == payload_data
    assert sqs_message.embedded_message is None
예제 #9
0
def test_deserialize_sqs_message_with_embedded_sns_message_having_data_payload_encoded_as_plaintext_json(
):
    payload_data = build_generic_json_data()
    sns_data = build_sns_notification_data(Message=json.dumps(payload_data))
    sns_json = json.dumps(sns_data)

    sqs_data = build_sqs_message_data(Body=sns_json)
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    assert sqs_message.payload_type == PayloadType.DATA
    assert sqs_message.payload == payload_data

    assert_sqs_message_has_embedded_sns_notification(sqs_message)
    assert sqs_message.embedded_message.payload_type == PayloadType.DATA
    assert sqs_message.embedded_message.payload == payload_data
예제 #10
0
def test_deserialize_sqs_message_with_embedded_sns_message_having_plaintext_payload(
):
    plaintext = 'foo-bar #132! {}'
    sns_data = build_sns_notification_data(Message=plaintext)
    sns_json = json.dumps(sns_data)

    sqs_data = build_sqs_message_data(Body=sns_json)
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    assert sqs_message.payload_type == PayloadType.PLAINTEXT
    assert sqs_message.payload == plaintext

    assert_sqs_message_has_embedded_sns_notification(sqs_message)
    assert sqs_message.embedded_message.payload_type == PayloadType.PLAINTEXT
    assert sqs_message.embedded_message.payload == plaintext
예제 #11
0
def test_deserialize_sqs_message_with_embedded_sns_message_having_mega_payload_encoded_as_binary_bson(
):
    mega_payload_data = build_mega_payload_data()
    mega_payload_blob = bson.dumps(mega_payload_data)

    sns_data = build_sns_notification_data(
        Message=b64encode(mega_payload_blob).decode())
    sns_json = json.dumps(sns_data)

    sqs_data = build_sqs_message_data(Body=sns_json)
    sqs_message = deserialize_sqs_message(sqs_data)

    assert_sqs_message_matches_data_attributes(sqs_message, sqs_data)

    expected_mega_payload = deserialize_payload(mega_payload_data)

    assert sqs_message.payload_type == PayloadType.MEGA
    assert sqs_message.payload == expected_mega_payload

    assert_sqs_message_has_embedded_sns_notification(sqs_message)
    assert sqs_message.embedded_message.payload_type == PayloadType.MEGA
    assert sqs_message.embedded_message.payload == expected_mega_payload
예제 #12
0
 def __extract_messages(self, queue_url, response):
     messages = []
     for data in response['Messages']:
         self.__log_message_data(queue_url, data)
         messages.append(deserialize_sqs_message(data))
     return messages