|
| 1 | +import chai from "chai"; |
| 2 | +import Sinon from "sinon"; |
| 3 | +import { logType } from "../../../constants/logs"; |
| 4 | +import discordActions from "../../../models/discordactions"; |
| 5 | +import { addLog } from "../../../models/logs"; |
| 6 | +import firestore from "../../../utils/firestore"; |
| 7 | +import { removeDiscordRoleFromUser } from "../../../utils/removeDiscordRoleFromUser"; |
| 8 | +import { groupData, memberGroupData } from "../../fixtures/discordactions/discordactions"; |
| 9 | +import addUser from "../../utils/addUser"; |
| 10 | +import cleanDb from "../../utils/cleanDb"; |
| 11 | +const { expect } = chai; |
| 12 | +const discordRolesModel = firestore.collection("discord-roles"); |
| 13 | +const memberRoleModel = firestore.collection("member-group-roles"); |
| 14 | +const logsModel = firestore.collection("logs"); |
| 15 | +const userData = require("../../fixtures/user/user")(); |
| 16 | + |
| 17 | +describe("removeDiscordRoleFromUser", function () { |
| 18 | + let userId; |
| 19 | + let discordId; |
| 20 | + let roleid; |
| 21 | + let fetchStub; |
| 22 | + |
| 23 | + beforeEach(async function () { |
| 24 | + userData[0].roles = { archived: false, in_discord: true }; |
| 25 | + userId = await addUser(userData[0]); |
| 26 | + discordId = userData[0].discordId; |
| 27 | + userData[0] = { ...userData[0], id: userId }; |
| 28 | + |
| 29 | + const addRolePromises = memberGroupData.map(async (data) => { |
| 30 | + await memberRoleModel.add(data); |
| 31 | + }); |
| 32 | + const discordRolesModelPromise = [discordRolesModel.add(groupData[0]), discordRolesModel.add(groupData[1])]; |
| 33 | + await Promise.all(discordRolesModelPromise); |
| 34 | + roleid = groupData[0].roleid; |
| 35 | + await memberRoleModel.add({ roleid, userid: discordId }); |
| 36 | + await Promise.all(addRolePromises); |
| 37 | + |
| 38 | + fetchStub = Sinon.stub(global, "fetch"); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(async function () { |
| 42 | + await cleanDb(); |
| 43 | + fetchStub.restore(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should remove discord role successfully", async function () { |
| 47 | + await addLog( |
| 48 | + logType.REMOVE_ROLE_FROM_USER_SUCCESS, |
| 49 | + { roleId: roleid, userid: discordId }, |
| 50 | + { message: "Role removed successfully from user", userData: userData[0] } |
| 51 | + ); |
| 52 | + |
| 53 | + fetchStub.returns( |
| 54 | + Promise.resolve({ json: () => Promise.resolve({ success: true, message: "Role deleted successfully" }) }) |
| 55 | + ); |
| 56 | + |
| 57 | + const isDiscordRoleRemoved = await removeDiscordRoleFromUser(userData[0], discordId, roleid); |
| 58 | + const successLog = await logsModel |
| 59 | + .where("type", "==", logType.REMOVE_ROLE_FROM_USER_SUCCESS) |
| 60 | + .where("meta.roleId", "==", roleid) |
| 61 | + .where("meta.userid", "==", discordId) |
| 62 | + .limit(1) |
| 63 | + .get(); |
| 64 | + |
| 65 | + expect(isDiscordRoleRemoved.success).to.be.equal(true); |
| 66 | + expect(isDiscordRoleRemoved.message).to.be.equal("Role deleted successfully"); |
| 67 | + expect(successLog.docs[0].data().body.message).to.be.equal("Role removed successfully from user"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should throw an error if role doesn't exist in database when attempting to remove", async function () { |
| 71 | + roleid = "randomRoleId"; |
| 72 | + |
| 73 | + const isDiscordRoleRemoved = await removeDiscordRoleFromUser(userData[0], discordId, roleid); |
| 74 | + const failedLog = await logsModel |
| 75 | + .where("type", "==", logType.REMOVE_ROLE_FROM_USER_FAILED) |
| 76 | + .where("meta.roleId", "==", roleid) |
| 77 | + .limit(1) |
| 78 | + .get(); |
| 79 | + |
| 80 | + expect(isDiscordRoleRemoved.success).to.be.equal(false); |
| 81 | + expect(isDiscordRoleRemoved.message).to.be.equal("Role doesn't exist"); |
| 82 | + expect(failedLog.docs[0].data().body.message).to.be.equal("Role doesn't exist"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should throw an error if role deletion from discord failed", async function () { |
| 86 | + fetchStub.rejects(new Error("Role deletion from discord failed")); |
| 87 | + |
| 88 | + const isDiscordRoleRemoved = await removeDiscordRoleFromUser(userData[0], discordId, roleid); |
| 89 | + const failedLog = await logsModel |
| 90 | + .where("type", "==", logType.REMOVE_ROLE_FROM_USER_FAILED) |
| 91 | + .where("meta.roleId", "==", roleid) |
| 92 | + .where("meta.userid", "==", discordId) |
| 93 | + .limit(1) |
| 94 | + .get(); |
| 95 | + |
| 96 | + expect(isDiscordRoleRemoved.success).to.be.equal(false); |
| 97 | + expect(isDiscordRoleRemoved.message).to.be.equal("Role deletion from discord failed"); |
| 98 | + expect(failedLog.docs[0].data().body.message).to.be.equal("Role deletion from discord failed"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should throw an error if role deleted from discord but not from database", async function () { |
| 102 | + fetchStub.returns(Promise.resolve({ json: () => Promise.resolve({ success: true }) })); |
| 103 | + |
| 104 | + const removeMemberGroupStub = Sinon.stub(discordActions, "removeMemberGroup").resolves({ |
| 105 | + roleId: roleid, |
| 106 | + wasSuccess: false, |
| 107 | + }); |
| 108 | + |
| 109 | + const isDiscordRoleRemoved = await removeDiscordRoleFromUser(userData[0], discordId, roleid); |
| 110 | + const failedLog = await logsModel |
| 111 | + .where("type", "==", logType.REMOVE_ROLE_FROM_USER_FAILED) |
| 112 | + .where("meta.roleId", "==", roleid) |
| 113 | + .where("meta.userid", "==", discordId) |
| 114 | + .limit(1) |
| 115 | + .get(); |
| 116 | + |
| 117 | + expect(isDiscordRoleRemoved.success).to.be.equal(false); |
| 118 | + expect(isDiscordRoleRemoved.message).to.be.equal("Role deletion from database failed"); |
| 119 | + expect(failedLog.docs[0].data().body.message).to.be.equal("Role deletion from database failed"); |
| 120 | + |
| 121 | + removeMemberGroupStub.restore(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments