-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unbitrotting ruleserver tests #418
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,18 @@ import { strict as assert } from "assert"; | |
|
||
import { newTestUser } from "./clientHelper"; | ||
import { Mjolnir } from "../../src/Mjolnir"; | ||
import config from "../../src/config"; | ||
import { getRequestFn, LogService, MatrixClient } from "matrix-bot-sdk"; | ||
import { read as configRead } from "../../src/config"; | ||
import { getRequestFn, LogService } from "matrix-bot-sdk"; | ||
import { createBanList, getFirstReaction } from "./commands/commandUtils"; | ||
|
||
/** | ||
* Get a copy of the rules from the ruleserver. | ||
*/ | ||
async function currentRules(mjolnir: Mjolnir): Promise<{ start: object, stop: object, since: string }> { | ||
return await new Promise((resolve, reject) => getRequestFn()({ | ||
uri: `http://${mjolnir.config.web.address}:${mjolnir.config.web.port}/api/1/ruleserver/updates/`, | ||
method: "GET" | ||
}, (error, response, body) => { | ||
uri: `http://${mjolnir.config.web.address}:${mjolnir.config.web.port}/api/1/ruleserver/updates/`, | ||
method: "GET" | ||
}, (error: object, _response: any, body: string) => { | ||
if (error) { | ||
reject(error) | ||
} else { | ||
|
@@ -26,7 +26,7 @@ async function currentRules(mjolnir: Mjolnir): Promise<{ start: object, stop: ob | |
* Wait for the rules to change as a result of the thunk. The returned promise will resolve when the rules being served have changed. | ||
* @param thunk Should cause the rules the RuleServer is serving to change some way. | ||
*/ | ||
async function waitForRuleChange(mjolnir: Mjolnir, thunk): Promise<void> { | ||
async function waitForRuleChange(mjolnir: Mjolnir, thunk: any): Promise<void> { | ||
const initialRules = await currentRules(mjolnir); | ||
let rules = initialRules; | ||
// 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> { | |
|
||
describe("Test: that policy lists are consumed by the associated synapse module", function () { | ||
this.afterEach(async function () { | ||
if(this.config.web.ruleServer.enabled) { | ||
if (this.config.web.ruleServer.enabled) { | ||
this.timeout(5000) | ||
LogService.debug('policyConsumptionTest', `Rules at end of test ${JSON.stringify(await currentRules(this.mjolnir), null, 2)}`); | ||
const mjolnir = config.RUNTIME.client!; | ||
// Clear any state associated with the account. | ||
await mjolnir.setAccountData('org.matrix.mjolnir.watched_lists', { | ||
await this.mjolnir.client.setAccountData('org.matrix.mjolnir.watched_lists', { | ||
references: [], | ||
}); | ||
} | ||
}) | ||
this.beforeAll(async function() { | ||
if (!this.config.web.ruleServer.enabled) { | ||
let config = configRead(); | ||
if (!config?.web?.ruleServer?.enabled) { | ||
LogService.warn("policyConsumptionTest", "Skipping policy consumption test because the ruleServer is not enabled") | ||
this.skip(); | ||
} | ||
}) | ||
this.beforeEach(async function () { | ||
this.timeout(1000); | ||
const mjolnir = this.config.RUNTIME.client!; | ||
}) | ||
it('blocks users in antispam when they are banned from sending messages and invites serverwide.', async function() { | ||
this.timeout(20000); | ||
// Create a few users and a room. | ||
let badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "spammer" }}); | ||
let badUserId = await badUser.getUserId(); | ||
const mjolnir = config.RUNTIME.client! | ||
let mjolnirUserId = await mjolnir.getUserId(); | ||
let mjolnirUserId = await this.mjolnir.client.getUserId(); | ||
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" }}); | ||
this.moderator = moderator; | ||
await moderator.joinRoom(this.mjolnir.managementRoomId); | ||
let unprotectedRoom = await badUser.createRoom({ invite: [await moderator.getUserId()]}); | ||
// We do this so the moderator can send invites, no other reason. | ||
await badUser.setUserPowerLevel(await moderator.getUserId(), unprotectedRoom, 100); | ||
await moderator.joinRoom(unprotectedRoom); | ||
const banList = await createBanList(this.mjolnir.managementRoomId, mjolnir, moderator); | ||
const banList = await createBanList(this.mjolnir.managementRoomId, this.mjolnir.client, moderator); | ||
await badUser.sendMessage(unprotectedRoom, {msgtype: 'm.text', body: 'Something bad and mean'}); | ||
|
||
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you have forgot to add the change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out that |
||
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { | ||
await waitForRuleChange(this.mjolnir, async () => { | ||
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir ban ${banList} ${badUserId}` }); | ||
}); | ||
}); | ||
|
@@ -96,8 +91,8 @@ describe("Test: that policy lists are consumed by the associated synapse module" | |
assert.ok(await moderator.sendMessage(unprotectedRoom, { msgtype: 'm.text', body: 'test'}), 'They should be able to send messages still too.'); | ||
|
||
// Test we can remove the rules. | ||
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => { | ||
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { | ||
await waitForRuleChange(this.mjolnir, async () => { | ||
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir unban ${banList} ${badUserId}` }); | ||
}); | ||
}); | ||
|
@@ -107,15 +102,14 @@ describe("Test: that policy lists are consumed by the associated synapse module" | |
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 () { | ||
this.timeout(20000); | ||
let badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "spammer" }}); | ||
const mjolnir = config.RUNTIME.client! | ||
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" }}); | ||
await moderator.joinRoom(this.mjolnir.managementRoomId); | ||
const banList = await createBanList(this.mjolnir.managementRoomId, mjolnir, moderator); | ||
const banList = await createBanList(this.mjolnir.managementRoomId, this.mjolnir.client, moderator); | ||
let badRoom = await badUser.createRoom(); | ||
let unrelatedRoom = await badUser.createRoom(); | ||
await badUser.sendMessage(badRoom, {msgtype: 'm.text', body: "Very Bad Stuff in this room"}); | ||
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => { | ||
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { | ||
await waitForRuleChange(this.mjolnir, async () => { | ||
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir ban ${banList} ${badRoom}` }); | ||
}); | ||
}); | ||
|
@@ -124,8 +118,8 @@ describe("Test: that policy lists are consumed by the associated synapse module" | |
assert.ok(await badUser.sendMessage(unrelatedRoom, { msgtype: 'm.text.', body: 'hey'}), 'should be able to send messages to unrelated room'); | ||
assert.ok(await badUser.inviteUser(await moderator.getUserId(), unrelatedRoom), 'They should still be able to invite to other rooms though'); | ||
// Test we can remove these rules. | ||
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => { | ||
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { | ||
await waitForRuleChange(this.mjolnir, async () => { | ||
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir unban ${banList} ${badRoom}` }); | ||
}); | ||
}); | ||
|
@@ -135,21 +129,20 @@ describe("Test: that policy lists are consumed by the associated synapse module" | |
}) | ||
it('Test: When a list becomes unwatched, the associated policies are stopped.', async function () { | ||
this.timeout(20000); | ||
const mjolnir = config.RUNTIME.client! | ||
let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" }}); | ||
await moderator.joinRoom(this.mjolnir.managementRoomId); | ||
const banList = await createBanList(this.mjolnir.managementRoomId, mjolnir, moderator); | ||
const banList = await createBanList(this.mjolnir.managementRoomId, this.mjolnir.client, moderator); | ||
let targetRoom = await moderator.createRoom(); | ||
await moderator.sendMessage(targetRoom, {msgtype: 'm.text', body: "Fluffy Foxes."}); | ||
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => { | ||
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { | ||
await waitForRuleChange(this.mjolnir, async () => { | ||
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir ban ${banList} ${targetRoom}` }); | ||
}); | ||
}); | ||
await assert.rejects(moderator.sendMessage(targetRoom, { msgtype: 'm.text', body: 'test'}), 'should not be able to send messages to a room which is listed.'); | ||
|
||
await waitForRuleChange(this.config.web.address, this.mjolnir.config.web.port, async () => { | ||
await getFirstReaction(mjolnir, this.mjolnir.managementRoomId, '✅', async () => { | ||
await waitForRuleChange(this.mjolnir, async () => { | ||
await getFirstReaction(this.mjolnir.client, this.mjolnir.managementRoomId, '✅', async () => { | ||
return await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir unwatch #${banList}:localhost:9999` }); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that
this.mjolnir
will exist in the beforeAll hook, but this works tooThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my experience, it didn't seem to exist.