UNPKG

@flamesshield/rules-engine

Version:

Independent rules engine for security analysis of Firebase

260 lines 8.75 kB
import { enrichProjectInformation } from '../utils/computed-properties.js'; /** * Factory class for creating standardized test data across the rules engine test suite. * Provides consistent methods for creating EnrichedProjectInformation, StaticRule objects, * and other test fixtures with sensible defaults and easy customization. */ export class TestDataFactory { /** * Creates a basic ProjectInformation object with minimal required fields */ static createBaseProjectInfo(overrides = {}) { return { project_id: 'test-project-id', databases: [], functionsv1: [], functionsv2: [], storage_buckets: [], auth: { auth_enabled: false, email_enabled: false, min_length: 6, max_password_length: 4096, special_chars: false, upper_case: false, lower_case: false, number_required: false, enforced: false, anonymous_auth_enabled: false, phone_auth_enabled: false, email_auth_configured: false, mfa_enabled: false, mfa_state: 'DISABLED', authorized_domains: [], authorized_domains_count: 0, email_verification_enabled: false, email_enumeration_protection: false, password_policy_enforcement: 'OFF', sms_region_configured: false, sms_template_configured: false }, app_check: { app_check_enabled: false, services: [], apps: [], total_services: 0, enforced_services: 0, unenforced_services: 0, total_apps: 0, security_summary: { overall_status: 'not_configured', risk_level: 'medium', recommendations: [] } }, function_secrets: [], ...overrides }; } /** * Creates an EnrichedProjectInformation object with computed properties */ static async createEnrichedProjectInfo(overrides = {}) { const baseInfo = this.createBaseProjectInfo(overrides); return await enrichProjectInformation(baseInfo); } /** * Creates a ProjectInformation object configured for AppCheck testing */ static async createProjectInfoWithAppCheck(appCheckEnabled = true, overrides = {}) { const projectInfo = this.createBaseProjectInfo({ ...overrides, // Add AppCheck-specific configuration here if needed }); const enriched = await enrichProjectInformation(projectInfo); if (appCheckEnabled) { return { ...enriched, }; } return enriched; } /** * Creates a ProjectInformation object configured for Authentication testing */ static async createProjectInfoWithAuth(authEnabled = true, overrides = {}) { const projectInfo = this.createBaseProjectInfo({ ...overrides, auth: { ...this.createBaseProjectInfo().auth, auth_enabled: authEnabled, ...overrides.auth, }, }); const enriched = await enrichProjectInformation(projectInfo); return enriched; } /** * Creates a basic StaticRule object for testing */ static createStaticRule(overrides = {}) { return { id: 'test-rule-001', name: 'Test Rule', description: 'A test rule for validation', conditions: { all: [ { fact: 'auth', path: '$.auth_enabled', operator: 'equal', value: true } ] }, event: { type: 'rule-violation', params: { severity: 'critical', category: 'security', message: 'Test validation error' } }, created_at: new Date().toISOString(), updated_at: new Date().toISOString(), ...overrides }; } /** * Creates an AppCheck-specific rule for testing */ static createAppCheckRule(overrides = {}) { return this.createStaticRule({ id: 'appcheck-rule-001', name: 'AppCheck Configuration Rule', description: 'Validates AppCheck configuration', severity: 'high', category: 'appcheck', conditions: { all: [ { fact: 'app_check', path: '$.app_check_enabled', operator: 'equal', value: true } ] }, event: { type: 'appcheck_validation', params: { message: 'AppCheck validation failed' } }, ...overrides }); } /** * Creates an Authentication-specific rule for testing */ static createAuthRule(overrides = {}) { return this.createStaticRule({ id: 'auth-rule-001', name: 'Authentication Configuration Rule', description: 'Validates Authentication configuration', severity: 'high', category: 'authentication', conditions: { all: [ { fact: 'auth', path: '$.auth_enabled', operator: 'equal', value: true } ] }, event: { type: 'auth_validation', params: { message: 'Authentication validation failed' } }, ...overrides }); } /** * Creates a collection of test rules for comprehensive testing */ static createTestRuleSet() { return [ this.createAppCheckRule(), this.createAuthRule(), this.createStaticRule({ id: 'security-rule-001', name: 'Security Rule', description: 'General security validation', category: 'security', event: { type: 'rule-violation', params: { severity: 'critical', category: 'security', message: 'Security validation error', rule_id: 'security-rule-001' } } }), this.createStaticRule({ id: 'performance-rule-001', name: 'Performance Rule', description: 'Performance optimization validation', category: 'performance', event: { type: 'rule-violation', params: { severity: 'critical', category: 'performance', message: 'Performance validation error', rule_id: 'performance-rule-001' } } }) ]; } /** * Creates mock data for edge cases and error scenarios */ static createEdgeCaseProjectInfo(scenario) { switch (scenario) { case 'empty': return {}; case 'invalid': return { project_id: '', databases: [], functionsv1: [], functionsv2: [] }; case 'missing-fields': return { project_id: 'partial-project' // Missing required fields like auth, app_check, etc. }; default: return {}; } } /** * Creates a rule execution context for testing */ static createRuleExecutionContext(projectInfo, rules = []) { return { projectInfo, rules: rules.length > 0 ? rules : this.createTestRuleSet(), executionId: `exec-${Date.now()}`, timestamp: new Date().toISOString() }; } } //# sourceMappingURL=TestDataFactory.js.map