Skip to content

Commit 617a5e4

Browse files
committed
add qn history tests and fix bug
1 parent bcc0d13 commit 617a5e4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

backend/qn-history-service/src/controllers/questionHistoryController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ export const readQnHistoryList = async (
142142
return;
143143
}
144144

145-
if (!(orderInt == 1 || orderInt == -1)) {
145+
if (orderInt !== 1 && orderInt !== -1) {
146146
res.status(400).json({ message: ORDER_INCORRECT_FORMAT_MESSAGE });
147+
return;
147148
}
148149

149150
if (!userId.match(MONGO_OBJ_ID_FORMAT)) {

backend/qn-history-service/tests/qnHistoryRoutes.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import supertest from "supertest";
33
import app from "../src/app";
44
import {
55
MONGO_OBJ_ID_MALFORMED_MESSAGE,
6+
ORDER_INCORRECT_FORMAT_MESSAGE,
67
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
78
PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE,
89
QN_HIST_NOT_FOUND_MESSAGE,
@@ -24,6 +25,7 @@ describe("Qn History Routes", () => {
2425
const submissionStatus = "Attempted";
2526
const dateAttempted = new Date();
2627
const timeTaken = 0;
28+
const code = "hi";
2729
const language = "Python";
2830
const newQnHistory = {
2931
userIds,
@@ -32,6 +34,7 @@ describe("Qn History Routes", () => {
3234
submissionStatus,
3335
dateAttempted,
3436
timeTaken,
37+
code,
3538
language,
3639
};
3740

@@ -46,6 +49,7 @@ describe("Qn History Routes", () => {
4649
dateAttempted.toISOString()
4750
);
4851
expect(res.body.qnHistory.timeTaken).toBe(timeTaken);
52+
expect(res.body.qnHistory.code).toBe(code);
4953
expect(res.body.qnHistory.language).toBe(language);
5054
});
5155
});
@@ -84,6 +88,14 @@ describe("Qn History Routes", () => {
8488
expect(res.body.message).toBe(PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE);
8589
});
8690

91+
it("Does not read without order", async () => {
92+
const res = await request.get(
93+
`${BASE_URL}?page=1&qnHistLimit=10&userId=66f77e9f27ab3f794bdae664`
94+
);
95+
expect(res.status).toBe(400);
96+
expect(res.body.message).toBe(PAGE_LIMIT_USERID_ORDER_REQUIRED_MESSAGE);
97+
});
98+
8799
it("Does not read with negative page", async () => {
88100
const res = await request.get(
89101
`${BASE_URL}?page=-1&qnHistLimit=10&userId=66f77e9f27ab3f794bdae664&order=1`
@@ -107,6 +119,14 @@ describe("Qn History Routes", () => {
107119
expect(res.status).toBe(400);
108120
expect(res.body.message).toBe(MONGO_OBJ_ID_MALFORMED_MESSAGE);
109121
});
122+
123+
it("Does not read with invalid order", async () => {
124+
const res = await request.get(
125+
`${BASE_URL}?page=1&qnHistLimit=10&userId=66f77e9f27ab3f794bdae664&order=2`
126+
);
127+
expect(res.status).toBe(400);
128+
expect(res.body.message).toBe(ORDER_INCORRECT_FORMAT_MESSAGE);
129+
});
110130
});
111131

112132
describe("GET /:id", () => {

0 commit comments

Comments
 (0)