Files
SupplierSystem/src/utils/workflowApi.unauthorized.test.ts
T
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

123 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
/**
* FE-1(2026-10-19):`workflowApi` 的 401 必须与 `localApiClient` 共用同一登出处理。
*
* 缺陷:workflow 请求层 401 只 throw 普通 Error,不清 session、不跳登录页、错误也没有
* status/code ⇒ 页面停在旧数据上,用户不知道要重新登录(与 localApiClient 行为不一致)。
* 修复:401 ⇒ `handleUnauthorized()`(清 session + 跳 `/login?reason=expired`;**无 session 不跳**),
* 抛出的错误带 `status:401` 与后端 `error` 字段 code。
*/
const SESSION_KEY = 'canteen.auth.session';
async function loadWorkflowApi() {
vi.resetModules();
return import('../utils/workflowApi');
}
function installLocationStub(): { href: string } {
const loc = { href: '' };
try {
Object.defineProperty(window, 'location', { configurable: true, value: loc });
} catch {
// 极端 jsdom 版本不可重定义时,退化为直接赋值观察
(window as unknown as { location: { href: string } }).location = loc;
}
return loc;
}
function mockUnauthorized(): void {
vi.stubGlobal(
'fetch',
vi.fn(async () => ({
ok: false,
status: 401,
json: async () => ({ error: 'token_expired', error_description: '登录已过期,请重新登录' }),
}))
);
}
beforeEach(() => {
localStorage.clear();
vi.unstubAllGlobals();
});
afterEach(() => {
vi.unstubAllGlobals();
localStorage.clear();
});
describe('B-10.1:raw fetch(文件上传)401 也走统一登出', () => {
it('uploadPickingOutboundPhotoApi 401 ⇒ 清 session、跳一次登录、错误带 status/code', async () => {
const { setStoredSession } = await import('../lib/localApiClient');
setStoredSession({ access_token: 't-upload', user: { id: 'u1' } } as never);
const loc = installLocationStub();
vi.stubGlobal(
'fetch',
vi.fn(async () => ({ ok: false, status: 401, json: async () => ({ error: 'token_expired' }) }))
);
const { uploadPickingOutboundPhotoApi } = await loadWorkflowApi();
const file = new File(['x'], 'a.jpg', { type: 'image/jpeg' });
const error = await uploadPickingOutboundPhotoApi(file).catch((err) => err);
expect(error).toMatchObject({ status: 401, code: 'token_expired' });
expect(localStorage.getItem(SESSION_KEY)).toBeNull();
expect(loc.href).toContain('/login');
expect(loc.href).toContain('reason=expired');
});
});
describe('workflowApi 401:与 localApiClient 共用登出处理', () => {
it('有 session ⇒ 清 session、跳 /login?reason=expired、错误带 status=401 与 code', async () => {
const { setStoredSession } = await import('../lib/localApiClient');
setStoredSession({ access_token: 't-1', user: { id: 'u1' } } as never);
expect(localStorage.getItem(SESSION_KEY)).toBeTruthy();
const loc = installLocationStub();
mockUnauthorized();
const { submitInspectionComplianceApi } = await loadWorkflowApi();
const error = await submitInspectionComplianceApi({}).catch((err) => err);
expect(error).toMatchObject({ status: 401, code: 'token_expired' });
expect(localStorage.getItem(SESSION_KEY), '401 后必须清掉本地 session').toBeNull();
expect(loc.href).toContain('/login');
expect(loc.href).toContain('reason=expired');
});
it('401 且 body 为 JSON null ⇒ 不崩、仍登出、错误带 status=401/code=unauthorized', async () => {
const { setStoredSession } = await import('../lib/localApiClient');
setStoredSession({ access_token: 't-null', user: { id: 'u1' } } as never);
const loc = installLocationStub();
vi.stubGlobal(
'fetch',
vi.fn(async () => ({ ok: false, status: 401, body: null, json: async () => null }))
);
const { submitInspectionComplianceApi } = await loadWorkflowApi();
const error = await submitInspectionComplianceApi({}).catch((err) => err);
expect(error, 'body=null 时不得因读 payload.error 抛 TypeError').toMatchObject({
status: 401,
code: 'unauthorized',
});
expect(localStorage.getItem(SESSION_KEY)).toBeNull();
expect(loc.href).toContain('/login');
expect(loc.href).toContain('reason=expired');
});
it('无 session(登录页本身 401)⇒ 不跳转、不清数据', async () => {
const loc = installLocationStub();
mockUnauthorized();
const { submitInspectionComplianceApi } = await loadWorkflowApi();
const error = await submitInspectionComplianceApi({}).catch((err) => err);
expect(error).toMatchObject({ status: 401 });
expect(loc.href, '无 session 时不得把登录页再跳走').toBe('');
expect(localStorage.getItem(SESSION_KEY)).toBeNull();
});
});