74 lines
2.8 KiB
JavaScript
74 lines
2.8 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;
|
|
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 token and signature are valid', async () => {
|
|
mockReq = {
|
|
headers: { 'x-callback-token': routerToken },
|
|
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 401 if x-callback-token is invalid', async () => {
|
|
mockReq = {
|
|
headers: { 'x-callback-token': 'wrong-token' },
|
|
body: {}
|
|
};
|
|
|
|
await NotificationController.handle(mockReq, mockRes);
|
|
|
|
expect(responseHelper.error).toHaveBeenCalledWith(mockRes, expect.stringContaining('Unauthorized token'), 401);
|
|
});
|
|
|
|
it('should return 403 if signature invalid', async () => {
|
|
mockReq = {
|
|
headers: { 'x-callback-token': routerToken },
|
|
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 = {
|
|
headers: { 'x-callback-token': routerToken },
|
|
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'));
|
|
});
|
|
});
|