Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(sns): add FIFO topic support with dedup, sequencing, and subscription validation (#279)
Yiannis Skarparisea8025853ee9
diff --git a/README.md b/README.mdindex e94d43d4..089161e9 100644--- a/README.md+++ b/README.md@@ -311,7 +311,7 @@ subnet = ec2.create_subnet(|---------|-----------|-------|| **S3** | CreateBucket, DeleteBucket, ListBuckets, HeadBucket, PutObject, GetObject, DeleteObject, HeadObject, CopyObject, ListObjects v1/v2, DeleteObjects, GetBucketVersioning, PutBucketVersioning, GetBucketEncryption, PutBucketEncryption, DeleteBucketEncryption, GetBucketLifecycleConfiguration, PutBucketLifecycleConfiguration, DeleteBucketLifecycle, GetBucketCors, PutBucketCors, DeleteBucketCors, GetBucketAcl, PutBucketAcl, GetBucketTagging, PutBucketTagging, DeleteBucketTagging, GetBucketPolicy, PutBucketPolicy, DeleteBucketPolicy, GetBucketNotificationConfiguration, PutBucketNotificationConfiguration, GetBucketLogging, PutBucketLogging, ListObjectVersions, CreateMultipartUpload, UploadPart, CompleteMultipartUpload, AbortMultipartUpload, PutObjectLockConfiguration, GetObjectLockConfiguration, PutObjectRetention, GetObjectRetention, PutObjectLegalHold, GetObjectLegalHold, PutBucketReplication, GetBucketReplication, DeleteBucketReplication | Optional disk persistence via `S3_PERSIST=1`; Object Lock with retention & legal hold enforcement on delete || **SQS** | CreateQueue, DeleteQueue, ListQueues, GetQueueUrl, GetQueueAttributes, SetQueueAttributes, PurgeQueue, SendMessage, ReceiveMessage, DeleteMessage, ChangeMessageVisibility, ChangeMessageVisibilityBatch, SendMessageBatch, DeleteMessageBatch, TagQueue, UntagQueue, ListQueueTags | Both Query API and JSON protocol; FIFO queues with deduplication; DLQ support |-| **SNS** | CreateTopic, DeleteTopic, ListTopics, GetTopicAttributes, SetTopicAttributes, Subscribe, Unsubscribe, ListSubscriptions, ListSubscriptionsByTopic, GetSubscriptionAttributes, SetSubscriptionAttributes, ConfirmSubscription, Publish, PublishBatch, TagResource, UntagResource, ListTagsForResource, CreatePlatformApplication, CreatePlatformEndpoint | SNS→SQS fanout delivery; SNS→Lambda fanout (synchronous invocation) |+| **SNS** | CreateTopic, DeleteTopic, ListTopics, GetTopicAttributes, SetTopicAttributes, Subscribe, Unsubscribe, ListSubscriptions, ListSubscriptionsByTopic, GetSubscriptionAttributes, SetSubscriptionAttributes, ConfirmSubscription, Publish, PublishBatch, TagResource, UntagResource, ListTagsForResource, CreatePlatformApplication, CreatePlatformEndpoint | SNS→SQS fanout delivery; SNS→Lambda fanout (synchronous invocation); FIFO topics with 5-minute deduplication, sequence numbers, content-based deduplication, and subscription validation || **DynamoDB** | CreateTable, UpdateTable, DeleteTable, DescribeTable, ListTables, PutItem, GetItem, DeleteItem, UpdateItem, Query, Scan, BatchWriteItem, BatchGetItem, TransactWriteItems, TransactGetItems, DescribeTimeToLive, UpdateTimeToLive, DescribeContinuousBackups, UpdateContinuousBackups, DescribeEndpoints, TagResource, UntagResource, ListTagsOfResource | TTL enforced via thread-safe background reaper (60s cadence); DynamoDB Streams — `StreamSpecification` emits INSERT/MODIFY/REMOVE records on all write operations, respects `StreamViewType` || **Lambda** | CreateFunction, DeleteFunction, GetFunction, GetFunctionConfiguration, ListFunctions, Invoke, UpdateFunctionCode, UpdateFunctionConfiguration, AddPermission, RemovePermission, GetPolicy, ListVersionsByFunction, PublishVersion, CreateAlias, GetAlias, UpdateAlias, DeleteAlias, ListAliases, TagResource, UntagResource, ListTags, CreateEventSourceMapping, DeleteEventSourceMapping, GetEventSourceMapping, ListEventSourceMappings, UpdateEventSourceMapping, CreateFunctionUrlConfig, GetFunctionUrlConfig, UpdateFunctionUrlConfig, DeleteFunctionUrlConfig, ListFunctionUrlConfigs, PutFunctionConcurrency, GetFunctionConcurrency, DeleteFunctionConcurrency, PutFunctionEventInvokeConfig, GetFunctionEventInvokeConfig, DeleteFunctionEventInvokeConfig, PublishLayerVersion, GetLayerVersion, GetLayerVersionByArn, ListLayerVersions, DeleteLayerVersion, ListLayers, AddLayerVersionPermission, RemoveLayerVersionPermission, GetLayerVersionPolicy | Python and Node.js runtimes execute with warm worker pool; `provided.al2023`/`provided.al2` runtimes execute via Docker RIE (Go, Rust, C++ support); `Publish=True` creates immutable numbered versions; Code via `ZipFile`, `S3Bucket`/`S3Key`, or `ImageUri` (Docker image); `PackageType: Image` pulls and invokes user-provided Docker images via Lambda RIE; SQS, Kinesis, and DynamoDB Streams event source mappings; Function URL CRUD; Lambda Layers CRUD; Aliases; Concurrency; EventInvokeConfig || **IAM** | CreateUser, GetUser, ListUsers, DeleteUser, CreateRole, GetRole, ListRoles, DeleteRole, CreatePolicy, GetPolicy, DeletePolicy, AttachRolePolicy, DetachRolePolicy, PutRolePolicy, GetRolePolicy, DeleteRolePolicy, ListRolePolicies, ListAttachedRolePolicies, CreateAccessKey, ListAccessKeys, DeleteAccessKey, CreateInstanceProfile, GetInstanceProfile, DeleteInstanceProfile, AddRoleToInstanceProfile, RemoveRoleFromInstanceProfile, ListInstanceProfiles, CreateGroup, GetGroup, AddUserToGroup, RemoveUserFromGroup, CreateServiceLinkedRole, DeleteServiceLinkedRole, GetServiceLinkedRoleDeletionStatus, CreateOpenIDConnectProvider, TagRole, UntagRole, TagUser, UntagUser, TagPolicy, UntagPolicy | |diff --git a/ministack/services/sns.py b/ministack/services/sns.pyindex 921de48e..8da2b49e 100644--- a/ministack/services/sns.py+++ b/ministack/services/sns.py@@ -8,15 +8,18 @@ Supports: CreateTopic, DeleteTopic, ListTopics, GetTopicAttributes, SetTopicAttrListTagsForResource, TagResource, UntagResource,CreatePlatformApplication, CreatePlatformEndpoint.SNS → Lambda fanout dispatches via _execute_function (synchronous).+FIFO topics: .fifo naming validation, MessageGroupId/MessageDeduplicationId enforcement,+ 5-minute deduplication window, sequence numbers, content-based deduplication,+ FIFO SQS subscription validation, PublishBatch FIFO support."""import asyncio-import threading as _threadingimport copyimport hashlibimport jsonimport loggingimport os+import threading as _threadingimport timefrom urllib.parse import parse_qs@@ -33,6 +36,7 @@ REGION = os.environ.get("MINISTACK_REGION", "us-east-1")import re as _re+def _normalize_arn(arn: str) -> str:"""Normalize an SNS ARN that has an empty account ID.Some SDKs (Go v2 with skipRequestingAccountId) construct ARNs with empty@@ -43,7 +47,7 @@ def _normalize_arn(arn: str) -> str:return _re.sub(r"(arn:aws:sns:[^:]+)::", rf"\1:{get_account_id()}:", arn)return arn-from ministack.core.persistence import load_state, PERSIST_STATE+from ministack.core.persistence import PERSIST_STATE, load_state_topics = AccountScopedDict()_sub_arn_to_topic = AccountScopedDict()@@ -113,6 +117,36 @@ def _create_topic(params):if not name:return _error("InvalidParameterException", "Name is required", 400)+ # ── Collect explicit attributes from the request ──+ explicit_attrs = {}+ i = 1+ while _p(params, f"Attributes.entry.{i}.key"):+ key = _p(params, f"Attributes.entry.{i}.key")+ val = _p(params, f"Attributes.entry.{i}.value")+ explicit_attrs[key] = val+ i += 1++ fifo_attr = explicit_attrs.get("FifoTopic", "")+ is_fifo_name = name.endswith(".fifo")++ # FIFO naming validation: FifoTopic=true requires .fifo suffix+ if fifo_attr == "true" and not is_fifo_name:+ return _error(+ "InvalidParameterException",+ "Invalid parameter: Topic names with FIFO attribute must end with .fifo suffix",+ 400,+ )++ # Auto-detect FIFO when name ends with .fifo but attribute not explicitly set+ if is_fifo_name and fifo_attr != "true":+ explicit_attrs["FifoTopic"] = "true"++ is_fifo = explicit_attrs.get("FifoTopic") == "true"++ # Default ContentBasedDeduplication to "false" for FIFO topics+ if is_fifo and "ContentBasedDeduplication" not in explicit_attrs:+ explicit_attrs["ContentBasedDeduplication"] = "false"+arn = f"arn:aws:sns:{REGION}:{get_account_id()}:{name}"if arn not in _topics:default_policy = json.dumps({@@ -127,7 +161,7 @@ def _create_topic(params):"Condition": {"StringEquals": {"AWS:SourceOwner": get_account_id()}},}],})- _topics[arn] = {+ topic = {"name": name,"arn": arn,"attributes": {@@ -153,21 +187,24 @@ def _create_topic(params):"tags": {},}- i = 1- while _p(params, f"Attributes.entry.{i}.key"):- key = _p(params, f"Attributes.entry.{i}.key")- val = _p(params, f"Attributes.entry.{i}.value")- _topics[arn]["attributes"][key] = val- i += 1+ # Apply explicit attributes (including auto-set FIFO attrs)+ topic["attributes"].update(explicit_attrs)++ # Initialize FIFO-specific state+ if is_fifo:+ topic["dedup_cache"] = {}+ topic["fifo_seq"] = 0# Store tags from CreateTopici = 1while _p(params, f"Tag.member.{i}.Key"):key = _p(params, f"Tag.member.{i}.Key")val = _p(params, f"Tag.member.{i}.Value")- _topics[arn]["tags"][key] = val+ topic["tags"][key] = vali += 1+ _topics[arn] = topic+return _xml(200, "CreateTopicResponse",f"<CreateTopicResult><TopicArn>{arn}</TopicArn></CreateTopicResult>")@@ -243,6 +280,16 @@ def _subscribe(params):if not protocol:return _error("InvalidParameterException", "Protocol is required", 400)+ # FIFO subscription validation: SQS endpoints must be FIFO queues+ if _is_fifo_topic(topic) and protocol == "sqs":+ queue_name = (endpoint or "").split(":")[-1]+ if not queue_name.endswith(".fifo"):+ return _error(+ "InvalidParameterException",+ "Invalid parameter: Invalid parameter: Topic with FIFO requires a subscription to a FIFO SQS Queue",+ 400,+ )+for existing in topic["subscriptions"]:if existing["protocol"] == protocol and existing["endpoint"] == endpoint:return _xml(200, "SubscribeResponse",@@ -426,6 +473,53 @@ def _set_subscription_attributes(params):return _xml(200, "SetSubscriptionAttributesResponse", "")+# ---------------------------------------------------------------------------+# FIFO helpers+# ---------------------------------------------------------------------------++# AWS SNS FIFO topics deduplicate messages for exactly 5 minutes (300 s).+# Publishing the same MessageDeduplicationId within this window returns the+# original MessageId/SequenceNumber without re-delivering to subscribers.+# Reference: https://docs.aws.amazon.com/sns/latest/dg/fifo-message-dedup.html+_DEDUP_WINDOW_S = 300+++def _is_fifo_topic(topic: dict) -> bool:+ """Return True if the topic is a FIFO topic."""+ return topic.get("attributes", {}).get("FifoTopic") == "true"+++def _prune_sns_dedup(topic: dict) -> None:+ """Remove expired entries (older than 300s) from the topic's dedup_cache."""+ now = time.time()+ topic["dedup_cache"] = {+ k: v for k, v in topic.get("dedup_cache", {}).items()+ if v["expire"] > now+ }+++def _resolve_dedup_id(topic: dict, params: dict, message: str) -> str:+ """Resolve the effective MessageDeduplicationId.++ Priority:+ 1. Explicit param value+ 2. SHA-256 of body when ContentBasedDeduplication is enabled+ 3. Raise ValueError when neither is available+ """+ explicit = _p(params, "MessageDeduplicationId") or ""+ if explicit:+ return explicit++ cbd = topic.get("attributes", {}).get("ContentBasedDeduplication", "false")+ if cbd == "true":+ return hashlib.sha256(message.encode()).hexdigest()++ raise ValueError(+ "Invalid parameter: The MessageDeduplicationId parameter is required "+ "for FIFO topics when ContentBasedDeduplication is not enabled"+ )++# ---------------------------------------------------------------------------# Publish# ---------------------------------------------------------------------------@@ -450,10 +544,77 @@ def _publish(params):if topic_arn not in _topics:return _error("NotFound", f"Topic does not exist: {topic_arn}", 404)+ topic = _topics[topic_arn]msg_attrs = _parse_message_attributes(params)- msg_id = new_uuid()+ fifo = _is_fifo_topic(topic)++ # ── FIFO validation, deduplication, and sequencing ──+ if fifo:+ group_id = _p(params, "MessageGroupId") or ""+ if not group_id:+ return _error(+ "InvalidParameterException",+ "Invalid parameter: The MessageGroupId parameter is required for FIFO topics",+ 400,+ )++ # Resolve dedup ID: explicit > CBD SHA-256 > error+ try:+ dedup_id = _resolve_dedup_id(topic, params, message)+ except ValueError as exc:+ return _error("InvalidParameterException", str(exc), 400)++ # Prune expired cache entries, then check for duplicate+ _prune_sns_dedup(topic)+ cached = topic.get("dedup_cache", {}).get(dedup_id)+ if cached:+ # Duplicate within the 5-minute window — return cached result+ return _xml(+ 200,+ "PublishResponse",+ f"<PublishResult>"+ f"<MessageId>{cached['message_id']}</MessageId>"+ f"<SequenceNumber>{cached['sequence_number']}</SequenceNumber>"+ f"</PublishResult>",+ )++ # New message: increment sequence counter+ topic["fifo_seq"] = topic.get("fifo_seq", 0) + 1+ seq_number = str(topic["fifo_seq"]).zfill(20)- _topics[topic_arn]["messages"].append({+ msg_id = new_uuid()+ topic["messages"].append({+ "id": msg_id,+ "message": message,+ "subject": subject,+ "message_structure": message_structure,+ "message_attributes": msg_attrs,+ "timestamp": int(time.time()),+ })++ _fanout(topic_arn, msg_id, message, subject, message_structure, msg_attrs,+ message_group_id=group_id, message_dedup_id=dedup_id)++ # Cache the entry for deduplication (300s window)+ topic.setdefault("dedup_cache", {})[dedup_id] = {+ "expire": time.time() + _DEDUP_WINDOW_S,+ "message_id": msg_id,+ "sequence_number": seq_number,+ }++ logger.info("SNS FIFO publish to %s: %s", topic_arn, message[:100])+ return _xml(+ 200,+ "PublishResponse",+ f"<PublishResult>"+ f"<MessageId>{msg_id}</MessageId>"+ f"<SequenceNumber>{seq_number}</SequenceNumber>"+ f"</PublishResult>",+ )++ # ── Standard (non-FIFO) publish path ──+ msg_id = new_uuid()+ topic["messages"].append({"id": msg_id,"message": message,"subject": subject,@@ -495,6 +656,9 @@ def _publish_batch(params):"Batch entry ids must be distinct", 400)ids_seen.add(eid)+ topic = _topics[topic_arn]+ fifo = _is_fifo_topic(topic)+successful = ""failed = ""for entry in entries:@@ -503,24 +667,102 @@ def _publish_batch(params):subject = entry.get("subject", "")message_structure = entry.get("message_structure", "")msg_attrs = entry.get("message_attributes", {})+ group_id = entry.get("message_group_id", "")+ entry_dedup_id = entry.get("message_dedup_id", "")++ # ── FIFO per-entry validation ──+ if fifo:+ if not group_id:+ failed += (+ "<member>"+ f"<Id>{_xml_escape(eid)}</Id>"+ f"<Code>InvalidParameterException</Code>"+ f"<Message>Invalid parameter: The MessageGroupId parameter is required for FIFO topics</Message>"+ f"<SenderFault>true</SenderFault>"+ "</member>"+ )+ continue- msg_id = new_uuid()- _topics[topic_arn]["messages"].append({- "id": msg_id,- "message": message,- "subject": subject,- "message_structure": message_structure,- "message_attributes": msg_attrs,- "timestamp": int(time.time()),- })- _fanout(topic_arn, msg_id, message, subject, message_structure, msg_attrs)+ # Build a mini params dict so _resolve_dedup_id can read the explicit value+ entry_params = {}+ if entry_dedup_id:+ entry_params["MessageDeduplicationId"] = [entry_dedup_id]+ try:+ dedup_id = _resolve_dedup_id(topic, entry_params, message)+ except ValueError as exc:+ failed += (+ "<member>"+ f"<Id>{_xml_escape(eid)}</Id>"+ f"<Code>InvalidParameterException</Code>"+ f"<Message>{_xml_escape(str(exc))}</Message>"+ f"<SenderFault>true</SenderFault>"+ "</member>"+ )+ continue- successful += (- "<member>"- f"<Id>{_xml_escape(eid)}</Id>"- f"<MessageId>{msg_id}</MessageId>"- "</member>"- )+ # Dedup check+ _prune_sns_dedup(topic)+ cached = topic.get("dedup_cache", {}).get(dedup_id)+ if cached:+ successful += (+ "<member>"+ f"<Id>{_xml_escape(eid)}</Id>"+ f"<MessageId>{cached['message_id']}</MessageId>"+ f"<SequenceNumber>{cached['sequence_number']}</SequenceNumber>"+ "</member>"+ )+ continue++ # New FIFO message: increment sequence counter+ topic["fifo_seq"] = topic.get("fifo_seq", 0) + 1+ seq_number = str(topic["fifo_seq"]).zfill(20)++ msg_id = new_uuid()+ topic["messages"].append({+ "id": msg_id,+ "message": message,+ "subject": subject,+ "message_structure": message_structure,+ "message_attributes": msg_attrs,+ "timestamp": int(time.time()),+ })++ _fanout(topic_arn, msg_id, message, subject, message_structure, msg_attrs,+ message_group_id=group_id, message_dedup_id=dedup_id)++ # Cache for deduplication+ topic.setdefault("dedup_cache", {})[dedup_id] = {+ "expire": time.time() + _DEDUP_WINDOW_S,+ "message_id": msg_id,+ "sequence_number": seq_number,+ }++ successful += (+ "<member>"+ f"<Id>{_xml_escape(eid)}</Id>"+ f"<MessageId>{msg_id}</MessageId>"+ f"<SequenceNumber>{seq_number}</SequenceNumber>"+ "</member>"+ )+ else:+ # ── Standard (non-FIFO) batch entry ──+ msg_id = new_uuid()+ topic["messages"].append({+ "id": msg_id,+ "message": message,+ "subject": subject,+ "message_structure": message_structure,+ "message_attributes": msg_attrs,+ "timestamp": int(time.time()),+ })+ _fanout(topic_arn, msg_id, message, subject, message_structure, msg_attrs)++ successful += (+ "<member>"+ f"<Id>{_xml_escape(eid)}</Id>"+ f"<MessageId>{msg_id}</MessageId>"+ "</member>"+ )return _xml(200, "PublishBatchResponse",f"<PublishBatchResult>"@@ -885,6 +1127,8 @@ def _parse_batch_entries(params) -> list[dict]:"subject": _p(params, f"PublishBatchRequestEntries.member.{i}.Subject"),"message_structure": _p(params, f"PublishBatchRequestEntries.member.{i}.MessageStructure"),"message_attributes": {},+ "message_group_id": _p(params, f"PublishBatchRequestEntries.member.{i}.MessageGroupId"),+ "message_dedup_id": _p(params, f"PublishBatchRequestEntries.member.{i}.MessageDeduplicationId"),}j = 1while True:diff --git a/tests/test_sns.py b/tests/test_sns.pyindex 68597a40..6077058b 100644--- a/tests/test_sns.py+++ b/tests/test_sns.py@@ -2,11 +2,15 @@ import ioimport jsonimport osimport time+import urllib.request+import uuid as _uuid_modimport zipfile-from urllib.parse import urlparse+from urllib.parse import urlencode, urlparse+import pytestfrom botocore.exceptions import ClientError-import uuid as _uuid_mod++ENDPOINT = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")def _make_zip(code: str) -> bytes:buf = io.BytesIO()@@ -407,3 +411,689 @@ def test_sns_to_sqs_fanout(sns, sqs):assert body["TopicArn"] == topic_arnassert body["Subject"] == "IntgTest"assert body["Type"] == "Notification"+++# ---------------------------------------------------------------------------+# FIFO Topic Creation Tests+# ---------------------------------------------------------------------------+++def test_sns_fifo_create_topic_with_fifo_suffix_and_attribute(sns):+ """Creating a FIFO topic with .fifo suffix and FifoTopic=true succeeds."""+ resp = sns.create_topic(+ Name="intg-fifo-create.fifo",+ Attributes={"FifoTopic": "true"},+ )+ arn = resp["TopicArn"]+ assert arn.endswith("intg-fifo-create.fifo")++ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["FifoTopic"] == "true"+++def test_sns_fifo_create_topic_without_fifo_suffix_returns_error(sns):+ """Creating a topic with FifoTopic=true but no .fifo suffix returns InvalidParameterException."""+ with pytest.raises(ClientError) as exc:+ sns.create_topic(+ Name="intg-fifo-no-suffix",+ Attributes={"FifoTopic": "true"},+ )+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+++def test_sns_fifo_auto_detect_from_suffix(sns):+ """Creating a topic with .fifo suffix auto-detects as FIFO even without explicit FifoTopic attribute."""+ resp = sns.create_topic(Name="intg-fifo-autodetect.fifo")+ arn = resp["TopicArn"]++ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["FifoTopic"] == "true"+++def test_sns_fifo_content_based_dedup_defaults_to_false(sns):+ """ContentBasedDeduplication defaults to 'false' for FIFO topics when not explicitly provided."""+ resp = sns.create_topic(+ Name="intg-fifo-cbd-default.fifo",+ Attributes={"FifoTopic": "true"},+ )+ arn = resp["TopicArn"]++ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["ContentBasedDeduplication"] == "false"+++def test_sns_fifo_content_based_dedup_set_to_true(sns):+ """ContentBasedDeduplication can be set to 'true' at creation time."""+ resp = sns.create_topic(+ Name="intg-fifo-cbd-true.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},+ )+ arn = resp["TopicArn"]++ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["ContentBasedDeduplication"] == "true"+++def test_sns_fifo_get_topic_attributes_returns_fifo_attrs(sns):+ """GetTopicAttributes returns all FIFO-related attributes correctly."""+ resp = sns.create_topic(+ Name="intg-fifo-getattrs.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},+ )+ arn = resp["TopicArn"]++ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["TopicArn"] == arn+ assert attrs["FifoTopic"] == "true"+ assert attrs["ContentBasedDeduplication"] == "true"+ # Standard attributes should still be present+ assert "Owner" in attrs+ assert "Policy" in attrs+++# ---------------------------------------------------------------------------+# FIFO Publish Validation Tests+# ---------------------------------------------------------------------------+++def test_sns_fifo_publish_without_message_group_id_returns_error(sns):+ """Publishing to a FIFO topic without MessageGroupId returns InvalidParameterException."""+ arn = sns.create_topic(+ Name="intg-fifo-pub-no-grp.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},+ )["TopicArn"]++ with pytest.raises(ClientError) as exc:+ sns.publish(TopicArn=arn, Message="missing group id")+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+++def test_sns_fifo_publish_without_dedup_id_cbd_false_returns_error(sns):+ """Publishing to a FIFO topic (CBD=false) without MessageDeduplicationId returns error."""+ arn = sns.create_topic(+ Name="intg-fifo-pub-no-dedup.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},+ )["TopicArn"]++ with pytest.raises(ClientError) as exc:+ sns.publish(+ TopicArn=arn,+ Message="missing dedup id",+ MessageGroupId="grp-1",+ )+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+++def test_sns_standard_topic_publish_without_message_group_id_succeeds(sns):+ """Publishing to a standard topic without MessageGroupId succeeds normally."""+ arn = sns.create_topic(Name="intg-std-pub-no-grp")["TopicArn"]++ resp = sns.publish(TopicArn=arn, Message="standard topic message")+ assert "MessageId" in resp+++def test_sns_fifo_publish_with_valid_params_returns_sequence_number(sns):+ """Publishing to a FIFO topic with valid params succeeds and returns SequenceNumber."""+ arn = sns.create_topic(+ Name="intg-fifo-pub-seq.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},+ )["TopicArn"]++ resp = sns.publish(+ TopicArn=arn,+ Message="fifo message with seq",+ MessageGroupId="grp-1",+ MessageDeduplicationId="dedup-seq-001",+ )+ assert "MessageId" in resp+ assert "SequenceNumber" in resp+ # Sequence number should be a zero-padded numeric string+ assert resp["SequenceNumber"].isdigit()+ assert len(resp["SequenceNumber"]) == 20+++def test_sns_standard_topic_publish_response_omits_sequence_number(sns):+ """Standard topic publish response does not include SequenceNumber."""+ arn = sns.create_topic(Name="intg-std-pub-no-seq")["TopicArn"]++ resp = sns.publish(TopicArn=arn, Message="standard topic no seq")+ assert "MessageId" in resp+ assert "SequenceNumber" not in resp+++# ---------------------------------------------------------------------------+# FIFO Deduplication and Sequence Number Tests+# ---------------------------------------------------------------------------+++def test_sns_fifo_explicit_dedup_id_returns_same_result_on_duplicate(sns):+ """Publishing the same MessageDeduplicationId twice returns the same MessageId and SequenceNumber."""+ arn = sns.create_topic(+ Name="intg-fifo-dedup-same.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},+ )["TopicArn"]++ resp1 = sns.publish(+ TopicArn=arn,+ Message="first publish",+ MessageGroupId="grp-1",+ MessageDeduplicationId="dedup-same-001",+ )+ resp2 = sns.publish(+ TopicArn=arn,+ Message="second publish different body",+ MessageGroupId="grp-1",+ MessageDeduplicationId="dedup-same-001",+ )++ assert resp1["MessageId"] == resp2["MessageId"]+ assert resp1["SequenceNumber"] == resp2["SequenceNumber"]+++def test_sns_fifo_cbd_dedup_subscriber_gets_one_message(sns, sqs):+ """CBD=true with same body twice deduplicates — subscriber receives only one message."""+ topic_arn = sns.create_topic(+ Name="intg-fifo-cbd-dedup.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},+ )["TopicArn"]++ q_url = sqs.create_queue(+ QueueName="intg-fifo-cbd-dedup-q.fifo",+ Attributes={"FifoQueue": "true"},+ )["QueueUrl"]+ q_arn = sqs.get_queue_attributes(+ QueueUrl=q_url, AttributeNames=["QueueArn"],+ )["Attributes"]["QueueArn"]++ sns.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=q_arn)++ # Publish the same body twice — CBD will generate the same dedup ID+ resp1 = sns.publish(+ TopicArn=topic_arn,+ Message="identical body for cbd",+ MessageGroupId="grp-cbd",+ )+ resp2 = sns.publish(+ TopicArn=topic_arn,+ Message="identical body for cbd",+ MessageGroupId="grp-cbd",+ )++ # Both responses should return the same MessageId (dedup hit)+ assert resp1["MessageId"] == resp2["MessageId"]++ # Subscriber should only receive one message+ msgs = sqs.receive_message(+ QueueUrl=q_url,+ MaxNumberOfMessages=10,+ WaitTimeSeconds=2,+ )+ assert len(msgs.get("Messages", [])) == 1+ body = json.loads(msgs["Messages"][0]["Body"])+ assert body["Message"] == "identical body for cbd"+++def test_sns_fifo_explicit_dedup_id_overrides_cbd(sns):+ """Explicit MessageDeduplicationId is used regardless of CBD setting."""+ arn = sns.create_topic(+ Name="intg-fifo-dedup-override.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},+ )["TopicArn"]++ # Publish two messages with the same body but different explicit dedup IDs+ resp1 = sns.publish(+ TopicArn=arn,+ Message="same body",+ MessageGroupId="grp-1",+ MessageDeduplicationId="explicit-dedup-A",+ )+ resp2 = sns.publish(+ TopicArn=arn,+ Message="same body",+ MessageGroupId="grp-1",+ MessageDeduplicationId="explicit-dedup-B",+ )++ # Different explicit dedup IDs → different messages (not deduplicated)+ assert resp1["MessageId"] != resp2["MessageId"]+ assert resp1["SequenceNumber"] != resp2["SequenceNumber"]++ # Now publish again with the same explicit dedup ID as the first → deduplicated+ resp3 = sns.publish(+ TopicArn=arn,+ Message="different body this time",+ MessageGroupId="grp-1",+ MessageDeduplicationId="explicit-dedup-A",+ )+ assert resp3["MessageId"] == resp1["MessageId"]+ assert resp3["SequenceNumber"] == resp1["SequenceNumber"]+++def test_sns_fifo_sequence_numbers_monotonically_increasing(sns):+ """Multiple non-duplicate publishes produce monotonically increasing sequence numbers."""+ arn = sns.create_topic(+ Name="intg-fifo-seq-incr.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},+ )["TopicArn"]++ seq_numbers = []+ for i in range(5):+ resp = sns.publish(+ TopicArn=arn,+ Message=f"message-{i}",+ MessageGroupId="grp-seq",+ MessageDeduplicationId=f"dedup-seq-{i}",+ )+ assert "SequenceNumber" in resp+ seq_numbers.append(resp["SequenceNumber"])++ # All sequence numbers should be numeric and zero-padded to 20 digits+ for seq in seq_numbers:+ assert seq.isdigit()+ assert len(seq) == 20++ # Sequence numbers should be strictly increasing+ for j in range(1, len(seq_numbers)):+ assert int(seq_numbers[j]) > int(seq_numbers[j - 1])+++# ---------------------------------------------------------------------------+# FIFO Subscription Validation Tests+# ---------------------------------------------------------------------------+++def test_sns_fifo_subscribe_non_fifo_sqs_queue_returns_error(sns, sqs):+ """Subscribing a non-FIFO SQS queue to a FIFO topic returns InvalidParameterException."""+ uid = _uuid_mod.uuid4().hex[:8]+ topic_arn = sns.create_topic(+ Name=f"intg-fifo-sub-nonfifo-{uid}.fifo",+ Attributes={"FifoTopic": "true"},+ )["TopicArn"]++ q_url = sqs.create_queue(QueueName=f"intg-fifo-sub-nonfifo-q-{uid}")["QueueUrl"]+ q_arn = sqs.get_queue_attributes(+ QueueUrl=q_url, AttributeNames=["QueueArn"],+ )["Attributes"]["QueueArn"]++ with pytest.raises(ClientError) as exc:+ sns.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=q_arn)+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+++def test_sns_fifo_subscribe_fifo_sqs_queue_succeeds(sns, sqs):+ """Subscribing a FIFO SQS queue to a FIFO topic succeeds."""+ uid = _uuid_mod.uuid4().hex[:8]+ topic_arn = sns.create_topic(+ Name=f"intg-fifo-sub-fifo-{uid}.fifo",+ Attributes={"FifoTopic": "true"},+ )["TopicArn"]++ q_url = sqs.create_queue(+ QueueName=f"intg-fifo-sub-fifo-q-{uid}.fifo",+ Attributes={"FifoQueue": "true"},+ )["QueueUrl"]+ q_arn = sqs.get_queue_attributes(+ QueueUrl=q_url, AttributeNames=["QueueArn"],+ )["Attributes"]["QueueArn"]++ resp = sns.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=q_arn)+ assert "SubscriptionArn" in resp+ assert resp["SubscriptionArn"] != "PendingConfirmation"+++def test_sns_fifo_subscribe_non_sqs_protocols_succeed(sns):+ """Subscribing email/lambda/http to a FIFO topic succeeds without FIFO queue validation."""+ uid = _uuid_mod.uuid4().hex[:8]+ topic_arn = sns.create_topic(+ Name=f"intg-fifo-sub-nonsqs-{uid}.fifo",+ Attributes={"FifoTopic": "true"},+ )["TopicArn"]++ # email protocol+ resp_email = sns.subscribe(+ TopicArn=topic_arn, Protocol="email", Endpoint=f"user-{uid}@example.com",+ )+ assert "SubscriptionArn" in resp_email++ # lambda protocol+ lambda_arn = f"arn:aws:lambda:us-east-1:000000000000:function:my-func-{uid}"+ resp_lambda = sns.subscribe(+ TopicArn=topic_arn, Protocol="lambda", Endpoint=lambda_arn,+ )+ assert "SubscriptionArn" in resp_lambda++ # http protocol+ resp_http = sns.subscribe(+ TopicArn=topic_arn, Protocol="http", Endpoint=f"http://example.com/hook-{uid}",+ )+ assert "SubscriptionArn" in resp_http+++# ---------------------------------------------------------------------------+# PublishBatch FIFO Support Tests+# ---------------------------------------------------------------------------+++def _raw_publish_batch(topic_arn, entries):+ """Send a PublishBatch request via raw HTTP to bypass boto3 client-side validation.++ This is needed because boto3 may raise ParamValidationError for entries+ missing MessageGroupId on FIFO topics before the request reaches the server.++ Each entry is a dict with keys: Id, Message, and optionally MessageGroupId,+ MessageDeduplicationId.+ """+ form = {"Action": "PublishBatch", "TopicArn": topic_arn}+ for i, entry in enumerate(entries, start=1):+ prefix = f"PublishBatchRequestEntries.member.{i}"+ form[f"{prefix}.Id"] = entry["Id"]+ form[f"{prefix}.Message"] = entry["Message"]+ if "MessageGroupId" in entry:+ form[f"{prefix}.MessageGroupId"] = entry["MessageGroupId"]+ if "MessageDeduplicationId" in entry:+ form[f"{prefix}.MessageDeduplicationId"] = entry["MessageDeduplicationId"]++ data = urlencode(form).encode()+ req = urllib.request.Request(ENDPOINT, data=data, method="POST")+ req.add_header("Content-Type", "application/x-www-form-urlencoded")+ resp = urllib.request.urlopen(req, timeout=10)+ body = resp.read().decode()+ return resp.status, body+++def test_sns_fifo_publish_batch_missing_group_id_fails_entries(sns):+ """PublishBatch to FIFO topic: entries missing MessageGroupId go to Failed list.+ """+ uid = _uuid_mod.uuid4().hex[:8]+ topic_arn = sns.create_topic(+ Name=f"intg-fifo-batch-nogrp-{uid}.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},+ )["TopicArn"]++ # Use raw HTTP to bypass boto3 client-side validation+ entries = [+ {"Id": "e1", "Message": "msg without group id"},+ {"Id": "e2", "Message": "msg without group id 2"},+ ]+ status, body = _raw_publish_batch(topic_arn, entries)+ assert status == 200++ # Both entries should be in the Failed list+ assert "<Failed>" in body+ assert body.count("<Id>e1</Id>") == 1+ assert body.count("<Id>e2</Id>") == 1+ assert "InvalidParameterException" in body+++def test_sns_fifo_publish_batch_all_valid_returns_successful_with_sequence(sns):+ """PublishBatch to FIFO topic: all valid entries return in Successful with SequenceNumber.+ """+ uid = _uuid_mod.uuid4().hex[:8]+ topic_arn = sns.create_topic(+ Name=f"intg-fifo-batch-valid-{uid}.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},+ )["TopicArn"]++ resp = sns.publish_batch(+ TopicArn=topic_arn,+ PublishBatchRequestEntries=[+ {+ "Id": "e1",+ "Message": "batch fifo msg 1",+ "MessageGroupId": "grp-1",+ "MessageDeduplicationId": f"dedup-batch-1-{uid}",+ },+ {+ "Id": "e2",+ "Message": "batch fifo msg 2",+ "MessageGroupId": "grp-1",+ "MessageDeduplicationId": f"dedup-batch-2-{uid}",+ },+ {+ "Id": "e3",+ "Message": "batch fifo msg 3",+ "MessageGroupId": "grp-2",+ "MessageDeduplicationId": f"dedup-batch-3-{uid}",+ },+ ],+ )++ assert len(resp["Successful"]) == 3+ assert len(resp.get("Failed", [])) == 0++ # Each successful entry should have a SequenceNumber+ seq_numbers = []+ for entry in resp["Successful"]:+ assert "MessageId" in entry+ assert "SequenceNumber" in entry+ seq = entry["SequenceNumber"]+ assert seq.isdigit()+ assert len(seq) == 20+ seq_numbers.append(int(seq))++ # Sequence numbers should be monotonically increasing+ for i in range(1, len(seq_numbers)):+ assert seq_numbers[i] > seq_numbers[i - 1]+++def test_sns_fifo_publish_batch_mixed_valid_invalid_entries(sns):+ """PublishBatch to FIFO topic: mixed entries correctly separate Successful and Failed.+ """+ uid = _uuid_mod.uuid4().hex[:8]+ topic_arn = sns.create_topic(+ Name=f"intg-fifo-batch-mixed-{uid}.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},+ )["TopicArn"]++ # Use raw HTTP: e1 is valid, e2 is missing MessageGroupId, e3 is valid+ entries = [+ {+ "Id": "e1",+ "Message": "valid msg 1",+ "MessageGroupId": "grp-1",+ "MessageDeduplicationId": f"dedup-mixed-1-{uid}",+ },+ {+ "Id": "e2",+ "Message": "invalid msg missing group id",+ # No MessageGroupId — should fail+ },+ {+ "Id": "e3",+ "Message": "valid msg 3",+ "MessageGroupId": "grp-2",+ "MessageDeduplicationId": f"dedup-mixed-3-{uid}",+ },+ ]+ status, body = _raw_publish_batch(topic_arn, entries)+ assert status == 200++ # e1 and e3 should be in Successful+ # e2 should be in Failed+ # Parse the XML to verify+ assert "<Successful>" in body+ assert "<Failed>" in body++ # Count successful entries (e1 and e3)+ successful_section = body.split("<Successful>")[1].split("</Successful>")[0]+ assert "<Id>e1</Id>" in successful_section+ assert "<Id>e3</Id>" in successful_section+ # Successful entries should have SequenceNumber+ assert "<SequenceNumber>" in successful_section++ # Count failed entries (e2)+ failed_section = body.split("<Failed>")[1].split("</Failed>")[0]+ assert "<Id>e2</Id>" in failed_section+ assert "InvalidParameterException" in failed_section+++# ---------------------------------------------------------------------------+# ContentBasedDeduplication Attribute Management Tests+# ---------------------------------------------------------------------------+++def test_sns_fifo_set_topic_attributes_toggle_cbd(sns):+ """SetTopicAttributes can toggle ContentBasedDeduplication on a FIFO topic.+ """+ uid = _uuid_mod.uuid4().hex[:8]+ arn = sns.create_topic(+ Name=f"intg-fifo-cbd-toggle-{uid}.fifo",+ Attributes={"FifoTopic": "true"},+ )["TopicArn"]++ # CBD defaults to "false"+ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["ContentBasedDeduplication"] == "false"++ # Enable CBD via SetTopicAttributes+ sns.set_topic_attributes(+ TopicArn=arn,+ AttributeName="ContentBasedDeduplication",+ AttributeValue="true",+ )+ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["ContentBasedDeduplication"] == "true"++ # Disable CBD via SetTopicAttributes+ sns.set_topic_attributes(+ TopicArn=arn,+ AttributeName="ContentBasedDeduplication",+ AttributeValue="false",+ )+ attrs = sns.get_topic_attributes(TopicArn=arn)["Attributes"]+ assert attrs["ContentBasedDeduplication"] == "false"+++def test_sns_fifo_publish_succeeds_without_dedup_id_after_enabling_cbd(sns):+ """After enabling CBD, publishing without an explicit dedup ID succeeds.+ """+ uid = _uuid_mod.uuid4().hex[:8]+ arn = sns.create_topic(+ Name=f"intg-fifo-cbd-enable-pub-{uid}.fifo",+ Attributes={"FifoTopic": "true"}, # CBD defaults to "false"+ )["TopicArn"]++ # Enable CBD+ sns.set_topic_attributes(+ TopicArn=arn,+ AttributeName="ContentBasedDeduplication",+ AttributeValue="true",+ )++ # Publish without explicit MessageDeduplicationId — should succeed+ resp = sns.publish(+ TopicArn=arn,+ Message="cbd enabled message",+ MessageGroupId="grp-1",+ )+ assert "MessageId" in resp+ assert "SequenceNumber" in resp+++def test_sns_fifo_publish_fails_without_dedup_id_after_disabling_cbd(sns):+ """After disabling CBD, publishing without an explicit dedup ID fails.+ """+ uid = _uuid_mod.uuid4().hex[:8]+ arn = sns.create_topic(+ Name=f"intg-fifo-cbd-disable-pub-{uid}.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "true"},+ )["TopicArn"]++ # Verify publishing without dedup ID works while CBD is enabled+ resp = sns.publish(+ TopicArn=arn,+ Message="should succeed with cbd on",+ MessageGroupId="grp-1",+ )+ assert "MessageId" in resp++ # Disable CBD+ sns.set_topic_attributes(+ TopicArn=arn,+ AttributeName="ContentBasedDeduplication",+ AttributeValue="false",+ )++ # Now publishing without explicit MessageDeduplicationId should fail+ with pytest.raises(ClientError) as exc:+ sns.publish(+ TopicArn=arn,+ Message="should fail with cbd off",+ MessageGroupId="grp-1",+ )+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+++# ---------------------------------------------------------------------------+# End-to-End FIFO SNS → SQS Fanout Integration Test+# ---------------------------------------------------------------------------+++def test_sns_fifo_e2e_fanout_with_dedup(sns, sqs):+ """End-to-end: FIFO SNS → SQS fanout passes MessageGroupId and deduplicates.+ """+ uid = _uuid_mod.uuid4().hex[:8]++ # 1. Create a FIFO topic and FIFO SQS queue, subscribe the queue to the topic+ topic_arn = sns.create_topic(+ Name=f"intg-fifo-e2e-{uid}.fifo",+ Attributes={"FifoTopic": "true", "ContentBasedDeduplication": "false"},+ )["TopicArn"]++ q_url = sqs.create_queue(+ QueueName=f"intg-fifo-e2e-q-{uid}.fifo",+ Attributes={"FifoQueue": "true"},+ )["QueueUrl"]+ q_arn = sqs.get_queue_attributes(+ QueueUrl=q_url, AttributeNames=["QueueArn"],+ )["Attributes"]["QueueArn"]++ sns.subscribe(TopicArn=topic_arn, Protocol="sqs", Endpoint=q_arn)++ # 2. Publish a message with MessageGroupId and MessageDeduplicationId+ dedup_id = f"dedup-e2e-{uid}"+ group_id = f"grp-e2e-{uid}"+ resp1 = sns.publish(+ TopicArn=topic_arn,+ Message="e2e fifo fanout message",+ MessageGroupId=group_id,+ MessageDeduplicationId=dedup_id,+ )+ assert "MessageId" in resp1+ assert "SequenceNumber" in resp1++ # 3. Receive the message from SQS and verify MessageGroupId is passed through+ msgs = sqs.receive_message(+ QueueUrl=q_url,+ MaxNumberOfMessages=10,+ WaitTimeSeconds=2,+ AttributeNames=["All"],+ )+ assert len(msgs.get("Messages", [])) == 1+ msg = msgs["Messages"][0]+ body = json.loads(msg["Body"])+ assert body["Message"] == "e2e fifo fanout message"+ attrs = msg.get("Attributes", {})+ assert attrs.get("MessageGroupId") == group_id++ # Delete the received message so the queue is clean for the next check+ sqs.delete_message(QueueUrl=q_url, ReceiptHandle=msg["ReceiptHandle"])++ # 4. Publish the same dedup ID again — should be deduplicated+ resp2 = sns.publish(+ TopicArn=topic_arn,+ Message="duplicate attempt",+ MessageGroupId=group_id,+ MessageDeduplicationId=dedup_id,+ )+ # Dedup hit: same MessageId and SequenceNumber as the first publish+ assert resp2["MessageId"] == resp1["MessageId"]+ assert resp2["SequenceNumber"] == resp1["SequenceNumber"]++ # Verify the subscriber does NOT receive a duplicate message+ dup_msgs = sqs.receive_message(+ QueueUrl=q_url,+ MaxNumberOfMessages=10,+ WaitTimeSeconds=1,+ )+ assert len(dup_msgs.get("Messages", [])) == 0, "Duplicate message should not be delivered"