Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 356849c2 rédigé par Sorunome's avatar Sorunome
Parcourir les fichiers

add new tests

parent 95b652e4
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
/*
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 { DiscordBot } from "./bot";
import * as Discord from "discord.js"; import * as Discord from "discord.js";
import { Util, ICommandActions, ICommandParameters, CommandPermissonCheck } from "./util"; import { Util, ICommandActions, ICommandParameters, CommandPermissonCheck } from "./util";
......
/*
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 { DiscordBot } from "./bot";
import { Log } from "./log"; import { Log } from "./log";
import { DiscordBridgeConfig } from "./config"; import { DiscordBridgeConfig } from "./config";
......
...@@ -60,10 +60,11 @@ describe("Util", () => { ...@@ -60,10 +60,11 @@ describe("Util", () => {
Chai.assert.equal(args[1], "arg2"); Chai.assert.equal(args[1], "arg2");
}); });
}); });
describe("ParseCommand", () => { describe("Command Stuff", () => {
it("parses commands", async () => {
const actions: ICommandActions = { const actions: ICommandActions = {
action: { action: {
description: "floof",
help: "Fox goes floof!",
params: ["param1", "param2"], params: ["param1", "param2"],
run: async ({param1, param2}) => { run: async ({param1, param2}) => {
return `param1: ${param1}\nparam2: ${param2}`; return `param1: ${param1}\nparam2: ${param2}`;
...@@ -72,16 +73,51 @@ describe("Util", () => { ...@@ -72,16 +73,51 @@ describe("Util", () => {
}; };
const parameters: ICommandParameters = { const parameters: ICommandParameters = {
param1: { param1: {
description: "1",
get: async (param: string) => { get: async (param: string) => {
return "param1_" + param; return "param1_" + param;
}, },
}, },
param2: { param2: {
description: "2",
get: async (param: string) => { get: async (param: string) => {
return "param2_" + param; return "param2_" + param;
}, },
}, },
}; };
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( const retStr = await Util.ParseCommand(
"!fox", "!fox",
"!fox action hello world", "!fox action hello world",
...@@ -91,6 +127,7 @@ describe("Util", () => { ...@@ -91,6 +127,7 @@ describe("Util", () => {
expect(retStr).equal("param1: param1_hello\nparam2: param2_world"); expect(retStr).equal("param1: param1_hello\nparam2: param2_world");
}); });
}); });
});
describe("GetMxidFromName", () => { describe("GetMxidFromName", () => {
it("Finds a single member", async () => { it("Finds a single member", async () => {
const mockRooms = { const mockRooms = {
...@@ -194,4 +231,84 @@ describe("Util", () => { ...@@ -194,4 +231,84 @@ describe("Util", () => {
expect(Date.now()).to.be.greaterThan(t + DELAY_FOR - 1); 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;
});
});
}); });
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter