Skip to content

Commit

Permalink
Put the toggle feature behind the dev feature flag (#2161)
Browse files Browse the repository at this point in the history
* Revoke roles temporarily based on revoked_roles

* Extended validator PATCH \users\self

* UpdateUser Set revoked_roles in user object

minor changes:
- fix typo in middlewares/validator to allow only member and super_user role

* Test coverage for the changes

* Modify Response

* Changes Property name from revoked_roles to disabled_roles

* refactored controller/users.js

* [refactor] controller logic & test/unit/model

* [fix] updateSelf developers can only updated disabled role property

udpateSelf funciton update data of those people people who are
either new or not in discord, in order case the user of the
feature are those who are in the discord as well as have
their userDetailsCompelete,

Before, users who lied in this category couldn't update their
user profile for that they need profile service

if someone falls in this category, he can only update the
disabled_roles property, for other properties such people
need to use profile services

* [chore-#2132] Add Test Case & refactor users model

* [refactor] remove redundent reassignment

* added dev flag and fallback if discord response isn't as expected

* fix lint error

---------

Co-authored-by: Amit Prakash <[email protected]>
Co-authored-by: Achintya Chatterjee <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2024
1 parent 1c3a1a6 commit 0c3153b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
5 changes: 4 additions & 1 deletion controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ const getSelfDetails = async (req, res) => {
const updateSelf = async (req, res) => {
try {
const { id: userId, roles: userRoles, discordId } = req.userData;
const devFeatureFlag = req.query.dev === "true";
const { user } = await dataAccess.retrieveUsers({ id: userId });
let rolesToDisable = [];

Expand Down Expand Up @@ -432,11 +433,13 @@ const updateSelf = async (req, res) => {

if (userRoles.in_discord && !user.incompleteUserDetails) {
const membersInDiscord = await getDiscordMembers();
if (!Array.isArray(membersInDiscord))
return res.status(404).send({ message: "Error Fetching Members From Discord" });
const discordMember = membersInDiscord.find((member) => member.user.id === discordId);
if (discordMember) {
const { roles } = discordMember;
if (roles && roles.includes(discordDeveloperRoleId)) {
if (req.body.disabledRoles) {
if (req.body.disabledRoles && devFeatureFlag) {
const updatedUser = await userQuery.addOrUpdate({ disabled_roles: rolesToDisable }, userId);
if (updatedUser) {
return res
Expand Down
32 changes: 28 additions & 4 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ describe("Users", function () {
it("Should return 200 when disabled_roles is being set to [super_user] in userObject ", async function () {
const res = await chai
.request(app)
.patch("/users/self")
.patch("/users/self?dev=true")
.set("cookie", `${cookieName}=${jwt}`)
.send({
disabledRoles: ["super_user"],
Expand All @@ -2450,7 +2450,7 @@ describe("Users", function () {
it("Should return 200 when disabled_roles is being set to [super_user, member] in userObject", async function () {
const res = await chai
.request(app)
.patch("/users/self")
.patch("/users/self?dev=true")
.set("cookie", `${cookieName}=${jwt}`)
.send({
disabledRoles: ["super_user", "member"],
Expand All @@ -2472,7 +2472,7 @@ describe("Users", function () {
});

it("Should return 200 when disabled_roles is being set to [], member in userObject", async function () {
const res = await chai.request(app).patch("/users/self").set("cookie", `${cookieName}=${jwt}`).send({
const res = await chai.request(app).patch("/users/self?dev=true").set("cookie", `${cookieName}=${jwt}`).send({
disabledRoles: [],
});
expect(res).to.have.status(200);
Expand All @@ -2489,10 +2489,34 @@ describe("Users", function () {
expect(res2.body.roles.member).to.be.equal(true);
});

it("Should return 403 when disabled_roles is being set to [], member in userObject without the dev flag", async function () {
const res = await chai.request(app).patch("/users/self").set("cookie", `${cookieName}=${jwt}`).send({
disabledRoles: [],
});
expect(res).to.have.status(403);
expect(res.body.message).to.equal(
"Developers can only update disabled_roles. Use profile service for updating other attributes."
);
});

it("Should return 404 when disabled_roles is being set to [], but discord reponds with an error", async function () {
fetchStub.returns(
Promise.resolve({
status: 200,
json: () => Promise.resolve({ error: "🚫 Bad Request Signature" }),
})
);
const res = await chai.request(app).patch("/users/self").set("cookie", `${cookieName}=${jwt}`).send({
disabledRoles: [],
});
expect(res).to.have.status(404);
expect(res.body.message).to.equal("Error Fetching Members From Discord");
});

it("Should return 400 when disabled_roles is being set to ['admin'], member in userObject", async function () {
const res = await chai
.request(app)
.patch("/users/self")
.patch("/users/self?dev=true")
.set("cookie", `${cookieName}=${jwt}`)
.send({
disabledRoles: ["admin"],
Expand Down

0 comments on commit 0c3153b

Please sign in to comment.