File: test_connection.py

package info (click to toggle)
python-boto 2.49.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,888 kB
  • sloc: python: 86,396; makefile: 112
file content (281 lines) | stat: -rw-r--r-- 11,190 bytes parent folder | download | duplicates (13)
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
#!/usr/bin/env python
# Copyright (c) 2013 Amazon.com, Inc. or its affiliates.  All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
import json
from tests.unit import unittest
from tests.unit import AWSMockServiceTestCase
from mock import Mock

from boto.sns.connection import SNSConnection

QUEUE_POLICY = {
    u'Policy':
        (u'{"Version":"2008-10-17","Id":"arn:aws:sqs:us-east-1:'
         'idnum:testqueuepolicy/SQSDefaultPolicy","Statement":'
         '[{"Sid":"sidnum","Effect":"Allow","Principal":{"AWS":"*"},'
         '"Action":"SQS:GetQueueUrl","Resource":'
         '"arn:aws:sqs:us-east-1:idnum:testqueuepolicy"}]}')}


class TestSNSConnection(AWSMockServiceTestCase):
    connection_class = SNSConnection

    def setUp(self):
        super(TestSNSConnection, self).setUp()

    def default_body(self):
        return b"{}"

    def test_sqs_with_existing_policy(self):
        self.set_http_response(status_code=200)

        queue = Mock()
        queue.get_attributes.return_value = QUEUE_POLICY
        queue.arn = 'arn:aws:sqs:us-east-1:idnum:queuename'

        self.service_connection.subscribe_sqs_queue('topic_arn', queue)
        self.assert_request_parameters({
               'Action': 'Subscribe',
               'ContentType': 'JSON',
               'Endpoint': 'arn:aws:sqs:us-east-1:idnum:queuename',
               'Protocol': 'sqs',
               'TopicArn': 'topic_arn',
               'Version': '2010-03-31',
        }, ignore_params_values=[])

        # Verify that the queue policy was properly updated.
        actual_policy = json.loads(queue.set_attribute.call_args[0][1])
        self.assertEqual(actual_policy['Version'], '2008-10-17')
        # A new statement should be appended to the end of the statement list.
        self.assertEqual(len(actual_policy['Statement']), 2)
        self.assertEqual(actual_policy['Statement'][1]['Action'],
                         'SQS:SendMessage')

    def test_sqs_with_no_previous_policy(self):
        self.set_http_response(status_code=200)

        queue = Mock()
        queue.get_attributes.return_value = {}
        queue.arn = 'arn:aws:sqs:us-east-1:idnum:queuename'

        self.service_connection.subscribe_sqs_queue('topic_arn', queue)
        self.assert_request_parameters({
               'Action': 'Subscribe',
               'ContentType': 'JSON',
               'Endpoint': 'arn:aws:sqs:us-east-1:idnum:queuename',
               'Protocol': 'sqs',
               'TopicArn': 'topic_arn',
               'Version': '2010-03-31',
        }, ignore_params_values=[])
        actual_policy = json.loads(queue.set_attribute.call_args[0][1])
        # Only a single statement should be part of the policy.
        self.assertEqual(len(actual_policy['Statement']), 1)

    def test_publish_with_positional_args(self):
        self.set_http_response(status_code=200)

        self.service_connection.publish('topic', 'message', 'subject')
        self.assert_request_parameters({
            'Action': 'Publish',
            'TopicArn': 'topic',
            'Subject': 'subject',
            'Message': 'message',
        }, ignore_params_values=['Version', 'ContentType'])

    def test_publish_with_kwargs(self):
        self.set_http_response(status_code=200)

        self.service_connection.publish(topic='topic',
                                        message='message',
                                        subject='subject')
        self.assert_request_parameters({
            'Action': 'Publish',
            'TopicArn': 'topic',
            'Subject': 'subject',
            'Message': 'message',
        }, ignore_params_values=['Version', 'ContentType'])

    def test_publish_with_target_arn(self):
        self.set_http_response(status_code=200)

        self.service_connection.publish(target_arn='target_arn',
                                        message='message',
                                        subject='subject')
        self.assert_request_parameters({
            'Action': 'Publish',
            'TargetArn': 'target_arn',
            'Subject': 'subject',
            'Message': 'message',
        }, ignore_params_values=['Version', 'ContentType'])

    def test_create_platform_application(self):
        self.set_http_response(status_code=200)

        self.service_connection.create_platform_application(
            name='MyApp',
            platform='APNS',
            attributes={
                'PlatformPrincipal': 'a ssl certificate',
                'PlatformCredential': 'a private key'
            }
        )
        self.assert_request_parameters({
            'Action': 'CreatePlatformApplication',
            'Name': 'MyApp',
            'Platform': 'APNS',
            'Attributes.entry.1.key': 'PlatformCredential',
            'Attributes.entry.1.value': 'a private key',
            'Attributes.entry.2.key': 'PlatformPrincipal',
            'Attributes.entry.2.value': 'a ssl certificate',
        }, ignore_params_values=['Version', 'ContentType'])

    def test_set_platform_application_attributes(self):
        self.set_http_response(status_code=200)

        self.service_connection.set_platform_application_attributes(
            platform_application_arn='arn:myapp',
            attributes={'PlatformPrincipal': 'a ssl certificate',
                        'PlatformCredential': 'a private key'})
        self.assert_request_parameters({
            'Action': 'SetPlatformApplicationAttributes',
            'PlatformApplicationArn': 'arn:myapp',
            'Attributes.entry.1.key': 'PlatformCredential',
            'Attributes.entry.1.value': 'a private key',
            'Attributes.entry.2.key': 'PlatformPrincipal',
            'Attributes.entry.2.value': 'a ssl certificate',
        }, ignore_params_values=['Version', 'ContentType'])

    def test_create_platform_endpoint(self):
        self.set_http_response(status_code=200)

        self.service_connection.create_platform_endpoint(
            platform_application_arn='arn:myapp',
            token='abcde12345',
            custom_user_data='john',
            attributes={'Enabled': False})
        self.assert_request_parameters({
            'Action': 'CreatePlatformEndpoint',
            'PlatformApplicationArn': 'arn:myapp',
            'Token': 'abcde12345',
            'CustomUserData': 'john',
            'Attributes.entry.1.key': 'Enabled',
            'Attributes.entry.1.value': False,
        }, ignore_params_values=['Version', 'ContentType'])

    def test_set_endpoint_attributes(self):
        self.set_http_response(status_code=200)

        self.service_connection.set_endpoint_attributes(
            endpoint_arn='arn:myendpoint',
            attributes={'CustomUserData': 'john',
                        'Enabled': False})
        self.assert_request_parameters({
            'Action': 'SetEndpointAttributes',
            'EndpointArn': 'arn:myendpoint',
            'Attributes.entry.1.key': 'CustomUserData',
            'Attributes.entry.1.value': 'john',
            'Attributes.entry.2.key': 'Enabled',
            'Attributes.entry.2.value': False,
        }, ignore_params_values=['Version', 'ContentType'])

    def test_message_is_required(self):
        self.set_http_response(status_code=200)

        with self.assertRaises(TypeError):
            self.service_connection.publish(topic='topic', subject='subject')

    def test_publish_with_json(self):
        self.set_http_response(status_code=200)

        self.service_connection.publish(
            message=json.dumps({
                'default': 'Ignored.',
                'GCM': {
                    'data': 'goes here',
                }
            }),
            message_structure='json',
            subject='subject',
            target_arn='target_arn'
        )
        self.assert_request_parameters({
            'Action': 'Publish',
            'TargetArn': 'target_arn',
            'Subject': 'subject',
            'MessageStructure': 'json',
        }, ignore_params_values=['Version', 'ContentType', 'Message'])
        self.assertDictEqual(
            json.loads(self.actual_request.params["Message"]),
            {"default": "Ignored.", "GCM": {"data": "goes here"}})

    def test_publish_with_utf8_message(self):
        self.set_http_response(status_code=200)
        subject = message = u'We \u2665 utf-8'.encode('utf-8')
        self.service_connection.publish('topic', message, subject)
        self.assert_request_parameters({
            'Action': 'Publish',
            'TopicArn': 'topic',
            'Subject': subject,
            'Message': message,
        }, ignore_params_values=['Version', 'ContentType'])

    def test_publish_with_attributes(self):
        self.set_http_response(status_code=200)

        self.service_connection.publish(
            message=json.dumps({
                'default': 'Ignored.',
                'GCM': {
                    'data': 'goes here',
                }
            }, sort_keys=True),
            message_structure='json',
            subject='subject',
            target_arn='target_arn',
            message_attributes={
                'name1': {
                    'data_type': 'Number',
                    'string_value': '42'
                },
                'name2': {
                    'data_type': 'String',
                    'string_value': 'Bob'
                },
            },
        )
        self.assert_request_parameters({
            'Action': 'Publish',
            'TargetArn': 'target_arn',
            'Subject': 'subject',
            'Message': '{"GCM": {"data": "goes here"}, "default": "Ignored."}',
            'MessageStructure': 'json',
            'MessageAttributes.entry.1.Name': 'name1',
            'MessageAttributes.entry.1.Value.DataType': 'Number',
            'MessageAttributes.entry.1.Value.StringValue': '42',
            'MessageAttributes.entry.2.Name': 'name2',
            'MessageAttributes.entry.2.Value.DataType': 'String',
            'MessageAttributes.entry.2.Value.StringValue': 'Bob',
        }, ignore_params_values=['Version', 'ContentType'])


if __name__ == '__main__':
    unittest.main()