From b12917343ab9b856f879b94f3d6f8357c6832685 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 1 Dec 2022 08:25:05 +0000 Subject: [PATCH 1/2] Fix power selector being wrongly disabled for admins themselves --- .../views/settings/tabs/room/RolesRoomSettingsTab.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/components/views/settings/tabs/room/RolesRoomSettingsTab.tsx b/src/components/views/settings/tabs/room/RolesRoomSettingsTab.tsx index 16b85fb2b22..3a273f4561c 100644 --- a/src/components/views/settings/tabs/room/RolesRoomSettingsTab.tsx +++ b/src/components/views/settings/tabs/room/RolesRoomSettingsTab.tsx @@ -324,12 +324,13 @@ export default class RolesRoomSettingsTab extends React.Component { let privilegedUsersSection =
{ _t('No users have specific privileges in this room') }
; let mutedUsersSection; if (Object.keys(userLevels).length) { - const privilegedUsers = []; - const mutedUsers = []; + const privilegedUsers: JSX.Element[] = []; + const mutedUsers: JSX.Element[] = []; Object.keys(userLevels).forEach((user) => { - if (!Number.isInteger(userLevels[user])) { return; } - const canChange = userLevels[user] < currentUserLevel && canChangeLevels; + if (!Number.isInteger(userLevels[user])) return; + const isMe = user === client.getUserId(); + const canChange = canChangeLevels && (userLevels[user] < currentUserLevel || isMe); if (userLevels[user] > defaultUserLevel) { // privileged privilegedUsers.push( Date: Thu, 1 Dec 2022 08:52:49 +0000 Subject: [PATCH 2/2] Add tests --- .../tabs/room/RolesRoomSettingsTab-test.tsx | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/test/components/views/settings/tabs/room/RolesRoomSettingsTab-test.tsx b/test/components/views/settings/tabs/room/RolesRoomSettingsTab-test.tsx index dc2427e1436..8bf89df64d2 100644 --- a/test/components/views/settings/tabs/room/RolesRoomSettingsTab-test.tsx +++ b/test/components/views/settings/tabs/room/RolesRoomSettingsTab-test.tsx @@ -18,6 +18,9 @@ import React from "react"; import { fireEvent, render, RenderResult } from "@testing-library/react"; import { MatrixClient } from "matrix-js-sdk/src/client"; import { EventType } from "matrix-js-sdk/src/@types/event"; +import { MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { Room } from "matrix-js-sdk/src/models/room"; +import { mocked } from "jest-mock"; import RolesRoomSettingsTab from "../../../../../../src/components/views/settings/tabs/room/RolesRoomSettingsTab"; import { mkStubRoom, stubClient } from "../../../../../test-utils"; @@ -29,23 +32,52 @@ import { ElementCall } from "../../../../../../src/models/Call"; describe("RolesRoomSettingsTab", () => { const roomId = "!room:example.com"; let cli: MatrixClient; + let room: Room; const renderTab = (): RenderResult => { return render(); }; - const getVoiceBroadcastsSelect = () => { + const getVoiceBroadcastsSelect = (): HTMLElement => { return renderTab().container.querySelector("select[label='Voice broadcasts']"); }; - const getVoiceBroadcastsSelectedOption = () => { + const getVoiceBroadcastsSelectedOption = (): HTMLElement => { return renderTab().container.querySelector("select[label='Voice broadcasts'] option:checked"); }; beforeEach(() => { stubClient(); cli = MatrixClientPeg.get(); - mkStubRoom(roomId, "test room", cli); + room = mkStubRoom(roomId, "test room", cli); + }); + + it("should allow an Admin to demote themselves but not others", () => { + mocked(cli.getRoom).mockReturnValue(room); + // @ts-ignore - mocked doesn't support overloads properly + mocked(room.currentState.getStateEvents).mockImplementation((type, key) => { + if (key === undefined) return [] as MatrixEvent[]; + if (type === "m.room.power_levels") { + return new MatrixEvent({ + sender: "@sender:server", + room_id: roomId, + type: "m.room.power_levels", + state_key: "", + content: { + users: { + [cli.getUserId()]: 100, + "@admin:server": 100, + }, + }, + }); + } + return null; + }); + mocked(room.currentState.mayClientSendStateEvent).mockReturnValue(true); + const { container } = renderTab(); + + expect(container.querySelector(`[placeholder="${cli.getUserId()}"]`)).not.toBeDisabled(); + expect(container.querySelector(`[placeholder="@admin:server"]`)).toBeDisabled(); }); it("should initially show »Moderator« permission for »Voice broadcasts«", () => { @@ -79,19 +111,19 @@ describe("RolesRoomSettingsTab", () => { }); }; - const getStartCallSelect = (tab: RenderResult) => { + const getStartCallSelect = (tab: RenderResult): HTMLElement => { return tab.container.querySelector("select[label='Start Element Call calls']"); }; - const getStartCallSelectedOption = (tab: RenderResult) => { + const getStartCallSelectedOption = (tab: RenderResult): HTMLElement => { return tab.container.querySelector("select[label='Start Element Call calls'] option:checked"); }; - const getJoinCallSelect = (tab: RenderResult) => { + const getJoinCallSelect = (tab: RenderResult): HTMLElement => { return tab.container.querySelector("select[label='Join Element Call calls']"); }; - const getJoinCallSelectedOption = (tab: RenderResult) => { + const getJoinCallSelectedOption = (tab: RenderResult): HTMLElement => { return tab.container.querySelector("select[label='Join Element Call calls'] option:checked"); };