Skip to content

Commit

Permalink
fix(tests): Test forwardWebhook (#3424)
Browse files Browse the repository at this point in the history
<!-- Describe the problem and your solution --> 
The test file for `forwardWebhook` was testing `sendAuth` instead. This
fixes that.

<!-- Issue ticket number and link (if applicable) -->

<!-- Testing instructions (skip if just adding/editing providers) -->
  • Loading branch information
nalanj authored Feb 3, 2025
1 parent 6d1f65a commit a0f4a31
Showing 1 changed file with 71 additions and 47 deletions.
118 changes: 71 additions & 47 deletions packages/webhooks/lib/forward.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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 * 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');

const connection: Pick<Connection, 'connection_id' | 'provider_config_key'> = {
connection_id: '1',
provider_config_key: 'providerkey'
const account: DBTeam = {
id: 1,
name: 'test',
uuid: 'whatever',
is_capped: true,
created_at: new Date(),
updated_at: new Date()
};

const webhookSettings: ExternalWebhook = {
Expand All @@ -24,19 +28,21 @@ const webhookSettings: ExternalWebhook = {
updated_at: new Date()
};

const getLogCtx = () => new logPackage.LogContext({ parentId: '1', operation: {} as any }, { dryRun: true, logToConsole: false });
const integration = {
id: 1,
provider: 'hubspot',
unique_key: 'hubspot'
} as IntegrationConfig;

describe('Webhooks: forward notification tests', () => {
beforeEach(() => {
vi.resetAllMocks();
});

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,
Expand All @@ -47,21 +53,20 @@ 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();
});

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,
Expand All @@ -71,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,
Expand All @@ -96,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);
});
});

0 comments on commit a0f4a31

Please sign in to comment.