feat: add bearer authentication key management with migration support (#503)

This commit is contained in:
samanhappy
2025-12-13 16:46:58 +08:00
committed by GitHub
parent 88efad9d60
commit 01855ca2ca
30 changed files with 2138 additions and 193 deletions

View File

@@ -2,7 +2,6 @@ import { getMcpSettingsJson } from '../../src/controllers/configController.js';
import * as DaoFactory from '../../src/dao/DaoFactory.js';
import { Request, Response } from 'express';
// Mock the DaoFactory module
jest.mock('../../src/dao/DaoFactory.js');
describe('ConfigController - getMcpSettingsJson', () => {
@@ -17,6 +16,7 @@ describe('ConfigController - getMcpSettingsJson', () => {
let mockUserConfigDao: { getAll: jest.Mock };
let mockOAuthClientDao: { findAll: jest.Mock };
let mockOAuthTokenDao: { findAll: jest.Mock };
let mockBearerKeyDao: { findAll: jest.Mock };
beforeEach(() => {
jest.clearAllMocks();
@@ -30,6 +30,7 @@ describe('ConfigController - getMcpSettingsJson', () => {
json: mockJson,
status: mockStatus,
};
mockServerDao = {
findById: jest.fn(),
findAll: jest.fn(),
@@ -40,68 +41,17 @@ describe('ConfigController - getMcpSettingsJson', () => {
mockUserConfigDao = { getAll: jest.fn() };
mockOAuthClientDao = { findAll: jest.fn() };
mockOAuthTokenDao = { findAll: jest.fn() };
mockBearerKeyDao = { findAll: jest.fn() };
// Setup ServerDao mock
(DaoFactory.getServerDao as jest.Mock).mockReturnValue(mockServerDao);
(DaoFactory.getUserDao as jest.Mock).mockReturnValue(mockUserDao);
(DaoFactory.getGroupDao as jest.Mock).mockReturnValue(mockGroupDao);
(DaoFactory.getSystemConfigDao as jest.Mock).mockReturnValue(mockSystemConfigDao);
(DaoFactory.getUserConfigDao as jest.Mock).mockReturnValue(mockUserConfigDao);
(DaoFactory.getOAuthClientDao as jest.Mock).mockReturnValue(mockOAuthClientDao);
(DaoFactory.getOAuthTokenDao as jest.Mock).mockReturnValue(mockOAuthTokenDao);
});
describe('Full Settings Export', () => {
it('should return settings aggregated from DAOs', async () => {
mockServerDao.findAll.mockResolvedValue([
{ name: 'server-a', command: 'node', args: ['index.js'], env: { A: '1' } },
{ name: 'server-b', command: 'npx', args: ['run'], env: null },
]);
mockUserDao.findAll.mockResolvedValue([
{ username: 'admin', password: 'hash', isAdmin: true },
]);
mockGroupDao.findAll.mockResolvedValue([{ id: 'g1', name: 'Group', servers: [] }]);
mockSystemConfigDao.get.mockResolvedValue({ routing: { skipAuth: false } });
mockUserConfigDao.getAll.mockResolvedValue({ admin: { routing: {} } });
mockOAuthClientDao.findAll.mockResolvedValue([
{ clientId: 'c1', clientSecret: 's', name: 'client' },
]);
mockOAuthTokenDao.findAll.mockResolvedValue([
{
accessToken: 'a',
accessTokenExpiresAt: new Date('2024-01-01T00:00:00Z'),
clientId: 'c1',
username: 'admin',
},
]);
await getMcpSettingsJson(mockRequest as Request, mockResponse as Response);
expect(mockServerDao.findAll).toHaveBeenCalled();
expect(mockUserDao.findAll).toHaveBeenCalled();
expect(mockJson).toHaveBeenCalledWith({
success: true,
data: {
mcpServers: {
'server-a': { command: 'node', args: ['index.js'], env: { A: '1' } },
'server-b': { command: 'npx', args: ['run'] },
},
users: [{ username: 'admin', password: 'hash', isAdmin: true }],
groups: [{ id: 'g1', name: 'Group', servers: [] }],
systemConfig: { routing: { skipAuth: false } },
userConfigs: { admin: { routing: {} } },
oauthClients: [{ clientId: 'c1', clientSecret: 's', name: 'client' }],
oauthTokens: [
{
accessToken: 'a',
accessTokenExpiresAt: new Date('2024-01-01T00:00:00Z'),
clientId: 'c1',
username: 'admin',
},
],
},
});
});
// Wire DaoFactory convenience functions to our mocks
(DaoFactory.getServerDao as unknown as jest.Mock).mockReturnValue(mockServerDao);
(DaoFactory.getUserDao as unknown as jest.Mock).mockReturnValue(mockUserDao);
(DaoFactory.getGroupDao as unknown as jest.Mock).mockReturnValue(mockGroupDao);
(DaoFactory.getSystemConfigDao as unknown as jest.Mock).mockReturnValue(mockSystemConfigDao);
(DaoFactory.getUserConfigDao as unknown as jest.Mock).mockReturnValue(mockUserConfigDao);
(DaoFactory.getOAuthClientDao as unknown as jest.Mock).mockReturnValue(mockOAuthClientDao);
(DaoFactory.getOAuthTokenDao as unknown as jest.Mock).mockReturnValue(mockOAuthTokenDao);
(DaoFactory.getBearerKeyDao as unknown as jest.Mock).mockReturnValue(mockBearerKeyDao);
});
describe('Individual Server Export', () => {
@@ -196,6 +146,7 @@ describe('ConfigController - getMcpSettingsJson', () => {
mockUserConfigDao.getAll.mockResolvedValue({});
mockOAuthClientDao.findAll.mockResolvedValue([]);
mockOAuthTokenDao.findAll.mockResolvedValue([]);
mockBearerKeyDao.findAll.mockResolvedValue([]);
await getMcpSettingsJson(mockRequest as Request, mockResponse as Response);