This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
forked from getmoto/moto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_topics.py
160 lines (135 loc) · 5.57 KB
/
test_topics.py
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
from __future__ import unicode_literals
import boto
import json
import six
import sure # noqa
from boto.exception import BotoServerError
from moto import mock_sns_deprecated
from moto.sns.models import DEFAULT_EFFECTIVE_DELIVERY_POLICY, DEFAULT_PAGE_SIZE
@mock_sns_deprecated
def test_create_and_delete_topic():
conn = boto.connect_sns()
conn.create_topic("some-topic")
topics_json = conn.get_all_topics()
topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"]
topics.should.have.length_of(1)
topics[0]['TopicArn'].should.equal(
"arn:aws:sns:{0}:123456789012:some-topic"
.format(conn.region.name)
)
# Delete the topic
conn.delete_topic(topics[0]['TopicArn'])
# And there should now be 0 topics
topics_json = conn.get_all_topics()
topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"]
topics.should.have.length_of(0)
@mock_sns_deprecated
def test_get_missing_topic():
conn = boto.connect_sns()
conn.get_topic_attributes.when.called_with(
"a-fake-arn").should.throw(BotoServerError)
@mock_sns_deprecated
def test_create_topic_in_multiple_regions():
for region in ['us-west-1', 'us-west-2']:
conn = boto.sns.connect_to_region(region)
conn.create_topic("some-topic")
list(conn.get_all_topics()["ListTopicsResponse"][
"ListTopicsResult"]["Topics"]).should.have.length_of(1)
@mock_sns_deprecated
def test_topic_corresponds_to_region():
for region in ['us-east-1', 'us-west-2']:
conn = boto.sns.connect_to_region(region)
conn.create_topic("some-topic")
topics_json = conn.get_all_topics()
topic_arn = topics_json["ListTopicsResponse"][
"ListTopicsResult"]["Topics"][0]['TopicArn']
topic_arn.should.equal(
"arn:aws:sns:{0}:123456789012:some-topic".format(region))
@mock_sns_deprecated
def test_topic_attributes():
conn = boto.connect_sns()
conn.create_topic("some-topic")
topics_json = conn.get_all_topics()
topic_arn = topics_json["ListTopicsResponse"][
"ListTopicsResult"]["Topics"][0]['TopicArn']
attributes = conn.get_topic_attributes(topic_arn)['GetTopicAttributesResponse'][
'GetTopicAttributesResult']['Attributes']
attributes["TopicArn"].should.equal(
"arn:aws:sns:{0}:123456789012:some-topic"
.format(conn.region.name)
)
attributes["Owner"].should.equal(123456789012)
json.loads(attributes["Policy"]).should.equal({
"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",
"SNS:Receive",
],
"Resource": "arn:aws:sns:us-east-1:123456789012:some-topic",
"Condition": {
"StringEquals": {
"AWS:SourceOwner": "123456789012"
}
}
}]
})
attributes["DisplayName"].should.equal("")
attributes["SubscriptionsPending"].should.equal(0)
attributes["SubscriptionsConfirmed"].should.equal(0)
attributes["SubscriptionsDeleted"].should.equal(0)
attributes["DeliveryPolicy"].should.equal("")
json.loads(attributes["EffectiveDeliveryPolicy"]).should.equal(
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"
if six.PY2:
policy = json.dumps({b"foo": b"bar"})
displayname = b"My display name"
delivery = {b"http": {b"defaultHealthyRetryPolicy": {b"numRetries": 5}}}
else:
policy = json.dumps({u"foo": u"bar"})
displayname = u"My display name"
delivery = {u"http": {u"defaultHealthyRetryPolicy": {u"numRetries": 5}}}
conn.set_topic_attributes(topic_arn, "Policy", policy)
conn.set_topic_attributes(topic_arn, "DisplayName", displayname)
conn.set_topic_attributes(topic_arn, "DeliveryPolicy", delivery)
attributes = conn.get_topic_attributes(topic_arn)['GetTopicAttributesResponse'][
'GetTopicAttributesResult']['Attributes']
attributes["Policy"].should.equal('{"foo": "bar"}')
attributes["DisplayName"].should.equal("My display name")
attributes["DeliveryPolicy"].should.equal(
"{'http': {'defaultHealthyRetryPolicy': {'numRetries': 5}}}")
@mock_sns_deprecated
def test_topic_paging():
conn = boto.connect_sns()
for index in range(DEFAULT_PAGE_SIZE + int(DEFAULT_PAGE_SIZE / 2)):
conn.create_topic("some-topic_" + str(index))
topics_json = conn.get_all_topics()
topics_list = topics_json["ListTopicsResponse"][
"ListTopicsResult"]["Topics"]
next_token = topics_json["ListTopicsResponse"][
"ListTopicsResult"]["NextToken"]
len(topics_list).should.equal(DEFAULT_PAGE_SIZE)
next_token.should.equal(DEFAULT_PAGE_SIZE)
topics_json = conn.get_all_topics(next_token=next_token)
topics_list = topics_json["ListTopicsResponse"][
"ListTopicsResult"]["Topics"]
next_token = topics_json["ListTopicsResponse"][
"ListTopicsResult"]["NextToken"]
topics_list.should.have.length_of(int(DEFAULT_PAGE_SIZE / 2))
next_token.should.equal(None)