LibreChat/api/server/routes/__tests__/apiKeys.spec.js
Danny Avila 254c2a9b06
🔓 fix: Keep owner as the Default Mode for API Key Runs (#18448)
#18377 made `delegated` the default of endpoints.agents.remoteApi.integrations.mode, so
on upgrade a run started with a personal API key lost its MCP tools until the operator
declared servers and the key's owner granted them. That is a surprise for deployments
that never asked for grants.

The default is `owner` again: a key run uses the MCP servers its owner can, as before
grants existed. `delegated` stays available as an opt-in. The config loader's warning now
fires only for an operator who sets `delegated` and declares no server to grant.
2026-11-19 06:58:22 -05:00

72 lines
3 KiB
JavaScript

/**
* Loads the real route against the real packages. Only the app's own singletons are stood in
* for, so a dependency the handlers need and the route does not pass fails here.
*/
const express = require('express');
const request = require('supertest');
const { PermissionTypes, Permissions } = require('librechat-data-provider');
const mockKeys = [{ id: 'key-1', name: 'CI', keyPrefix: 'sk-abc', createdAt: '1970-01-01' }];
jest.mock('~/models', () => ({
getAgentModelSummaries: jest.fn(),
getAgentApiKeyById: jest.fn(),
createAgentApiKey: jest.fn(),
deleteAgentApiKey: jest.fn(),
listAgentApiKeys: jest.fn(async () => mockKeys),
getRoleByName: jest.fn(),
}));
jest.mock('~/server/services/Config/app', () => ({ getAppConfig: jest.fn() }));
jest.mock('~/server/services/PermissionService', () => ({ findAccessibleResources: jest.fn() }));
jest.mock('~/server/middleware', () => ({
requireJwtAuth: (req, _res, next) => {
req.user = { id: 'owner', role: 'USER', tenantId: 'tenant' };
next();
},
}));
const { getAppConfig } = require('~/server/services/Config/app');
const { findAccessibleResources } = require('~/server/services/PermissionService');
const { getRoleByName, getAgentApiKeyById, getAgentModelSummaries } = require('~/models');
const router = require('../apiKeys');
describe('API key routes', () => {
const app = express();
app.use('/api/api-keys', router);
beforeEach(() => {
getRoleByName.mockResolvedValue({
permissions: { [PermissionTypes.REMOTE_AGENTS]: { [Permissions.USE]: true } },
});
});
it('lists keys without the grants flag where key runs are not limited', async () => {
getAppConfig.mockResolvedValueOnce({ endpoints: { agents: {} } });
const response = await request(app).get('/api/api-keys');
expect(response.status).toBe(200);
expect(response.body).toEqual({ keys: mockKeys });
});
it('says grants are in use from the configuration resolved for the owner who asks', async () => {
getAppConfig.mockResolvedValueOnce({
endpoints: { agents: { remoteApi: { integrations: { mode: 'delegated' } } } },
});
const response = await request(app).get('/api/api-keys');
expect(response.body).toEqual({ keys: mockKeys, grants: true });
expect(getAppConfig).toHaveBeenLastCalledWith(
expect.objectContaining({ userId: 'owner', role: 'USER', tenantId: 'tenant' }),
);
});
it('lists the agents a key can be granted for, and does not take the path for a key id', async () => {
findAccessibleResources.mockResolvedValue(['id-a']);
getAgentModelSummaries.mockResolvedValueOnce([{ id: 'agent_a', name: 'Analyst' }]);
const response = await request(app).get('/api/api-keys/agents');
expect(response.status).toBe(200);
expect(response.body).toEqual({ agents: [{ id: 'agent_a', name: 'Analyst' }] });
expect(findAccessibleResources).toHaveBeenCalledWith(
expect.objectContaining({ userId: 'owner', role: 'USER' }),
);
expect(getAgentApiKeyById).not.toHaveBeenCalled();
});
});