|
| 1 | +/** |
| 2 | + * Unit tests for OrgAuditService |
| 3 | + * |
| 4 | + * Uses a mock pg Pool so no real database connection is required. |
| 5 | + */ |
| 6 | + |
| 7 | +import { OrgAuditService } from '../services/orgAuditService.js'; |
| 8 | + |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | +// Helpers |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | + |
| 13 | +function makePool(queryResult: { rows: unknown[]; rowCount?: number }) { |
| 14 | + return { |
| 15 | + query: jest.fn().mockResolvedValue(queryResult), |
| 16 | + } as unknown as import('pg').Pool; |
| 17 | +} |
| 18 | + |
| 19 | +// --------------------------------------------------------------------------- |
| 20 | +// Tests |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | + |
| 23 | +describe('OrgAuditService', () => { |
| 24 | + describe('log()', () => { |
| 25 | + it('inserts a row with the correct parameters', async () => { |
| 26 | + const mockRow = { id: '1', organization_id: 42, change_type: 'setting_upserted' }; |
| 27 | + const mockPool = makePool({ rows: [mockRow] }); |
| 28 | + const service = new OrgAuditService(mockPool); |
| 29 | + |
| 30 | + const result = await service.log({ |
| 31 | + organizationId: 42, |
| 32 | + changeType: 'setting_upserted', |
| 33 | + configKey: 'payment_settings', |
| 34 | + oldValue: { currency: 'USD' }, |
| 35 | + newValue: { currency: 'EUR' }, |
| 36 | + actorId: 7, |
| 37 | + actorEmail: 'admin@example.com', |
| 38 | + actorIp: '127.0.0.1', |
| 39 | + }); |
| 40 | + |
| 41 | + expect(mockPool.query).toHaveBeenCalledTimes(1); |
| 42 | + const [sql, params] = (mockPool.query as jest.Mock).mock.calls[0]; |
| 43 | + expect(sql).toContain('INSERT INTO org_audit_log'); |
| 44 | + expect(params[0]).toBe(42); // organizationId |
| 45 | + expect(params[1]).toBe('setting_upserted'); // changeType |
| 46 | + expect(params[2]).toBe('payment_settings'); // configKey |
| 47 | + expect(JSON.parse(params[3])).toEqual({ currency: 'USD' }); // oldValue |
| 48 | + expect(JSON.parse(params[4])).toEqual({ currency: 'EUR' }); // newValue |
| 49 | + expect(params[5]).toBe(7); // actorId |
| 50 | + expect(params[6]).toBe('admin@example.com'); // actorEmail |
| 51 | + expect(result).toEqual(mockRow); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns null and does not throw when the insert fails', async () => { |
| 55 | + const mockPool = { |
| 56 | + query: jest.fn().mockRejectedValue(new Error('DB error')), |
| 57 | + } as unknown as import('pg').Pool; |
| 58 | + const service = new OrgAuditService(mockPool); |
| 59 | + |
| 60 | + const result = await service.log({ organizationId: 1, changeType: 'name_updated' }); |
| 61 | + expect(result).toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('stores null config_key when not provided', async () => { |
| 65 | + const mockPool = makePool({ rows: [{ id: '2' }] }); |
| 66 | + const service = new OrgAuditService(mockPool); |
| 67 | + |
| 68 | + await service.log({ organizationId: 1, changeType: 'name_updated', newValue: 'Acme' }); |
| 69 | + |
| 70 | + const params = (mockPool.query as jest.Mock).mock.calls[0][1]; |
| 71 | + expect(params[2]).toBeNull(); // configKey |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('list()', () => { |
| 76 | + it('returns rows and total count', async () => { |
| 77 | + const mockRows = [ |
| 78 | + { id: '1', organization_id: 10, change_type: 'setting_upserted', created_at: '2025-01-01' }, |
| 79 | + ]; |
| 80 | + const mockPool = { |
| 81 | + query: jest |
| 82 | + .fn() |
| 83 | + .mockResolvedValueOnce({ rows: mockRows }) // data query |
| 84 | + .mockResolvedValueOnce({ rows: [{ count: '1' }] }), // count query |
| 85 | + } as unknown as import('pg').Pool; |
| 86 | + const service = new OrgAuditService(mockPool); |
| 87 | + |
| 88 | + const { rows, total } = await service.list(10); |
| 89 | + expect(rows).toEqual(mockRows); |
| 90 | + expect(total).toBe(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('caps limit at 200', async () => { |
| 94 | + const mockPool = { |
| 95 | + query: jest |
| 96 | + .fn() |
| 97 | + .mockResolvedValue({ rows: [] }), |
| 98 | + } as unknown as import('pg').Pool; |
| 99 | + const service = new OrgAuditService(mockPool); |
| 100 | + |
| 101 | + await service.list(1, { limit: 9999 }); |
| 102 | + const params = (mockPool.query as jest.Mock).mock.calls[0][1]; |
| 103 | + expect(params[1]).toBe(200); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments