Skip to content

Commit cf079e2

Browse files
Add test "a student cannot use faculty command" for all faculty commands
1 parent d74dc44 commit cf079e2

14 files changed

+165
-45
lines changed

__tests__/jest/slashCommands/slash_add_instructors.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { findUserByDiscordId } = require("../../../src/db/services/userService");
55
const { findCourseFromDb } = require("../../../src/db/services/courseService");
66
const { findCourseMember } = require("../../../src/db/services/courseMemberService");
77
const { courseAdminRole } = require("../../../config.json");
8-
const { defaultTeacherInteraction, defaultAdminInteraction } = require("../../mocks/mockInteraction");
8+
const { defaultStudentInteraction, defaultTeacherInteraction, defaultAdminInteraction } = require("../../mocks/mockInteraction");
99
const models = require("../../mocks/mockModels");
1010

1111
jest.mock("../../../src/discordBot/services/message");
@@ -25,6 +25,7 @@ getUserWithUserId.mockImplementation(() => defaultAdminInteraction.member.user);
2525

2626
defaultAdminInteraction.options = { getString: jest.fn(() => { return "<@!3>"; }) };
2727
defaultTeacherInteraction.options = { getUser: jest.fn(() => { return { id: 2 }; }) };
28+
defaultStudentInteraction.options = { getUser: jest.fn(() => { return { id: 2 }; }) };
2829

2930
const initialResponse = "Adding instructors...";
3031

@@ -91,4 +92,12 @@ describe("slash add instructor command", () => {
9192
expect(editEphemeral).toHaveBeenCalledTimes(1);
9293
expect(editEphemeral).toHaveBeenCalledWith(defaultAdminInteraction, response);
9394
});
95+
96+
test("a student cannot use faculty command", async () => {
97+
const client = defaultStudentInteraction.client;
98+
const response = "You do not have permission to use this command.";
99+
await execute(defaultStudentInteraction, client, models);
100+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
101+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
102+
});
94103
});

__tests__/jest/slashCommands/slash_create_channel.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/create_channel");
2-
const { editEphemeral, editErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
2+
const { editEphemeral, editErrorEphemeral, sendErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
33
const { getCourseNameFromCategory } = require("../../../src/discordBot/services/service");
44
const { findCourseFromDb } = require("../../../src/db/services/courseService");
55
const { createChannelToDatabase, countChannelsByCourse, findChannelFromDbByName } = require("../../../src/db/services/channelService");
@@ -12,12 +12,13 @@ jest.mock("../../../src/db/services/channelService");
1212

1313
getCourseNameFromCategory.mockImplementation((name) => name.replace("📚", "").trim());
1414

15-
const { defaultTeacherInteraction } = require("../../mocks/mockInteraction");
15+
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
1616
findCourseFromDb.mockImplementation((courseName) => ({ name: courseName, courseName: courseName, id: 1 }));
1717
const courseName = "test";
1818
const channelName = "rules";
1919
const initialResponse = "Creating text channel...";
2020
defaultTeacherInteraction.options = { getString: jest.fn(() => channelName) };
21+
defaultStudentInteraction.options = { getString: jest.fn(() => channelName) };
2122

2223
afterEach(() => {
2324
jest.clearAllMocks();
@@ -83,4 +84,12 @@ describe("slash create channel command", () => {
8384
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
8485
expect(editErrorEphemeral).toHaveBeenCalledWith(defaultTeacherInteraction, response);
8586
});
87+
88+
test("a student cannot use faculty command", async () => {
89+
const client = defaultStudentInteraction.client;
90+
const response = "You do not have permission to use this command.";
91+
await execute(defaultStudentInteraction, client, models);
92+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
93+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
94+
});
8695
});

__tests__/jest/slashCommands/slash_create_course.test.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ createCourseToDatabase.mockImplementation(() => {return { name: "nickname", id:
2222
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
2323

2424
beforeEach(() => {
25-
defaultTeacherInteraction.options = {
26-
getString: jest.fn((name) => {
27-
const names = {
28-
coursecode: "TKT-100",
29-
full_name: "Long course name",
30-
nick_name: "nickname",
31-
};
32-
return names[name];
33-
}),
34-
};
25+
defaultTeacherInteraction.options = {
26+
getString: jest.fn((name) => {
27+
const names = {
28+
coursecode: "TKT-100",
29+
full_name: "Long course name",
30+
nick_name: "nickname",
31+
};
32+
return names[name];
33+
}),
34+
};
3535

36-
defaultStudentInteraction.options = {
37-
getString: jest.fn((name) => {
38-
const names = {
39-
coursecode: "TKT-100",
40-
full_name: "Long course name",
41-
};
42-
return names[name];
43-
}),
44-
};
36+
defaultStudentInteraction.options = {
37+
getString: jest.fn((name) => {
38+
const names = {
39+
coursecode: "TKT-100",
40+
full_name: "Long course name",
41+
};
42+
return names[name];
43+
}),
44+
};
4545
});
4646
afterEach(() => {
4747
jest.clearAllMocks();
@@ -67,7 +67,7 @@ describe("slash create command", () => {
6767
});
6868

6969
test("course code must be unique when nickname not given", async () => {
70-
defaultTeacherInteraction.options = {
70+
defaultTeacherInteraction.options = {
7171
getString: jest.fn((name) => {
7272
const names = {
7373
coursecode: "TKT-100",
@@ -86,7 +86,7 @@ defaultTeacherInteraction.options = {
8686
});
8787

8888
test("create course name without nick", async () => {
89-
defaultTeacherInteraction.options = {
89+
defaultTeacherInteraction.options = {
9090
getString: jest.fn((name) => {
9191
const names = {
9292
coursecode: "TKT-100",
@@ -160,4 +160,11 @@ defaultTeacherInteraction.options = {
160160
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultTeacherInteraction, "Emojis are not allowed!");
161161
});
162162

163+
test("a student cannot use faculty command", async () => {
164+
const client = defaultStudentInteraction.client;
165+
const response = "You do not have permission to use this command.";
166+
await execute(defaultStudentInteraction, client, models);
167+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
168+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
169+
});
163170
});

__tests__/jest/slashCommands/slash_delete_channel.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/delete_channel");
2-
const { sendEphemeral, editEphemeral, editErrorEphemeral } = require("../../../src/discordBot/services/message");
2+
const { sendEphemeral, editEphemeral, editErrorEphemeral, sendErrorEphemeral } = require("../../../src/discordBot/services/message");
33
const { confirmChoice } = require("../../../src/discordBot/services/confirm");
44
const { removeChannelFromDb, findChannelFromDbByName } = require("../../../src/db/services/channelService");
55
const { findCourseFromDb } = require("../../../src/db/services/courseService");
@@ -11,8 +11,9 @@ jest.mock("../../../src/db/services/channelService");
1111
jest.mock("../../../src/db/services/courseService");
1212

1313
const models = require("../../mocks/mockModels");
14-
const { defaultTeacherInteraction } = require("../../mocks/mockInteraction");
14+
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
1515
defaultTeacherInteraction.options = { getString: jest.fn((name) => name) };
16+
defaultStudentInteraction.options = { getString: jest.fn((name) => name) };
1617

1718
const initialResponse = "Deleting text channel...";
1819

@@ -125,4 +126,12 @@ describe("slash delete_channel", () => {
125126
expect(editEphemeral).toHaveBeenCalledTimes(1);
126127
expect(editEphemeral).toHaveBeenCalledWith(defaultTeacherInteraction, response);
127128
});
129+
130+
test("a student cannot use faculty command", async () => {
131+
const client = defaultStudentInteraction.client;
132+
const response = "You do not have permission to use this command.";
133+
await execute(defaultStudentInteraction, client, models);
134+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
135+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
136+
});
128137
});

__tests__/jest/slashCommands/slash_disable_bridge.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/disable_bridge");
2-
const { editEphemeral, editErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
2+
const { editEphemeral, editErrorEphemeral, sendErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
33
const { confirmChoice } = require("../../../src/discordBot/services/confirm");
44
const { findCourseFromDb } = require("../../../src/db/services/courseService");
55
const { isCourseCategory } = require("../../../src/discordBot/services/service");
@@ -12,7 +12,7 @@ jest.mock("../../../src/db/services/courseService");
1212
jest.mock("../../../src/db/services/channelService");
1313
jest.mock("../../../src/discordBot/services/service");
1414

15-
const { defaultTeacherInteraction } = require("../../mocks/mockInteraction");
15+
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
1616
const initalResponse = "Disabling the bridge to Telegram...";
1717
const defaultChannelModelInstanceMock = { save: jest.fn(), bridged: true, defaultChannel: true };
1818
const nonDefaultBridgedChannelModelInstanceMock = { save: jest.fn(), bridged: true, defaultChannel: false };
@@ -80,4 +80,12 @@ describe("slash disable_bridge command", () => {
8080
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
8181
expect(editErrorEphemeral).toHaveBeenCalledWith(defaultTeacherInteraction, response);
8282
});
83+
84+
test("a student cannot use faculty command", async () => {
85+
const client = defaultStudentInteraction.client;
86+
const response = "You do not have permission to use this command.";
87+
await execute(defaultStudentInteraction, client, models);
88+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
89+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
90+
});
8391
});

__tests__/jest/slashCommands/slash_edit_course.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/edit_course");
2-
const { sendEphemeral, editErrorEphemeral, editEphemeral } = require("../../../src/discordBot/services/message");
2+
const { sendEphemeral, editErrorEphemeral, sendErrorEphemeral, editEphemeral } = require("../../../src/discordBot/services/message");
33
const { confirmChoice } = require("../../../src/discordBot/services/confirm");
44
const {
55
findCategoryWithCourseName,
@@ -140,4 +140,11 @@ describe("slash edit command", () => {
140140
expect(editErrorEphemeral).toHaveBeenCalledWith(defaultTeacherInteraction, response);
141141
});
142142

143+
test("a student cannot use faculty command", async () => {
144+
const client = defaultStudentInteraction.client;
145+
const response = "You do not have permission to use this command.";
146+
await execute(defaultStudentInteraction, client, models);
147+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
148+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
149+
});
143150
});

__tests__/jest/slashCommands/slash_edit_topic.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/edit_topic");
2-
const { sendEphemeral, editErrorEphemeral, editEphemeral } = require("../../../src/discordBot/services/message");
2+
const { sendEphemeral, editErrorEphemeral, sendErrorEphemeral, editEphemeral } = require("../../../src/discordBot/services/message");
33
const { confirmChoice } = require("../../../src/discordBot/services/confirm");
44
const { findChannelFromDbByName, saveChannelTopicToDb } = require("../../../src/db/services/channelService");
55
const {
@@ -25,9 +25,11 @@ confirmChoice.mockImplementation(() => true);
2525
const mockSaveMethod = jest.fn();
2626
findChannelFromDbByName.mockImplementation(() => { return { topic: "topic", save: mockSaveMethod }; });
2727

28-
const { defaultTeacherInteraction } = require("../../mocks/mockInteraction");
28+
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
2929
const newTopic = "New topic!";
3030
defaultTeacherInteraction.options = { getString: jest.fn(() => newTopic) };
31+
defaultStudentInteraction.options = { getString: jest.fn(() => newTopic) };
32+
3133

3234
afterEach(() => {
3335
jest.clearAllMocks();
@@ -79,4 +81,12 @@ describe("slash edit_topic command", () => {
7981
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
8082
expect(editErrorEphemeral).toHaveBeenCalledWith(defaultTeacherInteraction, response);
8183
});
84+
85+
test("a student cannot use faculty command", async () => {
86+
const client = defaultStudentInteraction.client;
87+
const response = "You do not have permission to use this command.";
88+
await execute(defaultStudentInteraction, client, models);
89+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
90+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
91+
});
8292
});

__tests__/jest/slashCommands/slash_enable_bridge.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/enable_bridge");
2-
const { editEphemeral, editErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
2+
const { editEphemeral, editErrorEphemeral, sendErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
33
const { confirmChoice } = require("../../../src/discordBot/services/confirm");
44
const { findCourseFromDb } = require("../../../src/db/services/courseService");
55
const { isCourseCategory } = require("../../../src/discordBot/services/service");
@@ -13,7 +13,7 @@ jest.mock("../../../src/discordBot/services/service");
1313
jest.mock("../../../src/db/services/courseService");
1414
jest.mock("../../../src/db/services/channelService");
1515

16-
const { defaultTeacherInteraction } = require("../../mocks/mockInteraction");
16+
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
1717
const initalResponse = "Enabling the bridge to Telegram...";
1818
const defaultChannelModelInstanceMock = { save: jest.fn(), bridged: false, defaultChannel: true };
1919
const nonDefaultBridgedChannelModelInstanceMock = { save: jest.fn(), bridged: true, defaultChannel: false };
@@ -81,4 +81,12 @@ describe("slash enable_bridge command", () => {
8181
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
8282
expect(editErrorEphemeral).toHaveBeenCalledWith(defaultTeacherInteraction, response);
8383
});
84+
85+
test("a student cannot use faculty command", async () => {
86+
const client = defaultStudentInteraction.client;
87+
const response = "You do not have permission to use this command.";
88+
await execute(defaultStudentInteraction, client, models);
89+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
90+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
91+
});
8492
});

__tests__/jest/slashCommands/slash_hide_course.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/hide_course");
2-
const { editEphemeral, editErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
2+
const { editEphemeral, editErrorEphemeral, sendErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
33
const { confirmChoice } = require("../../../src/discordBot/services/confirm");
44
const {
55
msToMinutesAndSeconds,
@@ -14,9 +14,10 @@ jest.mock("../../../src/db/services/courseService");
1414
const time = "4:59";
1515
const initialResponse = "Hiding course...";
1616

17-
const { defaultTeacherInteraction } = require("../../mocks/mockInteraction");
17+
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
1818
const courseName = "test";
1919
defaultTeacherInteraction.options = { getString: jest.fn(() => courseName) };
20+
defaultStudentInteraction.options = { getString: jest.fn(() => courseName) };
2021
confirmChoice.mockImplementation(() => true);
2122
afterEach(() => {
2223
jest.clearAllMocks();
@@ -71,4 +72,12 @@ describe("slash hide command", () => {
7172
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
7273
expect(client.emit).toHaveBeenCalledTimes(0);
7374
});
75+
76+
test("a student cannot use faculty command", async () => {
77+
const client = defaultStudentInteraction.client;
78+
const response = "You do not have permission to use this command.";
79+
await execute(defaultStudentInteraction, client, models);
80+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
81+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
82+
});
7483
});

__tests__/jest/slashCommands/slash_lock_chat.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { execute } = require("../../../src/discordBot/commands/faculty/lock_chat");
2-
const { editEphemeral, editErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
2+
const { editEphemeral, editErrorEphemeral, sendErrorEphemeral, sendEphemeral } = require("../../../src/discordBot/services/message");
33
const { confirmChoice } = require("../../../src/discordBot/services/confirm");
44
const {
55
msToMinutesAndSeconds,
@@ -15,9 +15,10 @@ jest.mock("../../../src/db/services/courseService");
1515
const time = "4:59";
1616
const initialResponse = "Locking course...";
1717

18-
const { defaultTeacherInteraction } = require("../../mocks/mockInteraction");
18+
const { defaultTeacherInteraction, defaultStudentInteraction } = require("../../mocks/mockInteraction");
1919
const courseName = "test";
2020
defaultTeacherInteraction.options = { getString: jest.fn(() => courseName) };
21+
defaultStudentInteraction.options = { getString: jest.fn(() => courseName) };
2122
confirmChoice.mockImplementation(() => true);
2223
afterEach(() => {
2324
jest.clearAllMocks();
@@ -72,4 +73,12 @@ describe("slash lock_chat command", () => {
7273
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
7374
expect(client.emit).toHaveBeenCalledTimes(0);
7475
});
76+
77+
test("a student cannot use faculty command", async () => {
78+
const client = defaultStudentInteraction.client;
79+
const response = "You do not have permission to use this command.";
80+
await execute(defaultStudentInteraction, client, models);
81+
expect(sendErrorEphemeral).toHaveBeenCalledTimes(1);
82+
expect(sendErrorEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, response);
83+
});
7584
});

0 commit comments

Comments
 (0)