Skip to content

Commit

Permalink
feat: Added test cases for departed users api changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
VinuB-Dev committed Nov 25, 2024
1 parent 36e067d commit 3ba4c10
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 3 deletions.
154 changes: 154 additions & 0 deletions test/fixtures/abandoned-tasks/departed-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
const usersData = [
{
id: "user1_id",
discordId: "123456789",
github_id: "github_user1",
username: "archived_user1",
first_name: "Archived",
last_name: "User One",
linkedin_id: "archived_user1",
github_display_name: "archived-user-1",
phone: "1234567890",
email: "[email protected]",
roles: {
archived: true,
in_discord: false,
},
discordJoinedAt: "2024-01-01T00:00:00.000Z",
picture: {
publicId: "profile/user1",
url: "https://example.com/user1.jpg",
},
},
{
id: "user2_id",
discordId: "987654321",
github_id: "github_user2",
username: "archived_user2",
first_name: "Archived",
last_name: "User Two",
linkedin_id: "archived_user2",
github_display_name: "archived-user-2",
phone: "0987654321",
email: "[email protected]",
roles: {
archived: true,
in_discord: false,
},
discordJoinedAt: "2024-01-02T00:00:00.000Z",
picture: {
publicId: "profile/user2",
url: "https://example.com/user2.jpg",
},
},
{
id: "user3_id",
discordId: "555555555",
github_id: "github_user3",
username: "active_user",
first_name: "Active",
last_name: "User",
linkedin_id: "active_user",
github_display_name: "active-user",
phone: "5555555555",
email: "[email protected]",
roles: {
archived: false,
in_discord: true,
},
discordJoinedAt: "2024-01-03T00:00:00.000Z",
picture: {
publicId: "profile/user3",
url: "https://example.com/user3.jpg",
},
},
];

const tasksData = [
{
id: "task1_id",
title: "Abandoned Task 1",
type: "feature",
status: "IN_PROGRESS",
priority: "HIGH",
percentCompleted: 50,
createdAt: 1727027666,
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "archived_user1",
assigneeId: "user1_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/1",
url: "https://api.github.com/repos/org/repo/issues/1",
},
},
dependsOn: [],
},
{
id: "task2_id",
title: "Abandoned Task 2",
type: "bug",
status: "BLOCKED",
priority: "MEDIUM",
percentCompleted: 30,
createdAt: 1727027666,
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "archived_user2",
assigneeId: "user2_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/2",
url: "https://api.github.com/repos/org/repo/issues/2",
},
},
dependsOn: [],
},
{
id: "task3_id",
title: "Completed Archived Task",
type: "feature",
status: "DONE",
priority: "LOW",
percentCompleted: 100,
createdAt: 1727027666,
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "archived_user1",
assigneeId: "user1_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/3",
url: "https://api.github.com/repos/org/repo/issues/3",
},
},
dependsOn: [],
},
{
id: "task4_id",
title: "Active User Task",
type: "feature",
status: "IN_PROGRESS",
priority: "HIGH",
percentCompleted: 75,
createdAt: 1727027666,
updatedAt: 1727027999,
startedOn: 1727027777,
endsOn: 1731542400,
assignee: "active_user",
assigneeId: "user3_id",
github: {
issue: {
html_url: "https://github.com/org/repo/issues/4",
url: "https://api.github.com/repos/org/repo/issues/4",
},
},
dependsOn: [],
},
];

module.exports = { usersData, tasksData };
58 changes: 57 additions & 1 deletion test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ const { userPhotoVerificationData } = require("../fixtures/user/photo-verificati
const Sinon = require("sinon");
const { INTERNAL_SERVER_ERROR, SOMETHING_WENT_WRONG } = require("../../constants/errorMessages");
const photoVerificationModel = firestore.collection("photo-verification");

const userModel = firestore.collection("users");
const taskModel = firestore.collection("tasks");
const {
usersData: abandonedUsersData,
tasksData: abandonedTasksData,
} = require("../fixtures/abandoned-tasks/departed-users");
const userService = require("../../services/users");
chai.use(chaiHttp);

describe("Users", function () {
Expand Down Expand Up @@ -1441,6 +1447,56 @@ describe("Users", function () {
});
});

describe("GET /users?departed", function () {
beforeEach(async function () {
await cleanDb();
const userPromises = abandonedUsersData.map((user) => userModel.doc(user.id).set(user));
await Promise.all(userPromises);

const taskPromises = abandonedTasksData.map((task) => taskModel.add(task));
await Promise.all(taskPromises);
});

afterEach(async function () {
Sinon.restore();
await cleanDb();
});

it("should return a list of users with abandoned tasks", async function () {
const res = await chai.request(app).get("/users?dev=true&departed=true");
expect(res).to.have.status(200);
expect(res.body).to.have.property("message").that.equals("Users with abandoned tasks fetched successfully");
expect(res.body).to.have.property("users").to.be.an("array").with.lengthOf(2);
});

it("should return an empty array when no users have abandoned tasks", async function () {
await cleanDb();
const user = abandonedUsersData[2];
await userModel.add(user);

const task = abandonedTasksData[3];
await taskModel.add(task);
const res = await chai.request(app).get("/users?dev=true&departed=true");

expect(res).to.have.status(204);
});

it("should fail if dev flag is not passed", async function () {
const res = await chai.request(app).get("/users?departed=true");
expect(res).to.have.status(404);
expect(res.body.message).to.be.equal("Route not found");
});

it("should handle errors gracefully if getUsersWithIncompleteTasks fails", async function () {
Sinon.stub(userService, "getUsersWithIncompleteTasks").rejects(new Error(INTERNAL_SERVER_ERROR));

const res = await chai.request(app).get("/users?departed=true&dev=true");

expect(res).to.have.status(500);
expect(res.body.message).to.be.equal(INTERNAL_SERVER_ERROR);
});
});

describe("PUT /users/self/intro", function () {
let userStatusData;

Expand Down
42 changes: 42 additions & 0 deletions test/unit/models/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const dependencyModel = firestore.collection("TaskDependencies");
const tasksModel = firestore.collection("tasks");
const userData = require("../../fixtures/user/user");
const addUser = require("../../utils/addUser");
const {
usersData: abandonedUsersData,
tasksData: abandonedTasksData,
} = require("../../fixtures/abandoned-tasks/departed-users");

describe("tasks", function () {
afterEach(async function () {
Expand Down Expand Up @@ -352,4 +356,42 @@ describe("tasks", function () {
}
});
});

describe("fetchIncompleteTaskForUser", function () {
beforeEach(async function () {
await cleanDb();

const taskPromises = abandonedTasksData.map((task) => tasksModel.add(task));
await Promise.all(taskPromises);
});

afterEach(async function () {
await cleanDb();
sinon.restore();
});

it("should fetch tasks which are incomplete for the given user", async function () {
const inactiveUser = abandonedUsersData[0];
const incompleteTasks = await tasks.fetchIncompleteTaskForUser(inactiveUser.id);
expect(incompleteTasks.docs.length).to.be.equal(1);
});

it("should return an empty array if there are no tasks incomplete for the user", async function () {
await cleanDb();
const activeUser = abandonedUsersData[2];
const incompleteTasks = await tasks.fetchIncompleteTaskForUser(activeUser.id);
expect(incompleteTasks.docs.length).to.be.equal(0);
});

it("should handle errors gracefully if the database query fails", async function () {
sinon.stub(tasks, "fetchIncompleteTaskForUser").throws(new Error("Database query failed"));

try {
await tasks.fetchIncompleteTaskForUser();
expect.fail("Expected function to throw an error");
} catch (error) {
expect(error.message).to.equal("Database query failed");
}
});
});
});
45 changes: 45 additions & 0 deletions test/unit/models/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const photoVerificationModel = firestore.collection("photo-verification");
const userData = require("../../fixtures/user/user");
const addUser = require("../../utils/addUser");
const { userState } = require("../../../constants/userStatus");
const { usersData: abandonedUsersData } = require("../../fixtures/abandoned-tasks/departed-users");
/**
* Test the model functions and validate the data stored
*/
Expand Down Expand Up @@ -527,4 +528,48 @@ describe("users", function () {
expect(userDoc.user.roles.super_user).to.be.equal(false);
});
});

describe("fetchPaginatedUsers - Departed Users", function () {
beforeEach(async function () {
await cleanDb();

const userPromises = abandonedUsersData.map((user) => userModel.add(user));
await Promise.all(userPromises);
});

afterEach(async function () {
await cleanDb();
sinon.restore();
});

it("should fetch users not in discord server", async function () {
const result = await users.fetchPaginatedUsers({ departed: "true" });
expect(result.allUsers.length).to.be.equal(2);
});

it("should return no users if departed flag is false", async function () {
const result = await users.fetchPaginatedUsers({ departed: "false" });
expect(result.allUsers.length).to.be.equal(0);
});

it("should return an empty array if there are no departed users in the database", async function () {
await cleanDb();
const activeUser = abandonedUsersData[2];
await userModel.add(activeUser);

const result = await users.fetchPaginatedUsers({ departed: "true" });
expect(result.allUsers.length).to.be.equal(0);
});

it("should handle errors gracefully if the database query fails", async function () {
sinon.stub(users, "fetchPaginatedUsers").throws(new Error("Database query failed"));

try {
await users.fetchPaginatedUsers();
expect.fail("Expected function to throw an error");
} catch (error) {
expect(error.message).to.equal("Database query failed");
}
});
});
});
Loading

0 comments on commit 3ba4c10

Please sign in to comment.