UNPKG

spanwright

Version:

CLI tool to generate Cloud Spanner E2E testing framework projects with Go database tools and Playwright browser automation

75 lines 5.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const template_security_1 = require("../template-security"); const errors_1 = require("../errors"); (0, vitest_1.describe)('Template Security Module', () => { (0, vitest_1.describe)('escapeForTemplate', () => { (0, vitest_1.it)('should pass valid inputs through unchanged', () => { (0, vitest_1.expect)((0, template_security_1.escapeForTemplate)('validInput')).toBe('validInput'); (0, vitest_1.expect)((0, template_security_1.escapeForTemplate)('test-123')).toBe('test-123'); (0, vitest_1.expect)((0, template_security_1.escapeForTemplate)('project_name')).toBe('project_name'); }); (0, vitest_1.it)('should throw SecurityError for invalid inputs', () => { (0, vitest_1.expect)(() => (0, template_security_1.escapeForTemplate)('invalid.input')).toThrow(errors_1.SecurityError); (0, vitest_1.expect)(() => (0, template_security_1.escapeForTemplate)('invalid/input')).toThrow(errors_1.SecurityError); (0, vitest_1.expect)(() => (0, template_security_1.escapeForTemplate)('invalid\\input')).toThrow(errors_1.SecurityError); }); }); (0, vitest_1.describe)('validateTemplateInput', () => { (0, vitest_1.it)('should pass for valid generic identifiers', () => { (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('validInput')).not.toThrow(); (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('test-123')).not.toThrow(); (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('project_name')).not.toThrow(); }); (0, vitest_1.it)('should pass for valid project names', () => { (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('myProject', 'PROJECT_NAME')).not.toThrow(); (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('test-project', 'PROJECT_NAME')).not.toThrow(); }); (0, vitest_1.it)('should pass for valid schema paths', () => { (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('./schema/test.sql', 'SCHEMA_PATH')).not.toThrow(); (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('/path/to/schema', 'SCHEMA_PATH')).not.toThrow(); }); (0, vitest_1.it)('should throw SecurityError for invalid inputs', () => { (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('invalid.input')).toThrow(errors_1.SecurityError); (0, vitest_1.expect)(() => (0, template_security_1.validateTemplateInput)('123invalid', 'PROJECT_NAME')).toThrow(errors_1.SecurityError); }); }); (0, vitest_1.describe)('simpleTemplateReplace', () => { (0, vitest_1.it)('should replace template variables correctly', () => { const content = 'Hello PROJECT_NAME, this is a test.'; const replacements = { PROJECT_NAME: 'testProject' }; const result = (0, template_security_1.simpleTemplateReplace)(content, replacements); (0, vitest_1.expect)(result).toBe('Hello testProject, this is a test.'); }); (0, vitest_1.it)('should handle multiple replacements', () => { const content = 'Project: PROJECT_NAME, Database: DB_NAME'; const replacements = { PROJECT_NAME: 'myProject', DB_NAME: 'testDB', }; const result = (0, template_security_1.simpleTemplateReplace)(content, replacements); (0, vitest_1.expect)(result).toBe('Project: myProject, Database: testDB'); }); (0, vitest_1.it)('should validate replacement values', () => { const content = 'Hello PROJECT_NAME'; const replacements = { PROJECT_NAME: 'invalid.name' }; (0, vitest_1.expect)(() => (0, template_security_1.simpleTemplateReplace)(content, replacements)).toThrow(errors_1.SecurityError); }); }); (0, vitest_1.describe)('SAFE_PATTERNS', () => { (0, vitest_1.it)('should export validation patterns', () => { (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.GENERIC_IDENTIFIER).toBeDefined(); (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.PROJECT_NAME).toBeDefined(); (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.DATABASE_NAME).toBeDefined(); (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.SCHEMA_PATH).toBeDefined(); }); (0, vitest_1.it)('should validate correct patterns', () => { (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.GENERIC_IDENTIFIER.test('validInput')).toBe(true); (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.GENERIC_IDENTIFIER.test('invalid.input')).toBe(false); (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.PROJECT_NAME.test('myProject')).toBe(true); (0, vitest_1.expect)(template_security_1.SAFE_PATTERNS.PROJECT_NAME.test('123invalid')).toBe(false); }); }); }); //# sourceMappingURL=template-security.test.js.map