From 5ae14b8abf62c68262525d0c97f6014990cf7b8e Mon Sep 17 00:00:00 2001 From: Vighnesh Mishra Date: Wed, 27 Dec 2023 14:13:09 +0530 Subject: [PATCH 1/4] [Modified] Added validation --- controller/department.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/controller/department.js b/controller/department.js index fa0bd42..816c8b3 100644 --- a/controller/department.js +++ b/controller/department.js @@ -4,7 +4,10 @@ import { listdepartment, deletedepartment, } from "#services/department"; - +import { isEntityIdValid } from "#middleware/entityIdValidation"; +import Accreditation from "#models/accreditation"; +import Infrastructure from "#models/infrastructure"; +import Organization from "#models/organization"; import { logger } from "#util"; async function addDepartment(req, res) { @@ -16,7 +19,21 @@ async function addDepartment(req, res) { infrastructures, organization, } = req.body; + + const isAccredationValid = await isEntityIdValid(accreditations, Accreditation); + const isInfrastructureValid = await isEntityIdValid(infrastructures, Infrastructure); + const isOrganizationValid = await isEntityIdValid(organization, Organization); + try { + + if (!isAccredationValid && !isInfrastructureValid && !isOrganizationValid) { + + const error = ""; + if (!isBranchValid) error.concat("Invalid branch"); + if (!isCourseValid) error.concat(" Invalid course opted"); + res.status(400).json({ err: error }); + } + else { const department = await createnewdepartment( name, acronym, @@ -29,6 +46,7 @@ async function addDepartment(req, res) { res: `added Department successfully ${department.name}`, id: department.id, }); + } } catch (error) { logger.error("Error while inserting", error); res.status(500); From b600c66ec949843cc4d68a92bdd96cbc8be9a3a8 Mon Sep 17 00:00:00 2001 From: Vighnesh Mishra Date: Wed, 27 Dec 2023 14:13:27 +0530 Subject: [PATCH 2/4] [Modified] Added validation --- .husky/pre-commit | 13 ------------ controller/department.js | 45 ++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 35 deletions(-) delete mode 100755 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100755 index a69ae89..0000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - -# npm test -npx lint-staged - -if [ "$(uname)" == "Darwin" ]; then - npm run test -elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then - npm run test -elif [ "$(expr substr $(uname -s) 1 5)" == "MINGW" ]; then - npm run testWin -fi diff --git a/controller/department.js b/controller/department.js index 816c8b3..14ea1dc 100644 --- a/controller/department.js +++ b/controller/department.js @@ -10,6 +10,7 @@ import Infrastructure from "#models/infrastructure"; import Organization from "#models/organization"; import { logger } from "#util"; + async function addDepartment(req, res) { const { name, @@ -19,34 +20,34 @@ async function addDepartment(req, res) { infrastructures, organization, } = req.body; - const isAccredationValid = await isEntityIdValid(accreditations, Accreditation); const isInfrastructureValid = await isEntityIdValid(infrastructures, Infrastructure); const isOrganizationValid = await isEntityIdValid(organization, Organization); + try { - + if (!isAccredationValid && !isInfrastructureValid && !isOrganizationValid) { - - const error = ""; - if (!isBranchValid) error.concat("Invalid branch"); - if (!isCourseValid) error.concat(" Invalid course opted"); - res.status(400).json({ err: error }); - } - else { - const department = await createnewdepartment( - name, - acronym, - yearOfStarting, - accreditations, - infrastructures, - organization, - ); - res.json({ - res: `added Department successfully ${department.name}`, - id: department.id, - }); - } + + const error = ""; + if (!isBranchValid) error.concat("Invalid branch"); + if (!isCourseValid) error.concat(" Invalid course opted"); + res.status(400).json({ err: error }); + } + else { + const department = await createnewdepartment( + name, + acronym, + yearOfStarting, + accreditations, + infrastructures, + organization, + ); + res.json({ + res: `added Department successfully ${department.name}`, + id: department.id, + }); + } } catch (error) { logger.error("Error while inserting", error); res.status(500); From 4e21175a6a254393e63016e628def4cb820db3a5 Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Wed, 27 Dec 2023 17:09:25 +0530 Subject: [PATCH 3/4] readded husky precommit --- .husky/pre-commit | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000..a69ae89 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,13 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +# npm test +npx lint-staged + +if [ "$(uname)" == "Darwin" ]; then + npm run test +elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then + npm run test +elif [ "$(expr substr $(uname -s) 1 5)" == "MINGW" ]; then + npm run testWin +fi From 10327a3d398cd4dc8b48e652dae10701eb91bcd0 Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Wed, 27 Dec 2023 17:17:04 +0530 Subject: [PATCH 4/4] fixed testcases for department --- controller/department.js | 22 +++++++------ test/routes/department.test.js | 59 +++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/controller/department.js b/controller/department.js index 14ea1dc..2957d72 100644 --- a/controller/department.js +++ b/controller/department.js @@ -10,7 +10,6 @@ import Infrastructure from "#models/infrastructure"; import Organization from "#models/organization"; import { logger } from "#util"; - async function addDepartment(req, res) { const { name, @@ -20,21 +19,24 @@ async function addDepartment(req, res) { infrastructures, organization, } = req.body; - const isAccredationValid = await isEntityIdValid(accreditations, Accreditation); - const isInfrastructureValid = await isEntityIdValid(infrastructures, Infrastructure); + const isAccredationValid = await isEntityIdValid( + accreditations, + Accreditation, + ); + const isInfrastructureValid = await isEntityIdValid( + infrastructures, + Infrastructure, + ); const isOrganizationValid = await isEntityIdValid(organization, Organization); - try { - if (!isAccredationValid && !isInfrastructureValid && !isOrganizationValid) { - const error = ""; - if (!isBranchValid) error.concat("Invalid branch"); - if (!isCourseValid) error.concat(" Invalid course opted"); + if (!isAccredationValid) error.concat("Invalid Accreditation"); + if (!isInfrastructureValid) error.concat(" Invalid Infrastruction"); + if (!isOrganizationValid) error.concat(" Invalid Organization"); res.status(400).json({ err: error }); - } - else { + } else { const department = await createnewdepartment( name, acronym, diff --git a/test/routes/department.test.js b/test/routes/department.test.js index 18820f4..2444e75 100644 --- a/test/routes/department.test.js +++ b/test/routes/department.test.js @@ -1,11 +1,17 @@ import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies -import mongoose from "mongoose"; import departmentmodel from "#models/department"; import connector from "#models/databaseUtil"; +import accreditationModel from "#models/accreditation"; +import infrastructureModel from "#models/infrastructure"; +import organizationModel from "#models/organization"; jest.mock("#util"); const { agent } = global; +let accreditationIds; +let infrastructureIds; +let organizationIds; + // test case for deletion function cleanUp(callback) { @@ -14,9 +20,9 @@ function cleanUp(callback) { name: "Electronics", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }) .then(() => { connector.disconnect((DBerr) => { @@ -26,6 +32,21 @@ function cleanUp(callback) { }); } +/* eslint-disable no-underscore-dangle */ +async function getIds(callback) { + accreditationIds = await accreditationModel.read({}, 1); + accreditationIds = accreditationIds.data[0]._id; + infrastructureIds = await infrastructureModel.read({}, 1); + infrastructureIds = infrastructureIds.data[0]._id; + organizationIds = await organizationModel.read({}, 1); + organizationIds = organizationIds.data[0]._id; + callback(); +} + +beforeAll((done) => { + getIds(done); +}); + afterAll((done) => { cleanUp(done); }); @@ -36,9 +57,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); expect(response.status).toBe(200); @@ -52,9 +73,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); id = JSON.parse(id.res.text).id; }); @@ -64,9 +85,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); }); @@ -75,9 +96,9 @@ describe("Department CRUD", () => { name: "Computer", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); expect(response.body.res).not.toBeNull(); }); @@ -87,9 +108,9 @@ describe("Department CRUD", () => { name: "Electronics", acronym: "COMPS", yearOfStarting: "2020-09-01T00:00:00.000Z", - accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")], - infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], - organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")], + accreditations: [accreditationIds], + infrastructures: [infrastructureIds], + organization: [organizationIds], }); expect(response.status).toBe(200); expect(response.body.res).toMatch(/department updated/);