|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { authMockFns, createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + acceptClaim: vi.fn(), |
| 9 | + getClaimDetails: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/lib/billing/enterprise-owner-claim', () => ({ |
| 13 | + acceptEnterpriseOwnerClaim: mocks.acceptClaim, |
| 14 | + getEnterpriseOwnerClaimDetails: mocks.getClaimDetails, |
| 15 | + EnterpriseOwnerClaimEmailMismatchError: class EnterpriseOwnerClaimEmailMismatchError extends Error {}, |
| 16 | + EnterpriseOwnerClaimWorkspaceLimitError: class EnterpriseOwnerClaimWorkspaceLimitError extends Error {}, |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('@/lib/billing/enterprise-provisioning', () => ({ |
| 20 | + EnterpriseProvisioningError: class EnterpriseProvisioningError extends Error {}, |
| 21 | +})) |
| 22 | + |
| 23 | +import { POST } from '@/app/api/enterprise-owner-claims/[id]/accept/route' |
| 24 | +import { GET } from '@/app/api/enterprise-owner-claims/[id]/route' |
| 25 | + |
| 26 | +const claim = { |
| 27 | + id: 'claim-1', |
| 28 | + ownerEmail: 'owner@example.com', |
| 29 | + organizationName: 'Acme', |
| 30 | + organizationId: null, |
| 31 | + provisioningOperationId: null, |
| 32 | + stage: 'owner_acceptance' as const, |
| 33 | + status: 'awaiting_owner' as const, |
| 34 | + error: null, |
| 35 | + expiresAt: '2026-09-04T00:00:00.000Z', |
| 36 | + createdAt: '2026-08-28T00:00:00.000Z', |
| 37 | + updatedAt: '2026-08-28T00:00:00.000Z', |
| 38 | +} |
| 39 | + |
| 40 | +describe('Enterprise owner claim routes', () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks() |
| 43 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 44 | + user: { |
| 45 | + id: 'owner-1', |
| 46 | + name: 'Owner', |
| 47 | + email: 'owner@example.com', |
| 48 | + emailVerified: false, |
| 49 | + }, |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it('lets the invited account review the mailed claim before email verification', async () => { |
| 54 | + mocks.getClaimDetails.mockResolvedValue({ |
| 55 | + ...claim, |
| 56 | + invoiceAmountUsd: 10_000, |
| 57 | + billingInterval: 'year', |
| 58 | + seats: 10, |
| 59 | + invitations: 0, |
| 60 | + workspacePreview: { workspacesToMove: [], createsDefaultWorkspace: true }, |
| 61 | + acceptanceReview: { canAccept: true, reason: null, requiredSeats: 1 }, |
| 62 | + }) |
| 63 | + |
| 64 | + const response = await GET( |
| 65 | + createMockRequest( |
| 66 | + 'GET', |
| 67 | + undefined, |
| 68 | + {}, |
| 69 | + 'http://localhost/api/enterprise-owner-claims/claim-1?token=secure-token' |
| 70 | + ), |
| 71 | + { params: Promise.resolve({ id: 'claim-1' }) } |
| 72 | + ) |
| 73 | + |
| 74 | + expect(response.status).toBe(200) |
| 75 | + expect(mocks.getClaimDetails).toHaveBeenCalledWith({ |
| 76 | + claimId: 'claim-1', |
| 77 | + token: 'secure-token', |
| 78 | + userId: 'owner-1', |
| 79 | + userEmail: 'owner@example.com', |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('lets the acceptance transaction verify an unverified invited account', async () => { |
| 84 | + mocks.acceptClaim.mockResolvedValue({ |
| 85 | + success: true, |
| 86 | + claim, |
| 87 | + redirectPath: '/workspace', |
| 88 | + }) |
| 89 | + |
| 90 | + const response = await POST( |
| 91 | + createMockRequest( |
| 92 | + 'POST', |
| 93 | + { |
| 94 | + token: 'secure-token', |
| 95 | + disclosedWorkspaceIds: [], |
| 96 | + disclosedCreatesDefaultWorkspace: true, |
| 97 | + }, |
| 98 | + {}, |
| 99 | + 'http://localhost/api/enterprise-owner-claims/claim-1/accept' |
| 100 | + ), |
| 101 | + { params: Promise.resolve({ id: 'claim-1' }) } |
| 102 | + ) |
| 103 | + |
| 104 | + expect(response.status).toBe(200) |
| 105 | + expect(mocks.acceptClaim).toHaveBeenCalledWith({ |
| 106 | + claimId: 'claim-1', |
| 107 | + token: 'secure-token', |
| 108 | + userId: 'owner-1', |
| 109 | + userEmail: 'owner@example.com', |
| 110 | + userName: 'Owner', |
| 111 | + disclosedWorkspaceIds: [], |
| 112 | + disclosedCreatesDefaultWorkspace: true, |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments