The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
// oxlint-disable-next-line unicorn/require-module-specifiers -- isolate from the other route.test globals
export {};
const ORIGINAL_FETCH = global.fetch;
const ORIGINAL_WEBHOOK_URL = process.env.AFFILIATE_APPLICATION_WEBHOOK_URL;
const VALID_PAYLOAD = {
name: 'Ada Lovelace',
email: '[email protected]',
partnerType: 'CONTENT_CREATOR',
mainChannel: 'https://youtube.com/@ada',
acceptsTerms: true,
};
function buildRequest({
body = JSON.stringify(VALID_PAYLOAD),
ip = '198.51.100.1',
}: {
body?: string;
ip?: string;
} = {}) {
return new Request('https://example.com/api/affiliate-application', {
method: 'POST',
headers: { 'content-type': 'application/json', 'x-forwarded-for': ip },
body,
});
}
function mockWebhookReply(reply: object) {
const fetchSpy = jest
.fn()
.mockResolvedValue(new Response(JSON.stringify(reply), { status: 200 }));
global.fetch = fetchSpy;
return fetchSpy;
}
async function loadRoute() {
jest.resetModules();
return import('@/app/api/affiliate-application/route');
}
describe('POST /api/affiliate-application', () => {
beforeEach(() => {
process.env.AFFILIATE_APPLICATION_WEBHOOK_URL =
'https://hooks.example/workflow';
jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
jest.restoreAllMocks();
global.fetch = ORIGINAL_FETCH;
process.env.AFFILIATE_APPLICATION_WEBHOOK_URL = ORIGINAL_WEBHOOK_URL;
});
it('forwards the workflow payload without a secret and accepts success:true', async () => {
const fetchSpy = mockWebhookReply({
success: true,
workflowRunId: 'run-id',
});
const { POST } = await loadRoute();
const response = await POST(buildRequest());
expect(response.status).toBe(200);
await expect(response.json()).resolves.toEqual({ success: true });
const [url, init] = fetchSpy.mock.calls[0];
expect(url).toBe('https://hooks.example/workflow');
expect(init.headers).toEqual({ 'Content-Type': 'application/json' });
expect(JSON.parse(init.body)).toEqual({
name: 'Ada Lovelace',
email: '[email protected]',
partnerType: 'CONTENT_CREATOR',
mainChannelUrl: 'https://youtube.com/@ada',
audienceSize: '',
country: '',
promotionPlan: '',
isIntegrationPartner: false,
});
});
it('returns 502 when the workflow answers ok:true instead of success:true', async () => {
mockWebhookReply({ ok: true });
const { POST } = await loadRoute();
const response = await POST(buildRequest({ ip: '198.51.100.2' }));
expect(response.status).toBe(502);
});
it('returns 503 when the webhook URL is not set', async () => {
delete process.env.AFFILIATE_APPLICATION_WEBHOOK_URL;
const fetchSpy = mockWebhookReply({ success: true });
const { POST } = await loadRoute();
const response = await POST(buildRequest({ ip: '198.51.100.3' }));
expect(response.status).toBe(503);
expect(fetchSpy).not.toHaveBeenCalled();
});
it('returns 400 when the promotion plan is over the length limit', async () => {
const fetchSpy = mockWebhookReply({ success: true });
const { POST } = await loadRoute();
const response = await POST(
buildRequest({
body: JSON.stringify({
...VALID_PAYLOAD,
promotionPlan: 'a'.repeat(2001),
}),
ip: '198.51.100.4',
}),
);
expect(response.status).toBe(400);
expect(fetchSpy).not.toHaveBeenCalled();
});
});