'This is a variant of the below test_push_api, but using the full
push notification bouncer flow'

@override_settings(PUSH_NOTIFICATION_BOUNCER_URL='https://push.zulip.org.example.com')
@mock.patch('zerver.lib.push_notifications.requests.request')
def test_push_bouncer_api(self, mock):

    mock.side_effect = self.bounce_request
    user = self.example_user('cordelia')
    email = user.email
    self.login(email)
    server = RemoteZulipServer.objects.get(uuid=self.server_uuid)
    endpoints = [('/json/users/me/apns_device_token', 'apple-tokenaz', RemotePushDeviceToken.APNS), ('/json/users/me/android_gcm_reg_id', 'android-token', RemotePushDeviceToken.GCM)]
    for (endpoint, _, kind) in endpoints:
        broken_token = ('a' * 5000)
        result = self.client_post(endpoint, {'token': broken_token, 'token_kind': kind})
        self.assert_json_error(result, 'Empty    or    invalid    length    token')
        result = self.client_delete(endpoint, {'token': broken_token, 'token_kind': kind})
        self.assert_json_error(result, 'Empty    or    invalid    length    token')
        result = self.client_delete(endpoint, {'token': 'abcd1234', 'token_kind': kind})
        self.assert_json_error(result, 'Token    does    not    exist')
    for (endpoint, token, kind) in endpoints:
        result = self.client_post(endpoint, {'token': token})
        self.assert_json_success(result)
        result = self.client_post(endpoint, {'token': token})
        self.assert_json_success(result)
        tokens = list(RemotePushDeviceToken.objects.filter(user_id=user.id, token=token, server=server))
        self.assertEqual(len(tokens), 1)
        self.assertEqual(tokens[0].token, token)
    tokens = list(RemotePushDeviceToken.objects.filter(user_id=user.id, server=server))
    self.assertEqual(len(tokens), 2)
    for (endpoint, token, kind) in endpoints:
        result = self.client_delete(endpoint, {'token': token, 'token_kind': kind})
        self.assert_json_success(result)
        tokens = list(RemotePushDeviceToken.objects.filter(user_id=user.id, token=token, server=server))
        self.assertEqual(len(tokens), 0)
