Skip to content

Commit 81cd91c

Browse files
author
David Teller
authored
Unbitrotting ruleserver tests (#418)
1 parent fb52e3d commit 81cd91c

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

test/integration/policyConsumptionTest.ts

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { strict as assert } from "assert";
22

33
import { newTestUser } from "./clientHelper";
44
import { Mjolnir } from "../../src/Mjolnir";
5-
import config from "../../src/config";
6-
import { getRequestFn, LogService, MatrixClient } from "matrix-bot-sdk";
5+
import { read as configRead } from "../../src/config";
6+
import { getRequestFn, LogService } from "matrix-bot-sdk";
77
import { createBanList, getFirstReaction } from "./commands/commandUtils";
88

99
/**
1010
* Get a copy of the rules from the ruleserver.
1111
*/
1212
async function currentRules(mjolnir: Mjolnir): Promise<{ start: object, stop: object, since: string }> {
1313
return await new Promise((resolve, reject) => getRequestFn()({
14-
uri: `http://${mjolnir.config.web.address}:${mjolnir.config.web.port}/api/1/ruleserver/updates/`,
15-
method: "GET"
16-
}, (error, response, body) => {
14+
uri: `http://${mjolnir.config.web.address}:${mjolnir.config.web.port}/api/1/ruleserver/updates/`,
15+
method: "GET"
16+
}, (error: object, _response: any, body: string) => {
1717
if (error) {
1818
reject(error)
1919
} else {
@@ -26,7 +26,7 @@ async function currentRules(mjolnir: Mjolnir): Promise<{ start: object, stop: ob
2626
* Wait for the rules to change as a result of the thunk. The returned promise will resolve when the rules being served have changed.
2727
* @param thunk Should cause the rules the RuleServer is serving to change some way.
2828
*/
29-
async function waitForRuleChange(mjolnir: Mjolnir, thunk): Promise<void> {
29+
async function waitForRuleChange(mjolnir: Mjolnir, thunk: any): Promise<void> {
3030
const initialRules = await currentRules(mjolnir);
3131
let rules = initialRules;
3232
// We use JSON.stringify like this so that it is pretty printed in the log and human readable.
@@ -48,45 +48,40 @@ async function waitForRuleChange(mjolnir: Mjolnir, thunk): Promise<void> {
4848

4949
describe("Test: that policy lists are consumed by the associated synapse module", function () {
5050
this.afterEach(async function () {
51-
if(this.config.web.ruleServer.enabled) {
51+
if (this.config.web.ruleServer.enabled) {
5252
this.timeout(5000)
5353
LogService.debug('policyConsumptionTest', `Rules at end of test ${JSON.stringify(await currentRules(this.mjolnir), null, 2)}`);
54-
const mjolnir = config.RUNTIME.client!;
5554
// Clear any state associated with the account.
56-
await mjolnir.setAccountData('org.matrix.mjolnir.watched_lists', {
55+
await this.mjolnir.client.setAccountData('org.matrix.mjolnir.watched_lists', {
5756
references: [],
5857
});
5958
}
6059
})
6160
this.beforeAll(async function() {
62-
if (!this.config.web.ruleServer.enabled) {
61+
let config = configRead();
62+
if (!config?.web?.ruleServer?.enabled) {
6363
LogService.warn("policyConsumptionTest", "Skipping policy consumption test because the ruleServer is not enabled")
6464
this.skip();
6565
}
6666
})
67-
this.beforeEach(async function () {
68-
this.timeout(1000);
69-
const mjolnir = this.config.RUNTIME.client!;
70-
})
7167
it('blocks users in antispam when they are banned from sending messages and invites serverwide.', async function() {
7268
this.timeout(20000);
7369
// Create a few users and a room.
7470
let badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "spammer" }});
7571
let badUserId = await badUser.getUserId();
76-
const mjolnir = config.RUNTIME.client!
77-
let mjolnirUserId = await mjolnir.getUserId();
72+
let mjolnirUserId = await this.mjolnir.client.getUserId();
7873
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" }});
7974
this.moderator = moderator;
8075
await moderator.joinRoom(this.mjolnir.managementRoomId);
8176
let unprotectedRoom = await badUser.createRoom({ invite: [await moderator.getUserId()]});
8277
// We do this so the moderator can send invites, no other reason.
8378
await badUser.setUserPowerLevel(await moderator.getUserId(), unprotectedRoom, 100);
8479
await moderator.joinRoom(unprotectedRoom);
85-
const banList = await createBanList(this.mjolnir.managementRoomId, mjolnir, moderator);
80+
const banList = await createBanList(this.mjolnir.managementRoomId, this.mjolnir.client, moderator);
8681
await badUser.sendMessage(unprotectedRoom, {msgtype: 'm.text', body: 'Something bad and mean'});
8782

88-
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => {
89-
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => {
83+
await waitForRuleChange(this.mjolnir, async () => {
84+
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => {
9085
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir ban ${banList} ${badUserId}` });
9186
});
9287
});
@@ -96,8 +91,8 @@ describe("Test: that policy lists are consumed by the associated synapse module"
9691
assert.ok(await moderator.sendMessage(unprotectedRoom, { msgtype: 'm.text', body: 'test'}), 'They should be able to send messages still too.');
9792

9893
// Test we can remove the rules.
99-
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => {
100-
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => {
94+
await waitForRuleChange(this.mjolnir, async () => {
95+
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => {
10196
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir unban ${banList} ${badUserId}` });
10297
});
10398
});
@@ -107,15 +102,14 @@ describe("Test: that policy lists are consumed by the associated synapse module"
107102
it('Test: Cannot send message to a room that is listed in a policy list and cannot invite a user to the room either', async function () {
108103
this.timeout(20000);
109104
let badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "spammer" }});
110-
const mjolnir = config.RUNTIME.client!
111105
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" }});
112106
await moderator.joinRoom(this.mjolnir.managementRoomId);
113-
const banList = await createBanList(this.mjolnir.managementRoomId, mjolnir, moderator);
107+
const banList = await createBanList(this.mjolnir.managementRoomId, this.mjolnir.client, moderator);
114108
let badRoom = await badUser.createRoom();
115109
let unrelatedRoom = await badUser.createRoom();
116110
await badUser.sendMessage(badRoom, {msgtype: 'm.text', body: "Very Bad Stuff in this room"});
117-
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => {
118-
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => {
111+
await waitForRuleChange(this.mjolnir, async () => {
112+
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => {
119113
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir ban ${banList} ${badRoom}` });
120114
});
121115
});
@@ -124,8 +118,8 @@ describe("Test: that policy lists are consumed by the associated synapse module"
124118
assert.ok(await badUser.sendMessage(unrelatedRoom, { msgtype: 'm.text.', body: 'hey'}), 'should be able to send messages to unrelated room');
125119
assert.ok(await badUser.inviteUser(await moderator.getUserId(), unrelatedRoom), 'They should still be able to invite to other rooms though');
126120
// Test we can remove these rules.
127-
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => {
128-
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => {
121+
await waitForRuleChange(this.mjolnir, async () => {
122+
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => {
129123
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir unban ${banList} ${badRoom}` });
130124
});
131125
});
@@ -135,21 +129,20 @@ describe("Test: that policy lists are consumed by the associated synapse module"
135129
})
136130
it('Test: When a list becomes unwatched, the associated policies are stopped.', async function () {
137131
this.timeout(20000);
138-
const mjolnir = config.RUNTIME.client!
139132
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" }});
140133
await moderator.joinRoom(this.mjolnir.managementRoomId);
141-
const banList = await createBanList(this.mjolnir.managementRoomId, mjolnir, moderator);
134+
const banList = await createBanList(this.mjolnir.managementRoomId, this.mjolnir.client, moderator);
142135
let targetRoom = await moderator.createRoom();
143136
await moderator.sendMessage(targetRoom, {msgtype: 'm.text', body: "Fluffy Foxes."});
144-
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => {
145-
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => {
137+
await waitForRuleChange(this.mjolnir, async () => {
138+
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => {
146139
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir ban ${banList} ${targetRoom}` });
147140
});
148141
});
149142
await assert.rejects(moderator.sendMessage(targetRoom, { msgtype: 'm.text', body: 'test'}), 'should not be able to send messages to a room which is listed.');
150143

151-
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => {
152-
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => {
144+
await waitForRuleChange(this.mjolnir, async () => {
145+
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => {
153146
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir unwatch #${banList}:localhost:9999` });
154147
});
155148
});

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"./test/integration/manualLaunchScript.ts",
2424
"./test/integration/roomMembersTest.ts",
2525
"./test/integration/banListTest.ts",
26-
"./test/integration/reportPollingTest"
26+
"./test/integration/reportPollingTest",
27+
"./test/integration/policyConsumptionTest.ts",
2728
]
2829
}

0 commit comments

Comments
 (0)