|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + ComplianceAuditRequirementSchema, |
| 4 | + ComplianceEncryptionRequirementSchema, |
| 5 | + MaskingVisibilityRuleSchema, |
| 6 | + SecurityEventCorrelationSchema, |
| 7 | + DataClassificationPolicySchema, |
| 8 | + SecurityContextConfigSchema, |
| 9 | + type ComplianceAuditRequirement, |
| 10 | + type ComplianceEncryptionRequirement, |
| 11 | + type MaskingVisibilityRule, |
| 12 | + type SecurityEventCorrelation, |
| 13 | + type DataClassificationPolicy, |
| 14 | + type SecurityContextConfig, |
| 15 | + type SecurityContextConfigInput, |
| 16 | +} from './security-context.zod'; |
| 17 | + |
| 18 | +describe('ComplianceAuditRequirementSchema', () => { |
| 19 | + it('should accept valid GDPR audit requirement', () => { |
| 20 | + const req: ComplianceAuditRequirement = { |
| 21 | + framework: 'gdpr', |
| 22 | + requiredEvents: ['data.delete', 'data.export', 'auth.login'], |
| 23 | + retentionDays: 180, |
| 24 | + alertOnMissing: true, |
| 25 | + }; |
| 26 | + const result = ComplianceAuditRequirementSchema.parse(req); |
| 27 | + expect(result.framework).toBe('gdpr'); |
| 28 | + expect(result.requiredEvents).toHaveLength(3); |
| 29 | + expect(result.retentionDays).toBe(180); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should accept all framework types', () => { |
| 33 | + const frameworks = ['gdpr', 'hipaa', 'sox', 'pci_dss', 'ccpa', 'iso27001'] as const; |
| 34 | + frameworks.forEach(fw => { |
| 35 | + expect(() => ComplianceAuditRequirementSchema.parse({ |
| 36 | + framework: fw, |
| 37 | + requiredEvents: ['data.read'], |
| 38 | + retentionDays: 90, |
| 39 | + })).not.toThrow(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should default alertOnMissing to true', () => { |
| 44 | + const result = ComplianceAuditRequirementSchema.parse({ |
| 45 | + framework: 'hipaa', |
| 46 | + requiredEvents: ['data.read'], |
| 47 | + retentionDays: 365, |
| 48 | + }); |
| 49 | + expect(result.alertOnMissing).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should reject missing required fields', () => { |
| 53 | + expect(() => ComplianceAuditRequirementSchema.parse({})).toThrow(); |
| 54 | + expect(() => ComplianceAuditRequirementSchema.parse({ framework: 'gdpr' })).toThrow(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should reject invalid framework', () => { |
| 58 | + expect(() => ComplianceAuditRequirementSchema.parse({ |
| 59 | + framework: 'invalid', |
| 60 | + requiredEvents: [], |
| 61 | + retentionDays: 30, |
| 62 | + })).toThrow(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('ComplianceEncryptionRequirementSchema', () => { |
| 67 | + it('should accept valid HIPAA encryption requirement', () => { |
| 68 | + const req: ComplianceEncryptionRequirement = { |
| 69 | + framework: 'hipaa', |
| 70 | + dataClassifications: ['phi', 'pii'], |
| 71 | + minimumAlgorithm: 'aes-256-gcm', |
| 72 | + keyRotationMaxDays: 90, |
| 73 | + }; |
| 74 | + const result = ComplianceEncryptionRequirementSchema.parse(req); |
| 75 | + expect(result.framework).toBe('hipaa'); |
| 76 | + expect(result.dataClassifications).toEqual(['phi', 'pii']); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should apply defaults for algorithm and rotation', () => { |
| 80 | + const result = ComplianceEncryptionRequirementSchema.parse({ |
| 81 | + framework: 'pci_dss', |
| 82 | + dataClassifications: ['pci'], |
| 83 | + }); |
| 84 | + expect(result.minimumAlgorithm).toBe('aes-256-gcm'); |
| 85 | + expect(result.keyRotationMaxDays).toBe(90); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should accept all data classifications', () => { |
| 89 | + const result = ComplianceEncryptionRequirementSchema.parse({ |
| 90 | + framework: 'gdpr', |
| 91 | + dataClassifications: ['pii', 'phi', 'pci', 'financial', 'confidential', 'internal', 'public'], |
| 92 | + }); |
| 93 | + expect(result.dataClassifications).toHaveLength(7); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('MaskingVisibilityRuleSchema', () => { |
| 98 | + it('should accept valid masking visibility rule', () => { |
| 99 | + const rule: MaskingVisibilityRule = { |
| 100 | + dataClassification: 'pii', |
| 101 | + defaultMasked: true, |
| 102 | + unmaskRoles: ['admin', 'compliance_officer'], |
| 103 | + auditUnmask: true, |
| 104 | + requireApproval: false, |
| 105 | + }; |
| 106 | + const result = MaskingVisibilityRuleSchema.parse(rule); |
| 107 | + expect(result.dataClassification).toBe('pii'); |
| 108 | + expect(result.unmaskRoles).toEqual(['admin', 'compliance_officer']); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should apply defaults', () => { |
| 112 | + const result = MaskingVisibilityRuleSchema.parse({ dataClassification: 'phi' }); |
| 113 | + expect(result.defaultMasked).toBe(true); |
| 114 | + expect(result.auditUnmask).toBe(true); |
| 115 | + expect(result.requireApproval).toBe(false); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should accept approval workflow configuration', () => { |
| 119 | + const result = MaskingVisibilityRuleSchema.parse({ |
| 120 | + dataClassification: 'financial', |
| 121 | + requireApproval: true, |
| 122 | + approvalRoles: ['cfo', 'auditor'], |
| 123 | + }); |
| 124 | + expect(result.requireApproval).toBe(true); |
| 125 | + expect(result.approvalRoles).toEqual(['cfo', 'auditor']); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +describe('SecurityEventCorrelationSchema', () => { |
| 130 | + it('should apply all defaults for empty config', () => { |
| 131 | + const result = SecurityEventCorrelationSchema.parse({}); |
| 132 | + expect(result.enabled).toBe(true); |
| 133 | + expect(result.correlationId).toBe(true); |
| 134 | + expect(result.linkAuthToAudit).toBe(true); |
| 135 | + expect(result.linkEncryptionToAudit).toBe(true); |
| 136 | + expect(result.linkMaskingToAudit).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should accept custom correlation config', () => { |
| 140 | + const result = SecurityEventCorrelationSchema.parse({ |
| 141 | + enabled: true, |
| 142 | + linkEncryptionToAudit: false, |
| 143 | + }); |
| 144 | + expect(result.linkEncryptionToAudit).toBe(false); |
| 145 | + expect(result.linkAuthToAudit).toBe(true); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('DataClassificationPolicySchema', () => { |
| 150 | + it('should accept valid classification policy', () => { |
| 151 | + const policy: DataClassificationPolicy = { |
| 152 | + classification: 'pii', |
| 153 | + requireEncryption: true, |
| 154 | + requireMasking: true, |
| 155 | + requireAudit: true, |
| 156 | + retentionDays: 365, |
| 157 | + }; |
| 158 | + const result = DataClassificationPolicySchema.parse(policy); |
| 159 | + expect(result.classification).toBe('pii'); |
| 160 | + expect(result.requireEncryption).toBe(true); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should apply defaults', () => { |
| 164 | + const result = DataClassificationPolicySchema.parse({ classification: 'public' }); |
| 165 | + expect(result.requireEncryption).toBe(false); |
| 166 | + expect(result.requireMasking).toBe(false); |
| 167 | + expect(result.requireAudit).toBe(false); |
| 168 | + expect(result.retentionDays).toBeUndefined(); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should accept all classification levels', () => { |
| 172 | + const levels = ['pii', 'phi', 'pci', 'financial', 'confidential', 'internal', 'public'] as const; |
| 173 | + levels.forEach(level => { |
| 174 | + expect(() => DataClassificationPolicySchema.parse({ classification: level })).not.toThrow(); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +describe('SecurityContextConfigSchema', () => { |
| 180 | + it('should apply defaults for empty config', () => { |
| 181 | + const result = SecurityContextConfigSchema.parse({}); |
| 182 | + expect(result.enabled).toBe(true); |
| 183 | + expect(result.enforceOnWrite).toBe(true); |
| 184 | + expect(result.enforceOnRead).toBe(true); |
| 185 | + expect(result.failOpen).toBe(false); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should accept full security context configuration', () => { |
| 189 | + const config: SecurityContextConfigInput = { |
| 190 | + enabled: true, |
| 191 | + complianceAuditRequirements: [ |
| 192 | + { framework: 'gdpr', requiredEvents: ['data.delete', 'data.export'], retentionDays: 180 }, |
| 193 | + { framework: 'hipaa', requiredEvents: ['data.read', 'data.update'], retentionDays: 365 }, |
| 194 | + ], |
| 195 | + complianceEncryptionRequirements: [ |
| 196 | + { framework: 'hipaa', dataClassifications: ['phi'] }, |
| 197 | + { framework: 'pci_dss', dataClassifications: ['pci', 'financial'] }, |
| 198 | + ], |
| 199 | + maskingVisibility: [ |
| 200 | + { dataClassification: 'pii', unmaskRoles: ['admin'], requireApproval: true, approvalRoles: ['dpo'] }, |
| 201 | + ], |
| 202 | + dataClassifications: [ |
| 203 | + { classification: 'pii', requireEncryption: true, requireMasking: true, requireAudit: true }, |
| 204 | + { classification: 'public', requireEncryption: false }, |
| 205 | + ], |
| 206 | + eventCorrelation: { enabled: true, correlationId: true }, |
| 207 | + enforceOnWrite: true, |
| 208 | + enforceOnRead: true, |
| 209 | + failOpen: false, |
| 210 | + }; |
| 211 | + const result = SecurityContextConfigSchema.parse(config); |
| 212 | + expect(result.complianceAuditRequirements).toHaveLength(2); |
| 213 | + expect(result.complianceEncryptionRequirements).toHaveLength(2); |
| 214 | + expect(result.maskingVisibility).toHaveLength(1); |
| 215 | + expect(result.dataClassifications).toHaveLength(2); |
| 216 | + expect(result.eventCorrelation?.enabled).toBe(true); |
| 217 | + }); |
| 218 | + |
| 219 | + it('should leave optional arrays undefined when not provided', () => { |
| 220 | + const result = SecurityContextConfigSchema.parse({}); |
| 221 | + expect(result.complianceAuditRequirements).toBeUndefined(); |
| 222 | + expect(result.complianceEncryptionRequirements).toBeUndefined(); |
| 223 | + expect(result.maskingVisibility).toBeUndefined(); |
| 224 | + expect(result.dataClassifications).toBeUndefined(); |
| 225 | + expect(result.eventCorrelation).toBeUndefined(); |
| 226 | + }); |
| 227 | + |
| 228 | + it('should accept failOpen true for development environments', () => { |
| 229 | + const result = SecurityContextConfigSchema.parse({ failOpen: true }); |
| 230 | + expect(result.failOpen).toBe(true); |
| 231 | + }); |
| 232 | +}); |
| 233 | + |
| 234 | +describe('Type exports', () => { |
| 235 | + it('should have valid type exports', () => { |
| 236 | + const audit: ComplianceAuditRequirement = { |
| 237 | + framework: 'gdpr', requiredEvents: ['data.delete'], retentionDays: 180, alertOnMissing: true, |
| 238 | + }; |
| 239 | + const encryption: ComplianceEncryptionRequirement = { |
| 240 | + framework: 'hipaa', dataClassifications: ['phi'], minimumAlgorithm: 'aes-256-gcm', keyRotationMaxDays: 90, |
| 241 | + }; |
| 242 | + const masking: MaskingVisibilityRule = { |
| 243 | + dataClassification: 'pii', defaultMasked: true, auditUnmask: true, requireApproval: false, |
| 244 | + }; |
| 245 | + const correlation: SecurityEventCorrelation = { |
| 246 | + enabled: true, correlationId: true, linkAuthToAudit: true, linkEncryptionToAudit: true, linkMaskingToAudit: true, |
| 247 | + }; |
| 248 | + const policy: DataClassificationPolicy = { |
| 249 | + classification: 'confidential', requireEncryption: true, requireMasking: false, requireAudit: true, |
| 250 | + }; |
| 251 | + const context: SecurityContextConfig = { |
| 252 | + enabled: true, enforceOnWrite: true, enforceOnRead: true, failOpen: false, |
| 253 | + }; |
| 254 | + const contextInput: SecurityContextConfigInput = { enabled: true }; |
| 255 | + expect(audit).toBeDefined(); |
| 256 | + expect(encryption).toBeDefined(); |
| 257 | + expect(masking).toBeDefined(); |
| 258 | + expect(correlation).toBeDefined(); |
| 259 | + expect(policy).toBeDefined(); |
| 260 | + expect(context).toBeDefined(); |
| 261 | + expect(contextInput).toBeDefined(); |
| 262 | + }); |
| 263 | +}); |
0 commit comments