From d5600192e19a343675c477d28df1d35215533f7c Mon Sep 17 00:00:00 2001 From: guspix Date: Tue, 23 Apr 2024 00:03:44 -0400 Subject: [PATCH] Changed firebase-admin's deprecated send_all method for send_each --- README.rst | 2 +- push_notifications/gcm.py | 2 +- setup.cfg | 2 +- tests/test_admin.py | 6 +++--- tests/test_gcm_push_payload.py | 4 ++-- tests/test_models.py | 36 +++++++++++++++++----------------- tox.ini | 2 +- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/README.rst b/README.rst index 7e0d691e..d78434de 100644 --- a/README.rst +++ b/README.rst @@ -37,7 +37,7 @@ Dependencies - For WebPush (WP), pywebpush 1.3.0+ is required (optional). py-vapid 1.3.0+ is required for generating the WebPush private key; however this step does not need to occur on the application server. - For Apple Push (APNS), apns2 0.3+ is required (optional). -- For FCM, firebase-admin 5+ is required (optional). +- For FCM, firebase-admin 6.2+ is required (optional). Setup ----- diff --git a/push_notifications/gcm.py b/push_notifications/gcm.py index 61df3cb7..e2f9d537 100644 --- a/push_notifications/gcm.py +++ b/push_notifications/gcm.py @@ -182,7 +182,7 @@ def send_message( messages = [ _prepare_message(message, token) for token in chunk ] - responses = messaging.send_all(messages, dry_run=dry_run, app=app).responses + responses = messaging.send_each(messages, dry_run=dry_run, app=app).responses ret.extend(responses) _deactivate_devices_with_error_results(registration_ids, ret) return messaging.BatchResponse(ret) diff --git a/setup.cfg b/setup.cfg index f471a1d0..d031d6af 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,7 +42,7 @@ APNS = WP = pywebpush>=1.3.0 -FCM = firebase-admin>=5,<6 +FCM = firebase-admin>=6.2 [options.packages.find] diff --git a/tests/test_admin.py b/tests/test_admin.py index b7c3a14c..254774f5 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -24,7 +24,7 @@ def test_send_bulk_messages_action(self): admin.message_user = mock.Mock() with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: admin.send_messages(request, queryset, bulk=True) @@ -61,7 +61,7 @@ def test_send_single_message_action(self): admin.message_user = mock.Mock() with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: admin.send_messages(request, queryset, bulk=False) @@ -102,7 +102,7 @@ def test_send_bulk_messages_action_fail(self): ) with mock.patch( - "firebase_admin.messaging.send_all", return_value=response + "firebase_admin.messaging.send_each", return_value=response ) as p: admin.send_messages(request, queryset, bulk=True) diff --git a/tests/test_gcm_push_payload.py b/tests/test_gcm_push_payload.py index 657cdc9d..8bf1eb48 100644 --- a/tests/test_gcm_push_payload.py +++ b/tests/test_gcm_push_payload.py @@ -12,7 +12,7 @@ class GCMPushPayloadTest(TestCase): def test_fcm_push_payload(self): - with mock.patch("firebase_admin.messaging.send_all", return_value=FCM_SUCCESS) as p: + with mock.patch("firebase_admin.messaging.send_each", return_value=FCM_SUCCESS) as p: message = dict_to_fcm_message({"message": "Hello world"}) send_message("abc", message) @@ -37,7 +37,7 @@ def test_fcm_push_payload(self): self.assertEqual(message.android.notification.body, "Hello world") def test_fcm_push_payload_many(self): - with mock.patch("firebase_admin.messaging.send_all", return_value=FCM_SUCCESS) as p: + with mock.patch("firebase_admin.messaging.send_each", return_value=FCM_SUCCESS) as p: message = dict_to_fcm_message({"message": "Hello world"}) send_message(["abc", "123"], message) diff --git a/tests/test_models.py b/tests/test_models.py index 6a9996a7..2575fc4c 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -38,7 +38,7 @@ def test_can_create_save_device(self): def test_fcm_send_message(self): device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: device.send_message("Hello world") @@ -65,7 +65,7 @@ def test_fcm_send_message(self): def test_fcm_send_message_with_fcm_message(self): device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: message_to_send = messaging.Message( notification=messaging.Notification( @@ -99,7 +99,7 @@ def test_fcm_send_message_with_fcm_message(self): def test_fcm_send_message_extra_data(self): device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: device.send_message("Hello world", extra={"foo": "bar"}) @@ -125,7 +125,7 @@ def test_fcm_send_message_extra_data(self): def test_fcm_send_message_extra_options(self): device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: device.send_message("Hello world", collapse_key="test_key", foo="bar") @@ -152,7 +152,7 @@ def test_fcm_send_message_extra_options(self): def test_fcm_send_message_extra_notification(self): device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: device.send_message("Hello world", extra={"icon": "test_icon"}, title="test") @@ -180,7 +180,7 @@ def test_fcm_send_message_extra_notification(self): def test_fcm_send_message_extra_options_and_notification_and_data(self): device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS ) as p: device.send_message( "Hello world", @@ -215,7 +215,7 @@ def test_fcm_send_message_to_multiple_devices(self): self._create_fcm_devices(["abc", "abc1"]) with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: GCMDevice.objects.all().send_message("Hello world") @@ -245,7 +245,7 @@ def test_fcm_send_message_to_multiple_devices_fcm_message(self): self._create_fcm_devices(["abc", "abc1"]) with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: message_to_send = messaging.Message( notification=messaging.Notification( @@ -283,7 +283,7 @@ def test_gcm_send_message_does_not_send(self): device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="GCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: message_to_send = messaging.Message( notification=messaging.Notification( @@ -299,7 +299,7 @@ def test_gcm_send_multiple_message_does_not_send(self): self._create_devices(["abc", "abc1"]) with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: message_to_send = messaging.Message( notification=messaging.Notification( @@ -318,7 +318,7 @@ def test_fcm_send_message_active_devices(self): GCMDevice.objects.create(registration_id="xyz", active=False, cloud_message_type="FCM") with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: GCMDevice.objects.all().send_message("Hello world") @@ -344,7 +344,7 @@ def test_fcm_send_message_collapse_to_multiple_devices(self): self._create_fcm_devices(["abc", "abc1"]) with mock.patch( - "firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: GCMDevice.objects.all().send_message("Hello world", collapse_key="test_key") @@ -386,7 +386,7 @@ def test_fcm_send_message_to_single_device_with_error(self): [SendResponse(resp={"name": "..."}, exception=error)] ) with mock.patch( - "firebase_admin.messaging.send_all", return_value=return_value + "firebase_admin.messaging.send_each", return_value=return_value ): device = GCMDevice.objects.get(registration_id=devices[index]) device.send_message("Hello World!") @@ -399,7 +399,7 @@ def test_fcm_send_message_to_single_device_with_error_mismatch(self): [SendResponse(resp={"name": "..."}, exception=OSError())] ) with mock.patch( - "firebase_admin.messaging.send_all", + "firebase_admin.messaging.send_each", return_value=return_value ): # these errors are not device specific, device is not deactivated @@ -417,7 +417,7 @@ def test_fcm_send_message_to_multiple_devices_with_error(self): SendResponse(resp={"name": "..."}, exception=InvalidArgumentError("Invalid registration")), ]) with mock.patch( - "firebase_admin.messaging.send_all", return_value=return_value + "firebase_admin.messaging.send_each", return_value=return_value ): GCMDevice.objects.all().send_message("Hello World") self.assertFalse(GCMDevice.objects.get(registration_id="abc").active) @@ -436,7 +436,7 @@ def test_fcm_send_message_to_multiple_devices_with_error_b(self): ]) with mock.patch( - "firebase_admin.messaging.send_all", return_value=return_value + "firebase_admin.messaging.send_each", return_value=return_value ): GCMDevice.objects.all().send_message("Hello World") self.assertTrue(GCMDevice.objects.get(registration_id="abc").active) @@ -448,14 +448,14 @@ def test_fcm_send_message_with_no_reg_ids(self): self._create_fcm_devices(["abc", "abc1"]) with mock.patch( - "firebase_admin.messaging.send_all", + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: GCMDevice.objects.filter(registration_id="xyz").send_message("Hello World") p.assert_not_called() with mock.patch( - "firebase_admin.messaging.send_all", + "firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE ) as p: reg_ids = [obj.registration_id for obj in GCMDevice.objects.all()] diff --git a/tox.ini b/tox.ini index a990906f..3f74d009 100644 --- a/tox.ini +++ b/tox.ini @@ -36,7 +36,7 @@ deps = pytest-django pywebpush djangorestframework - firebase-admin>=5,<6 + firebase-admin>=6.2 dj22: Django>=2.2,<3.0 dj32: Django>=3.2,<3.3 dj40: Django>=4.0,<4.0.5