Files
SupplierSystem/server/tests/tenantRlsContract.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

72 lines
3.6 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.
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* DATA-8(2026-10-19 T4,D12 两批)RLS 覆盖契约。
* 真实库校验由 tester 只读查询:br001/br002/template 中"含 branch_code 且未开 RLS"=0、每表 1 条 policy。
*/
const dir = join(__dirname, '../../migrations');
const batch1 = readFileSync(join(dir, '20261063_tenant_core_rls_batch1.sql'), 'utf8');
const batch2 = readFileSync(join(dir, '20261064_tenant_core_rls_batch2.sql'), 'utf8');
const data1 = readFileSync(join(dir, '20261060_customer_sku_remarks_disable_rls.sql'), 'utf8');
function arrayBlock(sql: string): string {
const start = sql.indexOf('core_tables text[] := ARRAY[');
const end = sql.indexOf('];', start);
return start >= 0 && end > start ? sql.slice(start, end) : '';
}
function names(sql: string): string[] {
return [...arrayBlock(sql).matchAll(/'([^']+)'/g)].map((m) => m[1]);
}
const b1 = names(batch1);
const b2 = names(batch2);
const BATCH1_NEW = [
'after_sale_returns', 'after_sales', 'categories', 'cost_adjustments', 'customer_delivery_assignments',
'customer_quote_sheets', 'customer_quote_versions', 'delivery_persons', 'delivery_schedules', 'delivery_signatures',
'exception_tickets', 'fixed_costs', 'free_cost_invoices', 'free_cost_payment_vouchers', 'inspection_loss_confirmations',
'inspection_photo_archives', 'inspection_returns', 'inspection_review_logs', 'inventory_batch_reports',
'invoice_statement_links', 'invoices', 'operation_logs', 'order_allocation_batches', 'order_item_returns',
'picking_assign_rules', 'picking_containers', 'picking_scan_logs', 'processing_tasks', 'product_catalog', 'skus',
];
const BATCH2_NEW = [
'ai_prompt_overrides', 'assistant_conversations', 'assistant_turns', 'import_mapping_memories',
'market_compare_exclusions', 'market_external_catalog', 'market_price_history', 'market_price_indices',
'market_prices', 'market_sku_external_bindings', 'parse_sessions', 'price_invert_events', 'print_jobs',
'product_substitutions', 'quality_reports', 'quote_price_proposals', 'seasonal_recommendations',
'self_purchase_stock_inbounds', 'smart_classification_rules', 'statement_disputes', 'statement_lines',
'statement_payments', 'supplier_prep_amend_requests', 'supplier_rating_snapshots', 'supplier_statements',
'suppliers', 'system_configs', 'user_preferences', 'warehouses', 'weigh_devices',
];
describe('DATA-8:RLS 两批契约', () => {
it('batch1 = 15 既有 + 30 高频;batch2 = 累计 75(新增其余 30)', () => {
expect(b1).toHaveLength(45);
expect(b2).toHaveLength(75);
for (const t of BATCH1_NEW) expect(b1, `batch1 缺 ${t}`).toContain(t);
for (const t of BATCH2_NEW) expect(b2, `batch2 缺 ${t}`).toContain(t);
// 两批并集恰好 60 张新增
expect(new Set([...BATCH1_NEW, ...BATCH2_NEW]).size).toBe(60);
});
it('同形策略:ENABLE/FORCE + DROP POLICY IF EXISTS + CREATE POLICY + ensure_all', () => {
for (const sql of [batch1, batch2]) {
expect(sql).toContain('ENABLE ROW LEVEL SECURITY');
expect(sql).toContain('FORCE ROW LEVEL SECURITY');
expect(sql).toContain('DROP POLICY IF EXISTS');
expect(sql).toContain('CREATE POLICY');
expect(sql).toContain('public.ensure_all_tenant_core_rls()');
expect(sql).toContain("current_setting(''app.branch_code'', true)");
}
});
it('DATA-1 迁移对所有开启 RLS 的 schema 幂等 DISABLE', () => {
expect(data1).toContain('DISABLE ROW LEVEL SECURITY');
expect(data1).toContain('c.relrowsecurity = true');
expect(data1).toContain('DO $$');
});
});