Skip to content

Commit f6d028d

Browse files
Update: added test cases for internal server error
1 parent c976157 commit f6d028d

File tree

5 files changed

+86
-31
lines changed

5 files changed

+86
-31
lines changed

controllers/members.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const ROLES = require("../constants/roles");
22
const members = require("../models/members");
33
const tasks = require("../models/tasks");
4-
const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");
4+
const { SOMETHING_WENT_WRONG, INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
55
const dataAccess = require("../services/dataAccessLayer");
66
const { addLog } = require("../models/logs");
77
/**
@@ -99,13 +99,13 @@ const archiveMembers = async (req, res) => {
9999
if (successObject.isArchived) {
100100
return res.boom.badRequest("User is already archived");
101101
}
102-
addLog("archiveDetails", meta, { body: body?.reason });
102+
addLog("archive-details", meta, { body: body?.reason });
103103
return res.status(204).send();
104104
}
105105
return res.boom.notFound("User doesn't exist");
106106
} catch (err) {
107107
logger.error(`Error while retriving contributions ${err}`);
108-
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
108+
return res.status(500).json({ message: INTERNAL_SERVER_ERROR });
109109
}
110110
};
111111

models/logs.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { getBeforeHourTime } = require("../utils/time");
33
const logsModel = firestore.collection("logs");
44
const admin = require("firebase-admin");
55
const { logType } = require("../constants/logs");
6+
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
67

78
/**
89
* Adds log
@@ -22,7 +23,7 @@ const addLog = async (type, meta, body) => {
2223
return await logsModel.add(log);
2324
} catch (err) {
2425
logger.error("Error in adding log", err);
25-
throw err;
26+
throw new Error(INTERNAL_SERVER_ERROR);
2627
}
2728
};
2829

@@ -77,7 +78,7 @@ const fetchLogs = async (query, param) => {
7778
return logs;
7879
} catch (err) {
7980
logger.error("Error in adding log", err);
80-
throw err;
81+
throw new Error(INTERNAL_SERVER_ERROR);
8182
}
8283
};
8384

test/fixtures/logs/archievedUsers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const archivedUserDetailsModal = [
22
{
3-
type: "archivedUsers",
3+
type: "archive-details",
44
meta: {
55
userId: "TEST_USER_ID_1",
66
superUserId: "TEST_SUPER_USER_ID",
@@ -15,7 +15,7 @@ const archivedUserDetailsModal = [
1515
},
1616
},
1717
{
18-
type: "archivedUsers",
18+
type: "archive-details",
1919
meta: {
2020
userId: "TEST_USER_ID_2",
2121
superUserId: "TEST_SUPER_USER_ID",
@@ -30,7 +30,7 @@ const archivedUserDetailsModal = [
3030
},
3131
},
3232
{
33-
type: "archivedUsers",
33+
type: "archive-details",
3434
meta: {
3535
userId: "TEST_USER_ID_1",
3636
superUserId: "TEST_SUPER_USER_ID",

test/integration/members.test.js

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const userData = require("../fixtures/user/user")();
1212

1313
const config = require("config");
1414
const cookieName = config.get("userToken.cookieName");
15+
const Sinon = require("sinon");
16+
const { INTERNAL_SERVER_ERROR } = require("../../constants/errorMessages");
17+
const dataAccess = require("../.././services/dataAccessLayer");
1518

1619
chai.use(chaiHttp);
1720

@@ -258,11 +261,34 @@ describe("Members", function () {
258261
});
259262

260263
describe("PATCH /members/archiveMembers/:username", function () {
264+
let dataAccessStub;
261265
beforeEach(async function () {
262266
const superUserId = await addUser(superUser);
263267
jwt = authService.generateAuthToken({ userId: superUserId });
264268
});
265-
269+
afterEach(async function () {
270+
Sinon.restore();
271+
await cleanDb();
272+
});
273+
it("Should return an object with status 500 and an error message", function (done) {
274+
dataAccessStub = Sinon.stub(dataAccess, "retrieveUsers");
275+
dataAccessStub.throws(new Error(INTERNAL_SERVER_ERROR));
276+
addUser(userToBeArchived).then(() => {
277+
chai
278+
.request(app)
279+
.patch(`/members/archiveMembers/${userToBeArchived.username}`)
280+
.set("Cookie", `${cookieName}=${jwt}`)
281+
.send({ reason: "some reason" })
282+
.end((err, res) => {
283+
if (err) {
284+
return done(err);
285+
}
286+
expect(res).to.have.status(500);
287+
expect(res.body.message).to.be.equal(INTERNAL_SERVER_ERROR);
288+
return done();
289+
});
290+
});
291+
});
266292
it("Should return 404 if user doesn't exist", function (done) {
267293
chai
268294
.request(app)
@@ -279,7 +305,24 @@ describe("Members", function () {
279305
return done();
280306
});
281307
});
308+
it("Should return 400 if body is empty", function (done) {
309+
chai
310+
.request(app)
311+
.patch(`/members/archiveMembers/${userToBeArchived.username}`)
312+
.set("cookie", `${cookieName}=${jwt}`)
313+
.send({})
314+
.end((err, res) => {
315+
if (err) {
316+
return done(err);
317+
}
318+
319+
expect(res).to.have.status(400);
320+
expect(res.body).to.be.a("object");
321+
expect(res.body.message).to.equal("Reason is required");
282322

323+
return done();
324+
});
325+
});
283326
it("Should archive the user", function (done) {
284327
addUser(userToBeArchived).then(() => {
285328
chai
@@ -321,26 +364,5 @@ describe("Members", function () {
321364
});
322365
});
323366
});
324-
325-
it("Should return 401 if user is not a super user", function (done) {
326-
addUser(nonSuperUser).then((nonSuperUserId) => {
327-
const nonSuperUserJwt = authService.generateAuthToken({ userId: nonSuperUserId });
328-
chai
329-
.request(app)
330-
.patch(`/members/moveToMembers/${nonSuperUser.username}`)
331-
.set("cookie", `${cookieName}=${nonSuperUserJwt}`)
332-
.end((err, res) => {
333-
if (err) {
334-
return done(err);
335-
}
336-
337-
expect(res).to.have.status(401);
338-
expect(res.body).to.be.a("object");
339-
expect(res.body.message).to.equal("You are not authorized for this action.");
340-
341-
return done();
342-
});
343-
});
344-
});
345367
});
346368
});

test/unit/models/logs.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ const logsQuery = require("../../../models/logs");
66
const cacheData = require("../../fixtures/cloudflareCache/data");
77
const logsData = require("../../fixtures/logs/archievedUsers");
88
const app = require("../../../server");
9+
const Sinon = require("sinon");
10+
const { INTERNAL_SERVER_ERROR } = require("../../../constants/errorMessages");
11+
const userData = require("../../fixtures/user/user")();
12+
const addUser = require("../../utils/addUser");
13+
const cookieName = config.get("userToken.cookieName");
14+
const authService = require("../../../services/authService");
15+
16+
const superUser = userData[4];
17+
const userToBeMadeMember = userData[1];
918

1019
describe("Logs", function () {
1120
after(async function () {
@@ -40,7 +49,30 @@ describe("Logs", function () {
4049
});
4150
});
4251

43-
describe("GET /logs/archivedUsers", function () {
52+
describe("GET /logs/archive-details", function () {
53+
let addLogsStub;
54+
let jwt;
55+
beforeEach(async function () {
56+
const superUserId = await addUser(superUser);
57+
jwt = authService.generateAuthToken({ userId: superUserId });
58+
await cleanDb();
59+
});
60+
afterEach(function () {
61+
Sinon.restore();
62+
});
63+
64+
it("Should return an object with status 500 and an error message", async function () {
65+
addLogsStub = Sinon.stub(logsQuery, "fetchLogs");
66+
addLogsStub.throws(new Error(INTERNAL_SERVER_ERROR));
67+
68+
addUser(userToBeMadeMember).then(() => {
69+
const res = chai.request(app).get("/logs/archive-details").set("cookie", `${cookieName}=${jwt}`).send();
70+
71+
// expect(res).to.have.status(500);
72+
// expect(res.body).to.have.property("message").that.is.a("string");
73+
expect(res.body.message).to.equal(INTERNAL_SERVER_ERROR);
74+
});
75+
});
4476
it("Should return empty array if no logs found", async function () {
4577
const { type } = logsData.archivedUserDetailsModal[0];
4678
const query = {};

0 commit comments

Comments
 (0)