来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
109 lines
4.2 KiB
TypeScript
109 lines
4.2 KiB
TypeScript
/**
|
||
* 通用数据网关「禁删」名单(DENY_DELETE_TABLES)回归测试。
|
||
*
|
||
* 背景:customers 不在 DENY_WRITE_TABLES,且 queryExecutor 对任意表都构造裸
|
||
* `DELETE FROM <table>`(只对 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> = {}): 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<QuerySpec> & Pick<QuerySpec, 'table' | 'action'>): 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();
|
||
});
|
||
});
|