Test for ban reasons as well in autodetection

This commit is contained in:
Travis Ralston 2019-11-13 21:46:20 -07:00
parent 66a5775136
commit 417673aeef
2 changed files with 79 additions and 6 deletions

View File

@ -70,14 +70,19 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni
if (entity) break;
}
if (!list) {
list = mjolnir.lists.find(b => b.listShortcode.toLowerCase() === defaultShortcode);
if (!entity) {
// It'll be a server at this point - figure out which positional argument is the server
// name and where the reason starts.
let serverIndex = 2;
if (ruleType) serverIndex++;
if (list) serverIndex++;
entity = parts[serverIndex];
if (!ruleType) ruleType = RULE_SERVER;
argumentIndex = serverIndex + 1;
}
if (!entity) {
entity = parts[argumentIndex - 1];
if (!ruleType) ruleType = RULE_SERVER; // due to the conditions above, it can't be anything else
console.log(parts);
if (!list) {
list = mjolnir.lists.find(b => b.listShortcode.toLowerCase() === defaultShortcode);
}
let replyMessage = null;

View File

@ -62,6 +62,23 @@ describe("UnbanBanCommand", () => {
expect(bits.list.listShortcode).toBe("test");
});
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();
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");
});
it("should be able to detect servers with globs", async () => {
const mjolnir = createTestMjolnir();
(<any>mjolnir).lists = [{listShortcode: "test"}];
@ -113,6 +130,23 @@ describe("UnbanBanCommand", () => {
expect(bits.list.listShortcode).toBe("test");
});
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();
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");
});
it("should be able to detect room IDs with globs", async () => {
const mjolnir = createTestMjolnir();
(<any>mjolnir).lists = [{listShortcode: "test"}];
@ -147,6 +181,23 @@ describe("UnbanBanCommand", () => {
expect(bits.list.listShortcode).toBe("test");
});
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();
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");
});
it("should be able to detect room aliases with globs", async () => {
const mjolnir = createTestMjolnir();
(<any>mjolnir).lists = [{listShortcode: "test"}];
@ -198,6 +249,23 @@ describe("UnbanBanCommand", () => {
expect(bits.list.listShortcode).toBe("test");
});
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();
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");
});
it("should be able to detect user IDs with globs", async () => {
const mjolnir = createTestMjolnir();
(<any>mjolnir).lists = [{listShortcode: "test"}];