Files
SupplierSystem/server/tests/branchUiGatewayBoundary.test.ts
T
Ubuntu de14fb01f0
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
feat: 供应商系统 2026-11 阶段成果(分公司模板/真还原预览、商品远程搜索、主系统联调、加工幂等修复)
- 分公司类型模板:超管配置隐藏路由/菜单 + 中文文案覆盖,仅对挂类型的分公司生效(后端 service/routes、前端 Provider/守卫/配置页)
- 超管模板 iframe 真还原预览:28 个业务页注册、白名单点选编辑、空/示例数据 fixture、白名单漂移检测工具
- 商品/SKU 下拉远程搜索:服务端关键词搜索 + 前端 useRemoteOptionSearch/SearchableSelect 接入(订单/采购/商品等页)
- 主系统联调:子系统主动解绑主系统授权(幂等重试/本地兜底)、追加修改 price_amend 视图代理与 amend-price 转发
- 修复:仓内加工重复建单幂等(已完成需求单 guard、order 任务一商品一单 guard)
- 修复:出入库记录只取前 100 条 → 服务端分页 + 真实总数
- 其它:库存/订单若干修复、分公司管理响应式布局、i18n 文案、设计文档与回归测试
2026-11-07 20:10:48 +08:00

263 lines
8.8 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.
/**
* `/api/data` 网关拒写 `branch_ui_*` · 边界补充(tester)。
*
* 只补 dev 的 branchUiGatewayGuard.test.ts 未覆盖的形态:
* - insert 多行数组里夹带一行 branch_ui_* 也要整体拒绝;
* - 大小写 / 首尾空格 / 自造 `branch_ui_<x>` 前缀同样按前缀规则拒绝;
* - update / delete 用 `in` 形态指向 branch_ui_* 被拒;
* - 按行 id 的 delete 同样追加 NOT IN 兜底;带 config_key neq 的按 id 更新仍补兜底;
* - 非 system_configs 表不受影响;select(含 branch_ui_template.*)仍放行。
*/
import { describe, expect, it } from 'vitest';
import { applyTenantScope, type TenantContext } from '../src/services/tenantScope';
import type { QuerySpec } from '../src/services/queryExecutor';
function ctx(overrides: Partial<TenantContext> = {}): TenantContext {
return {
userId: 'U1',
roles: ['admin'],
permissions: ['system_manage', 'system_config'],
branchCode: 'SUP0001',
supplierCode: null,
purchaserCode: null,
isSuperAdmin: false,
isBranchAdmin: true,
isInternalStaff: true,
isSupplierAccount: false,
...overrides,
};
}
const SUPER = ctx({ userId: 'U-SUPER', roles: ['super_admin'], branchCode: null, isSuperAdmin: true, isBranchAdmin: false });
const BLOCKED = /只能由平台接口维护/;
describe('/api/data 拒写 branch_ui_* · 形态边界', () => {
it('insert 多行数组里夹带一行 branch_ui_* ⇒ 整体拒绝(不写入前一行)', () => {
expect(() =>
applyTenantScope(
{
table: 'system_configs',
action: 'insert',
insertData: [
{ config_key: 'nav_menu_layout', config_value: {}, branch_code: 'SUP0001' },
{ config_key: 'branch_ui_branch', config_value: {}, branch_code: 'SUP0001' },
],
},
ctx()
)
).toThrow(BLOCKED);
});
it('大小写 / 首尾空格 / 自造前缀都按 branch_ui_ 前缀拒绝', () => {
for (const configKey of ['BRANCH_UI_BRANCH', ' branch_ui_types ', 'branch_ui_settings']) {
expect(() =>
applyTenantScope(
{
table: 'system_configs',
action: 'insert',
insertData: { config_key: configKey, config_value: {}, branch_code: 'SUP0001' },
},
ctx()
)
).toThrow(BLOCKED);
}
});
it('update / delete 用 in 形态指向 branch_ui_* ⇒ 拒绝(分公司 admin 与超管)', () => {
for (const actor of [ctx(), SUPER]) {
expect(() =>
applyTenantScope(
{
table: 'system_configs',
action: 'update',
filters: [{ op: 'in', column: 'config_key', value: ['nav_menu_layout', 'branch_ui_branch'] }],
updateData: { config_value: {} },
},
actor
)
).toThrow(BLOCKED);
expect(() =>
applyTenantScope(
{
table: 'system_configs',
action: 'delete',
filters: [{ op: 'in', column: 'config_key', value: ['branch_ui_types'] }],
},
actor
)
).toThrow(BLOCKED);
}
});
it('按行 id 的 delete 也追加 NOT IN 兜底(不能按 id 删覆盖行)', () => {
const scoped = applyTenantScope(
{
table: 'system_configs',
action: 'delete',
filters: [{ op: 'eq', column: 'id', value: 'row-id' }],
},
ctx()
);
const neq = (scoped.filters || []).filter((filter) => filter.column === 'config_key');
expect(neq).toEqual(
expect.arrayContaining([
{ op: 'neq', column: 'config_key', value: 'branch_ui_types' },
{ op: 'neq', column: 'config_key', value: 'branch_ui_branch' },
])
);
});
it('带 config_key neq(retired 策略加的非定向过滤)的按 id 更新仍补两条兜底', () => {
const scoped = applyTenantScope(
{
table: 'system_configs',
action: 'update',
filters: [
{ op: 'eq', column: 'id', value: 'row-id' },
{ op: 'neq', column: 'config_key', value: 'retired_key' },
],
updateData: { config_value: {} },
},
ctx()
);
const neqValues = (scoped.filters || [])
.filter((filter) => filter.column === 'config_key' && filter.op === 'neq')
.map((filter) => String(filter.value));
expect(neqValues).toEqual(expect.arrayContaining(['branch_ui_types', 'branch_ui_branch']));
});
it('非 system_configs 表写不受影响(回归)', () => {
expect(() =>
applyTenantScope(
{
table: 'notifications',
action: 'update',
filters: [{ op: 'eq', column: 'config_key', value: 'branch_ui_branch' }],
updateData: { read: true },
},
ctx()
)
).not.toThrow();
});
it('select branch_ui_template.<code> 仍放行(只拒写,不拒读)', () => {
expect(() =>
applyTenantScope(
{
table: 'system_configs',
action: 'select',
filters: [{ op: 'ilike', column: 'config_key', value: 'branch_ui_template.%' }],
},
SUPER
)
).not.toThrow();
});
it('system_configs 按 id 更新普通配置不受影响(不加全表拒写)', () => {
expect(() =>
applyTenantScope(
{
table: 'system_configs',
action: 'update',
filters: [{ op: 'eq', column: 'id', value: 'row-id' }],
updateData: { config_value: { days: 7 } },
},
ctx()
)
).not.toThrow();
});
});
// ---------------------------------------------------------------------------
// R2(task-11)F11:LIKE/ILIKE 通配、IN 通配、OR 形态不得绕过 branch_ui_* 写保护
// ---------------------------------------------------------------------------
describe('R2-F11:config_key 通配/or 形态绕过一律拒绝(正常写不受影响)', () => {
const systemConfigsWrite = (filters: unknown[]) => ({
table: 'system_configs',
action: 'update' as const,
filters: filters as never[],
updateData: { config_value: { hacked: true } },
});
it('config_key 上任何 like/ilike(含无通配)⇒ 400', () => {
for (const filter of [
{ op: 'ilike', column: 'config_key', value: 'branch_ui%' },
{ op: 'ilike', column: 'config_key', value: '%branch_ui_branch%' },
{ op: 'ilike', column: 'config_key', value: 'BRANCH_UI%' },
{ op: 'ilike', column: 'config_key', value: 'branch_ui_types' }, // 无通配也拒(LIKE 语义)
{ op: 'like', column: 'config_key', value: 'branch\_ui%' },
]) {
expect(
() => applyTenantScope(systemConfigsWrite([filter]), ctx()),
JSON.stringify(filter)
).toThrow(BLOCKED);
}
});
it('in 数组含通配符(%/*)或含 branch_ui 项 ⇒ 400', () => {
for (const value of [
['%'],
['branch_ui%'],
['nav_menu_layout', '%branch_ui%'],
['*'],
['branch_ui_hacked'],
['nav_menu_layout', 'branch_ui_branch'],
]) {
expect(
() => applyTenantScope(systemConfigsWrite([{ op: 'in', column: 'config_key', value }]), ctx()),
JSON.stringify(value)
).toThrow(BLOCKED);
}
});
it('or 形态(config_key.ilike/like + branch_ui 或通配)⇒ 400;非 config_key 的 or 放行', () => {
for (const raw of [
'config_key.ilike.branch_ui%,status.eq.1',
'config_key.like.%branch_ui%',
'config_key.ilike.%',
'config_key.like.branch\_ui%',
]) {
expect(
() => applyTenantScope(systemConfigsWrite([{ op: 'or', raw }]), ctx()),
raw
).toThrow(BLOCKED);
}
expect(() =>
applyTenantScope(systemConfigsWrite([{ op: 'or', raw: 'config_key.eq.nav_menu_layout,status.eq.1' }]), ctx())
).not.toThrow();
expect(() =>
applyTenantScope(systemConfigsWrite([{ op: 'or', raw: 'description.ilike.%branch_ui%' }]), ctx())
).not.toThrow();
});
it('正常写不被误伤:eq 精确 key / 非 config_key 列 ilike / in 精确值', () => {
expect(() =>
applyTenantScope(systemConfigsWrite([{ op: 'eq', column: 'config_key', value: 'nav_menu_layout' }]), ctx())
).not.toThrow();
expect(() =>
applyTenantScope(
systemConfigsWrite([{ op: 'ilike', column: 'description', value: '%分公司配置%' }]),
ctx()
)
).not.toThrow();
expect(() =>
applyTenantScope(
systemConfigsWrite([{ op: 'in', column: 'config_key', value: ['nav_menu_layout', 'nav_badge_days'] }]),
ctx()
)
).not.toThrow();
// insert 普通 key(分公司 admin 正常写导航/徽标配置)
expect(() =>
applyTenantScope(
{
table: 'system_configs',
action: 'insert',
insertData: { config_key: 'nav_badge_days', config_value: { days: 7 }, branch_code: 'SUP0001' },
},
ctx()
)
).not.toThrow();
});
});