const assert = require('assert'); const App = require('../index'); const storeData = require('../creates/store_data'); describe('Zapier app definition', () => { it('exposes custom auth, one trigger, and one action', () => { assert.equal(App.authentication.type, 'custom'); assert.ok(App.triggers.new_item); assert.ok(App.creates.store_data); assert.equal(App.beforeRequest.length, 1); }); }); describe('addApiKeyHeader (beforeRequest)', () => { it('injects x-api-key only for the configured baseUrl', () => { const addApiKeyHeader = App.beforeRequest[0]; const bundle = { authData: { baseUrl: 'http://localhost:3000', apiKey: 'key-ada' } }; const own = addApiKeyHeader( { url: 'http://localhost:3000/v1/storage', headers: {} }, null, bundle, ); assert.equal(own.headers['x-api-key'], 'key-ada'); const foreign = addApiKeyHeader({ url: 'https://evil.example.com/f.pdf', headers: {} }, null, bundle); assert.equal(foreign.headers['x-api-key'], undefined); }); }); describe('store_data perform', () => { it('posts metadata JSON to /v1/storage', async () => { const requests = []; const z = { request: async (opts) => { requests.push(opts); return { data: { id: 'item_1' } }; }, }; const bundle = { authData: { baseUrl: 'http://localhost:3000', apiKey: 'key-ada' }, inputData: { title: 'report', note: 'hello' }, }; const result = await storeData.operation.perform(z, bundle); assert.equal(result.id, 'item_1'); assert.equal(requests.length, 1); assert.equal(requests[0].method, 'POST'); assert.equal(requests[0].url, 'http://localhost:3000/v1/storage'); }); it('downloads the mapped file first, then posts it as an attachment', async () => { const requests = []; const z = { request: async (opts) => { requests.push(opts); return opts.raw ? { body: Buffer.from('x') } : { data: { id: 'item_2' } }; }, }; const bundle = { authData: { baseUrl: 'http://localhost:3000', apiKey: 'key-ada' }, inputData: { title: 'with file', file: 'https://example.com/f.pdf', filename: 'f.pdf' }, }; await storeData.operation.perform(z, bundle); assert.equal(requests.length, 2); assert.equal(requests[0].url, 'https://example.com/f.pdf'); assert.equal(requests[0].raw, true); assert.equal(requests[1].url, 'http://localhost:3000/v1/storage'); assert.equal(requests[1].method, 'POST'); }); });