const notificationService = require('../../app/modules/notification/services/notification.service'); const responseHelper = require('../../app/helpers/response.helper'); jest.mock('../../app/modules/notification/services/notification.service'); jest.mock('../../app/helpers/response.helper'); const NotificationController = require('../../app/modules/notification/controllers/notification.controller'); describe('NotificationController', () => { let mockReq, mockRes; const routerToken = 'TEST_ROUTER_TOKEN'; beforeEach(() => { jest.clearAllMocks(); process.env.ROUTER_CALLBACK_TOKEN = routerToken; mockRes = { status: jest.fn().mockReturnThis(), json: jest.fn().mockReturnThis() }; }); it('should handle notification successfully when signature is valid', async () => { mockReq = { body: { order_id: 'I-123', transaction_status: 'settlement' } }; notificationService.verifySignature.mockReturnValue(true); notificationService.processNotification.mockResolvedValue(); await NotificationController.handle(mockReq, mockRes); expect(notificationService.verifySignature).toHaveBeenCalledWith(mockReq.body); expect(notificationService.processNotification).toHaveBeenCalledWith(mockReq.body); expect(responseHelper.success).toHaveBeenCalledWith(mockRes, expect.stringContaining('Handled')); }); it('should return 403 if signature invalid', async () => { mockReq = { body: { signature_key: 'invalid' } }; notificationService.verifySignature.mockReturnValue(false); await NotificationController.handle(mockReq, mockRes); expect(responseHelper.error).toHaveBeenCalledWith(mockRes, expect.stringContaining('Invalid Signature'), 403); }); it('should return error on service error', async () => { mockReq = { body: { order_id: 'I-123' } }; notificationService.verifySignature.mockReturnValue(true); notificationService.processNotification.mockRejectedValue(new Error('DB Error')); await NotificationController.handle(mockReq, mockRes); expect(responseHelper.error).toHaveBeenCalledWith(mockRes, expect.stringContaining('DB Error')); }); });