|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, hybridAuthMockFns } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { |
| 8 | + mockExecuteProviderRequest, |
| 9 | + mockRequireBillingAttributionHeader, |
| 10 | + mockCheckWorkspaceAccess, |
| 11 | + mockAuthorizeCredentialUse, |
| 12 | +} = vi.hoisted(() => ({ |
| 13 | + mockExecuteProviderRequest: vi.fn(), |
| 14 | + mockRequireBillingAttributionHeader: vi.fn(), |
| 15 | + mockCheckWorkspaceAccess: vi.fn(), |
| 16 | + mockAuthorizeCredentialUse: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('@/providers', () => ({ |
| 20 | + executeProviderRequest: mockExecuteProviderRequest, |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/lib/billing/core/billing-attribution', () => ({ |
| 24 | + BILLING_ATTRIBUTION_HEADER: 'x-sim-billing-attribution', |
| 25 | + requireBillingAttributionHeader: mockRequireBillingAttributionHeader, |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/workspaces/permissions/utils', () => ({ |
| 29 | + checkWorkspaceAccess: mockCheckWorkspaceAccess, |
| 30 | +})) |
| 31 | + |
| 32 | +vi.mock('@/lib/auth/credential-access', () => ({ |
| 33 | + authorizeCredentialUse: mockAuthorizeCredentialUse, |
| 34 | +})) |
| 35 | + |
| 36 | +vi.mock('@/app/api/auth/oauth/utils', () => ({ |
| 37 | + getServiceAccountToken: vi.fn(), |
| 38 | + refreshTokenIfNeeded: vi.fn(), |
| 39 | + resolveOAuthAccountId: vi.fn(), |
| 40 | +})) |
| 41 | + |
| 42 | +vi.mock('@/ee/access-control/utils/permission-check', () => ({ |
| 43 | + assertPermissionsAllowed: vi.fn(), |
| 44 | + IntegrationNotAllowedError: class IntegrationNotAllowedError extends Error {}, |
| 45 | + ModelNotAllowedError: class ModelNotAllowedError extends Error {}, |
| 46 | + ProviderNotAllowedError: class ProviderNotAllowedError extends Error {}, |
| 47 | +})) |
| 48 | + |
| 49 | +import { POST } from '@/app/api/providers/route' |
| 50 | + |
| 51 | +const BILLING_ATTRIBUTION = { |
| 52 | + actorUserId: 'user-1', |
| 53 | + workspaceId: 'ws-1', |
| 54 | + organizationId: 'org-1', |
| 55 | + billedAccountUserId: 'owner-1', |
| 56 | + billingEntity: { type: 'organization', id: 'org-1' }, |
| 57 | + billingPeriod: { |
| 58 | + start: '2026-07-01T00:00:00.000Z', |
| 59 | + end: '2026-08-01T00:00:00.000Z', |
| 60 | + }, |
| 61 | + payerSubscription: null, |
| 62 | +} |
| 63 | + |
| 64 | +describe('POST /api/providers', () => { |
| 65 | + beforeEach(() => { |
| 66 | + vi.clearAllMocks() |
| 67 | + hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ |
| 68 | + success: true, |
| 69 | + userId: 'user-1', |
| 70 | + authType: 'internal_jwt', |
| 71 | + }) |
| 72 | + mockCheckWorkspaceAccess.mockResolvedValue({ hasAccess: true }) |
| 73 | + mockRequireBillingAttributionHeader.mockReturnValue(BILLING_ATTRIBUTION) |
| 74 | + mockExecuteProviderRequest.mockResolvedValue({ |
| 75 | + content: 'hello', |
| 76 | + model: 'gpt-4o', |
| 77 | + tokens: { input: 1, output: 1, total: 2 }, |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('validates the attribution header and forwards it to executeProviderRequest', async () => { |
| 82 | + const res = await POST( |
| 83 | + createMockRequest( |
| 84 | + 'POST', |
| 85 | + { provider: 'openai', model: 'gpt-4o', workspaceId: 'ws-1' }, |
| 86 | + { 'x-sim-billing-attribution': 'encoded-attribution' } |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + expect(res.status).toBe(200) |
| 91 | + expect(mockRequireBillingAttributionHeader).toHaveBeenCalledWith(expect.anything(), { |
| 92 | + actorUserId: 'user-1', |
| 93 | + workspaceId: 'ws-1', |
| 94 | + }) |
| 95 | + expect(mockExecuteProviderRequest).toHaveBeenCalledWith( |
| 96 | + 'openai', |
| 97 | + expect.objectContaining({ billingAttribution: BILLING_ATTRIBUTION }) |
| 98 | + ) |
| 99 | + }) |
| 100 | + |
| 101 | + it('executes without attribution when the header is absent', async () => { |
| 102 | + const res = await POST( |
| 103 | + createMockRequest('POST', { provider: 'openai', model: 'gpt-4o', workspaceId: 'ws-1' }) |
| 104 | + ) |
| 105 | + |
| 106 | + expect(res.status).toBe(200) |
| 107 | + expect(mockRequireBillingAttributionHeader).not.toHaveBeenCalled() |
| 108 | + expect(mockExecuteProviderRequest).toHaveBeenCalledWith( |
| 109 | + 'openai', |
| 110 | + expect.objectContaining({ billingAttribution: undefined }) |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + it('ignores the attribution header when the body has no workspaceId to validate against', async () => { |
| 115 | + const res = await POST( |
| 116 | + createMockRequest( |
| 117 | + 'POST', |
| 118 | + { provider: 'openai', model: 'gpt-4o' }, |
| 119 | + { 'x-sim-billing-attribution': 'encoded-attribution' } |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + expect(res.status).toBe(200) |
| 124 | + expect(mockRequireBillingAttributionHeader).not.toHaveBeenCalled() |
| 125 | + expect(mockExecuteProviderRequest).toHaveBeenCalledWith( |
| 126 | + 'openai', |
| 127 | + expect.objectContaining({ billingAttribution: undefined }) |
| 128 | + ) |
| 129 | + }) |
| 130 | + |
| 131 | + it('fails when the attribution header does not match the authenticated scope', async () => { |
| 132 | + mockRequireBillingAttributionHeader.mockImplementation(() => { |
| 133 | + throw new Error('Billing attribution header does not match the authenticated request scope') |
| 134 | + }) |
| 135 | + |
| 136 | + const res = await POST( |
| 137 | + createMockRequest( |
| 138 | + 'POST', |
| 139 | + { provider: 'openai', model: 'gpt-4o', workspaceId: 'ws-1' }, |
| 140 | + { 'x-sim-billing-attribution': 'encoded-attribution' } |
| 141 | + ) |
| 142 | + ) |
| 143 | + |
| 144 | + expect(res.status).toBe(500) |
| 145 | + expect(mockExecuteProviderRequest).not.toHaveBeenCalled() |
| 146 | + }) |
| 147 | +}) |
0 commit comments