Skip to content

Commit d492a9f

Browse files
changed to typescript and modified unit tests
1 parent d8027cf commit d492a9f

File tree

4 files changed

+138
-92
lines changed

4 files changed

+138
-92
lines changed

models/discordactions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const updateGroupRole = async (roleData, docId) => {
133133
*
134134
* @param options { Object }: Data of the new role
135135
* @param options.rolename String : name of the role
136-
* @param options.roleId String : id of the role
136+
* @param options.roleid String : id of the role
137137
* @returns {Promise<discordRoleModel|Object>}
138138
*/
139139

test/unit/utils/remvoeDiscordRoleFromUser.test.js

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
});
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const discordRolesModel = require("../models/discordactions");
2-
const discordServices = require("../services/discordService");
3-
const { logType } = require("../constants/logs");
4-
const { addLog } = require("../models/logs");
1+
import { logType } from "../constants/logs";
2+
import discordActions from "../models/discordactions";
3+
import { addLog } from "../models/logs";
4+
import discordServices from "../services/discordService";
5+
import { userData } from "../types/global";
56

67
/**
78
* Removes a Discord role from a user using Discord Id.
@@ -10,11 +11,15 @@ const { addLog } = require("../models/logs");
1011
* @param {string} discordId - User's Discord ID.
1112
* @param {string} roleId - Discord Role ID.
1213
*
13-
* @returns {Promise<Object>} - Result with success status and message.
14+
* @returns {Promise<{ success: boolean; message: string }>} - Result with success status and message.
1415
*/
15-
export const removeDiscordRoleFromUser = async (userData, discordId, roleId) => {
16+
export const removeDiscordRoleFromUser = async (
17+
userData: userData,
18+
discordId: string,
19+
roleId: string
20+
): Promise<{ success: boolean; message: string }> => {
1621
try {
17-
const { roleExists } = await discordRolesModel.isGroupRoleExists({ roleid: roleId });
22+
const { roleExists } = await discordActions.isGroupRoleExists({ roleid: roleId, rolename: null });
1823

1924
if (!roleExists) {
2025
const message = "Role doesn't exist";
@@ -30,8 +35,8 @@ export const removeDiscordRoleFromUser = async (userData, discordId, roleId) =>
3035
throw new Error(message);
3136
}
3237

33-
const { wasSuccess } = await discordRolesModel.removeMemberGroup(roleId, discordId);
34-
if (!wasSuccess) {
38+
const deleteResponse = await discordActions.removeMemberGroup(roleId, discordId);
39+
if (deleteResponse && !deleteResponse.wasSuccess) {
3540
const message = "Role deletion from database failed";
3641
await addLog(logType.REMOVE_ROLE_FROM_USER_FAILED, { roleId, userid: discordId }, { message, userData });
3742
throw new Error(message);

0 commit comments

Comments
 (0)