/**
* 通用数据网关「禁删」名单(DENY_DELETE_TABLES)回归测试。
*
* 背景:customers 不在 DENY_WRITE_TABLES,且 queryExecutor 对任意表都构造裸
* `DELETE FROM
`(只对 orders 特判),因此 `/api/data` 的 delete 此前可
* 直删客户,绕过 customerDeleteService 的「有历史订单即拒删」与 12 张表级联;
* tenant_* schema 由 `LIKE ... INCLUDING ALL` 克隆、不含外键,DB 侧也不会拦,
* 结果只会留下 orders / customer_skus 等孤儿行。
*
* 本测试同时钉住「只拦 delete」这一范围决策:insert/update 仍走网关(客户新增/
* 编辑/批量导入依赖它),suppliers 有意不加进来(其前端删除入口本就依赖网关,
* 且无守卫/级联可恢复)。
*/
import { describe, expect, it } from 'vitest';
import { assertTableAccess } from '../src/services/accessPolicy';
import type { QuerySpec } from '../src/services/queryExecutor';
import type { TenantContext } from '../src/services/tenantScope';
function ctx(overrides: Partial = {}): TenantContext {
return {
userId: 'u-1',
roles: ['operator'],
permissions: [],
branchCode: 'BR002',
supplierCode: null,
purchaserCode: null,
isSuperAdmin: false,
isBranchAdmin: false,
isInternalStaff: true,
isSupplierAccount: false,
...overrides,
};
}
const customerManager = ctx({ permissions: ['customer_manage'] });
const superAdmin = ctx({ roles: ['super_admin'], isSuperAdmin: true, permissions: ['customer_manage'] });
function spec(partial: Partial & Pick): QuerySpec {
return { filters: [{ column: 'customer_code', op: 'eq', value: 'CUS1' }], ...partial };
}
/** 断言抛出的 forbidden(不是「表不允许访问」之类无关的拒绝) */
function expectForbidden(fn: () => void) {
let caught: any;
try {
fn();
} catch (error) {
caught = error;
}
expect(caught, '预期被拒绝但未抛错').toBeTruthy();
expect(caught.code).toBe('forbidden');
expect(String(caught.message)).toContain('不允许通过数据接口删除');
return caught;
}
describe('通用数据网关:customers 禁止直删', () => {
it('持 customer_manage 的账号 delete → 403 forbidden', () => {
expectForbidden(() => assertTableAccess(spec({ table: 'customers', action: 'delete' }), customerManager));
});
it('超管 delete 同样被拦(判定在 isSuperAdmin 短路之前)', () => {
expectForbidden(() => assertTableAccess(spec({ table: 'customers', action: 'delete' }), superAdmin));
});
});
describe('范围决策:只拦 delete,写入面保持原状', () => {
it('customers insert / update 仍放行(前端新增、编辑、批量导入依赖网关)', () => {
expect(() =>
assertTableAccess(spec({ table: 'customers', action: 'insert', insertData: { customer_code: 'CUS1' } }), customerManager)
).not.toThrow();
expect(() =>
assertTableAccess(spec({ table: 'customers', action: 'update', updateData: { company_name: 'X' } }), customerManager)
).not.toThrow();
});
it('customers select 仍放行(显式列,避开敏感表 * 规则)', () => {
expect(() =>
assertTableAccess(
spec({ table: 'customers', action: 'select', select: 'customer_code, company_name' }),
customerManager
)
).not.toThrow();
});
it('orders delete 不受本名单影响(网关执行层仍特判到 deleteOrderWithDependencies)', () => {
expect(() =>
assertTableAccess(
spec({ table: 'orders', action: 'delete', filters: [{ column: 'order_code', op: 'eq', value: 'SO1' }] }),
ctx({ permissions: ['order_delete'] })
)
).not.toThrow();
});
it('suppliers / after_sales 有意不在名单内(留给各自冻结条款裁决)', () => {
expect(() =>
assertTableAccess(
spec({ table: 'suppliers', action: 'delete', filters: [{ column: 'supplier_code', op: 'eq', value: 'S1' }] }),
ctx({ permissions: ['supplier_manage'] })
)
).not.toThrow();
expect(() =>
assertTableAccess(
spec({ table: 'after_sales', action: 'delete', filters: [{ column: 'id', op: 'eq', value: '1' }] }),
ctx({ permissions: ['aftersales_manage'] })
)
).not.toThrow();
});
});