def test_send_webhook_filter_webhook_clients(self):
        expected = 'client_type_{}'.format(ClientType.WEBHOOK)
        clients = [
            MobileClient(parent=ndb.Key(Account, 'user_id'),
                         user_id='user_id',
                         messaging_id='client_type_{}'.format(client_type),
                         client_type=client_type)
            for client_type in ClientType.names.keys()
        ]

        with patch(
                'models.notifications.requests.webhook_request.WebhookRequest',
                autospec=True) as mock_init:
            exit_code = TBANSHelper._send_webhook(clients, MockNotification())
            mock_init.assert_called_once_with(ANY, expected, ANY)
            self.assertEqual(exit_code, 0)
예제 #2
0
 def test_send_failed_partial(self):
     batch_response = messaging.BatchResponse([
         messaging.SendResponse({'name': 'abc'}, None),
         messaging.SendResponse(None, 'a')
     ])
     request = FCMRequest(app=self.app,
                          notification=MockNotification(),
                          tokens=['abc', 'def'])
     with patch.object(
             messaging, 'send_multicast',
             return_value=batch_response) as mock_send, patch.object(
                 request, 'defer_track_notification') as mock_track:
         response = request.send()
     mock_send.assert_called_once()
     mock_track.assert_called_once_with(1)
     self.assertEqual(response, batch_response)
    def test_send_error_unknown(self):
        message = WebhookRequest(
            MockNotification(webhook_message_data={'data': 'value'}),
            'https://www.thebluealliance.com', 'secret')

        error_mock = Mock()
        error_mock.side_effect = MockHTTPError(-1)

        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_send_fcm_batch(self):
        clients = [
            MobileClient(parent=ndb.Key(Account, 'user_id'),
                         user_id='user_id',
                         messaging_id='{}'.format(i),
                         client_type=ClientType.OS_IOS) for i in range(3)
        ]

        batch_response = messaging.BatchResponse([])
        with patch('models.notifications.requests.fcm_request.MAXIMUM_TOKENS',
                   2), patch.object(FCMRequest,
                                    'send',
                                    return_value=batch_response) as mock_send:
            exit_code = TBANSHelper._send_fcm(clients, MockNotification())
            self.assertEqual(mock_send.call_count, 2)
            self.assertEqual(exit_code, 0)
예제 #5
0
 def test_fcm_message_notification(self):
     platform_config = PlatformConfig(priority=PlatformPriority.HIGH,
                                      collapse_key='collapse_key')
     request = FCMRequest(self.app,
                          notification=MockNotification(
                              fcm_notification=messaging.Notification(
                                  title='Title', body='Some body message')),
                          tokens=['abc'])
     message = request._fcm_message()
     self.assertIsNotNone(message)
     self.assertIsNotNone(message.data)
     self.assertTrue(
         isinstance(message.notification, messaging.Notification))
     self.assertIsNone(message.android)
     self.assertTrue(isinstance(message.apns, messaging.APNSConfig))
     self.assertIsNone(message.webpush)
     self.assertEqual(message.tokens, ['abc'])
예제 #6
0
 def test_fcm_message_apns_content_available(self):
     request = FCMRequest(self.app,
                          notification=MockNotification(),
                          tokens=['abc'])
     message = request._fcm_message()
     self.assertIsNotNone(message)
     self.assertIsNotNone(message.data)
     self.assertIsNone(message.notification)
     self.assertIsNone(message.android)
     self.assertTrue(isinstance(message.apns, messaging.APNSConfig))
     self.assertTrue(isinstance(message.apns.payload,
                                messaging.APNSPayload))
     self.assertTrue(isinstance(message.apns.payload.aps, messaging.Aps))
     self.assertIsNone(message.apns.payload.aps.sound)
     self.assertTrue(message.apns.payload.aps.content_available)
     self.assertIsNone(message.webpush)
     self.assertEqual(message.tokens, ['abc'])
    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_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'
            })
예제 #9
0
    def test_defer_webhook(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='messaging_id',
                              client_type=ClientType.WEBHOOK)
        client.put()
        notification = MockNotification()
        TBANSHelper._defer_webhook([client], notification)

        # Make sure we'll send to FCM clients
        tasks = self.taskqueue_stub.get_filtered_tasks(
            queue_names='push-notifications')
        self.assertEqual(len(tasks), 1)

        # Make sure our taskqueue tasks execute what we expect
        with patch.object(TBANSHelper, '_send_webhook') as mock_send_webhook:
            deferred.run(tasks[0].payload)
            mock_send_webhook.assert_called_once_with([client], ANY)
예제 #10
0
 def test_fcm_message_data_payload(self):
     platform_config = PlatformConfig(priority=PlatformPriority.HIGH,
                                      collapse_key='collapse_key')
     request = FCMRequest(self.app,
                          notification=MockNotification(
                              data_payload={'some_data': 'some test data'}),
                          tokens=['abc'])
     message = request._fcm_message()
     self.assertIsNotNone(message)
     self.assertEqual(message.data, {
         'notification_type': 'verification',
         'some_data': 'some test data'
     })
     self.assertIsNone(message.notification)
     self.assertIsNone(message.android)
     self.assertTrue(isinstance(message.apns, messaging.APNSConfig))
     self.assertIsNone(message.webpush)
     self.assertEqual(message.tokens, ['abc'])
예제 #11
0
    def test_send_webhook_filter_webhook_clients_verified(self):
        clients = [
            MobileClient(parent=ndb.Key(Account, 'user_id'),
                         user_id='user_id',
                         messaging_id='unverified',
                         client_type=ClientType.WEBHOOK,
                         verified=False),
            MobileClient(parent=ndb.Key(Account, 'user_id'),
                         user_id='user_id',
                         messaging_id='verified',
                         client_type=ClientType.WEBHOOK,
                         verified=True)
        ]

        with patch(
                'models.notifications.requests.webhook_request.WebhookRequest',
                autospec=True) as mock_init:
            exit_code = TBANSHelper._send_webhook(clients, MockNotification())
            mock_init.assert_called_once_with(ANY, 'verified', ANY)
            self.assertEqual(exit_code, 0)
예제 #12
0
    def test_send(self):
        expected = [
            'client_type_{}'.format(client_type)
            for client_type in ClientType.FCM_CLIENTS
        ]
        clients = [
            MobileClient(parent=ndb.Key(Account, 'user_id'),
                         user_id='user_id',
                         messaging_id='client_type_{}'.format(client_type),
                         client_type=client_type)
            for client_type in ClientType.names.keys()
        ]
        # Insert an unverified webhook, just to test
        unverified = MobileClient(parent=ndb.Key(Account, 'user_id'),
                                  user_id='user_id',
                                  messaging_id='client_type_2',
                                  client_type=ClientType.WEBHOOK,
                                  verified=False)
        unverified.put()
        for c in clients:
            c.put()

        expected_fcm = [
            c for c in clients if c.client_type in ClientType.FCM_CLIENTS
        ]
        expected_webhook = [
            c for c in clients if c.client_type == ClientType.WEBHOOK
        ]

        notification = MockNotification()
        with patch.object(TBANSHelper, '_defer_fcm') as mock_fcm, patch.object(
                TBANSHelper, '_defer_webhook') as mock_webhook:
            TBANSHelper._send(['user_id'], notification)
            mock_fcm.assert_called_once_with(expected_fcm, notification)
            mock_webhook.assert_called_once_with(expected_webhook,
                                                 notification)
예제 #13
0
 def test_fcm_message_platform_config_override(self):
     platform_config = PlatformConfig(priority=PlatformPriority.HIGH,
                                      collapse_key='collapse_key')
     apns_config = messaging.APNSConfig(
         headers={'apns-collapse-id': 'ios_collapse_key'})
     request = FCMRequest(self.app,
                          notification=MockNotification(
                              platform_config=platform_config,
                              apns_config=apns_config),
                          tokens=['abc'])
     message = request._fcm_message()
     self.assertIsNotNone(message)
     self.assertIsNotNone(message.data)
     self.assertIsNone(message.notification)
     self.assertTrue(isinstance(message.android, messaging.AndroidConfig))
     self.assertTrue(isinstance(message.apns, messaging.APNSConfig))
     self.assertEqual(message.apns.headers,
                      {'apns-collapse-id': 'ios_collapse_key'})
     self.assertTrue(isinstance(message.webpush, messaging.WebpushConfig))
     self.assertEqual(message.webpush.headers, {
         'Topic': 'collapse_key',
         'Urgency': 'high'
     })
     self.assertEqual(message.tokens, ['abc'])
예제 #14
0
 def test_webhook_message_data(self):
     webhook_message_data = {'data': 'here'}
     notification = MockNotification(webhook_message_data=webhook_message_data)
     self.assertIsNone(notification.data_payload)
     self.assertEqual(notification.webhook_message_data, webhook_message_data)
예제 #15
0
 def test_subclass(self):
     request = FCMRequest(self.app, MockNotification(), tokens=['abcd'])
     self.assertTrue(isinstance(request, Request))
예제 #16
0
 def test_init_app(self):
     FCMRequest(self.app, notification=MockNotification(), tokens=['abcd'])
예제 #17
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"}')
예제 #18
0
 def test_str(self):
     message_str = WebhookRequest(MockNotification(),
                                  'https://www.thebluealliance.com',
                                  'secret')
     self.assertTrue('WebhookRequest(notification=' in str(message_str))
예제 #19
0
 def test_subclass(self):
     request = WebhookRequest(MockNotification(),
                              'https://www.thebluealliance.com', 'secret')
     self.assertTrue(isinstance(request, Request))
예제 #20
0
 def test_init_delivery_none(self):
     with self.assertRaises(TypeError):
         FCMRequest(self.app, notification=MockNotification())
예제 #21
0
 def test_init(self):
     Request(MockNotification())
예제 #22
0
 def test_send(self):
     request = Request(MockNotification())
     with self.assertRaises(NotImplementedError):
         request.send()
예제 #23
0
 def test_webhook_message_data_same_data_payload(self):
     data_payload = {'data': 'here'}
     notification = MockNotification(data_payload=data_payload)
     self.assertEqual(notification.data_payload, data_payload)
     self.assertEqual(notification.webhook_message_data, data_payload)