From ca661511af0e538a5b87ba68b274029d5a172a1c Mon Sep 17 00:00:00 2001
From: Will Hunt <will@half-shot.uk>
Date: Sat, 18 May 2019 02:09:12 +0100
Subject: [PATCH] testing fixes

---
 src/discordcommandhandler.ts        |  2 +-
 src/discordmessageprocessor.ts      |  1 +
 src/matrixmessageprocessor.ts       |  3 +-
 src/matrixroomhandler.ts            |  2 +-
 test/mocks/appservicemock.ts        | 28 ++++++++-------
 test/test_clientfactory.ts          |  1 -
 test/test_discordcommandhandler.ts  | 53 +++++++++--------------------
 test/test_matrixcommandhandler.ts   | 27 ++-------------
 test/test_matrixeventprocessor.ts   |  6 ----
 test/test_matrixmessageprocessor.ts |  2 --
 10 files changed, 39 insertions(+), 86 deletions(-)

diff --git a/src/discordcommandhandler.ts b/src/discordcommandhandler.ts
index ec89e53..7606383 100644
--- a/src/discordcommandhandler.ts
+++ b/src/discordcommandhandler.ts
@@ -126,7 +126,7 @@ export class DiscordCommandHandler {
             let errorMsg = "";
             await Promise.all(allChannelMxids.map(async (chanMxid) => {
                 try {
-                    await this.bridge.botIntent[funcKey](chanMxid, name);
+                    await this.bridge.botIntent.underlyingClient[funcKey + "User"](chanMxid, name);
                 } catch (e) {
                     // maybe we don't have permission to kick/ban/unban...?
                     errorMsg += `\nCouldn't ${funcKey} ${name} from ${chanMxid}`;
diff --git a/src/discordmessageprocessor.ts b/src/discordmessageprocessor.ts
index 3903202..29849fc 100644
--- a/src/discordmessageprocessor.ts
+++ b/src/discordmessageprocessor.ts
@@ -79,6 +79,7 @@ export class DiscordMessageProcessor {
             discordCallback: this.getDiscordParseCallbacksHTML(msg),
         });
 
+
         // parse the plain text stuff
         content = markdown.toHTML(content, {
             discordCallback: this.getDiscordParseCallbacks(msg),
diff --git a/src/matrixmessageprocessor.ts b/src/matrixmessageprocessor.ts
index dcc3603..cdaf95f 100644
--- a/src/matrixmessageprocessor.ts
+++ b/src/matrixmessageprocessor.ts
@@ -158,8 +158,7 @@ export class MatrixMessageProcessor {
                 try {
                     const resp = await this.params.mxClient.lookupRoomAlias(id);
                     if (resp && resp.roomId) {
-                        const roomId = resp.roomId;
-                        const channel = await this.bot.GetChannelFromRoomId(roomId);
+                        const channel = await this.bot.GetChannelFromRoomId(resp.roomId);
                         return `<#${channel.id}>`;
                     }
                 } catch (err) { } // ignore, room ID wasn't found
diff --git a/src/matrixroomhandler.ts b/src/matrixroomhandler.ts
index 375a3e9..c33309f 100644
--- a/src/matrixroomhandler.ts
+++ b/src/matrixroomhandler.ts
@@ -182,7 +182,7 @@ export class MatrixRoomHandler {
     //                 fields: {
     //                     guild_id: guild.id,
     //                 },
-    //                 icon: guild.iconURL || ICON_URL, // TODO: Use icons from our content repo. Potential security risk.
+    //                 icon: guild.iconURL || ICON_URL,
     //                 network_id: guild.id,
     //             };
     //         }),
diff --git a/test/mocks/appservicemock.ts b/test/mocks/appservicemock.ts
index 7458d46..d1a7b15 100644
--- a/test/mocks/appservicemock.ts
+++ b/test/mocks/appservicemock.ts
@@ -69,10 +69,6 @@ class IntentMock extends AppserviceMockBase {
         this.underlyingClient = new MatrixClientMock();
     }
 
-    public ban() {
-        this.funcCalled("ban");
-    }
-
     public join() {
         this.funcCalled("join");
     }
@@ -81,20 +77,16 @@ class IntentMock extends AppserviceMockBase {
         this.funcCalled("joinRoom");
     }
 
-    public kick() {
-        this.funcCalled("kick");
-    }
-
     public leave() {
         this.funcCalled("leave");
     }
 
+    public sendText(roomId: string, body: string) {
+        this.funcCalled("sendText", roomId, body);
+    }
+
     public sendEvent(roomId: string, body: string) {
         this.funcCalled("sendEvent", roomId, body);
-    } 
-
-    public unban() {
-        this.funcCalled("unban");
     }
 }
 
@@ -104,6 +96,10 @@ class MatrixClientMock extends AppserviceMockBase {
         super();
     }
 
+    public banUser(roomId: string, userId: string) {
+        this.funcCalled("banUser", roomId, userId);
+    }
+
     public sendMessage(roomId: string, eventContent: IMatrixEvent) {
         this.funcCalled("sendMessage", roomId, eventContent);
     } 
@@ -123,6 +119,10 @@ class MatrixClientMock extends AppserviceMockBase {
         this.funcCalled("leaveRoom", roomId);
     }
 
+    public kickUser(roomId: string, userId: string) {
+        this.funcCalled("kickUser", roomId, userId);
+    }
+
     public sendStateEvent(roomId: string, type: string, stateKey: string, content: {}) {
         this.funcCalled("sendStateEvent", roomId, type, stateKey, content);
     }
@@ -146,4 +146,8 @@ class MatrixClientMock extends AppserviceMockBase {
             return { alias: "#alias:localhost" };
         }
     }
+
+    public unbanUser(roomId: string, userId: string) {
+        this.funcCalled("unbanUser", roomId, userId);
+    }
 }
diff --git a/test/test_clientfactory.ts b/test/test_clientfactory.ts
index b3d6df7..72381e5 100644
--- a/test/test_clientfactory.ts
+++ b/test/test_clientfactory.ts
@@ -17,7 +17,6 @@ limitations under the License.
 import * as Chai from "chai";
 import * as Proxyquire from "proxyquire";
 import {DiscordBridgeConfigAuth} from "../src/config";
-import {MockDiscordClient} from "./mocks/discordclient";
 
 // we are a test file and thus need those
 /* tslint:disable:no-unused-expression max-file-line-count no-any */
diff --git a/test/test_discordcommandhandler.ts b/test/test_discordcommandhandler.ts
index a664395..f296ccc 100644
--- a/test/test_discordcommandhandler.ts
+++ b/test/test_discordcommandhandler.ts
@@ -21,40 +21,19 @@ import { MockChannel } from "./mocks/channel";
 import { MockMember } from "./mocks/member";
 import { MockGuild } from "./mocks/guild";
 import { Util } from "../src/util";
+import { AppserviceMock } from "./mocks/appservicemock";
 
 // we are a test file and thus need those
 /* tslint:disable:no-unused-expression max-file-line-count no-any */
 
 const expect = Chai.expect;
 
-let USERSJOINED = 0;
-let USERSKICKED = 0;
-let USERSBANNED = 0;
-let USERSUNBANNED = 0;
 let ROOMSUNBRIDGED = 0;
-let MESSAGESENT: any = {};
 let MARKED = -1;
 function createCH(opts: any = {}) {
-    USERSJOINED = 0;
-    USERSKICKED = 0;
-    USERSBANNED = 0;
-    USERSUNBANNED = 0;
     ROOMSUNBRIDGED = 0;
-    MESSAGESENT = {};
     MARKED = -1;
-    const bridge = {
-        getIntent: () => {
-            return {
-                ban: async () => { USERSBANNED++; },
-                getEvent: () => ({ content: { } }),
-                join: () => { USERSJOINED++; },
-                kick: async () => { USERSKICKED++; },
-                leave: () => { },
-                sendMessage: async (roomId, content) => { MESSAGESENT = content; return content; },
-                unban: async () => { USERSUNBANNED++; },
-            };
-        },
-    };
+    const bridge = new AppserviceMock();
     const cs = {
         GetRoomIdsFromChannel: async (chan) => {
             return [`#${chan.id}:localhost`];
@@ -83,12 +62,12 @@ function createCH(opts: any = {}) {
             },
         },
     })).DiscordCommandHandler;
-    return new discordCommandHndlr(bridge as any, discord as any);
+    return {handler: new discordCommandHndlr(bridge as any, discord as any), bridge};
 }
 
 describe("DiscordCommandHandler", () => {
     it("will kick a member", async () => {
-        const handler: any = createCH();
+        const {handler, bridge} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel]);
         channel.guild = guild;
@@ -102,10 +81,10 @@ describe("DiscordCommandHandler", () => {
             member,
         };
         await handler.Process(message);
-        expect(USERSKICKED).equals(1);
+        bridge.botIntent.underlyingClient.wasCalled("kickUser", true, "#123:localhost", "@123456:localhost");
     });
     it("will kick a member in all guild rooms", async () => {
-        const handler: any = createCH();
+        const {handler, bridge} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel, (new MockChannel("456"))]);
         channel.guild = guild;
@@ -120,10 +99,10 @@ describe("DiscordCommandHandler", () => {
         };
         await handler.Process(message);
         // tslint:disable-next-line:no-magic-numbers
-        expect(USERSKICKED).equals(2);
+        expect(bridge.botIntent.underlyingClient.wasCalled("kickUser"),).to.equal(2);
     });
     it("will deny permission", async () => {
-        const handler: any = createCH();
+        const {handler, bridge} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel]);
         channel.guild = guild;
@@ -137,10 +116,10 @@ describe("DiscordCommandHandler", () => {
             member,
         };
         await handler.Process(message);
-        expect(USERSKICKED).equals(0);
+        expect(bridge.botIntent.underlyingClient.wasCalled("kickUser", false)).to.equal(0);
     });
     it("will ban a member", async () => {
-        const handler: any = createCH();
+        const {handler, bridge} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel]);
         channel.guild = guild;
@@ -154,10 +133,10 @@ describe("DiscordCommandHandler", () => {
             member,
         };
         await handler.Process(message);
-        expect(USERSBANNED).equals(1);
+        expect(bridge.botIntent.underlyingClient.wasCalled("banUser")).to.equal(1);
     });
     it("will unban a member", async () => {
-        const handler: any = createCH();
+        const {handler, bridge} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel]);
         channel.guild = guild;
@@ -171,10 +150,10 @@ describe("DiscordCommandHandler", () => {
             member,
         };
         await handler.Process(message);
-        expect(USERSUNBANNED).equals(1);
+        expect(bridge.botIntent.underlyingClient.wasCalled("unbanUser")).to.equal(1);
     });
     it("handles !matrix approve", async () => {
-        const handler: any = createCH();
+        const {handler, bridge} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel]);
         channel.guild = guild;
@@ -191,7 +170,7 @@ describe("DiscordCommandHandler", () => {
         expect(MARKED).equals(1);
     });
     it("handles !matrix deny", async () => {
-        const handler: any = createCH();
+        const {handler} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel]);
         channel.guild = guild;
@@ -208,7 +187,7 @@ describe("DiscordCommandHandler", () => {
         expect(MARKED).equals(0);
     });
     it("handles !matrix unbridge", async () => {
-        const handler: any = createCH();
+        const {handler} = createCH();
         const channel = new MockChannel("123");
         const guild = new MockGuild("456", [channel]);
         channel.guild = guild;
diff --git a/test/test_matrixcommandhandler.ts b/test/test_matrixcommandhandler.ts
index 996d3a6..987c3cf 100644
--- a/test/test_matrixcommandhandler.ts
+++ b/test/test_matrixcommandhandler.ts
@@ -18,29 +18,16 @@ import * as Chai from "chai";
 import { Util } from "../src/util";
 import { DiscordBridgeConfig } from "../src/config";
 import { MockChannel } from "./mocks/channel";
-<<<<<<< HEAD
 import { AppserviceMock } from "./mocks/appservicemock";
-=======
 import * as Proxyquire from "proxyquire";
->>>>>>> develop
 
 // we are a test file and thus need those
 /* tslint:disable:no-unused-expression max-file-line-count no-any */
 
 const expect = Chai.expect;
 
-let USERSJOINED = 0;
-let USERSKICKED = 0;
-let USERSBANNED = 0;
-let USERSUNBANNED = 0;
-let MESSAGESENT: any = {};
 
 function createCH(opts: any = {}) {
-    USERSJOINED = 0;
-    USERSKICKED = 0;
-    USERSBANNED = 0;
-    USERSUNBANNED = 0;
-    MESSAGESENT = {};
 
     const bridge = new AppserviceMock();
 
@@ -51,14 +38,6 @@ function createCH(opts: any = {}) {
     } else {
         config.bridge.enableSelfServiceBridging = true;
     }
-    const mxClient = {
-        getUserId: () => "@user:localhost",
-        joinRoom: async () => {
-            USERSJOINED++;
-        },
-        sendReadReceipt: async () => { },
-        setRoomDirectoryVisibilityAppService: async () => { },
-    };
     const provisioner = {
         AskBridgePermission: async () => {
             if (opts.denyBridgePermission) {
@@ -101,7 +80,7 @@ function createCH(opts: any = {}) {
             },
         },
     })).MatrixCommandHandler;
-    return new MatrixCommandHndl(bot as any, bridge, config);
+    return {handler: new MatrixCommandHndl(bot as any, bridge, config), bridge};
 }
 
 function createEvent(msg: string, room?: string, userId?: string) {
@@ -125,9 +104,9 @@ function createContext(remoteData?: any) {
 describe("MatrixCommandHandler", () => {
     describe("Process", () => {
         it("should not process command if not in room", async () => {
-            const handler: any = createCH({disableSS: true});
+            const {handler, bridge} = createCH({disableSS: true});
             await handler.Process(createEvent("", "!666:localhost"), createContext());
-            expect(MESSAGESENT.body).to.equal(undefined);
+            expect(bridge.botIntent.underlyingClient.)
         });
         it("should warn if self service is disabled", async () => {
             const handler: any = createCH({disableSS: true});
diff --git a/test/test_matrixeventprocessor.ts b/test/test_matrixeventprocessor.ts
index dd1a5d2..4e9c8a7 100644
--- a/test/test_matrixeventprocessor.ts
+++ b/test/test_matrixeventprocessor.ts
@@ -17,13 +17,7 @@ limitations under the License.
 import * as Chai from "chai";
 import * as Discord from "discord.js";
 import * as Proxyquire from "proxyquire";
-
-import { PresenceHandler } from "../src/presencehandler";
-import { DiscordBot } from "../src/bot";
-import { MockGuild } from "./mocks/guild";
-import { MockCollection } from "./mocks/collection";
 import { MockMember } from "./mocks/member";
-import { MockEmoji } from "./mocks/emoji";
 import { MatrixEventProcessor, MatrixEventProcessorOpts } from "../src/matrixeventprocessor";
 import { DiscordBridgeConfig } from "../src/config";
 import { MockChannel } from "./mocks/channel";
diff --git a/test/test_matrixmessageprocessor.ts b/test/test_matrixmessageprocessor.ts
index d154bfa..3319248 100644
--- a/test/test_matrixmessageprocessor.ts
+++ b/test/test_matrixmessageprocessor.ts
@@ -15,12 +15,10 @@ limitations under the License.
 */
 
 import * as Chai from "chai";
-import * as Discord from "discord.js";
 import { MockGuild } from "./mocks/guild";
 import { MockMember } from "./mocks/member";
 import { MockChannel } from "./mocks/channel";
 import { MockEmoji } from "./mocks/emoji";
-import { DiscordBot } from "../src/bot";
 import { DbEmoji } from "../src/db/dbdataemoji";
 import { MatrixMessageProcessor } from "../src/matrixmessageprocessor";
 
-- 
GitLab