def test_fcm_message_platform_config_override(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') apns_config = messaging.APNSConfig( headers={'apns-collapse-id': 'ios_collapse_key'}) request = FCMRequest(self._app, notification=MockNotification( platform_config=platform_config, apns_config=apns_config), topic='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertIsNotNone(message.data) self.assertIsNone(message.notification) self.assertTrue(isinstance(message.android, messaging.AndroidConfig)) self.assertTrue(isinstance(message.apns, messaging.APNSConfig)) self.assertEqual(message.apns.headers, {'apns-collapse-id': 'ios_collapse_key'}) self.assertTrue(isinstance(message.webpush, messaging.WebpushConfig)) self.assertEqual(message.webpush.headers, { 'Topic': 'collapse_key', 'Urgency': 'high' }) self.assertIsNone(message.token) self.assertEqual(message.topic, 'abc') self.assertIsNone(message.condition)
def ping(self, request): """ Immediately dispatch a Ping to either FCM or a webhook """ self._validate_authentication() if request.fcm and request.webhook: return self._application_error('Cannot ping both FCM and webhook') from tbans.models.notifications.ping import PingNotification notification = PingNotification() if request.fcm: # An FCM request can still exist, I believe. It can take some notification and delivery options from tbans.requests.fcm_request import FCMRequest fcm_request = FCMRequest(self._firebase_app, notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition) logging.info('Ping - {}'.format(str(fcm_request))) message_id = fcm_request.send() logging.info('Ping Sent - {}'.format(str(message_id))) return TBANSResponse(code=200, message=message_id) elif request.webhook: from tbans.requests.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, request.webhook.url, request.webhook.secret) logging.info('Ping - {}'.format(str(webhook_request))) webhook_request.send() logging.info('Ping Sent') return TBANSResponse(code=200) else: return self._application_error('Did not specify FCM or webhook to ping')
def test_fcm_message_empty(self): request = FCMRequest(self._app, notification=MockNotification(), token='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertIsNotNone(message.data) self.assertIsNone(message.notification) self.assertIsNone(message.android) self.assertIsNone(message.apns) self.assertIsNone(message.webpush) self.assertEqual(message.token, 'abc') self.assertIsNone(message.topic) self.assertIsNone(message.condition)
def test_fcm_message_platform_config(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') request = FCMRequest(self._app, notification=MockNotification(platform_config=platform_config), topic='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertIsNotNone(message.data) self.assertIsNone(message.notification) self.assertTrue(isinstance(message.android, messaging.AndroidConfig)) self.assertTrue(isinstance(message.apns, messaging.APNSConfig)) self.assertTrue(isinstance(message.webpush, messaging.WebpushConfig)) self.assertIsNone(message.token) self.assertEqual(message.topic, 'abc') self.assertIsNone(message.condition)
def test_fcm_message_notification(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') request = FCMRequest(self._app, notification=MockNotification(fcm_notification=messaging.Notification(title='Title', body='Some body message')), condition='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertIsNotNone(message.data) self.assertTrue(isinstance(message.notification, messaging.Notification)) self.assertIsNone(message.android) self.assertIsNone(message.apns) self.assertIsNone(message.webpush) self.assertIsNone(message.token) self.assertIsNone(message.topic) self.assertEqual(message.condition, 'abc')
def test_fcm_message_data_payload(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') request = FCMRequest(self._app, notification=MockNotification(data_payload={'some_data': 'some test data'}), condition='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertEqual(message.data, {'notification_type': 'verification', 'some_data': 'some test data'}) self.assertIsNone(message.notification) self.assertIsNone(message.android) self.assertIsNone(message.apns) self.assertIsNone(message.webpush) self.assertIsNone(message.token) self.assertIsNone(message.topic) self.assertEqual(message.condition, 'abc')
def test_fcm_message_platform_config_override(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') apns_config = messaging.APNSConfig(headers={'apns-collapse-id': 'ios_collapse_key'}) request = FCMRequest(self._app, notification=MockNotification(platform_config=platform_config, apns_config=apns_config), topic='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertIsNotNone(message.data) self.assertIsNone(message.notification) self.assertTrue(isinstance(message.android, messaging.AndroidConfig)) self.assertTrue(isinstance(message.apns, messaging.APNSConfig)) self.assertEqual(message.apns.headers, {'apns-collapse-id': 'ios_collapse_key'}) self.assertTrue(isinstance(message.webpush, messaging.WebpushConfig)) self.assertEqual(message.webpush.headers, {'Topic': 'collapse_key', 'Urgency': 'high'}) self.assertIsNone(message.token) self.assertEqual(message.topic, 'abc') self.assertIsNone(message.condition)
def test_fcm_message_data_payload_default(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') request = FCMRequest(self._app, notification=MockNotification(), condition='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertEqual(message.data, {'notification_type': 'verification'}) self.assertIsNone(message.notification) self.assertIsNone(message.android) self.assertIsNone(message.apns) self.assertIsNone(message.webpush) self.assertIsNone(message.token) self.assertIsNone(message.topic) self.assertEqual(message.condition, 'abc')
def test_fcm_message_platform_config(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') request = FCMRequest( self._app, notification=MockNotification(platform_config=platform_config), topic='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertIsNotNone(message.data) self.assertIsNone(message.notification) self.assertTrue(isinstance(message.android, messaging.AndroidConfig)) self.assertTrue(isinstance(message.apns, messaging.APNSConfig)) self.assertTrue(isinstance(message.webpush, messaging.WebpushConfig)) self.assertIsNone(message.token) self.assertEqual(message.topic, 'abc') self.assertIsNone(message.condition)
def test_fcm_message_notification(self): platform_config = PlatformConfig(priority=PlatformPriority.HIGH, collapse_key='collapse_key') request = FCMRequest(self._app, notification=MockNotification( fcm_notification=messaging.Notification( title='Title', body='Some body message')), condition='abc') message = request._fcm_message() self.assertIsNotNone(message) self.assertIsNotNone(message.data) self.assertTrue( isinstance(message.notification, messaging.Notification)) self.assertIsNone(message.android) self.assertIsNone(message.apns) self.assertIsNone(message.webpush) self.assertIsNone(message.token) self.assertIsNone(message.topic) self.assertEqual(message.condition, 'abc')
def test_send(self): request = FCMRequest(self._app, notification=MockNotification(), token='abc') message_id = request.send() self.assertEqual(message_id, 'message-id')
def test_init_app_type(self): with self.assertRaises(ValueError): FCMRequest('abc', MockNotification(), token='abcd')
def test_str_condition(self): request = FCMRequest(self._app, MockNotification(), condition='hij') self.assertTrue( 'FCMRequest(condition="hij", notification=' in str(request))
def test_str_token(self): request = FCMRequest(self._app, MockNotification(), token='abc') self.assertTrue( 'FCMRequest(token="abc", notification=' in str(request))
def test_init_delivery_multiple(self): with self.assertRaises(TypeError): FCMRequest(self._app, notification=MockNotification(), token='abc', topic='def')
def test_init_delivery_none(self): with self.assertRaises(TypeError): FCMRequest(self._app, notification=MockNotification())
def test_init_app(self): FCMRequest(self._app, MockNotification(), token='abcd')
def test_str_topic(self): request = FCMRequest(self._app, MockNotification(), topic='def') self.assertTrue( 'FCMRequest(topic="def", notification=' in str(request))
def test_subclass(self): request = FCMRequest(self._app, MockNotification(), token='abcd') self.assertTrue(isinstance(request, Request))