|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + resolvePermission: vi.fn(), |
| 8 | + authorizeOrganization: vi.fn(), |
| 9 | + recordAudit: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@sim/audit', () => ({ |
| 13 | + AuditAction: { KNOWLEDGE_BASE_UPDATED: 'knowledge_base.updated' }, |
| 14 | + AuditResourceType: { KNOWLEDGE_BASE: 'knowledge_base' }, |
| 15 | + recordAudit: mocks.recordAudit, |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@sim/platform-authz/workspace', () => ({ |
| 19 | + permissionSatisfies: (actual: string | null, required: string) => { |
| 20 | + const rank = { read: 1, write: 2, admin: 3 } as const |
| 21 | + return ( |
| 22 | + actual !== null && rank[actual as keyof typeof rank] >= rank[required as keyof typeof rank] |
| 23 | + ) |
| 24 | + }, |
| 25 | + resolveEffectiveWorkspacePermission: mocks.resolvePermission, |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/core/application/organization-authorization', () => ({ |
| 29 | + authorizeOrganizationOperation: mocks.authorizeOrganization, |
| 30 | +})) |
| 31 | + |
| 32 | +import { defineAuthorizedKnowledgeUseCase } from '@/lib/knowledge/application/authorized-knowledge-use-case' |
| 33 | +import { knowledgeOperations } from '@/lib/knowledge/application/operations' |
| 34 | + |
| 35 | +const workspaceContext = { |
| 36 | + workspaceId: 'workspace-1', |
| 37 | + workspaceOrganizationId: 'organization-1', |
| 38 | + allowPersonalApiKeys: true, |
| 39 | + billedAccountUserId: 'owner-1', |
| 40 | + knowledgeBaseId: 'knowledge-1', |
| 41 | +} |
| 42 | +const organizationContext = { |
| 43 | + workspaceId: undefined, |
| 44 | + organizationId: 'organization-1', |
| 45 | + knowledgeBaseId: 'knowledge-1', |
| 46 | +} |
| 47 | +const legacyContext = { |
| 48 | + workspaceId: undefined, |
| 49 | + legacyPersonalOwnerUserId: 'user-1', |
| 50 | + knowledgeBaseId: 'knowledge-1', |
| 51 | +} |
| 52 | + |
| 53 | +const session = { kind: 'session', userId: 'user-1', sessionId: 'session-1' } as const |
| 54 | + |
| 55 | +function useCaseFor(context: object) { |
| 56 | + const execute = vi.fn(async () => 'done') |
| 57 | + const useCase = defineAuthorizedKnowledgeUseCase({ |
| 58 | + operation: knowledgeOperations.read, |
| 59 | + resolveContext: () => context as never, |
| 60 | + execute, |
| 61 | + projectAudit: () => ({ |
| 62 | + action: 'knowledge_base.updated', |
| 63 | + resourceType: 'knowledge_base', |
| 64 | + resourceId: 'knowledge-1', |
| 65 | + resourceName: 'Docs', |
| 66 | + description: 'audited', |
| 67 | + metadata: {}, |
| 68 | + }), |
| 69 | + }) |
| 70 | + return { useCase, execute } |
| 71 | +} |
| 72 | + |
| 73 | +describe('defineAuthorizedKnowledgeUseCase', () => { |
| 74 | + beforeEach(() => { |
| 75 | + vi.clearAllMocks() |
| 76 | + mocks.resolvePermission.mockResolvedValue('read') |
| 77 | + mocks.authorizeOrganization.mockResolvedValue(undefined) |
| 78 | + }) |
| 79 | + |
| 80 | + /** `authorize` must run the same funnel `execute` does, and nothing else. */ |
| 81 | + it('authorizes a workspace base through the workspace funnel without executing', async () => { |
| 82 | + const { useCase, execute } = useCaseFor(workspaceContext) |
| 83 | + |
| 84 | + await useCase.authorize({ principal: session, input: {} }) |
| 85 | + expect(mocks.resolvePermission).toHaveBeenCalledTimes(1) |
| 86 | + expect(execute).not.toHaveBeenCalled() |
| 87 | + expect(mocks.recordAudit).not.toHaveBeenCalled() |
| 88 | + |
| 89 | + mocks.resolvePermission.mockResolvedValue(null) |
| 90 | + await expect(useCase.authorize({ principal: session, input: {} })).rejects.toMatchObject({ |
| 91 | + name: 'NoWorkspaceAccessError', |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + it('executes a workspace base and records its audit under the workspace', async () => { |
| 96 | + const { useCase } = useCaseFor(workspaceContext) |
| 97 | + |
| 98 | + await expect(useCase.execute({ principal: session, input: {} })).resolves.toBe('done') |
| 99 | + expect(mocks.recordAudit).toHaveBeenCalledWith( |
| 100 | + expect.objectContaining({ workspaceId: 'workspace-1', resourceId: 'knowledge-1' }) |
| 101 | + ) |
| 102 | + }) |
| 103 | + |
| 104 | + it('authorizes and executes an organization base through the organization operation', async () => { |
| 105 | + const { useCase, execute } = useCaseFor(organizationContext) |
| 106 | + |
| 107 | + await useCase.authorize({ principal: session, input: {} }) |
| 108 | + expect(mocks.authorizeOrganization).toHaveBeenCalledWith( |
| 109 | + session, |
| 110 | + knowledgeOperations.read.organizationOperation, |
| 111 | + organizationContext |
| 112 | + ) |
| 113 | + expect(execute).not.toHaveBeenCalled() |
| 114 | + expect(mocks.resolvePermission).not.toHaveBeenCalled() |
| 115 | + |
| 116 | + await expect(useCase.execute({ principal: session, input: {} })).resolves.toBe('done') |
| 117 | + expect(mocks.recordAudit).toHaveBeenCalledWith( |
| 118 | + expect.objectContaining({ |
| 119 | + workspaceId: undefined, |
| 120 | + resourceId: 'knowledge-1', |
| 121 | + metadata: expect.objectContaining({ organizationId: 'organization-1' }), |
| 122 | + }) |
| 123 | + ) |
| 124 | + }) |
| 125 | + |
| 126 | + it('admits only the owner of a legacy personal base', async () => { |
| 127 | + const { useCase, execute } = useCaseFor(legacyContext) |
| 128 | + |
| 129 | + await useCase.authorize({ principal: session, input: {} }) |
| 130 | + expect(execute).not.toHaveBeenCalled() |
| 131 | + await expect(useCase.execute({ principal: session, input: {} })).resolves.toBe('done') |
| 132 | + |
| 133 | + const stranger = { kind: 'session', userId: 'user-2', sessionId: 'session-2' } as const |
| 134 | + await expect(useCase.authorize({ principal: stranger, input: {} })).rejects.toMatchObject({ |
| 135 | + code: 'not_found', |
| 136 | + }) |
| 137 | + await expect( |
| 138 | + useCase.authorize({ |
| 139 | + principal: { kind: 'workspace_api_key', workspaceId: 'workspace-1', keyId: 'key-1' }, |
| 140 | + input: {}, |
| 141 | + }) |
| 142 | + ).rejects.toMatchObject({ code: 'not_found' }) |
| 143 | + }) |
| 144 | +}) |
0 commit comments