示例#1
0
def test_default_headers_hook(mock_publish_over_sns, mock_convert_to_json,
                              message, settings):
    settings.HEDWIG_DEFAULT_HEADERS = 'tests.test_publisher.default_headers_hook'
    default_headers_hook.return_value = {
        'mickey': 'mouse',
    }

    message.validate()

    sns_id = str(uuid.uuid4())

    mock_publish_over_sns.return_value = {'MessageId': sns_id}

    publish(message)

    topic = _get_sns_topic(message)
    mock_publish_over_sns.assert_called_once_with(
        topic,
        mock_convert_to_json.return_value,
        {
            **message.headers,
            **default_headers_hook.return_value
        },
    )
    expected = message.as_dict()
    expected['metadata']['headers'].update(default_headers_hook.return_value)
    mock_convert_to_json.assert_called_once_with(expected)

    default_headers_hook.assert_called_once_with(message=message)
示例#2
0
def test_publish(mock_publish_over_sns, mock_convert_to_json, message):
    message.validate()

    sns_id = str(uuid.uuid4())

    mock_publish_over_sns.return_value = {'MessageId': sns_id}

    publish(message)

    topic = _get_sns_topic(message)
    mock_publish_over_sns.assert_called_once_with(
        topic, mock_convert_to_json.return_value, message.headers)
    mock_convert_to_json.assert_called_once_with(message.as_dict())
示例#3
0
def test_pre_serialize_hook(mock_publish_over_sns, mock_convert_to_json,
                            message, settings):
    settings.HEDWIG_PRE_SERIALIZE_HOOK = 'tests.test_publisher.pre_serialize_hook'

    message.validate()

    sns_id = str(uuid.uuid4())

    mock_publish_over_sns.return_value = {'MessageId': sns_id}

    publish(message)

    topic = _get_sns_topic(message)
    mock_publish_over_sns.assert_called_once_with(
        topic, mock_convert_to_json.return_value, message.headers)
    message_data = message.as_dict()
    message_data['metadata']['headers'].clear()
    mock_convert_to_json.assert_called_once_with(message_data)
示例#4
0
def test__publish_over_sns(mock_get_sns_client, message):
    message.validate()
    topic = _get_sns_topic(message)
    message_json = _convert_to_json(message.as_dict())

    _publish_over_sns(topic, message_json, message.headers)

    mock_get_sns_client.assert_called_once_with()
    mock_get_sns_client.return_value.publish.assert_called_once_with(
        TopicArn=topic,
        Message=message_json,
        MessageAttributes={
            k: {
                'DataType': 'String',
                'StringValue': str(v)
            }
            for k, v in message.headers.items()
        },
    )
示例#5
0
def test__get_sns_topic(message):
    message.validate()
    assert (
        _get_sns_topic(message) ==
        f'arn:aws:sns:{settings.AWS_REGION}:{settings.AWS_ACCOUNT_ID}:hedwig-'
        f'{message.topic}')