def test_send_fcm_unhandled_error(self): client = MobileClient(parent=ndb.Key(Account, 'user_id'), user_id='user_id', messaging_id='messaging_id', client_type=ClientType.OS_IOS) client.put() # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id']) batch_response = messaging.BatchResponse( [messaging.SendResponse(None, FirebaseError('code', 'message'))]) with patch.object(FCMRequest, 'send', return_value=batch_response), patch( 'logging.error') as mock_error: exit_code = TBANSHelper._send_fcm([client], MockNotification()) self.assertEqual(exit_code, 0) mock_error.assert_called_once_with( 'Unhandled FCM error for messaging_id - code / message') # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id']) # Check that we didn't queue for a retry tasks = self.taskqueue_stub.get_filtered_tasks( queue_names='push-notifications') self.assertEqual(len(tasks), 0)
def test_send_fcm_retry_backoff_time(self): client = MobileClient(parent=ndb.Key(Account, 'user_id'), user_id='user_id', messaging_id='messaging_id', client_type=ClientType.OS_IOS) client.put() import time batch_response = messaging.BatchResponse([ messaging.SendResponse(None, QuotaExceededError('code', 'message')) ]) for i in range(0, 6): with patch.object( FCMRequest, 'send', return_value=batch_response), patch('logging.error'): call_time = time.time() TBANSHelper._send_fcm([client], MockNotification(), i) # Check that we queue'd for a retry with the proper countdown time tasks = self.taskqueue_stub.get_filtered_tasks( queue_names='push-notifications') if i > 0: self.assertGreater(tasks[0].eta_posix - call_time, 0) # Make sure our taskqueue tasks execute what we expect with patch.object(TBANSHelper, '_send_fcm') as mock_send_fcm: deferred.run(tasks[0].payload) mock_send_fcm.assert_called_once_with([client], ANY, i + 1) self.taskqueue_stub.FlushQueue('push-notifications')
def test_send_fcm_invalid_argument_error(self): client = MobileClient(parent=ndb.Key(Account, 'user_id'), user_id='user_id', messaging_id='messaging_id', client_type=ClientType.OS_IOS) client.put() # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id']) batch_response = messaging.BatchResponse([ messaging.SendResponse(None, InvalidArgumentError('code', 'message')) ]) with patch.object(FCMRequest, 'send', return_value=batch_response), patch( 'logging.critical') as mock_critical: exit_code = TBANSHelper._send_fcm([client], MockNotification()) self.assertEqual(exit_code, 0) mock_critical.assert_called_once_with( 'Invalid argument when sending to FCM - code') # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id']) # Make sure we haven't queued for a retry tasks = self.taskqueue_stub.get_filtered_tasks( queue_names='push-notifications') self.assertEqual(len(tasks), 0)
def test_send_fcm_unavailable_error(self): client = MobileClient(parent=ndb.Key(Account, 'user_id'), user_id='user_id', messaging_id='messaging_id', client_type=ClientType.OS_IOS) client.put() # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id']) batch_response = messaging.BatchResponse([ messaging.SendResponse(None, UnavailableError('code', 'message')) ]) with patch.object(FCMRequest, 'send', return_value=batch_response), patch( 'logging.error') as mock_error: exit_code = TBANSHelper._send_fcm([client], MockNotification()) self.assertEqual(exit_code, 0) mock_error.assert_called_once_with( 'FCM unavailable - retrying client...') # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id']) # Check that we queue'd for a retry tasks = self.taskqueue_stub.get_filtered_tasks( queue_names='push-notifications') self.assertEqual(len(tasks), 1) # Make sure our taskqueue tasks execute what we expect with patch.object(TBANSHelper, '_send_fcm') as mock_send_fcm: deferred.run(tasks[0].payload) mock_send_fcm.assert_called_once_with([client], ANY, 1)
def deactivate_devices_with_error_result(cls, registration_id, firebase_exc, name=None) -> List[str]: return cls.objects.deactivate_devices_with_error_results( [registration_id], [messaging.SendResponse({"name": name}, firebase_exc)])
def test_send_fcm_sender_id_mismatch_error(self): client = MobileClient(parent=ndb.Key(Account, 'user_id'), user_id='user_id', messaging_id='messaging_id', client_type=ClientType.OS_IOS) client.put() # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id']) batch_response = messaging.BatchResponse([ messaging.SendResponse(None, SenderIdMismatchError('code', 'message')) ]) with patch.object(FCMRequest, 'send', return_value=batch_response), \ patch.object(MobileClient, 'delete_for_messaging_id', wraps=MobileClient.delete_for_messaging_id) as mock_delete, \ patch('logging.info') as mock_info: exit_code = TBANSHelper._send_fcm([client], MockNotification()) mock_delete.assert_called_once_with('messaging_id') self.assertEqual(exit_code, 0) mock_info.assert_called_with( 'Deleting mismatched client with ID: messaging_id') # Sanity check self.assertEqual(fcm_messaging_ids('user_id'), []) # Make sure we haven't queued for a retry tasks = self.taskqueue_stub.get_filtered_tasks( queue_names='push-notifications') self.assertEqual(len(tasks), 0)
def send_message( self, message: messaging.Message, **more_send_message_kwargs, ) -> Union[Optional[messaging.SendResponse], FirebaseError]: """ Send single message. The message's token should be blank (and will be overridden if not). Responds with message ID string. :param message: firebase.messaging.Message. If `message` includes a token/id, it will be overridden. :param more_send_message_kwargs: Parameters for firebase.messaging.send_all() - dry_run: bool. Whether to actually send the notification to the device - app: firebase_admin.App. Specify a specific app to use If there are any new parameters, you can still specify them here. :raises FirebaseError :returns messaging.SendResponse or FirebaseError if the device was deactivated due to an error. """ message.token = self.registration_id try: return messaging.SendResponse( {"name": messaging.send(message, **more_send_message_kwargs)}, None) except FirebaseError as e: self.deactivate_devices_with_error_result(self.registration_id, e) return e
def test_send_failed_partial(self): batch_response = messaging.BatchResponse([ messaging.SendResponse({'name': 'abc'}, None), messaging.SendResponse(None, 'a') ]) request = FCMRequest(app=self.app, notification=MockNotification(), tokens=['abc', 'def']) with patch.object( messaging, 'send_multicast', return_value=batch_response) as mock_send, patch.object( request, 'defer_track_notification') as mock_track: response = request.send() mock_send.assert_called_once() mock_track.assert_called_once_with(1) self.assertEqual(response, batch_response)
def test_ping_fcm_fail(self): client = MobileClient(parent=ndb.Key(Account, 'user_id'), user_id='user_id', messaging_id='token', client_type=ClientType.OS_IOS, device_uuid='uuid', display_name='Phone') batch_response = messaging.BatchResponse( [messaging.SendResponse(None, FirebaseError(500, 'testing'))]) with patch.object(FCMRequest, 'send', return_value=batch_response) as mock_send: success = TBANSHelper._ping_client(client) mock_send.assert_called_once() self.assertFalse(success)
def send_topic_message( message: messaging.Message, topic_name: str, app: "firebase_admin.App" = SETTINGS["DEFAULT_FIREBASE_APP"], **more_send_message_kwargs, ) -> Union[Optional[messaging.SendResponse], FirebaseError]: message.topic = topic_name try: return messaging.SendResponse( { "name": messaging.send( message, app=app, **more_send_message_kwargs) }, None, ) except FirebaseError as e: return e
def _makeSendResponse(self): return messaging.SendResponse(None, None)