Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: [Added Validation for Exam] #438

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions controller/exam.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@ import {
updateExamById,
} from "#services/exam";
import { logger } from "#util";
import { isEntityIdValid } from "#middleware/entityIdValidation";
import Infrastructure from "#models/infrastructure";
import Course from "#models/course";
import Faculty from "#models/faculty";

async function addExam(req, res) {
const { date, startTime, duration, infrastructure, supervisor, course } =
req.body;

// Checking if the provided IDs exist in the database
const isInfrastructureValid = await isEntityIdValid(
infrastructure,
Infrastructure,
);
const isSupervisorValid = await isEntityIdValid(supervisor, Faculty);
const isCourseValid = await isEntityIdValid(course, Course);

try {
const exam = await createExam(
date,
startTime,
duration,
supervisor,
infrastructure,
course,
);
res.json({ res: `added exam ${exam.id}`, id: exam.id });
if (!isInfrastructureValid || !isSupervisorValid || !isCourseValid) {
res.status(400).json({
error: `Invalid ID(s): Infra:${isInfrastructureValid} Supervisor:${isSupervisorValid} Course:${isCourseValid} `,
});
} else {
const exam = await createExam(
date,
startTime,
duration,
supervisor,
infrastructure,
course,
);
res.json({ res: `added exam ${exam.id}`, id: exam.id });
}
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
res.status(500).json({ err: "Error while inserting in DB" });
}
}

Expand Down
7 changes: 7 additions & 0 deletions middleware/EntityIdValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function isEntityIdValid(entityIds, EntityModel) {
// eslint-disable-next-line no-param-reassign
if (!Array.isArray(entityIds)) entityIds = [entityIds];
const filter = { _id: { $in: entityIds } };
const entities = await EntityModel.read(filter);
return entities.data.length === entityIds.length;
}
25 changes: 5 additions & 20 deletions test/routes/exam.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
import request from "supertest";
import app from "#app";
import connector from "#models/databaseUtil";
import examModel from "#models/exam";

jest.mock("#util");

let server;
let agent;

beforeAll((done) => {
server = app.listen(null, () => {
agent = request.agent(server);
connector.set("debug", false);
done();
});
});
const { agent } = global;

function cleanUp(callback) {
examModel
Expand All @@ -25,10 +13,7 @@ function cleanUp(callback) {
.then(() => {
connector.disconnect((DBerr) => {
if (DBerr) console.log("Database disconnect error: ", DBerr);
server.close((serverErr) => {
if (serverErr) console.log(serverErr);
callback();
});
callback();
});
});
}
Expand All @@ -43,9 +28,9 @@ describe("exam API", () => {
date: "2023-06-18T14:11:30Z",
startTime: "2023-06-18T14:11:30Z",
duration: 5,
supervisor: ["5f8778b54b553439ac49a03a"],
infrastructure: ["5f8778b54b553439ac49a03a"],
course: ["5f8778b54b553439ac49a03a"],
supervisor: "5f8778b54b553439ac49a03a",
infrastructure: "5f8778b54b553439ac49a03a",
course: "5f8778b54b553439ac49a03a",
});
expect(response.headers["content-type"]).toMatch(/json/);
expect(response.status).toBe(200);
Expand Down
Loading