import base64 import json from flexmock import flexmock from moto import mock_aws from owslogger import logger from ytownership.connectors import sqs TEST_CORRELATION_ID = 'c305d169-455b-4bdd-ab69-0c49c49ae4fc' TEST_RECEIVE_COUNT = 5 TEST_QUEUE = 'TEST_QUEUE' sqs_attributes_correlation_id = { sqs.CORRELATION_ID_ATTRIBUTE: { 'string_value': TEST_CORRELATION_ID } } sqs_attributes_receive_count = { sqs.APPROXIMATE_RECEIVE_COUNT_ATTRIBUTE: TEST_RECEIVE_COUNT } def test_get_connection(): """Test if get_connection() returns a valid connection object """ with mock_aws(): connection = sqs.get_connection() assert connection is not None def test_get_queue(): """Test if get_queue() returns a Queue """ with mock_aws(): connection = sqs.get_connection() connection.create_queue(QueueName=TEST_QUEUE) queue = sqs.get_queue(TEST_QUEUE, sqs_connection=connection) assert queue is not None def test_json_message_ext_context(): """Test if JSONMessageExt context can store and return a test value """ json_message = sqs.JSONMessageExt() json_message.context.test_value = 'test' assert json_message.context.test_value == 'test' def test_json_message_ext_correlation_id(): """Test if JSONMessageExt can return correlation_id """ mock_msg = flexmock( message_attributes={ 'Correlation-Id': {'StringValue': TEST_CORRELATION_ID}}) json_message = sqs.JSONMessageExt(mock_msg) assert json_message.correlation_id == TEST_CORRELATION_ID def test_json_message_ext_no_correlation_id(): """Test if JSONMessageExt can return None for message with empty correlation_id header """ mock_msg = flexmock(message_attributes={}) json_message = sqs.JSONMessageExt(mock_msg) assert json_message.correlation_id is None def test_json_message_ext_logger(): """Test if JSONMessageExt can return OwsLoggingAdapter """ mock_msg = flexmock( message_attributes={'Correlation-Id': {'StringValue': '1234'}}) json_message = sqs.JSONMessageExt(mock_msg) assert isinstance(json_message.logger, logger.OwsLoggingAdapter) def test_json_message_ext_receive_count(): """Test if JSONMessageExt can return approximate receive count """ mock_msg = flexmock(attributes={'ApproximateReceiveCount': 5}) json_message = sqs.JSONMessageExt(mock_msg) assert json_message.receive_count == TEST_RECEIVE_COUNT def test_sqs_message_body(): """Test if message body is received decoded """ with mock_aws(): connection = sqs.get_connection() connection.create_queue(QueueName=TEST_QUEUE) queue = sqs.get_queue(TEST_QUEUE, sqs_connection=connection) message = { 'content_owner_id': 'theorchardmusic', 'isrc': 'US26V4410006', 'territories': ['US'] } message_body = json.dumps(message) message_body = base64.b64encode( message_body.encode('utf-8')).decode('utf-8') queue.send_message(MessageBody=message_body) res_message = queue.receive_messages() json_message = sqs.JSONMessageExt(res_message[0]) assert json_message.get_body() == message