Skip to content

Commit 6e9f5c1

Browse files
authored
Bug : Added checks to validate user groups (#1942)
1 parent 4f20eed commit 6e9f5c1

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

controllers/discordactions.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ const addGroupRoleToMember = async (req, res) => {
115115
const [{ roleExists, existingRoles }, userData] = await Promise.all([roleExistsPromise, userDataPromise]);
116116

117117
if (!roleExists || req.userData.id !== userData.user.id) {
118-
res.boom.forbidden("Permission denied. Cannot add the role.");
118+
return res.boom.forbidden("Permission denied. Cannot add the role.");
119+
}
120+
121+
if (existingRoles.docs.length > 0) {
122+
const roleDetails = existingRoles.docs[0].data();
123+
if (roleDetails.rolename && !roleDetails.rolename.startsWith("group-")) {
124+
return res.boom.forbidden("Cannot use rolename that is not a group role");
125+
}
119126
}
120127

121128
const { roleData, wasSuccess } = await discordRolesModel.addGroupRoleToMember(memberGroupRole);
@@ -163,7 +170,7 @@ const deleteRole = async (req, res) => {
163170
const [{ roleExists }, userData] = await Promise.all([roleExistsPromise, userDataPromise]);
164171

165172
if (!roleExists || req.userData.id !== userData.user.id) {
166-
res.boom.forbidden("Permission denied. Cannot delete the role.");
173+
return res.boom.forbidden("Permission denied. Cannot delete the role.");
167174
}
168175
await discordServices.removeRoleFromUser(roleid, userid, req.userData);
169176

test/fixtures/discordactions/discordactions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const groupData = [
22
{ rolename: "Group 1", roleid: "1" },
33
{ rolename: "Group 2", roleid: "2" },
44
{ rolename: "Group 3", roleid: "3" },
5+
{ rolename: "admin", roleid: "4" },
56
];
67

78
const groupIdle7d = { rolename: "group-idle-7d+", roleid: 4, createdBy: "1dad23q23j131j" };

test/integration/discordactions.test.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ describe("Discord actions", function () {
210210
describe("POST /discord-actions/roles", function () {
211211
let roleid;
212212
beforeEach(async function () {
213-
const discordRoleModelPromise = [discordRoleModel.add(groupData[0]), discordRoleModel.add(groupData[1])];
213+
const discordRoleModelPromise = [
214+
discordRoleModel.add(groupData[0]),
215+
discordRoleModel.add(groupData[1]),
216+
discordRoleModel.add(groupData[3]),
217+
];
214218
roleid = groupData[0].roleid;
215219
await Promise.all(discordRoleModelPromise);
216220
});
@@ -219,7 +223,39 @@ describe("Discord actions", function () {
219223
sinon.restore();
220224
await cleanDb();
221225
});
226+
it("should not be able to add role if it is not a group type role", async function () {
227+
const fetchStub = sinon.stub(discordRolesModel, "isGroupRoleExists");
228+
229+
fetchStub.returns(
230+
{
231+
roleExists: true,
232+
existingRoles: {
233+
docs: [
234+
{
235+
data: () => ({
236+
date: new Date().toISOString(),
237+
createdBy: "CzI06Da1zPwciLcyIwU4",
238+
roleid: "1214641424516124823",
239+
rolename: "admin",
240+
}),
241+
},
242+
],
243+
},
244+
},
245+
{ user: userData[0] }
246+
);
222247

248+
const res = await chai
249+
.request(app)
250+
.post("/discord-actions/roles")
251+
.set("cookie", `${cookieName}=${jwt}`)
252+
.send({ roleid, userid: userData[0].discordId });
253+
expect(res).to.have.status(403);
254+
expect(res.body).to.be.an("object");
255+
expect(res.body.message).to.equal("Cannot use rolename that is not a group role");
256+
257+
fetchStub.restore();
258+
});
223259
it("should allow role to be added", async function () {
224260
fetchStub.returns(
225261
Promise.resolve({

0 commit comments

Comments
 (0)