From 287485a5c5eb9e66c514c4d8730ea44d77fde96c Mon Sep 17 00:00:00 2001 From: Akshat187 Date: Sat, 14 Dec 2024 23:47:18 +0530 Subject: [PATCH 1/7] updated the condition for taskrequestdata when it is not found --- models/taskRequests.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/models/taskRequests.js b/models/taskRequests.js index 6bc5c8572..3a596e498 100644 --- a/models/taskRequests.js +++ b/models/taskRequests.js @@ -171,10 +171,16 @@ const fetchTaskRequestById = async (taskRequestId) => { const taskRequestSnapshot = await taskRequestsCollection.doc(taskRequestId).get(); const taskRequestData = taskRequestSnapshot.data(); - if (taskRequestData) { - taskRequestData.id = taskRequestSnapshot.id; - taskRequestData.url = new URL(`/taskRequests/${taskRequestData.id}`, config.get("services.rdsUi.baseUrl")); + if (!taskRequestData){ + return { + taskRequestData, + taskRequestExists: false, + }; } + + taskRequestData.id = taskRequestSnapshot.id; + taskRequestData.url = new URL(`/taskRequests/${taskRequestData.id}`, config.get("services.rdsUi.baseUrl")); + return { taskRequestData, taskRequestExists: true, From 42a8f863519f9ab881df804af9ea547f1a845f29 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Thu, 19 Dec 2024 09:42:06 +0530 Subject: [PATCH 2/7] lint-fix --- models/taskRequests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/taskRequests.js b/models/taskRequests.js index 3a596e498..96e88b68e 100644 --- a/models/taskRequests.js +++ b/models/taskRequests.js @@ -171,13 +171,13 @@ const fetchTaskRequestById = async (taskRequestId) => { const taskRequestSnapshot = await taskRequestsCollection.doc(taskRequestId).get(); const taskRequestData = taskRequestSnapshot.data(); - if (!taskRequestData){ + if (!taskRequestData) { return { taskRequestData, taskRequestExists: false, }; } - + taskRequestData.id = taskRequestSnapshot.id; taskRequestData.url = new URL(`/taskRequests/${taskRequestData.id}`, config.get("services.rdsUi.baseUrl")); From ea2860d661508a4f8abbd9cc7b1e24ffc1e2011c Mon Sep 17 00:00:00 2001 From: Akshat187 Date: Thu, 19 Dec 2024 21:59:04 +0530 Subject: [PATCH 3/7] fixed the test case and and added a new test case for the API --- test/integration/taskRequests.test.js | 31 ++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/test/integration/taskRequests.test.js b/test/integration/taskRequests.test.js index 804a82bbf..4b22ca8ca 100644 --- a/test/integration/taskRequests.test.js +++ b/test/integration/taskRequests.test.js @@ -176,6 +176,22 @@ describe("Task Requests", function () { }); }); + it("should return 404 if the task request is not found for the given Id", function (done) { + chai + .request(app) + .get(`/taskRequests/1234`) + .set("cookie", `${cookieName}=${jwt}`) + .end((err, res) => { + if (err) { + return done(err); + } + + expect(res).to.have.status(404); + expect(res.body.message).to.be.equal("Task request not found"); + return done(); + }); + }); + it("should return 404 if the resource is not found", function (done) { sinon.stub(taskRequestsModel, "fetchTaskRequestById").callsFake(() => []); @@ -196,21 +212,26 @@ describe("Task Requests", function () { }); describe("When the user is not a super user", function () { + let taskrequestid; + before(async function () { userId = await addUser(member); - sinon.stub(authService, "verifyAuthToken").callsFake(() => ({ userId })); - jwt = authService.generateAuthToken({ userId }); + sinon.stub(authService, "verifyAuthToken").callsFake(() => ({ + userId, + })); + jwt = authService.generateAuthToken({ + userId, + }); taskId = (await tasksModel.updateTask(taskData[4])).taskId; - await userStatusModel.updateUserStatus(userId, idleUserStatus); - await taskRequestsModel.addOrUpdate(taskId, userId); + taskrequestid = (await taskRequestsModel.addOrUpdate(taskId, userId)).id; }); it("should be successful when the user is not a super user", function (done) { chai .request(app) - .get(`/taskRequests/taskrequstid`) + .get(`/taskRequests/${taskrequestid}`) .set("cookie", `${cookieName}=${jwt}`) .end((err, res) => { if (err) { From 64542ebf60d6c550b362396cefa3eb942b254cc1 Mon Sep 17 00:00:00 2001 From: Akshat187 Date: Fri, 20 Dec 2024 20:27:56 +0530 Subject: [PATCH 4/7] updated variable name --- models/taskRequests.js | 1 - test/integration/taskRequests.test.js | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/models/taskRequests.js b/models/taskRequests.js index 96e88b68e..2467a4c0f 100644 --- a/models/taskRequests.js +++ b/models/taskRequests.js @@ -173,7 +173,6 @@ const fetchTaskRequestById = async (taskRequestId) => { if (!taskRequestData) { return { - taskRequestData, taskRequestExists: false, }; } diff --git a/test/integration/taskRequests.test.js b/test/integration/taskRequests.test.js index 4b22ca8ca..bbc7e0ee6 100644 --- a/test/integration/taskRequests.test.js +++ b/test/integration/taskRequests.test.js @@ -212,7 +212,7 @@ describe("Task Requests", function () { }); describe("When the user is not a super user", function () { - let taskrequestid; + let taskRequestId; before(async function () { userId = await addUser(member); @@ -225,13 +225,13 @@ describe("Task Requests", function () { taskId = (await tasksModel.updateTask(taskData[4])).taskId; await userStatusModel.updateUserStatus(userId, idleUserStatus); - taskrequestid = (await taskRequestsModel.addOrUpdate(taskId, userId)).id; + taskRequestId = (await taskRequestsModel.addOrUpdate(taskId, userId)).id; }); it("should be successful when the user is not a super user", function (done) { chai .request(app) - .get(`/taskRequests/${taskrequestid}`) + .get(`/taskRequests/${taskRequestId}`) .set("cookie", `${cookieName}=${jwt}`) .end((err, res) => { if (err) { From b15028b68be9afac64ead2e5e2e66d4c2427281a Mon Sep 17 00:00:00 2001 From: Akshat187 Date: Sun, 22 Dec 2024 09:53:57 +0530 Subject: [PATCH 5/7] changed the error message --- models/taskRequests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/taskRequests.js b/models/taskRequests.js index 2467a4c0f..d4cb66b38 100644 --- a/models/taskRequests.js +++ b/models/taskRequests.js @@ -185,7 +185,7 @@ const fetchTaskRequestById = async (taskRequestId) => { taskRequestExists: true, }; } catch (err) { - logger.error("Error in updating task", err); + logger.error("Error in fetching taskRequest by Id", err); } return { From 6b2805419564e0acbb3d9a4ea9c249c822869d54 Mon Sep 17 00:00:00 2001 From: Akshat187 Date: Fri, 27 Dec 2024 23:45:38 +0530 Subject: [PATCH 6/7] wrote a test case for taskRequestById --- test/unit/models/task-requests.test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/unit/models/task-requests.test.js b/test/unit/models/task-requests.test.js index 092ff67fe..97e9e7d1f 100644 --- a/test/unit/models/task-requests.test.js +++ b/test/unit/models/task-requests.test.js @@ -6,6 +6,7 @@ const { fetchTaskRequests, approveTaskRequest, fetchPaginatedTaskRequests, + fetchTaskRequestById, addNewFields, removeOldField, addUsersCountAndCreatedAt, @@ -197,6 +198,31 @@ describe("Task requests | models", function () { }); }); + describe("fetchTaskRequestById", function () { + afterEach(async function () { + await cleanDb(); + }); + + it("should return task request data when task request exists", async function () { + const requestData = mockData.taskRequestData; + const addedTaskRequest = await createRequest(requestData, "testUser"); + const result = await fetchTaskRequestById(addedTaskRequest.id); + const assert = require("chai").assert; + assert.isTrue(result.taskRequestExists, "Task request should exist"); + + expect(result.taskRequestExists).to.be.equal(true); + expect(result.taskRequestData.id).to.equal(addedTaskRequest.id); + }); + + it("should return taskRequestExists as false when task request does not exist", async function () { + const mockTaskRequestId = "taskRequest1234"; + const requestData = mockData.taskRequestData; + await createRequest(requestData, "testUser"); + const result = await fetchTaskRequestById(mockTaskRequestId); + expect(result.taskRequestExists).to.be.equal(false); + }); + }); + describe("fetchTaskRequests", function () { beforeEach(async function () { const approvedTaskRequest = { From 03f877f65c5192ac066db6443b77f6cfe6d37e4f Mon Sep 17 00:00:00 2001 From: Akshat187 Date: Sun, 29 Dec 2024 11:14:34 +0530 Subject: [PATCH 7/7] refactored the import statement --- test/unit/models/task-requests.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/models/task-requests.test.js b/test/unit/models/task-requests.test.js index 97e9e7d1f..8a57288a2 100644 --- a/test/unit/models/task-requests.test.js +++ b/test/unit/models/task-requests.test.js @@ -1,6 +1,7 @@ const chai = require("chai"); const sinon = require("sinon"); const { expect } = chai; +const assert = require("chai").assert; const { createRequest, fetchTaskRequests, @@ -207,7 +208,6 @@ describe("Task requests | models", function () { const requestData = mockData.taskRequestData; const addedTaskRequest = await createRequest(requestData, "testUser"); const result = await fetchTaskRequestById(addedTaskRequest.id); - const assert = require("chai").assert; assert.isTrue(result.taskRequestExists, "Task request should exist"); expect(result.taskRequestExists).to.be.equal(true);