fix(core): Fix routing for waiting webhooks and forms (#14470)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2025-04-08 12:17:01 +02:00
committed by GitHub
parent 6c73d7ed81
commit b5d5b57118
2 changed files with 56 additions and 24 deletions

View File

@@ -193,13 +193,13 @@ export abstract class AbstractServer {
// Register a handler for waiting forms // Register a handler for waiting forms
this.app.all( this.app.all(
`/${this.endpointFormWaiting}/:path/{:suffix}`, `/${this.endpointFormWaiting}/:path{/:suffix}`,
createWebhookHandlerFor(Container.get(WaitingForms)), createWebhookHandlerFor(Container.get(WaitingForms)),
); );
// Register a handler for waiting webhooks // Register a handler for waiting webhooks
this.app.all( this.app.all(
`/${this.endpointWebhookWaiting}/:path/{:suffix}`, `/${this.endpointWebhookWaiting}/:path{/:suffix}`,
createWebhookHandlerFor(Container.get(WaitingWebhooks)), createWebhookHandlerFor(Container.get(WaitingWebhooks)),
); );
} }

View File

@@ -16,14 +16,20 @@ import { mockInstance } from '@test/mocking';
let agent: SuperAgentTest; let agent: SuperAgentTest;
describe('WebhookServer', () => { describe('WebhookServer', () => {
mockInstance(ExternalHooks);
describe('CORS', () => {
const corsOrigin = 'https://example.com';
const liveWebhooks = mockInstance(LiveWebhooks); const liveWebhooks = mockInstance(LiveWebhooks);
const testWebhooks = mockInstance(TestWebhooks); const testWebhooks = mockInstance(TestWebhooks);
mockInstance(WaitingWebhooks); const waitingWebhooks = mockInstance(WaitingWebhooks);
mockInstance(WaitingForms); mockInstance(WaitingForms);
mockInstance(ExternalHooks);
const globalConfig = Container.get(GlobalConfig);
const mockResponse = (data = {}, headers = {}, status = 200) => {
const response = mock<IWebhookResponseCallbackData>();
response.responseCode = status;
response.data = data;
response.headers = headers;
return response;
};
beforeAll(async () => { beforeAll(async () => {
const server = new WebhookServer(); const server = new WebhookServer();
@@ -33,6 +39,8 @@ describe('WebhookServer', () => {
agent = testAgent(server.app); agent = testAgent(server.app);
}); });
describe('CORS', () => {
const corsOrigin = 'https://example.com';
const tests = [ const tests = [
['webhook', liveWebhooks], ['webhook', liveWebhooks],
['webhookTest', testWebhooks], ['webhookTest', testWebhooks],
@@ -43,8 +51,9 @@ describe('WebhookServer', () => {
for (const [key, manager] of tests) { for (const [key, manager] of tests) {
describe(`for ${key}`, () => { describe(`for ${key}`, () => {
const pathPrefix = globalConfig.endpoints[key];
it('should handle preflight requests', async () => { it('should handle preflight requests', async () => {
const pathPrefix = Container.get(GlobalConfig).endpoints[key];
manager.getWebhookMethods.mockResolvedValueOnce(['GET']); manager.getWebhookMethods.mockResolvedValueOnce(['GET']);
const response = await agent const response = await agent
@@ -58,7 +67,6 @@ describe('WebhookServer', () => {
}); });
it('should handle regular requests', async () => { it('should handle regular requests', async () => {
const pathPrefix = Container.get(GlobalConfig).endpoints[key];
manager.getWebhookMethods.mockResolvedValueOnce(['GET']); manager.getWebhookMethods.mockResolvedValueOnce(['GET']);
manager.executeWebhook.mockResolvedValueOnce( manager.executeWebhook.mockResolvedValueOnce(
mockResponse({ test: true }, { key: 'value ' }), mockResponse({ test: true }, { key: 'value ' }),
@@ -75,13 +83,37 @@ describe('WebhookServer', () => {
}); });
}); });
} }
});
const mockResponse = (data = {}, headers = {}, status = 200) => { describe('Routing for Waiting Webhooks', () => {
const response = mock<IWebhookResponseCallbackData>(); const pathPrefix = globalConfig.endpoints.webhookWaiting;
response.responseCode = status;
response.data = data; waitingWebhooks.executeWebhook.mockImplementation(async (req) => {
response.headers = headers; return {
return response; noWebhookResponse: false,
responseCode: 200,
data: {
params: req.params,
},
}; };
}); });
it('should handle URLs without suffix', async () => {
const response = await agent.get(`/${pathPrefix}/12345`);
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
params: { path: '12345' },
});
});
it('should handle URLs with suffix', async () => {
const response = await agent.get(`/${pathPrefix}/12345/suffix`);
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
params: { path: '12345', suffix: 'suffix' },
});
});
});
}); });