Skip to content

Commit 32ddedb

Browse files
Merge pull request #5 from pusher/test-error-messages
Assert correct errors are thrown
2 parents 482ab20 + 7786ae7 commit 32ddedb

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

pusher_push_notifications/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ def publish(self, interests, publish_body):
113113
if not interests:
114114
raise ValueError('Publishes must target at least one interest')
115115
if len(interests) > MAX_NUMBER_OF_INTERESTS:
116-
raise ValueError('Number of interests ({}) exceeds maximum of {}'.format(
117-
len(interests),
118-
MAX_NUMBER_OF_INTERESTS
119-
))
116+
raise ValueError(
117+
'Number of interests ({}) exceeds maximum of {}'.format(
118+
len(interests),
119+
MAX_NUMBER_OF_INTERESTS,
120+
),
121+
)
120122
for interest in interests:
121123
if not isinstance(interest, six.string_types):
122124
raise TypeError(

tests/test_push_notifications.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,29 @@ def test_constructor_should_accept_valid_params(self):
2222
)
2323

2424
def test_constructor_should_fail_if_instance_id_not_string(self):
25-
with self.assertRaises(TypeError):
25+
with self.assertRaises(TypeError) as e:
2626
PushNotifications(
2727
instance_id=False,
2828
secret_key='1234',
2929
)
30+
self.assertIn('instance_id must be a string', str(e.exception))
3031

3132
def test_constructor_should_fail_if_secret_key_not_string(self):
32-
with self.assertRaises(TypeError):
33+
with self.assertRaises(TypeError) as e:
3334
PushNotifications(
3435
instance_id='1234',
3536
secret_key=False,
3637
)
38+
self.assertIn('secret_key must be a string', str(e.exception))
3739

3840
def test_constructor_should_fail_if_endpoint_not_string(self):
39-
with self.assertRaises(TypeError):
41+
with self.assertRaises(TypeError) as e:
4042
PushNotifications(
4143
instance_id='1234',
4244
secret_key='1234',
4345
endpoint=False,
4446
)
47+
self.assertIn('endpoint must be a string', str(e.exception))
4548

4649
def test_constructor_should_set_endpoint_default(self):
4750
pn_client = PushNotifications(
@@ -136,7 +139,7 @@ def test_publish_should_fail_if_interests_not_list(self):
136139
'INSTANCE_ID',
137140
'SECRET_KEY'
138141
)
139-
with self.assertRaises(TypeError):
142+
with self.assertRaises(TypeError) as e:
140143
pn_client.publish(
141144
interests=False,
142145
publish_body={
@@ -147,24 +150,26 @@ def test_publish_should_fail_if_interests_not_list(self):
147150
},
148151
},
149152
)
153+
self.assertIn('interests must be a list', str(e.exception))
150154

151155
def test_publish_should_fail_if_body_not_dict(self):
152156
pn_client = PushNotifications(
153157
'INSTANCE_ID',
154158
'SECRET_KEY'
155159
)
156-
with self.assertRaises(TypeError):
160+
with self.assertRaises(TypeError) as e:
157161
pn_client.publish(
158162
interests=['donuts'],
159163
publish_body=False,
160164
)
165+
self.assertIn('publish_body must be a dictionary', str(e.exception))
161166

162167
def test_publish_should_fail_if_no_interests_passed(self):
163168
pn_client = PushNotifications(
164169
'INSTANCE_ID',
165170
'SECRET_KEY'
166171
)
167-
with self.assertRaises(ValueError):
172+
with self.assertRaises(ValueError) as e:
168173
pn_client.publish(
169174
interests=[],
170175
publish_body={
@@ -175,6 +180,7 @@ def test_publish_should_fail_if_no_interests_passed(self):
175180
},
176181
},
177182
)
183+
self.assertIn('must target at least one interest', str(e.exception))
178184

179185
def test_publish_should_succeed_if_100_interests_passed(self):
180186
pn_client = PushNotifications(
@@ -215,7 +221,7 @@ def test_publish_should_fail_if_too_many_interests_passed(self):
215221
'publishId': '1234',
216222
},
217223
)
218-
with self.assertRaises(ValueError):
224+
with self.assertRaises(ValueError) as e:
219225
pn_client.publish(
220226
interests=['interest-' + str(i) for i in range(0, 101)],
221227
publish_body={
@@ -226,13 +232,14 @@ def test_publish_should_fail_if_too_many_interests_passed(self):
226232
},
227233
},
228234
)
235+
self.assertIn('Number of interests (101) exceeds maximum', str(e.exception))
229236

230237
def test_publish_should_fail_if_interest_not_a_string(self):
231238
pn_client = PushNotifications(
232239
'INSTANCE_ID',
233240
'SECRET_KEY'
234241
)
235-
with self.assertRaises(TypeError):
242+
with self.assertRaises(TypeError) as e:
236243
pn_client.publish(
237244
interests=[False],
238245
publish_body={
@@ -243,13 +250,14 @@ def test_publish_should_fail_if_interest_not_a_string(self):
243250
},
244251
},
245252
)
253+
self.assertIn('Interest False is not a string', str(e.exception))
246254

247255
def test_publish_should_fail_if_interest_too_long(self):
248256
pn_client = PushNotifications(
249257
'INSTANCE_ID',
250258
'SECRET_KEY'
251259
)
252-
with self.assertRaises(ValueError):
260+
with self.assertRaises(ValueError) as e:
253261
pn_client.publish(
254262
interests=['A'*200],
255263
publish_body={
@@ -260,13 +268,14 @@ def test_publish_should_fail_if_interest_too_long(self):
260268
},
261269
},
262270
)
271+
self.assertIn('longer than the maximum of 164 chars', str(e.exception))
263272

264273
def test_publish_should_fail_if_interest_contains_invalid_chars(self):
265274
pn_client = PushNotifications(
266275
'INSTANCE_ID',
267276
'SECRET_KEY'
268277
)
269-
with self.assertRaises(ValueError):
278+
with self.assertRaises(ValueError) as e:
270279
pn_client.publish(
271280
interests=['bad|interest'],
272281
publish_body={
@@ -277,7 +286,9 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
277286
},
278287
},
279288
)
280-
with self.assertRaises(ValueError):
289+
self.assertIn('"bad|interest" contains a forbidden character', str(e.exception))
290+
291+
with self.assertRaises(ValueError) as e:
281292
pn_client.publish(
282293
interests=['bad(interest)'],
283294
publish_body={
@@ -288,6 +299,7 @@ def test_publish_should_fail_if_interest_contains_invalid_chars(self):
288299
},
289300
},
290301
)
302+
self.assertIn('"bad(interest)" contains a forbidden character', str(e.exception))
291303

292304
def test_publish_should_raise_on_http_4xx_error(self):
293305
pn_client = PushNotifications(
@@ -301,7 +313,7 @@ def test_publish_should_raise_on_http_4xx_error(self):
301313
status_code=400,
302314
json={'error': 'Invalid request', 'description': 'blah'},
303315
)
304-
with self.assertRaises(PusherValidationError):
316+
with self.assertRaises(PusherValidationError) as e:
305317
pn_client.publish(
306318
interests=['donuts'],
307319
publish_body={
@@ -312,6 +324,7 @@ def test_publish_should_raise_on_http_4xx_error(self):
312324
},
313325
},
314326
)
327+
self.assertIn('Invalid request: blah', str(e.exception))
315328

316329
def test_publish_should_raise_on_http_5xx_error(self):
317330
pn_client = PushNotifications(
@@ -325,7 +338,7 @@ def test_publish_should_raise_on_http_5xx_error(self):
325338
status_code=500,
326339
json={'error': 'Server error', 'description': 'blah'},
327340
)
328-
with self.assertRaises(PusherServerError):
341+
with self.assertRaises(PusherServerError) as e:
329342
pn_client.publish(
330343
interests=['donuts'],
331344
publish_body={
@@ -336,6 +349,7 @@ def test_publish_should_raise_on_http_5xx_error(self):
336349
},
337350
},
338351
)
352+
self.assertIn('Server error: blah', str(e.exception))
339353

340354
def test_publish_should_raise_on_http_401_error(self):
341355
pn_client = PushNotifications(
@@ -349,7 +363,7 @@ def test_publish_should_raise_on_http_401_error(self):
349363
status_code=401,
350364
json={'error': 'Auth error', 'description': 'blah'},
351365
)
352-
with self.assertRaises(PusherAuthError):
366+
with self.assertRaises(PusherAuthError) as e:
353367
pn_client.publish(
354368
interests=['donuts'],
355369
publish_body={
@@ -360,6 +374,7 @@ def test_publish_should_raise_on_http_401_error(self):
360374
},
361375
},
362376
)
377+
self.assertIn('Auth error: blah', str(e.exception))
363378

364379
def test_publish_should_raise_on_http_404_error(self):
365380
pn_client = PushNotifications(
@@ -373,7 +388,7 @@ def test_publish_should_raise_on_http_404_error(self):
373388
status_code=404,
374389
json={'error': 'Instance not found', 'description': 'blah'},
375390
)
376-
with self.assertRaises(PusherMissingInstanceError):
391+
with self.assertRaises(PusherMissingInstanceError) as e:
377392
pn_client.publish(
378393
interests=['donuts'],
379394
publish_body={
@@ -384,3 +399,4 @@ def test_publish_should_raise_on_http_404_error(self):
384399
},
385400
},
386401
)
402+
self.assertIn('Instance not found: blah', str(e.exception))

0 commit comments

Comments
 (0)