master-zapier-plan-draft/vendor/zapier-platform/example-apps/oauth2/test/authentication.test.js
George Lambert b4150c8250 Milestone 0: import zappier billing, Verae middleware, and Zapier research
Compose-ready workspace: packages/zappier (rate card, portal, Stripe),
packages/verae-zapier-middleware (timestamp + NATS), packages/verae-zapier
(CLI app), vendor/zapier-platform, and research/zapier vendor corpus.

Gate 0 structure checks pass. Product code and research are not yet wired.
2026-09-09 02:37:36 -04:00

118 lines
3.5 KiB
JavaScript

/* globals describe, it, expect, beforeAll */
const zapier = require('zapier-platform-core');
zapier.tools.env.inject(); // read from the .env file
const App = require('../index');
const appTester = zapier.createAppTester(App);
// Only here so the tests out of the box.
// You should create a `.env` file and populate it with the necessarily configuration
// it should look like:
/*
CLIENT_ID=1234
CLIENT_SECRET=asdf
*/
// then you can delete the following 2 lines
process.env.CLIENT_ID = process.env.CLIENT_ID || '1234';
process.env.CLIENT_SECRET = process.env.CLIENT_SECRET || 'asdf';
describe('oauth2 app', () => {
beforeAll(() => {
// It's a good idea to store your Client ID and Secret in the environment rather than in code.
if (!(process.env.CLIENT_ID && process.env.CLIENT_SECRET)) {
throw new Error(
`Before running the tests, make sure CLIENT_ID and CLIENT_SECRET are available in the environment.`,
);
}
});
it('generates an authorize URL', async () => {
const bundle = {
// In production, these will be generated by Zapier and set automatically
inputData: {
state: '4444',
redirect_uri: 'https://zapier.com/',
},
environment: {
CLIENT_ID: process.env.CLIENT_ID,
CLIENT_SECRET: process.env.CLIENT_SECRET,
},
};
const authorizeUrl = await appTester(
App.authentication.oauth2Config.authorizeUrl,
bundle,
);
expect(authorizeUrl).toBe(
'https://auth-json-server.zapier-staging.com/oauth/authorize?client_id=1234&state=4444&redirect_uri=https%3A%2F%2Fzapier.com%2F&response_type=code',
);
});
it('can fetch an access token', async () => {
const bundle = {
inputData: {
// In production, Zapier passes along whatever code your API set in the query params when it redirects
// the user's browser to the `redirect_uri`
code: 'one_time_code',
},
environment: {
CLIENT_ID: process.env.CLIENT_ID,
CLIENT_SECRET: process.env.CLIENT_SECRET,
},
cleanedRequest: {
querystring: {
accountDomain: 'test-account',
code: 'one_time_code',
},
},
rawRequest: {
querystring: '?accountDomain=test-account&code=one_time_code',
},
};
const result = await appTester(
App.authentication.oauth2Config.getAccessToken,
bundle,
);
expect(result.access_token).toBe('a_token');
expect(result.refresh_token).toBe('a_refresh_token');
});
it('can refresh the access token', async () => {
const bundle = {
// In production, Zapier provides these. For testing, we have hard-coded them.
// When writing tests for your own app, you should consider exporting them and doing process.env.MY_ACCESS_TOKEN
authData: {
access_token: 'a_token',
refresh_token: 'a_refresh_token',
},
environment: {
CLIENT_ID: process.env.CLIENT_ID,
CLIENT_SECRET: process.env.CLIENT_SECRET,
},
};
const result = await appTester(
App.authentication.oauth2Config.refreshAccessToken,
bundle,
);
expect(result.access_token).toBe('a_token');
});
it('includes the access token in future requests', async () => {
const bundle = {
authData: {
access_token: 'a_token',
refresh_token: 'a_refresh_token',
},
};
const response = await appTester(App.authentication.test, bundle);
expect(response.data).toHaveProperty('username');
expect(response.data.username).toBe('Bret');
});
});