-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathutils.test.ts
More file actions
318 lines (274 loc) · 10.8 KB
/
Copy pathutils.test.ts
File metadata and controls
318 lines (274 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* @vitest-environment node
*/
import { db } from '@sim/db'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockChangeWorkspaceStoragePayerInTx } = vi.hoisted(() => ({
mockChangeWorkspaceStoragePayerInTx: vi.fn(),
}))
vi.mock('@sim/db', () => ({
db: { select: vi.fn(), transaction: vi.fn() },
}))
vi.mock('@/lib/billing/storage/payer-transfer', () => ({
changeWorkspaceStoragePayerInTx: mockChangeWorkspaceStoragePayerInTx,
}))
import {
listAccessibleWorkspaceRowsForUser,
reassignBilledAccountForUser,
reassignWorkflowOwnershipForWorkspaceMemberRemovalTx,
} from '@/lib/workspaces/utils'
const mockDb = db as unknown as {
select: ReturnType<typeof vi.fn>
transaction: ReturnType<typeof vi.fn>
}
function createMockChain(finalResult: unknown) {
const chain: any = {}
chain.then = vi.fn().mockImplementation((resolve: any) => resolve(finalResult))
chain.from = vi.fn().mockReturnValue(chain)
chain.where = vi.fn().mockReturnValue(chain)
chain.innerJoin = vi.fn().mockReturnValue(chain)
chain.limit = vi.fn().mockReturnValue(chain)
chain.orderBy = vi.fn().mockReturnValue(chain)
return chain
}
function createSelectChain(result: unknown) {
const limit = vi.fn().mockResolvedValue(result)
const where = vi.fn().mockReturnValue({ limit })
const from = vi.fn().mockReturnValue({
where: vi.fn().mockResolvedValue(result),
limit,
})
return { from, where, limit }
}
function createGroupedSelectChain(result: unknown) {
const groupBy = vi.fn().mockResolvedValue(result)
const where = vi.fn().mockReturnValue({ groupBy })
const from = vi.fn().mockReturnValue({ where })
return { from, where, groupBy }
}
function createUpdateChain(result: unknown) {
const returning = vi.fn().mockResolvedValue(result)
const where = vi.fn().mockReturnValue({ returning })
const set = vi.fn().mockReturnValue({ where })
return { set, where, returning }
}
describe('reassignBilledAccountForUser', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('routes each resolved workspace through the payer helper in its own transaction', async () => {
const updateChain = createUpdateChain([])
const tx = {
update: vi.fn().mockReturnValue(updateChain),
}
mockDb.select.mockReturnValueOnce(
createMockChain([
{ id: 'workspace-personal', ownerId: 'owner-1', organizationId: null },
{ id: 'workspace-org', ownerId: 'owner-2', organizationId: 'org-1' },
])
)
mockDb.transaction.mockImplementation(
async (callback: (transaction: typeof tx) => Promise<unknown>) => callback(tx)
)
const result = await reassignBilledAccountForUser('departing-user')
expect(mockDb.transaction).toHaveBeenCalledTimes(2)
expect(mockChangeWorkspaceStoragePayerInTx).toHaveBeenNthCalledWith(1, tx, {
workspaceId: 'workspace-personal',
organizationId: null,
billedAccountUserId: 'owner-1',
expectedCurrentPayer: {
organizationId: null,
billedAccountUserId: 'departing-user',
},
})
expect(mockChangeWorkspaceStoragePayerInTx).toHaveBeenNthCalledWith(2, tx, {
workspaceId: 'workspace-org',
organizationId: 'org-1',
billedAccountUserId: 'owner-2',
expectedCurrentPayer: {
organizationId: 'org-1',
billedAccountUserId: 'departing-user',
},
})
expect(updateChain.set).toHaveBeenCalledTimes(2)
expect(updateChain.set).toHaveBeenCalledWith({ updatedAt: expect.any(Date) })
expect(result).toEqual({
reassigned: [
{ workspaceId: 'workspace-personal', newBilledAccountUserId: 'owner-1' },
{ workspaceId: 'workspace-org', newBilledAccountUserId: 'owner-2' },
],
unresolved: [],
})
})
it('preserves the admin fallback and unresolved behavior', async () => {
const updateChain = createUpdateChain([])
const tx = {
update: vi.fn().mockReturnValue(updateChain),
}
mockDb.select
.mockReturnValueOnce(
createMockChain([
{ id: 'workspace-admin', ownerId: 'departing-user', organizationId: null },
{ id: 'workspace-unresolved', ownerId: 'departing-user', organizationId: null },
])
)
.mockReturnValueOnce(createMockChain([{ userId: 'admin-1' }]))
.mockReturnValueOnce(createMockChain([]))
mockDb.transaction.mockImplementation(
async (callback: (transaction: typeof tx) => Promise<unknown>) => callback(tx)
)
const result = await reassignBilledAccountForUser('departing-user')
expect(mockChangeWorkspaceStoragePayerInTx).toHaveBeenCalledTimes(1)
expect(mockChangeWorkspaceStoragePayerInTx).toHaveBeenCalledWith(
tx,
expect.objectContaining({
workspaceId: 'workspace-admin',
billedAccountUserId: 'admin-1',
})
)
expect(result).toEqual({
reassigned: [{ workspaceId: 'workspace-admin', newBilledAccountUserId: 'admin-1' }],
unresolved: ['workspace-unresolved'],
})
})
it('stops the loop and propagates payer-transfer errors', async () => {
const updateChain = createUpdateChain([])
const tx = {
update: vi.fn().mockReturnValue(updateChain),
}
mockDb.select.mockReturnValueOnce(
createMockChain([
{ id: 'workspace-1', ownerId: 'owner-1', organizationId: null },
{ id: 'workspace-2', ownerId: 'owner-2', organizationId: null },
])
)
mockDb.transaction.mockImplementation(
async (callback: (transaction: typeof tx) => Promise<unknown>) => callback(tx)
)
mockChangeWorkspaceStoragePayerInTx.mockRejectedValueOnce(new Error('payer transfer failed'))
await expect(reassignBilledAccountForUser('departing-user')).rejects.toThrow(
'payer transfer failed'
)
expect(mockDb.transaction).toHaveBeenCalledTimes(1)
expect(mockChangeWorkspaceStoragePayerInTx).toHaveBeenCalledTimes(1)
expect(tx.update).not.toHaveBeenCalled()
})
})
describe('reassignWorkflowOwnershipForWorkspaceMemberRemovalTx', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('reassigns departing member workflows to the workspace billed account', async () => {
const workspaceSelect = createSelectChain([
{ id: 'workspace-1', billedAccountUserId: 'billed-1' },
])
const workflowCountSelect = createGroupedSelectChain([
{ workspaceId: 'workspace-1', workflowCount: 2 },
])
const workflowUpdate = createUpdateChain([])
const tx = {
select: vi.fn().mockReturnValueOnce(workspaceSelect).mockReturnValueOnce(workflowCountSelect),
update: vi.fn().mockReturnValue(workflowUpdate),
}
const result = await reassignWorkflowOwnershipForWorkspaceMemberRemovalTx({
tx: tx as any,
workspaceIds: ['workspace-1'],
departingUserId: 'departing-1',
})
expect(tx.update).toHaveBeenCalledTimes(1)
expect(workflowUpdate.set).toHaveBeenCalledWith(
expect.objectContaining({ updatedAt: expect.any(Date) })
)
expect(result).toEqual({
reassigned: [{ workspaceId: 'workspace-1', newWorkflowUserId: 'billed-1', workflowCount: 2 }],
unresolved: [],
})
})
it('marks a workspace unresolved when the departing member is the billed account', async () => {
const workspaceSelect = createSelectChain([
{ id: 'workspace-1', billedAccountUserId: 'departing-1' },
])
const workflowCountSelect = createGroupedSelectChain([
{ workspaceId: 'workspace-1', workflowCount: 1 },
])
const tx = {
select: vi.fn().mockReturnValueOnce(workspaceSelect).mockReturnValueOnce(workflowCountSelect),
update: vi.fn(),
}
const result = await reassignWorkflowOwnershipForWorkspaceMemberRemovalTx({
tx: tx as any,
workspaceIds: ['workspace-1'],
departingUserId: 'departing-1',
})
expect(tx.update).not.toHaveBeenCalled()
expect(result).toEqual({ reassigned: [], unresolved: ['workspace-1'] })
})
it('does not mark a workspace unresolved when the departing billing account owns no workflows', async () => {
const workspaceSelect = createSelectChain([
{ id: 'workspace-1', billedAccountUserId: 'departing-1' },
])
const workflowCountSelect = createGroupedSelectChain([])
const tx = {
select: vi.fn().mockReturnValueOnce(workspaceSelect).mockReturnValueOnce(workflowCountSelect),
update: vi.fn(),
}
const result = await reassignWorkflowOwnershipForWorkspaceMemberRemovalTx({
tx: tx as any,
workspaceIds: ['workspace-1'],
departingUserId: 'departing-1',
})
expect(tx.update).not.toHaveBeenCalled()
expect(result).toEqual({ reassigned: [], unresolved: [] })
})
it('marks a workspace unresolved when no billed account is configured', async () => {
const workspaceSelect = createSelectChain([{ id: 'workspace-1', billedAccountUserId: null }])
const workflowCountSelect = createGroupedSelectChain([
{ workspaceId: 'workspace-1', workflowCount: 1 },
])
const tx = {
select: vi.fn().mockReturnValueOnce(workspaceSelect).mockReturnValueOnce(workflowCountSelect),
update: vi.fn(),
}
const result = await reassignWorkflowOwnershipForWorkspaceMemberRemovalTx({
tx: tx as any,
workspaceIds: ['workspace-1'],
departingUserId: 'departing-1',
})
expect(tx.update).not.toHaveBeenCalled()
expect(result).toEqual({ reassigned: [], unresolved: ['workspace-1'] })
})
})
describe('listAccessibleWorkspaceRowsForUser', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('elevates an org admin to admin on an org workspace where they hold a lower explicit grant', async () => {
const orgWorkspace = { id: 'ws-1', name: 'Shared', ownerId: 'owner-x', organizationId: 'org-1' }
mockDb.select
.mockReturnValueOnce(createMockChain([{ workspace: orgWorkspace, permissionType: 'write' }]))
.mockReturnValueOnce(createMockChain([{ organizationId: 'org-1', role: 'admin' }]))
.mockReturnValueOnce(createMockChain([orgWorkspace]))
const rows = await listAccessibleWorkspaceRowsForUser('user-1', 'active')
expect(rows).toEqual([{ workspace: orgWorkspace, permissionType: 'admin' }])
})
it('keeps a lower explicit grant on a workspace owned by a different organization', async () => {
const externalWorkspace = {
id: 'ws-ext',
name: 'External',
ownerId: 'owner-y',
organizationId: 'org-2',
}
const orgWorkspace = { id: 'ws-1', name: 'Shared', ownerId: 'owner-x', organizationId: 'org-1' }
mockDb.select
.mockReturnValueOnce(
createMockChain([{ workspace: externalWorkspace, permissionType: 'write' }])
)
.mockReturnValueOnce(createMockChain([{ organizationId: 'org-1', role: 'admin' }]))
.mockReturnValueOnce(createMockChain([orgWorkspace]))
const rows = await listAccessibleWorkspaceRowsForUser('user-1', 'active')
expect(rows).toEqual([
{ workspace: externalWorkspace, permissionType: 'write' },
{ workspace: orgWorkspace, permissionType: 'admin' },
])
})
})