feat: implement notification controller for handling incoming notifications with signature verification and add corresponding unit tests.
This commit is contained in:
@@ -5,12 +5,8 @@ class NotificationController {
|
|||||||
static async handle(req, res) {
|
static async handle(req, res) {
|
||||||
const payload = req.body;
|
const payload = req.body;
|
||||||
|
|
||||||
try {
|
|
||||||
const callbackToken = req.headers['x-callback-token'];
|
|
||||||
if (callbackToken !== process.env.ROUTER_CALLBACK_TOKEN) {
|
|
||||||
return responseHelper.error(res, "Unauthorized token router gateway", 401);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
const isValid = notificationService.verifySignature(payload);
|
const isValid = notificationService.verifySignature(payload);
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
return responseHelper.error(res, "Invalid Signature Key", 403);
|
return responseHelper.error(res, "Invalid Signature Key", 403);
|
||||||
|
|||||||
@@ -19,9 +19,8 @@ describe('NotificationController', () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle notification successfully when token and signature are valid', async () => {
|
it('should handle notification successfully when signature is valid', async () => {
|
||||||
mockReq = {
|
mockReq = {
|
||||||
headers: { 'x-callback-token': routerToken },
|
|
||||||
body: { order_id: 'I-123', transaction_status: 'settlement' }
|
body: { order_id: 'I-123', transaction_status: 'settlement' }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,20 +34,9 @@ describe('NotificationController', () => {
|
|||||||
expect(responseHelper.success).toHaveBeenCalledWith(mockRes, expect.stringContaining('Handled'));
|
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 () => {
|
it('should return 403 if signature invalid', async () => {
|
||||||
mockReq = {
|
mockReq = {
|
||||||
headers: { 'x-callback-token': routerToken },
|
|
||||||
body: { signature_key: 'invalid' }
|
body: { signature_key: 'invalid' }
|
||||||
};
|
};
|
||||||
notificationService.verifySignature.mockReturnValue(false);
|
notificationService.verifySignature.mockReturnValue(false);
|
||||||
@@ -60,7 +48,6 @@ describe('NotificationController', () => {
|
|||||||
|
|
||||||
it('should return error on service error', async () => {
|
it('should return error on service error', async () => {
|
||||||
mockReq = {
|
mockReq = {
|
||||||
headers: { 'x-callback-token': routerToken },
|
|
||||||
body: { order_id: 'I-123' }
|
body: { order_id: 'I-123' }
|
||||||
};
|
};
|
||||||
notificationService.verifySignature.mockReturnValue(true);
|
notificationService.verifySignature.mockReturnValue(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user