I’m starting to learn JavaScript while working on a Zapier app. I have an auth function for testing that fails to report an error when invalid credentials are provided.
My test configuration:
const should = require('should');
const zapier = require('zapier-platform-core');
const MyApplication = require('../../index');
const appTester = zapier.createAppTester(MyApplication);
describe('Authentication Validation', () => {
zapier.tools.env.inject();
it('should retrieve user information', done => {
const requestConfig = {
authData: { apiKey: process.env.AUTH_KEY },
inputData: {}
};
appTester(MyApplication.triggers['retrieveUsers'].operation.perform, requestConfig)
.then(response => {
response.includes('id');
done();
})
.catch(response);
});
});
Expected successful response:
{"id":2950,"groupName":"Active Users Group","totalUsers":6}
Failure response for authentication issue:
{"STATUS":"ERROR","MESSAGE":"Invalid authentication token"}
My trigger function implementation:
const { replaceVariables } = require('../helpers');
const retrieveUserInfo = (z, bundle) => {
let apiUrl = 'https://api.example.com/users?method=getUsers&apiKey={{apiKey}}';
apiUrl = replaceVariables(apiUrl, bundle);
const request = z.request({ url: apiUrl });
return request.then(response => {
response.throwForStatus();
return z.JSON.parse(response.content);
});
};
module.exports = {
key: 'retrieveUsers',
noun: 'Users',
display: {
label: 'Retrieve Users',
description: 'Fetches a list of users when triggered.',
hidden: true,
important: false
},
operation: {
inputFields: [
{
key: 'userGroup',
label: 'User Group',
type: 'string',
required: false
}
],
outputFields: [
{
key: 'totalUsers',
type: 'string'
},
{
key: 'id',
type: 'string',
label: 'User ID'
},
{
key: 'groupName',
type: 'string',
label: 'Group Name'
}
],
perform: retrieveUserInfo,
sample: { totalUsers: 160, id: 18390, groupName: 'Active Users Group' }
}
};
When validating the authentication on Zapier’s site, I need the auth process to fail properly and display the error MESSAGE. What steps should I take?