Files
twenty/packages/twenty-server/test/integration/metadata/suites/developers/webhooks.integration-spec.ts
T
martmull 67fa0cc93c Fix flaky webhook delivery integration test (fixed sleep -> poll) (#22699)
# Context

Part of a CI flakiness sweep. `webhooks.integration-spec.ts` › "should
deliver webhook successfully when safe mode is disabled" intermittently
fails with `expect(receiver.receivedPayloads.length).toBe(1) ...
Received: 0` on unrelated PRs (example: run 28959576750, shard 3).

# Root cause

The test asserted delivery after a fixed 100ms sleep. Delivery actually
crosses: a fire-and-forget `EventEmitter2.emit` (the GraphQL response
returns before the job is even enqueued) → BullMQ hop 1
(`CallWebhookJobsJob`, which also recomputes the just-invalidated
`flatWebhookMaps` cache) → BullMQ hop 2 (`CallWebhookJob`) → HTTP POST
to the in-test receiver. Two Redis round trips plus a cache rebuild
routinely exceed 100ms on loaded CI runners. The global
`waitForAllJobsToFinish` only runs in `afterEach`, after the assertion.

# Fix

- Poll the receiver with the existing `expectEventually` helper (30s
deadline, 100ms interval) instead of sleeping, and give the test an
explicit 60s timeout (suite default is 20s). Worst case the test fails
slower; it can no longer fail while delivery is merely in flight.
- Bonus bug found during adversarial review of this fix: the `finally`
cleanup deleted config key `HTTP_TOOL_SAFE_MODE_ENABLED` while the test
creates `OUTBOUND_HTTP_SAFE_MODE_ENABLED`, silently leaving outbound
safe mode disabled in the DB for every suite that runs after this one.
Fixed the key.

Duplicate-delivery risk was checked: `CallWebhookJob.handle` never
throws (errors swallowed), so `retryLimit: 3` can't produce a second
payload that would break `toBe(1)`.

Test-only change, 1 file, +15/-9.

---
_Generated by [Claude
Code](https://claude.ai/code/session_01AtD2wWm3EthV6t3Hs31QyB)_

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/22699?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
2026-07-09 10:58:07 +02:00

356 lines
12 KiB
TypeScript

import { gql } from 'graphql-tag';
import { makeGraphqlAPIRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';
import {
createWebhook,
createWebhookReceiver,
deleteWebhook,
getWebhook,
getWebhooks,
updateWebhook,
} from 'test/integration/metadata/suites/utils/webhook-test.util';
import { makeAdminPanelAPIRequest } from 'test/integration/twenty-config/utils/make-admin-panel-api-request.util';
import { expectEventually } from 'test/integration/utils/expect-eventually.util';
import { v4 as uuidv4 } from 'uuid';
import { type UpdateWebhookInput } from 'src/engine/metadata-modules/webhook/dtos/update-webhook.input';
const CREATE_CONFIG_VARIABLE_MUTATION = gql`
mutation CreateDatabaseConfigVariable($key: String!, $value: JSON!) {
createDatabaseConfigVariable(key: $key, value: $value)
}
`;
const GET_CONFIG_VARIABLE_QUERY = gql`
query GetDatabaseConfigVariable($key: String!) {
getDatabaseConfigVariable(key: $key) {
value
source
}
}
`;
const DELETE_CONFIG_VARIABLE_MUTATION = gql`
mutation DeleteDatabaseConfigVariable($key: String!) {
deleteDatabaseConfigVariable(key: $key)
}
`;
const DESTROY_PERSON_MUTATION = gql`
mutation DestroyPerson($id: ID!) {
destroyPerson(id: $id) {
id
}
}
`;
const CREATE_PERSON_MUTATION = gql`
mutation CreatePerson($data: PersonCreateInput!) {
createPerson(data: $data) {
id
name {
firstName
lastName
}
}
}
`;
describe('webhooksResolver (e2e)', () => {
let createdWebhookId: string | undefined;
let createdPersonId: string | undefined;
afterEach(async () => {
if (createdPersonId) {
await makeGraphqlAPIRequest({
query: DESTROY_PERSON_MUTATION,
variables: { id: createdPersonId },
}).catch(() => {});
createdPersonId = undefined;
}
if (createdWebhookId) {
await deleteWebhook(createdWebhookId).catch(() => {});
createdWebhookId = undefined;
}
});
describe('webhooks query', () => {
it('should find many webhooks', async () => {
const response = await getWebhooks();
expect(response.status).toBe(200);
expect(response.body.data).toBeDefined();
expect(response.body.errors).toBeUndefined();
expect(response.body.data.webhooks).toBeDefined();
expect(Array.isArray(response.body.data.webhooks)).toBe(true);
});
});
describe('createWebhook mutation', () => {
it('should create a webhook successfully', async () => {
const webhookInput = {
targetUrl: 'https://example.com/webhook',
operations: ['person.created', 'company.updated'],
description: 'Test webhook',
secret: 'test-secret',
};
const response = await createWebhook(webhookInput);
expect(response.status).toBe(200);
expect(response.body.data).toBeDefined();
expect(response.body.errors).toBeUndefined();
const createdWebhookData = response.body.data.createWebhook;
expect(createdWebhookData).toBeDefined();
expect(createdWebhookData.id).toBeDefined();
expect(createdWebhookData.targetUrl).toBe(webhookInput.targetUrl);
expect(createdWebhookData.operations).toEqual(webhookInput.operations);
expect(createdWebhookData.description).toBe(webhookInput.description);
expect(createdWebhookData.secret).toBe(webhookInput.secret);
createdWebhookId = createdWebhookData.id;
});
it('should fail to create webhook with invalid URL', async () => {
const webhookInput = {
targetUrl: 'invalid-url',
operations: ['person.created'],
description: 'Test webhook',
secret: 'test-secret',
};
const response = await createWebhook(webhookInput);
expect(response.status).toBe(200);
expect(response.body.errors).toBeDefined();
expect(response.body.errors.length).toBeGreaterThan(0);
});
});
describe('updateWebhook mutation', () => {
it('should update a webhook successfully', async () => {
const createResponse = await createWebhook({
targetUrl: 'https://example.com/webhook',
operations: ['person.created'],
description: 'Test webhook',
secret: 'test-secret',
});
const createdWebhookData = createResponse.body.data.createWebhook;
createdWebhookId = createdWebhookData.id;
const updateInput: UpdateWebhookInput = {
id: createdWebhookData.id,
update: {
targetUrl: 'https://updated.com/webhook',
operations: ['person.updated', 'company.created'],
description: 'Updated webhook',
secret: 'updated-secret',
},
};
const updateResponse = await updateWebhook(updateInput);
expect(updateResponse.status).toBe(200);
expect(updateResponse.body.data).toBeDefined();
expect(updateResponse.body.errors).toBeUndefined();
const updatedWebhookData = updateResponse.body.data.updateWebhook;
expect(updatedWebhookData.id).toBe(createdWebhookData.id);
expect(updatedWebhookData.targetUrl).toBe(updateInput.update.targetUrl);
expect(updatedWebhookData.operations).toEqual(
updateInput.update.operations,
);
expect(updatedWebhookData.description).toBe(
updateInput.update.description,
);
expect(updatedWebhookData.secret).toBe(updateInput.update.secret);
});
});
describe('webhook query', () => {
it('should find a specific webhook', async () => {
const createResponse = await createWebhook({
targetUrl: 'https://example.com/webhook',
operations: ['person.created'],
description: 'Test webhook',
secret: 'test-secret',
});
const createdWebhookData = createResponse.body.data.createWebhook;
createdWebhookId = createdWebhookData.id;
const queryResponse = await getWebhook(createdWebhookData.id);
expect(queryResponse.status).toBe(200);
expect(queryResponse.body.data).toBeDefined();
expect(queryResponse.body.errors).toBeUndefined();
const webhookData = queryResponse.body.data.webhook;
expect(webhookData).toBeDefined();
expect(webhookData.id).toBe(createdWebhookData.id);
expect(webhookData.targetUrl).toBe(createdWebhookData.targetUrl);
expect(webhookData.operations).toEqual(createdWebhookData.operations);
expect(webhookData.description).toBe(createdWebhookData.description);
expect(webhookData.secret).toBe(createdWebhookData.secret);
});
});
describe('deleteWebhook mutation', () => {
it('should delete a webhook successfully', async () => {
const createResponse = await createWebhook({
targetUrl: 'https://example.com/webhook',
operations: ['person.created'],
description: 'Test webhook',
secret: 'test-secret',
});
const createdWebhookData = createResponse.body.data.createWebhook;
const deleteResponse = await deleteWebhook(createdWebhookData.id);
expect(deleteResponse.status).toBe(200);
expect(deleteResponse.body.data).toBeDefined();
expect(deleteResponse.body.errors).toBeUndefined();
const queryResponse = await getWebhook(createdWebhookData.id);
expect(queryResponse.status).toBe(200);
expect(queryResponse.body.data.webhook).toBeNull();
createdWebhookId = undefined;
});
});
describe('webhook delivery', () => {
const WEBHOOK_RECEIVER_PORT = 4317;
it('should block delivery to private IP when safe mode is enabled (SSRF protection)', async () => {
const receiver = await createWebhookReceiver(WEBHOOK_RECEIVER_PORT);
try {
const createWebhookResponse = await createWebhook({
targetUrl: `http://127.0.0.1:${WEBHOOK_RECEIVER_PORT}/webhook`,
operations: ['person.created'],
description: 'SSRF test webhook',
secret: 'test-secret',
});
expect(createWebhookResponse.body.errors).toBeUndefined();
createdWebhookId = createWebhookResponse.body.data.createWebhook.id;
const testId = uuidv4().slice(0, 8);
const createPersonResponse = await makeGraphqlAPIRequest({
query: CREATE_PERSON_MUTATION,
variables: {
data: {
name: {
firstName: 'SSRFTest',
lastName: `User-${testId}`,
},
},
},
});
expect(createPersonResponse.status).toBe(200);
expect(createPersonResponse.body.errors).toBeUndefined();
createdPersonId = createPersonResponse.body.data.createPerson.id;
jest.useRealTimers();
await new Promise((resolve) => setTimeout(resolve, 100));
jest.useFakeTimers();
expect(receiver.receivedPayloads.length).toBe(0);
} finally {
await receiver.close();
}
});
it('should deliver webhook successfully when safe mode is disabled', async () => {
jest.useRealTimers();
const receiver = await createWebhookReceiver(WEBHOOK_RECEIVER_PORT);
try {
const createConfigResponse = await makeAdminPanelAPIRequest({
query: CREATE_CONFIG_VARIABLE_MUTATION,
variables: {
key: 'OUTBOUND_HTTP_SAFE_MODE_ENABLED',
value: false,
},
});
expect(createConfigResponse.body.errors).toBeUndefined();
expect(
createConfigResponse.body.data.createDatabaseConfigVariable,
).toBe(true);
const verifyConfig = await makeAdminPanelAPIRequest({
query: GET_CONFIG_VARIABLE_QUERY,
variables: { key: 'OUTBOUND_HTTP_SAFE_MODE_ENABLED' },
});
expect(verifyConfig.body.data.getDatabaseConfigVariable.value).toBe(
false,
);
expect(verifyConfig.body.data.getDatabaseConfigVariable.source).toBe(
'DATABASE',
);
const createWebhookResponse = await createWebhook({
targetUrl: `http://127.0.0.1:${WEBHOOK_RECEIVER_PORT}/webhook`,
operations: ['person.created'],
description: 'Delivery test webhook',
secret: 'test-secret',
});
expect(createWebhookResponse.body.errors).toBeUndefined();
createdWebhookId = createWebhookResponse.body.data.createWebhook.id;
const testId = uuidv4().slice(0, 8);
const createPersonResponse = await makeGraphqlAPIRequest({
query: CREATE_PERSON_MUTATION,
variables: {
data: {
name: {
firstName: 'WebhookDelivery',
lastName: `Test-${testId}`,
},
},
},
});
expect(createPersonResponse.status).toBe(200);
expect(createPersonResponse.body.errors).toBeUndefined();
createdPersonId = createPersonResponse.body.data.createPerson.id;
// Delivery crosses two BullMQ hops behind a fire-and-forget event
// emit, so poll the receiver instead of guessing at the latency.
await expectEventually(
() => {
expect(receiver.receivedPayloads.length).toBe(1);
expect(receiver.receivedPayloads[0]).toMatchObject({
targetUrl: `http://127.0.0.1:${WEBHOOK_RECEIVER_PORT}/webhook`,
eventName: 'person.created',
});
},
{ timeoutMs: 30_000, intervalMs: 100 },
);
} finally {
await receiver.close();
await makeAdminPanelAPIRequest({
query: DELETE_CONFIG_VARIABLE_MUTATION,
variables: { key: 'OUTBOUND_HTTP_SAFE_MODE_ENABLED' },
}).catch(() => {});
jest.useFakeTimers();
}
}, 60_000);
});
});