来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
164 lines
7.6 KiB
TypeScript
164 lines
7.6 KiB
TypeScript
/**
|
||
* 登录落地路径一致性契约。
|
||
*
|
||
* 起因(2026-09-15):同一个「用户该落到哪个首页」的逻辑存在三份实现,且互相矛盾 ——
|
||
* - 后端 src/services/accountLoginService.ts resolveWebHomePath:super_admin → /system/config
|
||
* - 前端 src/utils/authRouting.ts getDefaultHomePath:super_admin → /dashboard ← 错
|
||
* - 前端 src/utils/unifiedLogin.ts resolveAccountHomePath:super_admin → /dashboard ← 错
|
||
* 而 /dashboard 的路由守卫是 roles: ['admin'](不含 super_admin),超管 roles 只有 'super_admin',
|
||
* 于是 App.tsx「无权限 → 跳回默认首页」会把超管打回 /dashboard,形成自环。
|
||
*
|
||
* 定案:**超管默认界面是「系统设置」(/system/config)**。
|
||
* 本测试同时守住那条更本质的不变量:默认首页必须是该角色真能访问的路由。
|
||
*/
|
||
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
const REPO_ROOT = path.join(__dirname, '../..');
|
||
const read = (p: string) => readFileSync(path.join(REPO_ROOT, p), 'utf8');
|
||
|
||
const BACKEND_HOME_SRC = 'server/src/services/accountLoginService.ts';
|
||
const AUTH_ROUTING_SRC = 'src/utils/authRouting.ts';
|
||
const UNIFIED_LOGIN_SRC = 'src/utils/unifiedLogin.ts';
|
||
const FRONTEND_MODULES_SRC = 'src/modules/businessModules.ts';
|
||
|
||
/** 超管与管理员的目标落地路径(定案值) */
|
||
const SUPER_ADMIN_HOME = '/system/config';
|
||
const ADMIN_HOME = '/dashboard';
|
||
const PURCHASER_HOME = '/purchaser/dashboard';
|
||
/** 「无任何可进页面」账号的兜底落地路径 —— 该页不得声明任何权限维度 */
|
||
const NO_ACCESS_HOME = '/no-access';
|
||
|
||
function extract(src: string, re: RegExp, label: string): string {
|
||
const m = re.exec(src);
|
||
if (!m) throw new Error(`未能在 ${label} 中解析出落地路径,源码结构可能已变更`);
|
||
return m[1];
|
||
}
|
||
|
||
const backendHome = (role: 'super_admin' | 'admin' | 'purchaser') =>
|
||
extract(
|
||
read(BACKEND_HOME_SRC),
|
||
new RegExp(`roleSet\\.has\\('${role}'\\)[^\\n]*return '([^']+)'`),
|
||
BACKEND_HOME_SRC
|
||
);
|
||
|
||
const routingHome = (role: string) =>
|
||
extract(read(AUTH_ROUTING_SRC), new RegExp(`hasRole\\('${role}'\\)\\) return '([^']+)'`), AUTH_ROUTING_SRC);
|
||
|
||
const unifiedLoginHome = (role: string) =>
|
||
extract(
|
||
read(UNIFIED_LOGIN_SRC),
|
||
new RegExp(`roles\\?\\.includes\\('${role}'\\)\\)[^\\n]*\\n\\s*return '([^']+)'`),
|
||
UNIFIED_LOGIN_SRC
|
||
);
|
||
|
||
/** 从路由表里取某个 path 条目的 roles 数组 */
|
||
function routeRoles(routePath: string, pageName: string): string[] {
|
||
const src = read(FRONTEND_MODULES_SRC);
|
||
const m = new RegExp(`path: '${routePath}',[^}]*page: '${pageName}'[^}]*roles: \\[([^\\]]*)\\]`).exec(src);
|
||
if (!m) throw new Error(`未能在 ${FRONTEND_MODULES_SRC} 找到 ${routePath} 的路由守卫 roles`);
|
||
return m[1]
|
||
.split(',')
|
||
.map((s) => s.trim().replace(/^'|'$/g, ''))
|
||
.filter(Boolean);
|
||
}
|
||
|
||
describe('登录落地路径三处实现一致性', () => {
|
||
it('超管的落地路径三处一致且为「系统设置」', () => {
|
||
expect(backendHome('super_admin')).toBe(SUPER_ADMIN_HOME);
|
||
expect(routingHome('super_admin')).toBe(SUPER_ADMIN_HOME);
|
||
expect(unifiedLoginHome('super_admin')).toBe(SUPER_ADMIN_HOME);
|
||
});
|
||
|
||
it('管理员的落地路径三处一致且为数据看板', () => {
|
||
expect(backendHome('admin')).toBe(ADMIN_HOME);
|
||
expect(routingHome('admin')).toBe(ADMIN_HOME);
|
||
expect(unifiedLoginHome('admin')).toBe(ADMIN_HOME);
|
||
});
|
||
|
||
it('采购员的落地路径三处一致', () => {
|
||
expect(backendHome('purchaser')).toBe(PURCHASER_HOME);
|
||
expect(routingHome('purchaser')).toBe(PURCHASER_HOME);
|
||
});
|
||
|
||
it('不变量:落地路径必须是该角色真能访问的路由(否则自环)', () => {
|
||
const dashboardRoles = routeRoles('dashboard', 'Dashboard');
|
||
const systemConfigRoles = routeRoles('config', 'SystemConfig');
|
||
|
||
// /dashboard 只放行 admin:超管落过去必然被 App.tsx 打回默认首页 → 自环
|
||
expect(dashboardRoles).not.toContain('super_admin');
|
||
expect(dashboardRoles).toContain('admin');
|
||
expect(SUPER_ADMIN_HOME).not.toBe('/dashboard');
|
||
|
||
// 超管的落地路由必须放行 super_admin
|
||
expect(systemConfigRoles).toContain('super_admin');
|
||
});
|
||
|
||
describe('2026-09-15 补:门户残骸账号的自环(原修复只覆盖了超管)', () => {
|
||
/** 客户门户账号的落地路径:后端按 kinds.has('customer') 判定 */
|
||
const backendCustomerHome = () =>
|
||
extract(
|
||
read(BACKEND_HOME_SRC),
|
||
/kinds\.has\('customer'\) && !roleSet\.size\) return '([^']+)'/,
|
||
`${BACKEND_HOME_SRC} 的 customer 分支`
|
||
);
|
||
|
||
/** 后端最终兜底(无任何权限命中时):取函数内最后一个 return '...' */
|
||
const backendFallbackHome = () => {
|
||
const src = read(BACKEND_HOME_SRC);
|
||
const fnStart = src.indexOf('export async function resolveWebHomePath');
|
||
if (fnStart < 0) throw new Error(`未能在 ${BACKEND_HOME_SRC} 找到 resolveWebHomePath`);
|
||
const body = src.slice(fnStart, src.indexOf('\n}', fnStart));
|
||
const hits = [...body.matchAll(/return '([^']+)';/g)].map((m) => m[1]);
|
||
if (!hits.length) throw new Error(`未能在 ${BACKEND_HOME_SRC} 的 resolveWebHomePath 中解析出 return`);
|
||
return hits[hits.length - 1];
|
||
};
|
||
|
||
/** 前端 getDefaultHomePath 最终兜底 */
|
||
const routingFallbackHome = () => {
|
||
const src = read(AUTH_ROUTING_SRC);
|
||
const fnStart = src.indexOf('export function getDefaultHomePath');
|
||
if (fnStart < 0) throw new Error(`未能在 ${AUTH_ROUTING_SRC} 找到 getDefaultHomePath`);
|
||
const body = src.slice(fnStart, src.indexOf('\n}', fnStart));
|
||
const hits = [...body.matchAll(/return '([^']+)';/g)].map((m) => m[1]);
|
||
if (!hits.length) throw new Error(`未能在 ${AUTH_ROUTING_SRC} 的 getDefaultHomePath 中解析出 return`);
|
||
return hits[hits.length - 1];
|
||
};
|
||
|
||
/** unifiedLogin 登录成功返回处对后端 home_path 的兜底 */
|
||
const unifiedLoginApiFallback = () => {
|
||
const src = read(UNIFIED_LOGIN_SRC);
|
||
const m = /homePath: result\.home_path \|\| '([^']+)'/.exec(src);
|
||
if (!m) throw new Error(`未能在 ${UNIFIED_LOGIN_SRC} 解析出 result.home_path 的兜底`);
|
||
return m[1];
|
||
};
|
||
|
||
it('客户门户账号不再落到需要权限的 /customer-order', () => {
|
||
// /customer-order 守卫要求 customer_order/order_entry,门户账号两者皆无
|
||
expect(backendCustomerHome()).not.toBe('/customer-order');
|
||
});
|
||
|
||
it('四处兜底一致且都指向不需要权限的页面', () => {
|
||
const unifiedSrc = read(UNIFIED_LOGIN_SRC);
|
||
const portalHit = /customerPortal && !profile\?\.roles\?\.length\) return '([^']+)'/.exec(unifiedSrc);
|
||
expect(portalHit?.[1]).toBe(NO_ACCESS_HOME);
|
||
expect(backendCustomerHome()).toBe(NO_ACCESS_HOME);
|
||
expect(backendFallbackHome()).toBe(NO_ACCESS_HOME);
|
||
expect(routingFallbackHome()).toBe(NO_ACCESS_HOME);
|
||
// 第 4 处:登录接口返回的 home_path 缺失时的兜底,也必须指向无权页
|
||
expect(unifiedLoginApiFallback()).toBe(NO_ACCESS_HOME);
|
||
});
|
||
|
||
it('兜底页 /no-access 真实存在且不声明任何权限维度', () => {
|
||
const appSrc = read('src/App.tsx');
|
||
const m = /path="\/no-access"[\s\S]{0,400}?<ProtectedRoute([^>]*)>/.exec(appSrc);
|
||
expect(m, 'App.tsx 中未注册 /no-access 路由').not.toBeNull();
|
||
// 不得带 permission= / roles=,否则又会变成「落地到需要权限的页面」
|
||
expect(m![1]).not.toMatch(/permission=/);
|
||
expect(m![1]).not.toMatch(/roles=/);
|
||
expect(read('src/pages/auth/NoAccess.tsx')).toContain('export const NoAccess');
|
||
});
|
||
});
|
||
});
|