|
| 1 | +import { httpReplayIndexerEvents } from './admin.controllers'; |
| 2 | +import { emitAuditEvent } from '../../utils/audit.utils'; |
| 3 | +import { AdminRequest } from '../../middlewares/admin-guard.middleware'; |
| 4 | +import { Response } from 'express'; |
| 5 | + |
| 6 | +jest.mock('../../utils/prisma.utils', () => ({ |
| 7 | + prisma: { |
| 8 | + creatorProfile: { |
| 9 | + findUnique: jest.fn(), |
| 10 | + update: jest.fn(), |
| 11 | + }, |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('../../utils/audit.utils', () => ({ |
| 16 | + emitAuditEvent: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +describe('httpReplayIndexerEvents', () => { |
| 20 | + const next = jest.fn(); |
| 21 | + |
| 22 | + const createRes = (): Response => |
| 23 | + ({ |
| 24 | + status: jest.fn().mockReturnThis(), |
| 25 | + json: jest.fn(), |
| 26 | + }) as unknown as Response; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns validation error when dryRun is not a boolean', async () => { |
| 33 | + const req = { |
| 34 | + body: { startLedger: 10, dryRun: 'true' }, |
| 35 | + adminId: 'admin-1', |
| 36 | + } as unknown as AdminRequest; |
| 37 | + const res = createRes(); |
| 38 | + |
| 39 | + await httpReplayIndexerEvents(req, res, next); |
| 40 | + |
| 41 | + expect(res.status).toHaveBeenCalledWith(400); |
| 42 | + expect(res.json).toHaveBeenCalledWith( |
| 43 | + expect.objectContaining({ |
| 44 | + success: false, |
| 45 | + error: expect.objectContaining({ |
| 46 | + message: 'Invalid request body', |
| 47 | + details: expect.arrayContaining([expect.objectContaining({ field: 'dryRun' })]), |
| 48 | + }), |
| 49 | + }) |
| 50 | + ); |
| 51 | + expect(emitAuditEvent).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not emit audit event when dryRun=true', async () => { |
| 55 | + const req = { |
| 56 | + body: { startLedger: 20, dryRun: true }, |
| 57 | + adminId: 'admin-2', |
| 58 | + } as unknown as AdminRequest; |
| 59 | + const res = createRes(); |
| 60 | + |
| 61 | + await httpReplayIndexerEvents(req, res, next); |
| 62 | + |
| 63 | + expect(emitAuditEvent).not.toHaveBeenCalled(); |
| 64 | + expect(res.status).toHaveBeenCalledWith(200); |
| 65 | + expect(res.json).toHaveBeenCalledWith( |
| 66 | + expect.objectContaining({ |
| 67 | + success: true, |
| 68 | + data: expect.objectContaining({ |
| 69 | + type: 'INDEXER_REPLAY_INITIATED', |
| 70 | + startLedger: 20, |
| 71 | + dryRun: true, |
| 72 | + initiatedBy: 'admin-2', |
| 73 | + }), |
| 74 | + }) |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('emits audit event when dryRun=false', async () => { |
| 79 | + const req = { |
| 80 | + body: { startLedger: 30, dryRun: false }, |
| 81 | + adminId: 'admin-3', |
| 82 | + } as unknown as AdminRequest; |
| 83 | + const res = createRes(); |
| 84 | + |
| 85 | + await httpReplayIndexerEvents(req, res, next); |
| 86 | + |
| 87 | + expect(emitAuditEvent).toHaveBeenCalledWith( |
| 88 | + expect.objectContaining({ |
| 89 | + actor: 'admin-3', |
| 90 | + action: 'replay_indexer_events', |
| 91 | + targetId: '30', |
| 92 | + metadata: expect.objectContaining({ startLedger: 30, dryRun: false }), |
| 93 | + }) |
| 94 | + ); |
| 95 | + expect(res.status).toHaveBeenCalledWith(200); |
| 96 | + }); |
| 97 | +}); |
0 commit comments