From 46bf76331795005b0b6050081adc7c5ceca16b91 Mon Sep 17 00:00:00 2001 From: ahsrah7 Date: Sun, 20 Oct 2024 13:18:48 +0530 Subject: [PATCH] middleware tests added --- .../skipAuthorizeRolesWrapper.test.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/unit/middlewares/skipAuthorizeRolesWrapper.test.js diff --git a/test/unit/middlewares/skipAuthorizeRolesWrapper.test.js b/test/unit/middlewares/skipAuthorizeRolesWrapper.test.js new file mode 100644 index 0000000000..7c47334235 --- /dev/null +++ b/test/unit/middlewares/skipAuthorizeRolesWrapper.test.js @@ -0,0 +1,43 @@ +const chai = require("chai"); +const sinon = require("sinon"); +const { assert } = chai; +const skipAuthorizeRolesUnderFF = require("../../../middlewares/skipAuthorizeRolesWrapper"); + +describe("skipAuthorizeRolesUnderFF Middleware", function () { + let req, res, next, authorizeMiddleware; + + beforeEach(function () { + req = { query: {} }; + res = {}; + next = sinon.spy(); + authorizeMiddleware = sinon.spy(); + }); + + it("should call next() when dev is true", function () { + req.query.dev = "true"; + + const middleware = skipAuthorizeRolesUnderFF(authorizeMiddleware); + middleware(req, res, next); + + assert.isTrue(next.calledOnce, "next() should be called once"); + assert.isFalse(authorizeMiddleware.called, "authorizeMiddleware should not be called"); + }); + + it("should call authorizeMiddleware when dev is false", function () { + req.query.dev = "false"; + + const middleware = skipAuthorizeRolesUnderFF(authorizeMiddleware); + middleware(req, res, next); + + assert.isTrue(authorizeMiddleware.calledOnce, "authorizeMiddleware should be called once"); + assert.isFalse(next.called, "next() should not be called"); + }); + + it("should call authorizeMiddleware when dev is not provided", function () { + const middleware = skipAuthorizeRolesUnderFF(authorizeMiddleware); + middleware(req, res, next); + + assert.isTrue(authorizeMiddleware.calledOnce, "authorizeMiddleware should be called once"); + assert.isFalse(next.called, "next() should not be called"); + }); +});