1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-08-09 08:42:50 +03:00

Apply prettier formatting

This commit is contained in:
Michael Weimann
2022-12-12 12:24:14 +01:00
parent 1cac306093
commit 526645c791
1576 changed files with 65385 additions and 62478 deletions

View File

@@ -12,40 +12,37 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {
Room,
RoomMember,
EventType,
MatrixEvent,
} from 'matrix-js-sdk/src/matrix';
import { Room, RoomMember, EventType, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from '../../../src/MatrixClientPeg';
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
import {
makeRoomPermalink,
makeUserPermalink,
parsePermalink,
RoomPermalinkCreator,
} from "../../../src/utils/permalinks/Permalinks";
import { getMockClientWithEventEmitter } from '../../test-utils';
import { getMockClientWithEventEmitter } from "../../test-utils";
describe('Permalinks', function() {
const userId = '@test:example.com';
describe("Permalinks", function () {
const userId = "@test:example.com";
const mockClient = getMockClientWithEventEmitter({
getUserId: jest.fn().mockReturnValue(userId),
getRoom: jest.fn(),
});
mockClient.credentials = { userId };
const makeMemberWithPL = (roomId: Room['roomId'], userId: string, powerLevel: number): RoomMember => {
const makeMemberWithPL = (roomId: Room["roomId"], userId: string, powerLevel: number): RoomMember => {
const member = new RoomMember(roomId, userId);
member.powerLevel = powerLevel;
return member;
};
function mockRoom(
roomId: Room['roomId'], members: RoomMember[], serverACLContent?: { deny?: string[], allow?: string[]},
roomId: Room["roomId"],
members: RoomMember[],
serverACLContent?: { deny?: string[]; allow?: string[] },
): Room {
members.forEach(m => m.membership = "join");
members.forEach((m) => (m.membership = "join"));
const powerLevelsUsers = members.reduce((pl, member) => {
if (Number.isFinite(member.powerLevel)) {
pl[member.userId] = member.powerLevel;
@@ -58,35 +55,38 @@ describe('Permalinks', function() {
const powerLevels = new MatrixEvent({
type: EventType.RoomPowerLevels,
room_id: roomId,
state_key: '',
state_key: "",
content: {
users: powerLevelsUsers, users_default: 0,
users: powerLevelsUsers,
users_default: 0,
},
});
const serverACL = serverACLContent ? new MatrixEvent({
type: EventType.RoomServerAcl,
room_id: roomId,
state_key: '',
content: serverACLContent,
}) : undefined;
const serverACL = serverACLContent
? new MatrixEvent({
type: EventType.RoomServerAcl,
room_id: roomId,
state_key: "",
content: serverACLContent,
})
: undefined;
const stateEvents = serverACL ? [powerLevels, serverACL] : [powerLevels];
room.currentState.setStateEvents(stateEvents);
jest.spyOn(room, 'getCanonicalAlias').mockReturnValue(null);
jest.spyOn(room, 'getJoinedMembers').mockReturnValue(members);
jest.spyOn(room, 'getMember').mockImplementation((userId) => members.find(m => m.userId === userId));
jest.spyOn(room, "getCanonicalAlias").mockReturnValue(null);
jest.spyOn(room, "getJoinedMembers").mockReturnValue(members);
jest.spyOn(room, "getMember").mockImplementation((userId) => members.find((m) => m.userId === userId));
return room;
}
beforeEach(function() {
beforeEach(function () {
jest.clearAllMocks();
});
afterAll(() => {
jest.spyOn(MatrixClientPeg, 'get').mockRestore();
jest.spyOn(MatrixClientPeg, "get").mockRestore();
});
it('should pick no candidate servers when the room has no members', function() {
it("should pick no candidate servers when the room has no members", function () {
const room = mockRoom("!fake:example.org", []);
const creator = new RoomPermalinkCreator(room);
creator.load();
@@ -94,7 +94,7 @@ describe('Permalinks', function() {
expect(creator.serverCandidates.length).toBe(0);
});
it('should gracefully handle invalid MXIDs', () => {
it("should gracefully handle invalid MXIDs", () => {
const roomId = "!fake:example.org";
const alice50 = makeMemberWithPL(roomId, "@alice:pl_50:org", 50);
const room = mockRoom(roomId, [alice50]);
@@ -103,16 +103,12 @@ describe('Permalinks', function() {
expect(creator.serverCandidates).toBeTruthy();
});
it('should pick a candidate server for the highest power level user in the room', function() {
it("should pick a candidate server for the highest power level user in the room", function () {
const roomId = "!fake:example.org";
const alice50 = makeMemberWithPL(roomId, "@alice:pl_50", 50);
const alice75 = makeMemberWithPL(roomId, "@alice:pl_75", 75);
const alice95 = makeMemberWithPL(roomId, "@alice:pl_95", 95);
const room = mockRoom("!fake:example.org", [
alice50,
alice75,
alice95,
]);
const room = mockRoom("!fake:example.org", [alice50, alice75, alice95]);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
@@ -121,7 +117,7 @@ describe('Permalinks', function() {
// we don't check the 2nd and 3rd servers because that is done by the next test
});
it('should change candidate server when highest power level user leaves the room', function() {
it("should change candidate server when highest power level user leaves the room", function () {
const roomId = "!fake:example.org";
const member95 = makeMemberWithPL(roomId, "@alice:pl_95", 95);
@@ -143,7 +139,7 @@ describe('Permalinks', function() {
expect(creator.serverCandidates[0]).toBe("pl_95");
});
it('should pick candidate servers based on user population', function() {
it("should pick candidate servers based on user population", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:first", 0),
@@ -162,7 +158,7 @@ describe('Permalinks', function() {
expect(creator.serverCandidates[2]).toBe("third");
});
it('should pick prefer candidate servers with higher power levels', function() {
it("should pick prefer candidate servers with higher power levels", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:first", 100),
@@ -178,7 +174,7 @@ describe('Permalinks', function() {
expect(creator.serverCandidates[2]).toBe("third");
});
it('should pick a maximum of 3 candidate servers', function() {
it("should pick a maximum of 3 candidate servers", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:alpha", 100),
@@ -193,55 +189,45 @@ describe('Permalinks', function() {
expect(creator.serverCandidates.length).toBe(3);
});
it('should not consider IPv4 hosts', function() {
it("should not consider IPv4 hosts", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:127.0.0.1", 100),
]);
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:127.0.0.1", 100)]);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
expect(creator.serverCandidates.length).toBe(0);
});
it('should not consider IPv6 hosts', function() {
it("should not consider IPv6 hosts", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:[::1]", 100),
]);
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:[::1]", 100)]);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
expect(creator.serverCandidates.length).toBe(0);
});
it('should not consider IPv4 hostnames with ports', function() {
it("should not consider IPv4 hostnames with ports", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:127.0.0.1:8448", 100),
]);
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:127.0.0.1:8448", 100)]);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
expect(creator.serverCandidates.length).toBe(0);
});
it('should not consider IPv6 hostnames with ports', function() {
it("should not consider IPv6 hostnames with ports", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:[::1]:8448", 100),
]);
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:[::1]:8448", 100)]);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
expect(creator.serverCandidates.length).toBe(0);
});
it('should work with hostnames with ports', function() {
it("should work with hostnames with ports", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:example.org:8448", 100),
]);
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:example.org:8448", 100)]);
const creator = new RoomPermalinkCreator(room);
creator.load();
@@ -250,45 +236,57 @@ describe('Permalinks', function() {
expect(creator.serverCandidates[0]).toBe("example.org:8448");
});
it('should not consider servers explicitly denied by ACLs', function() {
it("should not consider servers explicitly denied by ACLs", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
], {
deny: ["evilcorp.com", "*.evilcorp.com"],
allow: ["*"],
});
const room = mockRoom(
roomId,
[
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
],
{
deny: ["evilcorp.com", "*.evilcorp.com"],
allow: ["*"],
},
);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
expect(creator.serverCandidates.length).toBe(0);
});
it('should not consider servers not allowed by ACLs', function() {
it("should not consider servers not allowed by ACLs", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
], {
deny: [],
allow: [], // implies "ban everyone"
});
const room = mockRoom(
roomId,
[
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
],
{
deny: [],
allow: [], // implies "ban everyone"
},
);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
expect(creator.serverCandidates.length).toBe(0);
});
it('should consider servers not explicitly banned by ACLs', function() {
it("should consider servers not explicitly banned by ACLs", function () {
const roomId = "!fake:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
], {
deny: ["*.evilcorp.com"], // evilcorp.com is still good though
allow: ["*"],
});
const room = mockRoom(
roomId,
[
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
],
{
deny: ["*.evilcorp.com"], // evilcorp.com is still good though
allow: ["*"],
},
);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
@@ -296,15 +294,19 @@ describe('Permalinks', function() {
expect(creator.serverCandidates[0]).toEqual("evilcorp.com");
});
it('should consider servers not disallowed by ACLs', function() {
it("should consider servers not disallowed by ACLs", function () {
const roomId = "!fake:example.org";
const room = mockRoom("!fake:example.org", [
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
], {
deny: [],
allow: ["evilcorp.com"], // implies "ban everyone else"
});
const room = mockRoom(
"!fake:example.org",
[
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
],
{
deny: [],
allow: ["evilcorp.com"], // implies "ban everyone else"
},
);
const creator = new RoomPermalinkCreator(room);
creator.load();
expect(creator.serverCandidates).toBeTruthy();
@@ -312,7 +314,7 @@ describe('Permalinks', function() {
expect(creator.serverCandidates[0]).toEqual("evilcorp.com");
});
it('should generate an event permalink for room IDs with no candidate servers', function() {
it("should generate an event permalink for room IDs with no candidate servers", function () {
const room = mockRoom("!somewhere:example.org", []);
const creator = new RoomPermalinkCreator(room);
creator.load();
@@ -320,7 +322,7 @@ describe('Permalinks', function() {
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com");
});
it('should generate an event permalink for room IDs with some candidate servers', function() {
it("should generate an event permalink for room IDs with some candidate servers", function () {
const roomId = "!somewhere:example.org";
const room = mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:first", 100),
@@ -332,8 +334,8 @@ describe('Permalinks', function() {
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com?via=first&via=second");
});
it('should generate a room permalink for room IDs with some candidate servers', function() {
mockClient.getRoom.mockImplementation((roomId: Room['roomId']) => {
it("should generate a room permalink for room IDs with some candidate servers", function () {
mockClient.getRoom.mockImplementation((roomId: Room["roomId"]) => {
return mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:first", 100),
makeMemberWithPL(roomId, "@bob:second", 0),
@@ -343,14 +345,14 @@ describe('Permalinks', function() {
expect(result).toBe("https://matrix.to/#/!somewhere:example.org?via=first&via=second");
});
it('should generate a room permalink for room aliases with no candidate servers', function() {
it("should generate a room permalink for room aliases with no candidate servers", function () {
mockClient.getRoom.mockReturnValue(null);
const result = makeRoomPermalink("#somewhere:example.org");
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
});
it('should generate a room permalink for room aliases without candidate servers', function() {
mockClient.getRoom.mockImplementation((roomId: Room['roomId']) => {
it("should generate a room permalink for room aliases without candidate servers", function () {
mockClient.getRoom.mockImplementation((roomId: Room["roomId"]) => {
return mockRoom(roomId, [
makeMemberWithPL(roomId, "@alice:first", 100),
makeMemberWithPL(roomId, "@bob:second", 0),
@@ -360,26 +362,27 @@ describe('Permalinks', function() {
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
});
it('should generate a user permalink', function() {
it("should generate a user permalink", function () {
const result = makeUserPermalink("@someone:example.org");
expect(result).toBe("https://matrix.to/#/@someone:example.org");
});
it('should correctly parse room permalinks with a via argument', () => {
it("should correctly parse room permalinks with a via argument", () => {
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=some.org");
expect(result.roomIdOrAlias).toBe("!room_id:server");
expect(result.viaServers).toEqual(["some.org"]);
});
it('should correctly parse room permalink via arguments', () => {
it("should correctly parse room permalink via arguments", () => {
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=foo.bar&via=bar.foo");
expect(result.roomIdOrAlias).toBe("!room_id:server");
expect(result.viaServers).toEqual(["foo.bar", "bar.foo"]);
});
it('should correctly parse event permalink via arguments', () => {
const result = parsePermalink("https://matrix.to/#/!room_id:server/$event_id/some_thing_here/foobar" +
"?via=m1.org&via=m2.org");
it("should correctly parse event permalink via arguments", () => {
const result = parsePermalink(
"https://matrix.to/#/!room_id:server/$event_id/some_thing_here/foobar" + "?via=m1.org&via=m2.org",
);
expect(result.eventId).toBe("$event_id/some_thing_here/foobar");
expect(result.roomIdOrAlias).toBe("!room_id:server");
expect(result.viaServers).toEqual(["m1.org", "m2.org"]);