fix(qa): 第五轮系统性质量保障缺陷修复 + 单元/集成测试

安全/鉴权:登出清服务端 httpOnly 令牌、登录 IP 限流前置 bcrypt、停用账号
令牌在 6 个内联 role handler 失效、admin/items 存在性 oracle 认证前置、
同意偏好 sanitize fail-closed、api-crypto 按 HTTP 方法判体。

CMS:workflow 状态更新+审计收进交互式 $transaction(失败即回滚)、编辑器
richtext 走 textarea 防换行损毁、array/object 子字段按作用域读写防污染、
about/contact/erp-upgrade 补 ISR revalidate 与路由映射。

UI/a11y:AnimatedCounter 入视口前显起点、CLS 累计上报、MetricCard 动画入
effect 并卸载取消 rAF、后台状态四态映射/搜索防抖+序号守卫/媒体单 input/
吞错改横幅/表单 label-aria 关联。

新增对应判别性单测与 pagination/rate-limit/client-ip/sanitize/validate-content-data
等模块及 tests-integration 真库集成层。
This commit is contained in:
zhangxiang committed 2026-09-28 10:48:07 +08:00
1 parent 040951c0a3
commit a366bd1400
224 files changed
+8938 -5579

No files matched your search

+13 -1
View File
@@ -26,6 +26,18 @@ import {
} from 'lucide-react';
import { ThemeToggle } from '@/components/theme/theme-toggle';
// 令牌的 role claim 有两个来源:login 签发 User.role 标量(取值见 prisma/seed.ts:199,313,324 与
// schema 默认值 'admin'),refresh 则按 UserRole.roleCode 重签(B-7 修复,refresh/route.ts:31)。
// 侧栏必须同时认这两套取值,否则刷新后的 super_admin 会掉进 else 分支显示成「编辑」。
const ROLE_LABELS: Record<string, string> = {
admin: '管理员',
super_admin: '超级管理员',
content_admin: '内容管理员',
content_editor: '内容编辑',
reviewer: '审核员',
readonly: '只读用户',
};
const menuItems = [
{
label: '内容管理',
@@ -238,7 +250,7 @@ export function AdminLayout({ children }: { children: React.ReactNode }) {
<p className="text-sm font-medium text-text-primary truncate">
{user?.nickname || user?.username}
</p>
<p className="text-xs text-text-muted">{user?.role === 'admin' ? '管理员' : '编辑'}</p>
<p className="text-xs text-text-muted">{ROLE_LABELS[user?.role ?? ''] ?? '编辑'}</p>
</div>
<button
onClick={handleLogout}
@@ -0,0 +1,61 @@
import { renderHook, act, waitFor } from '@testing-library/react';
import type { ReactNode } from 'react';
import { AuthProvider, useAuth } from '@/components/admin/auth-context';
const push = jest.fn();
jest.mock('next/navigation', () => ({ useRouter: () => ({ push }) }));
describe('AuthContext.logout', () => {
let fetchMock: jest.Mock;
const wrapper = ({ children }: { children: ReactNode }) => (
<AuthProvider>{children}</AuthProvider>
);
beforeEach(() => {
push.mockClear();
fetchMock = jest.fn().mockResolvedValue({ ok: true, json: async () => ({}) });
global.fetch = fetchMock;
localStorage.setItem('novalon_admin_token', 'tok');
localStorage.setItem(
'novalon_admin_user',
JSON.stringify({ id: '1', username: 'a', nickname: 'A', role: 'x', avatar: '' }),
);
});
afterEach(() => {
localStorage.clear();
});
it('清除服务端 httpOnly cookie(调用 /api/auth/logout)并本地状态与跳转', async () => {
const { result } = renderHook(() => useAuth(), { wrapper });
await waitFor(() => expect(result.current.loading).toBe(false));
act(() => {
result.current.logout();
});
// 关键判据:若不调用 logout 端点,clearTokenCookie 永不触发,
// authenticateRequest 会凭 cookie 在登出后继续放行全站受守卫 API 至多 24h。
expect(fetchMock).toHaveBeenCalledWith(
'/api/auth/logout',
expect.objectContaining({ method: 'POST' }),
);
expect(localStorage.getItem('novalon_admin_token')).toBeNull();
expect(localStorage.getItem('novalon_admin_user')).toBeNull();
expect(push).toHaveBeenCalledWith('/admin/login');
});
it('即使网络失败也清理本地状态并跳转', async () => {
fetchMock.mockRejectedValueOnce(new Error('network down'));
const { result } = renderHook(() => useAuth(), { wrapper });
await waitFor(() => expect(result.current.loading).toBe(false));
await act(async () => {
result.current.logout();
});
expect(localStorage.getItem('novalon_admin_token')).toBeNull();
expect(push).toHaveBeenCalledWith('/admin/login');
});
});
+9
View File
@@ -72,6 +72,15 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}, []);
const logout = useCallback(() => {
// 必须请求 /api/auth/logout 让服务端 clearTokenCookie 下发 Max-Age=0 的 Set-Cookie。
// 登录时 httpOnly 的 novalon_token(24h)/novalon_refresh(7d) 由 login 路由写入,
// 而 authenticateRequest 会回退读取 cookie —— 仅清 localStorage 无法真正登出,
// 受守卫 API 会在登出后仍凭 cookie 放行至多 24h。
try {
fetch('/api/auth/logout', { method: 'POST' }).catch(() => {});
} catch {
// 网络不可用也要继续清理本地并跳转
}
setToken(null);
setUser(null);
localStorage.removeItem('novalon_admin_token');