refactor(web): isAdmin 收口到 isAdminProfile(B4)
5 处逐字硬编码 profile?.account==='admin'||userId==='ADMIN'(Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage)收口到 auth/token.ts 纯函数 isAdminProfile,口径不变。
顺带:5 个页面测试的 vi.mock('../auth/token') 从整模块替换改为 importOriginal 只覆盖 getProfile(不再 mock 纯函数,修复 mock 漏 isAdminProfile 致的 render 崩)。
验证:新增 token.test.ts 4 测试 + tsc -b 干净 + vitest 18 文件/77 测试全绿。见总览 §10 B4。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,8 @@ import { vi, describe, it, expect } from 'vitest';
|
||||
import { NewFeatureShell } from './pages/NewFeatureShell';
|
||||
import { AiFeatureForm } from './pages/AiFeatureForm';
|
||||
|
||||
vi.mock('./auth/token', () => ({
|
||||
vi.mock('./auth/token', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('./auth/token')>()),
|
||||
getProfile: () => ({ userId: 'ADMIN', account: 'admin', name: 'x', roles: [] }),
|
||||
}));
|
||||
vi.mock('./api/meta', () => ({ listEntities: vi.fn().mockResolvedValue([]), createEntity: vi.fn() }));
|
||||
|
||||
23
src/auth/token.test.ts
Normal file
23
src/auth/token.test.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { isAdminProfile, type UserProfile } from './token';
|
||||
|
||||
const base: UserProfile = { userId: 'U1', account: 'user', name: '张三', roles: [] };
|
||||
|
||||
describe('isAdminProfile', () => {
|
||||
it('account 为 admin 判为管理员', () => {
|
||||
expect(isAdminProfile({ ...base, account: 'admin' })).toBe(true);
|
||||
});
|
||||
|
||||
it('userId 为 ADMIN 判为管理员', () => {
|
||||
expect(isAdminProfile({ ...base, userId: 'ADMIN' })).toBe(true);
|
||||
});
|
||||
|
||||
it('普通用户(account/userId 均非管理员)判否', () => {
|
||||
expect(isAdminProfile(base)).toBe(false);
|
||||
});
|
||||
|
||||
it('null / undefined 判否', () => {
|
||||
expect(isAdminProfile(null)).toBe(false);
|
||||
expect(isAdminProfile(undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -74,3 +74,11 @@ export function clearSession(): void {
|
||||
export function isAuthenticated(): boolean {
|
||||
return !!getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员判定(收口 Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage 5 处逐字硬编码,
|
||||
* 总览 §10.3)。口径与历史完全一致:account==='admin' 或 userId==='ADMIN'。
|
||||
*/
|
||||
export function isAdminProfile(profile: UserProfile | null | undefined): boolean {
|
||||
return profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
}
|
||||
|
||||
@@ -6,7 +6,10 @@ import { vi, describe, it, expect } from 'vitest';
|
||||
// AiFeatureForm 现以 amis form 渲染(<AmisRenderer>)。amis 内部(auto-develop 提交、ARRAYFILTER 取
|
||||
// 草稿 targetId、审阅按钮)由 Playwright e2e 覆盖;单测仅守 admin 守卫 + 容器/amis 挂载点。
|
||||
let mockProfile: { userId: string; account: string; name: string; roles: string[] } | null = null;
|
||||
vi.mock('../auth/token', () => ({ getProfile: () => mockProfile }));
|
||||
vi.mock('../auth/token', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../auth/token')>()),
|
||||
getProfile: () => mockProfile,
|
||||
}));
|
||||
vi.mock('../components/AmisRenderer', () => ({ AmisRenderer: () => <div data-testid="amis-mount" /> }));
|
||||
|
||||
import { AiFeatureForm } from './AiFeatureForm';
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// 故 api 用 adaptor 包成 {status:0,data:payload},再以 ARRAYFILTER 取 PAGE_SCHEMA 草稿 targetId。
|
||||
import { useMemo } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { getProfile } from '../auth/token';
|
||||
import { getProfile, isAdminProfile } from '../auth/token';
|
||||
import { AmisRenderer } from '../components/AmisRenderer';
|
||||
|
||||
const TYPE_OPTIONS = [
|
||||
@@ -90,7 +90,7 @@ function buildSchema(): Record<string, unknown> {
|
||||
export function AiFeatureForm() {
|
||||
const navigate = useNavigate();
|
||||
const profile = getProfile();
|
||||
const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
const isAdmin = isAdminProfile(profile);
|
||||
const schema = useMemo(buildSchema, []);
|
||||
|
||||
if (!isAdmin) {
|
||||
|
||||
@@ -12,7 +12,8 @@ vi.mock('../api/menu', () => ({
|
||||
|
||||
// 受控 profile:默认 admin
|
||||
const profileMock = vi.fn();
|
||||
vi.mock('../auth/token', () => ({
|
||||
vi.mock('../auth/token', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../auth/token')>()),
|
||||
getProfile: () => profileMock(),
|
||||
}));
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { fetchMenu, collectLeaves, type MenuLeaf } from '../api/menu';
|
||||
import { getProfile } from '../auth/token';
|
||||
import { getProfile, isAdminProfile } from '../auth/token';
|
||||
|
||||
interface Group {
|
||||
root: string;
|
||||
@@ -23,7 +23,7 @@ export function DesignCenterPage() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const profile = getProfile();
|
||||
const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
const isAdmin = isAdminProfile(profile);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
@@ -7,7 +7,10 @@ vi.mock('../api/meta', () => ({
|
||||
listEntityColumns: vi.fn(),
|
||||
createEntity: vi.fn(),
|
||||
}));
|
||||
vi.mock('../auth/token', () => ({ getProfile: vi.fn() }));
|
||||
vi.mock('../auth/token', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../auth/token')>()),
|
||||
getProfile: vi.fn(),
|
||||
}));
|
||||
|
||||
import { listEntities, createEntity } from '../api/meta';
|
||||
import { getProfile } from '../auth/token';
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// v2:可建新实体 + 给已有实体加字段(受控 ALTER 安全通道,只加列/恒可空)。字段表见同文件下方组件。
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Dialog } from '../components/ui/Dialog';
|
||||
import { getProfile } from '../auth/token';
|
||||
import { getProfile, isAdminProfile } from '../auth/token';
|
||||
import {
|
||||
listEntities,
|
||||
createEntity,
|
||||
@@ -19,7 +19,7 @@ export function validateTableName(t: string): boolean {
|
||||
|
||||
export function ModelerPage() {
|
||||
const profile = getProfile();
|
||||
const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
const isAdmin = isAdminProfile(profile);
|
||||
|
||||
const [entities, setEntities] = useState<EntitySummary[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
@@ -8,7 +8,10 @@ import { vi, describe, it, expect } from 'vitest';
|
||||
// (e2e/five-ring.spec、master-detail.spec) 覆盖。单测仅守:① admin 守卫;② admin 下先渲染页面类型选择卡;
|
||||
// ③ 选一种类型后渲染 amis 挂载点(wizard 壳)。
|
||||
let mockProfile: { userId: string; account: string; name: string; roles: string[] } | null = null;
|
||||
vi.mock('../auth/token', () => ({ getProfile: () => mockProfile }));
|
||||
vi.mock('../auth/token', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../auth/token')>()),
|
||||
getProfile: () => mockProfile,
|
||||
}));
|
||||
vi.mock('../components/AmisRenderer', () => ({ AmisRenderer: () => <div data-testid="amis-mount" /> }));
|
||||
|
||||
import { NewFeatureShell } from './NewFeatureShell';
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
// 红线:发布恒人审;standard 写入恒走后端 PageSchemaWriter(前端只调 /api/design/recipe/compile)。
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { getProfile } from '../auth/token';
|
||||
import { getProfile, isAdminProfile } from '../auth/token';
|
||||
import { AmisRenderer } from '../components/AmisRenderer';
|
||||
|
||||
const TYPE_OPTIONS = [
|
||||
@@ -279,7 +279,7 @@ function RecipeChooser({ onPick }: { onPick: (recipe: string) => void }) {
|
||||
export function NewFeatureShell() {
|
||||
const navigate = useNavigate();
|
||||
const profile = getProfile();
|
||||
const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
const isAdmin = isAdminProfile(profile);
|
||||
const [recipe, setRecipe] = useState<string | null>(null);
|
||||
const schema = useMemo(() => (recipe ? buildWizardSchema(recipe) : null), [recipe]);
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Icon } from '../components/ui/Icon';
|
||||
import { GlobalSearch } from './GlobalSearch';
|
||||
import type { MenuLeaf } from '../api/menu';
|
||||
import type { UserProfile } from '../auth/token';
|
||||
import { isAdminProfile } from '../auth/token';
|
||||
|
||||
interface TopbarProps {
|
||||
leaves: MenuLeaf[];
|
||||
@@ -30,7 +31,7 @@ export function Topbar({
|
||||
// 用户名显示:取 name 首字作头像
|
||||
const avatarChar = profile?.name?.[0] ?? '?';
|
||||
const navigate = useNavigate();
|
||||
const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
const isAdmin = isAdminProfile(profile);
|
||||
|
||||
return (
|
||||
<header className="topbar">
|
||||
|
||||
Reference in New Issue
Block a user