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

fix: staging issue for remove unverified role on /verify #2236

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
12 changes: 12 additions & 0 deletions controllers/external-accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { getDiscordMembers } = require("../services/discordService");
const { addOrUpdate, getUsersByRole, updateUsersInBatch } = require("../models/users");
const { retrieveDiscordUsers, fetchUsersForKeyValues } = require("../services/dataAccessLayer");
const { EXTERNAL_ACCOUNTS_POST_ACTIONS } = require("../constants/external-accounts");
const discordServices = require("../services/discordService");
const config = require("config");
const logger = require("../utils/logger");
const { markUnDoneTasksOfArchivedUsersBacklog } = require("../models/tasks");

Expand Down Expand Up @@ -69,6 +71,16 @@ const linkExternalAccount = async (req, res) => {
userId
);

try {
const unverifiedRoleId = config.get("discordUnverifiedRoleId");
await discordServices.removeRoleFromUser(unverifiedRoleId, attributes.discordId, req.userData);
} catch (error) {
logger.error(`Error getting external account data: ${error}`);
return res.boom.internal("Role Deletion failed. Please contact admin.", {
message: "Role Deletion failed. Please contact admin.",
});
}

return res.status(204).json({ message: "Your discord profile has been linked successfully" });
} catch (error) {
logger.error(`Error getting external account data: ${error}`);
Expand Down
24 changes: 24 additions & 0 deletions test/integration/external-accounts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const firestore = require("../../utils/firestore");
const userData = require("../fixtures/user/user")();
const userModel = firestore.collection("users");
const tasksModel = firestore.collection("tasks");
const discordServices = require("../../services/discordService");
const { EXTERNAL_ACCOUNTS_POST_ACTIONS } = require("../../constants/external-accounts");
chai.use(chaiHttp);
const cookieName = config.get("userToken.cookieName");
Expand Down Expand Up @@ -537,6 +538,8 @@ describe("External Accounts", function () {
expect(getUserResponseBeforeUpdate.body).to.not.have.property("discordId");
expect(getUserResponseBeforeUpdate.body).to.not.have.property("discordJoinedAt");

const removeRoleFromUserStub = Sinon.stub(discordServices, "removeRoleFromUser").resolves();

const response = await chai
.request(app)
.patch(`/external-accounts/link/${externalAccountData[2].token}`)
Expand All @@ -553,6 +556,27 @@ describe("External Accounts", function () {
expect(updatedUserDetails.body.roles.in_discord).to.equal(true);
expect(updatedUserDetails.body).to.have.property("discordId");
expect(updatedUserDetails.body).to.have.property("discordJoinedAt");

removeRoleFromUserStub.restore();
});

it("Should return 500 when unverified role deletion failed", async function () {
await externalAccountsModel.addExternalAccountData(externalAccountData[2]);

const removeRoleFromUserStub = Sinon.stub(discordServices, "removeRoleFromUser").rejects();

const response = await chai
.request(app)
.patch(`/external-accounts/link/${externalAccountData[2].token}`)
.query({ action: EXTERNAL_ACCOUNTS_POST_ACTIONS.DISCORD_USERS_SYNC })
.set("Cookie", `${cookieName}=${newUserJWT}`);

expect(response).to.have.status(500);
expect(response.body).to.be.an("object");
expect(response.body).to.have.property("message");
expect(response.body.message).to.equal(`Role Deletion failed. Please contact admin.`);

removeRoleFromUserStub.restore();
});
});
});
Loading