Skip to content

Commit

Permalink
fix error handling on duplicated APNS devices
Browse files Browse the repository at this point in the history
  • Loading branch information
bielfrontera committed Sep 6, 2024
1 parent b2cbfb0 commit 7b8d8c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions push_notifications/apns.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ def apns_send_message(registration_id, alert, application_id=None, creds=None, *
creds=creds, **kwargs
)
except apns2_errors.APNsException as apns2_exception:
if isinstance(apns2_exception, apns2_errors.Unregistered):
device = models.APNSDevice.objects.get(registration_id=registration_id)
device.active = False
device.save()
if isinstance(apns2_exception, apns2_errors.Unregistered):
models.APNSDevice.objects.filter(registration_id=registration_id).update(active=False)

raise APNSServerError(status=apns2_exception.__class__.__name__)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_apns_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,17 @@ def test_apns_send_message_to_bulk_devices_with_error(self):
self.assertFalse(APNSDevice.objects.get(registration_id=token).active)
else:
self.assertTrue(APNSDevice.objects.get(registration_id=token).active)

def test_apns_send_message_to_duplicated_device_with_error(self):
# these errors are device specific, device.active will be set false
devices = ["abc", "abc"]
self._create_devices(devices)

with mock.patch("push_notifications.apns._apns_send") as s:
s.side_effect = Unregistered
device = APNSDevice.objects.filter(registration_id="abc").first()
with self.assertRaises(APNSError) as ae:
device.send_message("Hello World!")
self.assertEqual(ae.exception.status, "Unregistered")
for device in APNSDevice.objects.filter(registration_id="abc"):
self.assertFalse(device.active)

0 comments on commit 7b8d8c9

Please sign in to comment.