diff --git a/moto/sqs/responses.py b/moto/sqs/responses.py index b2725d577..9351e8220 100644 --- a/moto/sqs/responses.py +++ b/moto/sqs/responses.py @@ -202,8 +202,17 @@ class SQSResponse(BaseResponse): def set_queue_attributes(self): # TODO validate self.get_param('QueueUrl') + attribute = self.attribute + + # Fixes issue with Policy set to empty str + attribute_names = self._get_multi_param("Attribute") + if attribute_names: + for attr in attribute_names: + if attr["Name"] == "Policy" and len(attr["Value"]) == 0: + attribute = {attr["Name"]: None} + queue_name = self._get_queue_name() - self.sqs_backend.set_queue_attributes(queue_name, self.attribute) + self.sqs_backend.set_queue_attributes(queue_name, attribute) return SET_QUEUE_ATTRIBUTE_RESPONSE diff --git a/tests/test_sqs/test_sqs.py b/tests/test_sqs/test_sqs.py index fca108ada..b0d8cb79c 100644 --- a/tests/test_sqs/test_sqs.py +++ b/tests/test_sqs/test_sqs.py @@ -205,6 +205,31 @@ def test_create_queue_with_policy(): ) +@mock_sqs +def test_set_queue_attribute_empty_policy_removes_attr(): + client = boto3.client("sqs", region_name="us-east-1") + response = client.create_queue( + QueueName="test-queue", + Attributes={ + "Policy": json.dumps( + { + "Version": "2012-10-17", + "Id": "test", + "Statement": [{"Effect": "Allow", "Principal": "*", "Action": "*"}], + } + ) + }, + ) + queue_url = response["QueueUrl"] + + empty_policy = {"Policy": ""} + client.set_queue_attributes(QueueUrl=queue_url, Attributes=empty_policy) + response = client.get_queue_attributes(QueueUrl=queue_url, AttributeNames=["All"])[ + "Attributes" + ] + response.shouldnt.have.key("Policy") + + @mock_sqs def test_get_queue_url(): client = boto3.client("sqs", region_name="us-east-1")