From f1ea79e9ab820871cf9bfd2b03e960ed87f8606f Mon Sep 17 00:00:00 2001 From: Rishi <148757583+rishirishhh@users.noreply.github.com> Date: Wed, 18 Dec 2024 23:05:40 +0530 Subject: [PATCH 1/2] fix: deprecated GET task/self (#2284) * fix: deprecated GET task/self * fix: fixed the failing test * Update tasks.test.js * added warning message for deprecated route * added test cases for dev = false or when dev is not present * added a new error message * added error message to the controller * changes to the error code in test * correct warning message * Update tasks.test.js * Update tasks.test.js * added new warning message * test changes * Merge pull request #2292 from vikasosmium/deprecate-stocks-self-GET-route Added New Route to fetch User Stocks * Remove user.data() in firestore data set (#2282) * Updated set * Added Dev Feature Flag * Added variable for dev flag * Update dev feature flag to be used as boolean * Tests working of addOrUpdate feature when dev feature flag is true --------- Co-authored-by: Achintya Chatterjee <55826451+Achintya-Chatterjee@users.noreply.github.com> Co-authored-by: Prakash Choudhary <34452139+prakashchoudhary07@users.noreply.github.com> * new route definition minor changes * Update tasks.js * reverted errorMessages.ts * reverted tasks.js controller * reverted tasks.js router * updated tasks.test.js --------- Co-authored-by: Vikas Singh <59792866+vikasosmium@users.noreply.github.com> Co-authored-by: Rahul Goyal <76257739+RahulGoyal-tech@users.noreply.github.com> Co-authored-by: Achintya Chatterjee <55826451+Achintya-Chatterjee@users.noreply.github.com> Co-authored-by: Prakash Choudhary <34452139+prakashchoudhary07@users.noreply.github.com> --- controllers/tasks.js | 30 +++++++++++++++++++++--------- routes/tasks.js | 2 ++ test/integration/tasks.test.js | 8 ++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/controllers/tasks.js b/controllers/tasks.js index ca1557a7e..39ef4a3d2 100644 --- a/controllers/tasks.js +++ b/controllers/tasks.js @@ -251,20 +251,32 @@ const getUserTasks = async (req, res) => { * @param req {Object} - Express request object * @param res {Object} - Express response object */ + +/** + * @deprecated + * WARNING: This API endpoint is being deprecated and will be removed in future versions. + * Please use the updated API endpoint: `/tasks/:username` for retrieving user's task details. + * + * This API is kept temporarily for backward compatibility. + */ + const getSelfTasks = async (req, res) => { try { const { username } = req.userData; - if (username) { - if (req.query.completed) { - const allCompletedTasks = await tasks.fetchUserCompletedTasks(username); - return res.json(allCompletedTasks); - } else { - const allTasks = await tasks.fetchSelfTasks(username); - return res.json(allTasks); - } + if (!username) { + return res.boom.notFound("User doesn't exist"); } - return res.boom.notFound("User doesn't exist"); + + const tasksData = req.query.completed + ? await tasks.fetchUserCompletedTasks(username) + : await tasks.fetchSelfTasks(username); + + res.set( + "X-Deprecation-Warning", + "WARNING: This endpoint is deprecated and will be removed in the future. Please use /tasks/:username to get the task details." + ); + return res.json(tasksData); } catch (err) { logger.error(`Error while fetching tasks: ${err}`); return res.boom.badImplementation(INTERNAL_SERVER_ERROR); diff --git a/routes/tasks.js b/routes/tasks.js index 5596f982c..99c2fca6e 100644 --- a/routes/tasks.js +++ b/routes/tasks.js @@ -35,6 +35,7 @@ const enableDevModeMiddleware = (req, res, next) => { router.get("/", getTasksValidator, cacheResponse({ invalidationKey: ALL_TASKS, expiry: 10 }), tasks.fetchTasks); router.get("/self", authenticate, tasks.getSelfTasks); + router.get("/overdue", authenticate, authorizeRoles([SUPERUSER]), tasks.overdueTasks); router.post( "/", @@ -54,6 +55,7 @@ router.patch( ); router.get("/:id/details", tasks.getTask); router.get("/:username", tasks.getUserTasks); + router.patch( "/self/:id", authenticate, diff --git a/test/integration/tasks.test.js b/test/integration/tasks.test.js index 74d691e4d..550618dda 100644 --- a/test/integration/tasks.test.js +++ b/test/integration/tasks.test.js @@ -583,6 +583,10 @@ describe("Tasks", function () { return done; } expect(res).to.have.status(200); + expect(res).to.have.header( + "X-Deprecation-Warning", + "WARNING: This endpoint is deprecated and will be removed in the future. Please use /tasks/:username to get the task details." + ); expect(res.body).to.be.a("array"); expect(res.body[0].status).to.equal(COMPLETED); @@ -630,6 +634,10 @@ describe("Tasks", function () { .get("/tasks/self") .set("cookie", `${cookieName}=${authService.generateAuthToken({ userId: assignedUser })}`); expect(res).to.have.status(200); + expect(res).to.have.header( + "X-Deprecation-Warning", + "WARNING: This endpoint is deprecated and will be removed in the future. Please use /tasks/:username to get the task details." + ); expect(res.body).to.be.a("array"); expect([taskId1, taskId2]).to.include(taskId1); }); From 0df8007b5b45fe511471105662ccdc262ab95748 Mon Sep 17 00:00:00 2001 From: Vikas Singh <59792866+vikasosmium@users.noreply.github.com> Date: Wed, 18 Dec 2024 23:38:10 +0530 Subject: [PATCH 2/2] Added New PUT Route for User Intro (#2293) * added one put api for user intro * route checking fix * deprecate message comment * fixed test cases * added one userauthorization middleware and test cases fixes * added userAuthorization middleware * Revert "added one userauthorization middleware and test cases fixes" This reverts commit 7dcd8851a2ea941cf9a247bbfc6bcf549e496b7d. * Revert "Revert "added one userauthorization middleware and test cases fixes"" This reverts commit 238cd1755b865562be75c182d3ce0fb4c6bb98f3. * fixed test case msg --------- Co-authored-by: Achintya Chatterjee <55826451+Achintya-Chatterjee@users.noreply.github.com> Co-authored-by: Rishi <148757583+rishirishhh@users.noreply.github.com> --- routes/users.js | 12 +++- test/integration/users.test.js | 102 +++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/routes/users.js b/routes/users.js index 94d301cda..e38c2ca31 100644 --- a/routes/users.js +++ b/routes/users.js @@ -12,6 +12,8 @@ const { authorizeAndAuthenticate } = require("../middlewares/authorizeUsersAndSe const ROLES = require("../constants/roles"); const { Services } = require("../constants/bot"); const authenticateProfile = require("../middlewares/authenticateProfile"); +const { devFlagMiddleware } = require("../middlewares/devFlag"); +const { userAuthorization } = require("../middlewares/userAuthorization"); router.post("/", authorizeAndAuthenticate([ROLES.SUPERUSER], [Services.CRON_JOB_HANDLER]), users.markUnverified); router.post("/update-in-discord", authenticate, authorizeRoles([SUPERUSER]), users.setInDiscordScript); @@ -35,7 +37,15 @@ router.patch( ); router.get("/:username", users.getUser); router.get("/:userId/intro", authenticate, authorizeRoles([SUPERUSER]), users.getUserIntro); -router.put("/self/intro", authenticate, userValidator.validateJoinData, users.addUserIntro); +router.put("/self/intro", authenticate, userValidator.validateJoinData, users.addUserIntro); // This route is being deprecated soon, please use alternate available route `/users/:userId/intro`. +router.put( + "/:userId/intro", + devFlagMiddleware, + authenticate, + userValidator.validateJoinData, + userAuthorization, + users.addUserIntro +); router.get("/:id/skills", users.getUserSkills); router.get("/:id/badges", getUserBadges); router.patch( diff --git a/test/integration/users.test.js b/test/integration/users.test.js index 4c7143b8d..f607e0d61 100644 --- a/test/integration/users.test.js +++ b/test/integration/users.test.js @@ -1579,6 +1579,108 @@ describe("Users", function () { }); }); + describe("PUT /users/:userId/intro", function () { + let userStatusData; + + beforeEach(async function () { + await userStatusModel.updateUserStatus(userId, userStatusDataAfterSignup); + const updateStatus = await userStatusModel.updateUserStatus(userId, userStatusDataAfterFillingJoinSection); + userStatusData = (await firestore.collection("usersStatus").doc(updateStatus.id).get()).data(); + }); + + it("should return 409 if the data already present", function (done) { + addJoinData(joinData(userId)[3]); + chai + .request(app) + .put(`/users/${userId}/intro?dev=true`) + .set("Cookie", `${cookieName}=${jwt}`) + .send(joinData(userId)[3]) + .end((err, res) => { + if (err) { + return done(err); + } + expect(res).to.have.status(409); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.equal("User data is already present!"); + return done(); + }); + }); + + it("Should store the info in db", function (done) { + chai + .request(app) + .put(`/users/${userId}/intro?dev=true`) + .set("Cookie", `${cookieName}=${jwt}`) + .send(joinData()[2]) + .end((err, res) => { + if (err) { + return done(err); + } + expect(res).to.have.status(201); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.equal("User join data and newstatus data added and updated successfully"); + expect(userStatusData).to.have.own.property("currentStatus"); + expect(userStatusData).to.have.own.property("monthlyHours"); + expect(userStatusData.currentStatus.state).to.equal("ONBOARDING"); + expect(userStatusData.monthlyHours.committed).to.equal(40); + return done(); + }); + }); + + it("Should return 401 for Unauthenticated User Request", function (done) { + chai + .request(app) + .put(`/users/${userId}/intro?dev=true`) + .set("Cookie", `${cookieName}=""`) + .send(joinData()[2]) + .end((err, res) => { + if (err) { + return done(err); + } + expect(res).to.have.status(401); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.equal("Unauthenticated User"); + return done(); + }); + }); + + it("Should return 400 for invalid Data", function (done) { + chai + .request(app) + .put(`/users/${userId}/intro?dev=true`) + .set("Cookie", `${cookieName}=${jwt}`) + .send(joinData()[1]) + .end((err, res) => { + if (err) { + return done(err); + } + expect(res).to.have.status(400); + expect(res.body).to.be.a("object"); + expect(res.body.message).to.be.equal('"firstName" is required'); + return done(); + }); + }); + + it("Should return 403 for Forbidden access", function (done) { + const userId = "anotherUser123"; + addJoinData(joinData(userId)[3]); + + chai + .request(app) + .put(`/users/${userId}/intro?dev=true`) + .set("cookie", `${cookieName}=${jwt}`) + .send(joinData(userId)[3]) + .end((err, res) => { + if (err) return done(err); + + expect(res).to.have.status(403); + expect(res.body).to.be.an("object"); + expect(res.body.message).to.equal("Unauthorized access"); + return done(); + }); + }); + }); + describe("PATCH /users/rejectDiff", function () { let profileDiffsId;