|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + assertWebhookExecutionPrincipal, |
| 7 | + createWebhookExecutionPrincipal, |
| 8 | +} from '@/lib/webhooks/execution-principal' |
| 9 | + |
| 10 | +describe('webhook execution principals', () => { |
| 11 | + it('represents a generic webhook as its billing actor', () => { |
| 12 | + const principal = createWebhookExecutionPrincipal({ |
| 13 | + webhookId: 'webhook-1', |
| 14 | + workflowId: 'workflow-1', |
| 15 | + workspaceId: 'workspace-1', |
| 16 | + provider: 'generic', |
| 17 | + subject: { kind: 'sim_user', userId: 'billing-actor-1' }, |
| 18 | + }) |
| 19 | + |
| 20 | + expect(principal.subject).toEqual({ kind: 'sim_user', userId: 'billing-actor-1' }) |
| 21 | + expect(() => |
| 22 | + assertWebhookExecutionPrincipal(principal, { |
| 23 | + webhookId: 'webhook-1', |
| 24 | + workflowId: 'workflow-1', |
| 25 | + workspaceId: 'workspace-1', |
| 26 | + provider: 'generic', |
| 27 | + userId: 'billing-actor-1', |
| 28 | + }) |
| 29 | + ).not.toThrow() |
| 30 | + }) |
| 31 | + |
| 32 | + it('rejects a generic webhook whose subject is not its billing actor', () => { |
| 33 | + expect(() => |
| 34 | + createWebhookExecutionPrincipal({ |
| 35 | + webhookId: 'webhook-1', |
| 36 | + workflowId: 'workflow-1', |
| 37 | + workspaceId: 'workspace-1', |
| 38 | + provider: 'generic', |
| 39 | + subject: { |
| 40 | + kind: 'external_user', |
| 41 | + provider: 'generic', |
| 42 | + tenantId: 'tenant-1', |
| 43 | + subjectId: 'subject-1', |
| 44 | + }, |
| 45 | + }) |
| 46 | + ).toThrow('Generic webhook execution requires a Sim user billing actor') |
| 47 | + }) |
| 48 | + |
| 49 | + it('rejects a serialized Sim subject that disagrees with the admitted billing actor', () => { |
| 50 | + const principal = createWebhookExecutionPrincipal({ |
| 51 | + webhookId: 'webhook-1', |
| 52 | + workflowId: 'workflow-1', |
| 53 | + workspaceId: 'workspace-1', |
| 54 | + provider: 'generic', |
| 55 | + subject: { kind: 'sim_user', userId: 'billing-actor-1' }, |
| 56 | + }) |
| 57 | + |
| 58 | + expect(() => |
| 59 | + assertWebhookExecutionPrincipal(principal, { |
| 60 | + webhookId: 'webhook-1', |
| 61 | + workflowId: 'workflow-1', |
| 62 | + workspaceId: 'workspace-1', |
| 63 | + provider: 'generic', |
| 64 | + userId: 'different-user', |
| 65 | + }) |
| 66 | + ).toThrow('Webhook job principal does not match its canonical execution scope') |
| 67 | + }) |
| 68 | +}) |
0 commit comments