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: from tbans.models.requests.notifications.fcm_request import FCMRequest fcm_request = FCMRequest(notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition) logging.info('Ping - {}'.format(str(fcm_request))) response = fcm_request.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) elif request.webhook: from tbans.models.requests.notifications.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, request.webhook.url, request.webhook.secret) logging.info('Ping - {}'.format(str(webhook_request))) response = webhook_request.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) else: return self._application_error( 'Did not specify FCM or webhook to ping')
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: from tbans.models.requests.notifications.fcm_request import FCMRequest fcm_request = FCMRequest(notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition) logging.info('Ping - {}'.format(str(fcm_request))) response = fcm_request.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) elif request.webhook: from tbans.models.requests.notifications.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, request.webhook.url, request.webhook.secret) logging.info('Ping - {}'.format(str(webhook_request))) response = webhook_request.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) else: return self._application_error('Did not specify FCM or webhook to ping')
def test_set_payload(self): data = {} FCMRequest._set_payload(data, 'test', None) self.assertEqual(data.get('test', 'test-default'), 'test-default') data_payload = {'data': 'some_data'} FCMRequest._set_payload(data, 'test', MockPayload(data_payload)) self.assertEqual(data.get('test', 'test-default'), data_payload)
def test_set_platform_payload_override(self): data = {} platform_payload = PlatformPayload(priority=PlatformPayloadPriority.HIGH, collapse_key='collapse_key') apns_payload = PlatformPayload(platform_type=PlatformPayloadType.APNS, priority=PlatformPayloadPriority.NORMAL, collapse_key='different_collapse_key') notification = MockNotification(platform_payload=platform_payload, apns_payload=apns_payload) FCMRequest._set_platform_payload(data, PlatformPayloadType.APNS, notification.apns_payload, notification.platform_payload) self.assertEqual(data, {'apns': {'apns-collapse-id': 'different_collapse_key', 'apns-priority': '5'}})
def test_transform_fcm_response_error(self): _status_code = 404 _content = '{"error": {"code": 404,"message": "Requested entity was not found.","status": "NOT_FOUND","details": [{"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError","errorCode": "UNREGISTERED"}]}}' response = MockResponse(_status_code, _content) transformed_response = FCMRequest._transform_fcm_response(response) self.assertEqual(transformed_response.status_code, _status_code) self.assertEqual(transformed_response.content, 'registration-token-not-registered')
def test_transform_fcm_response_success(self): _status_code = 200 _content = '{"name": "projects/testbed-test/messages/1545762214218984"}' response = MockResponse(_status_code, _content) transformed_response = FCMRequest._transform_fcm_response(response) self.assertEqual(transformed_response.status_code, _status_code) self.assertEqual(transformed_response.content, _content)
def test_init_delivery_multiple(self): with self.assertRaises(TypeError): FCMRequest(notification=MockNotification(), token='abc', topic='def')
def test_init_delivery_none(self): with self.assertRaises(TypeError): FCMRequest(notification=MockNotification())
def test_subclass(self): request = FCMRequest(MockNotification(), token='abcd') self.assertTrue(isinstance(request, NotificationRequest))
def test_json_string_platform_payload(self): message = FCMRequest(MockNotification(platform_payload=PlatformPayload(priority=PlatformPayloadPriority.HIGH, collapse_key='collapse_key')), token='abc') self.assertEqual(message.json_string(), '{"message": {"apns": {"apns-collapse-id": "collapse_key", "apns-priority": "10"}, "token": "abc", "data": {"message_type": "verification"}, "android": {"priority": "HIGH", "collapse_key": "collapse_key"}, "webpush": {"Topic": "collapse_key", "Urgency": "high"}}}')
def test_json_string_condition(self): message = FCMRequest(MockNotification(), condition="\'dogs\' in topics") self.assertEqual(message.json_string(), '{"message": {"data": {"message_type": "verification"}, "condition": "\'dogs\' in topics"}}')
def test_fcm_url(self): message = FCMRequest(MockNotification(), token='abc') self.assertEqual(message._fcm_url, 'https://fcm.googleapis.com/v1/projects/testbed-test/messages:send')
def test_str_condition(self): message = FCMRequest(MockNotification(), condition='hij') self.assertTrue('FCMRequest(condition="hij", notification=' in str(message))
def test_str_topic(self): message = FCMRequest(MockNotification(), topic='def') self.assertTrue('FCMRequest(topic="def", notification=' in str(message))
def test_str_token(self): message = FCMRequest(MockNotification(), token='abc') self.assertTrue('FCMRequest(token="abc", notification=' in str(message))
def test_delivery_option_token(self): message = FCMRequest(MockNotification(), condition='ghi') delivery_option = message._delivery_option() self.assertEqual(delivery_option[0], 'condition') self.assertEqual(delivery_option[1], 'ghi')
def test_delivery_option_token(self): message = FCMRequest(MockNotification(), topic='def') delivery_option = message._delivery_option() self.assertEqual(delivery_option[0], 'topic') self.assertEqual(delivery_option[1], 'def')
def test_json_string_platform_payload_override(self): platform_payload = PlatformPayload(priority=PlatformPayloadPriority.HIGH, collapse_key='collapse_key') apns_payload = PlatformPayload(platform_type=PlatformPayloadType.APNS, priority=PlatformPayloadPriority.NORMAL, collapse_key='different_collapse_key') message = FCMRequest(MockNotification(platform_payload=platform_payload, apns_payload=apns_payload), token='abc') self.assertEqual(message.json_string(), '{"message": {"apns": {"apns-collapse-id": "different_collapse_key", "apns-priority": "5"}, "token": "abc", "data": {"message_type": "verification"}, "android": {"priority": "HIGH", "collapse_key": "collapse_key"}, "webpush": {"Topic": "collapse_key", "Urgency": "high"}}}')
def test_json_string_topic(self): message = FCMRequest(MockNotification(), topic='abcd') self.assertEqual(message.json_string(), '{"message": {"topic": "abcd", "data": {"message_type": "verification"}}}')
def test_json_string_data_payload(self): message = FCMRequest(MockNotification(data_payload={"verification": "some_code"}), token='abc') self.assertEqual(message.json_string(), '{"message": {"token": "abc", "data": {"message_type": "verification", "verification": "some_code"}}}')
def test_send(self): message = FCMRequest(notification=MockNotification(), token='abc') response = message.send() self.assertEqual(response.status_code, 200) self.assertEqual(response.content, 'Some content here')
def test_json_string_notification_payload(self): message = FCMRequest(MockNotification(notification_payload=NotificationPayload(title='Title Here', body='Body here')), token='abc') self.assertEqual(message.json_string(), '{"message": {"notification": {"body": "Body here", "title": "Title Here"}, "token": "abc", "data": {"message_type": "verification"}}}')