Files
monkey-ai-web/test/unit/post-login-redirect.test.js
T
zhanjunhao 13105bdba5 fix(ui): 修复登录与并发请求并统一表格、主题和页面展示
验证:868 项单测、ESLint、Prettier、UI 依赖审计和差异检查通过。
2026-11-19 18:58:46 +08:00

123 lines
3.6 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 assert from 'node:assert/strict';
import test, { afterEach, beforeEach } from 'node:test';
import {
consumePostLoginRedirect,
DEFAULT_LOGIN_REDIRECT,
savePostLoginRedirect,
} from '../../src/helpers/data.js';
beforeEach(() => {
const storage = new Map();
globalThis.sessionStorage = {
getItem: (key) => storage.get(key) ?? null,
setItem: (key, value) => storage.set(key, value),
removeItem: (key) => storage.delete(key),
};
});
afterEach(() => delete globalThis.sessionStorage);
test('direct login ignores and consumes an older saved target', () => {
savePostLoginRedirect('/user-center/usage-record');
assert.equal(
consumePostLoginRedirect({ redirect: null }),
DEFAULT_LOGIN_REDIRECT,
);
assert.equal(consumePostLoginRedirect(), DEFAULT_LOGIN_REDIRECT);
savePostLoginRedirect('/org/settings');
assert.equal(
consumePostLoginRedirect({ redirect: null, enterprise: true }),
'/org/workbench',
);
});
test('login drops incompatible and error destinations without bypassing guards', () => {
for (const redirect of [
'/org/settings',
'/Org/settings/',
'/%6frg/settings',
'/foo/../org/settings',
'/forbidden?from=login',
]) {
assert.equal(
consumePostLoginRedirect({ redirect }),
DEFAULT_LOGIN_REDIRECT,
);
}
for (const redirect of [
'/console/setting',
'/Console/setting',
'/forbidden',
]) {
assert.equal(
consumePostLoginRedirect({ redirect, enterprise: true }),
'/org/workbench',
);
assert.equal(consumePostLoginRedirect({ redirect, orgOnly: true }), '/org');
}
});
test('allowed deep links preserve query and hash; OAuth cache is single-use', () => {
const personalLink = '/user-center/usage-record?filter=1#results';
const enterpriseLink = '/org/settings?tab=members#top';
const oauthLink = '/client/authorize?destination=desktop';
assert.equal(
consumePostLoginRedirect({ redirect: personalLink }),
personalLink,
);
assert.equal(
consumePostLoginRedirect({ redirect: enterpriseLink, enterprise: true }),
enterpriseLink,
);
assert.equal(
consumePostLoginRedirect({ redirect: enterpriseLink, orgOnly: true }),
enterpriseLink,
);
assert.equal(
consumePostLoginRedirect({ redirect: '/console/setting' }),
'/console/setting',
);
savePostLoginRedirect(oauthLink);
assert.equal(consumePostLoginRedirect(), oauthLink);
assert.equal(consumePostLoginRedirect(), DEFAULT_LOGIN_REDIRECT);
savePostLoginRedirect(oauthLink);
savePostLoginRedirect(null);
assert.equal(consumePostLoginRedirect(), DEFAULT_LOGIN_REDIRECT);
});
test('malformed, external and authentication targets fall back', () => {
for (const redirect of [
'/\t/another.example',
'/%FF',
'//another.example',
'/LOGIN',
'/org/example/login',
'/user/reset',
]) {
assert.equal(
consumePostLoginRedirect({ redirect }),
DEFAULT_LOGIN_REDIRECT,
);
}
});