def validate_token(token_str: bytes, kind: int) -> None: if token_str == '' or len(token_str) > 4096: raise JsonableError(_('Empty or invalid length token')) if kind == PushDeviceToken.APNS: # Validate that we can actually decode the token. try: b64_to_hex(token_str) except Exception: raise JsonableError(_('Invalid APNS token'))
def test_end_to_end(self): # type: () -> None remote_gcm_tokens = [u'dddd'] for token in remote_gcm_tokens: RemotePushDeviceToken.objects.create( kind=RemotePushDeviceToken.GCM, token=apn.hex_to_b64(token), user_id=self.user_profile.id, server=RemoteZulipServer.objects.get(uuid=self.server_uuid), ) message = self.get_message(Recipient.PERSONAL, type_id=1) UserMessage.objects.create( user_profile=self.user_profile, message=message ) missed_message = { 'message_id': message.id, 'triggers': { 'private_message': True, 'mentioned': False, 'stream_push_notify': False, }, } with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \ mock.patch('zerver.lib.push_notifications.requests.request', side_effect=self.bounce_request), \ mock.patch('zerver.lib.push_notifications.gcm') as mock_gcm, \ mock.patch('zerver.lib.push_notifications._apns_client') as mock_apns, \ mock.patch('logging.info') as mock_info, \ mock.patch('logging.warning'): apns_devices = [ (apn.b64_to_hex(device.token), device.ios_app_id, device.token) for device in RemotePushDeviceToken.objects.filter( kind=PushDeviceToken.APNS) ] gcm_devices = [ (apn.b64_to_hex(device.token), device.ios_app_id, device.token) for device in RemotePushDeviceToken.objects.filter( kind=PushDeviceToken.GCM) ] mock_gcm.json_request.return_value = { 'success': {gcm_devices[0][2]: message.id}} mock_apns.get_notification_result.return_value = 'Success' apn.handle_push_notification(self.user_profile.id, missed_message) for _, _, token in apns_devices: mock_info.assert_any_call( "APNs: Success sending for user %d to device %s", self.user_profile.id, token) for _, _, token in gcm_devices: mock_info.assert_any_call( "GCM: Sent %s as %s" % (token, message.id))
def test_end_to_end(self): # type: () -> None remote_gcm_tokens = [u'dddd'] for token in remote_gcm_tokens: RemotePushDeviceToken.objects.create( kind=RemotePushDeviceToken.GCM, token=apn.hex_to_b64(token), user_id=self.user_profile.id, server=RemoteZulipServer.objects.get(uuid=self.server_uuid), ) message = self.get_message(Recipient.PERSONAL, type_id=1) UserMessage.objects.create( user_profile=self.user_profile, message=message ) missed_message = { 'message_id': message.id, 'trigger': 'private_message', } with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \ mock.patch('zerver.lib.push_notifications.requests.request', side_effect=self.bounce_request), \ mock.patch('zerver.lib.push_notifications.gcm') as mock_gcm, \ mock.patch('zerver.lib.push_notifications._apns_client') as mock_apns, \ mock.patch('logging.info') as mock_info, \ mock.patch('logging.warning'): apns_devices = [ (apn.b64_to_hex(device.token), device.ios_app_id, device.token) for device in RemotePushDeviceToken.objects.filter( kind=PushDeviceToken.APNS) ] gcm_devices = [ (apn.b64_to_hex(device.token), device.ios_app_id, device.token) for device in RemotePushDeviceToken.objects.filter( kind=PushDeviceToken.GCM) ] mock_gcm.json_request.return_value = { 'success': {gcm_devices[0][2]: message.id}} mock_apns.get_notification_result.return_value = 'Success' apn.handle_push_notification(self.user_profile.id, missed_message) for _, _, token in apns_devices: mock_info.assert_any_call( "APNs: Success sending for user %d to device %s", self.user_profile.id, token) for _, _, token in gcm_devices: mock_info.assert_any_call( "GCM: Sent %s as %s" % (token, message.id))
def test_end_to_end_connection_error(self): # type: () -> None remote_gcm_tokens = [u'dddd'] for token in remote_gcm_tokens: RemotePushDeviceToken.objects.create( kind=RemotePushDeviceToken.GCM, token=apn.hex_to_b64(token), user_id=self.user_profile.id, server=RemoteZulipServer.objects.get(uuid=self.server_uuid), ) message = self.get_message(Recipient.PERSONAL, type_id=1) UserMessage.objects.create( user_profile=self.user_profile, message=message ) def retry(queue_name, event, processor): # type: (Any, Any, Any) -> None apn.handle_push_notification(event['user_profile_id'], event) missed_message = { 'user_profile_id': self.user_profile.id, 'message_id': message.id, 'triggers': { 'private_message': True, 'mentioned': False, 'stream_push_notify': False, }, } with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \ mock.patch('zerver.lib.push_notifications.requests.request', side_effect=self.bounce_request), \ mock.patch('zerver.lib.push_notifications.gcm') as mock_gcm, \ mock.patch('zerver.lib.push_notifications.send_notifications_to_bouncer', side_effect=requests.ConnectionError), \ mock.patch('zerver.lib.queue.queue_json_publish', side_effect=retry) as mock_retry, \ mock.patch('logging.warning') as mock_warn: gcm_devices = [ (apn.b64_to_hex(device.token), device.ios_app_id, device.token) for device in RemotePushDeviceToken.objects.filter( kind=PushDeviceToken.GCM) ] mock_gcm.json_request.return_value = { 'success': {gcm_devices[0][2]: message.id}} apn.handle_push_notification(self.user_profile.id, missed_message) self.assertEqual(mock_retry.call_count, 3) mock_warn.assert_called_with("Maximum retries exceeded for " "trigger:%s event:" "push_notification" % (self.user_profile.id,))
def test_end_to_end_connection_error(self): # type: () -> None remote_gcm_tokens = [u'dddd'] for token in remote_gcm_tokens: RemotePushDeviceToken.objects.create( kind=RemotePushDeviceToken.GCM, token=apn.hex_to_b64(token), user_id=self.user_profile.id, server=RemoteZulipServer.objects.get(uuid=self.server_uuid), ) message = self.get_message(Recipient.PERSONAL, type_id=1) UserMessage.objects.create( user_profile=self.user_profile, message=message ) def retry(queue_name, event, processor): # type: (Any, Any, Any) -> None apn.handle_push_notification(event['user_profile_id'], event) missed_message = { 'user_profile_id': self.user_profile.id, 'message_id': message.id, 'trigger': 'private_message', } with self.settings(PUSH_NOTIFICATION_BOUNCER_URL=''), \ mock.patch('zerver.lib.push_notifications.requests.request', side_effect=self.bounce_request), \ mock.patch('zerver.lib.push_notifications.gcm') as mock_gcm, \ mock.patch('zerver.lib.push_notifications.send_notifications_to_bouncer', side_effect=requests.ConnectionError), \ mock.patch('zerver.lib.queue.queue_json_publish', side_effect=retry) as mock_retry, \ mock.patch('logging.warning') as mock_warn: gcm_devices = [ (apn.b64_to_hex(device.token), device.ios_app_id, device.token) for device in RemotePushDeviceToken.objects.filter( kind=PushDeviceToken.GCM) ] mock_gcm.json_request.return_value = { 'success': {gcm_devices[0][2]: message.id}} apn.handle_push_notification(self.user_profile.id, missed_message) self.assertEqual(mock_retry.call_count, 3) mock_warn.assert_called_with("Maximum retries exceeded for " "trigger:%s event:" "push_notification" % (self.user_profile.id,))