4
4
5
5
import requests_mock
6
6
7
+ import random
8
+
7
9
from pusher_push_notifications import (
8
10
PushNotifications ,
9
11
PusherAuthError ,
@@ -130,22 +132,21 @@ def test_publish_should_make_correct_http_request(self):
130
132
},
131
133
)
132
134
133
-
134
135
def test_publish_should_fail_if_interests_not_list (self ):
135
136
pn_client = PushNotifications (
136
137
'INSTANCE_ID' ,
137
138
'SECRET_KEY'
138
139
)
139
140
with self .assertRaises (TypeError ):
140
141
pn_client .publish (
141
- interests = False ,
142
- publish_body = {
143
- 'apns' : {
144
- 'aps' : {
145
- 'alert' : 'Hello World!' ,
146
- },
142
+ interests = False ,
143
+ publish_body = {
144
+ 'apns' : {
145
+ 'aps' : {
146
+ 'alert' : 'Hello World!' ,
147
147
},
148
148
},
149
+ },
149
150
)
150
151
151
152
def test_publish_should_fail_if_body_not_dict (self ):
@@ -155,8 +156,8 @@ def test_publish_should_fail_if_body_not_dict(self):
155
156
)
156
157
with self .assertRaises (TypeError ):
157
158
pn_client .publish (
158
- interests = ['donuts' ],
159
- publish_body = False ,
159
+ interests = ['donuts' ],
160
+ publish_body = False ,
160
161
)
161
162
162
163
def test_publish_should_fail_if_no_interests_passed (self ):
@@ -166,15 +167,68 @@ def test_publish_should_fail_if_no_interests_passed(self):
166
167
)
167
168
with self .assertRaises (ValueError ):
168
169
pn_client .publish (
169
- interests = [],
170
+ interests = [],
171
+ publish_body = {
172
+ 'apns' : {
173
+ 'aps' : {
174
+ 'alert' : 'Hello World!' ,
175
+ },
176
+ },
177
+ },
178
+ )
179
+
180
+ def test_publish_should_succeed_if_100_interests_passed (self ):
181
+ pn_client = PushNotifications (
182
+ 'INSTANCE_ID' ,
183
+ 'SECRET_KEY'
184
+ )
185
+ with requests_mock .Mocker () as http_mock :
186
+ http_mock .register_uri (
187
+ requests_mock .ANY ,
188
+ requests_mock .ANY ,
189
+ status_code = 200 ,
190
+ json = {
191
+ 'publishId' : '1234' ,
192
+ },
193
+ )
194
+ pn_client .publish (
195
+ interests = [str (el ) for el in random .sample (range (1000 ), 100 )],
196
+ publish_body = {
197
+ 'apns' : {
198
+ 'aps' : {
199
+ 'alert' : 'Hello World!' ,
200
+ },
201
+ },
202
+ },
203
+ )
204
+
205
+ def test_publish_should_fail_if_too_many_interests_passed (self ):
206
+ pn_client = PushNotifications (
207
+ 'INSTANCE_ID' ,
208
+ 'SECRET_KEY'
209
+ )
210
+ interests_ints = random .sample (range (1000 ), 101 )
211
+
212
+ with requests_mock .Mocker () as http_mock :
213
+ http_mock .register_uri (
214
+ requests_mock .ANY ,
215
+ requests_mock .ANY ,
216
+ status_code = 200 ,
217
+ json = {
218
+ 'publishId' : '1234' ,
219
+ },
220
+ )
221
+ with self .assertRaises (ValueError ):
222
+ pn_client .publish (
223
+ interests = [str (el ) for el in interests_ints ],
170
224
publish_body = {
171
225
'apns' : {
172
226
'aps' : {
173
227
'alert' : 'Hello World!' ,
174
228
},
175
229
},
176
230
},
177
- )
231
+ )
178
232
179
233
def test_publish_should_fail_if_interest_not_a_string (self ):
180
234
pn_client = PushNotifications (
@@ -183,14 +237,14 @@ def test_publish_should_fail_if_interest_not_a_string(self):
183
237
)
184
238
with self .assertRaises (TypeError ):
185
239
pn_client .publish (
186
- interests = [False ],
187
- publish_body = {
188
- 'apns' : {
189
- 'aps' : {
190
- 'alert' : 'Hello World!' ,
191
- },
240
+ interests = [False ],
241
+ publish_body = {
242
+ 'apns' : {
243
+ 'aps' : {
244
+ 'alert' : 'Hello World!' ,
192
245
},
193
246
},
247
+ },
194
248
)
195
249
196
250
def test_publish_should_fail_if_interest_too_long (self ):
@@ -200,14 +254,14 @@ def test_publish_should_fail_if_interest_too_long(self):
200
254
)
201
255
with self .assertRaises (ValueError ):
202
256
pn_client .publish (
203
- interests = ['A' * 200 ],
204
- publish_body = {
205
- 'apns' : {
206
- 'aps' : {
207
- 'alert' : 'Hello World!' ,
208
- },
257
+ interests = ['A' * 200 ],
258
+ publish_body = {
259
+ 'apns' : {
260
+ 'aps' : {
261
+ 'alert' : 'Hello World!' ,
209
262
},
210
263
},
264
+ },
211
265
)
212
266
213
267
def test_publish_should_fail_if_interest_contains_invalid_chars (self ):
@@ -217,25 +271,25 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
217
271
)
218
272
with self .assertRaises (ValueError ):
219
273
pn_client .publish (
220
- interests = ['bad|interest' ],
221
- publish_body = {
222
- 'apns' : {
223
- 'aps' : {
224
- 'alert' : 'Hello World!' ,
225
- },
274
+ interests = ['bad|interest' ],
275
+ publish_body = {
276
+ 'apns' : {
277
+ 'aps' : {
278
+ 'alert' : 'Hello World!' ,
226
279
},
227
280
},
281
+ },
228
282
)
229
283
with self .assertRaises (ValueError ):
230
284
pn_client .publish (
231
- interests = ['bad(interest)' ],
232
- publish_body = {
233
- 'apns' : {
234
- 'aps' : {
235
- 'alert' : 'Hello World!' ,
236
- },
285
+ interests = ['bad(interest)' ],
286
+ publish_body = {
287
+ 'apns' : {
288
+ 'aps' : {
289
+ 'alert' : 'Hello World!' ,
237
290
},
238
291
},
292
+ },
239
293
)
240
294
241
295
def test_publish_should_raise_on_http_4xx_error (self ):
@@ -252,14 +306,14 @@ def test_publish_should_raise_on_http_4xx_error(self):
252
306
)
253
307
with self .assertRaises (PusherValidationError ):
254
308
pn_client .publish (
255
- interests = ['donuts' ],
256
- publish_body = {
257
- 'apns' : {
258
- 'aps' : {
259
- 'alert' : 'Hello World!' ,
260
- },
309
+ interests = ['donuts' ],
310
+ publish_body = {
311
+ 'apns' : {
312
+ 'aps' : {
313
+ 'alert' : 'Hello World!' ,
261
314
},
262
315
},
316
+ },
263
317
)
264
318
265
319
def test_publish_should_raise_on_http_5xx_error (self ):
@@ -276,14 +330,14 @@ def test_publish_should_raise_on_http_5xx_error(self):
276
330
)
277
331
with self .assertRaises (PusherServerError ):
278
332
pn_client .publish (
279
- interests = ['donuts' ],
280
- publish_body = {
281
- 'apns' : {
282
- 'aps' : {
283
- 'alert' : 'Hello World!' ,
284
- },
333
+ interests = ['donuts' ],
334
+ publish_body = {
335
+ 'apns' : {
336
+ 'aps' : {
337
+ 'alert' : 'Hello World!' ,
285
338
},
286
339
},
340
+ },
287
341
)
288
342
289
343
def test_publish_should_raise_on_http_401_error (self ):
@@ -300,14 +354,14 @@ def test_publish_should_raise_on_http_401_error(self):
300
354
)
301
355
with self .assertRaises (PusherAuthError ):
302
356
pn_client .publish (
303
- interests = ['donuts' ],
304
- publish_body = {
305
- 'apns' : {
306
- 'aps' : {
307
- 'alert' : 'Hello World!' ,
308
- },
357
+ interests = ['donuts' ],
358
+ publish_body = {
359
+ 'apns' : {
360
+ 'aps' : {
361
+ 'alert' : 'Hello World!' ,
309
362
},
310
363
},
364
+ },
311
365
)
312
366
313
367
def test_publish_should_raise_on_http_404_error (self ):
@@ -324,12 +378,12 @@ def test_publish_should_raise_on_http_404_error(self):
324
378
)
325
379
with self .assertRaises (PusherMissingInstanceError ):
326
380
pn_client .publish (
327
- interests = ['donuts' ],
328
- publish_body = {
329
- 'apns' : {
330
- 'aps' : {
331
- 'alert' : 'Hello World!' ,
332
- },
381
+ interests = ['donuts' ],
382
+ publish_body = {
383
+ 'apns' : {
384
+ 'aps' : {
385
+ 'alert' : 'Hello World!' ,
333
386
},
334
387
},
388
+ },
335
389
)
0 commit comments