2024-05-31 00:40:03 -07:00
|
|
|
import type SuperAgentTest from 'supertest/lib/agent';
|
2023-08-01 08:32:30 -07:00
|
|
|
import { agent as testAgent } from 'supertest';
|
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
|
|
|
|
import { AbstractServer } from '@/AbstractServer';
|
2024-08-07 01:23:44 -07:00
|
|
|
import { ActiveWebhooks } from '@/webhooks/ActiveWebhooks';
|
2023-08-01 08:32:30 -07:00
|
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
2024-08-07 01:23:44 -07:00
|
|
|
import { TestWebhooks } from '@/webhooks/TestWebhooks';
|
|
|
|
import { WaitingWebhooks } from '@/webhooks/WaitingWebhooks';
|
2023-12-13 07:00:51 -08:00
|
|
|
import { WaitingForms } from '@/WaitingForms';
|
2024-08-07 01:23:44 -07:00
|
|
|
import type { IWebhookResponseCallbackData } from '@/webhooks/webhook.types';
|
2023-08-01 08:32:30 -07:00
|
|
|
|
2024-08-05 02:12:25 -07:00
|
|
|
import { mockInstance } from '@test/mocking';
|
2024-07-31 08:45:11 -07:00
|
|
|
import { GlobalConfig } from '@n8n/config';
|
|
|
|
import Container from 'typedi';
|
2023-08-01 08:32:30 -07:00
|
|
|
|
|
|
|
let agent: SuperAgentTest;
|
|
|
|
|
|
|
|
describe('WebhookServer', () => {
|
|
|
|
mockInstance(ExternalHooks);
|
|
|
|
mockInstance(InternalHooks);
|
|
|
|
|
|
|
|
describe('CORS', () => {
|
|
|
|
const corsOrigin = 'https://example.com';
|
2023-12-28 01:04:32 -08:00
|
|
|
const activeWebhooks = mockInstance(ActiveWebhooks);
|
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 () => {
|
|
|
|
const server = new (class extends AbstractServer {
|
|
|
|
testWebhooksEnabled = true;
|
|
|
|
})();
|
|
|
|
await server.start();
|
|
|
|
agent = testAgent(server.app);
|
|
|
|
});
|
|
|
|
|
|
|
|
const tests = [
|
2023-12-28 01:04:32 -08:00
|
|
|
['webhook', activeWebhooks],
|
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;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|