2019-11-13 23:38:19 -05:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-11-15 13:03:06 -05:00
|
|
|
import expect from "expect";
|
2019-11-13 23:38:19 -05:00
|
|
|
import { Mjolnir } from "../../src/Mjolnir";
|
|
|
|
import { DEFAULT_LIST_EVENT_TYPE } from "../../src/commands/SetDefaultBanListCommand";
|
|
|
|
import { parseArguments } from "../../src/commands/UnbanBanCommand";
|
2022-08-16 10:51:18 -04:00
|
|
|
import { read as configRead } from "../../src/config";
|
2022-07-26 15:47:26 -04:00
|
|
|
import { RULE_ROOM, RULE_SERVER, RULE_USER } from "../../src/models/ListRule";
|
2019-11-13 23:38:19 -05:00
|
|
|
|
2022-07-26 15:47:26 -04:00
|
|
|
function createTestMjolnir(defaultShortcode: string|null = null): Mjolnir {
|
2022-08-16 10:51:18 -04:00
|
|
|
const config = configRead();
|
2019-11-13 23:38:19 -05:00
|
|
|
const client = {
|
2022-07-26 15:47:26 -04:00
|
|
|
// Mock `MatrixClient.getAccountData` .
|
2019-11-13 23:38:19 -05:00
|
|
|
getAccountData: (eventType: string): Promise<any> => {
|
2022-07-26 15:47:26 -04:00
|
|
|
if (eventType === DEFAULT_LIST_EVENT_TYPE || defaultShortcode) {
|
2019-11-13 23:38:19 -05:00
|
|
|
return Promise.resolve({shortcode: defaultShortcode});
|
|
|
|
}
|
2022-07-26 15:47:26 -04:00
|
|
|
throw new Error(`Unknown event type ${eventType}, expected ${DEFAULT_LIST_EVENT_TYPE}`);
|
2019-11-13 23:38:19 -05:00
|
|
|
},
|
|
|
|
};
|
2022-08-09 06:29:27 -04:00
|
|
|
return <Mjolnir>{
|
|
|
|
client,
|
|
|
|
config,
|
|
|
|
};
|
2019-11-13 23:38:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function createFakeEvent(command: string): any {
|
|
|
|
return {
|
|
|
|
sender: "@alice:example.org",
|
|
|
|
event_id: "$example",
|
|
|
|
content: {
|
|
|
|
body: command,
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("UnbanBanCommand", () => {
|
|
|
|
describe("parseArguments", () => {
|
|
|
|
it("should be able to detect servers", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_SERVER);
|
|
|
|
expect(bits!.entity).toBe("example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:46:20 -05:00
|
|
|
it("should be able to detect servers with ban reasons", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test example.org reason here";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBe("reason here");
|
|
|
|
expect(bits!.ruleType).toBe(RULE_SERVER);
|
|
|
|
expect(bits!.entity).toBe("example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:46:20 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:38:19 -05:00
|
|
|
it("should be able to detect servers with globs", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
const command = "!mjolnir ban test *.example.org --force";
|
2019-11-13 23:38:19 -05:00
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_SERVER);
|
|
|
|
expect(bits!.entity).toBe("*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be able to detect servers with the type specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
const command = "!mjolnir ban test server @*.example.org --force";
|
2019-11-13 23:38:19 -05:00
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_SERVER);
|
|
|
|
expect(bits!.entity).toBe("@*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be able to detect room IDs", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test !example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_ROOM);
|
|
|
|
expect(bits!.entity).toBe("!example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:46:20 -05:00
|
|
|
it("should be able to detect room IDs with ban reasons", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test !example.org reason here";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBe("reason here");
|
|
|
|
expect(bits!.ruleType).toBe(RULE_ROOM);
|
|
|
|
expect(bits!.entity).toBe("!example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:46:20 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:38:19 -05:00
|
|
|
it("should be able to detect room IDs with globs", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
const command = "!mjolnir ban test !*.example.org --force";
|
2019-11-13 23:38:19 -05:00
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_ROOM);
|
|
|
|
expect(bits!.entity).toBe("!*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be able to detect room aliases", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test #example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_ROOM);
|
|
|
|
expect(bits!.entity).toBe("#example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:46:20 -05:00
|
|
|
it("should be able to detect room aliases with ban reasons", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test #example.org reason here";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBe("reason here");
|
|
|
|
expect(bits!.ruleType).toBe(RULE_ROOM);
|
|
|
|
expect(bits!.entity).toBe("#example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:46:20 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:38:19 -05:00
|
|
|
it("should be able to detect room aliases with globs", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
const command = "!mjolnir ban test #*.example.org --force";
|
2019-11-13 23:38:19 -05:00
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_ROOM);
|
|
|
|
expect(bits!.entity).toBe("#*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be able to detect rooms with the type specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
const command = "!mjolnir ban test room @*.example.org --force";
|
2019-11-13 23:38:19 -05:00
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_ROOM);
|
|
|
|
expect(bits!.entity).toBe("@*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be able to detect user IDs", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test @example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:46:20 -05:00
|
|
|
it("should be able to detect user IDs with ban reasons", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test @example.org reason here";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBe("reason here");
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:46:20 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:38:19 -05:00
|
|
|
it("should be able to detect user IDs with globs", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
const command = "!mjolnir ban test @*.example.org --force";
|
2019-11-13 23:38:19 -05:00
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be able to detect user IDs with the type specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
const command = "!mjolnir ban test user #*.example.org --force";
|
2019-11-13 23:38:19 -05:00
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("#*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
2021-02-05 11:59:15 -05:00
|
|
|
it("should error if wildcards used without --force", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
expect(content).toBeDefined();
|
|
|
|
expect(content['body']).toContain("Wildcard bans require an additional `--force` argument to confirm");
|
|
|
|
return Promise.resolve("$fake");
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test *.example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have correct ban reason with --force after", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test user #*.example.org reason here --force";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBe("reason here");
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("#*.example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2021-02-05 11:59:15 -05:00
|
|
|
});
|
|
|
|
|
2019-11-13 23:38:19 -05:00
|
|
|
describe("[without default list]", () => {
|
|
|
|
it("should error if no list (with type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
expect(content).toBeDefined();
|
|
|
|
expect(content['body']).toContain("No ban list matching that shortcode was found");
|
|
|
|
return Promise.resolve("$fake");
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban user @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should error if no list (without type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
expect(content).toBeDefined();
|
|
|
|
expect(content['body']).toContain("No ban list matching that shortcode was found");
|
|
|
|
return Promise.resolve("$fake");
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not error if a list (with type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban user test @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not error if a list (without type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not error if a list (with type reversed) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir();
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban test user @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("[with default list]", () => {
|
|
|
|
it("should use the default list if no list (with type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir("test");
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}, {listShortcode: "other"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban user @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should use the default list if no list (without type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir("test");
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}, {listShortcode: "other"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("test");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should use the specified list if a list (with type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir("test");
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}, {listShortcode: "other"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban user other @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("other");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should use the specified list if a list (without type) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir("test");
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}, {listShortcode: "other"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban other @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("other");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not error if a list (with type reversed) is specified", async () => {
|
|
|
|
const mjolnir = createTestMjolnir("test");
|
|
|
|
(<any>mjolnir).lists = [{listShortcode: "test"}, {listShortcode: "other"}];
|
|
|
|
mjolnir.client.sendMessage = (roomId: string, content: any): Promise<string> => {
|
|
|
|
throw new Error("sendMessage should not have been called: " + JSON.stringify(content));
|
|
|
|
};
|
|
|
|
|
|
|
|
const command = "!mjolnir ban other user @example:example.org";
|
|
|
|
const bits = await parseArguments("!a", createFakeEvent(command), mjolnir, command.split(' '));
|
|
|
|
expect(bits).toBeTruthy();
|
2022-07-26 15:47:26 -04:00
|
|
|
expect(bits!.reason).toBeFalsy();
|
|
|
|
expect(bits!.ruleType).toBe(RULE_USER);
|
|
|
|
expect(bits!.entity).toBe("@example:example.org");
|
|
|
|
expect(bits!.list).toBeDefined();
|
|
|
|
expect(bits!.list!.listShortcode).toBe("other");
|
2019-11-13 23:38:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|