Example #1
0
 def test_send(self):
     message = WebhookRequest(
         MockNotification(webhook_payload={'data': 'value'}),
         'https://www.thebluealliance.com/', 'secret')
     response = message.send()
     self.assertEqual(response.status_code, 200)
     self.assertIsNone(response.content)
    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')
Example #3
0
    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')
Example #4
0
 def test_generate_webhook_checksum(self):
     message = WebhookRequest(
         MockNotification(webhook_payload={'data': 'value'}),
         'https://www.thebluealliance.com/', 'secret')
     message_json = message.json_string()
     self.assertEqual(message._generate_webhook_checksum(message_json),
                      'dbecd85ae53d221c387647085912d2107fa04cd5')
Example #5
0
 def test_webhook_message(self):
     webhook_payload = {'test': 'something'}
     notification = MockNotification(webhook_payload=webhook_payload)
     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.models.requests.notifications.webhook_request import WebhookRequest
        webhook_request = WebhookRequest(notification, request.webhook.url, request.webhook.secret)
        logging.info('Verification - {}'.format(str(webhook_request)))

        response = webhook_request.send()
        logging.info('Verification Response - {}'.format(str(response)))
        return VerificationResponse(code=response.status_code, message=response.content, verification_key=notification.verification_key)
Example #7
0
    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.requests.notifications.webhook_request import WebhookRequest
        webhook_request = WebhookRequest(notification, request.webhook.url,
                                         request.webhook.secret)
        logging.info('Verification - {}'.format(str(webhook_request)))

        response = webhook_request.send()
        logging.info('Verification Response - {}'.format(str(response)))
        return VerificationResponse(
            code=response.status_code,
            message=response.content,
            verification_key=notification.verification_key)
Example #8
0
 def test_subclass(self):
     request = WebhookRequest(MockNotification(),
                              'https://www.thebluealliance.com/', 'secret')
     self.assertTrue(isinstance(request, NotificationRequest))
Example #9
0
 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"}')
Example #10
0
 def test_url_empty(self):
     with self.assertRaises(ValueError):
         WebhookRequest(MockNotification(), '', 'secret')
Example #11
0
 def test_url_type(self):
     with self.assertRaises(TypeError):
         WebhookRequest(MockNotification(), 200, 'secret')
 def test_generate_webhook_checksum(self):
     message = WebhookRequest(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 test_send(self):
     message = WebhookRequest(MockNotification(webhook_payload={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret')
     response = message.send()
     self.assertEqual(response.status_code, 200)
     self.assertIsNone(response.content)
 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_payload = {'test': 'something'}
     notification = MockNotification(webhook_payload=webhook_payload)
     message = WebhookRequest(notification, 'https://www.thebluealliance.com/', 'secret')
     self.assertEqual(message.json_string(), '{"message_data": {"test": "something"}, "message_type": "verification"}')
Example #16
0
 def test_secret_type(self):
     with self.assertRaises(TypeError):
         WebhookRequest(MockNotification(),
                        'https://www.thebluealliance.com/', 200)
Example #17
0
 def test_secret_empty(self):
     # TODO: Migrate existing users, make this throw again
     WebhookRequest(MockNotification(), 'https://www.thebluealliance.com/',
                    '')
Example #18
0
 def test_str(self):
     message_str = WebhookRequest(MockNotification(),
                                  'https://www.thebluealliance.com/',
                                  'secret')
     self.assertTrue('WebhookRequest(notification=' in str(message_str))