def test_generate_webhook_checksum(self): message = WebhookMessage( MockNotification(webhook_payload={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') message_json = message.json_string() self.assertEqual(message._generate_webhook_checksum(message_json), 'dbecd85ae53d221c387647085912d2107fa04cd5')
def ping(self, request): """ Immediately dispatch a Ping to either FCM or a webhook """ if not self._authenticated: raise remote.ApplicationError('Unauthenticated') if request.fcm and request.webhook: return TBANSResponse(code=400, message='Cannot ping both FCM and webhook') from tbans.models.notifications.ping import PingNotification notification = PingNotification() if request.fcm: from tbans.models.messages.fcm_message import FCMMessage message = FCMMessage(notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition) logging.info('Ping - {}'.format(str(message))) response = message.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) elif request.webhook: from tbans.models.messages.webhook_message import WebhookMessage message = WebhookMessage(notification, request.webhook.url, request.webhook.secret) logging.info('Ping - {}'.format(str(message))) response = message.send() logging.info('Ping Response - {}'.format(str(response))) return TBANSResponse(code=response.status_code, message=response.content) else: return TBANSResponse(code=400, message='Did not specify FCM or webhook to ping')
def test_send(self): message = WebhookMessage( MockNotification(webhook_payload={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') response = message.send() self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.content)
def test_webhook_message(self): webhook_payload = {'test': 'something'} notification = MockNotification(webhook_payload=webhook_payload) message = WebhookMessage(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.models.messages.webhook_message import WebhookMessage message = WebhookMessage(notification, request.webhook.url, request.webhook.secret) logging.info('Verification - {}'.format(str(message))) response = message.send() logging.info('Verification Response - {}'.format(str(response))) return VerificationResponse(code=response.status_code, message=response.content, verification_key=notification.verification_key)
def test_webhook_message_no_payload(self): notification = MockNotification() message = WebhookMessage(notification, 'https://www.thebluealliance.com/', 'secret') self.assertEqual(message.json_string(), '{"message_type": "verification"}')
def test_str(self): message_str = WebhookMessage(MockNotification(), 'https://www.thebluealliance.com/', 'secret') self.assertTrue('WebhookMessage(notification=' in str(message_str))
def test_secret_type(self): with self.assertRaises(TypeError): WebhookMessage(MockNotification(), 'https://www.thebluealliance.com/', 200)
def test_url_type(self): with self.assertRaises(TypeError): WebhookMessage(MockNotification(), 200, 'secret')
def test_url(self): with self.assertRaises(ValueError): WebhookMessage(MockNotification(), None, 'secret')
def test_subclass(self): message = WebhookMessage(MockNotification(), 'https://www.thebluealliance.com/', 'secret') self.assertTrue(isinstance(message, Message))