Files
BetterDesk/web-nodejs/tests/rateLimiter.test.js
T
UNITRONIX 493a27eaf3 feat(rateLimiter): add resolveApiPath function for accurate API path resolution
- Introduced resolveApiPath to handle cases where Express strips the `/api` prefix from req.path, ensuring correct path classification for rate-limiting.
- Updated isPanelPollRequest and isPanelPreferenceWrite functions to utilize resolveApiPath for improved accuracy.
- Added tests to verify the functionality of resolveApiPath and its integration with existing rate-limiting logic.
2026-07-26 10:30:44 +02:00

79 lines
3.0 KiB
JavaScript

/**
* Panel poll rate-limit classification tests
*/
const {
isPanelPollRequest,
isPanelPreferenceWrite,
resolveApiPath,
PANEL_POLL_PATHS
} = require('../middleware/rateLimiter');
describe('rateLimiter panel poll paths', () => {
it('classifies dashboard client-config GET as panel poll', () => {
expect(isPanelPollRequest({ method: 'GET', path: '/api/dashboard/client-config' })).toBe(true);
});
it('does not classify POST mutations as panel poll', () => {
expect(isPanelPollRequest({ method: 'POST', path: '/api/devices' })).toBe(false);
expect(isPanelPollRequest({ method: 'POST', path: '/api/dashboard/client-config' })).toBe(false);
});
it('includes core widget paths in the poll set', () => {
expect(PANEL_POLL_PATHS.has('/api/stats')).toBe(true);
expect(PANEL_POLL_PATHS.has('/api/dashboard/activity')).toBe(true);
});
it('includes devices page read paths in the poll set', () => {
expect(PANEL_POLL_PATHS.has('/api/folders')).toBe(true);
expect(PANEL_POLL_PATHS.has('/api/tags')).toBe(true);
expect(PANEL_POLL_PATHS.has('/api/device-groups')).toBe(true);
expect(PANEL_POLL_PATHS.has('/api/bd/notifications')).toBe(true);
});
it('classifies /api/panel/* GET as panel poll via prefix', () => {
expect(isPanelPollRequest({ method: 'GET', path: '/api/panel/strategies' })).toBe(true);
expect(isPanelPollRequest({ method: 'GET', path: '/api/panel/user-groups' })).toBe(true);
});
it('does not classify unrelated API paths as panel poll', () => {
expect(isPanelPollRequest({ method: 'GET', path: '/api/settings/info' })).toBe(false);
});
it('classifies desktop layout POST as panel preference write', () => {
expect(isPanelPreferenceWrite({ method: 'POST', path: '/api/desktop/layout' })).toBe(true);
expect(isPanelPreferenceWrite({ method: 'GET', path: '/api/desktop/layout' })).toBe(false);
expect(isPanelPreferenceWrite({ method: 'POST', path: '/api/devices' })).toBe(false);
});
it('rejoins /api mount prefix when Express strips it from req.path', () => {
// app.use('/api/', apiLimiter) → path=/bd/notifications, baseUrl=/api
expect(resolveApiPath({
path: '/bd/notifications',
baseUrl: '/api',
originalUrl: '/api/bd/notifications?limit=10'
})).toBe('/api/bd/notifications');
expect(isPanelPollRequest({
method: 'GET',
path: '/bd/notifications',
baseUrl: '/api',
originalUrl: '/api/bd/notifications?limit=10'
})).toBe(true);
expect(isPanelPollRequest({
method: 'GET',
path: '/registrations/count',
baseUrl: '/api',
originalUrl: '/api/registrations/count'
})).toBe(true);
expect(isPanelPreferenceWrite({
method: 'POST',
path: '/desktop/layout',
baseUrl: '/api',
originalUrl: '/api/desktop/layout'
})).toBe(true);
});
});