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_send_fail(self): message = WebhookRequest( MockNotification( webhook_message_data={'data': 'some new value value'}), 'https://somenewwebsite.com', 'somenewsecret') with self.assertRaises(AssertionError): response = message.send()
def test_generate_webhook_checksum(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') message_json = message._json_string() self.assertEqual(message._generate_webhook_checksum(message_json), 'dbecd85ae53d221c387647085912d2107fa04cd5')
def test_send(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') response = message.send() self.assertEqual(response.status_code, 200) self.assertEqual(response.content, 'Some content here')
def test_webhook_message(self): webhook_message_data = {'test': 'something'} notification = MockNotification( webhook_message_data=webhook_message_data) message = WebhookRequest(notification, 'https://www.thebluealliance.com/', 'secret') self.assertEqual( message._json_string(), '{"message_data": {"test": "something"}, "message_type": "verification"}' )
def verification(self, request): """ Immediately dispatch a Verification to a webhook """ self._validate_authentication() from tbans.models.notifications.verification import VerificationNotification notification = VerificationNotification(request.webhook.url, request.webhook.secret) from tbans.requests.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, request.webhook.url, request.webhook.secret) logging.info('Verification - {}'.format(str(webhook_request))) webhook_request.send() logging.info('Verification Key - {}'.format(notification.verification_key)) return VerificationResponse(code=200, verification_key=notification.verification_key)
def test_send_fail(self): message = WebhookRequest(MockNotification(webhook_message_data={'data': 'some new value value'}), 'https://somenewwebsite.com', 'somenewsecret') response = message.send() with self.assertRaises(AssertionError): response.get_result()
def test_send(self): message = WebhookRequest(MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') response = message.send() result = response.get_result() self.assertEqual(result.status_code, 200) self.assertEqual(result.content, 'Some content here')
def test_generate_webhook_checksum(self): message = WebhookRequest(MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') message_json = message._json_string() self.assertEqual(message._generate_webhook_checksum(message_json), 'dbecd85ae53d221c387647085912d2107fa04cd5')
def test_webhook_message_no_payload(self): notification = MockNotification() message = WebhookRequest(notification, 'https://www.thebluealliance.com/', 'secret') self.assertEqual(message._json_string(), '{"message_type": "verification"}')
def test_webhook_message(self): webhook_message_data = {'test': 'something'} notification = MockNotification(webhook_message_data=webhook_message_data) message = WebhookRequest(notification, 'https://www.thebluealliance.com/', 'secret') self.assertEqual(message._json_string(), '{"message_data": {"test": "something"}, "message_type": "verification"}')
def test_subclass(self): request = WebhookRequest(MockNotification(), 'https://www.thebluealliance.com/', 'secret') self.assertTrue(isinstance(request, Request))
def test_url_type(self): with self.assertRaises(TypeError): WebhookRequest(MockNotification(), 200, 'secret')
def test_str(self): message_str = WebhookRequest(MockNotification(), 'https://www.thebluealliance.com/', 'secret') self.assertTrue('WebhookRequest(notification=' in str(message_str))
def test_secret_empty(self): with self.assertRaises(ValueError): WebhookRequest(MockNotification(), 'https://www.thebluealliance.com/', '')
def test_secret_type(self): with self.assertRaises(TypeError): WebhookRequest(MockNotification(), 'https://www.thebluealliance.com/', 200)
def test_url_empty(self): with self.assertRaises(ValueError): WebhookRequest(MockNotification(), '', 'secret')