Skip to content

Commit e7bb54b

Browse files
authored
Merge pull request #2394 from Real-Dev-Squad/fix/progresses-post-route
Restrict archived users to write progress of a task
1 parent 69eb465 commit e7bb54b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

constants/progresses.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const PROGRESSES_SIZE = 20;
2222
const PROGRESSES_PAGE_SIZE = 0;
2323
const VALID_PROGRESS_TYPES = ["task", "user"];
2424

25+
const UNAUTHORIZED_WRITE = "Unauthorized to write progress of task";
26+
2527
module.exports = {
2628
PROGRESSES_RESPONSE_MESSAGES,
2729
MILLISECONDS_IN_DAY,
@@ -31,4 +33,5 @@ module.exports = {
3133
PROGRESS_VALID_SORT_FIELDS,
3234
PROGRESSES_SIZE,
3335
PROGRESSES_PAGE_SIZE,
36+
UNAUTHORIZED_WRITE,
3437
};

controllers/progresses.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
INTERNAL_SERVER_ERROR_MESSAGE,
66
PROGRESSES_SIZE,
77
PROGRESSES_PAGE_SIZE,
8+
UNAUTHORIZED_WRITE,
89
} = require("../constants/progresses");
910
const { sendTaskUpdate } = require("../utils/sendTaskUpdate");
1011
const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEEDED } = PROGRESSES_RESPONSE_MESSAGES;
@@ -45,6 +46,10 @@ const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEED
4546
*/
4647

4748
const createProgress = async (req, res) => {
49+
if (req.userData.roles.archived) {
50+
return res.boom.forbidden(UNAUTHORIZED_WRITE);
51+
}
52+
4853
const {
4954
body: { type, completed, planned, blockers, taskId },
5055
} = req;

test/integration/progressesTasks.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const {
1616

1717
const userData = require("../fixtures/user/user")();
1818
const taskData = require("../fixtures/tasks/tasks")();
19-
const { INTERNAL_SERVER_ERROR_MESSAGE } = require("../../constants/progresses");
19+
const { INTERNAL_SERVER_ERROR_MESSAGE, UNAUTHORIZED_WRITE } = require("../../constants/progresses");
2020
const cookieName = config.get("userToken.cookieName");
2121
const { expect } = chai;
2222

@@ -32,6 +32,8 @@ describe("Test Progress Updates API for Tasks", function () {
3232
let taskId1;
3333
let taskId2;
3434
let fetchMock;
35+
let archivedUserId;
36+
let archivedUserToken;
3537

3638
beforeEach(async function () {
3739
fetchMock = sinon.stub(global, "fetch");
@@ -40,6 +42,8 @@ describe("Test Progress Updates API for Tasks", function () {
4042
toFake: ["Date"],
4143
});
4244
userId = await addUser(userData[1]);
45+
archivedUserId = await addUser(userData[5]);
46+
archivedUserToken = authService.generateAuthToken({ userId: archivedUserId });
4347
userToken = authService.generateAuthToken({ userId: userId });
4448
const taskObject1 = await tasks.updateTask(taskData[0]);
4549
taskId1 = taskObject1.taskId;
@@ -165,6 +169,22 @@ describe("Test Progress Updates API for Tasks", function () {
165169
return done();
166170
});
167171
});
172+
173+
it("should return forbidden response when user is not in discord", function (done) {
174+
chai
175+
.request(app)
176+
.post("/progresses")
177+
.set("Cookie", `${cookieName}=${archivedUserToken}`)
178+
.send(taskProgressDay1("1111"))
179+
.end((err, res) => {
180+
if (err) {
181+
return done(err);
182+
}
183+
expect(res.statusCode).to.equal(403);
184+
expect(res.body.message).to.equal(UNAUTHORIZED_WRITE);
185+
return done();
186+
});
187+
});
168188
});
169189

170190
describe("Verify the GET progress records", function () {

0 commit comments

Comments
 (0)