|
| 1 | +/** |
| 2 | + * Tests for the workflow deployed-state API route. |
| 3 | + * Covers internal-JWT authorization (acting user required + workspace read |
| 4 | + * permission) and the unchanged session path. |
| 5 | + * |
| 6 | + * @vitest-environment node |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + workflowAuthzMockFns, |
| 11 | + workflowsPersistenceUtilsMock, |
| 12 | + workflowsPersistenceUtilsMockFns, |
| 13 | + workflowsUtilsMock, |
| 14 | + workflowsUtilsMockFns, |
| 15 | +} from '@sim/testing' |
| 16 | +import { NextRequest } from 'next/server' |
| 17 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 18 | + |
| 19 | +const { mockVerifyInternalToken } = vi.hoisted(() => ({ |
| 20 | + mockVerifyInternalToken: vi.fn(), |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/lib/auth/internal', () => ({ |
| 24 | + verifyInternalToken: mockVerifyInternalToken, |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('@/lib/workflows/persistence/utils', () => workflowsPersistenceUtilsMock) |
| 28 | + |
| 29 | +vi.mock('@/lib/workflows/utils', () => workflowsUtilsMock) |
| 30 | + |
| 31 | +import { GET } from './route' |
| 32 | + |
| 33 | +const mockAuthorizeWorkflowByWorkspacePermission = |
| 34 | + workflowAuthzMockFns.mockAuthorizeWorkflowByWorkspacePermission |
| 35 | +const mockLoadDeployedWorkflowState = workflowsPersistenceUtilsMockFns.mockLoadDeployedWorkflowState |
| 36 | +const mockValidateWorkflowPermissions = workflowsUtilsMockFns.mockValidateWorkflowPermissions |
| 37 | + |
| 38 | +const DEPLOYED_STATE = { |
| 39 | + blocks: { 'block-1': { id: 'block-1', type: 'starter' } }, |
| 40 | + edges: [], |
| 41 | + loops: {}, |
| 42 | + parallels: {}, |
| 43 | + variables: {}, |
| 44 | +} |
| 45 | + |
| 46 | +function createRequest(options?: { bearerToken?: string }) { |
| 47 | + const headers: Record<string, string> = {} |
| 48 | + if (options?.bearerToken) { |
| 49 | + headers.Authorization = `Bearer ${options.bearerToken}` |
| 50 | + } |
| 51 | + return new NextRequest('http://localhost:3000/api/workflows/workflow-123/deployed', { headers }) |
| 52 | +} |
| 53 | + |
| 54 | +const routeParams = () => ({ params: Promise.resolve({ id: 'workflow-123' }) }) |
| 55 | + |
| 56 | +describe('GET /api/workflows/[id]/deployed', () => { |
| 57 | + beforeEach(() => { |
| 58 | + vi.clearAllMocks() |
| 59 | + mockVerifyInternalToken.mockResolvedValue({ valid: false }) |
| 60 | + mockLoadDeployedWorkflowState.mockResolvedValue(DEPLOYED_STATE) |
| 61 | + }) |
| 62 | + |
| 63 | + describe('internal JWT path', () => { |
| 64 | + it('returns 200 when the token carries a user with read permission', async () => { |
| 65 | + mockVerifyInternalToken.mockResolvedValue({ valid: true, userId: 'user-123' }) |
| 66 | + mockAuthorizeWorkflowByWorkspacePermission.mockResolvedValue({ |
| 67 | + allowed: true, |
| 68 | + status: 200, |
| 69 | + workflow: { id: 'workflow-123', workspaceId: 'workspace-456' }, |
| 70 | + workspacePermission: 'read', |
| 71 | + }) |
| 72 | + |
| 73 | + const response = await GET(createRequest({ bearerToken: 'internal-token' }), routeParams()) |
| 74 | + |
| 75 | + expect(response.status).toBe(200) |
| 76 | + const data = await response.json() |
| 77 | + expect(data.deployedState).toEqual(DEPLOYED_STATE) |
| 78 | + expect(mockAuthorizeWorkflowByWorkspacePermission).toHaveBeenCalledWith({ |
| 79 | + workflowId: 'workflow-123', |
| 80 | + userId: 'user-123', |
| 81 | + action: 'read', |
| 82 | + }) |
| 83 | + expect(mockValidateWorkflowPermissions).not.toHaveBeenCalled() |
| 84 | + }) |
| 85 | + |
| 86 | + it('returns 403 when the acting user lacks read permission', async () => { |
| 87 | + mockVerifyInternalToken.mockResolvedValue({ valid: true, userId: 'user-123' }) |
| 88 | + mockAuthorizeWorkflowByWorkspacePermission.mockResolvedValue({ |
| 89 | + allowed: false, |
| 90 | + status: 403, |
| 91 | + message: 'Unauthorized: Access denied to read this workflow', |
| 92 | + workflow: { id: 'workflow-123', workspaceId: 'workspace-456' }, |
| 93 | + workspacePermission: null, |
| 94 | + }) |
| 95 | + |
| 96 | + const response = await GET(createRequest({ bearerToken: 'internal-token' }), routeParams()) |
| 97 | + |
| 98 | + expect(response.status).toBe(403) |
| 99 | + const data = await response.json() |
| 100 | + expect(data.error).toBe('Unauthorized: Access denied to read this workflow') |
| 101 | + expect(mockLoadDeployedWorkflowState).not.toHaveBeenCalled() |
| 102 | + }) |
| 103 | + |
| 104 | + it('returns 403 when the token carries no acting user (fail closed)', async () => { |
| 105 | + mockVerifyInternalToken.mockResolvedValue({ valid: true, userId: undefined }) |
| 106 | + |
| 107 | + const response = await GET(createRequest({ bearerToken: 'internal-token' }), routeParams()) |
| 108 | + |
| 109 | + expect(response.status).toBe(403) |
| 110 | + const data = await response.json() |
| 111 | + expect(data.error).toBe('Forbidden') |
| 112 | + expect(mockAuthorizeWorkflowByWorkspacePermission).not.toHaveBeenCalled() |
| 113 | + expect(mockLoadDeployedWorkflowState).not.toHaveBeenCalled() |
| 114 | + }) |
| 115 | + |
| 116 | + it('returns 404 when the workflow does not exist', async () => { |
| 117 | + mockVerifyInternalToken.mockResolvedValue({ valid: true, userId: 'user-123' }) |
| 118 | + mockAuthorizeWorkflowByWorkspacePermission.mockResolvedValue({ |
| 119 | + allowed: false, |
| 120 | + status: 404, |
| 121 | + message: 'Workflow not found', |
| 122 | + workflow: null, |
| 123 | + workspacePermission: null, |
| 124 | + }) |
| 125 | + |
| 126 | + const response = await GET(createRequest({ bearerToken: 'internal-token' }), routeParams()) |
| 127 | + |
| 128 | + expect(response.status).toBe(404) |
| 129 | + const data = await response.json() |
| 130 | + expect(data.error).toBe('Workflow not found') |
| 131 | + expect(mockLoadDeployedWorkflowState).not.toHaveBeenCalled() |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + describe('session path', () => { |
| 136 | + it('returns 200 when session permissions validate', async () => { |
| 137 | + mockValidateWorkflowPermissions.mockResolvedValue({ |
| 138 | + error: null, |
| 139 | + session: { user: { id: 'user-123' } }, |
| 140 | + workflow: { id: 'workflow-123' }, |
| 141 | + }) |
| 142 | + |
| 143 | + const response = await GET(createRequest(), routeParams()) |
| 144 | + |
| 145 | + expect(response.status).toBe(200) |
| 146 | + const data = await response.json() |
| 147 | + expect(data.deployedState).toEqual(DEPLOYED_STATE) |
| 148 | + expect(mockValidateWorkflowPermissions).toHaveBeenCalledWith( |
| 149 | + 'workflow-123', |
| 150 | + expect.any(String), |
| 151 | + 'read' |
| 152 | + ) |
| 153 | + expect(mockAuthorizeWorkflowByWorkspacePermission).not.toHaveBeenCalled() |
| 154 | + }) |
| 155 | + |
| 156 | + it('propagates validateWorkflowPermissions errors unchanged', async () => { |
| 157 | + mockValidateWorkflowPermissions.mockResolvedValue({ |
| 158 | + error: { message: 'Unauthorized', status: 401 }, |
| 159 | + session: null, |
| 160 | + workflow: null, |
| 161 | + }) |
| 162 | + |
| 163 | + const response = await GET(createRequest(), routeParams()) |
| 164 | + |
| 165 | + expect(response.status).toBe(401) |
| 166 | + const data = await response.json() |
| 167 | + expect(data.error).toBe('Unauthorized') |
| 168 | + }) |
| 169 | + |
| 170 | + it('falls back to session validation when the bearer token is not a valid internal token', async () => { |
| 171 | + mockVerifyInternalToken.mockResolvedValue({ valid: false }) |
| 172 | + mockValidateWorkflowPermissions.mockResolvedValue({ |
| 173 | + error: { message: 'Unauthorized', status: 401 }, |
| 174 | + session: null, |
| 175 | + workflow: null, |
| 176 | + }) |
| 177 | + |
| 178 | + const response = await GET(createRequest({ bearerToken: 'not-internal' }), routeParams()) |
| 179 | + |
| 180 | + expect(response.status).toBe(401) |
| 181 | + expect(mockValidateWorkflowPermissions).toHaveBeenCalled() |
| 182 | + expect(mockAuthorizeWorkflowByWorkspacePermission).not.toHaveBeenCalled() |
| 183 | + }) |
| 184 | + }) |
| 185 | + |
| 186 | + it('returns null deployedState when loading the snapshot fails', async () => { |
| 187 | + mockVerifyInternalToken.mockResolvedValue({ valid: true, userId: 'user-123' }) |
| 188 | + mockAuthorizeWorkflowByWorkspacePermission.mockResolvedValue({ |
| 189 | + allowed: true, |
| 190 | + status: 200, |
| 191 | + workflow: { id: 'workflow-123', workspaceId: 'workspace-456' }, |
| 192 | + workspacePermission: 'admin', |
| 193 | + }) |
| 194 | + mockLoadDeployedWorkflowState.mockRejectedValue(new Error('no active deployment')) |
| 195 | + |
| 196 | + const response = await GET(createRequest({ bearerToken: 'internal-token' }), routeParams()) |
| 197 | + |
| 198 | + expect(response.status).toBe(200) |
| 199 | + const data = await response.json() |
| 200 | + expect(data.deployedState).toBeNull() |
| 201 | + }) |
| 202 | +}) |
0 commit comments