Skip to content

Commit 171f06e

Browse files
committed
fix(api-key): read workspace key settings on the auth pool
Give the memory harness integration mock a dbFor so the billing pool resolves.
1 parent 40b4b0a commit 171f06e

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎apps/sim/executor/handlers/agent/memory-harness.integration.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const fixture = vi.hoisted(() => ({
3030
uploads: '',
3131
}))
3232

33-
vi.mock('@sim/db', () => ({
34-
db: new Proxy(
33+
vi.mock('@sim/db', () => {
34+
const db = new Proxy(
3535
{},
3636
{
3737
get(_target, property) {
@@ -40,8 +40,9 @@ vi.mock('@sim/db', () => ({
4040
return typeof value === 'function' ? value.bind(fixture.database) : value
4141
},
4242
}
43-
),
44-
}))
43+
)
44+
return { db, dbFor: () => db }
45+
})
4546
vi.mock('@/lib/uploads/core/setup.server', () => ({
4647
get UPLOAD_DIR_SERVER() {
4748
return fixture.uploads

‎apps/sim/lib/api-key/service.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function authenticateApiKeyFromHeader(
5656
let workspaceSettings: WorkspaceBillingSettings | null = null
5757

5858
if (options.workspaceId) {
59-
workspaceSettings = await getWorkspaceBillingSettings(options.workspaceId)
59+
workspaceSettings = await getWorkspaceBillingSettings(options.workspaceId, getAuthPool())
6060
if (!workspaceSettings) {
6161
return { success: false, error: 'Workspace not found' }
6262
}

‎apps/sim/lib/db/pool-isolation.integration.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,13 @@ describe('dedicated pools under a saturated process pool', () => {
125125
await expect(settlesWithin(lookup, ISOLATED_BUDGET_MS)).resolves.toBe('done')
126126
await expect(lookup).resolves.toEqual({ success: false, error: 'Invalid API key' })
127127
})
128+
129+
it('reads workspace key settings on the auth pool', async () => {
130+
const lookup = authenticateApiKeyFromHeader(`sk-sim-${generateId()}`, {
131+
workspaceId: generateId(),
132+
})
133+
134+
await expect(settlesWithin(lookup, ISOLATED_BUDGET_MS)).resolves.toBe('done')
135+
await expect(lookup).resolves.toEqual({ success: false, error: 'Workspace not found' })
136+
})
128137
})

‎apps/sim/lib/workspaces/utils.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
type ChangeWorkspaceStoragePayerParams,
1010
changeWorkspaceStoragePayerInTx,
1111
} from '@/lib/billing/storage/payer-transfer'
12-
import type { DbOrTx } from '@/lib/db/types'
12+
import type { DbClient, DbOrTx } from '@/lib/db/types'
1313

1414
const logger = createLogger('WorkspaceUtils')
1515

@@ -21,13 +21,14 @@ export interface WorkspaceBillingSettings {
2121
export type WorkspaceScope = 'active' | 'archived' | 'all'
2222

2323
export async function getWorkspaceBillingSettings(
24-
workspaceId: string
24+
workspaceId: string,
25+
executor: DbClient = db
2526
): Promise<WorkspaceBillingSettings | null> {
2627
if (!workspaceId) {
2728
return null
2829
}
2930

30-
const rows = await db
31+
const rows = await executor
3132
.select({
3233
billedAccountUserId: workspaceTable.billedAccountUserId,
3334
allowPersonalApiKeys: workspaceTable.allowPersonalApiKeys,

0 commit comments

Comments
 (0)