File: test_topics.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (724 lines) | stat: -rw-r--r-- 25,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import json

import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from moto.sns.models import DEFAULT_EFFECTIVE_DELIVERY_POLICY, DEFAULT_PAGE_SIZE


@mock_aws
def test_create_and_delete_topic():
    conn = boto3.client("sns", region_name="us-east-1")
    for topic_name in ("some-topic", "-some-topic-", "_some-topic_", "a" * 256):
        conn.create_topic(Name=topic_name)

        topics_json = conn.list_topics()
        topics = topics_json["Topics"]
        assert len(topics) == 1
        assert topics[0]["TopicArn"] == (
            f"arn:aws:sns:{conn._client_config.region_name}:{ACCOUNT_ID}:{topic_name}"
        )

        # Delete the topic
        conn.delete_topic(TopicArn=topics[0]["TopicArn"])

        # Ensure DeleteTopic is idempotent
        conn.delete_topic(TopicArn=topics[0]["TopicArn"])

        # And there should now be 0 topics
        topics_json = conn.list_topics()
        topics = topics_json["Topics"]
        assert len(topics) == 0


@mock_aws
def test_delete_non_existent_topic():
    conn = boto3.client("sns", region_name="us-east-1")

    # Ensure DeleteTopic does not throw an error for non-existent topics
    conn.delete_topic(
        TopicArn="arn:aws:sns:us-east-1:123456789012:this-topic-does-not-exist"
    )


@mock_aws
def test_create_topic_with_attributes():
    conn = boto3.client("sns", region_name="us-east-1")
    conn.create_topic(
        Name="some-topic-with-attribute", Attributes={"DisplayName": "test-topic"}
    )
    topics_json = conn.list_topics()
    topic_arn = topics_json["Topics"][0]["TopicArn"]

    attributes = conn.get_topic_attributes(TopicArn=topic_arn)["Attributes"]
    assert attributes["DisplayName"] == "test-topic"


@mock_aws
def test_create_topic_with_tags():
    conn = boto3.client("sns", region_name="us-east-1")
    response = conn.create_topic(
        Name="some-topic-with-tags",
        Tags=[
            {"Key": "tag_key_1", "Value": "tag_value_1"},
            {"Key": "tag_key_2", "Value": "tag_value_2"},
        ],
    )
    topic_arn = response["TopicArn"]

    assert conn.list_tags_for_resource(ResourceArn=topic_arn)["Tags"] == (
        [
            {"Key": "tag_key_1", "Value": "tag_value_1"},
            {"Key": "tag_key_2", "Value": "tag_value_2"},
        ]
    )


@mock_aws
def test_create_topic_should_be_indempodent():
    conn = boto3.client("sns", region_name="us-east-1")
    topic_arn = conn.create_topic(Name="some-topic")["TopicArn"]
    conn.set_topic_attributes(
        TopicArn=topic_arn, AttributeName="DisplayName", AttributeValue="should_be_set"
    )
    topic_display_name = conn.get_topic_attributes(TopicArn=topic_arn)["Attributes"][
        "DisplayName"
    ]
    assert topic_display_name == "should_be_set"

    # recreate topic to prove indempodentcy
    topic_arn = conn.create_topic(Name="some-topic")["TopicArn"]
    topic_display_name = conn.get_topic_attributes(TopicArn=topic_arn)["Attributes"][
        "DisplayName"
    ]
    assert topic_display_name == "should_be_set"


@mock_aws
def test_get_missing_topic():
    conn = boto3.client("sns", region_name="us-east-1")
    with pytest.raises(ClientError):
        conn.get_topic_attributes(
            TopicArn="arn:aws:sns:us-east-1:424242424242:a-fake-arn"
        )


@mock_aws
def test_create_topic_must_meet_constraints():
    conn = boto3.client("sns", region_name="us-east-1")
    common_random_chars = [":", ";", "!", "@", "|", "^", "%"]
    for char in common_random_chars:
        with pytest.raises(ClientError):
            conn.create_topic(Name=f"no{char}_invalidchar")
    with pytest.raises(ClientError):
        conn.create_topic(Name="no spaces allowed")


@mock_aws
def test_create_topic_should_be_of_certain_length():
    conn = boto3.client("sns", region_name="us-east-1")
    too_short = ""
    with pytest.raises(ClientError):
        conn.create_topic(Name=too_short)
    too_long = "x" * 257
    with pytest.raises(ClientError):
        conn.create_topic(Name=too_long)


@mock_aws
def test_create_topic_in_multiple_regions():
    for region in ["us-west-1", "us-west-2"]:
        conn = boto3.client("sns", region_name=region)
        topic_arn = conn.create_topic(Name="some-topic")["TopicArn"]
        # We can find the topic
        assert len(list(conn.list_topics()["Topics"])) == 1

        # We can read the Topic details
        topic = boto3.resource("sns", region_name=region).Topic(topic_arn)
        topic.load()

    # Topic does not exist in different region though
    with pytest.raises(ClientError) as exc:
        sns_resource = boto3.resource("sns", region_name="eu-north-1")
        topic = sns_resource.Topic(topic_arn)
        topic.load()
    err = exc.value.response["Error"]
    assert err["Code"] == "NotFound"
    assert err["Message"] == "Topic does not exist"
    assert err["Type"] == "Sender"


@mock_aws
def test_topic_corresponds_to_region():
    for region in ["us-east-1", "us-west-2"]:
        conn = boto3.client("sns", region_name=region)
        conn.create_topic(Name="some-topic")
        topics_json = conn.list_topics()
        topic_arn = topics_json["Topics"][0]["TopicArn"]
        assert topic_arn == f"arn:aws:sns:{region}:{ACCOUNT_ID}:some-topic"


@mock_aws
def test_topic_attributes():
    conn = boto3.client("sns", region_name="us-east-1")
    conn.create_topic(Name="some-topic")

    topics_json = conn.list_topics()
    topic_arn = topics_json["Topics"][0]["TopicArn"]

    attributes = conn.get_topic_attributes(TopicArn=topic_arn)["Attributes"]
    assert attributes["TopicArn"] == (
        f"arn:aws:sns:{conn._client_config.region_name}:{ACCOUNT_ID}:some-topic"
    )
    assert attributes["Owner"] == ACCOUNT_ID
    assert json.loads(attributes["Policy"]) == {
        "Version": "2008-10-17",
        "Id": "__default_policy_ID",
        "Statement": [
            {
                "Effect": "Allow",
                "Sid": "__default_statement_ID",
                "Principal": {"AWS": "*"},
                "Action": [
                    "SNS:GetTopicAttributes",
                    "SNS:SetTopicAttributes",
                    "SNS:AddPermission",
                    "SNS:RemovePermission",
                    "SNS:DeleteTopic",
                    "SNS:Subscribe",
                    "SNS:ListSubscriptionsByTopic",
                    "SNS:Publish",
                ],
                "Resource": f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:some-topic",
                "Condition": {"StringEquals": {"AWS:SourceOwner": ACCOUNT_ID}},
            }
        ],
    }
    assert attributes["DisplayName"] == ""
    assert attributes["SubscriptionsPending"] == "0"
    assert attributes["SubscriptionsConfirmed"] == "0"
    assert attributes["SubscriptionsDeleted"] == "0"
    assert attributes["DeliveryPolicy"] == ""
    assert json.loads(attributes["EffectiveDeliveryPolicy"]) == (
        DEFAULT_EFFECTIVE_DELIVERY_POLICY
    )

    # boto can't handle prefix-mandatory strings:
    # i.e. unicode on Python 2 -- u"foobar"
    # and bytes on Python 3 -- b"foobar"
    policy = json.dumps({"foo": "bar"})
    displayname = "My display name"
    delivery = json.dumps({"http": {"defaultHealthyRetryPolicy": {"numRetries": 5}}})
    conn.set_topic_attributes(
        TopicArn=topic_arn, AttributeName="Policy", AttributeValue=policy
    )
    conn.set_topic_attributes(
        TopicArn=topic_arn, AttributeName="DisplayName", AttributeValue=displayname
    )
    conn.set_topic_attributes(
        TopicArn=topic_arn, AttributeName="DeliveryPolicy", AttributeValue=delivery
    )

    attributes = conn.get_topic_attributes(TopicArn=topic_arn)["Attributes"]
    assert attributes["Policy"] == '{"foo":"bar"}'
    assert attributes["DisplayName"] == "My display name"
    assert attributes["DeliveryPolicy"] == (
        '{"http": {"defaultHealthyRetryPolicy": {"numRetries": 5}}}'
    )


@mock_aws
def test_topic_paging():
    conn = boto3.client("sns", region_name="us-east-1")
    for index in range(DEFAULT_PAGE_SIZE + int(DEFAULT_PAGE_SIZE / 2)):
        conn.create_topic(Name="some-topic_" + str(index))

    response = conn.list_topics()
    topics_list = response["Topics"]
    next_token = response["NextToken"]

    assert len(topics_list) == DEFAULT_PAGE_SIZE
    assert int(next_token) == DEFAULT_PAGE_SIZE

    response = conn.list_topics(NextToken=next_token)
    topics_list = response["Topics"]
    assert "NextToken" not in response

    assert len(topics_list) == int(DEFAULT_PAGE_SIZE / 2)


@mock_aws
def test_add_remove_permissions():
    client = boto3.client("sns", region_name="us-east-1")
    topic_arn = client.create_topic(Name="test-permissions")["TopicArn"]

    client.add_permission(
        TopicArn=topic_arn,
        Label="test",
        AWSAccountId=["999999999999"],
        ActionName=["Publish"],
    )

    response = client.get_topic_attributes(TopicArn=topic_arn)
    assert json.loads(response["Attributes"]["Policy"]) == {
        "Version": "2008-10-17",
        "Id": "__default_policy_ID",
        "Statement": [
            {
                "Effect": "Allow",
                "Sid": "__default_statement_ID",
                "Principal": {"AWS": "*"},
                "Action": [
                    "SNS:GetTopicAttributes",
                    "SNS:SetTopicAttributes",
                    "SNS:AddPermission",
                    "SNS:RemovePermission",
                    "SNS:DeleteTopic",
                    "SNS:Subscribe",
                    "SNS:ListSubscriptionsByTopic",
                    "SNS:Publish",
                ],
                "Resource": f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:test-permissions",
                "Condition": {"StringEquals": {"AWS:SourceOwner": ACCOUNT_ID}},
            },
            {
                "Sid": "test",
                "Effect": "Allow",
                "Principal": {"AWS": "arn:aws:iam::999999999999:root"},
                "Action": "SNS:Publish",
                "Resource": f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:test-permissions",
            },
        ],
    }

    client.remove_permission(TopicArn=topic_arn, Label="test")

    response = client.get_topic_attributes(TopicArn=topic_arn)
    assert json.loads(response["Attributes"]["Policy"]) == {
        "Version": "2008-10-17",
        "Id": "__default_policy_ID",
        "Statement": [
            {
                "Effect": "Allow",
                "Sid": "__default_statement_ID",
                "Principal": {"AWS": "*"},
                "Action": [
                    "SNS:GetTopicAttributes",
                    "SNS:SetTopicAttributes",
                    "SNS:AddPermission",
                    "SNS:RemovePermission",
                    "SNS:DeleteTopic",
                    "SNS:Subscribe",
                    "SNS:ListSubscriptionsByTopic",
                    "SNS:Publish",
                ],
                "Resource": f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:test-permissions",
                "Condition": {"StringEquals": {"AWS:SourceOwner": ACCOUNT_ID}},
            }
        ],
    }

    client.add_permission(
        TopicArn=topic_arn,
        Label="test",
        AWSAccountId=["888888888888", "999999999999"],
        ActionName=["Publish", "Subscribe"],
    )

    response = client.get_topic_attributes(TopicArn=topic_arn)
    assert json.loads(response["Attributes"]["Policy"])["Statement"][1] == {
        "Sid": "test",
        "Effect": "Allow",
        "Principal": {
            "AWS": [
                "arn:aws:iam::888888888888:root",
                "arn:aws:iam::999999999999:root",
            ]
        },
        "Action": ["SNS:Publish", "SNS:Subscribe"],
        "Resource": f"arn:aws:sns:us-east-1:{ACCOUNT_ID}:test-permissions",
    }

    # deleting non existing permission should be successful
    client.remove_permission(TopicArn=topic_arn, Label="non-existing")


@mock_aws
def test_add_permission_errors():
    client = boto3.client("sns", region_name="us-east-1")
    topic_arn = client.create_topic(Name="test-permissions")["TopicArn"]
    client.add_permission(
        TopicArn=topic_arn,
        Label="test",
        AWSAccountId=["999999999999"],
        ActionName=["Publish"],
    )

    with pytest.raises(ClientError) as client_err:
        client.add_permission(
            TopicArn=topic_arn,
            Label="test",
            AWSAccountId=["999999999999"],
            ActionName=["AddPermission"],
        )
    assert client_err.value.response["Error"]["Message"] == "Statement already exists"

    with pytest.raises(ClientError) as client_err:
        client.add_permission(
            TopicArn=topic_arn + "-not-existing",
            Label="test-2",
            AWSAccountId=["999999999999"],
            ActionName=["AddPermission"],
        )
    assert client_err.value.response["Error"]["Code"] == "NotFound"
    assert client_err.value.response["Error"]["Message"] == "Topic does not exist"

    with pytest.raises(ClientError) as client_err:
        client.add_permission(
            TopicArn=topic_arn,
            Label="test-2",
            AWSAccountId=["999999999999"],
            ActionName=["NotExistingAction"],
        )
    assert client_err.value.response["Error"]["Message"] == (
        "Policy statement action out of service scope!"
    )


@mock_aws
def test_remove_permission_errors():
    client = boto3.client("sns", region_name="us-east-1")
    topic_arn = client.create_topic(Name="test-permissions")["TopicArn"]
    client.add_permission(
        TopicArn=topic_arn,
        Label="test",
        AWSAccountId=["999999999999"],
        ActionName=["Publish"],
    )

    with pytest.raises(ClientError) as client_err:
        client.remove_permission(TopicArn=topic_arn + "-not-existing", Label="test")

    assert client_err.value.response["Error"]["Code"] == "NotFound"
    assert client_err.value.response["Error"]["Message"] == "Topic does not exist"


@mock_aws
def test_tag_topic():
    conn = boto3.client("sns", region_name="us-east-1")
    response = conn.create_topic(Name="some-topic-with-tags")
    topic_arn = response["TopicArn"]

    conn.tag_resource(
        ResourceArn=topic_arn, Tags=[{"Key": "tag_key_1", "Value": "tag_value_1"}]
    )
    assert conn.list_tags_for_resource(ResourceArn=topic_arn)["Tags"] == (
        [{"Key": "tag_key_1", "Value": "tag_value_1"}]
    )

    conn.tag_resource(
        ResourceArn=topic_arn, Tags=[{"Key": "tag_key_2", "Value": "tag_value_2"}]
    )
    assert conn.list_tags_for_resource(ResourceArn=topic_arn)["Tags"] == (
        [
            {"Key": "tag_key_1", "Value": "tag_value_1"},
            {"Key": "tag_key_2", "Value": "tag_value_2"},
        ]
    )

    conn.tag_resource(
        ResourceArn=topic_arn, Tags=[{"Key": "tag_key_1", "Value": "tag_value_X"}]
    )
    assert conn.list_tags_for_resource(ResourceArn=topic_arn)["Tags"] == (
        [
            {"Key": "tag_key_1", "Value": "tag_value_X"},
            {"Key": "tag_key_2", "Value": "tag_value_2"},
        ]
    )


@mock_aws
def test_untag_topic():
    conn = boto3.client("sns", region_name="us-east-1")
    response = conn.create_topic(
        Name="some-topic-with-tags",
        Tags=[
            {"Key": "tag_key_1", "Value": "tag_value_1"},
            {"Key": "tag_key_2", "Value": "tag_value_2"},
        ],
    )
    topic_arn = response["TopicArn"]

    conn.untag_resource(ResourceArn=topic_arn, TagKeys=["tag_key_1"])
    assert conn.list_tags_for_resource(ResourceArn=topic_arn)["Tags"] == (
        [{"Key": "tag_key_2", "Value": "tag_value_2"}]
    )

    # removing a non existing tag should not raise any error
    conn.untag_resource(ResourceArn=topic_arn, TagKeys=["not-existing-tag"])
    assert conn.list_tags_for_resource(ResourceArn=topic_arn)["Tags"] == (
        [{"Key": "tag_key_2", "Value": "tag_value_2"}]
    )


@mock_aws
def test_list_tags_for_resource_error():
    conn = boto3.client("sns", region_name="us-east-1")
    conn.create_topic(
        Name="some-topic-with-tags", Tags=[{"Key": "tag_key_1", "Value": "tag_value_X"}]
    )

    with pytest.raises(ClientError) as client_err:
        conn.list_tags_for_resource(ResourceArn="not-existing-topic")
    assert client_err.value.response["Error"]["Message"] == "Resource does not exist"


@mock_aws
def test_tag_resource_errors():
    conn = boto3.client("sns", region_name="us-east-1")
    response = conn.create_topic(
        Name="some-topic-with-tags", Tags=[{"Key": "tag_key_1", "Value": "tag_value_X"}]
    )
    topic_arn = response["TopicArn"]

    with pytest.raises(ClientError) as client_err:
        conn.tag_resource(
            ResourceArn="not-existing-topic",
            Tags=[{"Key": "tag_key_1", "Value": "tag_value_1"}],
        )
    assert client_err.value.response["Error"]["Message"] == "Resource does not exist"

    too_many_tags = [
        {"Key": f"tag_key_{i}", "Value": f"tag_value_{i}"} for i in range(51)
    ]
    with pytest.raises(ClientError) as client_err:
        conn.tag_resource(ResourceArn=topic_arn, Tags=too_many_tags)
    assert client_err.value.response["Error"]["Message"] == (
        "Could not complete request: tag quota of per resource exceeded"
    )

    # when the request fails, the tags should not be updated
    assert conn.list_tags_for_resource(ResourceArn=topic_arn)["Tags"] == (
        [{"Key": "tag_key_1", "Value": "tag_value_X"}]
    )


@mock_aws
def test_untag_resource_error():
    conn = boto3.client("sns", region_name="us-east-1")
    conn.create_topic(
        Name="some-topic-with-tags", Tags=[{"Key": "tag_key_1", "Value": "tag_value_X"}]
    )

    with pytest.raises(ClientError) as client_err:
        conn.untag_resource(ResourceArn="not-existing-topic", TagKeys=["tag_key_1"])
    assert client_err.value.response["Error"]["Message"] == "Resource does not exist"


@mock_aws
def test_create_fifo_topic():
    conn = boto3.client("sns", region_name="us-east-1")
    response = conn.create_topic(
        Name="test_topic.fifo", Attributes={"FifoTopic": "true"}
    )

    assert "TopicArn" in response

    try:
        conn.create_topic(Name="test_topic", Attributes={"FifoTopic": "true"})
    except ClientError as err:
        assert err.response["Error"]["Code"] == "InvalidParameter"
        assert err.response["Error"]["Message"] == (
            "Fifo Topic names must end with .fifo and must be made up of only "
            "uppercase and lowercase ASCII letters, numbers, underscores, "
            "and hyphens, and must be between 1 and 256 characters long."
        )
        assert err.response["Error"]["Type"] == "Sender"

    try:
        conn.create_topic(Name="test_topic.fifo")
    except ClientError as err:
        assert err.response["Error"]["Code"] == "InvalidParameter"
        assert err.response["Error"]["Message"] == (
            "Topic names must be made up of only uppercase and lowercase "
            "ASCII letters, numbers, underscores, "
            "and hyphens, and must be between 1 and 256 characters long."
        )

    try:
        conn.create_topic(Name="topic.name.fifo", Attributes={"FifoTopic": "true"})
    except ClientError as err:
        assert err.response["Error"]["Code"] == "InvalidParameter"
        assert err.response["Error"]["Message"] == (
            "Fifo Topic names must end with .fifo and must be made up of only "
            "uppercase and lowercase ASCII letters, numbers, underscores, "
            "and hyphens, and must be between 1 and 256 characters long."
        )


@mock_aws
def test_topic_kms_master_key_id_attribute():
    client = boto3.client("sns", region_name="us-west-2")
    resp = client.create_topic(Name="test-sns-no-key-attr")
    topic_arn = resp["TopicArn"]
    resp = client.get_topic_attributes(TopicArn=topic_arn)
    assert "KmsMasterKeyId" not in resp["Attributes"]

    client.set_topic_attributes(
        TopicArn=topic_arn, AttributeName="KmsMasterKeyId", AttributeValue="test-key"
    )
    resp = client.get_topic_attributes(TopicArn=topic_arn)
    assert "KmsMasterKeyId" in resp["Attributes"]
    assert resp["Attributes"]["KmsMasterKeyId"] == "test-key"

    resp = client.create_topic(
        Name="test-sns-with-key-attr", Attributes={"KmsMasterKeyId": "key-id"}
    )
    topic_arn = resp["TopicArn"]
    resp = client.get_topic_attributes(TopicArn=topic_arn)
    assert "KmsMasterKeyId" in resp["Attributes"]
    assert resp["Attributes"]["KmsMasterKeyId"] == "key-id"


@mock_aws
def test_topic_fifo_get_attributes():
    client = boto3.client("sns", region_name="us-east-1")
    resp = client.create_topic(
        Name="test-topic-fifo-get-attr.fifo", Attributes={"FifoTopic": "true"}
    )
    topic_arn = resp["TopicArn"]
    attributes = client.get_topic_attributes(TopicArn=topic_arn)["Attributes"]

    assert "FifoTopic" in attributes
    assert "ContentBasedDeduplication" in attributes

    assert attributes["FifoTopic"] == "true"
    assert attributes["ContentBasedDeduplication"] == "false"

    client.set_topic_attributes(
        TopicArn=topic_arn,
        AttributeName="ContentBasedDeduplication",
        AttributeValue="true",
    )
    attributes = client.get_topic_attributes(TopicArn=topic_arn)["Attributes"]
    assert attributes["ContentBasedDeduplication"] == "true"


@mock_aws
def test_topic_get_attributes():
    client = boto3.client("sns", region_name="us-east-1")
    resp = client.create_topic(Name="test-topic-get-attr")
    topic_arn = resp["TopicArn"]
    attributes = client.get_topic_attributes(TopicArn=topic_arn)["Attributes"]

    assert "FifoTopic" not in attributes
    assert "ContentBasedDeduplication" not in attributes


@mock_aws
def test_topic_get_attributes_with_fifo_false():
    client = boto3.client("sns", region_name="us-east-1")
    resp = client.create_topic(
        Name="test-topic-get-attr-with-fifo-false", Attributes={"FifoTopic": "false"}
    )
    topic_arn = resp["TopicArn"]
    attributes = client.get_topic_attributes(TopicArn=topic_arn)["Attributes"]

    assert "FifoTopic" not in attributes
    assert "ContentBasedDeduplication" not in attributes


@mock_aws
def test_list_config_service_resources():
    sns_client = boto3.client("sns", region_name="us-east-1")
    config_client = boto3.client("config", region_name="us-east-1")

    topic_arns = []
    for i in range(3):
        response = sns_client.create_topic(Name=f"test-topic-{i}")
        topic_arns.append(response["TopicArn"])

    response = config_client.list_discovered_resources(resourceType="AWS::SNS::Topic")
    assert len(response["resourceIdentifiers"]) == 3
    assert all(
        r["resourceType"] == "AWS::SNS::Topic" for r in response["resourceIdentifiers"]
    )

    response = config_client.list_discovered_resources(
        resourceType="AWS::SNS::Topic", resourceIds=[topic_arns[0]]
    )
    assert len(response["resourceIdentifiers"]) == 1
    assert response["resourceIdentifiers"][0]["resourceId"] == topic_arns[0]

    response = config_client.list_discovered_resources(
        resourceType="AWS::SNS::Topic", resourceName="test-topic-1"
    )
    assert len(response["resourceIdentifiers"]) == 1
    assert response["resourceIdentifiers"][0]["resourceName"] == "test-topic-1"

    response = config_client.list_discovered_resources(
        resourceType="AWS::SNS::Topic", limit=2
    )
    assert len(response["resourceIdentifiers"]) == 2
    assert "nextToken" in response

    response = config_client.list_discovered_resources(
        resourceType="AWS::SNS::Topic", limit=2, nextToken=response["nextToken"]
    )
    assert len(response["resourceIdentifiers"]) == 1
    assert "nextToken" not in response


@mock_aws
def test_get_config_resource():
    sns_client = boto3.client("sns", region_name="us-east-1")
    config_client = boto3.client("config", region_name="us-east-1")

    response = sns_client.create_topic(
        Name="config-test.fifo",
        Attributes={
            "FifoTopic": "true",
            "ContentBasedDeduplication": "true",
            "DisplayName": "Test Topic",
            "KmsMasterKeyId": "test-key",
        },
        Tags=[{"Key": "Environment", "Value": "Test"}],
    )
    topic_arn = response["TopicArn"]

    response = config_client.batch_get_resource_config(
        resourceKeys=[
            {
                "resourceType": "AWS::SNS::Topic",
                "resourceId": topic_arn,
            }
        ]
    )

    assert len(response["baseConfigurationItems"]) == 1
    config_item = response["baseConfigurationItems"][0]

    assert config_item["resourceType"] == "AWS::SNS::Topic"
    assert config_item["resourceId"] == topic_arn
    assert config_item["resourceName"] == "config-test.fifo"

    configuration = json.loads(config_item["configuration"])
    assert configuration["displayName"] == "Test Topic"
    assert configuration["kmsMasterKeyId"] == "test-key"
    assert configuration["fifoTopic"] is True
    assert configuration["contentBasedDeduplication"] is True
    tags = json.loads(config_item["supplementaryConfiguration"]["Tags"])
    assert tags == [{"Key": "Environment", "Value": "Test"}]

    response = config_client.batch_get_resource_config(
        resourceKeys=[
            {
                "resourceType": "AWS::SNS::Topic",
                "resourceId": "arn:aws:sns:us-east-1:123456789012:fake",
            }
        ]
    )
    assert len(response["baseConfigurationItems"]) == 0