import base64 import json import uuid from moto import mock_aws import boto3 from flexmock import flexmock from masters_registry.connectors import sqs from masters_registry.constant import field_const from masters_registry.constant import sqs_const from masters_registry.models import yt_ownership from masters_registry import config sqs_resource = sqs.sqs_resource sqs_client = boto3.client('sqs') @mock_aws def test_send_message(): """Assert that sqs contains expected message after sending """ from moto.core import patch_client, patch_resource patch_client(sqs_client) patch_resource(sqs_resource) sqs_client.create_queue(QueueName=config.QUEUE_NAME_YT_OWNERSHIP) queue_yt_ownership = sqs_resource.get_queue_by_name( QueueName=config.QUEUE_NAME_YT_OWNERSHIP) flexmock(yt_ownership).should_receive('sqs.queue_yt_ownership').and_return(queue_yt_ownership) # send a message to the queue test_isrc = 'TESTISRC' test_territories = ['US', 'CA'] test_correlation_id = str(uuid.uuid1()) yt_ownership.send_message( isrc=test_isrc, territories=test_territories, correlation_id=test_correlation_id) # verify the message is in the queue messages = sqs.queue_yt_ownership.receive_messages( MaxNumberOfMessages=10, MessageAttributeNames=['All']) assert 1 == len(messages) message_body = base64.b64decode( messages[0].body.encode('utf-8')).decode('utf-8') message_body = json.loads(message_body) message_attributes = messages[0].message_attributes assert test_isrc == message_body[field_const.ISRC] assert set(test_territories) == set( message_body[field_const.TERRITORIES]) assert test_correlation_id == message_attributes[ field_const.CORRELATION_ID][sqs_const.STRING_VALUE]