Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test : add tests for pagination of progresses api #2328

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
158 changes: 155 additions & 3 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 Down Expand Up @@ -229,7 +229,7 @@ describe("Test Progress Updates API for Tasks", function () {
.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).to.have.keys(["message", "data", "count", "links"]);
expect(res.body.data).to.be.an("array");
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
res.body.data.forEach((progress) => {
Expand Down Expand Up @@ -388,7 +388,7 @@ describe("Test Progress Updates API for Tasks", function () {
.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).to.have.keys(["message", "data", "count", "links"]);
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(4);
Expand Down Expand Up @@ -615,4 +615,156 @@ describe("Test Progress Updates API for Tasks", function () {
});
});
});

describe("GET /progresses (getPaginatedProgressDocument)", function () {
AnujChhikara marked this conversation as resolved.
Show resolved Hide resolved
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
AnujChhikara marked this conversation as resolved.
Show resolved Hide resolved
.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:
"The server has encountered an unexpected error. Please contact the administrator for more information.",
});
AnujChhikara marked this conversation as resolved.
Show resolved Hide resolved
return done();
});
});
});
AnujChhikara marked this conversation as resolved.
Show resolved Hide resolved
});
127 changes: 124 additions & 3 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 Down Expand Up @@ -233,7 +233,7 @@ describe("Test Progress Updates API for Users", function () {
.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).to.have.keys(["message", "data", "count", "links"]);
expect(res.body.data).to.be.an("array");
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
res.body.data.forEach((progress) => {
Expand All @@ -260,7 +260,7 @@ describe("Test Progress Updates API for Users", function () {
.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).to.have.keys(["message", "data", "count", "links"]);
expect(res.body.data).to.be.an("array");
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
res.body.data.forEach((progress) => {
Expand Down Expand Up @@ -499,4 +499,125 @@ 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:
"The server has encountered an unexpected error. Please contact the administrator for more information.",
});
return done();
});
});
});
});
Loading
Loading