54 lines
2.1 KiB
JavaScript
54 lines
2.1 KiB
JavaScript
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;
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
mockRes = {
|
|
status: jest.fn().mockReturnThis(),
|
|
json: jest.fn().mockReturnThis()
|
|
};
|
|
});
|
|
|
|
it('should handle notification successfully', 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'));
|
|
});
|
|
});
|