Skip to content

Commit

Permalink
add integration tests for webhook params support
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Aug 25, 2023
1 parent 9cbe0c7 commit 8ce08fa
Showing 1 changed file with 81 additions and 23 deletions.
104 changes: 81 additions & 23 deletions packages/cli/test/integration/webhooks.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { InternalHooks } from '@/InternalHooks';
import { getLogger } from '@/Logger';
import { NodeTypes } from '@/NodeTypes';
import { Push } from '@/push';
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';

import { mockInstance, initActiveWorkflowRunner } from './shared/utils';
import * as testDb from './shared/testDb';
Expand All @@ -22,47 +23,43 @@ describe('Webhook API', () => {

let agent: SuperAgentTest;

beforeAll(async () => {
await testDb.init();
});

afterAll(async () => {
await testDb.terminate();
});

describe('Content-Type support', () => {
beforeAll(async () => {
await testDb.init();

const node = new WebhookTestingNode();
const user = await testDb.createUser();
await testDb.createWorkflow(
{
active: true,
nodes: [
{
name: 'Webhook',
type: node.description.name,
typeVersion: 1,
parameters: {
httpMethod: 'POST',
path: 'abcd',
},
id: '74786112-fb73-4d80-bd9a-43982939b801',
webhookId: '5ccef736-be16-4d10-b7fb-feed7a61ff22',
position: [740, 420],
},
],
},
user,
);
await testDb.createWorkflow(createWebhookWorkflow(node), user);

const nodeTypes = mockInstance(NodeTypes);
nodeTypes.getByName.mockReturnValue(node);
nodeTypes.getByNameAndVersion.mockReturnValue(node);

await initActiveWorkflowRunner();

const server = new (class extends AbstractServer {})();
await server.start();
agent = testAgent(server.app);
});

afterAll(async () => {
await testDb.truncate(['Workflow']);
});

test('should handle JSON', async () => {
const response = await agent.post('/webhook/abcd').send({ test: true });
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({ type: 'application/json', body: { test: true } });
expect(response.body).toEqual({
type: 'application/json',
body: { test: true },
params: {},
});
});

test('should handle XML', async () => {
Expand All @@ -83,6 +80,7 @@ describe('Webhook API', () => {
inner: 'value',
},
},
params: {},
});
});

Expand All @@ -95,6 +93,7 @@ describe('Webhook API', () => {
expect(response.body).toEqual({
type: 'application/x-www-form-urlencoded',
body: { x: '5', y: 'str', z: 'false' },
params: {},
});
});

Expand All @@ -107,6 +106,7 @@ describe('Webhook API', () => {
expect(response.body).toEqual({
type: 'text/plain',
body: '{"key": "value"}',
params: {},
});
});

Expand All @@ -133,6 +133,44 @@ describe('Webhook API', () => {
});
});

describe('Params support', () => {
beforeAll(async () => {
const node = new WebhookTestingNode();
const user = await testDb.createUser();
await testDb.createWorkflow(createWebhookWorkflow(node, ':variable', 'PATCH'), user);

const nodeTypes = mockInstance(NodeTypes);
nodeTypes.getByName.mockReturnValue(node);
nodeTypes.getByNameAndVersion.mockReturnValue(node);

await initActiveWorkflowRunner();

const server = new (class extends AbstractServer {})();
await server.start();
agent = testAgent(server.app);
});

afterAll(async () => {
await testDb.truncate(['Workflow']);
});

test('should handle params', async () => {
const response = await agent
.patch('/webhook/5ccef736-be16-4d10-b7fb-feed7a61ff22/test')
.send({ test: true });
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'application/json',
body: { test: true },
params: {
variable: 'test',
},
});

await agent.post('/webhook/abcd').send({ test: true }).expect(404);
});
});

class WebhookTestingNode implements INodeType {
description: INodeTypeDescription = {
displayName: 'Webhook Testing Node',
Expand Down Expand Up @@ -173,8 +211,28 @@ describe('Webhook API', () => {
webhookResponse: {
type: req.contentType,
body: req.body,
params: req.params,
},
};
}
}

const createWebhookWorkflow = (
node: WebhookTestingNode,
path = 'abcd',
httpMethod = 'POST',
): Partial<WorkflowEntity> => ({
active: true,
nodes: [
{
name: 'Webhook',
type: node.description.name,
typeVersion: 1,
parameters: { httpMethod, path },
id: '74786112-fb73-4d80-bd9a-43982939b801',
webhookId: '5ccef736-be16-4d10-b7fb-feed7a61ff22',
position: [740, 420],
},
],
});
});

0 comments on commit 8ce08fa

Please sign in to comment.