예제 #1
0
 def test_clients_empty(self):
     abc = MobileClient(
         parent=ndb.Key(Account, 'abc'),
         user_id='abc',
         messaging_id='token',
         client_type=ClientType.OS_IOS,
         device_uuid='uuid',
         display_name='Phone'
     )
     abc.put()
     unverified = MobileClient(
         parent=ndb.Key(Account, 'efg'),
         user_id='efg',
         messaging_id='token',
         client_type=ClientType.OS_IOS,
         device_uuid='uuid',
         display_name='Phone',
         verified=False
     )
     unverified.put()
     # Test empty users returns empty
     self.assertEqual(MobileClient.clients(users=[]), [])
     # Test empty client types return empty
     self.assertEqual(MobileClient.clients(users=['abc'], client_types=[]), [])
     # Test empty users and client types returns empty
     self.assertEqual(MobileClient.clients(users=[], client_types=[]), [])
     # Test client type + users does not return empty
     self.assertEqual(MobileClient.clients(users=['abc']), [abc])
     # Test fetching for only verified
     self.assertEqual(MobileClient.clients(users=['efg']), [])
예제 #2
0
    def test_delete_for_messaging_id(self):
        user_id_one = 'user_id_one'
        messaging_id_one = 'messaging_id1'
        messaging_id_two = 'messaging_id2'

        user_id_two = 'user_id_two'
        messaging_id_three = 'messaging_id3'

        for (user_id, messaging_ids) in [(user_id_one, [messaging_id_one, messaging_id_two]), (user_id_two, [messaging_id_three])]:
            for messaging_id in messaging_ids:
                MobileClient(
                    parent=ndb.Key(Account, user_id),
                    user_id=user_id,
                    messaging_id=messaging_id,
                    client_type=ClientType.OS_IOS,
                    device_uuid=messaging_id[::-1],
                    display_name='Phone').put()

        MobileClient.delete_for_messaging_id(messaging_id_one)

        clients_one = [client.messaging_id for client in MobileClient.query(MobileClient.user_id == 'user_id_one').fetch()]
        clients_two = [client.messaging_id for client in MobileClient.query(MobileClient.user_id == 'user_id_two').fetch()]

        self.assertEqual(clients_one, [messaging_id_two])
        self.assertEqual(clients_two, [messaging_id_three])

        MobileClient.delete_for_messaging_id(messaging_id_two)

        clients_one = [client.messaging_id for client in MobileClient.query(MobileClient.user_id == 'user_id_one').fetch()]
        clients_two = [client.messaging_id for client in MobileClient.query(MobileClient.user_id == 'user_id_two').fetch()]

        self.assertEqual(clients_one, [])
        self.assertEqual(clients_two, [messaging_id_three])

        MobileClient.delete_for_messaging_id('does_not_exist')
예제 #3
0
    def test_send_fcm_unavailable_error(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='messaging_id',
                              client_type=ClientType.OS_IOS)
        client.put()

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id'])

        batch_response = messaging.BatchResponse([
            messaging.SendResponse(None, UnavailableError('code', 'message'))
        ])
        with patch.object(FCMRequest, 'send',
                          return_value=batch_response), patch(
                              'logging.error') as mock_error:
            exit_code = TBANSHelper._send_fcm([client], MockNotification())
            self.assertEqual(exit_code, 0)
            mock_error.assert_called_once_with(
                'FCM unavailable - retrying client...')

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id'])

        # Check that we queue'd for a retry
        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_fcm') as mock_send_fcm:
            deferred.run(tasks[0].payload)
            mock_send_fcm.assert_called_once_with([client], ANY, 1)
예제 #4
0
    def test_broadcast_fcm(self):
        for client_type in ClientType.FCM_CLIENTS:
            client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                                  user_id='user_id',
                                  messaging_id='token',
                                  client_type=client_type,
                                  device_uuid='uuid',
                                  display_name='Phone')
            client_key = client.put()

            from notifications.base_notification import BaseNotification
            with patch.object(BaseNotification, 'send') as mock_send:
                TBANSHelper.broadcast([client_type], 'Broadcast',
                                      'Test broadcast')
                # Make sure we didn't send to Android
                mock_send.assert_not_called()

            # 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_fcm') as mock_send_fcm:
                deferred.run(tasks[0].payload)
                mock_send_fcm.assert_called_once_with([client], ANY)
                # Make sure the notification is a BroadcastNotification
                notification = mock_send_fcm.call_args[0][1]
                self.assertTrue(isinstance(notification,
                                           BroadcastNotification))

            self.taskqueue_stub.FlushQueue('push-notifications')

            client_key.delete()
예제 #5
0
    def test_send_fcm_sender_id_mismatch_error(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='messaging_id',
                              client_type=ClientType.OS_IOS)
        client.put()

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id'])

        batch_response = messaging.BatchResponse([
            messaging.SendResponse(None,
                                   SenderIdMismatchError('code', 'message'))
        ])
        with patch.object(FCMRequest, 'send', return_value=batch_response), \
            patch.object(MobileClient, 'delete_for_messaging_id', wraps=MobileClient.delete_for_messaging_id) as mock_delete, \
                patch('logging.info') as mock_info:
            exit_code = TBANSHelper._send_fcm([client], MockNotification())
            mock_delete.assert_called_once_with('messaging_id')
            self.assertEqual(exit_code, 0)
            mock_info.assert_called_with(
                'Deleting mismatched client with ID: messaging_id')

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), [])

        # Make sure we haven't queued for a retry
        tasks = self.taskqueue_stub.get_filtered_tasks(
            queue_names='push-notifications')
        self.assertEqual(len(tasks), 0)
    def post(self):
        if not self.user_bundle.user:
            self.response.set_status(401)
            return

        user_id = self.user_bundle.user.user_id()
        fcm_token = self.request.get('fcm_token')
        uuid = self.request.get('uuid')
        display_name = self.request.get('display_name')
        client_type = ClientType.WEB

        query = MobileClient.query(MobileClient.user_id == user_id,
                                   MobileClient.device_uuid == uuid,
                                   MobileClient.client_type == client_type)
        if query.count() == 0:
            # Record doesn't exist yet, so add it
            MobileClient(parent=ndb.Key(Account, user_id),
                         user_id=user_id,
                         messaging_id=fcm_token,
                         client_type=client_type,
                         device_uuid=uuid,
                         display_name=display_name).put()
        else:
            # Record already exists, update it
            client = query.fetch(1)[0]
            client.messaging_id = fcm_token
            client.display_name = display_name
            client.put()
예제 #7
0
    def test_send_fcm_unhandled_error(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='messaging_id',
                              client_type=ClientType.OS_IOS)
        client.put()

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id'])

        batch_response = messaging.BatchResponse(
            [messaging.SendResponse(None, FirebaseError('code', 'message'))])
        with patch.object(FCMRequest, 'send',
                          return_value=batch_response), patch(
                              'logging.error') as mock_error:
            exit_code = TBANSHelper._send_fcm([client], MockNotification())
            self.assertEqual(exit_code, 0)
            mock_error.assert_called_once_with(
                'Unhandled FCM error for messaging_id - code / message')

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id'])

        # Check that we didn't queue for a retry
        tasks = self.taskqueue_stub.get_filtered_tasks(
            queue_names='push-notifications')
        self.assertEqual(len(tasks), 0)
예제 #8
0
    def register_client(self, request):
        user_id = get_current_user_id(self.headers)
        if user_id is None:
            return BaseResponse(code=401, message="Unauthorized to register")
        gcm_id = request.mobile_id
        os = ClientType.enums[request.operating_system]
        name = request.name
        uuid = request.device_uuid

        query = MobileClient.query(MobileClient.user_id == user_id,
                                   MobileClient.device_uuid == uuid,
                                   MobileClient.client_type == os)
        # trying to figure out an elusive dupe bug
        logging.info("DEBUGGING")
        logging.info("User ID: {}".format(user_id))
        logging.info("UUID: {}".format(uuid))
        logging.info("Count: {}".format(query.count()))
        if query.count() == 0:
            # Record doesn't exist yet, so add it
            MobileClient(parent=ndb.Key(Account, user_id),
                         user_id=user_id,
                         messaging_id=gcm_id,
                         client_type=os,
                         device_uuid=uuid,
                         display_name=name).put()
            return BaseResponse(code=200, message="Registration successful")
        else:
            # Record already exists, update it
            client = query.fetch(1)[0]
            client.messaging_id = gcm_id
            client.display_name = name
            client.put()
            return BaseResponse(code=304, message="Client already exists")
예제 #9
0
    def test_send_fcm_invalid_argument_error(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='messaging_id',
                              client_type=ClientType.OS_IOS)
        client.put()

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id'])

        batch_response = messaging.BatchResponse([
            messaging.SendResponse(None,
                                   InvalidArgumentError('code', 'message'))
        ])
        with patch.object(FCMRequest, 'send',
                          return_value=batch_response), patch(
                              'logging.critical') as mock_critical:
            exit_code = TBANSHelper._send_fcm([client], MockNotification())
            self.assertEqual(exit_code, 0)
            mock_critical.assert_called_once_with(
                'Invalid argument when sending to FCM - code')

        # Sanity check
        self.assertEqual(fcm_messaging_ids('user_id'), ['messaging_id'])

        # Make sure we haven't queued for a retry
        tasks = self.taskqueue_stub.get_filtered_tasks(
            queue_names='push-notifications')
        self.assertEqual(len(tasks), 0)
예제 #10
0
    def post(self):
        self._require_login()
        self._require_registration()

        # Check to make sure that they aren't trying to edit another user
        current_user_account_id = self.user_bundle.account.key.id()
        target_account_id = self.request.get('account_id')
        if target_account_id == current_user_account_id:
            url = self.request.get('url')
            secret_key = self.request.get('secret')
            query = MobileClient.query(MobileClient.messaging_id == url,
                                       ancestor=ndb.Key(
                                           Account, current_user_account_id))
            if query.count() == 0:
                # Webhook doesn't exist, add it
                verification_key = NotificationHelper.verify_webhook(
                    url, secret_key)
                client = MobileClient(parent=self.user_bundle.account.key,
                                      user_id=current_user_account_id,
                                      messaging_id=url,
                                      display_name=self.request.get('name'),
                                      secret=secret_key,
                                      client_type=ClientType.WEBHOOK,
                                      verified=False,
                                      verification_code=verification_key)
                client.put()
            else:
                # Webhook already exists. Update the secret
                current = query.fetch()[0]
                current.secret = secret_key
                current.put()
            self.redirect('/account')
        else:
            self.redirect('/')
예제 #11
0
    def test_clients(self):
        user_id_one = 'user_id_one'
        token_one = 'token1'
        token_two = 'token2'

        user_id_two = 'user_id_two'
        token_three = 'token3'

        user_id_three = 'user_id_three'

        for (user_id, tokens) in [(user_id_one, [token_one, token_two]), (user_id_two, [token_three])]:
            clients = [MobileClient(
                        parent=ndb.Key(Account, user_id),
                        user_id=user_id,
                        messaging_id=token,
                        client_type=ClientType.OS_IOS,
                        device_uuid=token[::-1],
                        display_name='Phone') for token in tokens]
            for client in clients:
                client.put()

        self.assertEqual([client.messaging_id for client in MobileClient.clients([user_id_one])], [token_one, token_two])
        self.assertEqual([client.messaging_id for client in MobileClient.clients([user_id_two])], [token_three])
        self.assertEqual([client.messaging_id for client in MobileClient.clients([user_id_one, user_id_two])], [token_one, token_two, token_three])
        self.assertEqual([client.messaging_id for client in MobileClient.clients([user_id_three])], [])
예제 #12
0
    def test_send_fcm_retry_backoff_time(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='messaging_id',
                              client_type=ClientType.OS_IOS)
        client.put()

        import time

        batch_response = messaging.BatchResponse([
            messaging.SendResponse(None, QuotaExceededError('code', 'message'))
        ])
        for i in range(0, 6):
            with patch.object(
                    FCMRequest, 'send',
                    return_value=batch_response), patch('logging.error'):
                call_time = time.time()
                TBANSHelper._send_fcm([client], MockNotification(), i)

                # Check that we queue'd for a retry with the proper countdown time
                tasks = self.taskqueue_stub.get_filtered_tasks(
                    queue_names='push-notifications')
                if i > 0:
                    self.assertGreater(tasks[0].eta_posix - call_time, 0)

                # Make sure our taskqueue tasks execute what we expect
                with patch.object(TBANSHelper, '_send_fcm') as mock_send_fcm:
                    deferred.run(tasks[0].payload)
                    mock_send_fcm.assert_called_once_with([client], ANY, i + 1)

                self.taskqueue_stub.FlushQueue('push-notifications')
예제 #13
0
    def test_ping_fcm_unsupported(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='token',
                              client_type=-1,
                              device_uuid='uuid',
                              display_name='Phone')

        with self.assertRaises(Exception,
                               msg='Unsupported FCM client type: -1'):
            TBANSHelper._ping_client(client)
예제 #14
0
 def test_clients_multiple(self):
     abc = MobileClient(
         parent=ndb.Key(Account, 'abc'),
         user_id='abc',
         messaging_id='token',
         client_type=ClientType.OS_IOS,
         device_uuid='uuid',
         display_name='Phone'
     )
     abc.put()
     efg = MobileClient(
         parent=ndb.Key(Account, 'efg'),
         user_id='efg',
         messaging_id='token',
         client_type=ClientType.OS_IOS,
         device_uuid='uuid',
         display_name='Phone'
     )
     efg.put()
     self.assertEqual(MobileClient.clients(['abc', 'efg']), [abc, efg])
예제 #15
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)
예제 #16
0
    def test_clients_type(self):
        clients = [MobileClient(
                    parent=ndb.Key(Account, 'user_id'),
                    user_id='user_id',
                    messaging_id='messaging_id_{}'.format(client_type),
                    client_type=client_type) for client_type in ClientType.names.keys()]
        for client in clients:
            client.put()

        self.assertEqual([client.messaging_id for client in MobileClient.clients(['user_id'], client_types=[ClientType.OS_ANDROID])], ['messaging_id_0'])
        self.assertEqual([client.messaging_id for client in MobileClient.clients(['user_id'], client_types=ClientType.FCM_CLIENTS)], ['messaging_id_1', 'messaging_id_3'])
        self.assertEqual([client.messaging_id for client in MobileClient.clients(['user_id'], client_types=[ClientType.WEBHOOK])], ['messaging_id_2'])
    def test_fcm_messaging_ids_unsupported_type(self):
        user_id = 'user_id'

        for (token, os) in [('a', ClientType.OS_ANDROID), ('b', ClientType.OS_IOS), ('c', ClientType.WEBHOOK), ('d', ClientType.WEB)]:
            MobileClient(
                parent=ndb.Key(Account, user_id),
                user_id=user_id,
                messaging_id=token,
                client_type=os,
                device_uuid=token,
                display_name=token).put()

        self.assertEqual(MobileClient.fcm_messaging_ids(user_id), ['b', 'd'])
예제 #18
0
    def test_ping_webhook(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='https://thebluealliance.com',
                              client_type=ClientType.WEBHOOK,
                              secret='secret',
                              display_name='Webhook')

        with patch.object(TBANSHelper, '_ping_webhook',
                          return_value=True) as mock_ping_webhook:
            success = TBANSHelper.ping(client)
            mock_ping_webhook.assert_called_once_with(client)
            self.assertTrue(success)
예제 #19
0
    def test_ping_client(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='token',
                              client_type=ClientType.OS_IOS,
                              device_uuid='uuid',
                              display_name='Phone')

        with patch.object(TBANSHelper, '_ping_client',
                          return_value=True) as mock_ping_client:
            success = TBANSHelper.ping(client)
            mock_ping_client.assert_called_once_with(client)
            self.assertTrue(success)
예제 #20
0
    def test_send_webhook_multiple(self):
        clients = [
            MobileClient(parent=ndb.Key(Account, 'user_id'),
                         user_id='user_id',
                         messaging_id='{}'.format(i),
                         client_type=ClientType.WEBHOOK) for i in range(3)
        ]

        batch_response = messaging.BatchResponse([])
        with patch.object(WebhookRequest, 'send',
                          return_value=batch_response) as mock_send:
            exit_code = TBANSHelper._send_webhook(clients, MockNotification())
            self.assertEqual(mock_send.call_count, 3)
            self.assertEqual(exit_code, 0)
예제 #21
0
    def test_ping_webhook_failure(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='https://thebluealliance.com',
                              client_type=ClientType.WEBHOOK,
                              secret='secret',
                              display_name='Webhook')

        from models.notifications.requests.webhook_request import WebhookRequest
        with patch.object(WebhookRequest, 'send',
                          return_value=False) as mock_send:
            success = TBANSHelper._ping_webhook(client)
            mock_send.assert_called_once()
            self.assertFalse(success)
예제 #22
0
    def test_ping_fcm_fail(self):
        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id='token',
                              client_type=ClientType.OS_IOS,
                              device_uuid='uuid',
                              display_name='Phone')

        batch_response = messaging.BatchResponse(
            [messaging.SendResponse(None, FirebaseError(500, 'testing'))])
        with patch.object(FCMRequest, 'send',
                          return_value=batch_response) as mock_send:
            success = TBANSHelper._ping_client(client)
            mock_send.assert_called_once()
            self.assertFalse(success)
예제 #23
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)
예제 #24
0
    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)
예제 #25
0
    def test_ping_android(self):
        client_type = ClientType.OS_ANDROID
        messaging_id = 'token'

        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id=messaging_id,
                              client_type=client_type,
                              device_uuid='uuid',
                              display_name='Phone')

        from notifications.ping import PingNotification
        with patch.object(PingNotification, 'send') as mock_send:
            success = TBANSHelper._ping_client(client)
            mock_send.assert_called_once_with({client_type: [messaging_id]})
            self.assertTrue(success)
예제 #26
0
    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)
예제 #27
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)
예제 #28
0
    def test_broadcast_android(self):
        client_type = ClientType.OS_ANDROID
        messaging_id = 'token'

        client = MobileClient(parent=ndb.Key(Account, 'user_id'),
                              user_id='user_id',
                              messaging_id=messaging_id,
                              client_type=client_type,
                              device_uuid='uuid',
                              display_name='Phone')
        client.put()

        from notifications.broadcast import BroadcastNotification
        with patch.object(BroadcastNotification, 'send') as mock_send:
            TBANSHelper.broadcast([client_type], 'Broadcast', 'Test broadcast')
            mock_send.assert_called_once_with({client_type: [messaging_id]})

        # Make sure we didn't send to FCM or webhooks
        tasks = self.taskqueue_stub.GetTasks('push-notifications')
        self.assertEqual(len(tasks), 0)
예제 #29
0
    def post(self):
        self._require_registration()
        self._require_request_user_is_bundle_user()

        # Name and URL must be non-None
        url = self.request.get('url', None)
        name = self.request.get('name', None)
        if not url or not name:
            return self.redirect('/webhooks/add?error=1')

        # Always generate secret server-side; previously allowed clients to set the secret
        import uuid
        secret = uuid.uuid4().hex

        current_user_account_id = self.user_bundle.account.key.id()
        query = MobileClient.query(MobileClient.messaging_id == url, ancestor=ndb.Key(Account, current_user_account_id))
        if query.count() == 0:
            # Webhook doesn't exist, add it
            from helpers.tbans_helper import TBANSHelper
            verification_key = TBANSHelper.verify_webhook(url, secret)

            client = MobileClient(
                parent=self.user_bundle.account.key,
                user_id=current_user_account_id,
                messaging_id=url,
                display_name=name,
                secret=secret,
                client_type=ClientType.WEBHOOK,
                verified=False,
                verification_code=verification_key)
            client.put()
        else:
            # Webhook already exists. Update the secret
            current = query.fetch()[0]
            current.secret = secret
            current.put()

        self.redirect('/account')
    def test_fcm_messaging_ids(self):
        user_id_one = 'user_id_one'
        token_one = 'token1'
        token_two = 'token2'

        user_id_two = 'user_id_two'
        token_three = 'token3'

        user_id_three = 'user_id_three'

        for (user_id, tokens) in [(user_id_one, [token_one, token_two]), (user_id_two, [token_three])]:
            for token in tokens:
                MobileClient(
                    parent=ndb.Key(Account, user_id),
                    user_id=user_id,
                    messaging_id=token,
                    client_type=ClientType.OS_IOS,
                    device_uuid=token[::-1],
                    display_name='Phone').put()

        self.assertEqual(MobileClient.fcm_messaging_ids(user_id_one), [token_one, token_two])
        self.assertEqual(MobileClient.fcm_messaging_ids(user_id_two), [token_three])
        self.assertEqual(MobileClient.fcm_messaging_ids(user_id_three), [])