|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { authOAuthUtilsMock, inputValidationMock } from '@sim/testing' |
| 5 | +import { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) |
| 9 | +vi.mock('@/app/api/auth/oauth/utils', () => authOAuthUtilsMock) |
| 10 | + |
| 11 | +import { microsoftTeamsHandler } from '@/lib/webhooks/providers/microsoft-teams' |
| 12 | + |
| 13 | +const WEBHOOK_ID = 'webhook-uuid-1234' |
| 14 | + |
| 15 | +function makeRequest(body: string): NextRequest { |
| 16 | + return new NextRequest('https://app.example.com/api/webhooks/trigger/abc', { |
| 17 | + method: 'POST', |
| 18 | + body, |
| 19 | + headers: { 'Content-Type': 'application/json' }, |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +function makeNotificationBody(clientState?: unknown): string { |
| 24 | + return JSON.stringify({ |
| 25 | + value: [ |
| 26 | + { |
| 27 | + subscriptionId: 'sub-1', |
| 28 | + changeType: 'created', |
| 29 | + resource: 'chats/19:abc@thread.v2/messages/1700000000000', |
| 30 | + resourceData: { id: '1700000000000' }, |
| 31 | + ...(clientState !== undefined ? { clientState } : {}), |
| 32 | + }, |
| 33 | + ], |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +async function runVerifyAuth(rawBody: string, providerConfig: Record<string, unknown>) { |
| 38 | + return microsoftTeamsHandler.verifyAuth!({ |
| 39 | + webhook: { id: WEBHOOK_ID }, |
| 40 | + workflow: {}, |
| 41 | + request: makeRequest(rawBody), |
| 42 | + rawBody, |
| 43 | + requestId: 'test-req', |
| 44 | + providerConfig, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +describe('microsoftTeamsHandler verifyAuth (chat subscription clientState)', () => { |
| 49 | + const chatSubscriptionConfig = { triggerId: 'microsoftteams_chat_subscription' } |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks() |
| 53 | + }) |
| 54 | + |
| 55 | + it('accepts notifications whose clientState matches the webhook id', async () => { |
| 56 | + const res = await runVerifyAuth(makeNotificationBody(WEBHOOK_ID), chatSubscriptionConfig) |
| 57 | + expect(res).toBeNull() |
| 58 | + }) |
| 59 | + |
| 60 | + it('rejects notifications with a forged clientState', async () => { |
| 61 | + const res = await runVerifyAuth(makeNotificationBody('forged'), chatSubscriptionConfig) |
| 62 | + expect(res?.status).toBe(401) |
| 63 | + }) |
| 64 | + |
| 65 | + it('rejects notifications missing clientState', async () => { |
| 66 | + const res = await runVerifyAuth(makeNotificationBody(), chatSubscriptionConfig) |
| 67 | + expect(res?.status).toBe(401) |
| 68 | + }) |
| 69 | + |
| 70 | + it('rejects non-string clientState values', async () => { |
| 71 | + const res = await runVerifyAuth( |
| 72 | + makeNotificationBody({ nested: WEBHOOK_ID }), |
| 73 | + chatSubscriptionConfig |
| 74 | + ) |
| 75 | + expect(res?.status).toBe(401) |
| 76 | + }) |
| 77 | + |
| 78 | + it('rejects payloads without a value array', async () => { |
| 79 | + const res = await runVerifyAuth(JSON.stringify({ hello: 'world' }), chatSubscriptionConfig) |
| 80 | + expect(res?.status).toBe(401) |
| 81 | + }) |
| 82 | + |
| 83 | + it('rejects payloads with an empty value array', async () => { |
| 84 | + const res = await runVerifyAuth(JSON.stringify({ value: [] }), chatSubscriptionConfig) |
| 85 | + expect(res?.status).toBe(401) |
| 86 | + }) |
| 87 | + |
| 88 | + it('rejects unparseable bodies', async () => { |
| 89 | + const res = await runVerifyAuth('not-json', chatSubscriptionConfig) |
| 90 | + expect(res?.status).toBe(401) |
| 91 | + }) |
| 92 | + |
| 93 | + it('rejects batches where any notification has a mismatched clientState', async () => { |
| 94 | + const rawBody = JSON.stringify({ |
| 95 | + value: [ |
| 96 | + { subscriptionId: 'sub-1', resourceData: { id: '1' }, clientState: WEBHOOK_ID }, |
| 97 | + { subscriptionId: 'sub-2', resourceData: { id: '2' }, clientState: 'forged' }, |
| 98 | + ], |
| 99 | + }) |
| 100 | + const res = await runVerifyAuth(rawBody, chatSubscriptionConfig) |
| 101 | + expect(res?.status).toBe(401) |
| 102 | + }) |
| 103 | + |
| 104 | + it('fails closed when the webhook record has no id', async () => { |
| 105 | + const res = await microsoftTeamsHandler.verifyAuth!({ |
| 106 | + webhook: {}, |
| 107 | + workflow: {}, |
| 108 | + request: makeRequest(makeNotificationBody('')), |
| 109 | + rawBody: makeNotificationBody(''), |
| 110 | + requestId: 'test-req', |
| 111 | + providerConfig: chatSubscriptionConfig, |
| 112 | + }) |
| 113 | + expect(res?.status).toBe(401) |
| 114 | + }) |
| 115 | + |
| 116 | + it('does not require clientState for non-subscription trigger types', async () => { |
| 117 | + const res = await runVerifyAuth(JSON.stringify({ type: 'message', text: 'hi' }), {}) |
| 118 | + expect(res).toBeNull() |
| 119 | + }) |
| 120 | +}) |
0 commit comments