|
1 | 1 | /** |
2 | 2 | * @vitest-environment node |
3 | 3 | */ |
4 | | -import { dbChainMockFns, permissionsMock, permissionsMockFns, resetDbChainMock } from '@sim/testing' |
| 4 | +import { |
| 5 | + dbChainMockFns, |
| 6 | + flattenMockConditions, |
| 7 | + hasMockCondition, |
| 8 | + permissionsMock, |
| 9 | + permissionsMockFns, |
| 10 | + resetDbChainMock, |
| 11 | + schemaMock, |
| 12 | +} from '@sim/testing' |
5 | 13 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
6 | 14 |
|
7 | 15 | const { |
@@ -31,7 +39,63 @@ vi.mock('@/lib/billing/core/usage', () => ({ |
31 | 39 | ensureUserStatsExists: mockEnsureUserStatsExists, |
32 | 40 | })) |
33 | 41 |
|
34 | | -import { KnowledgeBasePermissionError, updateKnowledgeBase } from '@/lib/knowledge/service' |
| 42 | +import { |
| 43 | + getKnowledgeBases, |
| 44 | + KnowledgeBasePermissionError, |
| 45 | + updateKnowledgeBase, |
| 46 | +} from '@/lib/knowledge/service' |
| 47 | + |
| 48 | +/** |
| 49 | + * The listing query authorizes on current workspace membership, never on stale creator |
| 50 | + * identity: a user removed from a workspace must stop seeing knowledge bases they created |
| 51 | + * there. The creator fallback exists only for legacy knowledge bases with no `workspaceId`. |
| 52 | + */ |
| 53 | +describe('getKnowledgeBases — creator fallback is scoped to legacy non-workspace KBs', () => { |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks() |
| 56 | + resetDbChainMock() |
| 57 | + }) |
| 58 | + |
| 59 | + /** Every disjunct that grants on `knowledgeBase.userId`, from the last select chain's WHERE. */ |
| 60 | + const capturedCreatorBranches = (): unknown[] => { |
| 61 | + const [condition] = dbChainMockFns.where.mock.calls.at(-1) ?? [] |
| 62 | + const orNode = flattenMockConditions(condition).find((node) => node.type === 'or') |
| 63 | + expect(orNode, 'WHERE clause has no or(...) branch').toBeDefined() |
| 64 | + return (orNode?.conditions as unknown[]).filter((disjunct) => |
| 65 | + hasMockCondition( |
| 66 | + disjunct, |
| 67 | + (node) => |
| 68 | + node.type === 'eq' && |
| 69 | + node.left === schemaMock.knowledgeBase.userId && |
| 70 | + node.right === 'user-a' |
| 71 | + ) |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + /** The creator fallback must be the sole grant for legacy KBs and never reach workspace KBs. */ |
| 76 | + const expectCreatorBranchIsLegacyOnly = () => { |
| 77 | + const branches = capturedCreatorBranches() |
| 78 | + expect(branches).toHaveLength(1) |
| 79 | + expect( |
| 80 | + hasMockCondition( |
| 81 | + branches[0], |
| 82 | + (node) => node.type === 'isNull' && node.column === schemaMock.knowledgeBase.workspaceId |
| 83 | + ) |
| 84 | + ).toBe(true) |
| 85 | + } |
| 86 | + |
| 87 | + it('requires workspaceId IS NULL on the creator branch when no workspace filter is given', async () => { |
| 88 | + await getKnowledgeBases('user-a', undefined, 'all') |
| 89 | + |
| 90 | + expectCreatorBranchIsLegacyOnly() |
| 91 | + }) |
| 92 | + |
| 93 | + it('keeps the same guard on the workspace-filtered branch', async () => { |
| 94 | + await getKnowledgeBases('user-a', 'ws-1', 'active') |
| 95 | + |
| 96 | + expectCreatorBranchIsLegacyOnly() |
| 97 | + }) |
| 98 | +}) |
35 | 99 |
|
36 | 100 | /** |
37 | 101 | * These tests guard the workspace mass-assignment fix: |
@@ -82,7 +146,7 @@ describe('updateKnowledgeBase — workspace transfer authorization', () => { |
82 | 146 |
|
83 | 147 | await expect( |
84 | 148 | updateKnowledgeBase('kb-1', { workspaceId: null }, 'req-1', { actorUserId: 'owner' }) |
85 | | - ).rejects.not.toBeInstanceOf(KnowledgeBasePermissionError) |
| 149 | + ).resolves.toBeDefined() |
86 | 150 | expect(permissionsMockFns.mockGetUserEntityPermissions).not.toHaveBeenCalled() |
87 | 151 | }) |
88 | 152 |
|
|
0 commit comments