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_generate_webhook_checksum_hmac_unicode_nonascii(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com', '\x80secret') message_json = message._json_string() self.assertEqual( message._generate_webhook_hmac(message_json), '2e9e974847184a9f611ed082ba0e76525d1364de520968fbe188737344358d61')
def test_generate_webhook_checksum_hmac_unicode_ascii(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com', unicode('secret')) message_json = message._json_string() self.assertEqual( message._generate_webhook_hmac(message_json), 'fb2b61d55884a648b35801688754924778b3e71ea2bf8f5effb0f3ffecb5c940')
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_send(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com', 'secret') with patch.object(urllib2, 'urlopen') as mock_urlopen, patch.object( message, 'defer_track_notification') as mock_track: success = message.send() mock_urlopen.assert_called_once() mock_track.assert_called_once_with(1) self.assertTrue(success)
def _ping_webhook(client): from models.notifications.ping import PingNotification notification = PingNotification() from models.notifications.requests.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, client.messaging_id, client.secret) logging.info('Ping - {}'.format(str(webhook_request))) success = webhook_request.send() logging.info('Ping Sent') return success
def verify_webhook(url, secret): """ Immediately dispatch a Verification to a webhook """ from models.notifications.verification import VerificationNotification notification = VerificationNotification(url, secret) from models.notifications.requests.webhook_request import WebhookRequest webhook_request = WebhookRequest(notification, url, secret) logging.info('Verification - {}'.format(str(webhook_request))) webhook_request.send() logging.info('Verification Key - {}'.format(notification.verification_key)) return notification.verification_key
def test_send_error_other(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com', 'secret') error_mock = Mock() error_mock.side_effect = Exception('testing') with patch.object(urllib2, 'urlopen', error_mock) as mock_urlopen, patch.object( message, 'defer_track_notification') as mock_track: success = message.send() mock_urlopen.assert_called_once() mock_track.assert_not_called() self.assertTrue(success)
def _send_webhook(cls, clients, notification): # Only send to webhooks if notifications are enabled if not cls._notifications_enabled(): return 1 # Make sure we're only sending to webhook clients clients = [client for client in clients if client.client_type == ClientType.WEBHOOK] from models.notifications.requests.webhook_request import WebhookRequest for client in clients: webhook_request = WebhookRequest(notification, client.messaging_id, client.secret) logging.info(str(webhook_request)) webhook_request.send() return 0
def test_send_headers(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com', 'secret') with patch.object(urllib2, 'urlopen') as mock_urlopen: message.send() mock_urlopen.assert_called_once() request = mock_urlopen.call_args_list[0][0][0] self.assertIsNotNone(request) self.assertEqual( request.headers, { 'X-tba-checksum': 'dbecd85ae53d221c387647085912d2107fa04cd5', 'Content-type': 'application/json; charset="utf-8"', 'X-tba-hmac': 'fb2b61d55884a648b35801688754924778b3e71ea2bf8f5effb0f3ffecb5c940', 'X-tba-version': '1' })
def test_send_fail_deadline_error(self): message = WebhookRequest( MockNotification(webhook_message_data={'data': 'value'}), 'https://www.thebluealliance.com', 'secret') error_mock = Mock() error_mock.side_effect = Exception( 'Deadline exceeded while waiting for HTTP response from URL: https://thebluealliance.com' ) with patch.object(urllib2, 'urlopen', error_mock) as mock_urlopen, patch.object( message, 'defer_track_notification') as mock_track: success = message.send() mock_urlopen.assert_called_once() mock_track.assert_not_called() self.assertTrue(success)
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_str(self): message_str = WebhookRequest(MockNotification(), 'https://www.thebluealliance.com', 'secret') self.assertTrue('WebhookRequest(notification=' in str(message_str))
def test_subclass(self): request = WebhookRequest(MockNotification(), 'https://www.thebluealliance.com', 'secret') self.assertTrue(isinstance(request, Request))