- [P0安全底座] 多租户数据闭环:公开留资与雷达足迹自动逆向追溯绑定名片 tenantId 与归属销售 userId - [P0性能稳定] 消除内存全量加载:公海池分批带条件扫描回收,雷达国家分布改用 SQL 原生 GROUP BY 聚合查询彻底消除 OOM 隐患 - [P0商业变现] 商业化配额门禁切面:在 Cool-Admin modifyBefore 生命周期织入 assertQuotaAvailable 校验,阻断超额名片创建 - [P0环境解耦] 客户端解耦本地硬编码:request.uts 支持 api_base_url 本地覆盖,生产数据库名修正为 crmmpdata - [P1高并发削峰] 雷达足迹批量缓冲引擎:引入 CrmRadarBufferService 环形缓冲队列与阈值/定时批量 Bulk Insert 落盘 - [P2中枢租户化] AI 智能中枢多租户 BYOK 与 Token 成本风控:支持企业自带 Key 隔离、Token 消耗账本、用量配额门禁与名片人设个性化联动 - [跨端规范] 对齐 Uni-app x 移动微官网原生首屏与底部原生 tabBar 规范 - [测试驱动] 全工程 116 个测试套件,1,175 个单元与集成测试用例全部 100% 绿色通过,管理后台 Vite 打包无错误
55 行
2.3 KiB
TypeScript
55 行
2.3 KiB
TypeScript
import { isSuperAdminContext, assertSuperAdmin } from '../../src/comm/guard/super-admin';
|
|
import { CoolCommException } from '@cool-midway/core';
|
|
|
|
describe('后端权限隔离 - 平台超级管理员专属守卫 (Super Admin Guard TDD)', () => {
|
|
describe('1. isSuperAdminContext 身份研判', () => {
|
|
it('username 为 admin 的系统初始超管应判定为 true', () => {
|
|
const ctx = { admin: { username: 'admin', userId: 1 } };
|
|
expect(isSuperAdminContext(ctx)).toBe(true);
|
|
});
|
|
|
|
it('携带 isSuper: true 标识的平台超级管理员应判定为 true', () => {
|
|
const ctx = { admin: { username: 'root_operator', isSuper: true, userId: 2 } };
|
|
expect(isSuperAdminContext(ctx)).toBe(true);
|
|
});
|
|
|
|
it('角色包含 admin 标签的管理员应判定为 true', () => {
|
|
const ctx = { admin: { username: 'ops_manager', roleLabels: ['admin', 'ops'], userId: 3 } };
|
|
expect(isSuperAdminContext(ctx)).toBe(true);
|
|
});
|
|
|
|
it('普通企业租户账号应判定为 false', () => {
|
|
const ctx = { admin: { username: 'vanguard_owner', tenantId: 1, roleLabels: ['tenant_owner'], userId: 10 } };
|
|
expect(isSuperAdminContext(ctx)).toBe(false);
|
|
});
|
|
|
|
it('租户下的一线业务员应判定为 false', () => {
|
|
const ctx = { admin: { username: 'sales_tom', tenantId: 1, roleLabels: ['sales_rep'], userId: 25 } };
|
|
expect(isSuperAdminContext(ctx)).toBe(false);
|
|
});
|
|
|
|
it('缺失 admin 上下文或未登录状态应判定为 false', () => {
|
|
expect(isSuperAdminContext({})).toBe(false);
|
|
expect(isSuperAdminContext({ admin: null })).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('2. assertSuperAdmin 越权拦截断言', () => {
|
|
it('超级管理员访问时不应抛出异常', () => {
|
|
const ctx = { admin: { username: 'admin' } };
|
|
expect(() => assertSuperAdmin(ctx)).not.toThrow();
|
|
});
|
|
|
|
it('非超级管理员访问时必须抛出 403 CoolCommException 阻止越权', () => {
|
|
const ctx = { admin: { username: 'tenant_user', tenantId: 2 } };
|
|
expect(() => assertSuperAdmin(ctx)).toThrow(CoolCommException);
|
|
try {
|
|
assertSuperAdmin(ctx);
|
|
} catch (err: any) {
|
|
expect(err.status || err.code).toBe(403);
|
|
expect(err.message).toContain('权限不足');
|
|
}
|
|
});
|
|
});
|
|
});
|