|
| 1 | +jest.mock('@db', () => ({ |
| 2 | + db: { findingException: { findMany: jest.fn() } }, |
| 3 | +})); |
| 4 | + |
| 5 | +import { db } from '@db'; |
| 6 | +import { |
| 7 | + ActiveExceptionSet, |
| 8 | + loadActiveExceptionSet, |
| 9 | +} from './finding-exceptions'; |
| 10 | + |
| 11 | +const findMany = jest.mocked(db.findingException.findMany); |
| 12 | + |
| 13 | +describe('ActiveExceptionSet', () => { |
| 14 | + it('matches by (connectionId, checkId, resourceId)', () => { |
| 15 | + const set = new ActiveExceptionSet([ |
| 16 | + ActiveExceptionSet.key('c1', 'aws-s3-public-access', 'bucket-1'), |
| 17 | + ]); |
| 18 | + expect(set.has('c1', 'aws-s3-public-access', 'bucket-1')).toBe(true); |
| 19 | + expect(set.has('c1', 'aws-s3-public-access', 'bucket-2')).toBe(false); |
| 20 | + expect(set.has('c2', 'aws-s3-public-access', 'bucket-1')).toBe(false); |
| 21 | + expect(set.size).toBe(1); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('loadActiveExceptionSet', () => { |
| 26 | + beforeEach(() => jest.clearAllMocks()); |
| 27 | + |
| 28 | + it('builds the set from active exceptions', async () => { |
| 29 | + findMany.mockResolvedValue([ |
| 30 | + { |
| 31 | + connectionId: 'c1', |
| 32 | + checkId: 'aws-s3-public-access', |
| 33 | + resourceId: 'bucket-1', |
| 34 | + }, |
| 35 | + ] as never); |
| 36 | + |
| 37 | + const set = await loadActiveExceptionSet('org_1'); |
| 38 | + |
| 39 | + expect(set.has('c1', 'aws-s3-public-access', 'bucket-1')).toBe(true); |
| 40 | + // Query only active exceptions (not revoked, not expired). |
| 41 | + expect(findMany).toHaveBeenCalledWith( |
| 42 | + expect.objectContaining({ |
| 43 | + where: expect.objectContaining({ |
| 44 | + organizationId: 'org_1', |
| 45 | + revokedAt: null, |
| 46 | + }), |
| 47 | + }), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('fail-safe: returns an empty set if the lookup throws (suppress nothing)', async () => { |
| 52 | + findMany.mockRejectedValue(new Error('db down')); |
| 53 | + const set = await loadActiveExceptionSet('org_1'); |
| 54 | + expect(set.size).toBe(0); |
| 55 | + expect(set.has('c1', 'aws-s3-public-access', 'bucket-1')).toBe(false); |
| 56 | + }); |
| 57 | +}); |
0 commit comments