|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + getSession: vi.fn(), |
| 10 | + read: vi.fn(), |
| 11 | + update: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/auth', () => ({ getSession: mocks.getSession })) |
| 15 | + |
| 16 | +vi.mock('@/lib/credential-groups/application/manage-access', () => ({ |
| 17 | + readCredentialGroupAccess: { |
| 18 | + operation: { id: 'credential_groups.access.read' }, |
| 19 | + execute: mocks.read, |
| 20 | + }, |
| 21 | + updateCredentialGroupAccess: { |
| 22 | + operation: { id: 'credential_groups.access.update' }, |
| 23 | + execute: mocks.update, |
| 24 | + }, |
| 25 | +})) |
| 26 | + |
| 27 | +import { GET, PUT } from '@/app/api/workspaces/[id]/credential-groups/[groupId]/access/route' |
| 28 | + |
| 29 | +const WORKSPACE_ID = '11111111-1111-4111-8111-111111111111' |
| 30 | +const GROUP_ID = 'group-1' |
| 31 | +const url = `http://localhost:3000/api/workspaces/${WORKSPACE_ID}/credential-groups/${GROUP_ID}/access` |
| 32 | +const context = { params: Promise.resolve({ id: WORKSPACE_ID, groupId: GROUP_ID }) } |
| 33 | + |
| 34 | +describe('Credential Group access route', () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks() |
| 37 | + mocks.getSession.mockResolvedValue({ |
| 38 | + user: { id: 'admin-1' }, |
| 39 | + session: { id: 'session-1' }, |
| 40 | + }) |
| 41 | + mocks.read.mockResolvedValue({ revision: 0, grants: [] }) |
| 42 | + mocks.update.mockResolvedValue({ |
| 43 | + revision: 1, |
| 44 | + grants: [ |
| 45 | + { |
| 46 | + id: 'grant-1', |
| 47 | + subject: { type: 'workflow', workflowId: 'workflow-1' }, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it('reads the managed policy without exposing the built-in actor rule', async () => { |
| 54 | + const request = new NextRequest(url) |
| 55 | + const response = await GET(request, context) |
| 56 | + |
| 57 | + expect(response.status).toBe(200) |
| 58 | + expect(await response.json()).toEqual({ revision: 0, grants: [] }) |
| 59 | + expect(mocks.read).toHaveBeenCalledWith({ |
| 60 | + principal: { kind: 'session', userId: 'admin-1', sessionId: 'session-1' }, |
| 61 | + input: { assertedWorkspaceId: WORKSPACE_ID, credentialGroupId: GROUP_ID }, |
| 62 | + request, |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('updates exact managed subjects with optimistic revision input', async () => { |
| 67 | + const body = { |
| 68 | + expectedRevision: 0, |
| 69 | + grants: [{ subject: { type: 'workflow', workflowId: 'workflow-1' } }], |
| 70 | + } |
| 71 | + const request = new NextRequest(url, { |
| 72 | + method: 'PUT', |
| 73 | + body: JSON.stringify(body), |
| 74 | + headers: { 'content-type': 'application/json' }, |
| 75 | + }) |
| 76 | + const response = await PUT(request, context) |
| 77 | + |
| 78 | + expect(response.status).toBe(200) |
| 79 | + expect(mocks.update).toHaveBeenCalledWith({ |
| 80 | + principal: { kind: 'session', userId: 'admin-1', sessionId: 'session-1' }, |
| 81 | + input: { |
| 82 | + assertedWorkspaceId: WORKSPACE_ID, |
| 83 | + credentialGroupId: GROUP_ID, |
| 84 | + ...body, |
| 85 | + }, |
| 86 | + request, |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('authenticates before parsing a malformed policy body', async () => { |
| 91 | + mocks.getSession.mockResolvedValue(null) |
| 92 | + const request = new NextRequest(url, { |
| 93 | + method: 'PUT', |
| 94 | + body: '{', |
| 95 | + headers: { 'content-type': 'application/json' }, |
| 96 | + }) |
| 97 | + |
| 98 | + const response = await PUT(request, context) |
| 99 | + |
| 100 | + expect(response.status).toBe(401) |
| 101 | + expect(mocks.update).not.toHaveBeenCalled() |
| 102 | + }) |
| 103 | + |
| 104 | + it('rejects caller-supplied effects and actions at the HTTP boundary', async () => { |
| 105 | + const request = new NextRequest(url, { |
| 106 | + method: 'PUT', |
| 107 | + body: JSON.stringify({ |
| 108 | + expectedRevision: 0, |
| 109 | + grants: [ |
| 110 | + { |
| 111 | + subject: { type: 'workflow', workflowId: 'workflow-1' }, |
| 112 | + effect: 'allow', |
| 113 | + actions: ['credential_groups.credentials.use'], |
| 114 | + }, |
| 115 | + ], |
| 116 | + }), |
| 117 | + headers: { 'content-type': 'application/json' }, |
| 118 | + }) |
| 119 | + |
| 120 | + const response = await PUT(request, context) |
| 121 | + |
| 122 | + expect(response.status).toBe(400) |
| 123 | + expect(mocks.update).not.toHaveBeenCalled() |
| 124 | + }) |
| 125 | +}) |
0 commit comments