/* globals describe, it, expect, beforeAll, beforeEach, afterEach */ const zapier = require('zapier-platform-core'); const nock = require('nock'); 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.`, ); } }); afterEach(() => { nock.cleanAll(); }); 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://github.com/login/oauth/authorize?client_id=1234&state=4444&redirect_uri=https%3A%2F%2Fzapier.com%2F&response_type=code', ); }); }); describe('getAccessToken', () => { beforeEach(async () => { nock('https://github.com/login/oauth') .post('/access_token') .reply(200, { access_token: 'someAccessToken' }); }); afterEach(() => { nock.cleanAll(); }); it('returns the expected tokens', async () => { const result = await appTester( App.authentication.oauth2Config.getAccessToken, ); expect(result.access_token).toBe('someAccessToken'); }); }); describe('testAuth', () => { 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, }, }; beforeEach(async () => { nock('https://api.github.com') .get('/user') .reply(200, { json: { login: 'myLogin', }, }); }); afterEach(() => { nock.cleanAll(); }); it('returns the expected info', async () => { const result = await appTester(App.authentication.test, bundle); expect(result.data.json.login).toBe('myLogin'); }); });