Skip to content

Commit

Permalink
sns: add multi-account capability to failing tests (localstack#9358)
Browse files Browse the repository at this point in the history
  • Loading branch information
sannya-singal authored Oct 20, 2023
1 parent 030e879 commit 05a52a1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
22 changes: 20 additions & 2 deletions localstack/services/sns/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,26 @@ def create_platform_endpoint(
return result

def unsubscribe(self, context: RequestContext, subscription_arn: subscriptionARN) -> None:
call_moto(context)
store = self.get_store(account_id=context.account_id, region_name=context.region)
count = len(subscription_arn.split(":"))
try:
parsed_arn = parse_arn(subscription_arn)
except InvalidArnException:
# TODO: check for invalid SubscriptionGUID
if count < 6:
raise InvalidParameterException(
f"Invalid parameter: SubscriptionArn Reason: An ARN must have at least 6 elements, not {count}"
)

account_id = parsed_arn["account"]
region_name = parsed_arn["region"]

store = self.get_store(account_id=account_id, region_name=region_name)

if count == 6 and subscription_arn not in store.topic_subscriptions:
raise InvalidParameterException("Invalid parameter: SubscriptionId")

moto_sns_backend = self.get_moto_backend(account_id, region_name)
moto_sns_backend.unsubscribe(subscription_arn)

# pop the subscription at the end, to avoid race condition by iterating over the topic subscriptions
subscription = store.subscriptions.get(subscription_arn)
Expand Down
20 changes: 18 additions & 2 deletions tests/aws/services/sns/test_sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from werkzeug import Response

from localstack import config
from localstack.aws.accounts import get_aws_account_id
from localstack.aws.api.lambda_ import Runtime
from localstack.constants import (
AWS_REGION_US_EAST_1,
Expand Down Expand Up @@ -178,7 +177,7 @@ def test_create_topic_test_arn(self, sns_create_topic, snapshot, aws_client):
assert topic_arn_params[5] == topic_name

if not is_aws_cloud():
assert topic_arn_params[4] == get_aws_account_id()
assert topic_arn_params[4] == TEST_AWS_ACCOUNT_ID

topic_attrs = aws_client.sns.get_topic_attributes(TopicArn=topic_arn)
snapshot.match("get-topic-attrs", topic_attrs)
Expand Down Expand Up @@ -229,6 +228,23 @@ def test_create_topic_after_delete_with_new_tags(self, sns_create_topic, snapsho
topic1 = sns_create_topic(Name=topic_name, Tags=[{"Key": "Name", "Value": "abc"}])
snapshot.match("topic-1", topic1)

@markers.aws.validated
def test_unsubscribe_wrong_arn_format(self, snapshot, aws_client):
with pytest.raises(ClientError) as e:
aws_client.sns.unsubscribe(SubscriptionArn="randomstring")

snapshot.match("invalid-unsubscribe-arn-1", e.value.response)

with pytest.raises(ClientError) as e:
aws_client.sns.unsubscribe(SubscriptionArn="arn:aws:sns:us-east-1:random")

snapshot.match("invalid-unsubscribe-arn-2", e.value.response)

with pytest.raises(ClientError) as e:
aws_client.sns.unsubscribe(SubscriptionArn="arn:aws:sns:us-east-1:111111111111:random")

snapshot.match("invalid-unsubscribe-arn-3", e.value.response)


class TestSNSPublishCrud:
"""
Expand Down
38 changes: 38 additions & 0 deletions tests/aws/services/sns/test_sns.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -4673,5 +4673,43 @@
}
}
}
},
"tests/aws/services/sns/test_sns.py::TestSNSTopicCrud::test_unsubscribe_wrong_arn_format": {
"recorded-date": "20-10-2023, 12:52:36",
"recorded-content": {
"invalid-unsubscribe-arn-1": {
"Error": {
"Code": "InvalidParameter",
"Message": "Invalid parameter: SubscriptionArn Reason: An ARN must have at least 6 elements, not 1",
"Type": "Sender"
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 400
}
},
"invalid-unsubscribe-arn-2": {
"Error": {
"Code": "InvalidParameter",
"Message": "Invalid parameter: SubscriptionArn Reason: An ARN must have at least 6 elements, not 5",
"Type": "Sender"
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 400
}
},
"invalid-unsubscribe-arn-3": {
"Error": {
"Code": "InvalidParameter",
"Message": "Invalid parameter: SubscriptionId",
"Type": "Sender"
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 400
}
}
}
}
}

0 comments on commit 05a52a1

Please sign in to comment.