- 分公司类型模板:超管配置隐藏路由/菜单 + 中文文案覆盖,仅对挂类型的分公司生效(后端 service/routes、前端 Provider/守卫/配置页) - 超管模板 iframe 真还原预览:28 个业务页注册、白名单点选编辑、空/示例数据 fixture、白名单漂移检测工具 - 商品/SKU 下拉远程搜索:服务端关键词搜索 + 前端 useRemoteOptionSearch/SearchableSelect 接入(订单/采购/商品等页) - 主系统联调:子系统主动解绑主系统授权(幂等重试/本地兜底)、追加修改 price_amend 视图代理与 amend-price 转发 - 修复:仓内加工重复建单幂等(已完成需求单 guard、order 任务一商品一单 guard) - 修复:出入库记录只取前 100 条 → 服务端分页 + 真实总数 - 其它:库存/订单若干修复、分公司管理响应式布局、i18n 文案、设计文档与回归测试
235 lines
8.7 KiB
TypeScript
235 lines
8.7 KiB
TypeScript
/**
|
||
* 分公司类型模板路由层 · 边界补充(tester)。
|
||
*
|
||
* 只补 dev 的 branchUiRoutes.test.ts 未覆盖的授权/作用域/错误映射边:
|
||
* - templates / branches 写接口的非超管 403(dev 只测了超管 200);
|
||
* - admin-config 未登录 401;
|
||
* - 超管 effective 传空 `?branch_code=` ⇒ applied:false 且不查库;x-operating-branch 同样不生效;
|
||
* - 非超管同时带 `?branch_code=别家` 与 `x-operating-branch` 仍只读自己;
|
||
* - service 抛 409 / 无 status 异常时路由的 HTTP 映射。
|
||
*
|
||
* 只 mock 认证、ctx、service/audit;`requireSuperAdminRole` / `requireAdminRole` 用真实实现,
|
||
* 因此 403 是被测守卫的真实行为。
|
||
*/
|
||
import express from 'express';
|
||
import request from 'supertest';
|
||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||
import type { TenantContext } from '../src/services/tenantScope';
|
||
|
||
const mocks = vi.hoisted(() => ({
|
||
getWorkflowCtx: vi.fn(),
|
||
resolveEffectiveBranchUi: vi.fn(),
|
||
getBranchUiAdminConfig: vi.fn(),
|
||
saveBranchUiTypes: vi.fn(),
|
||
saveBranchUiTemplate: vi.fn(),
|
||
saveBranchUiBranchOverride: vi.fn(),
|
||
deleteBranchUiType: vi.fn(),
|
||
reopenBranchUiMenus: vi.fn(),
|
||
writeAuditLog: vi.fn(),
|
||
}));
|
||
|
||
vi.mock('../src/middleware/auth', () => ({
|
||
authMiddleware: (req: any, res: any, next: any) => {
|
||
if (!req.headers['x-test-user-id']) return res.status(401).json({ error: 'invalid_token' });
|
||
req.userId = String(req.headers['x-test-user-id']);
|
||
req.tokenScope = 'web';
|
||
next();
|
||
},
|
||
}));
|
||
|
||
vi.mock('../src/modules/workflow/context', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/modules/workflow/context')>();
|
||
return {
|
||
...actual,
|
||
getWorkflowCtx: (...args: unknown[]) => mocks.getWorkflowCtx(...args),
|
||
};
|
||
});
|
||
|
||
vi.mock('../src/services/auditService', () => ({
|
||
writeAuditLog: (...args: unknown[]) => mocks.writeAuditLog(...args),
|
||
}));
|
||
|
||
vi.mock('../src/services/branchUiConfigService', () => ({
|
||
BRANCH_UI_TYPES_KEY: 'branch_ui_types',
|
||
BRANCH_UI_BRANCH_KEY: 'branch_ui_branch',
|
||
BRANCH_UI_TEMPLATE_KEY_PREFIX: 'branch_ui_template.',
|
||
emptyBranchUiEffective: (branchCode: string | null) => ({
|
||
applied: false,
|
||
revision: null,
|
||
branch_code: branchCode,
|
||
hidden_menu_keys: [],
|
||
type_hidden_menu_keys: [],
|
||
reopened_menu_keys: [],
|
||
text_overrides: {},
|
||
updated_at: null,
|
||
revisions: null,
|
||
}),
|
||
resolveEffectiveBranchUi: (...args: unknown[]) => mocks.resolveEffectiveBranchUi(...args),
|
||
getBranchUiAdminConfig: (...args: unknown[]) => mocks.getBranchUiAdminConfig(...args),
|
||
saveBranchUiTypes: (...args: unknown[]) => mocks.saveBranchUiTypes(...args),
|
||
saveBranchUiTemplate: (...args: unknown[]) => mocks.saveBranchUiTemplate(...args),
|
||
saveBranchUiBranchOverride: (...args: unknown[]) => mocks.saveBranchUiBranchOverride(...args),
|
||
deleteBranchUiType: (...args: unknown[]) => mocks.deleteBranchUiType(...args),
|
||
reopenBranchUiMenus: (...args: unknown[]) => mocks.reopenBranchUiMenus(...args),
|
||
}));
|
||
|
||
import { mountBranchUiWorkflowRoutes } from '../src/modules/workflow/routes/branchUiRoutes';
|
||
|
||
function ctx(overrides: Partial<TenantContext> = {}): TenantContext {
|
||
return {
|
||
userId: 'U1',
|
||
roles: ['admin'],
|
||
permissions: [],
|
||
branchCode: 'SUP0001',
|
||
supplierCode: null,
|
||
purchaserCode: null,
|
||
isSuperAdmin: false,
|
||
isBranchAdmin: true,
|
||
isInternalStaff: true,
|
||
isSupplierAccount: false,
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
const SUPER_CTX = ctx({ userId: 'U-SUPER', roles: ['super_admin'], branchCode: null, isSuperAdmin: true, isBranchAdmin: false });
|
||
|
||
function buildApp() {
|
||
const app = express();
|
||
app.use(express.json());
|
||
const router = express.Router();
|
||
mountBranchUiWorkflowRoutes(router);
|
||
app.use('/api/workflow', router);
|
||
return app;
|
||
}
|
||
|
||
const app = buildApp();
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
mocks.getWorkflowCtx.mockResolvedValue(ctx());
|
||
mocks.resolveEffectiveBranchUi.mockResolvedValue({
|
||
applied: true,
|
||
revision: 'types:1,tpl:2,branch:3',
|
||
branch_code: 'SUP0001',
|
||
type: { code: 'retail', name: '零售型' },
|
||
hidden_menu_keys: [],
|
||
type_hidden_menu_keys: [],
|
||
reopened_menu_keys: [],
|
||
text_overrides: {},
|
||
updated_at: null,
|
||
revisions: { types: 1, template: 2, branch: 3 },
|
||
});
|
||
mocks.getBranchUiAdminConfig.mockResolvedValue({
|
||
types: { revision: 1, updated_at: null, updated_by: null, corrupted: false, types: [], assignments: {} },
|
||
templates: [],
|
||
branch_overrides: [],
|
||
branches: [],
|
||
menu_keys: [],
|
||
revision: 'types:1,tpl:0,branch:0',
|
||
});
|
||
mocks.saveBranchUiTypes.mockResolvedValue({ revision: 2 });
|
||
mocks.saveBranchUiTemplate.mockResolvedValue({ revision: 3, type_code: 'retail', hidden_count: 0, text_override_count: 0 });
|
||
mocks.saveBranchUiBranchOverride.mockResolvedValue({
|
||
revision: 2,
|
||
branch_code: 'SUP0001',
|
||
reopened_count: 0,
|
||
hidden_count: 0,
|
||
text_override_count: 0,
|
||
});
|
||
mocks.deleteBranchUiType.mockResolvedValue({ revision: 2, type_code: 'retail', removed_assignments: 0, template_deleted: false });
|
||
mocks.reopenBranchUiMenus.mockResolvedValue({ revision: 2, reopened_menu_keys: [] });
|
||
mocks.writeAuditLog.mockResolvedValue(undefined);
|
||
});
|
||
|
||
afterEach(() => {
|
||
vi.restoreAllMocks();
|
||
});
|
||
|
||
describe('写接口非超管 403(AC-02)', () => {
|
||
it('PUT /templates/:typeCode 分公司 admin ⇒ 403,service 不被调用', async () => {
|
||
const res = await request(app)
|
||
.put('/api/workflow/platform/branch-ui/templates/retail')
|
||
.set('x-test-user-id', 'U1')
|
||
.send({ hidden_menu_keys: [] });
|
||
expect(res.status).toBe(403);
|
||
expect(mocks.saveBranchUiTemplate).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('PUT /branches/:branchCode 分公司 admin ⇒ 403,service 不被调用', async () => {
|
||
const res = await request(app)
|
||
.put('/api/workflow/platform/branch-ui/branches/SUP0001')
|
||
.set('x-test-user-id', 'U1')
|
||
.send({ text_overrides: { 'orders.title': 'x' } });
|
||
expect(res.status).toBe(403);
|
||
expect(mocks.saveBranchUiBranchOverride).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('GET /admin-config 未登录 ⇒ 401', async () => {
|
||
const res = await request(app).get('/api/workflow/platform/branch-ui/admin-config');
|
||
expect(res.status).toBe(401);
|
||
});
|
||
});
|
||
|
||
describe('effective 作用域边界(AC-32)', () => {
|
||
it('超管带空 ?branch_code= ⇒ applied:false 且不查库', async () => {
|
||
mocks.getWorkflowCtx.mockResolvedValue(SUPER_CTX);
|
||
const res = await request(app)
|
||
.get('/api/workflow/platform/branch-ui/effective?branch_code=')
|
||
.set('x-test-user-id', 'U-SUPER');
|
||
expect(res.status).toBe(200);
|
||
expect(res.body.data.applied).toBe(false);
|
||
expect(mocks.resolveEffectiveBranchUi).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('超管只带 x-operating-branch(不带 ?branch_code=)⇒ 仍 applied:false', async () => {
|
||
mocks.getWorkflowCtx.mockResolvedValue(SUPER_CTX);
|
||
const res = await request(app)
|
||
.get('/api/workflow/platform/branch-ui/effective')
|
||
.set('x-test-user-id', 'U-SUPER')
|
||
.set('x-operating-branch', 'SUP0001');
|
||
expect(res.status).toBe(200);
|
||
expect(res.body.data.applied).toBe(false);
|
||
expect(mocks.resolveEffectiveBranchUi).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('非超管同时带 ?branch_code=别家 与 x-operating-branch ⇒ 仍只读自己分公司', async () => {
|
||
const res = await request(app)
|
||
.get('/api/workflow/platform/branch-ui/effective?branch_code=SUP9999')
|
||
.set('x-test-user-id', 'U1')
|
||
.set('x-operating-branch', 'SUP9999');
|
||
expect(res.status).toBe(200);
|
||
expect(mocks.resolveEffectiveBranchUi).toHaveBeenCalledWith('SUP0001');
|
||
});
|
||
});
|
||
|
||
describe('错误映射(§8.2)', () => {
|
||
it('service 抛 409 ⇒ HTTP 409 + revision 透传', async () => {
|
||
mocks.getWorkflowCtx.mockResolvedValue(SUPER_CTX);
|
||
mocks.saveBranchUiTemplate.mockRejectedValue(
|
||
Object.assign(new Error('配置已被其他管理员修改,请刷新后重试'), {
|
||
status: 409,
|
||
code: 'revision_conflict',
|
||
revision: 7,
|
||
})
|
||
);
|
||
const res = await request(app)
|
||
.put('/api/workflow/platform/branch-ui/templates/retail')
|
||
.set('x-test-user-id', 'U-SUPER')
|
||
.send({ hidden_menu_keys: [] });
|
||
expect(res.status).toBe(409);
|
||
expect(res.body).toMatchObject({ error: 'revision_conflict', revision: 7 });
|
||
expect(mocks.writeAuditLog).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('service 抛无 status 的异常 ⇒ 500(不泄露内部信息)', async () => {
|
||
mocks.getWorkflowCtx.mockResolvedValue(SUPER_CTX);
|
||
mocks.saveBranchUiTypes.mockRejectedValue(new Error('boom'));
|
||
const res = await request(app)
|
||
.put('/api/workflow/platform/branch-ui/types')
|
||
.set('x-test-user-id', 'U-SUPER')
|
||
.send({ types: [] });
|
||
expect(res.status).toBe(500);
|
||
expect(res.body.error).toBe('server_error');
|
||
});
|
||
});
|