Skip to content

Commit

Permalink
Merge pull request #1639 from Real-Dev-Squad/develop
Browse files Browse the repository at this point in the history
Dev to main sync
  • Loading branch information
iamitprakash authored Oct 30, 2023
2 parents a880313 + 211dd38 commit 7c5e573
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 74 deletions.
1 change: 1 addition & 0 deletions constants/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const logType = {
PROFILE_DIFF_APPROVED: "PROFILE_DIFF_APPROVED",
PROFILE_DIFF_REJECTED: "PROFILE_DIFF_REJECTED",
CLOUDFLARE_CACHE_PURGED: "CLOUDFLARE_CACHE_PURGED",
EVENTS_REMOVE_PEER: "EVENTS_REMOVE_PEER",
};

module.exports = { logType };
3 changes: 3 additions & 0 deletions controllers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const logger = require("../utils/logger");
const { removeUnwantedProperties } = require("../utils/events");

const crypto = require("crypto");
const { addLog } = require("../models/logs");
const { logType } = require("../constants/logs");

const tokenService = new EventTokenService();
const apiService = new EventAPIService(tokenService);
Expand Down Expand Up @@ -296,6 +298,7 @@ const kickoutPeer = async (req, res) => {
try {
await apiService.post(`/active-rooms/${id}/remove-peers`, payload);
await eventQuery.kickoutPeer({ eventId: id, peerId: payload.peer_id, reason: req.body.reason });
addLog(logType.EVENTS_REMOVE_PEER, { removed_by: req.userData.id }, payload);
return res.status(200).json({
message: `Selected Participant is removed from event.`,
});
Expand Down
1 change: 1 addition & 0 deletions controllers/extensionRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ const getSelfExtensionRequests = async (req, res) => {
const superUserId = logs[0].meta.userId;
const name = await getFullName(superUserId);
latestExtensionRequest.reviewedBy = `${name?.first_name} ${name?.last_name}`;
latestExtensionRequest.reviewedAt = logs[0].timestamp._seconds;
}
}
allExtensionRequests = [latestExtensionRequest];
Expand Down
3 changes: 3 additions & 0 deletions controllers/tasksRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { INTERNAL_SERVER_ERROR, SOMETHING_WENT_WRONG } = require("../constants/er
const { TASK_REQUEST_TYPE } = require("../constants/taskRequests");
const taskRequestsModel = require("../models/taskRequests");
const tasksModel = require("../models/tasks.js");
const { updateUserStatusOnTaskUpdate } = require("../models/userStatus");
const githubService = require("../services/githubService");
const usersUtils = require("../utils/users");

Expand Down Expand Up @@ -167,6 +168,8 @@ const approveTaskRequest = async (req, res) => {
if (response.isTaskRequestInvalid) {
return res.boom.badRequest("Task request was previously approved or rejected.");
}
await updateUserStatusOnTaskUpdate(user.username);

return res.status(200).json({
message: `Task successfully assigned to user ${response.approvedTo}`,
taskRequest: response.taskRequest,
Expand Down
10 changes: 0 additions & 10 deletions middlewares/taskRequests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");
const { userState } = require("../constants/userStatus");
const userStatusModel = require("../models/userStatus.js");
const dataAccess = require("../services/dataAccessLayer");
/**
* Validates user id for task request
Expand All @@ -20,14 +18,6 @@ async function validateUser(req, res, next) {
return res.boom.conflict("User does not exist");
}

const { userStatusExists, data: userStatus } = await userStatusModel.getUserStatus(userId);
if (!userStatusExists) {
return res.boom.conflict("User status does not exist");
}
if (userStatus.currentStatus.state === userState.ACTIVE) {
return res.boom.conflict("User is currently active on another task");
}

req.body.user = user;

return next();
Expand Down
95 changes: 95 additions & 0 deletions test/integration/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const event1Data = eventData[0];
const userData = require("../fixtures/user/user")();

const eventQuery = require("../../models/events");
const logsModel = require("../../models/logs");

const defaultUser = userData[16];

Expand Down Expand Up @@ -721,4 +722,98 @@ describe("events", function () {
});
});
});

describe("PATCH /events/:id/peers/kickout", function () {
let service;
let superUserAuthToken;
let memberAuthToken;
beforeEach(async function () {
const superUser = userData[4];
const member = userData[6];
const superUserId = await addUser(superUser);
const memberUserId = await addUser(member);
superUserAuthToken = authService.generateAuthToken({ userId: superUserId });
memberAuthToken = authService.generateAuthToken({ userId: memberUserId });
});

afterEach(function () {
service.restore();
sinon.restore();
});

it("returns a success message when the request is successful for super user", function (done) {
const payload = {
peerId: "peer123",
reason: "Kicked out for a reason",
};

service = sinon.stub(EventAPIService.prototype, "post").returns({ message: "peer remove request submitted" });

sinon.stub(eventQuery, "kickoutPeer").returns({ message: "Selected Participant is removed from event." });
sinon.stub(logsModel, "addLog");

chai
.request(app)
.patch(`/events/${event1Data.room_id}/peers/kickout`)
.set("cookie", `${cookieName}=${superUserAuthToken}`)
.send(payload)
.end((error, response) => {
if (error) {
return done(error);
}

expect(response).to.have.status(200);
expect(response.body.message).to.be.a("string");
expect(response.body.message).to.equal("Selected Participant is removed from event.");

return done();
});
});

it("returns a success message when the request is successful for member user", function (done) {
const payload = {
peerId: "peer123",
reason: "Kicked out for a reason",
};

service = sinon.stub(EventAPIService.prototype, "post").returns({ message: "peer remove request submitted" });

sinon.stub(eventQuery, "kickoutPeer").returns({ message: "Selected Participant is removed from event." });
sinon.stub(logsModel, "addLog");

chai
.request(app)
.patch(`/events/${event1Data.room_id}/peers/kickout`)
.set("cookie", `${cookieName}=${memberAuthToken}`)
.send(payload)
.end((error, response) => {
if (error) {
return done(error);
}

expect(response).to.have.status(200);
expect(response.body.message).to.be.a("string");
expect(response.body.message).to.equal("Selected Participant is removed from event.");

return done();
});
});

it("should return unauthorized error if user is not authenticated", function (done) {
chai
.request(app)
.patch(`/events/${event1Data.room_id}/peers/kickout`)
.end((error, response) => {
if (error) {
return done(error);
}

expect(response).to.have.status(401);
expect(response.body.error).to.be.equal("Unauthorized");
expect(response.body.message).to.be.equal("Unauthenticated User");

return done();
});
});
});
});
102 changes: 38 additions & 64 deletions test/integration/taskRequests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const config = require("config");
const { TASK_REQUEST_TYPE } = require("../../constants/taskRequests");
const usersUtils = require("../../utils/users");
const githubService = require("../../services/githubService");
const { userState } = require("../../constants/userStatus");

const cookieName = config.get("userToken.cookieName");

Expand Down Expand Up @@ -374,26 +375,6 @@ describe("Task Requests", function () {

taskId = (await tasksModel.updateTask(taskData[4])).taskId;
});

it("should match response", function (done) {
chai
.request(app)
.post("/taskRequests/addOrUpdate")
.set("cookie", `${cookieName}=${jwt}`)
.send({
taskId,
userId,
})
.end((err, res) => {
if (err) {
return done(err);
}

expect(res).to.have.status(409);
expect(res.body.message).to.equal("User status does not exist");
return done();
});
});
});

describe("When the user status is not idle", function () {
Expand All @@ -404,37 +385,14 @@ describe("Task Requests", function () {

taskId = (await tasksModel.updateTask(taskData[4])).taskId;
});
it("should match response when the user is active on another task", function (done) {
sinon
.stub(userStatusModel, "getUserStatus")
.callsFake(() => ({ userStatusExists: true, data: activeUserStatus }));

chai
.request(app)
.post("/taskRequests/addOrUpdate")
.set("cookie", `${cookieName}=${jwt}`)
.send({
taskId,
userId,
})
.end((err, res) => {
if (err) {
return done(err);
}

expect(res).to.have.status(409);
expect(res.body.message).to.equal("User is currently active on another task");
return done();
});
});
});
});

describe("PATCH /taskRequests/approve - approves task request", function () {
let activeUserId, oooUserId;

describe("When the user is super user", function () {
before(async function () {
beforeEach(async function () {
userId = await addUser(member);
activeUserId = await addUser(activeMember);
oooUserId = await addUser(member2);
Expand All @@ -450,6 +408,10 @@ describe("Task Requests", function () {
await userStatusModel.updateUserStatus(oooUserId, { ...oooUserStatus });
await taskRequestsModel.addOrUpdate(taskId, userId);
});
afterEach(async function () {
sinon.restore();
await cleanDb();
});

it("should match response for successfull approval", function (done) {
sinon.stub(taskRequestsModel, "approveTaskRequest").resolves({ approvedTo: member.username });
Expand All @@ -472,26 +434,6 @@ describe("Task Requests", function () {
});
});

it("should return 409 error with message when user is active", function (done) {
chai
.request(app)
.patch("/taskRequests/approve")
.set("cookie", `${cookieName}=${jwt}`)
.send({
taskRequestId: taskId,
userId: activeUserId,
})
.end((err, res) => {
if (err) {
return done(err);
}

expect(res).to.have.status(409);
expect(res.body.message).to.equal("User is currently active on another task");
return done();
});
});

it("should throw 400 error when taskRequestId is missing", function (done) {
chai
.request(app)
Expand Down Expand Up @@ -581,6 +523,38 @@ describe("Task Requests", function () {
return done();
});
});
describe("Checks the user status", function () {
it("Should change the user status to ACTIVE when request is successful", async function () {
sinon.stub(taskRequestsModel, "approveTaskRequest").resolves({ approvedTo: member.username });
const res = await chai
.request(app)
.patch("/taskRequests/approve")
.set("cookie", `${cookieName}=${jwt}`)
.send({
taskRequestId: taskId,
userId,
});

expect(res).to.have.status(200);
const userStatus = await userStatusModel.getUserStatus(userId);
expect(userStatus.data.currentStatus.state).to.be.equal(userState.ACTIVE);
});
it("Should not change the user status to ACTIVE when request is unsuccessful", async function () {
sinon.stub(taskRequestsModel, "approveTaskRequest").resolves({ isTaskRequestInvalid: true });
const res = await chai
.request(app)
.patch("/taskRequests/approve")
.set("cookie", `${cookieName}=${jwt}`)
.send({
taskRequestId: taskId,
userId,
});

expect(res).to.have.status(400);
const userStatus = await userStatusModel.getUserStatus(userId);
expect(userStatus.data.currentStatus.state).to.be.equal(userState.IDLE);
});
});
});

describe("When the user is not super user", function () {
Expand Down

0 comments on commit 7c5e573

Please sign in to comment.