diff --git a/moto/sqs/responses.py b/moto/sqs/responses.py index 0ddf5a31..f84d293d 100644 --- a/moto/sqs/responses.py +++ b/moto/sqs/responses.py @@ -439,6 +439,20 @@ class SQSResponse(BaseResponse): queue_name = self._get_queue_name() tags = self._get_map_prefix('Tag', key_end='.Key', value_end='.Value') + try: + self.sqs_backend.get_queue(queue_name) + except QueueDoesNotExist as e: + return self._error('AWS.SimpleQueueService.NonExistentQueue', + e.description) + + if len(tags) == 0: + return self._error('MissingParameter', + 'The request must contain the parameter Tags.') + + if len(tags) > 50: + return self._error('InvalidParameterValue', + 'Too many tags added for queue {}.'.format(queue_name)) + self.sqs_backend.tag_queue(queue_name, tags) template = self.response_template(TAG_QUEUE_RESPONSE) diff --git a/tests/test_sqs/test_sqs.py b/tests/test_sqs/test_sqs.py index 4d06411e..f5d09ba4 100644 --- a/tests/test_sqs/test_sqs.py +++ b/tests/test_sqs/test_sqs.py @@ -1108,6 +1108,53 @@ def test_tags(): }) +@mock_sqs +def test_tag_queue_errors(): + client = boto3.client('sqs', region_name='us-east-1') + + response = client.create_queue( + QueueName='test-queue-with-tags', + tags={ + 'tag_key_1': 'tag_value_X' + } + ) + queue_url = response['QueueUrl'] + + client.tag_queue.when.called_with( + QueueUrl=queue_url + '-not-existing', + Tags={ + 'tag_key_1': 'tag_value_1' + } + ).should.throw( + ClientError, + 'The specified queue does not exist for this wsdl version.' + ) + + client.tag_queue.when.called_with( + QueueUrl=queue_url, + Tags={} + ).should.throw( + ClientError, + 'The request must contain the parameter Tags.' + ) + + too_many_tags = {'tag_key_{}'.format(i): 'tag_value_{}'.format(i) for i in range(51)} + client.tag_queue.when.called_with( + QueueUrl=queue_url, + Tags=too_many_tags + ).should.throw( + ClientError, + 'Too many tags added for queue test-queue-with-tags.' + ) + + # when the request fails, the tags should not be updated + client.list_queue_tags(QueueUrl=queue_url)['Tags'].should.equal( + { + 'tag_key_1': 'tag_value_X' + } + ) + + @mock_sqs def test_untag_queue_errors(): client = boto3.client('sqs', region_name='us-east-1') @@ -1127,7 +1174,7 @@ def test_untag_queue_errors(): ] ).should.throw( ClientError, - "The specified queue does not exist for this wsdl version." + 'The specified queue does not exist for this wsdl version.' ) client.untag_queue.when.called_with(