From 8a85833b8db7eadd6932486c5a4cbab7b9fb9a0b Mon Sep 17 00:00:00 2001 From: Biel Frontera Date: Fri, 6 Sep 2024 13:17:42 +0200 Subject: [PATCH] fix error handling on duplicated APNS devices --- push_notifications/apns.py | 4 +--- tests/test_apns_models.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/push_notifications/apns.py b/push_notifications/apns.py index 04064872..766062a7 100644 --- a/push_notifications/apns.py +++ b/push_notifications/apns.py @@ -116,9 +116,7 @@ def apns_send_message(registration_id, alert, application_id=None, creds=None, * ) 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() + models.APNSDevice.objects.filter(registration_id=registration_id).update(active=False) raise APNSServerError(status=apns2_exception.__class__.__name__) diff --git a/tests/test_apns_models.py b/tests/test_apns_models.py index bd15a97d..2b3f76a5 100644 --- a/tests/test_apns_models.py +++ b/tests/test_apns_models.py @@ -131,3 +131,17 @@ def test_apns_send_message_to_bulk_devices_with_error(self): 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)