16
16
17
17
18
18
class TestPushNotificationsInterests (unittest .TestCase ):
19
- def test_publish_should_make_correct_http_request (self ):
19
+ def test_publish_to_interests_should_make_correct_http_request (self ):
20
+ pn_client = PushNotifications (
21
+ 'INSTANCE_ID' ,
22
+ 'SECRET_KEY'
23
+ )
24
+ with requests_mock .Mocker () as http_mock :
25
+ http_mock .register_uri (
26
+ requests_mock .ANY ,
27
+ requests_mock .ANY ,
28
+ status_code = 200 ,
29
+ json = {
30
+ 'publishId' : '1234' ,
31
+ },
32
+ )
33
+ response = pn_client .publish_to_interests (
34
+ interests = ['donuts' ],
35
+ publish_body = {
36
+ 'apns' : {
37
+ 'aps' : {
38
+ 'alert' : 'Hello World!' ,
39
+ },
40
+ },
41
+ },
42
+ )
43
+ req = http_mock .request_history [0 ]
44
+
45
+ method = req .method
46
+ path = req .path
47
+ headers = dict (req ._request .headers .lower_items ())
48
+ body = req .json ()
49
+
50
+ self .assertEqual (
51
+ method ,
52
+ 'POST' ,
53
+ )
54
+ self .assertEqual (
55
+ path ,
56
+ '/publish_api/v1/instances/instance_id/publishes' ,
57
+ )
58
+ self .assertDictEqual (
59
+ headers ,
60
+ {
61
+ 'content-type' : 'application/json' ,
62
+ 'content-length' : '69' ,
63
+ 'authorization' : 'Bearer SECRET_KEY' ,
64
+ 'x-pusher-library' : 'pusher-push-notifications-python 1.0.1' ,
65
+ 'host' : 'instance_id.pushnotifications.pusher.com' ,
66
+ },
67
+ )
68
+ self .assertDictEqual (
69
+ body ,
70
+ {
71
+ 'interests' : ['donuts' ],
72
+ 'apns' : {
73
+ 'aps' : {
74
+ 'alert' : 'Hello World!' ,
75
+ },
76
+ },
77
+ },
78
+ )
79
+ self .assertDictEqual (
80
+ response ,
81
+ {
82
+ 'publishId' : '1234' ,
83
+ },
84
+ )
85
+
86
+ def test_deprecated_alias_still_works (self ):
20
87
pn_client = PushNotifications (
21
88
'INSTANCE_ID' ,
22
89
'SECRET_KEY'
@@ -83,13 +150,13 @@ def test_publish_should_make_correct_http_request(self):
83
150
},
84
151
)
85
152
86
- def test_publish_should_fail_if_interests_not_list (self ):
153
+ def test_publish_to_interests_should_fail_if_interests_not_list (self ):
87
154
pn_client = PushNotifications (
88
155
'INSTANCE_ID' ,
89
156
'SECRET_KEY'
90
157
)
91
158
with self .assertRaises (TypeError ) as e :
92
- pn_client .publish (
159
+ pn_client .publish_to_interests (
93
160
interests = False ,
94
161
publish_body = {
95
162
'apns' : {
@@ -101,25 +168,25 @@ def test_publish_should_fail_if_interests_not_list(self):
101
168
)
102
169
self .assertIn ('interests must be a list' , str (e .exception ))
103
170
104
- def test_publish_should_fail_if_body_not_dict (self ):
171
+ def test_publish_to_interests_should_fail_if_body_not_dict (self ):
105
172
pn_client = PushNotifications (
106
173
'INSTANCE_ID' ,
107
174
'SECRET_KEY'
108
175
)
109
176
with self .assertRaises (TypeError ) as e :
110
- pn_client .publish (
177
+ pn_client .publish_to_interests (
111
178
interests = ['donuts' ],
112
179
publish_body = False ,
113
180
)
114
181
self .assertIn ('publish_body must be a dictionary' , str (e .exception ))
115
182
116
- def test_publish_should_fail_if_no_interests_passed (self ):
183
+ def test_publish_to_interests_should_fail_if_no_interests_passed (self ):
117
184
pn_client = PushNotifications (
118
185
'INSTANCE_ID' ,
119
186
'SECRET_KEY'
120
187
)
121
188
with self .assertRaises (ValueError ) as e :
122
- pn_client .publish (
189
+ pn_client .publish_to_interests (
123
190
interests = [],
124
191
publish_body = {
125
192
'apns' : {
@@ -131,7 +198,7 @@ def test_publish_should_fail_if_no_interests_passed(self):
131
198
)
132
199
self .assertIn ('must target at least one interest' , str (e .exception ))
133
200
134
- def test_publish_should_succeed_if_100_interests_passed (self ):
201
+ def test_publish_to_interests_should_succeed_if_100_interests_passed (self ):
135
202
pn_client = PushNotifications (
136
203
'INSTANCE_ID' ,
137
204
'SECRET_KEY'
@@ -145,7 +212,7 @@ def test_publish_should_succeed_if_100_interests_passed(self):
145
212
'publishId' : '1234' ,
146
213
},
147
214
)
148
- pn_client .publish (
215
+ pn_client .publish_to_interests (
149
216
interests = ['interest-' + str (i ) for i in range (0 , 100 )],
150
217
publish_body = {
151
218
'apns' : {
@@ -156,7 +223,7 @@ def test_publish_should_succeed_if_100_interests_passed(self):
156
223
},
157
224
)
158
225
159
- def test_publish_should_fail_if_too_many_interests_passed (self ):
226
+ def test_publish_to_interests_should_fail_if_too_many_interests_passed (self ):
160
227
pn_client = PushNotifications (
161
228
'INSTANCE_ID' ,
162
229
'SECRET_KEY'
@@ -171,7 +238,7 @@ def test_publish_should_fail_if_too_many_interests_passed(self):
171
238
},
172
239
)
173
240
with self .assertRaises (ValueError ) as e :
174
- pn_client .publish (
241
+ pn_client .publish_to_interests (
175
242
interests = ['interest-' + str (i ) for i in range (0 , 101 )],
176
243
publish_body = {
177
244
'apns' : {
@@ -183,13 +250,13 @@ def test_publish_should_fail_if_too_many_interests_passed(self):
183
250
)
184
251
self .assertIn ('Number of interests (101) exceeds maximum' , str (e .exception ))
185
252
186
- def test_publish_should_fail_if_interest_not_a_string (self ):
253
+ def test_publish_to_interests_should_fail_if_interest_not_a_string (self ):
187
254
pn_client = PushNotifications (
188
255
'INSTANCE_ID' ,
189
256
'SECRET_KEY'
190
257
)
191
258
with self .assertRaises (TypeError ) as e :
192
- pn_client .publish (
259
+ pn_client .publish_to_interests (
193
260
interests = [False ],
194
261
publish_body = {
195
262
'apns' : {
@@ -201,13 +268,13 @@ def test_publish_should_fail_if_interest_not_a_string(self):
201
268
)
202
269
self .assertIn ('Interest False is not a string' , str (e .exception ))
203
270
204
- def test_publish_should_fail_if_interest_too_long (self ):
271
+ def test_publish_to_interests_should_fail_if_interest_too_long (self ):
205
272
pn_client = PushNotifications (
206
273
'INSTANCE_ID' ,
207
274
'SECRET_KEY'
208
275
)
209
276
with self .assertRaises (ValueError ) as e :
210
- pn_client .publish (
277
+ pn_client .publish_to_interests (
211
278
interests = ['A' * 200 ],
212
279
publish_body = {
213
280
'apns' : {
@@ -219,13 +286,13 @@ def test_publish_should_fail_if_interest_too_long(self):
219
286
)
220
287
self .assertIn ('longer than the maximum of 164 chars' , str (e .exception ))
221
288
222
- def test_publish_should_fail_if_interest_contains_invalid_chars (self ):
289
+ def test_publish_to_interests_should_fail_if_interest_contains_invalid_chars (self ):
223
290
pn_client = PushNotifications (
224
291
'INSTANCE_ID' ,
225
292
'SECRET_KEY'
226
293
)
227
294
with self .assertRaises (ValueError ) as e :
228
- pn_client .publish (
295
+ pn_client .publish_to_interests (
229
296
interests = ['bad:interest' ],
230
297
publish_body = {
231
298
'apns' : {
@@ -238,7 +305,7 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
238
305
self .assertIn ('"bad:interest" contains a forbidden character' , str (e .exception ))
239
306
240
307
with self .assertRaises (ValueError ) as e :
241
- pn_client .publish (
308
+ pn_client .publish_to_interests (
242
309
interests = ['bad|interest' ],
243
310
publish_body = {
244
311
'apns' : {
@@ -251,7 +318,7 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
251
318
self .assertIn ('"bad|interest" contains a forbidden character' , str (e .exception ))
252
319
253
320
with self .assertRaises (ValueError ) as e :
254
- pn_client .publish (
321
+ pn_client .publish_to_interests (
255
322
interests = ['bad(interest)' ],
256
323
publish_body = {
257
324
'apns' : {
@@ -263,7 +330,7 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
263
330
)
264
331
self .assertIn ('"bad(interest)" contains a forbidden character' , str (e .exception ))
265
332
266
- def test_publish_should_raise_on_http_4xx_error (self ):
333
+ def test_publish_to_interests_should_raise_on_http_4xx_error (self ):
267
334
pn_client = PushNotifications (
268
335
'INSTANCE_ID' ,
269
336
'SECRET_KEY'
@@ -276,7 +343,7 @@ def test_publish_should_raise_on_http_4xx_error(self):
276
343
json = {'error' : 'Invalid request' , 'description' : 'blah' },
277
344
)
278
345
with self .assertRaises (PusherValidationError ) as e :
279
- pn_client .publish (
346
+ pn_client .publish_to_interests (
280
347
interests = ['donuts' ],
281
348
publish_body = {
282
349
'apns' : {
@@ -288,7 +355,7 @@ def test_publish_should_raise_on_http_4xx_error(self):
288
355
)
289
356
self .assertIn ('Invalid request: blah' , str (e .exception ))
290
357
291
- def test_publish_should_raise_on_http_5xx_error (self ):
358
+ def test_publish_to_interests_should_raise_on_http_5xx_error (self ):
292
359
pn_client = PushNotifications (
293
360
'INSTANCE_ID' ,
294
361
'SECRET_KEY'
@@ -301,7 +368,7 @@ def test_publish_should_raise_on_http_5xx_error(self):
301
368
json = {'error' : 'Server error' , 'description' : 'blah' },
302
369
)
303
370
with self .assertRaises (PusherServerError ) as e :
304
- pn_client .publish (
371
+ pn_client .publish_to_interests (
305
372
interests = ['donuts' ],
306
373
publish_body = {
307
374
'apns' : {
@@ -313,7 +380,7 @@ def test_publish_should_raise_on_http_5xx_error(self):
313
380
)
314
381
self .assertIn ('Server error: blah' , str (e .exception ))
315
382
316
- def test_publish_should_raise_on_http_401_error (self ):
383
+ def test_publish_to_interests_should_raise_on_http_401_error (self ):
317
384
pn_client = PushNotifications (
318
385
'INSTANCE_ID' ,
319
386
'SECRET_KEY'
@@ -326,7 +393,7 @@ def test_publish_should_raise_on_http_401_error(self):
326
393
json = {'error' : 'Auth error' , 'description' : 'blah' },
327
394
)
328
395
with self .assertRaises (PusherAuthError ) as e :
329
- pn_client .publish (
396
+ pn_client .publish_to_interests (
330
397
interests = ['donuts' ],
331
398
publish_body = {
332
399
'apns' : {
@@ -338,7 +405,7 @@ def test_publish_should_raise_on_http_401_error(self):
338
405
)
339
406
self .assertIn ('Auth error: blah' , str (e .exception ))
340
407
341
- def test_publish_should_raise_on_http_404_error (self ):
408
+ def test_publish_to_interests_should_raise_on_http_404_error (self ):
342
409
pn_client = PushNotifications (
343
410
'INSTANCE_ID' ,
344
411
'SECRET_KEY'
@@ -351,7 +418,7 @@ def test_publish_should_raise_on_http_404_error(self):
351
418
json = {'error' : 'Instance not found' , 'description' : 'blah' },
352
419
)
353
420
with self .assertRaises (PusherMissingInstanceError ) as e :
354
- pn_client .publish (
421
+ pn_client .publish_to_interests (
355
422
interests = ['donuts' ],
356
423
publish_body = {
357
424
'apns' : {
@@ -364,7 +431,7 @@ def test_publish_should_raise_on_http_404_error(self):
364
431
self .assertIn ('Instance not found: blah' , str (e .exception ))
365
432
366
433
367
- def test_publish_should_error_correctly_if_error_not_json (self ):
434
+ def test_publish_to_interests_should_error_correctly_if_error_not_json (self ):
368
435
pn_client = PushNotifications (
369
436
'INSTANCE_ID' ,
370
437
'SECRET_KEY'
@@ -377,7 +444,7 @@ def test_publish_should_error_correctly_if_error_not_json(self):
377
444
text = '<notjson></notjson>' ,
378
445
)
379
446
with self .assertRaises (PusherServerError ) as e :
380
- pn_client .publish (
447
+ pn_client .publish_to_interests (
381
448
interests = ['donuts' ],
382
449
publish_body = {
383
450
'apns' : {
@@ -389,7 +456,7 @@ def test_publish_should_error_correctly_if_error_not_json(self):
389
456
)
390
457
self .assertIn ('Unknown error: no description' , str (e .exception ))
391
458
392
- def test_publish_should_handle_not_json_success (self ):
459
+ def test_publish_to_interests_should_handle_not_json_success (self ):
393
460
pn_client = PushNotifications (
394
461
'INSTANCE_ID' ,
395
462
'SECRET_KEY'
@@ -402,7 +469,7 @@ def test_publish_should_handle_not_json_success(self):
402
469
text = '<notjson></notjson>' ,
403
470
)
404
471
with self .assertRaises (PusherBadResponseError ) as e :
405
- pn_client .publish (
472
+ pn_client .publish_to_interests (
406
473
interests = ['donuts' ],
407
474
publish_body = {
408
475
'apns' : {
0 commit comments