0

Creating a zapier integration with custom authentication. To test my authentication I make a GET request to an api endpoint. If the request is successful the inputed details are saved throught zapiers own system if the API request is unsuccessful then the auth has failed.

Here is the error I am getting in my authentication.test.js

You must pass in a function/array/object. We got undefined instead.

Here is my test code.

const App = require('../../index')
const appTester = zapier.createAppTester(App);

describe('custom auth', () => {
    it('passes authentication and returns json', async () => {
        const bundle = {
            authData: {
                clientId: '#####################',
                clientSecret: '##########################'
            }
        }
        const response = await appTester(App.authentication.test, bundle);
        expext(response.data).toHaveProperty('id');
    });

    it('fails on bad auth', async () => {
        const bundle = {
            authData: {
                clientId: 'not',
                clientSecret: 'work'
            }
        }   
        try {
            await appTester(App.authentication.test, bundle);       
        } catch (error) {
            expect(error.message).toContain('Error 401 Auth Denied');
            return;
        }
        throw new Error('appTester should have thrown')
    })
});

The error is occuring because of this line

const response = await appTester(App.authentication.test, bundle)

It believes that App.authentication.test is not a function.

But as you can see in my App(index.js)

    config: authentication,
    befores = [],
    afters = [],
  } = require('./authentication');
  const update_client = require('./creates/update_client')
  
  
  
  module.exports = {
    version: require('./package.json').version,
    platformVersion: require('zapier-platform-core').version,
  
    authentication,
  
    beforeRequest: [...befores],
  
    afterResponse: [...afters],
    
    triggers: {},
  
    searches: {},
  
    creates: {
      [update_client.key]: update_client
    },
  
    resources: {},
  };

Authentication is there, and if i go into authentication there is the test function shown below.




function test(z, bundle) {
    z.request({ url: '###################'});
}

const includeAuthData = (request, z, bundle) => {
  if (bundle.authData.clientId && bundle.authData.clientSecret) {
    request.headers['clientId'] = bundle.authData.clientId;
    request.headers['clientSecret'] = bundle.authData.clientSecret;
  }

  return request;
};

const handleBadResponses = (response, z, bundle) => {
  if (response.status === 401) {
    throw new z.errors.Error(
      'The API Key you supplied is incorrect',
      'AuthenticationError',
      response.status
    );
  }

  return response;
};



module.exports = {
  config: {
    type: 'custom', 
    fields: [
        { key: 'clientId', label: 'ID', required: true },
        { key: 'clientSecret', label: 'Secret Key', required: true}                   
    ],
    
  },
  test,
  conectionLabel: '{{json.id}}',  
  befores: [includeAuthData],
  afters: [handleBadResponses]

};

As you can see test is a function in Authentication and it is being exported to app.

This faily identical to the zapier docd zapier custom auth docs

And I deleted it and restarted the project multiple times to check for errors.

Ethandev
  • 1
  • 2

1 Answers1

0

The problem is your test function under App.authentication.test.

In the Zapier provided examples, the syntax used is an arrow function which omits the need to include the return keyword. Your example shows it is a regular function and you don't return anything, which will evaluate to undefined. Try changing the test function to the following:

function test(z, bundle) {
  return z.request({ url: '###################'});
}