Files
MyGoNavi/frontend/src/utils/connectionReadOnly.test.ts
Syngnat 1ae2b74279 🐛 fix(sql-parser): 修复尾随注释导致事务与只读判定异常
- 过滤分号后的纯注释语句并保留数据库可执行版本注释
- 前后端按数据库方言统一处理双横线、井号与块注释
- 修复事务选择、只读保护、SQL 审计及 AI 风险分析误判
- 补充流式 SQL、事务执行与方言解析回归测试
2026-07-13 12:52:56 +08:00

74 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
findConnectionMutatingStatements,
isConnectionDataEditRestricted,
isConnectionDataImportRestricted,
isConnectionScriptExecutionRestricted,
isConnectionStructureEditRestricted,
resolveConnectionProtectionConfig,
} from './connectionReadOnly';
describe('connectionReadOnly', () => {
it('maps legacy readOnly connections to the full production protection set', () => {
expect(resolveConnectionProtectionConfig({
type: 'postgres',
readOnly: true,
})).toEqual({
restrictDataEdit: true,
restrictStructureEdit: true,
restrictScriptExecution: true,
restrictDataImport: true,
});
});
it('keeps partial protection flags isolated from each other', () => {
const config = {
type: 'postgres',
protection: {
restrictDataEdit: true,
restrictDataImport: true,
},
};
expect(isConnectionDataEditRestricted(config)).toBe(true);
expect(isConnectionDataImportRestricted(config)).toBe(true);
expect(isConnectionStructureEditRestricted(config)).toBe(false);
expect(isConnectionScriptExecutionRestricted(config)).toBe(false);
});
it('only blocks mutating SQL when script execution protection is enabled', () => {
expect(findConnectionMutatingStatements({
type: 'postgres',
protection: {
restrictScriptExecution: true,
},
}, "SELECT * FROM users; UPDATE users SET name = 'next';")).toEqual([
"UPDATE users SET name = 'next'",
]);
expect(findConnectionMutatingStatements({
type: 'postgres',
protection: {
restrictDataEdit: true,
},
}, "UPDATE users SET name = 'next';")).toEqual([]);
});
it('uses the connection dialect when filtering comment-only statements', () => {
expect(findConnectionMutatingStatements({
type: 'postgres',
protection: {
restrictScriptExecution: true,
},
}, 'SELECT * FROM users; /*! MySQL-only comment */')).toEqual([]);
expect(findConnectionMutatingStatements({
type: 'mysql',
protection: {
restrictScriptExecution: true,
},
}, 'SELECT * FROM users;--compact')).toEqual(['--compact']);
});
});