feat: gate code runs by user whitelist

This commit is contained in:
John
2026-07-02 08:42:44 +08:00
parent 8a53dff856
commit c7d8140a69
7 changed files with 117 additions and 2 deletions
+71
View File
@@ -142,6 +142,77 @@ test('POST /agent/runs accepts explicit code tool mode and task type', async ()
});
});
test('POST /agent/runs accepts code tool mode for whitelisted user', async () => {
const created = [];
const handler = createPostAgentRunsHandler({
userAuth: {
async ownsSession() {
return true;
},
},
agentRunGateway: {
async createRun(userId, payload) {
created.push({ userId, payload });
return { id: 'run-code', status: 'queued' };
},
},
codeRunsEnabled: true,
codeRunUserIds: new Set(['user-1']),
});
const res = createResponseRecorder();
await handler(
{
currentUser: { id: 'user-1' },
body: {
session_id: 'session-1',
request_id: 'req-code',
user_message: { role: 'user', content: [] },
tool_mode: 'code',
},
},
res,
);
assert.equal(res.statusCode, 202);
assert.equal(created[0].userId, 'user-1');
assert.equal(created[0].payload.toolMode, 'code');
});
test('POST /agent/runs rejects code tool mode for non-whitelisted user', async () => {
const handler = createPostAgentRunsHandler({
userAuth: {
async ownsSession() {
throw new Error('should not check ownership after user gate rejects code mode');
},
},
agentRunGateway: {
async createRun() {
throw new Error('should not be called');
},
},
codeRunsEnabled: true,
codeRunUserIds: new Set(['user-2']),
});
const res = createResponseRecorder();
await handler(
{
currentUser: { id: 'user-1' },
body: {
session_id: 'session-1',
request_id: 'req-code',
user_message: { role: 'user', content: [] },
tool_mode: 'code',
},
},
res,
);
assert.equal(res.statusCode, 403);
assert.deepEqual(res.body, { message: '当前用户未开启代码任务灰度' });
});
test('POST /agent/runs rejects code tool mode when server gate is disabled', async () => {
const handler = createPostAgentRunsHandler({
userAuth: {