Warn when demoting self via /op and /deop slash commands (#11214)
* Warn when demoting self via /op and /deop slash commands * Iterate and DRY * i18n * Improve coverage * Improve coverage * Improve coverage * Iterate
This commit is contained in:
parent
b6c7fe4235
commit
a8f632ae19
8 changed files with 431 additions and 267 deletions
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
|
||||
import { mocked } from "jest-mock";
|
||||
|
||||
import { Command, Commands, getCommand } from "../src/SlashCommands";
|
||||
|
@ -26,6 +26,9 @@ import { SdkContextClass } from "../src/contexts/SDKContext";
|
|||
import Modal from "../src/Modal";
|
||||
import WidgetUtils from "../src/utils/WidgetUtils";
|
||||
import { WidgetType } from "../src/widgets/WidgetType";
|
||||
import { warnSelfDemote } from "../src/components/views/right_panel/UserInfo";
|
||||
|
||||
jest.mock("../src/components/views/right_panel/UserInfo");
|
||||
|
||||
describe("SlashCommands", () => {
|
||||
let client: MatrixClient;
|
||||
|
@ -47,7 +50,7 @@ describe("SlashCommands", () => {
|
|||
});
|
||||
};
|
||||
|
||||
const setCurrentLocalRoon = (): void => {
|
||||
const setCurrentLocalRoom = (): void => {
|
||||
mocked(SdkContextClass.instance.roomViewStore.getRoomId).mockReturnValue(localRoomId);
|
||||
mocked(client.getRoom).mockImplementation((rId: string): Room | null => {
|
||||
if (rId === localRoomId) return localRoom;
|
||||
|
@ -60,8 +63,8 @@ describe("SlashCommands", () => {
|
|||
|
||||
client = createTestClient();
|
||||
|
||||
room = new Room(roomId, client, client.getUserId()!);
|
||||
localRoom = new LocalRoom(localRoomId, client, client.getUserId()!);
|
||||
room = new Room(roomId, client, client.getSafeUserId());
|
||||
localRoom = new LocalRoom(localRoomId, client, client.getSafeUserId());
|
||||
|
||||
jest.spyOn(SdkContextClass.instance.roomViewStore, "getRoomId");
|
||||
});
|
||||
|
@ -116,12 +119,73 @@ describe("SlashCommands", () => {
|
|||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
setCurrentLocalRoom();
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("/op", () => {
|
||||
beforeEach(() => {
|
||||
command = findCommand("op")!;
|
||||
});
|
||||
|
||||
it("should return usage if no args", () => {
|
||||
expect(command.run(client, roomId, null, undefined).error).toBe(command.getUsage());
|
||||
});
|
||||
|
||||
it("should reject with usage if given an invalid power level value", () => {
|
||||
expect(command.run(client, roomId, null, "@bob:server Admin").error).toBe(command.getUsage());
|
||||
});
|
||||
|
||||
it("should reject with usage for invalid input", () => {
|
||||
expect(command.run(client, roomId, null, " ").error).toBe(command.getUsage());
|
||||
});
|
||||
|
||||
it("should warn about self demotion", async () => {
|
||||
setCurrentRoom();
|
||||
const member = new RoomMember(roomId, client.getSafeUserId());
|
||||
member.membership = "join";
|
||||
member.powerLevel = 100;
|
||||
room.getMember = () => member;
|
||||
command.run(client, roomId, null, `${client.getUserId()} 0`);
|
||||
expect(warnSelfDemote).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should default to 50 if no powerlevel specified", async () => {
|
||||
setCurrentRoom();
|
||||
const member = new RoomMember(roomId, "@user:server");
|
||||
member.membership = "join";
|
||||
room.getMember = () => member;
|
||||
command.run(client, roomId, null, member.userId);
|
||||
expect(client.setPowerLevel).toHaveBeenCalledWith(roomId, member.userId, 50);
|
||||
});
|
||||
});
|
||||
|
||||
describe("/deop", () => {
|
||||
beforeEach(() => {
|
||||
command = findCommand("deop")!;
|
||||
});
|
||||
|
||||
it("should return usage if no args", () => {
|
||||
expect(command.run(client, roomId, null, undefined).error).toBe(command.getUsage());
|
||||
});
|
||||
|
||||
it("should warn about self demotion", async () => {
|
||||
setCurrentRoom();
|
||||
const member = new RoomMember(roomId, client.getSafeUserId());
|
||||
member.membership = "join";
|
||||
member.powerLevel = 100;
|
||||
room.getMember = () => member;
|
||||
command.run(client, roomId, null, client.getSafeUserId());
|
||||
expect(warnSelfDemote).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should reject with usage for invalid input", () => {
|
||||
expect(command.run(client, roomId, null, " ").error).toBe(command.getUsage());
|
||||
});
|
||||
});
|
||||
|
||||
describe("/tovirtual", () => {
|
||||
beforeEach(() => {
|
||||
command = findCommand("tovirtual")!;
|
||||
|
@ -139,7 +203,7 @@ describe("SlashCommands", () => {
|
|||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
setCurrentLocalRoom();
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -155,7 +219,7 @@ describe("SlashCommands", () => {
|
|||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
setCurrentLocalRoom();
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -181,7 +245,7 @@ describe("SlashCommands", () => {
|
|||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
setCurrentLocalRoom();
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -199,7 +263,7 @@ describe("SlashCommands", () => {
|
|||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
setCurrentLocalRoom();
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -208,9 +272,9 @@ describe("SlashCommands", () => {
|
|||
|
||||
describe("/part", () => {
|
||||
it("should part room matching alias if found", async () => {
|
||||
const room1 = new Room("room-id", client, client.getUserId()!);
|
||||
const room1 = new Room("room-id", client, client.getSafeUserId());
|
||||
room1.getCanonicalAlias = jest.fn().mockReturnValue("#foo:bar");
|
||||
const room2 = new Room("other-room", client, client.getUserId()!);
|
||||
const room2 = new Room("other-room", client, client.getSafeUserId());
|
||||
room2.getCanonicalAlias = jest.fn().mockReturnValue("#baz:bar");
|
||||
mocked(client.getRooms).mockReturnValue([room1, room2]);
|
||||
|
||||
|
@ -222,9 +286,9 @@ describe("SlashCommands", () => {
|
|||
});
|
||||
|
||||
it("should part room matching alt alias if found", async () => {
|
||||
const room1 = new Room("room-id", client, client.getUserId()!);
|
||||
const room1 = new Room("room-id", client, client.getSafeUserId());
|
||||
room1.getAltAliases = jest.fn().mockReturnValue(["#foo:bar"]);
|
||||
const room2 = new Room("other-room", client, client.getUserId()!);
|
||||
const room2 = new Room("other-room", client, client.getSafeUserId());
|
||||
room2.getAltAliases = jest.fn().mockReturnValue(["#baz:bar"]);
|
||||
mocked(client.getRooms).mockReturnValue([room1, room2]);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue