Skip to content

Commit

Permalink
test : add tests for pagination of progresses api (#2328)
Browse files Browse the repository at this point in the history
* added test for the get progresses pagination

* added test for dev=false

* minor fix

* added unit test for utils/progresses functions

* added progressed model unit tests

* added test for 500

* added 500 message from constant

* fix test naming
  • Loading branch information
AnujChhikara authored Jan 22, 2025
1 parent cbd992a commit 73fb9e5
Show file tree
Hide file tree
Showing 4 changed files with 559 additions and 35 deletions.
155 changes: 153 additions & 2 deletions test/integration/progressesTasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const firestore = require("../../utils/firestore");
const app = require("../../server");
const authService = require("../../services/authService");
const tasks = require("../../models/tasks");

const progressesModel = require("../../models/progresses");
const addUser = require("../utils/addUser");
const cleanDb = require("../utils/cleanDb");
const {
Expand All @@ -16,7 +16,7 @@ const {

const userData = require("../fixtures/user/user")();
const taskData = require("../fixtures/tasks/tasks")();

const { INTERNAL_SERVER_ERROR_MESSAGE } = require("../../constants/progresses");
const cookieName = config.get("userToken.cookieName");
const { expect } = chai;

Expand Down Expand Up @@ -615,4 +615,155 @@ describe("Test Progress Updates API for Tasks", function () {
});
});
});

describe("GET /progresses (getPaginatedProgressDocument)", function () {
beforeEach(async function () {
const userId = await addUser(userData[1]);
const taskObject1 = await tasks.updateTask(taskData[0]);
const taskId1 = taskObject1.taskId;
const progressData1 = stubbedModelTaskProgressData(userId, taskId1, 1683626400000, 1683590400000); // 2023-05-09
const progressData2 = stubbedModelTaskProgressData(userId, taskId1, 1683885600000, 1683849600000); // 2023-05-12
await firestore.collection("progresses").doc("taskProgressDocument1").set(progressData1);
await firestore.collection("progresses").doc("taskProgressDocument2").set(progressData2);
});

afterEach(async function () {
await cleanDb();
});

it("should return paginated results when dev=true is passed", function (done) {
chai
.request(app)
.get(`/progresses?type=task&dev=true&page=0&size=1`)
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.have.keys(["message", "data", "count", "links"]);
expect(res.body.links).to.have.keys(["next", "prev"]);
expect(res.body.data).to.be.an("array");
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
expect(res.body.count).to.be.equal(1);
res.body.data.forEach((progress) => {
expect(progress).to.have.keys([
"id",
"type",
"completed",
"planned",
"blockers",
"userId",
"userData",
"taskId",
"createdAt",
"date",
]);
});

return done();
});
});

it("should not return paginated results when dev=false is passed", function (done) {
chai
.request(app)
.get(`/progresses?type=task&dev=false&page=0&size=1`)
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.have.keys(["message", "data", "count"]);
expect(res.body.data).to.be.an("array");
expect(res.body.count).to.not.equal(1);
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
res.body.data.forEach((progress) => {
expect(progress).to.have.keys([
"id",
"type",
"completed",
"planned",
"blockers",
"userId",
"taskId",
"createdAt",
"date",
]);
});

return done();
});
});

it("should return null for next link on the last page", function (done) {
const size = 1;
const page = 1;

chai
.request(app)
.get(`/progresses?type=task&dev=true&page=${page}&size=${size}`)
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.have.keys(["message", "data", "count", "links"]);
expect(res.body.links).to.have.keys(["next", "prev"]);
expect(res.body.data).to.be.an("array");
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
expect(res.body.links.next).to.be.equal(null);
expect(res.body.links.prev).to.equal(`/progresses?type=task&page=${page - 1}&size=${size}&dev=true`);
return done();
});
});

it("should return a bad request error for invalid size parameter", function (done) {
chai
.request(app)
.get(`/progresses?type=task&dev=true&page=0&size=104`)
.end((_err, res) => {
expect(res).to.have.status(400);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("size must be in the range 1-100");
return done();
});
});

it("should return an empty array of progresses data on a page with no data", function (done) {
const size = 10;
const page = 100;

chai
.request(app)
.get(`/progresses?type=task&dev=true&page=${page}&size=${size}`)
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Progress document retrieved successfully.");
// eslint-disable-next-line no-unused-expressions
expect(res.body.data).to.be.an("array").that.is.empty;
expect(res.body.links).to.have.keys(["next", "prev"]);
// eslint-disable-next-line no-unused-expressions
expect(res.body.links.next).to.be.null;
expect(res.body.links.prev).to.equal(`/progresses?type=task&page=${page - 1}&size=${size}&dev=true`);
return done();
});
});

it("Should return 500 Internal Server Error if there is an exception", function (done) {
sinon.stub(progressesModel, "getPaginatedProgressDocument").throws(new Error("Database error"));

chai
.request(app)
.get(`/progresses?type=task&dev=true&page=0&size=1`)
.end((err, res) => {
if (err) return done(err);

if (err) {
return done(err);
}

expect(res).to.have.status(500);
expect(res.body).to.deep.equal({
message: INTERNAL_SERVER_ERROR_MESSAGE,
});
return done();
});
});
});
});
124 changes: 122 additions & 2 deletions test/integration/progressesUsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sinon = require("sinon");
const firestore = require("../../utils/firestore");
const app = require("../../server");
const authService = require("../../services/authService");

const progressesModel = require("../../models/progresses");
const addUser = require("../utils/addUser");
const cleanDb = require("../utils/cleanDb");
const {
Expand All @@ -14,7 +14,7 @@ const {
} = require("../fixtures/progress/progresses");

const userData = require("../fixtures/user/user")();

const { INTERNAL_SERVER_ERROR_MESSAGE } = require("../../constants/progresses");
const cookieName = config.get("userToken.cookieName");
const { expect } = chai;

Expand Down Expand Up @@ -499,4 +499,124 @@ describe("Test Progress Updates API for Users", function () {
});
});
});

describe("GET /progresses (getPaginatedProgressDocument)", function () {
beforeEach(async function () {
const userId1 = await addUser(userData[0]);
const userId2 = await addUser(userData[1]);
const progressData1 = stubbedModelProgressData(userId1, 1683957764140, 1683936000000);
const progressData2 = stubbedModelProgressData(userId2, 1683957764140, 1683936000000);
await firestore.collection("progresses").doc("progressDoc1").set(progressData1);
await firestore.collection("progresses").doc("progressDoc2").set(progressData2);
});

afterEach(async function () {
await cleanDb();
});

it("should return paginated results when dev=true is passed", function (done) {
chai
.request(app)
.get(`/progresses?type=user&dev=true&page=0&size=1`)
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.have.keys(["message", "data", "count", "links"]);
expect(res.body.links).to.have.keys(["next", "prev"]);
expect(res.body.data).to.be.an("array");
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
expect(res.body.count).to.be.equal(1);
res.body.data.forEach((progress) => {
expect(progress).to.have.keys([
"id",
"type",
"completed",
"planned",
"blockers",
"userData",
"userId",
"createdAt",
"date",
]);
});

return done();
});
});

it("should return null for next link on the last page", function (done) {
const size = 1;
const page = 1;

chai
.request(app)
.get(`/progresses?type=user&dev=true&page=${page}&size=${size}`)
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.have.keys(["message", "data", "count", "links"]);
expect(res.body.links).to.have.keys(["next", "prev"]);
expect(res.body.data).to.be.an("array");
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
expect(res.body.links.next).to.be.equal(null);
expect(res.body.links.prev).to.equal(`/progresses?type=user&page=${page - 1}&size=${size}&dev=true`);
return done();
});
});

it("should return a bad request error for invalid size parameter", function (done) {
chai
.request(app)
.get(`/progresses?type=user&dev=true&page=0&size=104`)
.end((_err, res) => {
expect(res).to.have.status(400);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("size must be in the range 1-100");
return done();
});
});

it("should return an empty array of progresses data on a page with no data", function (done) {
const size = 10;
const page = 100;

chai
.request(app)
.get(`/progresses?type=user&dev=true&page=${page}&size=${size}`)
.end((err, res) => {
if (err) return done(err);
expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Progress document retrieved successfully.");
// eslint-disable-next-line no-unused-expressions
expect(res.body.data).to.be.an("array").that.is.empty;
expect(res.body.links).to.have.keys(["next", "prev"]);
// eslint-disable-next-line no-unused-expressions
expect(res.body.links.next).to.be.null;
expect(res.body.links.prev).to.equal(`/progresses?type=user&page=${page - 1}&size=${size}&dev=true`);
return done();
});
});

it("Should return 500 Internal Server Error if there is an exception", function (done) {
sinon.stub(progressesModel, "getPaginatedProgressDocument").throws(new Error("Database error"));

chai
.request(app)
.get(`/progresses?type=user&dev=true&page=0&size=1`)
.end((err, res) => {
if (err) return done(err);

if (err) {
return done(err);
}

expect(res).to.have.status(500);
expect(res.body).to.deep.equal({
message: INTERNAL_SERVER_ERROR_MESSAGE,
});
return done();
});
});
});
});
Loading

0 comments on commit 73fb9e5

Please sign in to comment.