Skip to content
Extraits de code Groupes Projets
Valider 7db1ea66 rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

Add essential tests for provisoner

parent 36cecf6c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -40,8 +40,8 @@ export class Provisioner { ...@@ -40,8 +40,8 @@ export class Provisioner {
let responded = false; let responded = false;
let resolve: (msg: string) => void; let resolve: (msg: string) => void;
let reject: (err: string) => void; let reject: (err: Error) => void;
const deferP: Promise<string> = new Promise((res, rej) => {resolve = res; reject = rej;}); const deferP: Promise<string> = new Promise((res, rej) => {resolve = res; reject = rej; });
const approveFn = (approved: boolean, expired = false) => { const approveFn = (approved: boolean, expired = false) => {
if (responded) { if (responded) {
...@@ -54,9 +54,9 @@ export class Provisioner { ...@@ -54,9 +54,9 @@ export class Provisioner {
resolve("Approved"); resolve("Approved");
} else { } else {
if (expired) { if (expired) {
reject("Timed out waiting for a response from the Discord owners"); reject(Error("Timed out waiting for a response from the Discord owners"));
} else { } else {
reject("The bridge has been declined by the Discord guild"); reject(Error("The bridge has been declined by the Discord guild"));
} }
} }
}; };
......
import {MockMember} from "./member"; import {MockMember} from "./member";
import {MockCollection} from "./collection"; import {MockCollection} from "./collection";
import {Permissions, PermissionResolvable} from "discord.js";
// we are a test file and thus need those // we are a test file and thus need those
/* tslint:disable:no-unused-expression max-file-line-count no-any */ /* tslint:disable:no-unused-expression max-file-line-count no-any */
...@@ -14,7 +15,12 @@ export class MockChannel { ...@@ -14,7 +15,12 @@ export class MockChannel {
public name: string = "", public name: string = "",
public topic: string = "", public topic: string = "",
) { } ) { }
public async send(data: any): Promise<any> { public async send(data: any): Promise<any> {
return data; return data;
} }
public permissionsFor(member: MockMember) {
return new Permissions(Permissions.FLAGS.MANAGE_WEBHOOKS as PermissionResolvable);
}
} }
import * as Chai from "chai";
import * as Discord from "discord.js";
import * as Proxyquire from "proxyquire";
import { Provisioner } from "../src/provisioner";
import { MockChannel } from "./mocks/channel";
import { MockMember } from "./mocks/member";
// we are a test file and thus need those
/* tslint:disable:no-any */
const expect = Chai.expect;
const INTERVAL = 250;
let lastStatus = null;
// const assert = Chai.assert;
const bot = {
GetBotId: () => {
return "1234";
},
GetIntentFromDiscordMember: (member) => {
return {
getClient: () => {
return {
setPresence: async (status) => {
lastStatus = status;
},
};
},
};
},
};
const TIMEOUT_MS = 1000;
describe("Provisioner", () => {
describe("AskBridgePermission", () => {
it("should fail to bridge a room that timed out", async () => {
const p = new Provisioner();
const startAt = Date.now();
await p.AskBridgePermission(
new MockChannel("foo", "bar") as any,
"Mark",
TIMEOUT_MS,
).then(() => {
throw Error("Should have thrown an error");
}).catch((err) => {
expect(err.message).to.eq("Timed out waiting for a response from the Discord owners");
const delay = Date.now() - startAt;
if (delay < TIMEOUT_MS) {
throw Error(`Should have waited for timeout before resolving, waited: ${delay}ms`);
}
});
});
it("should fail to bridge a room that was declined", async () => {
const p = new Provisioner();
const promise = p.AskBridgePermission(
new MockChannel("foo", "bar") as any,
"Mark",
TIMEOUT_MS,
).then(() => {
throw Error("Should have thrown an error");
}).catch((err) => {
expect(err.message).to.eq("The bridge has been declined by the Discord guild");
});
await p.MarkApproved(new MockChannel("foo", "bar") as any, new MockMember("abc", "Mark") as any, false);
await promise;
});
it("should bridge a room that was approved", async () => {
const p = new Provisioner();
const promise = p.AskBridgePermission(
new MockChannel("foo", "bar") as any,
"Mark",
TIMEOUT_MS,
).then((msg) => {
expect(msg).to.eq("Approved");
});
await p.MarkApproved(new MockChannel("foo", "bar") as any, new MockMember("abc", "Mark") as any, true);
await promise;
});
});
});
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter