2024-09-12 09:07:18 -07:00
|
|
|
import { GlobalConfig } from '@n8n/config';
|
2023-08-01 08:32:30 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { agent as testAgent } from 'supertest';
|
|
|
|
import type SuperAgentTest from 'supertest/lib/agent';
|
|
|
|
import Container from 'typedi';
|
2023-08-01 08:32:30 -07:00
|
|
|
|
2024-08-22 02:10:37 -07:00
|
|
|
import { ExternalHooks } from '@/external-hooks';
|
2024-09-12 09:07:18 -07:00
|
|
|
import { LiveWebhooks } from '@/webhooks/live-webhooks';
|
2024-08-22 02:10:37 -07:00
|
|
|
import { TestWebhooks } from '@/webhooks/test-webhooks';
|
2024-10-17 06:59:53 -07:00
|
|
|
import { WaitingForms } from '@/webhooks/waiting-forms';
|
2024-08-22 02:10:37 -07:00
|
|
|
import { WaitingWebhooks } from '@/webhooks/waiting-webhooks';
|
2024-09-16 04:37:14 -07:00
|
|
|
import { WebhookServer } from '@/webhooks/webhook-server';
|
2024-08-07 01:23:44 -07:00
|
|
|
import type { IWebhookResponseCallbackData } from '@/webhooks/webhook.types';
|
2024-08-05 02:12:25 -07:00
|
|
|
import { mockInstance } from '@test/mocking';
|
2023-08-01 08:32:30 -07:00
|
|
|
|
|
|
|
let agent: SuperAgentTest;
|
|
|
|
|
|
|
|
describe('WebhookServer', () => {
|
|
|
|
mockInstance(ExternalHooks);
|
|
|
|
|
|
|
|
describe('CORS', () => {
|
|
|
|
const corsOrigin = 'https://example.com';
|
2024-08-12 23:49:24 -07:00
|
|
|
const liveWebhooks = mockInstance(LiveWebhooks);
|
2023-08-01 08:32:30 -07:00
|
|
|
const testWebhooks = mockInstance(TestWebhooks);
|
|
|
|
mockInstance(WaitingWebhooks);
|
2023-12-13 07:00:51 -08:00
|
|
|
mockInstance(WaitingForms);
|
2023-08-01 08:32:30 -07:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2024-09-16 04:37:14 -07:00
|
|
|
const server = new WebhookServer();
|
|
|
|
// @ts-expect-error: testWebhooksEnabled is private
|
|
|
|
server.testWebhooksEnabled = true;
|
2023-08-01 08:32:30 -07:00
|
|
|
await server.start();
|
|
|
|
agent = testAgent(server.app);
|
|
|
|
});
|
|
|
|
|
|
|
|
const tests = [
|
2024-08-12 23:49:24 -07:00
|
|
|
['webhook', liveWebhooks],
|
2023-08-01 08:32:30 -07:00
|
|
|
['webhookTest', testWebhooks],
|
2023-12-13 07:00:51 -08:00
|
|
|
// TODO: enable webhookWaiting & waitingForms after CORS support is added
|
2023-08-01 08:32:30 -07:00
|
|
|
// ['webhookWaiting', waitingWebhooks],
|
2023-12-13 07:00:51 -08:00
|
|
|
// ['formWaiting', waitingForms],
|
2023-08-01 08:32:30 -07:00
|
|
|
] as const;
|
|
|
|
|
|
|
|
for (const [key, manager] of tests) {
|
|
|
|
describe(`for ${key}`, () => {
|
|
|
|
it('should handle preflight requests', async () => {
|
2024-07-31 08:45:11 -07:00
|
|
|
const pathPrefix = Container.get(GlobalConfig).endpoints[key];
|
2023-08-01 08:32:30 -07:00
|
|
|
manager.getWebhookMethods.mockResolvedValueOnce(['GET']);
|
|
|
|
|
2023-11-22 08:49:56 -08:00
|
|
|
const response = await agent
|
|
|
|
.options(`/${pathPrefix}/abcd`)
|
|
|
|
.set('origin', corsOrigin)
|
|
|
|
.set('access-control-request-method', 'GET');
|
2023-08-01 08:32:30 -07:00
|
|
|
expect(response.statusCode).toEqual(204);
|
|
|
|
expect(response.body).toEqual({});
|
|
|
|
expect(response.headers['access-control-allow-origin']).toEqual(corsOrigin);
|
|
|
|
expect(response.headers['access-control-allow-methods']).toEqual('OPTIONS, GET');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle regular requests', async () => {
|
2024-07-31 08:45:11 -07:00
|
|
|
const pathPrefix = Container.get(GlobalConfig).endpoints[key];
|
2023-08-01 08:32:30 -07:00
|
|
|
manager.getWebhookMethods.mockResolvedValueOnce(['GET']);
|
2023-08-14 03:38:17 -07:00
|
|
|
manager.executeWebhook.mockResolvedValueOnce(
|
|
|
|
mockResponse({ test: true }, { key: 'value ' }),
|
|
|
|
);
|
2023-08-01 08:32:30 -07:00
|
|
|
|
2023-11-22 08:49:56 -08:00
|
|
|
const response = await agent
|
|
|
|
.get(`/${pathPrefix}/abcd`)
|
|
|
|
.set('origin', corsOrigin)
|
|
|
|
.set('access-control-request-method', 'GET');
|
2023-08-01 08:32:30 -07:00
|
|
|
expect(response.statusCode).toEqual(200);
|
|
|
|
expect(response.body).toEqual({ test: true });
|
|
|
|
expect(response.headers['access-control-allow-origin']).toEqual(corsOrigin);
|
2023-08-14 03:38:17 -07:00
|
|
|
expect(response.headers.key).toEqual('value');
|
2023-08-01 08:32:30 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-14 03:38:17 -07:00
|
|
|
const mockResponse = (data = {}, headers = {}, status = 200) => {
|
2024-08-07 01:23:44 -07:00
|
|
|
const response = mock<IWebhookResponseCallbackData>();
|
2023-08-01 08:32:30 -07:00
|
|
|
response.responseCode = status;
|
|
|
|
response.data = data;
|
2023-08-14 03:38:17 -07:00
|
|
|
response.headers = headers;
|
2023-08-01 08:32:30 -07:00
|
|
|
return response;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|