|
| 1 | +/** |
| 2 | + * Unit tests for MCP endpoint controller (packages/server/src/controllers/mcp-endpoint/index.ts) |
| 3 | + * |
| 4 | + * Tests the Express request handlers: token extraction, auth enforcement, |
| 5 | + * rate limiter middleware delegation, and request routing to the service layer. |
| 6 | + */ |
| 7 | +import { Request, Response, NextFunction } from 'express' |
| 8 | + |
| 9 | +// --- Mock setup --- |
| 10 | +const mockHandleMcpRequest = jest.fn() |
| 11 | +const mockHandleMcpDeleteRequest = jest.fn() |
| 12 | + |
| 13 | +jest.mock('../../services/mcp-endpoint', () => ({ |
| 14 | + __esModule: true, |
| 15 | + default: { |
| 16 | + handleMcpRequest: (...args: any[]) => mockHandleMcpRequest(...args), |
| 17 | + handleMcpDeleteRequest: (...args: any[]) => mockHandleMcpDeleteRequest(...args) |
| 18 | + } |
| 19 | +})) |
| 20 | + |
| 21 | +const mockGetRateLimiter = jest.fn().mockReturnValue((_req: any, _res: any, next: any) => next()) |
| 22 | + |
| 23 | +jest.mock('../../utils/rateLimit', () => ({ |
| 24 | + RateLimiterManager: { |
| 25 | + getInstance: () => ({ |
| 26 | + getRateLimiter: () => mockGetRateLimiter() |
| 27 | + }) |
| 28 | + } |
| 29 | +})) |
| 30 | + |
| 31 | +jest.mock('../../utils/logger', () => ({ |
| 32 | + __esModule: true, |
| 33 | + default: { |
| 34 | + debug: jest.fn(), |
| 35 | + info: jest.fn(), |
| 36 | + warn: jest.fn(), |
| 37 | + error: jest.fn() |
| 38 | + } |
| 39 | +})) |
| 40 | + |
| 41 | +// Import after mocking |
| 42 | +import mcpEndpointController from '.' |
| 43 | + |
| 44 | +// Helper: create mock Express objects |
| 45 | +function mockReq(overrides: Record<string, any> = {}): Request { |
| 46 | + return { |
| 47 | + params: { chatflowId: 'flow-123' }, |
| 48 | + headers: {}, |
| 49 | + query: {}, |
| 50 | + get: jest.fn(), |
| 51 | + ...overrides |
| 52 | + } as unknown as Request |
| 53 | +} |
| 54 | + |
| 55 | +function mockRes(): Response { |
| 56 | + const res: any = { |
| 57 | + status: jest.fn().mockReturnThis(), |
| 58 | + json: jest.fn().mockReturnThis(), |
| 59 | + locals: {} |
| 60 | + } |
| 61 | + return res as Response |
| 62 | +} |
| 63 | + |
| 64 | +function mockNext(): NextFunction { |
| 65 | + return jest.fn() |
| 66 | +} |
| 67 | + |
| 68 | +beforeEach(() => { |
| 69 | + jest.clearAllMocks() |
| 70 | +}) |
| 71 | + |
| 72 | +describe('MCP Endpoint Controller', () => { |
| 73 | + describe('authenticateToken', () => { |
| 74 | + it('returns 401 when Authorization header is missing', () => { |
| 75 | + const req = mockReq({ headers: {} }) |
| 76 | + const res = mockRes() |
| 77 | + const next = mockNext() |
| 78 | + |
| 79 | + mcpEndpointController.authenticateToken(req, res, next) |
| 80 | + |
| 81 | + expect(res.status).toHaveBeenCalledWith(401) |
| 82 | + expect(res.json).toHaveBeenCalledWith( |
| 83 | + expect.objectContaining({ |
| 84 | + jsonrpc: '2.0', |
| 85 | + error: expect.objectContaining({ code: -32001 }) |
| 86 | + }) |
| 87 | + ) |
| 88 | + expect(next).not.toHaveBeenCalled() |
| 89 | + }) |
| 90 | + |
| 91 | + it('returns 401 when Authorization header is not Bearer', () => { |
| 92 | + const req = mockReq({ headers: { authorization: 'Basic dXNlcjpwYXNz' } }) |
| 93 | + const res = mockRes() |
| 94 | + const next = mockNext() |
| 95 | + |
| 96 | + mcpEndpointController.authenticateToken(req, res, next) |
| 97 | + |
| 98 | + expect(res.status).toHaveBeenCalledWith(401) |
| 99 | + expect(next).not.toHaveBeenCalled() |
| 100 | + }) |
| 101 | + |
| 102 | + it('returns 401 when Bearer token is empty', () => { |
| 103 | + const req = mockReq({ headers: { authorization: 'Bearer ' } }) |
| 104 | + const res = mockRes() |
| 105 | + const next = mockNext() |
| 106 | + |
| 107 | + mcpEndpointController.authenticateToken(req, res, next) |
| 108 | + |
| 109 | + expect(res.status).toHaveBeenCalledWith(401) |
| 110 | + expect(next).not.toHaveBeenCalled() |
| 111 | + }) |
| 112 | + |
| 113 | + it('sets res.locals.token and calls next on valid Bearer token', () => { |
| 114 | + const req = mockReq({ headers: { authorization: 'Bearer my-secret-token' } }) |
| 115 | + const res = mockRes() |
| 116 | + const next = mockNext() |
| 117 | + |
| 118 | + mcpEndpointController.authenticateToken(req, res, next) |
| 119 | + |
| 120 | + expect(res.locals.token).toBe('my-secret-token') |
| 121 | + expect(next).toHaveBeenCalled() |
| 122 | + expect(res.status).not.toHaveBeenCalled() |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + describe('handlePost', () => { |
| 127 | + it('calls service with chatflowId and token from res.locals.token', async () => { |
| 128 | + const req = mockReq({ params: { chatflowId: 'flow-123' } }) |
| 129 | + const res = mockRes() |
| 130 | + res.locals.token = 'my-secret-token' |
| 131 | + const next = mockNext() |
| 132 | + mockHandleMcpRequest.mockResolvedValue(undefined) |
| 133 | + |
| 134 | + await mcpEndpointController.handlePost(req, res, next) |
| 135 | + |
| 136 | + expect(mockHandleMcpRequest).toHaveBeenCalledWith('flow-123', 'my-secret-token', req, res) |
| 137 | + }) |
| 138 | + |
| 139 | + it('calls next(error) on unexpected errors', async () => { |
| 140 | + const req = mockReq({ params: { chatflowId: 'flow-123' } }) |
| 141 | + const res = mockRes() |
| 142 | + res.locals.token = 'token' |
| 143 | + const next = mockNext() |
| 144 | + const error = new Error('Unexpected') |
| 145 | + mockHandleMcpRequest.mockRejectedValue(error) |
| 146 | + |
| 147 | + await mcpEndpointController.handlePost(req, res, next) |
| 148 | + |
| 149 | + expect(next).toHaveBeenCalledWith(error) |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + describe('handleDelete', () => { |
| 154 | + it('delegates to handleMcpDeleteRequest with chatflowId', async () => { |
| 155 | + const req = mockReq({ params: { chatflowId: 'flow-789' } }) |
| 156 | + const res = mockRes() |
| 157 | + const next = mockNext() |
| 158 | + mockHandleMcpDeleteRequest.mockResolvedValue(undefined) |
| 159 | + |
| 160 | + await mcpEndpointController.handleDelete(req, res, next) |
| 161 | + |
| 162 | + expect(mockHandleMcpDeleteRequest).toHaveBeenCalledWith('flow-789', req, res) |
| 163 | + }) |
| 164 | + }) |
| 165 | + |
| 166 | + describe('getRateLimiterMiddleware', () => { |
| 167 | + it('delegates to RateLimiterManager', async () => { |
| 168 | + const req = mockReq() |
| 169 | + const res = mockRes() |
| 170 | + const next = mockNext() |
| 171 | + |
| 172 | + await mcpEndpointController.getRateLimiterMiddleware(req, res, next) |
| 173 | + |
| 174 | + expect(mockGetRateLimiter).toHaveBeenCalled() |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
0 commit comments