diff --git a/test/utils/MegolmExportEncryption-test.js b/test/utils/MegolmExportEncryption-test.ts similarity index 95% rename from test/utils/MegolmExportEncryption-test.js rename to test/utils/MegolmExportEncryption-test.ts index 8b16085efc..4fb92a8f8b 100644 --- a/test/utils/MegolmExportEncryption-test.js +++ b/test/utils/MegolmExportEncryption-test.ts @@ -20,7 +20,8 @@ import { Crypto } from "@peculiar/webcrypto"; const webCrypto = new Crypto(); -function getRandomValues(buf) { +function getRandomValues(buf: T): T { + // @ts-ignore fussy generics return nodeCrypto.randomFillSync(buf); } @@ -64,7 +65,7 @@ const TEST_VECTORS=[ ], ]; -function stringToArray(s) { +function stringToArray(s: string): ArrayBufferLike { return new TextEncoder().encode(s).buffer; } @@ -72,7 +73,10 @@ describe('MegolmExportEncryption', function() { let MegolmExportEncryption; beforeAll(() => { - window.crypto = { subtle: webCrypto.subtle, getRandomValues }; + window.crypto = { + subtle: webCrypto.subtle, + getRandomValues, + }; MegolmExportEncryption = require("../../src/utils/MegolmExportEncryption"); }); diff --git a/test/utils/ShieldUtils-test.js b/test/utils/ShieldUtils-test.ts similarity index 83% rename from test/utils/ShieldUtils-test.js rename to test/utils/ShieldUtils-test.ts index 85f9de3150..10ccb80f96 100644 --- a/test/utils/ShieldUtils-test.js +++ b/test/utils/ShieldUtils-test.ts @@ -1,7 +1,28 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +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 { + MatrixClient, + Room, +} from 'matrix-js-sdk/src/matrix'; + import { shieldStatusForRoom } from '../../src/utils/ShieldUtils'; import DMRoomMap from '../../src/utils/DMRoomMap'; -function mkClient(selfTrust) { +function mkClient(selfTrust = false) { return { getUserId: () => "@self:localhost", checkUserTrust: (userId) => ({ @@ -12,7 +33,7 @@ function mkClient(selfTrust) { isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T", }), getStoredDevicesForUser: (userId) => ["DEVICE"], - }; + } as unknown as MatrixClient; } describe("mkClient self-test", function() { @@ -42,9 +63,14 @@ describe("mkClient self-test", function() { describe("shieldStatusForMembership self-trust behaviour", function() { beforeAll(() => { - DMRoomMap.sharedInstance = { + const mockInstance = { getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null, - }; + } as unknown as DMRoomMap; + jest.spyOn(DMRoomMap, 'shared').mockReturnValue(mockInstance); + }); + + afterAll(() => { + jest.spyOn(DMRoomMap, 'shared').mockRestore(); }); it.each( @@ -55,7 +81,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual("normal"); }); @@ -68,7 +94,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -81,7 +107,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -94,7 +120,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -107,7 +133,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -120,7 +146,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -128,9 +154,10 @@ describe("shieldStatusForMembership self-trust behaviour", function() { describe("shieldStatusForMembership other-trust behaviour", function() { beforeAll(() => { - DMRoomMap.sharedInstance = { + const mockInstance = { getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null, - }; + } as unknown as DMRoomMap; + jest.spyOn(DMRoomMap, 'shared').mockReturnValue(mockInstance); }); it.each( @@ -140,7 +167,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -152,7 +179,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -164,7 +191,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); }); @@ -176,7 +203,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() { const room = { roomId: dm ? "DM" : "other", getEncryptionTargetMembers: () => ["@self:localhost", "@WF:h", "@FT:h"].map((userId) => ({ userId })), - }; + } as unknown as Room; const status = await shieldStatusForRoom(client, room); expect(status).toEqual(result); });