def test_push_payload(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", "Hello world", None, badge=1, sound="chime", extra={"custom_data": 12345}, expiration=3, socket=socket) p.assert_called_once_with("123", b'{"aps":{"alert":"Hello world","badge":1,"sound":"chime"},"custom_data":12345}', 0, 3, 10)
def test_push_payload_with_app_id(self): settings.PUSH_NOTIFICATIONS_SETTINGS['APNS_CERTIFICATES_MODEL'] = { 'model': 'push_notifications.ApplicationModel', 'key': 'application_id', 'value': 'apns_certificate', } import ssl from django.core.files.base import ContentFile f = ContentFile("QWERTY!!!") a = ApplicationModel.objects.create(application_id='qwertyxxx') a.apns_certificate.save("uiopcertxxx", f) path = a.apns_certificate.path socket = mock.MagicMock() with mock.patch("ssl.wrap_socket", return_value=socket) as s: with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", "Hello world", 'qwertyxxx', badge=1, sound="chime", extra={"custom_data": 12345}, expiration=3) s.assert_called_once_with(*s.call_args[0], ca_certs=None, certfile=path, ssl_version=ssl.PROTOCOL_TLSv1) p.assert_called_once_with( "123", b'{"aps":{"alert":"Hello world","badge":1,"sound":"chime"},"custom_data":12345}', 0, 3, 10)
def test_localised_push_with_empty_body(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", None, loc_key="TEST_LOC_KEY", expiration=3, socket=socket) p.assert_called_once_with( "123", b'{"aps":{"alert":{"loc-key":"TEST_LOC_KEY"}}}', 0, 3, 10 )
def test_push_payload(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", "Hello world", badge=1, sound="chime", extra={"custom_data": 12345}, expiration=3, socket=socket) p.assert_called_once_with("123", '{"aps":{"sound":"chime","badge":1,"alert":"Hello world"},"custom_data":12345}', 0, 3, 10)
def test_using_extra(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send( "123", "sample", extra={"foo": "bar"}, identifier=10, expiration=30, priority=10, socket=socket ) p.assert_called_once_with("123", b'{"aps":{"alert":"sample"},"foo":"bar"}', 10, 30, 10)
def test_localised_push_with_empty_body(self): with mock.patch("apns2.credentials.init_context"): with mock.patch("apns2.client.APNsClient.connect"): with mock.patch("apns2.client.APNsClient.send_notification") as s: _apns_send("123", None, loc_key="TEST_LOC_KEY", expiration=3) args, kargs = s.call_args self.assertEqual(args[0], "123") self.assertEqual(args[1].alert.body_localized_key, "TEST_LOC_KEY") self.assertEqual(kargs["expiration"], 3)
def test_collapse_id(self): with mock.patch("apns2.credentials.init_context"): with mock.patch("apns2.client.APNsClient.connect"): with mock.patch( "apns2.client.APNsClient.send_notification") as s: _apns_send("123", "sample", collapse_id="456789") args, kargs = s.call_args self.assertEqual(args[0], "123") self.assertEqual(args[1].alert, "sample") self.assertEqual(kargs["collapse_id"], "456789")
def test_localised_push_with_empty_body(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", None, loc_key="TEST_LOC_KEY", expiration=3, socket=socket) p.assert_called_once_with( "123", b'{"aps":{"alert":{"loc-key":"TEST_LOC_KEY"}}}', 0, 3, 10)
def test_using_extra(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", "sample", extra={"foo": "bar"}, identifier=10, expiration=30, priority=10, socket=socket) p.assert_called_once_with( "123", b'{"aps":{"alert":"sample"},"foo":"bar"}', 10, 30, 10)
def test_push_payload(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_message") as p: _apns_send('123', 'Hello world', badge=1, sound='chime', extra={"custom_data": 12345}, socket=socket) p.assert_called_once_with( '123', '{"aps":{"sound":"chime","badge":1,"alert":"Hello world"},' '"custom_data":12345}')
def apns_send_bulk_message(registration_ids, alert, **kwargs): """ Sends an APNS notification to one or more registration_ids. The registration_ids argument needs to be a list. Note that if set alert should always be a string. If it is not set, it won't be included in the notification. You will need to pass None to this for silent notifications. """ with closing(_apns_create_socket()) as socket: for identifier, registration_id in enumerate(registration_ids): _apns_send(registration_id, alert, identifier=identifier, socket=socket, **kwargs) _apns_check_errors(socket)
def test_using_extra(self): with mock.patch("apns2.credentials.init_context"): with mock.patch("apns2.client.APNsClient.connect"): with mock.patch("apns2.client.APNsClient.send_notification") as s: _apns_send( "123", "sample", extra={"foo": "bar"}, expiration=30, priority=10 ) args, kargs = s.call_args self.assertEqual(args[0], "123") self.assertEqual(args[1].alert, "sample") self.assertEqual(args[1].custom, {"foo": "bar"}) self.assertEqual(kargs["priority"], NotificationPriority.Immediate) self.assertEqual(kargs["expiration"], 30)
def test_push_payload_with_thread_id(self): with mock.patch("apns2.credentials.init_context"): with mock.patch("apns2.client.APNsClient.connect"): with mock.patch("apns2.client.APNsClient.send_notification") as s: _apns_send( "123", "Hello world", thread_id="565", sound="chime", extra={"custom_data": 12345}, expiration=3 ) args, kargs = s.call_args self.assertEqual(args[0], "123") self.assertEqual(args[1].alert, "Hello world") self.assertEqual(args[1].thread_id, "565") self.assertEqual(args[1].sound, "chime") self.assertEqual(args[1].custom, {"custom_data": 12345}) self.assertEqual(kargs["expiration"], 3)
def test_push_payload_with_alert_dict(self): with mock.patch("apns2.credentials.init_context"): with mock.patch("apns2.client.APNsClient.connect"): with mock.patch("apns2.client.APNsClient.send_notification") as s: _apns_send( "123", alert={"title": "t1", "body": "b1"}, sound="chime", extra={"custom_data": 12345}, expiration=3 ) args, kargs = s.call_args self.assertEqual(args[0], "123") self.assertEqual(args[1].alert["body"], "b1") self.assertEqual(args[1].alert["title"], "t1") self.assertEqual(args[1].sound, "chime") self.assertEqual(args[1].custom, {"custom_data": 12345}) self.assertEqual(kargs["expiration"], 3)
def test_push_payload_with_alert_dict(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", alert={ 'title': 't1', 'body': 'b1' }, sound="chime", extra={"custom_data": 12345}, expiration=3, socket=socket) p.assert_called_once_with( "123", b'{"aps":{"alert":{"body":"b1","title":"t1"},"sound":"chime"},"custom_data":12345}', 0, 3, 10)
def test_push_payload(self): with mock.patch("apns2.client.init_context"): with mock.patch("apns2.client.APNsClient.connect"): with mock.patch("apns2.client.APNsClient.send_notification") as s: _apns_send( "123", "Hello world", badge=1, sound="chime", extra={"custom_data": 12345}, expiration=3 ) self.assertTrue(s.called) args, kargs = s.call_args self.assertEqual(args[0], "123") self.assertEqual(args[1].alert, "Hello world") self.assertEqual(args[1].badge, 1) self.assertEqual(args[1].sound, "chime") self.assertEqual(args[1].custom, {"custom_data": 12345}) self.assertEqual(kargs["expiration"], 3)
def test_push_payload(self): with mock.patch("apns2.client.init_context"): with mock.patch("apns2.client.APNsClient.connect"): with mock.patch( "apns2.client.APNsClient.send_notification") as s: _apns_send("123", "Hello world", badge=1, sound="chime", extra={"custom_data": 12345}, expiration=3) self.assertTrue(s.called) args, kargs = s.call_args self.assertEqual(args[0], "123") self.assertEqual(args[1].alert, "Hello world") self.assertEqual(args[1].badge, 1) self.assertEqual(args[1].sound, "chime") self.assertEqual(args[1].custom, {"custom_data": 12345}) self.assertEqual(kargs["expiration"], 3)
def test_push_payload_with_app_id(self): settings.PUSH_NOTIFICATIONS_SETTINGS['APNS_CERTIFICATES_MODEL'] = { 'model':'push_notifications.ApplicationModel', 'key':'application_id', 'value':'apns_certificate', } import ssl from django.core.files.base import ContentFile f = ContentFile("QWERTY!!!") a = ApplicationModel.objects.create(application_id='qwertyxxx') a.apns_certificate.save("uiopcertxxx",f) path = a.apns_certificate.path socket = mock.MagicMock() with mock.patch("ssl.wrap_socket",return_value=socket) as s: with mock.patch("push_notifications.apns._apns_pack_frame") as p: _apns_send("123", "Hello world", 'qwertyxxx', badge=1, sound="chime", extra={"custom_data": 12345}, expiration=3) s.assert_called_once_with(*s.call_args[0],ca_certs=None,certfile=path,ssl_version=ssl.PROTOCOL_TLSv1) p.assert_called_once_with("123", b'{"aps":{"alert":"Hello world","badge":1,"sound":"chime"},"custom_data":12345}', 0, 3, 10)
def apns_send_message(registration_id, alert, **kwargs): """ Sends an APNS notification to a single registration_id. This will send the notification as form data. If sending multiple notifications, it is more efficient to use apns_send_bulk_message() Note that if set alert should always be a string. If it is not set, it won't be included in the notification. You will need to pass None to this for silent notifications. """ return _apns_send(registration_id, alert, **kwargs)
def test_localised_push_with_empty_body(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_message") as p: _apns_send('123', None, loc_key='TEST_LOC_KEY', socket=socket) p.assert_called_once_with('123', '{"aps":{"alert":{"loc-key":"TEST_LOC_KEY"}}}')
def test_localised_push_with_empty_body(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_message") as p: _apns_send('123', None, loc_key='TEST_LOC_KEY', socket=socket) p.assert_called_once_with( '123', '{"aps":{"alert":{"loc-key":"TEST_LOC_KEY"}}}')
def test_using_extra(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_message") as p: _apns_send('123', 'sample', extra={"foo": "bar"}, socket=socket) p.assert_called_once_with('123', '{"aps":{"alert":"sample"},"foo":"bar"}')
def test_push_payload(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_message") as p: _apns_send('123', 'Hello world', badge=1, sound='chime', extra={"custom_data": 12345}, socket=socket) p.assert_called_once_with('123', '{"aps":{"sound":"chime","badge":1,"alert":"Hello world"},' '"custom_data":12345}')
def test_push_payload(self): socket = mock.MagicMock() with mock.patch("push_notifications.apns._apns_pack_message") as p: _apns_send('123', 'Hello world', badge=1, sound='chime', extra={"custom_data": 12345}, socket=socket) p.assert_called_once_with('123', {'aps': {'alert': 'Hello world', 'badge': 1, 'sound': 'chime'}, "custom_data": 12345})