|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * POST /api/workspaces refuses a workspace-creation-denied group at two |
| 5 | + * moments: the preflight policy read, and the revocation race the insert |
| 6 | + * detects. Both are the same decision, so both must produce the same body — |
| 7 | + * the preflight one used to answer a bare `{ error }` with no |
| 8 | + * `details.code`, so a client keying off the code saw the capability refusal |
| 9 | + * only in the rarer case. |
| 10 | + */ |
| 11 | +import { createMockRequest } from '@sim/testing' |
| 12 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | + |
| 14 | +const { mockGetSession, mockGetWorkspaceCreationPolicy, mockCreateWorkspace } = vi.hoisted(() => ({ |
| 15 | + mockGetSession: vi.fn(), |
| 16 | + mockGetWorkspaceCreationPolicy: vi.fn(), |
| 17 | + mockCreateWorkspace: vi.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/lib/auth', () => ({ |
| 21 | + auth: { api: { getSession: vi.fn() } }, |
| 22 | + getSession: mockGetSession, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/lib/auth/session-response', () => ({ |
| 26 | + getActiveOrganizationId: () => null, |
| 27 | +})) |
| 28 | + |
| 29 | +vi.mock('@/lib/workspaces/create', () => ({ |
| 30 | + createWorkspace: mockCreateWorkspace, |
| 31 | +})) |
| 32 | + |
| 33 | +vi.mock('@/lib/workspaces/list', () => ({ |
| 34 | + listWorkspacesForViewer: vi.fn(), |
| 35 | +})) |
| 36 | + |
| 37 | +vi.mock('@/lib/posthog/server', () => ({ |
| 38 | + captureServerEvent: vi.fn(), |
| 39 | +})) |
| 40 | + |
| 41 | +vi.mock('@sim/audit', () => ({ |
| 42 | + recordAudit: vi.fn(), |
| 43 | + AuditAction: { WORKSPACE_CREATED: 'workspace.created' }, |
| 44 | + AuditResourceType: { WORKSPACE: 'workspace' }, |
| 45 | +})) |
| 46 | + |
| 47 | +vi.mock('@/lib/workspaces/policy', async () => { |
| 48 | + class WorkspaceCreationCapabilityWithheldError extends Error {} |
| 49 | + class WorkspaceCreationContextChangedError extends Error {} |
| 50 | + return { |
| 51 | + getWorkspaceCreationPolicy: mockGetWorkspaceCreationPolicy, |
| 52 | + WorkspaceCreationCapabilityWithheldError, |
| 53 | + WorkspaceCreationContextChangedError, |
| 54 | + } |
| 55 | +}) |
| 56 | + |
| 57 | +import { WorkspaceCreationCapabilityWithheldError } from '@/lib/workspaces/policy' |
| 58 | +import { POST } from '@/app/api/workspaces/route' |
| 59 | + |
| 60 | +function createRequest() { |
| 61 | + return createMockRequest('POST', { name: 'New workspace' }) |
| 62 | +} |
| 63 | + |
| 64 | +const deniedPolicy = { |
| 65 | + canCreate: false, |
| 66 | + status: 403, |
| 67 | + reason: 'Your permission group does not allow creating workspaces.', |
| 68 | + blockedReasonCode: 'permission-group-denied', |
| 69 | +} |
| 70 | + |
| 71 | +describe('POST /api/workspaces capability refusal', () => { |
| 72 | + beforeEach(() => { |
| 73 | + vi.clearAllMocks() |
| 74 | + mockGetSession.mockResolvedValue({ |
| 75 | + user: { id: 'user-1', name: 'A', email: 'a@example.com' }, |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + it('answers the preflight denial with the capability refusal envelope', async () => { |
| 80 | + mockGetWorkspaceCreationPolicy.mockResolvedValue(deniedPolicy) |
| 81 | + |
| 82 | + const response = await POST(createRequest()) |
| 83 | + |
| 84 | + expect(response.status).toBe(403) |
| 85 | + expect(await response.json()).toMatchObject({ |
| 86 | + details: { code: 'PERMISSION_GROUP_CAPABILITY_BLOCKED' }, |
| 87 | + }) |
| 88 | + expect(mockCreateWorkspace).not.toHaveBeenCalled() |
| 89 | + }) |
| 90 | + |
| 91 | + it('answers the revocation race with the same envelope', async () => { |
| 92 | + mockGetWorkspaceCreationPolicy.mockResolvedValue({ canCreate: true, status: 200 }) |
| 93 | + mockCreateWorkspace.mockRejectedValue(new WorkspaceCreationCapabilityWithheldError()) |
| 94 | + |
| 95 | + const response = await POST(createRequest()) |
| 96 | + |
| 97 | + expect(response.status).toBe(403) |
| 98 | + expect(await response.json()).toMatchObject({ |
| 99 | + details: { code: 'PERMISSION_GROUP_CAPABILITY_BLOCKED' }, |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + /** A non-capability block keeps its own reason and status. */ |
| 104 | + it('leaves an unrelated policy refusal alone', async () => { |
| 105 | + mockGetWorkspaceCreationPolicy.mockResolvedValue({ |
| 106 | + canCreate: false, |
| 107 | + status: 402, |
| 108 | + reason: 'Your organization subscription is inactive.', |
| 109 | + blockedReasonCode: 'organization-subscription-inactive', |
| 110 | + }) |
| 111 | + |
| 112 | + const response = await POST(createRequest()) |
| 113 | + |
| 114 | + expect(response.status).toBe(402) |
| 115 | + const body = await response.json() |
| 116 | + expect(body.error).toBe('Your organization subscription is inactive.') |
| 117 | + expect(body.details).toBeUndefined() |
| 118 | + }) |
| 119 | +}) |
0 commit comments