|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { dbChainMock, dbChainMockFns, queueTableRows, resetDbChainMock } from '@sim/testing' |
| 6 | +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { mockCredentialExpression, mockEq, mockIsNull, mockLike, tables } = vi.hoisted(() => ({ |
| 9 | + mockCredentialExpression: vi.fn(() => 'webhook.credentialId'), |
| 10 | + mockEq: vi.fn((left: unknown, right: unknown) => ({ left, right })), |
| 11 | + mockIsNull: vi.fn((value: unknown) => ({ isNull: value })), |
| 12 | + mockLike: vi.fn((left: unknown, right: unknown) => ({ left, right })), |
| 13 | + tables: { |
| 14 | + account: { id: 'account.id', accountId: 'account.accountId', providerId: 'account.providerId' }, |
| 15 | + credential: { |
| 16 | + id: 'credential.id', |
| 17 | + accountId: 'credential.accountId', |
| 18 | + providerId: 'credential.providerId', |
| 19 | + type: 'credential.type', |
| 20 | + workspaceId: 'credential.workspaceId', |
| 21 | + }, |
| 22 | + webhook: { |
| 23 | + archivedAt: 'webhook.archivedAt', |
| 24 | + deploymentVersionId: 'webhook.deploymentVersionId', |
| 25 | + isActive: 'webhook.isActive', |
| 26 | + provider: 'webhook.provider', |
| 27 | + providerConfig: 'webhook.providerConfig', |
| 28 | + routingKey: 'webhook.routingKey', |
| 29 | + workflowId: 'webhook.workflowId', |
| 30 | + }, |
| 31 | + workflow: { |
| 32 | + archivedAt: 'workflow.archivedAt', |
| 33 | + id: 'workflow.id', |
| 34 | + workspaceId: 'workflow.workspaceId', |
| 35 | + }, |
| 36 | + workflowDeploymentVersion: { |
| 37 | + id: 'workflowDeploymentVersion.id', |
| 38 | + isActive: 'workflowDeploymentVersion.isActive', |
| 39 | + workflowId: 'workflowDeploymentVersion.workflowId', |
| 40 | + }, |
| 41 | + }, |
| 42 | +})) |
| 43 | + |
| 44 | +vi.mock('@sim/db', () => ({ |
| 45 | + ...dbChainMock, |
| 46 | + ...tables, |
| 47 | + webhookCredentialIdExpression: mockCredentialExpression, |
| 48 | +})) |
| 49 | + |
| 50 | +vi.mock('drizzle-orm', () => ({ |
| 51 | + and: vi.fn((...conditions: unknown[]) => conditions), |
| 52 | + eq: mockEq, |
| 53 | + isNull: mockIsNull, |
| 54 | + like: mockLike, |
| 55 | + or: vi.fn((...conditions: unknown[]) => conditions), |
| 56 | +})) |
| 57 | + |
| 58 | +import { findLegacyTikTokWebhooks } from '@/lib/webhooks/tiktok-legacy-routing' |
| 59 | + |
| 60 | +const ACCOUNT_UUID = '11111111-2222-3333-4444-555555555555' |
| 61 | + |
| 62 | +describe('findLegacyTikTokWebhooks', () => { |
| 63 | + beforeEach(() => { |
| 64 | + vi.clearAllMocks() |
| 65 | + resetDbChainMock() |
| 66 | + }) |
| 67 | + |
| 68 | + afterAll(() => { |
| 69 | + resetDbChainMock() |
| 70 | + }) |
| 71 | + |
| 72 | + it('returns only the exact legacy account match and enforces tenant routing constraints', async () => { |
| 73 | + queueTableRows(tables.account, [ |
| 74 | + { |
| 75 | + accountId: `act.user-${ACCOUNT_UUID}`, |
| 76 | + webhook: { id: 'webhook-1' }, |
| 77 | + workflow: { id: 'workflow-1' }, |
| 78 | + }, |
| 79 | + { |
| 80 | + accountId: `act.user-other-${ACCOUNT_UUID}`, |
| 81 | + webhook: { id: 'webhook-2' }, |
| 82 | + workflow: { id: 'workflow-2' }, |
| 83 | + }, |
| 84 | + ]) |
| 85 | + |
| 86 | + await expect(findLegacyTikTokWebhooks('act.user')).resolves.toEqual([ |
| 87 | + { webhook: { id: 'webhook-1' }, workflow: { id: 'workflow-1' } }, |
| 88 | + ]) |
| 89 | + expect(mockEq).toHaveBeenCalledWith('workflow.workspaceId', 'credential.workspaceId') |
| 90 | + expect(mockEq).toHaveBeenCalledWith('webhook.provider', 'tiktok') |
| 91 | + expect(mockIsNull).toHaveBeenCalledWith('webhook.routingKey') |
| 92 | + expect(mockLike).toHaveBeenCalledWith( |
| 93 | + 'account.accountId', |
| 94 | + 'act.user-________-____-____-____-____________' |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + it('does not query for an empty open ID', async () => { |
| 99 | + await expect(findLegacyTikTokWebhooks('')).resolves.toEqual([]) |
| 100 | + expect(dbChainMockFns.select).not.toHaveBeenCalled() |
| 101 | + }) |
| 102 | +}) |
0 commit comments