From 41b04d4998ca022a6b2fba1f3b4dc005c99710e7 Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Fri, 31 Jan 2025 14:12:41 -0500 Subject: [PATCH 1/2] WIP: Fix up forward webhook tests --- packages/webhooks/lib/forward.unit.test.ts | 38 +++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/webhooks/lib/forward.unit.test.ts b/packages/webhooks/lib/forward.unit.test.ts index 3e0df8072d9..a1faf168f43 100644 --- a/packages/webhooks/lib/forward.unit.test.ts +++ b/packages/webhooks/lib/forward.unit.test.ts @@ -1,11 +1,22 @@ import { vi, expect, describe, it, beforeEach } from 'vitest'; import { sendAuth } from './auth.js'; +import { forwardWebhook } from './forward.js'; import { axiosInstance } from '@nangohq/utils'; -import type { Connection, DBEnvironment, ExternalWebhook } from '@nangohq/types'; +import type { Connection, DBEnvironment, DBTeam, ExternalWebhook, IntegrationConfig } from '@nangohq/types'; import * as logPackage from '@nangohq/logs'; +import { logContextGetter } from '@nangohq/logs'; const spy = vi.spyOn(axiosInstance, 'post'); +const account: DBTeam = { + id: 1, + name: 'test', + uuid: 'whatever', + is_capped: true, + created_at: new Date(), + updated_at: new Date() +}; + const connection: Pick = { connection_id: '1', provider_config_key: 'providerkey' @@ -24,6 +35,12 @@ const webhookSettings: ExternalWebhook = { updated_at: new Date() }; +const integration = { + id: 1, + provider: 'hubspot', + unique_key: 'hubspot' +} as IntegrationConfig; + const getLogCtx = () => new logPackage.LogContext({ parentId: '1', operation: {} as any }, { dryRun: true, logToConsole: false }); describe('Webhooks: forward notification tests', () => { @@ -32,11 +49,9 @@ describe('Webhooks: forward notification tests', () => { }); it('Should not send a forward webhook if the webhook url is not present', async () => { - const logCtx = getLogCtx(); - - await sendAuth({ - connection, - success: true, + await forwardWebhook({ + connectionIds: [], + account, environment: { name: 'dev', id: 1, @@ -47,11 +62,12 @@ describe('Webhooks: forward notification tests', () => { primary_url: '', secondary_url: '' }, - provider: 'hubspot', - type: 'auth', - auth_mode: 'OAUTH2', - operation: 'creation', - logCtx + logContextGetter, + integration, + payload: { some: 'data' }, + webhookOriginalHeaders: { + 'content-type': 'application/json' + } }); expect(spy).not.toHaveBeenCalled(); }); From 9dfeed3ca9f06910faa522e6d72e152c711fde68 Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Fri, 31 Jan 2025 14:22:22 -0500 Subject: [PATCH 2/2] Fix up forward webhook test --- packages/webhooks/lib/forward.unit.test.ts | 88 ++++++++++++---------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/packages/webhooks/lib/forward.unit.test.ts b/packages/webhooks/lib/forward.unit.test.ts index a1faf168f43..89812221b6e 100644 --- a/packages/webhooks/lib/forward.unit.test.ts +++ b/packages/webhooks/lib/forward.unit.test.ts @@ -1,9 +1,7 @@ import { vi, expect, describe, it, beforeEach } from 'vitest'; -import { sendAuth } from './auth.js'; import { forwardWebhook } from './forward.js'; import { axiosInstance } from '@nangohq/utils'; -import type { Connection, DBEnvironment, DBTeam, ExternalWebhook, IntegrationConfig } from '@nangohq/types'; -import * as logPackage from '@nangohq/logs'; +import type { DBEnvironment, DBTeam, ExternalWebhook, IntegrationConfig } from '@nangohq/types'; import { logContextGetter } from '@nangohq/logs'; const spy = vi.spyOn(axiosInstance, 'post'); @@ -17,11 +15,6 @@ const account: DBTeam = { updated_at: new Date() }; -const connection: Pick = { - connection_id: '1', - provider_config_key: 'providerkey' -}; - const webhookSettings: ExternalWebhook = { id: 1, environment_id: 1, @@ -41,8 +34,6 @@ const integration = { unique_key: 'hubspot' } as IntegrationConfig; -const getLogCtx = () => new logPackage.LogContext({ parentId: '1', operation: {} as any }, { dryRun: true, logToConsole: false }); - describe('Webhooks: forward notification tests', () => { beforeEach(() => { vi.resetAllMocks(); @@ -73,11 +64,9 @@ describe('Webhooks: forward notification tests', () => { }); it('Should send a forward webhook if the webhook url is not present but the secondary is', async () => { - const logCtx = getLogCtx(); - - await sendAuth({ - connection, - success: true, + await forwardWebhook({ + connectionIds: [], + account, environment: { name: 'dev', id: 1, @@ -87,21 +76,20 @@ describe('Webhooks: forward notification tests', () => { ...webhookSettings, primary_url: '' }, - provider: 'hubspot', - type: 'auth', - auth_mode: 'OAUTH2', - operation: 'creation', - logCtx + logContextGetter, + integration, + payload: { some: 'data' }, + webhookOriginalHeaders: { + 'content-type': 'application/json' + } }); expect(spy).toHaveBeenCalledTimes(1); }); it('Should send a forwarded webhook if the webhook url is present', async () => { - const logCtx = getLogCtx(); - - await sendAuth({ - connection, - success: true, + await forwardWebhook({ + connectionIds: [], + account, environment: { name: 'dev', id: 1, @@ -112,33 +100,53 @@ describe('Webhooks: forward notification tests', () => { ...webhookSettings, primary_url: '' }, - provider: 'hubspot', - type: 'auth', - auth_mode: 'OAUTH2', - operation: 'creation', - logCtx + logContextGetter, + integration, + payload: { some: 'data' }, + webhookOriginalHeaders: { + 'content-type': 'application/json' + } }); expect(spy).toHaveBeenCalledTimes(1); }); it('Should send a forwarded webhook twice if the webhook url and secondary are present', async () => { - const logCtx = getLogCtx(); - - await sendAuth({ - connection, - success: true, + await forwardWebhook({ + connectionIds: [], + account, environment: { name: 'dev', id: 1, secret_key: 'secret' } as DBEnvironment, webhookSettings: webhookSettings, - provider: 'hubspot', - type: 'auth', - auth_mode: 'OAUTH2', - operation: 'creation', - logCtx + logContextGetter, + integration, + payload: { some: 'data' }, + webhookOriginalHeaders: { + 'content-type': 'application/json' + } }); expect(spy).toHaveBeenCalledTimes(2); }); + + it('Should send a forwarded webhook to each webhook and for each connection if the webhook url and secondary are present', async () => { + await forwardWebhook({ + connectionIds: ['1', '2'], + account, + environment: { + name: 'dev', + id: 1, + secret_key: 'secret' + } as DBEnvironment, + webhookSettings: webhookSettings, + logContextGetter, + integration, + payload: { some: 'data' }, + webhookOriginalHeaders: { + 'content-type': 'application/json' + } + }); + expect(spy).toHaveBeenCalledTimes(4); + }); });