2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
104 lines
2.9 KiB
JavaScript
104 lines
2.9 KiB
JavaScript
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,
|
|
);
|
|
}
|
|
});
|