From d60002ba7760243df2ec3bbb5ccef82ccd45315f Mon Sep 17 00:00:00 2001 From: vikasosmium <59792866+vikasosmium@users.noreply.github.com> Date: Wed, 18 Dec 2024 12:50:54 +0530 Subject: [PATCH 1/3] added one get api to get user extension req --- routes/extensionRequests.js | 9 ++ test/integration/extensionRequests.test.js | 104 +++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/routes/extensionRequests.js b/routes/extensionRequests.js index e44e65c82..e7d9186e9 100644 --- a/routes/extensionRequests.js +++ b/routes/extensionRequests.js @@ -11,10 +11,19 @@ const { getExtensionRequestsValidator, } = require("../middlewares/validators/extensionRequests"); const skipAuthorizeRolesUnderFF = require("../middlewares/skipAuthorizeRolesWrapper"); +const { userAuthorization } = require("../middlewares/userAuthorization"); +const { devFlagMiddleware } = require("../middlewares/devFlag"); router.post("/", authenticate, createExtensionRequest, extensionRequests.createTaskExtensionRequest); router.get("/", authenticate, getExtensionRequestsValidator, extensionRequests.fetchExtensionRequests); router.get("/self", authenticate, extensionRequests.getSelfExtensionRequests); +router.get( + "/user/:userId", + devFlagMiddleware, + authenticate, + userAuthorization, + extensionRequests.getSelfExtensionRequests +); router.get("/:id", authenticate, authorizeRoles([SUPERUSER, APPOWNER]), extensionRequests.getExtensionRequest); // remove the skipAuthorizeRolesUnderFF & authorizeRoles middleware when removing the feature flag router.patch( diff --git a/test/integration/extensionRequests.test.js b/test/integration/extensionRequests.test.js index 29a5ad255..bf64c44ab 100644 --- a/test/integration/extensionRequests.test.js +++ b/test/integration/extensionRequests.test.js @@ -362,6 +362,110 @@ describe("Extension Requests", function () { }); }); + describe("GET /extension-requests/user/:userId", function () { + it("should return success response and extension request of the authenticated user", function (done) { + chai + .request(app) + .get(`/extension-requests/user/${appOwner.id}?dev=true`) + .set("cookie", `${cookieName}=${appOwnerjwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); + expect(res.body.allExtensionRequests).to.be.a("array"); + expect(res.body.allExtensionRequests[0].assignee).to.equal(appOwner.username); + expect([extensionRequestId1, extensionRequestId2]).contains(res.body.allExtensionRequests[0].id); + expect([extensionRequestId1, extensionRequestId2]).contains(res.body.allExtensionRequests[1].id); + expect(res.body.allExtensionRequests[1].assignee).to.equal(appOwner.username); + return done(); + }); + }); + + it("should return success response and all extension requests with query params", function (done) { + chai + .request(app) + .get(`/extension-requests/user/${appOwner.id}?dev=true`) + .query({ taskId: taskId2, status: "APPROVED" }) + .set("cookie", `${cookieName}=${appOwnerjwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); + expect(res.body.allExtensionRequests).to.be.a("array"); + expect(res.body.allExtensionRequests[0].assignee).to.equal(appOwner.username); + expect(res.body.allExtensionRequests[0].id).to.equal(extensionRequestId2); + return done(); + }); + }); + + it("should return success response and an empty array of extensionRequest if assignee is not same as latest one", function (done) { + chai + .request(app) + .get(`/extension-requests/user/${appOwner.id}?dev=true`) + .query({ taskId: taskId7 }) + .set("cookie", `${cookieName}=${appOwnerjwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); + expect(res.body.allExtensionRequests).to.be.a("array").with.lengthOf(0); + return done(); + }); + }); + + it("should return success response and a single latestExtensionRequest if assignee same as latest one", function (done) { + chai + .request(app) + .get(`/extension-requests/user/${appOwner.id}?dev=true`) + .query({ taskId: taskId2 }) + .set("cookie", `${cookieName}=${appOwnerjwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(200); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.be.equal("Extension Requests returned successfully!"); + expect(res.body.allExtensionRequests).to.be.a("array").with.lengthOf(1); + return done(); + }); + }); + + it("Should return 401 if not logged in", function (done) { + chai + .request(app) + .get(`/extension-requests/user/${appOwner.id}?dev=true`) + .end((err, res) => { + if (err) { + return done(); + } + + expect(res).to.have.status(401); + expect(res.body).to.be.an("object"); + expect(res.body).to.eql({ + statusCode: 401, + error: "Unauthorized", + message: "Unauthenticated User", + }); + + return done(); + }); + }); + }); + describe("POST /extension-requests - creates a new extension requests", function () { it("Should return success response after adding the extension request", function (done) { chai From d678d3f57246e6979913fc1d3b5f260b8ce3efa0 Mon Sep 17 00:00:00 2001 From: vikasosmium <59792866+vikasosmium@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:22:35 +0530 Subject: [PATCH 2/3] added deprecation warning --- controllers/extensionRequests.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/controllers/extensionRequests.js b/controllers/extensionRequests.js index f4ebc0d97..314850e17 100644 --- a/controllers/extensionRequests.js +++ b/controllers/extensionRequests.js @@ -149,6 +149,11 @@ const getSelfExtensionRequests = async (req, res) => { const { id: userId } = req.userData; const { taskId, status } = req.query; + res.set( + "X-Deprecation-Warning", + "WARNING: This endpoint is being deprecated and will be removed in the future. Please use `/extension-requests/user/:userId` route to get the user extension-requests details based on userID." + ); + if (userId) { let allExtensionRequests; if (taskId) { From 3cccc93e14b47bbba7504f4302d5b55409f7e76d Mon Sep 17 00:00:00 2001 From: vikasosmium <59792866+vikasosmium@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:35:54 +0530 Subject: [PATCH 3/3] added warning for self route --- controllers/extensionRequests.js | 5 ----- routes/extensionRequests.js | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/controllers/extensionRequests.js b/controllers/extensionRequests.js index 314850e17..f4ebc0d97 100644 --- a/controllers/extensionRequests.js +++ b/controllers/extensionRequests.js @@ -149,11 +149,6 @@ const getSelfExtensionRequests = async (req, res) => { const { id: userId } = req.userData; const { taskId, status } = req.query; - res.set( - "X-Deprecation-Warning", - "WARNING: This endpoint is being deprecated and will be removed in the future. Please use `/extension-requests/user/:userId` route to get the user extension-requests details based on userID." - ); - if (userId) { let allExtensionRequests; if (taskId) { diff --git a/routes/extensionRequests.js b/routes/extensionRequests.js index e7d9186e9..693e7107d 100644 --- a/routes/extensionRequests.js +++ b/routes/extensionRequests.js @@ -16,7 +16,7 @@ const { devFlagMiddleware } = require("../middlewares/devFlag"); router.post("/", authenticate, createExtensionRequest, extensionRequests.createTaskExtensionRequest); router.get("/", authenticate, getExtensionRequestsValidator, extensionRequests.fetchExtensionRequests); -router.get("/self", authenticate, extensionRequests.getSelfExtensionRequests); +router.get("/self", authenticate, extensionRequests.getSelfExtensionRequests); // This endpoint is being deprecated. Please use `/extension-requests/user/:userId` route to get the user extension-requests details based on userID." router.get( "/user/:userId", devFlagMiddleware,