Files
monkey-ai-web/src/helpers/requestDedupe.test.js
T
2026-10-15 16:33:46 +08:00

63 lines
2.2 KiB
JavaScript

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import test from 'node:test';
import assert from 'node:assert/strict';
import { buildGetRequestKey, getHeaderValue } from './requestDedupe.js';
test('GET dedupe key isolates requests by enterprise header', () => {
const orgA = buildGetRequestKey('/api/org/data-security/config', {
headers: { 'X-Org-Id': '101' },
});
const orgB = buildGetRequestKey('/api/org/data-security/config', {
headers: { 'x-org-id': '202' },
});
assert.notEqual(orgA, orgB);
assert.match(orgA, /org=101/);
assert.match(orgB, /org=202/);
});
test('GET dedupe key uses the current enterprise when the header is injected later', () => {
const orgA = buildGetRequestKey('/api/org/info', {}, '101');
const orgB = buildGetRequestKey('/api/org/info', {}, '202');
assert.notEqual(orgA, orgB);
});
test('header lookup supports AxiosHeaders-style accessors', () => {
const headers = { get: (name) => (name === 'X-Org-Id' ? 303 : undefined) };
assert.equal(getHeaderValue(headers, 'X-Org-Id'), '303');
});
test('GET dedupe key isolates users and explicit authorization', () => {
const userA = buildGetRequestKey('/api/org/info', {}, '101', '7');
const userB = buildGetRequestKey('/api/org/info', {}, '101', '8');
const bearerA = buildGetRequestKey('/api/data', {
headers: { Authorization: 'Bearer token-a' },
});
const bearerB = buildGetRequestKey('/api/data', {
headers: { Authorization: 'Bearer token-b' },
});
assert.notEqual(userA, userB);
assert.notEqual(bearerA, bearerB);
});