Skip to content

Commit

Permalink
Add Unit Tests for GCMDeviceAdmin and fix FCM v1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Jahn committed Jan 17, 2024
1 parent ecc9f1f commit 056ebf6
Show file tree
Hide file tree
Showing 3 changed files with 336 additions and 110 deletions.
130 changes: 130 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from unittest import mock


from django.contrib.admin import AdminSite
from django.contrib import messages
from django.http import HttpRequest
from django.test import TestCase

from firebase_admin.messaging import Message, BatchResponse, SendResponse, UnregisteredError

from push_notifications.admin import GCMDeviceAdmin
from push_notifications.models import GCMDevice
from tests import responses


class GCMDeviceAdminTestCase(TestCase):
def test_send_bulk_messages_action(self):
request = HttpRequest()

GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
queryset = GCMDevice.objects.all()
admin = GCMDeviceAdmin(GCMDevice, AdminSite())
admin.message_user = mock.Mock()

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
) as p:
admin.send_messages(request, queryset, bulk=True)

# one call
self.assertEqual(len(p.mock_calls), 1)

call = p.call_args
kwargs = call[1]

self.assertTrue("dry_run" in kwargs)
self.assertFalse(kwargs["dry_run"])
self.assertTrue("app" in kwargs)
self.assertIsNone(kwargs["app"])

# only one message
call_messages = call[0][0]
self.assertEqual(len(call_messages), 1)

message = call_messages[0]
self.assertIsInstance(message, Message)
self.assertEqual(message.token, "abc")
self.assertEqual(message.android.notification.body, "Test bulk notification")

admin.message_user.assert_called_once_with(
request, "All messages were sent.", level=messages.SUCCESS
)

def test_send_single_message_action(self):
request = HttpRequest()

GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
queryset = GCMDevice.objects.all()
admin = GCMDeviceAdmin(GCMDevice, AdminSite())
admin.message_user = mock.Mock()

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
) as p:
admin.send_messages(request, queryset, bulk=False)

# one call
self.assertEqual(len(p.mock_calls), 1)

call = p.call_args
kwargs = call[1]

self.assertTrue("dry_run" in kwargs)
self.assertFalse(kwargs["dry_run"])
self.assertTrue("app" in kwargs)
self.assertIsNone(kwargs["app"])

# only one message
call_messages = call[0][0]
self.assertEqual(len(call_messages), 1)

message = call_messages[0]
self.assertIsInstance(message, Message)
self.assertEqual(message.token, "abc")
self.assertEqual(message.android.notification.body, "Test single notification")

admin.message_user.assert_called_once_with(
request, "All messages were sent.", level=messages.SUCCESS
)

def test_send_bulk_messages_action_fail(self):
request = HttpRequest()

GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
queryset = GCMDevice.objects.all()
admin = GCMDeviceAdmin(GCMDevice, AdminSite())
admin.message_user = mock.Mock()

response = BatchResponse(
[SendResponse(resp={"name": "..."}, exception=UnregisteredError("error"),)]
)

with mock.patch(
"firebase_admin.messaging.send_all", return_value=response
) as p:
admin.send_messages(request, queryset, bulk=True)

# one call
self.assertEqual(len(p.mock_calls), 1)

call = p.call_args
kwargs = call[1]

self.assertTrue("dry_run" in kwargs)
self.assertFalse(kwargs["dry_run"])
self.assertTrue("app" in kwargs)
self.assertIsNone(kwargs["app"])

# only one message
call_messages = call[0][0]
self.assertEqual(len(call_messages), 1)

message = call_messages[0]
self.assertIsInstance(message, Message)
self.assertEqual(message.token, "abc")
self.assertEqual(message.android.notification.body, "Test bulk notification")

admin.message_user.assert_called_once_with(
request, "Some messages could not be processed: UnregisteredError('error')", level=messages.ERROR
)
45 changes: 21 additions & 24 deletions tests/test_gcm_push_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ def test_fcm_push_payload(self):

send_message("abc", message)

# one call
self.assertEqual(len(p.mock_calls), 1)
call = p.mock_calls[0]
self.assertEqual(p.call_count, 1)

# only messages is args, dry_run and app are in kwargs
self.assertEqual(len(call.args), 1)
call = p.call_args
kwargs = call[1]

self.assertTrue("dry_run" in call.kwargs)
self.assertFalse(call.kwargs["dry_run"])
self.assertTrue("app" in call.kwargs)
self.assertIsNone(call.kwargs["app"])
self.assertTrue("dry_run" in kwargs)
self.assertFalse(kwargs["dry_run"])
self.assertTrue("app" in kwargs)
self.assertIsNone(kwargs["app"])

# only one message
self.assertEqual(len(call.args[0]), 1)
messages = call[0][0]
self.assertEqual(len(messages), 1)

message = call.args[0][0]
message = messages[0]
self.assertIsInstance(message, Message)
self.assertEqual(message.token, "abc")
self.assertEqual(message.android.notification.body, "Hello world")
Expand All @@ -44,27 +43,25 @@ def test_fcm_push_payload_many(self):
send_message(["abc", "123"], message)

# one call
self.assertEqual(len(p.mock_calls), 1)
call = p.mock_calls[0]

# only messages is args, dry_run and app are in kwargs
self.assertEqual(len(call.args), 1)
messages_arg = call.args[0]
self.assertEqual(p.call_count, 1)
call = p.call_args
kwargs = call[1]

self.assertTrue("dry_run" in call.kwargs)
self.assertFalse(call.kwargs["dry_run"])
self.assertTrue("app" in call.kwargs)
self.assertIsNone(call.kwargs["app"])
self.assertTrue("dry_run" in kwargs)
self.assertFalse(kwargs["dry_run"])
self.assertTrue("app" in kwargs)
self.assertIsNone(kwargs["app"])

# two message
self.assertEqual(len(messages_arg), 2)
messages = call[0][0]
self.assertEqual(len(messages), 2)

message_one = messages_arg[0]
message_one = messages[0]
self.assertIsInstance(message_one, Message)
self.assertEqual(message_one.token, "abc")
self.assertEqual(message_one.android.notification.body, "Hello world")

message_two = messages_arg[1]
message_two = messages[1]
self.assertIsInstance(message_two, Message)
self.assertEqual( message_two.token,"123")
self.assertEqual( message_two.android.notification.body, "Hello world")
Expand Down
Loading

0 comments on commit 056ebf6

Please sign in to comment.