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

Allow user to edit Extension Requests before approval #2215

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions controllers/extensionRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,22 @@ const getSelfExtensionRequests = async (req, res) => {
* @param res {Object} - Express response object
*/
const updateExtensionRequest = async (req, res) => {
const { dev } = req.query;
const isDev = dev === "true";
try {
const extensionRequest = await extensionRequestsQuery.fetchExtensionRequest(req.params.id);
if (!extensionRequest.extensionRequestData) {
return res.boom.notFound("Extension Request not found");
}

if (
isDev &&
!req.userData?.roles.super_user &&
extensionRequest.extensionRequestData.status !== EXTENSION_REQUEST_STATUS.PENDING
) {
return res.boom.badRequest("Only pending extension request can be updated");
}

if (req.body.assignee) {
const { taskData: task } = await tasks.fetchTask(extensionRequest.extensionRequestData.taskId);
if (task.assignee !== (await getUsername(req.body.assignee))) {
Expand Down
13 changes: 13 additions & 0 deletions middlewares/skipAuthorizeRolesWrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const skipAuthorizeRolesUnderFF = (authorizeMiddleware) => {
return (req, res, next) => {
const { dev } = req.query;
const isDev = dev === "true";
if (isDev) {
next();
} else {
authorizeMiddleware(req, res, next);
}
};
};

module.exports = skipAuthorizeRolesUnderFF;
4 changes: 3 additions & 1 deletion routes/extensionRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ const {
updateExtensionRequestStatus,
getExtensionRequestsValidator,
} = require("../middlewares/validators/extensionRequests");
const skipAuthorizeRolesUnderFF = require("../middlewares/skipAuthorizeRolesWrapper");

router.post("/", authenticate, createExtensionRequest, extensionRequests.createTaskExtensionRequest);
router.get("/", authenticate, getExtensionRequestsValidator, extensionRequests.fetchExtensionRequests);
router.get("/self", authenticate, extensionRequests.getSelfExtensionRequests);
router.get("/:id", authenticate, authorizeRoles([SUPERUSER, APPOWNER]), extensionRequests.getExtensionRequest);
// remove the skipAuthorizeRolesUnderFF & authorizeRoles middleware when removing the feature flag
router.patch(
"/:id",
authenticate,
authorizeRoles([SUPERUSER, APPOWNER]),
skipAuthorizeRolesUnderFF(authorizeRoles([SUPERUSER, APPOWNER])),
Dismissed Show dismissed Hide dismissed
ahsrah7 marked this conversation as resolved.
Show resolved Hide resolved
updateExtensionRequest,
extensionRequests.updateExtensionRequest
);
Expand Down
51 changes: 51 additions & 0 deletions test/integration/extensionRequests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,57 @@ describe("Extension Requests", function () {
});
});

it("User should be able to update the extensionRequest for the given extensionRequestId", function (done) {
chai
.request(app)
.patch(`/extension-requests/${extensionRequestId4}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.send({
title: "new-title",
})
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(204);
return done();
});
});

it("User should not be able to update the extensionRequest if already approved", function (done) {
chai
.request(app)
.patch(`/extension-requests/${extensionRequestId1}?dev=true`)
.set("cookie", `${cookieName}=${jwt}`)
.send({
title: "new-title",
})
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(400);
return done();
});
});

it("Super user should be able to update the extensionRequest if already approved", function (done) {
chai
.request(app)
.patch(`/extension-requests/${extensionRequestId1}?dev=true`)
.set("cookie", `${cookieName}=${superUserJwt}`)
.send({
title: "new-title",
})
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(204);
return done();
});
});

it("Should return 400 if assignee of the extensionrequest is upated with a different user", function (done) {
chai
.request(app)
Expand Down
43 changes: 43 additions & 0 deletions test/unit/middlewares/skipAuthorizeRolesWrapper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const chai = require("chai");
const sinon = require("sinon");
const { assert } = chai;
const skipAuthorizeRolesUnderFF = require("../../../middlewares/skipAuthorizeRolesWrapper");

describe("skipAuthorizeRolesUnderFF Middleware", function () {
let req, res, next, authorizeMiddleware;

beforeEach(function () {
req = { query: {} };
res = {};
next = sinon.spy();
authorizeMiddleware = sinon.spy();
});

it("should call next() when dev is true", function () {
req.query.dev = "true";

const middleware = skipAuthorizeRolesUnderFF(authorizeMiddleware);
middleware(req, res, next);

assert.isTrue(next.calledOnce, "next() should be called once");
assert.isFalse(authorizeMiddleware.called, "authorizeMiddleware should not be called");
});

it("should call authorizeMiddleware when dev is false", function () {
req.query.dev = "false";

const middleware = skipAuthorizeRolesUnderFF(authorizeMiddleware);
middleware(req, res, next);

assert.isTrue(authorizeMiddleware.calledOnce, "authorizeMiddleware should be called once");
assert.isFalse(next.called, "next() should not be called");
});

it("should call authorizeMiddleware when dev is not provided", function () {
const middleware = skipAuthorizeRolesUnderFF(authorizeMiddleware);
middleware(req, res, next);

assert.isTrue(authorizeMiddleware.calledOnce, "authorizeMiddleware should be called once");
assert.isFalse(next.called, "next() should not be called");
});
});
Loading