Skip to content

Commit

Permalink
update test to not put member that is being banned in mod room and cl…
Browse files Browse the repository at this point in the history
…eanup test while at it
  • Loading branch information
H-Shay committed Oct 14, 2024
1 parent 1fb77c4 commit d5e7fc8
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions test/integration/standardConsequenceTest.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { strict as assert } from "assert";

import { Mjolnir } from "../../src/Mjolnir";
import { Protection } from "../../src/protections/IProtection";
import { newTestUser, noticeListener } from "./clientHelper";
import { matrixClient, mjolnir } from "./mjolnirSetupUtils";
import { newTestUser } from "./clientHelper";
import { ConsequenceBan, ConsequenceRedact } from "../../src/protections/consequence";
import { MatrixClient } from "@vector-im/matrix-bot-sdk";

describe("Test: standard consequences", function () {
let badUser;
let goodUser;
let badUser: MatrixClient;
let goodUser: MatrixClient;
this.beforeEach(async function () {
badUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "standard-consequences" } });
goodUser = await newTestUser(this.config.homeserverUrl, { name: { contains: "standard-consequences" } });
Expand All @@ -32,6 +30,7 @@ describe("Test: standard consequences", function () {
name = "JY2TPN";
description = "A test protection";
settings = {};
// @ts-ignore
handleEvent = async (mjolnir: Mjolnir, roomId: string, event: any) => {
if (event.content.body === "ngmWkF") {
return [new ConsequenceRedact("asd")];
Expand All @@ -43,7 +42,7 @@ describe("Test: standard consequences", function () {

let reply: Promise<any> = new Promise(async (resolve, reject) => {
const messageId = await badUser.sendMessage(protectedRoomId, { msgtype: "m.text", body: "ngmWkF" });
let redaction;
let redaction: any;
badUser.on("room.event", (roomId, event) => {
if (roomId === protectedRoomId && event?.type === "m.room.redaction" && event.redacts === messageId) {
redaction = event;
Expand All @@ -59,13 +58,12 @@ describe("Test: standard consequences", function () {
});
});

const [eventRedact, eventMessage] = await reply;
await reply;
});
it("Mjolnir applies a standard consequence ban", async function () {
this.timeout(20000);

let protectedRoomId = await this.mjolnir.client.createRoom({ invite: [await badUser.getUserId()] });
await badUser.joinRoom(this.mjolnir.managementRoomId);
await badUser.joinRoom(protectedRoomId);
await this.mjolnir.addProtectedRoom(protectedRoomId);

Expand All @@ -74,6 +72,7 @@ describe("Test: standard consequences", function () {
name = "0LxMTy";
description = "A test protection";
settings = {};
// @ts-ignore
handleEvent = async (mjolnir: Mjolnir, roomId: string, event: any) => {
if (event.content.body === "7Uga3d") {
return [new ConsequenceBan("asd")];
Expand All @@ -84,19 +83,20 @@ describe("Test: standard consequences", function () {
await this.mjolnir.protectionManager.enableProtection("0LxMTy");

let reply: Promise<any> = new Promise(async (resolve, reject) => {
const messageId = await badUser.sendMessage(protectedRoomId, { msgtype: "m.text", body: "7Uga3d" });
let ban;
await badUser.sendMessage(protectedRoomId, { msgtype: "m.text", body: "7Uga3d" });
let ban: any;
const badId = await badUser.getUserId();
badUser.on("room.leave", (roomId, event) => {
if (
roomId === protectedRoomId &&
event?.type === "m.room.member" &&
event.content?.membership === "ban" &&
event.state_key === badUser.userId
event.state_key === badId
) {
ban = event;
}
});
badUser.on("room.event", (roomId, event) => {
this.mjolnir.client.on("room.event", (roomId: any, event: any) => {
if (
roomId === this.mjolnir.managementRoomId &&
event?.type === "m.room.message" &&
Expand All @@ -108,13 +108,15 @@ describe("Test: standard consequences", function () {
});
});

const [eventBan, eventMessage] = await reply;
await reply;
});
it("Mjolnir doesn't ban a good user", async function () {
this.timeout(20000);
const badId = await badUser.getUserId();
const goodId = await goodUser.getUserId();

let protectedRoomId = await this.mjolnir.client.createRoom({
invite: [await goodUser.getUserId(), await badUser.getUserId()],
invite: [goodId, badId],
});
await badUser.joinRoom(protectedRoomId);
await goodUser.joinRoom(protectedRoomId);
Expand All @@ -125,6 +127,7 @@ describe("Test: standard consequences", function () {
name = "95B1Cr";
description = "A test protection";
settings = {};
// @ts-ignore
handleEvent = async (mjolnir: Mjolnir, roomId: string, event: any) => {
if (event.content.body === "8HUnwb") {
return [new ConsequenceBan("asd")];
Expand All @@ -135,21 +138,21 @@ describe("Test: standard consequences", function () {
await this.mjolnir.protectionManager.enableProtection("95B1Cr");

let reply = new Promise(async (resolve, reject) => {
this.mjolnir.client.on("room.message", async (roomId, event) => {
this.mjolnir.client.on("room.message", async (roomId: string, event: any) => {
if (event?.content?.body === "SUwvFT") {
await badUser.sendMessage(protectedRoomId, { msgtype: "m.text", body: "8HUnwb" });
}
});

this.mjolnir.client.on("room.event", (roomId, event) => {
this.mjolnir.client.on("room.event", (roomId: string, event: any) => {
if (
roomId === protectedRoomId &&
event?.type === "m.room.member" &&
event.content?.membership === "ban"
) {
if (event.state_key === goodUser.userId) {
if (event.state_key === goodId) {
reject("good user has been banned");
} else if (event.state_key === badUser.userId) {
} else if (event.state_key === badId) {
resolve(null);
}
}
Expand Down

0 comments on commit d5e7fc8

Please sign in to comment.