Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): Test forwardWebhook #3424

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading