From 356849c265243cd664eac9a27922ca296ec44cb3 Mon Sep 17 00:00:00 2001 From: Sorunome <mail@sorunome.de> Date: Sat, 11 May 2019 11:08:45 +0200 Subject: [PATCH] add new tests --- src/discordcommandhandler.ts | 16 ++++ src/matrixcommandhandler.ts | 16 ++++ test/test_util.ts | 169 +++++++++++++++++++++++++++++------ 3 files changed, 175 insertions(+), 26 deletions(-) diff --git a/src/discordcommandhandler.ts b/src/discordcommandhandler.ts index efcb53f..042ec66 100644 --- a/src/discordcommandhandler.ts +++ b/src/discordcommandhandler.ts @@ -1,3 +1,19 @@ +/* +Copyright 2019 matrix-appservice-discord + +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. +*/ + import { DiscordBot } from "./bot"; import * as Discord from "discord.js"; import { Util, ICommandActions, ICommandParameters, CommandPermissonCheck } from "./util"; diff --git a/src/matrixcommandhandler.ts b/src/matrixcommandhandler.ts index b2bbcf0..0660d33 100644 --- a/src/matrixcommandhandler.ts +++ b/src/matrixcommandhandler.ts @@ -1,3 +1,19 @@ +/* +Copyright 2019 matrix-appservice-discord + +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. +*/ + import { DiscordBot } from "./bot"; import { Log } from "./log"; import { DiscordBridgeConfig } from "./config"; diff --git a/test/test_util.ts b/test/test_util.ts index 5f294ef..ed109f1 100644 --- a/test/test_util.ts +++ b/test/test_util.ts @@ -60,35 +60,72 @@ describe("Util", () => { Chai.assert.equal(args[1], "arg2"); }); }); - describe("ParseCommand", () => { - it("parses commands", async () => { - const actions: ICommandActions = { - action: { - params: ["param1", "param2"], - run: async ({param1, param2}) => { - return `param1: ${param1}\nparam2: ${param2}`; - }, + describe("Command Stuff", () => { + const actions: ICommandActions = { + action: { + description: "floof", + help: "Fox goes floof!", + params: ["param1", "param2"], + run: async ({param1, param2}) => { + return `param1: ${param1}\nparam2: ${param2}`; }, - }; - const parameters: ICommandParameters = { - param1: { - get: async (param: string) => { - return "param1_" + param; - }, + }, + }; + const parameters: ICommandParameters = { + param1: { + description: "1", + get: async (param: string) => { + return "param1_" + param; }, - param2: { - get: async (param: string) => { - return "param2_" + param; - }, + }, + param2: { + description: "2", + get: async (param: string) => { + return "param2_" + param; }, - }; - const retStr = await Util.ParseCommand( - "!fox", - "!fox action hello world", - actions, - parameters, - ); - expect(retStr).equal("param1: param1_hello\nparam2: param2_world"); + }, + }; + describe("ParseHelpMessage", () => { + it("parses general help message", async () => { + const {command, args} = Util.MsgToArgs("!fox help", "!fox"); + const retStr = await Util.ParseHelpMessage( + "!fox", + actions, + parameters, + args, + ); + expect(retStr).to.equal( +`Available Commands: + - \`!fox action <param1> <param2>\`: floof + +Parameters: + - \`<param1>\`: 1 + - \`<param2>\`: 2 +`); + }); + it("parses specific help message", async () => { + const {command, args} = Util.MsgToArgs("!fox help action", "!fox"); + const retStr = await Util.ParseHelpMessage( + "!fox", + actions, + parameters, + args, + ); + expect(retStr).to.equal( +`\`!fox action <param1> <param2>\`: floof +Fox goes floof!`); + }); + }); + describe("ParseCommand", () => { + it("parses commands", async () => { + const retStr = await Util.ParseCommand( + "!fox", + "!fox action hello world", + actions, + parameters, + ); + expect(retStr).equal("param1: param1_hello\nparam2: param2_world"); + }); }); }); describe("GetMxidFromName", () => { @@ -194,4 +231,84 @@ describe("Util", () => { expect(Date.now()).to.be.greaterThan(t + DELAY_FOR - 1); }); }); + describe("CheckMatrixPermission", () => { + const PERM_LEVEL = 50; + it("should deny", async () => { + const ret = await Util.CheckMatrixPermission( + { + getStateEvent: async () => { + return { + blah: { + blubb: PERM_LEVEL, + }, + }; + }, + } as any, + "@user:localhost", + "", + PERM_LEVEL, + "blah", + "blubb", + ); + expect(ret).to.be.false; + }); + it("should allow cat/subcat", async () => { + const ret = await Util.CheckMatrixPermission( + { + getStateEvent: async () => { + return { + blah: { + blubb: PERM_LEVEL, + }, + users: { + "@user:localhost": PERM_LEVEL, + }, + }; + }, + } as any, + "@user:localhost", + "", + PERM_LEVEL, + "blah", + "blubb", + ); + expect(ret).to.be.true; + }); + it("should allow cat", async () => { + const ret = await Util.CheckMatrixPermission( + { + getStateEvent: async () => { + return { + blah: PERM_LEVEL, + users: { + "@user:localhost": PERM_LEVEL, + }, + }; + }, + } as any, + "@user:localhost", + "", + PERM_LEVEL, + "blah", + ); + expect(ret).to.be.true; + }); + it("should allow based on default", async () => { + const ret = await Util.CheckMatrixPermission( + { + getStateEvent: async () => { + return { + blah: PERM_LEVEL, + users_default: PERM_LEVEL, + }; + }, + } as any, + "@user:localhost", + "", + PERM_LEVEL, + "blah", + ); + expect(ret).to.be.true; + }); + }); }); -- GitLab