Skip to content

Commit 0ffa5ee

Browse files
committed
Add delete_user method
1 parent 042333e commit 0ffa5ee

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

pusher_push_notifications/__init__.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ def authenticate_user(self, user_id):
339339
auth token for the requested user id (string)
340340
341341
Raises:
342-
ValueError: if user_id is not a string
342+
TypeError: if user_id is not a string
343343
ValueError: is user_id is longer than the maximum of 164 chars
344344
345345
"""
346346
if not isinstance(user_id, six.string_types):
347-
raise ValueError('user_id must be a string')
347+
raise TypeError('user_id must be a string')
348348
if len(user_id) > USER_ID_MAX_LENGTH:
349349
raise ValueError('user_id longer than the maximum of 164 chars')
350350

@@ -363,3 +363,42 @@ def authenticate_user(self, user_id):
363363
self.secret_key,
364364
algorithm='HS256',
365365
).decode('utf-8')
366+
367+
def delete_user(self, user_id):
368+
"""Remove the user with the given ID (and all of their devices) from
369+
the Pusher Beams database. The user will no longer receive any
370+
notifications. This action cannot be undone.
371+
372+
Args:
373+
user_id (string): id of the user to be deleted
374+
375+
Returns:
376+
None
377+
378+
Raises:
379+
TypeError: if user_id is not a string
380+
ValueError: is user_id is longer than the maximum of 164 chars
381+
382+
"""
383+
if not isinstance(user_id, six.string_types):
384+
raise TypeError('user_id must be a string')
385+
if len(user_id) > USER_ID_MAX_LENGTH:
386+
raise ValueError('user_id longer than the maximum of 164 chars')
387+
388+
session = requests.Session()
389+
request = requests.Request(
390+
'DELETE',
391+
'https://{}/user_api/v1/instances/{}/users/{}'.format(
392+
self.endpoint,
393+
self.instance_id,
394+
user_id,
395+
),
396+
headers={
397+
'host': self.endpoint,
398+
'authorization': 'Bearer {}'.format(self.secret_key),
399+
'x-pusher-library': 'pusher-push-notifications-python {}'.format(
400+
SDK_VERSION,
401+
)
402+
},
403+
)
404+
session.send(request.prepare())

tests/test_users.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_authenticate_user_should_fail_if_user_id_not_a_string(self):
5050
'INSTANCE_ID',
5151
'SECRET_KEY'
5252
)
53-
with self.assertRaises(ValueError) as e:
53+
with self.assertRaises(TypeError) as e:
5454
pn_client.authenticate_user(user_id)
5555
self.assertIn('user_id must be a string', str(e.exception))
5656

@@ -419,3 +419,58 @@ def test_publish_to_users_should_handle_not_json_success(self):
419419
},
420420
)
421421
self.assertIn('The server returned a malformed response', str(e.exception))
422+
423+
def test_delete_user_should_make_correct_http_request(self):
424+
pn_client = PushNotifications(
425+
'INSTANCE_ID',
426+
'SECRET_KEY'
427+
)
428+
with requests_mock.Mocker() as http_mock:
429+
http_mock.register_uri(
430+
requests_mock.ANY,
431+
requests_mock.ANY,
432+
status_code=200,
433+
json='',
434+
)
435+
pn_client.delete_user('alice')
436+
req = http_mock.request_history[0]
437+
438+
method = req.method
439+
path = req.path
440+
headers = dict(req._request.headers.lower_items())
441+
442+
self.assertEqual(
443+
method,
444+
'DELETE',
445+
)
446+
self.assertEqual(
447+
path,
448+
'/user_api/v1/instances/instance_id/users/alice',
449+
)
450+
self.assertDictEqual(
451+
headers,
452+
{
453+
'content-length': '0',
454+
'authorization': 'Bearer SECRET_KEY',
455+
'x-pusher-library': 'pusher-push-notifications-python 1.0.1',
456+
'host': 'instance_id.pushnotifications.pusher.com',
457+
},
458+
)
459+
460+
def test_delete_user_should_fail_if_user_id_not_a_string(self):
461+
pn_client = PushNotifications(
462+
'INSTANCE_ID',
463+
'SECRET_KEY'
464+
)
465+
with self.assertRaises(TypeError) as e:
466+
pn_client.delete_user(False)
467+
self.assertIn('user_id must be a string', str(e.exception))
468+
469+
def test_delete_user_should_fail_if_user_id_too_long(self):
470+
pn_client = PushNotifications(
471+
'INSTANCE_ID',
472+
'SECRET_KEY'
473+
)
474+
with self.assertRaises(ValueError) as e:
475+
pn_client.delete_user('A'*165)
476+
self.assertIn('longer than the maximum of 164 chars', str(e.exception))

0 commit comments

Comments
 (0)