|
| 1 | +import { |
| 2 | + AWS_SCAN_MODES, |
| 3 | + DEFAULT_AWS_SCAN_MODE, |
| 4 | + isSecurityHubMode, |
| 5 | + resolveAwsScanMode, |
| 6 | +} from './aws-scan-mode'; |
| 7 | + |
| 8 | +describe('aws-scan-mode', () => { |
| 9 | + describe('resolveAwsScanMode', () => { |
| 10 | + it('returns "security_hub" when the value is exactly that string', () => { |
| 11 | + expect(resolveAwsScanMode('security_hub')).toBe('security_hub'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('returns the default for "comp_scanners"', () => { |
| 15 | + expect(resolveAwsScanMode('comp_scanners')).toBe('comp_scanners'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns the default for unknown strings', () => { |
| 19 | + // Defensive — typos / future modes / corrupted JSON variables must |
| 20 | + // never accidentally activate Security Hub mode. |
| 21 | + expect(resolveAwsScanMode('SECURITY_HUB')).toBe(DEFAULT_AWS_SCAN_MODE); |
| 22 | + expect(resolveAwsScanMode('securityhub')).toBe(DEFAULT_AWS_SCAN_MODE); |
| 23 | + expect(resolveAwsScanMode('xyz')).toBe(DEFAULT_AWS_SCAN_MODE); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns the default for missing / non-string values', () => { |
| 27 | + expect(resolveAwsScanMode(undefined)).toBe(DEFAULT_AWS_SCAN_MODE); |
| 28 | + expect(resolveAwsScanMode(null)).toBe(DEFAULT_AWS_SCAN_MODE); |
| 29 | + expect(resolveAwsScanMode(0)).toBe(DEFAULT_AWS_SCAN_MODE); |
| 30 | + expect(resolveAwsScanMode({})).toBe(DEFAULT_AWS_SCAN_MODE); |
| 31 | + expect(resolveAwsScanMode([])).toBe(DEFAULT_AWS_SCAN_MODE); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('isSecurityHubMode', () => { |
| 36 | + it('is true only for the exact "security_hub" string', () => { |
| 37 | + expect(isSecurityHubMode('security_hub')).toBe(true); |
| 38 | + expect(isSecurityHubMode('comp_scanners')).toBe(false); |
| 39 | + expect(isSecurityHubMode(undefined)).toBe(false); |
| 40 | + expect(isSecurityHubMode('SECURITY_HUB')).toBe(false); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('DEFAULT_AWS_SCAN_MODE', () => { |
| 45 | + it('is "comp_scanners" — today\'s behavior is the safe default', () => { |
| 46 | + // This is intentionally guarded by a test: changing the default |
| 47 | + // would silently shift production behavior for every existing |
| 48 | + // connection that does not have an explicit scan mode set. |
| 49 | + expect(DEFAULT_AWS_SCAN_MODE).toBe('comp_scanners'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('AWS_SCAN_MODES', () => { |
| 54 | + it('lists exactly the two known modes (source of truth for DTOs / validators)', () => { |
| 55 | + // If a new mode is added, this test must change in lockstep with |
| 56 | + // the AwsScanMode union — the DTO `@IsIn(AWS_SCAN_MODES)` will |
| 57 | + // automatically pick up the new value, but reviewers should see |
| 58 | + // this test fail as a sanity check that the list was updated. |
| 59 | + expect([...AWS_SCAN_MODES]).toEqual(['comp_scanners', 'security_hub']); |
| 60 | + }); |
| 61 | + |
| 62 | + it('contains the default mode', () => { |
| 63 | + // Guards against future refactors that might remove the default |
| 64 | + // from the list — would break validation for every existing |
| 65 | + // connection that lacks an explicit mode. |
| 66 | + expect([...AWS_SCAN_MODES]).toContain(DEFAULT_AWS_SCAN_MODE); |
| 67 | + }); |
| 68 | + |
| 69 | + it('every entry passes resolveAwsScanMode round-trip (self-consistency)', () => { |
| 70 | + // Catches drift if someone adds a mode to the array without |
| 71 | + // updating resolveAwsScanMode. |
| 72 | + for (const mode of AWS_SCAN_MODES) { |
| 73 | + expect(resolveAwsScanMode(mode)).toBe(mode); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments