Skip to content

Commit af4e868

Browse files
committed
Handle response body properly in delete_user
1 parent b6f9a6d commit af4e868

File tree

2 files changed

+105
-3
lines changed

2 files changed

+105
-3
lines changed

pusher_push_notifications/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ def _make_request(self, method, path, path_params, body=None):
142142
try:
143143
response_body = response.json()
144144
except ValueError:
145-
raise PusherBadResponseError(
146-
'The server returned a malformed response',
147-
)
145+
response_body = None
148146

149147
return response_body
150148

@@ -262,6 +260,11 @@ def publish_to_interests(self, interests, publish_body):
262260
body=publish_body,
263261
)
264262

263+
if response_body is None:
264+
raise PusherBadResponseError(
265+
'The server returned a malformed response',
266+
)
267+
265268
return response_body
266269

267270
def publish_to_users(self, user_ids, publish_body):
@@ -331,6 +334,11 @@ def publish_to_users(self, user_ids, publish_body):
331334
body=publish_body,
332335
)
333336

337+
if response_body is None:
338+
raise PusherBadResponseError(
339+
'The server returned a malformed response',
340+
)
341+
334342
return response_body
335343

336344
def authenticate_user(self, user_id):

tests/test_users.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,97 @@ def test_delete_user_should_fail_if_user_id_too_long(self):
474474
with self.assertRaises(ValueError) as e:
475475
pn_client.delete_user('A'*165)
476476
self.assertIn('longer than the maximum of 164 chars', str(e.exception))
477+
478+
def test_delete_user_should_raise_on_http_4xx_error(self):
479+
pn_client = PushNotifications(
480+
'INSTANCE_ID',
481+
'SECRET_KEY'
482+
)
483+
with requests_mock.Mocker() as http_mock:
484+
http_mock.register_uri(
485+
requests_mock.ANY,
486+
requests_mock.ANY,
487+
status_code=400,
488+
json={'error': 'Invalid request', 'description': 'blah'},
489+
)
490+
with self.assertRaises(PusherValidationError) as e:
491+
pn_client.delete_user('user-0001')
492+
self.assertIn('Invalid request: blah', str(e.exception))
493+
494+
def test_delete_user_should_raise_on_http_5xx_error(self):
495+
pn_client = PushNotifications(
496+
'INSTANCE_ID',
497+
'SECRET_KEY'
498+
)
499+
with requests_mock.Mocker() as http_mock:
500+
http_mock.register_uri(
501+
requests_mock.ANY,
502+
requests_mock.ANY,
503+
status_code=500,
504+
json={'error': 'Server error', 'description': 'blah'},
505+
)
506+
with self.assertRaises(PusherServerError) as e:
507+
pn_client.delete_user('user-0001')
508+
self.assertIn('Server error: blah', str(e.exception))
509+
510+
def test_delete_user_should_raise_on_http_401_error(self):
511+
pn_client = PushNotifications(
512+
'INSTANCE_ID',
513+
'SECRET_KEY'
514+
)
515+
with requests_mock.Mocker() as http_mock:
516+
http_mock.register_uri(
517+
requests_mock.ANY,
518+
requests_mock.ANY,
519+
status_code=401,
520+
json={'error': 'Auth error', 'description': 'blah'},
521+
)
522+
with self.assertRaises(PusherAuthError) as e:
523+
pn_client.delete_user('user-0001')
524+
self.assertIn('Auth error: blah', str(e.exception))
525+
526+
def test_publish_to_users_should_raise_on_http_404_error(self):
527+
pn_client = PushNotifications(
528+
'INSTANCE_ID',
529+
'SECRET_KEY'
530+
)
531+
with requests_mock.Mocker() as http_mock:
532+
http_mock.register_uri(
533+
requests_mock.ANY,
534+
requests_mock.ANY,
535+
status_code=404,
536+
json={'error': 'Instance not found', 'description': 'blah'},
537+
)
538+
with self.assertRaises(PusherMissingInstanceError) as e:
539+
pn_client.delete_user('user-0001')
540+
self.assertIn('Instance not found: blah', str(e.exception))
541+
542+
def test_delete_user_should_error_correctly_if_error_not_json(self):
543+
pn_client = PushNotifications(
544+
'INSTANCE_ID',
545+
'SECRET_KEY'
546+
)
547+
with requests_mock.Mocker() as http_mock:
548+
http_mock.register_uri(
549+
requests_mock.ANY,
550+
requests_mock.ANY,
551+
status_code=500,
552+
text='<notjson></notjson>',
553+
)
554+
with self.assertRaises(PusherServerError) as e:
555+
pn_client.delete_user('user-0001')
556+
self.assertIn('Unknown error: no description', str(e.exception))
557+
558+
def test_delete_user_should_not_error_on_not_json_success(self):
559+
pn_client = PushNotifications(
560+
'INSTANCE_ID',
561+
'SECRET_KEY'
562+
)
563+
with requests_mock.Mocker() as http_mock:
564+
http_mock.register_uri(
565+
requests_mock.ANY,
566+
requests_mock.ANY,
567+
status_code=200,
568+
text='<notjson></notjson>',
569+
)
570+
pn_client.delete_user('alice')

0 commit comments

Comments
 (0)