4
4
5
5
import requests_mock
6
6
7
- from pusher_push_notifications import PushNotifications
7
+ from pusher_push_notifications import (
8
+ PushNotifications ,
9
+ PusherAuthError ,
10
+ PusherMissingInstanceError ,
11
+ PusherServerError ,
12
+ PusherValidationError ,
13
+ )
8
14
9
15
10
16
class TestPushNotifications (unittest .TestCase ):
@@ -66,8 +72,12 @@ def test_publish_should_make_correct_http_request(self):
66
72
http_mock .register_uri (
67
73
requests_mock .ANY ,
68
74
requests_mock .ANY ,
75
+ status_code = 200 ,
76
+ json = {
77
+ 'publishId' : '1234' ,
78
+ },
69
79
)
70
- pn_client .publish (
80
+ response = pn_client .publish (
71
81
interests = ['donuts' ],
72
82
publish_body = {
73
83
'apns' : {
@@ -113,15 +123,184 @@ def test_publish_should_make_correct_http_request(self):
113
123
},
114
124
},
115
125
)
126
+ self .assertDictEqual (
127
+ response ,
128
+ {
129
+ 'publishId' : '1234' ,
130
+ },
131
+ )
132
+
133
+
134
+ def test_publish_should_fail_if_interests_not_list (self ):
135
+ pn_client = PushNotifications (
136
+ 'INSTANCE_ID' ,
137
+ 'SECRET_KEY'
138
+ )
139
+ with self .assertRaises (TypeError ):
140
+ pn_client .publish (
141
+ interests = False ,
142
+ publish_body = {
143
+ 'apns' : {
144
+ 'aps' : {
145
+ 'alert' : 'Hello World!' ,
146
+ },
147
+ },
148
+ },
149
+ )
150
+
151
+ def test_publish_should_fail_if_body_not_dict (self ):
152
+ pn_client = PushNotifications (
153
+ 'INSTANCE_ID' ,
154
+ 'SECRET_KEY'
155
+ )
156
+ with self .assertRaises (TypeError ):
157
+ pn_client .publish (
158
+ interests = ['donuts' ],
159
+ publish_body = False ,
160
+ )
161
+
162
+ def test_publish_should_fail_if_no_interests_passed (self ):
163
+ pn_client = PushNotifications (
164
+ 'INSTANCE_ID' ,
165
+ 'SECRET_KEY'
166
+ )
167
+ with self .assertRaises (ValueError ):
168
+ pn_client .publish (
169
+ interests = [],
170
+ publish_body = {
171
+ 'apns' : {
172
+ 'aps' : {
173
+ 'alert' : 'Hello World!' ,
174
+ },
175
+ },
176
+ },
177
+ )
178
+
179
+ def test_publish_should_fail_if_interest_not_a_string (self ):
180
+ pn_client = PushNotifications (
181
+ 'INSTANCE_ID' ,
182
+ 'SECRET_KEY'
183
+ )
184
+ with self .assertRaises (TypeError ):
185
+ pn_client .publish (
186
+ interests = [False ],
187
+ publish_body = {
188
+ 'apns' : {
189
+ 'aps' : {
190
+ 'alert' : 'Hello World!' ,
191
+ },
192
+ },
193
+ },
194
+ )
195
+
196
+ def test_publish_should_fail_if_interest_too_long (self ):
197
+ pn_client = PushNotifications (
198
+ 'INSTANCE_ID' ,
199
+ 'SECRET_KEY'
200
+ )
201
+ with self .assertRaises (ValueError ):
202
+ pn_client .publish (
203
+ interests = ['A' * 200 ],
204
+ publish_body = {
205
+ 'apns' : {
206
+ 'aps' : {
207
+ 'alert' : 'Hello World!' ,
208
+ },
209
+ },
210
+ },
211
+ )
212
+
213
+ def test_publish_should_fail_if_interest_contains_invalid_chars (self ):
214
+ pn_client = PushNotifications (
215
+ 'INSTANCE_ID' ,
216
+ 'SECRET_KEY'
217
+ )
218
+ with self .assertRaises (ValueError ):
219
+ pn_client .publish (
220
+ interests = ['bad-interest' ],
221
+ publish_body = {
222
+ 'apns' : {
223
+ 'aps' : {
224
+ 'alert' : 'Hello World!' ,
225
+ },
226
+ },
227
+ },
228
+ )
229
+ with self .assertRaises (ValueError ):
230
+ pn_client .publish (
231
+ interests = ['bad(interest)' ],
232
+ publish_body = {
233
+ 'apns' : {
234
+ 'aps' : {
235
+ 'alert' : 'Hello World!' ,
236
+ },
237
+ },
238
+ },
239
+ )
240
+
241
+ def test_publish_should_raise_on_http_4xx_error (self ):
242
+ pn_client = PushNotifications (
243
+ 'INSTANCE_ID' ,
244
+ 'SECRET_KEY'
245
+ )
246
+ with requests_mock .Mocker () as http_mock :
247
+ http_mock .register_uri (
248
+ requests_mock .ANY ,
249
+ requests_mock .ANY ,
250
+ status_code = 400 ,
251
+ json = {'error' : 'Invalid request' , 'description' : 'blah' },
252
+ )
253
+ with self .assertRaises (PusherValidationError ):
254
+ pn_client .publish (
255
+ interests = ['donuts' ],
256
+ publish_body = {
257
+ 'apns' : {
258
+ 'aps' : {
259
+ 'alert' : 'Hello World!' ,
260
+ },
261
+ },
262
+ },
263
+ )
264
+
265
+ def test_publish_should_raise_on_http_5xx_error (self ):
266
+ pn_client = PushNotifications (
267
+ 'INSTANCE_ID' ,
268
+ 'SECRET_KEY'
269
+ )
270
+ with requests_mock .Mocker () as http_mock :
271
+ http_mock .register_uri (
272
+ requests_mock .ANY ,
273
+ requests_mock .ANY ,
274
+ status_code = 500 ,
275
+ json = {'error' : 'Server error' , 'description' : 'blah' },
276
+ )
277
+ with self .assertRaises (PusherServerError ):
278
+ pn_client .publish (
279
+ interests = ['donuts' ],
280
+ publish_body = {
281
+ 'apns' : {
282
+ 'aps' : {
283
+ 'alert' : 'Hello World!' ,
284
+ },
285
+ },
286
+ },
287
+ )
116
288
117
- def test_publish_should_fail_if_interests_not_list (self ):
118
- pn_client = PushNotifications (
119
- 'INSTANCE_ID' ,
120
- 'SECRET_KEY'
289
+ def test_publish_should_raise_on_http_401_error (self ):
290
+ pn_client = PushNotifications (
291
+ 'INSTANCE_ID' ,
292
+ 'SECRET_KEY'
293
+ )
294
+ with requests_mock .Mocker () as http_mock :
295
+ http_mock .register_uri (
296
+ requests_mock .ANY ,
297
+ requests_mock .ANY ,
298
+ status_code = 401 ,
299
+ json = {'error' : 'Auth error' , 'description' : 'blah' },
121
300
)
122
- with self .assertRaises (TypeError ):
301
+ with self .assertRaises (PusherAuthError ):
123
302
pn_client .publish (
124
- interests = False ,
303
+ interests = [ 'donuts' ] ,
125
304
publish_body = {
126
305
'apns' : {
127
306
'aps' : {
@@ -131,13 +310,26 @@ def test_publish_should_fail_if_interests_not_list(self):
131
310
},
132
311
)
133
312
134
- def test_publish_should_fail_if_body_not_dict (self ):
135
- pn_client = PushNotifications (
136
- 'INSTANCE_ID' ,
137
- 'SECRET_KEY'
313
+ def test_publish_should_raise_on_http_404_error (self ):
314
+ pn_client = PushNotifications (
315
+ 'INSTANCE_ID' ,
316
+ 'SECRET_KEY'
317
+ )
318
+ with requests_mock .Mocker () as http_mock :
319
+ http_mock .register_uri (
320
+ requests_mock .ANY ,
321
+ requests_mock .ANY ,
322
+ status_code = 404 ,
323
+ json = {'error' : 'Instance not found' , 'description' : 'blah' },
138
324
)
139
- with self .assertRaises (TypeError ):
325
+ with self .assertRaises (PusherMissingInstanceError ):
140
326
pn_client .publish (
141
327
interests = ['donuts' ],
142
- publish_body = False ,
328
+ publish_body = {
329
+ 'apns' : {
330
+ 'aps' : {
331
+ 'alert' : 'Hello World!' ,
332
+ },
333
+ },
334
+ },
143
335
)
0 commit comments