Skip to content

Commit ef37641

Browse files
committed
wrorte test cases for new route
1 parent bfbb918 commit ef37641

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

test/integration/userStatus.test.js

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable consistent-return */
12
const chai = require("chai");
23
const { expect } = chai;
34
const chaiHttp = require("chai-http");
@@ -24,6 +25,8 @@ const { userState } = require("../../constants/userStatus");
2425
const cookieName = config.get("userToken.cookieName");
2526
const userStatusModel = require("../../models/userStatus");
2627
const { convertTimestampToUTCStartOrEndOfDay } = require("../../utils/time");
28+
const bot = require("../utils/generateBotToken");
29+
const { CRON_JOB_HANDLER } = require("../../constants/bot");
2730

2831
chai.use(chaiHttp);
2932

@@ -32,13 +35,15 @@ describe("UserStatus", function () {
3235
let superUserId;
3336
let superUserAuthToken;
3437
let userId = "";
38+
let cronjobJwtToken;
3539

3640
beforeEach(async function () {
3741
userId = await addUser();
3842
jwt = authService.generateAuthToken({ userId });
3943
superUserId = await addUser(superUser);
4044
superUserAuthToken = authService.generateAuthToken({ userId: superUserId });
4145
await updateUserStatus(userId, userStatusDataForNewUser);
46+
cronjobJwtToken = bot.generateCronJobToken({ name: CRON_JOB_HANDLER });
4247
});
4348

4449
afterEach(async function () {
@@ -276,7 +281,89 @@ describe("UserStatus", function () {
276281
});
277282

278283
describe("PATCH /users/status/sync", function () {
279-
// TODO: Added test cases here
284+
describe("PATCH /users/status/sync", function () {
285+
it("should return all user statuses", function (done) {
286+
const fakeResponse = {
287+
message: "All User Status updated successfully.",
288+
data: {
289+
usersCount: 5,
290+
oooUsersAltered: 0,
291+
oooUsersUnaltered: 0,
292+
nonOooUsersAltered: 3,
293+
nonOooUsersUnaltered: 0,
294+
},
295+
};
296+
297+
const chaiRequestStub = sinon.stub(chai, "request").returns({
298+
patch: sinon.stub().returnsThis(),
299+
set: sinon.stub().returnsThis(),
300+
end: (callback) => {
301+
callback(null, { body: fakeResponse, status: 200 });
302+
},
303+
});
304+
305+
chai
306+
.request(app)
307+
.patch("/users/status/sync")
308+
.set("Authorization", `Bearer ${cronjobJwtToken}`)
309+
.end((err, res) => {
310+
chaiRequestStub.restore();
311+
if (err) {
312+
return done(err);
313+
}
314+
expect(res).to.have.status(200);
315+
expect(res.body.message).to.equal("All User Status updated successfully.");
316+
expect(res.body.data).to.deep.equal(fakeResponse.data);
317+
done();
318+
});
319+
});
320+
321+
describe("No users found for status update", function () {
322+
it("should return 500 error with appropriate message", function (done) {
323+
const updateAllUserStatusStub = sinon.stub().resolves();
324+
const getTaskBasedUsersStatusStub = sinon.stub().resolves({ data: { users: [] } });
325+
326+
sinon.replace(userStatusModel, "updateAllUserStatus", updateAllUserStatusStub);
327+
sinon.replace(userStatusModel, "getTaskBasedUsersStatus", getTaskBasedUsersStatusStub);
328+
329+
chai
330+
.request(app)
331+
.patch("/users/status/sync")
332+
.set("Authorization", `Bearer ${cronjobJwtToken}`)
333+
.end((err, res) => {
334+
sinon.restore();
335+
if (err) {
336+
return done(err);
337+
}
338+
expect(res).to.have.status(500);
339+
expect(res.body.message).to.equal("Error: Users data is not in the expected format or no users found");
340+
done();
341+
});
342+
});
343+
});
344+
345+
describe("Error while updating user statuses", function () {
346+
it("should return 500 error with appropriate message", function (done) {
347+
const updateAllUserStatusStub = sinon.stub().rejects(new Error("Failed to update user statuses"));
348+
sinon.replace(userStatusModel, "updateAllUserStatus", updateAllUserStatusStub);
349+
chai
350+
.request(app)
351+
.patch("/users/status/sync")
352+
.set("Authorization", `Bearer ${cronjobJwtToken}`)
353+
.end((err, res) => {
354+
sinon.restore();
355+
if (err) {
356+
return done(err);
357+
}
358+
expect(res).to.have.status(500);
359+
expect(res.body.message).to.equal(
360+
"The server has encountered an unexpected error. Please contact the administrator for more information."
361+
);
362+
done();
363+
});
364+
});
365+
});
366+
});
280367
});
281368

282369
describe("PATCH /users/status/:userid", function () {

0 commit comments

Comments
 (0)