mirror of
https://github.com/element-hq/element-web
synced 2024-11-23 01:35:49 +03:00
Test typescriptification continued (#8327)
* test/utils/MegolmExportEncryption-test.js -> test/utils/MegolmExportEncryption-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * test/utils/ShieldUtils-test.js - test/utils/ShieldUtils-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * type fixes for ShieldUtils-test Signed-off-by: Kerry Archibald <kerrya@element.io> * test/DecryptionFailureTracker-test.js -> test/DecryptionFailureTracker-test.ts Signed-off-by: Kerry Archibald <kerrya@element.io> * remove unsupported assertion failure messages Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts issues in DecryptionFailureTracker Signed-off-by: Kerry Archibald <kerrya@element.io> * add mock restores Signed-off-by: Kerry Archibald <kerrya@element.io> * newline Signed-off-by: Kerry Archibald <kerrya@element.io> * remove commented decriptionfailuretracker code and test Signed-off-by: Kerry Archibald <kerrya@element.io> * make should aggregate error codes correctly pass Signed-off-by: Kerry Archibald <kerrya@element.io> * cheaters types in MegolmExportEncryption Signed-off-by: Kerry Archibald <kerrya@element.io> * lint Signed-off-by: Kerry Archibald <kerrya@element.io> * Revert "fix ts issues in DecryptionFailureTracker" This reverts commit 1ae748cc51088d60722320dbefae04a62310e2e1. Signed-off-by: Kerry Archibald <kerrya@element.io> * Revert "remove unsupported assertion failure messages" This reverts commit 7bd93d075c4d8d45befcbfd59c889782c9a44b48. * Revert "test/DecryptionFailureTracker-test.js -> test/DecryptionFailureTracker-test.ts" This reverts commit 1670025bd2af9a355c2761998202f602d61f242e. * revert change to DecryptionFailureTracker Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
parent
633229ca26
commit
4a04be6d1c
2 changed files with 50 additions and 19 deletions
|
@ -20,7 +20,8 @@ import { Crypto } from "@peculiar/webcrypto";
|
||||||
|
|
||||||
const webCrypto = new Crypto();
|
const webCrypto = new Crypto();
|
||||||
|
|
||||||
function getRandomValues(buf) {
|
function getRandomValues<T extends ArrayBufferView>(buf: T): T {
|
||||||
|
// @ts-ignore fussy generics
|
||||||
return nodeCrypto.randomFillSync(buf);
|
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;
|
return new TextEncoder().encode(s).buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +73,10 @@ describe('MegolmExportEncryption', function() {
|
||||||
let MegolmExportEncryption;
|
let MegolmExportEncryption;
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
window.crypto = { subtle: webCrypto.subtle, getRandomValues };
|
window.crypto = {
|
||||||
|
subtle: webCrypto.subtle,
|
||||||
|
getRandomValues,
|
||||||
|
};
|
||||||
MegolmExportEncryption = require("../../src/utils/MegolmExportEncryption");
|
MegolmExportEncryption = require("../../src/utils/MegolmExportEncryption");
|
||||||
});
|
});
|
||||||
|
|
|
@ -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 { shieldStatusForRoom } from '../../src/utils/ShieldUtils';
|
||||||
import DMRoomMap from '../../src/utils/DMRoomMap';
|
import DMRoomMap from '../../src/utils/DMRoomMap';
|
||||||
|
|
||||||
function mkClient(selfTrust) {
|
function mkClient(selfTrust = false) {
|
||||||
return {
|
return {
|
||||||
getUserId: () => "@self:localhost",
|
getUserId: () => "@self:localhost",
|
||||||
checkUserTrust: (userId) => ({
|
checkUserTrust: (userId) => ({
|
||||||
|
@ -12,7 +33,7 @@ function mkClient(selfTrust) {
|
||||||
isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T",
|
isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T",
|
||||||
}),
|
}),
|
||||||
getStoredDevicesForUser: (userId) => ["DEVICE"],
|
getStoredDevicesForUser: (userId) => ["DEVICE"],
|
||||||
};
|
} as unknown as MatrixClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("mkClient self-test", function() {
|
describe("mkClient self-test", function() {
|
||||||
|
@ -42,9 +63,14 @@ describe("mkClient self-test", function() {
|
||||||
|
|
||||||
describe("shieldStatusForMembership self-trust behaviour", function() {
|
describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
DMRoomMap.sharedInstance = {
|
const mockInstance = {
|
||||||
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
|
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(
|
it.each(
|
||||||
|
@ -55,7 +81,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual("normal");
|
expect(status).toEqual("normal");
|
||||||
});
|
});
|
||||||
|
@ -68,7 +94,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -81,7 +107,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -94,7 +120,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -107,7 +133,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -120,7 +146,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -128,9 +154,10 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
|
||||||
|
|
||||||
describe("shieldStatusForMembership other-trust behaviour", function() {
|
describe("shieldStatusForMembership other-trust behaviour", function() {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
DMRoomMap.sharedInstance = {
|
const mockInstance = {
|
||||||
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
|
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
|
||||||
};
|
} as unknown as DMRoomMap;
|
||||||
|
jest.spyOn(DMRoomMap, 'shared').mockReturnValue(mockInstance);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.each(
|
it.each(
|
||||||
|
@ -140,7 +167,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -152,7 +179,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -164,7 +191,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
||||||
|
@ -176,7 +203,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
|
||||||
const room = {
|
const room = {
|
||||||
roomId: dm ? "DM" : "other",
|
roomId: dm ? "DM" : "other",
|
||||||
getEncryptionTargetMembers: () => ["@self:localhost", "@WF:h", "@FT:h"].map((userId) => ({ userId })),
|
getEncryptionTargetMembers: () => ["@self:localhost", "@WF:h", "@FT:h"].map((userId) => ({ userId })),
|
||||||
};
|
} as unknown as Room;
|
||||||
const status = await shieldStatusForRoom(client, room);
|
const status = await shieldStatusForRoom(client, room);
|
||||||
expect(status).toEqual(result);
|
expect(status).toEqual(result);
|
||||||
});
|
});
|
Loading…
Reference in a new issue