From bd8bd1b404d4269adbf34780b01dbf8e34b93785 Mon Sep 17 00:00:00 2001 From: Tauke190 Date: Sat, 11 Nov 2023 18:30:11 -0500 Subject: [PATCH 1/3] Testing of adminIssueDetailsGETRequest --- back-end/test/adminIssueviewdetails.test.js | 108 ++++++++++++++++++++ back-end/test/dummy.test.js | 2 +- 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 back-end/test/adminIssueviewdetails.test.js diff --git a/back-end/test/adminIssueviewdetails.test.js b/back-end/test/adminIssueviewdetails.test.js new file mode 100644 index 0000000..4b573b1 --- /dev/null +++ b/back-end/test/adminIssueviewdetails.test.js @@ -0,0 +1,108 @@ +/* eslint-disable */ + +// Unit Testing +import chai, { assert } from "chai"; +import sinon from "sinon"; +import axios from "axios"; +import fs from "fs/promises"; +import { adminIssueViewDetailsHandler } from "../src/controllers/adminIssueViewDetailsHandler.js"; + +const data = await fs.readFile("./public/json/mockapi.json", "utf8"); +const mockData = JSON.parse(data); +// Filter the mock data to match the handler's expected behavior + +const filteredData = mockData.filter( + (item) => String(item.index) === String("3") +); + +describe("Unit Tests for Retrieval of Admin specific indexed issue of a department", () => { + let req, res, axiosGetStub; + let sendSpy, jsonSpy; + + beforeEach(() => { + req = { params: { paramName: "3" } }; + res = { + json: sinon.spy(), + status: sinon.stub().returns({ send: sinon.spy() }) // Stubbed here + }; + sendSpy = res.status().send; // Save a reference to the send spy created above + jsonSpy = res.json; // Save a reference to the json spy + axiosGetStub = sinon.stub(axios, "get"); + }); + + afterEach(() => { + sinon.restore(); // Restores all stubs and spies created through sinon + }); + + it('should filter and return the correct number of data records for the specific indexed issue in that department"', async () => { + // Stub the axios call to resolve with the entire mock data + axiosGetStub.resolves({ data: mockData }); + + // Execute the handler + await adminIssueViewDetailsHandler(req, res); + + // Assert the axios call was made once + assert(axiosGetStub.calledOnce); + // Assert the correct amount of data is returned + assert.equal( + res.json.firstCall.args[0].length, + filteredData.length, + "Number of records should match the filtered data length" + ); + // Assert the returned data matches the filtered data + assert.deepEqual( + res.json.firstCall.args[0], + filteredData, + "Returned data should match the filtered data" + ); + }); +}); + +// Integration Testing + +import chaiHttp from "chai-http"; + +chai.use(chaiHttp); + +describe("Unit Tests for Retrieval of Admin specific indexed issue of a department Endpoints", () => { + describe("GET /api/issues/student/:department/:paramName", () => { + it("should retrieve issues for a given department with a specific issue index", async () => { + const department = "IT"; + const issueindex = "3"; + const res = await chai + .request("http://localhost:5000") + .get(`/api/issues/admin/${department}/${issueindex}`); + + assert.equal(res.status, 200, "Response status should be 200"); + assert.isArray(res.body, "Response body should be an array"); + assert.lengthOf( + res.body, + filteredData.length, + `Response body should have ${filteredData.length} records` + ); + + // Check if each record in the response matches the corresponding record in filteredData + res.body.forEach((record, index) => { + assert.deepEqual( + record, + filteredData[index], + `Record at index ${index} should match the expected data` + ); + }); + }); + + // it("should handle requests for a non-existent student gracefully", async () => { + // const nonExistentStudentNetID = "nonexistent"; + // const res = await chai + // .request(server) + // .get(`/api/issues/student/${nonExistentStudentNetID}`); + + // assert.equal( + // res.status, + // 500, + // "Response status should be 500 for a non-existent student" + // ); + // }); + }); +}); +/* eslint-enable */ diff --git a/back-end/test/dummy.test.js b/back-end/test/dummy.test.js index db6f725..67a9286 100644 --- a/back-end/test/dummy.test.js +++ b/back-end/test/dummy.test.js @@ -10,4 +10,4 @@ describe("Sample Test Suite", () => { assert.equal(2 * 2, 5); }); }); -/*eslint-enable*/ \ No newline at end of file +// /*eslint-enable*/ \ No newline at end of file From 0910904f31dfcebccaec9bc0f10b256b3c4879d9 Mon Sep 17 00:00:00 2001 From: Tauke190 Date: Mon, 13 Nov 2023 19:57:25 -0800 Subject: [PATCH 2/3] adminIssueviewdetails.test.js --- back-end/test/adminIssueviewdetails.test.js | 40 +++++++++++++-------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/back-end/test/adminIssueviewdetails.test.js b/back-end/test/adminIssueviewdetails.test.js index 4b573b1..b9bbcd2 100644 --- a/back-end/test/adminIssueviewdetails.test.js +++ b/back-end/test/adminIssueviewdetails.test.js @@ -56,15 +56,27 @@ describe("Unit Tests for Retrieval of Admin specific indexed issue of a departme "Returned data should match the filtered data" ); }); + + // Edge Case Test Case + it('should return an empty array when specific index of the issue is not found', async () => { + req.params.paramName = "darkSoul"; + axiosGetStub.resolves({ data: mockData }); + await adminIssueViewDetailsHandler(req, res); + assert.isTrue(axiosGetStub.calledOnce, "axios.get should be called once"); + assert.equal( + res.json.firstCall.args[0].length, + 0, + "Number of records should be 0 when paramName is not found" + ); + }); }); // Integration Testing import chaiHttp from "chai-http"; - chai.use(chaiHttp); -describe("Unit Tests for Retrieval of Admin specific indexed issue of a department Endpoints", () => { +describe("Integration Tests for Retrieval of Admin specific indexed issue of a department Endpoints", () => { describe("GET /api/issues/student/:department/:paramName", () => { it("should retrieve issues for a given department with a specific issue index", async () => { const department = "IT"; @@ -91,18 +103,18 @@ describe("Unit Tests for Retrieval of Admin specific indexed issue of a departme }); }); - // it("should handle requests for a non-existent student gracefully", async () => { - // const nonExistentStudentNetID = "nonexistent"; - // const res = await chai - // .request(server) - // .get(`/api/issues/student/${nonExistentStudentNetID}`); - - // assert.equal( - // res.status, - // 500, - // "Response status should be 500 for a non-existent student" - // ); - // }); + it("should handle requests for a non-existent department & non existent issue index gracefully", async () => { + const nonExistentDepartment = "nonexistent"; + const nonExistentissueindex = "nonexistent"; + const res = await chai + .request("http://localhost:5000") + .get(`/api/issues/admin/${nonExistentDepartment}/${nonExistentissueindex}`); + assert.lengthOf( + res.body, + 0, + "Response body should be empty for nonExistentDepartment and nonExistentissueIndex" + ); + }); }); }); /* eslint-enable */ From 1c0a2b6bd3c7e11fbaa7d18e12177c8f33a8da22 Mon Sep 17 00:00:00 2001 From: Tauke190 Date: Mon, 13 Nov 2023 20:02:20 -0800 Subject: [PATCH 3/3] Some fixes --- back-end/test/adminIssueviewdetails.test.js | 1 - back-end/test/dummy.test.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/back-end/test/adminIssueviewdetails.test.js b/back-end/test/adminIssueviewdetails.test.js index b9bbcd2..0493cb2 100644 --- a/back-end/test/adminIssueviewdetails.test.js +++ b/back-end/test/adminIssueviewdetails.test.js @@ -72,7 +72,6 @@ describe("Unit Tests for Retrieval of Admin specific indexed issue of a departme }); // Integration Testing - import chaiHttp from "chai-http"; chai.use(chaiHttp); diff --git a/back-end/test/dummy.test.js b/back-end/test/dummy.test.js index 67a9286..db6f725 100644 --- a/back-end/test/dummy.test.js +++ b/back-end/test/dummy.test.js @@ -10,4 +10,4 @@ describe("Sample Test Suite", () => { assert.equal(2 * 2, 5); }); }); -// /*eslint-enable*/ \ No newline at end of file +/*eslint-enable*/ \ No newline at end of file