Skip to content

Commit c537f20

Browse files
committed
protected rooms config test
#370
1 parent cea6944 commit c537f20

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
import { strict as assert } from "assert";
3+
import { MatrixClient, Permalinks, UserID } from "matrix-bot-sdk";
4+
import { Mjolnir } from "../../src/Mjolnir";
5+
import PolicyList from "../../src/models/PolicyList";
6+
import { newTestUser } from "./clientHelper";
7+
import { createBanList, getFirstReaction } from "./commands/commandUtils";
8+
9+
async function createPolicyList(client: MatrixClient): Promise<PolicyList> {
10+
const serverName = new UserID(await client.getUserId()).domain;
11+
const policyListId = await client.createRoom({ preset: "public_chat" });
12+
return new PolicyList(policyListId, Permalinks.forRoom(policyListId), client);
13+
}
14+
15+
async function getProtectedRoomsFromAccountData(client: MatrixClient): Promise<string[]> {
16+
const rooms: { rooms?: string[] } = await client.getAccountData("org.matrix.mjolnir.protected_rooms");
17+
return rooms.rooms!;
18+
}
19+
20+
describe('Test: config.protectAllJoinedRooms behaves correctly.', function() {
21+
it('does not clobber the account data.', async function() {
22+
// set up account data for a protected room with your own list and a watched list.
23+
const mjolnir: Mjolnir = this.mjolnir!;
24+
25+
// moderator sets up some rooms, that aren't explicitly protected
26+
const moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } });
27+
await moderator.joinRoom(mjolnir.managementRoomId);
28+
const implicitlyProtectedRooms = await Promise.all(
29+
[...Array(2).keys()].map(_ => moderator.createRoom({ preset: "public_chat" }))
30+
);
31+
await Promise.all(
32+
implicitlyProtectedRooms.map(roomId => mjolnir.client.joinRoom(roomId))
33+
);
34+
35+
// we sync and check that none of them end up in account data
36+
await mjolnir.protectedRoomsTracker.syncLists();
37+
(await getProtectedRoomsFromAccountData(mjolnir.client))
38+
.forEach(roomId => assert.equal(implicitlyProtectedRooms.includes(roomId), false));
39+
40+
// ... but they are protected
41+
mjolnir.protectedRoomsTracker.getProtectedRooms()
42+
.forEach(roomId => assert.equal(implicitlyProtectedRooms.includes(roomId), true));
43+
44+
// We create one policy list with Mjolnir, and we watch another that is maintained by someone else.
45+
const policyListShortcode = await createBanList(mjolnir.managementRoomId, mjolnir.client, moderator);
46+
const unprotectedWatchedList = await createPolicyList(moderator);
47+
await mjolnir.watchList(unprotectedWatchedList.roomRef);
48+
await mjolnir.protectedRoomsTracker.syncLists();
49+
50+
// We expect that the watched list will not be protected, despite config.protectAllJoinedRooms being true
51+
// this is necessary so that it doesn't try change acl, ban users etc in someone else's list.
52+
assert.equal(mjolnir.protectedRoomsTracker.getProtectedRooms().includes(unprotectedWatchedList.roomId), false);
53+
const accountDataAfterListSetup = await getProtectedRoomsFromAccountData(mjolnir.client);
54+
assert.equal(accountDataAfterListSetup.includes(unprotectedWatchedList.roomId), false);
55+
// But our own list should be protected AND stored in account data
56+
assert.equal(accountDataAfterListSetup.length, 1);
57+
const policyListId = accountDataAfterListSetup[0];
58+
assert.equal(mjolnir.protectedRoomsTracker.getProtectedRooms().includes(policyListId), true);
59+
// Confirm that it is the right room, since we only get the shortcode back when using the command to create a list.
60+
const shortcodeInfo = await mjolnir.client.getRoomStateEvent(policyListId, "org.matrix.mjolnir.shortcode", "");
61+
assert.equal(shortcodeInfo.shortcode, policyListShortcode);
62+
})
63+
});
64+

0 commit comments

Comments
 (0)