SNS: allow SetSubscriptionAttributes to unset FilterPolicy (#7251)
This commit is contained in:
parent
6c7dff6af6
commit
a141d29c97
@ -732,8 +732,17 @@ class SNSBackend(BaseBackend):
|
|||||||
raise SNSNotFoundError(
|
raise SNSNotFoundError(
|
||||||
"Subscription does not exist", template="wrapped_single_error"
|
"Subscription does not exist", template="wrapped_single_error"
|
||||||
)
|
)
|
||||||
|
# AWS does not return the FilterPolicy scope if the FilterPolicy is not set
|
||||||
|
# if the FilterPolicy is set and not the FilterPolicyScope, it returns the default value
|
||||||
|
attributes = {**subscription.attributes}
|
||||||
|
if "FilterPolicyScope" in attributes and not attributes.get("FilterPolicy"):
|
||||||
|
attributes.pop("FilterPolicyScope", None)
|
||||||
|
attributes.pop("FilterPolicy", None)
|
||||||
|
|
||||||
return subscription.attributes
|
elif "FilterPolicy" in attributes and "FilterPolicyScope" not in attributes:
|
||||||
|
attributes["FilterPolicyScope"] = "MessageAttributes"
|
||||||
|
|
||||||
|
return attributes
|
||||||
|
|
||||||
def set_subscription_attributes(self, arn: str, name: str, value: Any) -> None:
|
def set_subscription_attributes(self, arn: str, name: str, value: Any) -> None:
|
||||||
if name not in [
|
if name not in [
|
||||||
@ -753,6 +762,7 @@ class SNSBackend(BaseBackend):
|
|||||||
subscription = _subscription[0]
|
subscription = _subscription[0]
|
||||||
|
|
||||||
if name == "FilterPolicy":
|
if name == "FilterPolicy":
|
||||||
|
if value:
|
||||||
filter_policy = json.loads(value)
|
filter_policy = json.loads(value)
|
||||||
# we validate the filter policy differently depending on the scope
|
# we validate the filter policy differently depending on the scope
|
||||||
# we need to always set the scope first
|
# we need to always set the scope first
|
||||||
@ -762,6 +772,9 @@ class SNSBackend(BaseBackend):
|
|||||||
subscription._filter_policy_matcher = FilterPolicyMatcher(
|
subscription._filter_policy_matcher = FilterPolicyMatcher(
|
||||||
filter_policy, filter_policy_scope
|
filter_policy, filter_policy_scope
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
subscription._filter_policy = None
|
||||||
|
subscription._filter_policy_matcher = None
|
||||||
|
|
||||||
subscription.attributes[name] = value
|
subscription.attributes[name] = value
|
||||||
|
|
||||||
|
@ -393,6 +393,7 @@ def test_set_subscription_attributes():
|
|||||||
assert attrs["Attributes"]["RawMessageDelivery"] == "true"
|
assert attrs["Attributes"]["RawMessageDelivery"] == "true"
|
||||||
assert attrs["Attributes"]["DeliveryPolicy"] == delivery_policy
|
assert attrs["Attributes"]["DeliveryPolicy"] == delivery_policy
|
||||||
assert attrs["Attributes"]["FilterPolicy"] == filter_policy
|
assert attrs["Attributes"]["FilterPolicy"] == filter_policy
|
||||||
|
assert attrs["Attributes"]["FilterPolicyScope"] == "MessageAttributes"
|
||||||
|
|
||||||
filter_policy_scope = "MessageBody"
|
filter_policy_scope = "MessageBody"
|
||||||
conn.set_subscription_attributes(
|
conn.set_subscription_attributes(
|
||||||
@ -405,6 +406,17 @@ def test_set_subscription_attributes():
|
|||||||
|
|
||||||
assert attrs["Attributes"]["FilterPolicyScope"] == filter_policy_scope
|
assert attrs["Attributes"]["FilterPolicyScope"] == filter_policy_scope
|
||||||
|
|
||||||
|
# test unsetting a filter policy
|
||||||
|
conn.set_subscription_attributes(
|
||||||
|
SubscriptionArn=subscription_arn,
|
||||||
|
AttributeName="FilterPolicy",
|
||||||
|
AttributeValue="",
|
||||||
|
)
|
||||||
|
|
||||||
|
attrs = conn.get_subscription_attributes(SubscriptionArn=subscription_arn)
|
||||||
|
assert "FilterPolicy" not in attrs["Attributes"]
|
||||||
|
assert "FilterPolicyScope" not in attrs["Attributes"]
|
||||||
|
|
||||||
# not existing subscription
|
# not existing subscription
|
||||||
with pytest.raises(ClientError):
|
with pytest.raises(ClientError):
|
||||||
conn.set_subscription_attributes(
|
conn.set_subscription_attributes(
|
||||||
@ -413,7 +425,7 @@ def test_set_subscription_attributes():
|
|||||||
AttributeValue="true",
|
AttributeValue="true",
|
||||||
)
|
)
|
||||||
with pytest.raises(ClientError):
|
with pytest.raises(ClientError):
|
||||||
attrs = conn.get_subscription_attributes(SubscriptionArn="invalid")
|
conn.get_subscription_attributes(SubscriptionArn="invalid")
|
||||||
|
|
||||||
# invalid attr name
|
# invalid attr name
|
||||||
with pytest.raises(ClientError):
|
with pytest.raises(ClientError):
|
||||||
|
Loading…
Reference in New Issue
Block a user