|
| 1 | +import { Test } from '@nestjs/testing'; |
| 2 | +import { AuditService } from './audit.service'; |
| 3 | +import { PrismaService } from '../prisma/prisma.service'; |
| 4 | + |
| 5 | +const FIXTURES = [ |
| 6 | + { id: 'a1', actor: 'GABC', action: 'reindex', ipAddress: '1.1.1.1', createdAt: new Date('2024-01-01T10:00:00Z'), payload: {} }, |
| 7 | + { id: 'a2', actor: 'GABC', action: 'pause', ipAddress: '1.1.1.1', createdAt: new Date('2024-01-02T10:00:00Z'), payload: {} }, |
| 8 | + { id: 'a3', actor: 'GXYZ', action: 'reindex', ipAddress: '2.2.2.2', createdAt: new Date('2024-01-03T10:00:00Z'), payload: {} }, |
| 9 | + { id: 'a4', actor: 'GXYZ', action: 'feature_flag_update', ipAddress: '2.2.2.2', createdAt: new Date('2024-01-04T10:00:00Z'), payload: {} }, |
| 10 | + { id: 'a5', actor: 'GABC', action: 'reindex', ipAddress: '1.1.1.1', createdAt: new Date('2024-01-05T10:00:00Z'), payload: {} }, |
| 11 | +]; |
| 12 | + |
| 13 | +function buildPrismaMock() { |
| 14 | + return { |
| 15 | + adminAuditLog: { |
| 16 | + create: jest.fn(), |
| 17 | + findMany: jest.fn(), |
| 18 | + count: jest.fn(), |
| 19 | + }, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe('AuditService', () => { |
| 24 | + let service: AuditService; |
| 25 | + let prisma: ReturnType<typeof buildPrismaMock>; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + prisma = buildPrismaMock(); |
| 29 | + const module = await Test.createTestingModule({ |
| 30 | + providers: [ |
| 31 | + AuditService, |
| 32 | + { provide: PrismaService, useValue: prisma }, |
| 33 | + ], |
| 34 | + }).compile(); |
| 35 | + service = module.get(AuditService); |
| 36 | + }); |
| 37 | + |
| 38 | + // ── write ────────────────────────────────────────────────────────────────── |
| 39 | + |
| 40 | + it('write creates a row', async () => { |
| 41 | + prisma.adminAuditLog.create.mockResolvedValue({}); |
| 42 | + await service.write({ actor: 'GABC', action: 'test', payload: {} }); |
| 43 | + expect(prisma.adminAuditLog.create).toHaveBeenCalledWith({ |
| 44 | + data: { actor: 'GABC', action: 'test', payload: {} }, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + // ── findAll filters ──────────────────────────────────────────────────────── |
| 49 | + |
| 50 | + it('returns all rows when no filters', async () => { |
| 51 | + prisma.adminAuditLog.findMany.mockResolvedValue(FIXTURES.slice(0, 20)); |
| 52 | + const result = await service.findAll({ limit: 20 }); |
| 53 | + expect(prisma.adminAuditLog.findMany).toHaveBeenCalledWith( |
| 54 | + expect.objectContaining({ where: {} }), |
| 55 | + ); |
| 56 | + expect(result.items).toHaveLength(FIXTURES.slice(0, 20).length); |
| 57 | + }); |
| 58 | + |
| 59 | + it('filters by action', async () => { |
| 60 | + const filtered = FIXTURES.filter((f) => f.action === 'reindex'); |
| 61 | + prisma.adminAuditLog.findMany.mockResolvedValue(filtered); |
| 62 | + const result = await service.findAll({ action: 'reindex' }); |
| 63 | + expect(prisma.adminAuditLog.findMany).toHaveBeenCalledWith( |
| 64 | + expect.objectContaining({ where: { action: 'reindex' } }), |
| 65 | + ); |
| 66 | + expect(result.items).toEqual(filtered); |
| 67 | + }); |
| 68 | + |
| 69 | + it('filters by actor', async () => { |
| 70 | + const filtered = FIXTURES.filter((f) => f.actor === 'GABC'); |
| 71 | + prisma.adminAuditLog.findMany.mockResolvedValue(filtered); |
| 72 | + const result = await service.findAll({ actor: 'GABC' }); |
| 73 | + expect(prisma.adminAuditLog.findMany).toHaveBeenCalledWith( |
| 74 | + expect.objectContaining({ where: { actor: 'GABC' } }), |
| 75 | + ); |
| 76 | + expect(result.items).toEqual(filtered); |
| 77 | + }); |
| 78 | + |
| 79 | + it('filters by date range', async () => { |
| 80 | + const filtered = FIXTURES.filter( |
| 81 | + (f) => f.createdAt >= new Date('2024-01-02T00:00:00Z') && f.createdAt <= new Date('2024-01-04T23:59:59Z'), |
| 82 | + ); |
| 83 | + prisma.adminAuditLog.findMany.mockResolvedValue(filtered); |
| 84 | + const result = await service.findAll({ from: '2024-01-02T00:00:00Z', to: '2024-01-04T23:59:59Z' }); |
| 85 | + expect(prisma.adminAuditLog.findMany).toHaveBeenCalledWith( |
| 86 | + expect.objectContaining({ |
| 87 | + where: { |
| 88 | + createdAt: { |
| 89 | + gte: new Date('2024-01-02T00:00:00Z'), |
| 90 | + lte: new Date('2024-01-04T23:59:59Z'), |
| 91 | + }, |
| 92 | + }, |
| 93 | + }), |
| 94 | + ); |
| 95 | + expect(result.items).toEqual(filtered); |
| 96 | + }); |
| 97 | + |
| 98 | + it('combines action + actor filters', async () => { |
| 99 | + const filtered = FIXTURES.filter((f) => f.action === 'reindex' && f.actor === 'GABC'); |
| 100 | + prisma.adminAuditLog.findMany.mockResolvedValue(filtered); |
| 101 | + const result = await service.findAll({ action: 'reindex', actor: 'GABC' }); |
| 102 | + expect(prisma.adminAuditLog.findMany).toHaveBeenCalledWith( |
| 103 | + expect.objectContaining({ where: { action: 'reindex', actor: 'GABC' } }), |
| 104 | + ); |
| 105 | + expect(result.items).toEqual(filtered); |
| 106 | + }); |
| 107 | + |
| 108 | + // ── cursor pagination ────────────────────────────────────────────────────── |
| 109 | + |
| 110 | + it('returns hasMore=false when results fit in one page', async () => { |
| 111 | + prisma.adminAuditLog.findMany.mockResolvedValue(FIXTURES.slice(0, 3)); |
| 112 | + const result = await service.findAll({ limit: 5 }); |
| 113 | + expect(result.hasMore).toBe(false); |
| 114 | + expect(result.nextCursor).toBeNull(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns hasMore=true and nextCursor when more rows exist', async () => { |
| 118 | + // take=2+1=3 rows returned, meaning there are more |
| 119 | + const page = [...FIXTURES.slice(0, 2), FIXTURES[2]]; |
| 120 | + prisma.adminAuditLog.findMany.mockResolvedValue(page); |
| 121 | + const result = await service.findAll({ limit: 2 }); |
| 122 | + expect(result.hasMore).toBe(true); |
| 123 | + expect(result.nextCursor).toBe(FIXTURES[1].id); |
| 124 | + expect(result.items).toHaveLength(2); |
| 125 | + }); |
| 126 | + |
| 127 | + it('passes cursor to prisma when provided', async () => { |
| 128 | + prisma.adminAuditLog.findMany.mockResolvedValue([]); |
| 129 | + await service.findAll({ cursor: 'a2', limit: 2 }); |
| 130 | + expect(prisma.adminAuditLog.findMany).toHaveBeenCalledWith( |
| 131 | + expect.objectContaining({ cursor: { id: 'a2' }, skip: 1 }), |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + // ── streamCsv ────────────────────────────────────────────────────────────── |
| 136 | + |
| 137 | + it('streams CSV rows and ends response', async () => { |
| 138 | + prisma.adminAuditLog.findMany |
| 139 | + .mockResolvedValueOnce([ |
| 140 | + { id: 'a1', actor: 'GABC', action: 'reindex', ipAddress: '1.1.1.1', createdAt: new Date('2024-01-01T10:00:00Z') }, |
| 141 | + ]) |
| 142 | + .mockResolvedValueOnce([]); // second batch empty → done |
| 143 | + |
| 144 | + const chunks: string[] = []; |
| 145 | + const res = { |
| 146 | + setHeader: jest.fn(), |
| 147 | + write: jest.fn((chunk: string) => chunks.push(chunk)), |
| 148 | + end: jest.fn(), |
| 149 | + } as unknown as import('express').Response; |
| 150 | + |
| 151 | + await service.streamCsv({}, res); |
| 152 | + |
| 153 | + expect(res.setHeader).toHaveBeenCalledWith('Content-Type', 'text/csv'); |
| 154 | + expect(chunks[0]).toBe('id,actor,action,ipAddress,createdAt\n'); |
| 155 | + expect(chunks[1]).toContain('a1,GABC,reindex'); |
| 156 | + expect(res.end).toHaveBeenCalled(); |
| 157 | + }); |
| 158 | + |
| 159 | + it('CSV export matches JSON results for same filters', async () => { |
| 160 | + const rows = [ |
| 161 | + { id: 'a3', actor: 'GXYZ', action: 'reindex', ipAddress: '2.2.2.2', createdAt: new Date('2024-01-03T10:00:00Z') }, |
| 162 | + ]; |
| 163 | + // findAll returns same row |
| 164 | + prisma.adminAuditLog.findMany |
| 165 | + .mockResolvedValueOnce([...rows, rows[0]]) // +1 for hasMore check in findAll |
| 166 | + .mockResolvedValueOnce(rows) // first batch in streamCsv |
| 167 | + .mockResolvedValueOnce([]); // second batch empty |
| 168 | + |
| 169 | + const jsonResult = await service.findAll({ action: 'reindex', actor: 'GXYZ', limit: 1 }); |
| 170 | + |
| 171 | + const chunks: string[] = []; |
| 172 | + const res = { |
| 173 | + setHeader: jest.fn(), |
| 174 | + write: jest.fn((c: string) => chunks.push(c)), |
| 175 | + end: jest.fn(), |
| 176 | + } as unknown as import('express').Response; |
| 177 | + await service.streamCsv({ action: 'reindex', actor: 'GXYZ' }, res); |
| 178 | + |
| 179 | + const csvLines = chunks.join('').split('\n').filter(Boolean).slice(1); // skip header |
| 180 | + expect(csvLines).toHaveLength(jsonResult.items.length); |
| 181 | + expect(csvLines[0]).toContain(jsonResult.items[0].id); |
| 182 | + }); |
| 183 | +}); |
0 commit comments