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)
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())
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)
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() }, )
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}')