2022-01-25 09:47:50 -05:00
|
|
|
import { strict as assert } from "assert";
|
|
|
|
|
2022-01-25 12:55:02 -05:00
|
|
|
import { Mjolnir } from "../../src/Mjolnir";
|
2023-01-05 02:36:35 -05:00
|
|
|
import { Protection } from "../../src/protections/IProtection";
|
2022-01-25 09:47:50 -05:00
|
|
|
import { ProtectionSettingValidationError } from "../../src/protections/ProtectionSettings";
|
|
|
|
import { NumberProtectionSetting, StringProtectionSetting, StringListProtectionSetting } from "../../src/protections/ProtectionSettings";
|
|
|
|
import { newTestUser, noticeListener } from "./clientHelper";
|
|
|
|
import { matrixClient, mjolnir } from "./mjolnirSetupUtils";
|
2024-07-29 12:48:36 -04:00
|
|
|
import {MessageIsMedia} from "../../src/protections/MessageIsMedia";
|
2022-01-25 09:47:50 -05:00
|
|
|
|
|
|
|
describe("Test: Protection settings", function() {
|
|
|
|
let client;
|
2024-07-29 12:48:36 -04:00
|
|
|
let room;
|
2022-01-25 09:47:50 -05:00
|
|
|
this.beforeEach(async function () {
|
2022-08-16 10:51:18 -04:00
|
|
|
client = await newTestUser(this.config.homeserverUrl, { name: { contains: "protection-settings" }});
|
2022-01-25 09:47:50 -05:00
|
|
|
await client.start();
|
2024-07-29 12:48:36 -04:00
|
|
|
room = await client.createRoom();
|
|
|
|
await client.joinRoom(room)
|
2022-01-25 09:47:50 -05:00
|
|
|
})
|
|
|
|
this.afterEach(async function () {
|
|
|
|
await client.stop();
|
|
|
|
})
|
|
|
|
it("Mjolnir refuses to save invalid protection setting values", async function() {
|
|
|
|
this.timeout(20000);
|
|
|
|
await assert.rejects(
|
2022-09-29 09:49:09 -04:00
|
|
|
async () => await this.mjolnir.protectionManager.setProtectionSettings("BasicFloodingProtection", {"maxPerMinute": "soup"}),
|
2022-01-25 09:47:50 -05:00
|
|
|
ProtectionSettingValidationError
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it("Mjolnir successfully saves valid protection setting values", async function() {
|
|
|
|
this.timeout(20000);
|
|
|
|
|
2023-01-05 02:36:35 -05:00
|
|
|
await this.mjolnir.protectionManager.registerProtection(new class extends Protection {
|
2022-02-02 10:31:11 -05:00
|
|
|
name = "05OVMS";
|
|
|
|
description = "A test protection";
|
|
|
|
settings = { test: new NumberProtectionSetting(3) };
|
|
|
|
});
|
2022-01-25 09:47:50 -05:00
|
|
|
|
2022-09-29 09:49:09 -04:00
|
|
|
await this.mjolnir.protectionManager.setProtectionSettings("05OVMS", { test: 123 });
|
2022-01-25 09:47:50 -05:00
|
|
|
assert.equal(
|
2022-09-29 09:49:09 -04:00
|
|
|
(await this.mjolnir.protectionManager.getProtectionSettings("05OVMS"))["test"],
|
2022-01-25 09:47:50 -05:00
|
|
|
123
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it("Mjolnir should accumulate changed settings", async function() {
|
|
|
|
this.timeout(20000);
|
|
|
|
|
2023-01-05 02:36:35 -05:00
|
|
|
await this.mjolnir.protectionManager.registerProtection(new class extends Protection {
|
2022-02-02 10:31:11 -05:00
|
|
|
name = "HPUjKN";
|
2023-01-05 02:36:35 -05:00
|
|
|
description = "A test protection";
|
2022-02-02 10:31:11 -05:00
|
|
|
settings = {
|
|
|
|
test1: new NumberProtectionSetting(3),
|
|
|
|
test2: new NumberProtectionSetting(4)
|
|
|
|
};
|
|
|
|
});
|
2022-01-25 09:47:50 -05:00
|
|
|
|
2022-09-29 09:49:09 -04:00
|
|
|
await this.mjolnir.protectionManager.setProtectionSettings("HPUjKN", { test1: 1 });
|
|
|
|
await this.mjolnir.protectionManager.setProtectionSettings("HPUjKN", { test2: 2 });
|
|
|
|
const settings = await this.mjolnir.protectionManager.getProtectionSettings("HPUjKN");
|
2022-01-25 09:47:50 -05:00
|
|
|
assert.equal(settings["test1"], 1);
|
|
|
|
assert.equal(settings["test2"], 2);
|
|
|
|
});
|
|
|
|
it("Mjolnir responds to !set correctly", async function() {
|
|
|
|
this.timeout(20000);
|
2022-08-16 10:51:18 -04:00
|
|
|
await client.joinRoom(this.config.managementRoom);
|
2022-01-25 09:47:50 -05:00
|
|
|
|
2023-01-05 02:36:35 -05:00
|
|
|
await this.mjolnir.protectionManager.registerProtection(new class extends Protection {
|
2022-02-02 10:31:11 -05:00
|
|
|
name = "JY2TPN";
|
|
|
|
description = "A test protection";
|
|
|
|
settings = { test: new StringProtectionSetting() };
|
|
|
|
});
|
2022-01-25 09:47:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
let reply = new Promise((resolve, reject) => {
|
|
|
|
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
|
|
|
if (event.content.body.includes("Changed JY2TPN.test ")) {
|
|
|
|
resolve(event);
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
await client.sendMessage(this.mjolnir.managementRoomId, {msgtype: "m.text", body: "!mjolnir config set JY2TPN.test asd"})
|
|
|
|
await reply
|
|
|
|
|
2022-09-29 09:49:09 -04:00
|
|
|
const settings = await this.mjolnir.protectionManager.getProtectionSettings("JY2TPN");
|
2022-01-25 09:47:50 -05:00
|
|
|
assert.equal(settings["test"], "asd");
|
|
|
|
});
|
|
|
|
it("Mjolnir adds a value to a list setting", async function() {
|
|
|
|
this.timeout(20000);
|
2022-08-16 10:51:18 -04:00
|
|
|
await client.joinRoom(this.config.managementRoom);
|
2022-01-25 09:47:50 -05:00
|
|
|
|
2023-01-05 02:36:35 -05:00
|
|
|
await this.mjolnir.protectionManager.registerProtection(new class extends Protection {
|
2022-02-02 10:31:11 -05:00
|
|
|
name = "r33XyT";
|
|
|
|
description = "A test protection";
|
|
|
|
settings = { test: new StringListProtectionSetting() };
|
|
|
|
});
|
2022-01-25 09:47:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
let reply = new Promise((resolve, reject) => {
|
|
|
|
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
|
|
|
if (event.content.body.includes("Changed r33XyT.test ")) {
|
|
|
|
resolve(event);
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
await client.sendMessage(this.mjolnir.managementRoomId, {msgtype: "m.text", body: "!mjolnir config add r33XyT.test asd"})
|
|
|
|
await reply
|
|
|
|
|
2022-09-29 09:49:09 -04:00
|
|
|
assert.deepEqual(await this.mjolnir.protectionManager.getProtectionSettings("r33XyT"), { "test": ["asd"] });
|
2022-01-25 09:47:50 -05:00
|
|
|
});
|
|
|
|
it("Mjolnir removes a value from a list setting", async function() {
|
|
|
|
this.timeout(20000);
|
2022-08-16 10:51:18 -04:00
|
|
|
await client.joinRoom(this.config.managementRoom);
|
2022-01-25 09:47:50 -05:00
|
|
|
|
2023-01-05 02:36:35 -05:00
|
|
|
await this.mjolnir.protectionManager.registerProtection(new class extends Protection {
|
2022-02-02 10:31:11 -05:00
|
|
|
name = "oXzT0E";
|
|
|
|
description = "A test protection";
|
|
|
|
settings = { test: new StringListProtectionSetting() };
|
|
|
|
});
|
2022-01-25 09:47:50 -05:00
|
|
|
|
2022-02-07 08:05:14 -05:00
|
|
|
let reply = () => new Promise((resolve, reject) => {
|
2022-01-25 09:47:50 -05:00
|
|
|
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
|
|
|
if (event.content.body.includes("Changed oXzT0E.test ")) {
|
2022-02-07 08:05:14 -05:00
|
|
|
resolve(event);
|
2022-01-25 09:47:50 -05:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
await client.sendMessage(this.mjolnir.managementRoomId, {msgtype: "m.text", body: "!mjolnir config add oXzT0E.test asd"})
|
2022-02-07 08:05:14 -05:00
|
|
|
await reply();
|
2022-01-25 09:47:50 -05:00
|
|
|
await client.sendMessage(this.mjolnir.managementRoomId, {msgtype: "m.text", body: "!mjolnir config remove oXzT0E.test asd"})
|
2022-02-07 08:05:14 -05:00
|
|
|
await reply();
|
2022-01-25 09:47:50 -05:00
|
|
|
|
2022-09-29 09:49:09 -04:00
|
|
|
assert.deepEqual(await this.mjolnir.protectionManager.getProtectionSettings("oXzT0E"), { "test": [] });
|
2022-01-25 09:47:50 -05:00
|
|
|
});
|
2022-02-02 10:31:11 -05:00
|
|
|
it("Mjolnir will change a protection setting in-place", async function() {
|
|
|
|
this.timeout(20000);
|
2022-08-16 10:51:18 -04:00
|
|
|
await client.joinRoom(this.config.managementRoom);
|
2022-02-02 10:31:11 -05:00
|
|
|
|
2023-01-05 02:36:35 -05:00
|
|
|
await this.mjolnir.protectionManager.registerProtection(new class extends Protection {
|
2022-02-02 10:31:11 -05:00
|
|
|
name = "d0sNrt";
|
|
|
|
description = "A test protection";
|
|
|
|
settings = { test: new StringProtectionSetting() };
|
|
|
|
});
|
|
|
|
|
2023-01-05 02:36:35 -05:00
|
|
|
let replyPromise: Promise<any> = new Promise((resolve, reject) => {
|
2022-02-02 10:31:11 -05:00
|
|
|
let i = 0;
|
|
|
|
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
|
|
|
if (event.content.body.includes("Changed d0sNrt.test ")) {
|
|
|
|
if (++i == 2) {
|
|
|
|
resolve(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
await client.sendMessage(this.mjolnir.managementRoomId, {msgtype: "m.text", body: "!mjolnir config set d0sNrt.test asd1"})
|
|
|
|
await client.sendMessage(this.mjolnir.managementRoomId, {msgtype: "m.text", body: "!mjolnir config set d0sNrt.test asd2"})
|
|
|
|
assert.equal(
|
|
|
|
(await replyPromise).content.body.split("\n", 3)[2],
|
|
|
|
"Changed d0sNrt.test to asd2 (was asd1)"
|
|
|
|
)
|
|
|
|
});
|
2024-07-29 12:48:36 -04:00
|
|
|
it("Events are checked for new content under media protections", async function() {
|
|
|
|
this.timeout(20000);
|
|
|
|
await client.joinRoom(this.config.managementRoom);
|
|
|
|
|
|
|
|
await this.mjolnir.protectionManager.registerProtection(new MessageIsMedia());
|
|
|
|
|
|
|
|
// send a regular media message to make sure protections are running
|
|
|
|
await client.sendMessage(room, {msgtype: "m.image", body: ""})
|
|
|
|
let reply = () => new Promise((resolve, reject) => {
|
|
|
|
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
|
|
|
if (event.content.body.includes("Redacting event")) {
|
|
|
|
resolve(event);
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
await reply;
|
|
|
|
|
|
|
|
await client.sendMessage(room, {body: "", msgtype: "m.text", "m.new_content": {msgtype: "m.image", body: ""}, "m.relates_to": {"rel_type": "m.replace"}})
|
|
|
|
let reply2 = () => new Promise((resolve, reject) => {
|
|
|
|
client.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => {
|
|
|
|
if (event.content.body.includes("Redacting event")) {
|
|
|
|
resolve(event);
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
await reply2;
|
|
|
|
});
|
2022-01-25 09:47:50 -05:00
|
|
|
});
|
|
|
|
|