Skip to content

Commit 808f7ee

Browse files
committed
test(legacy crypto): update existing tests to not use legacy crypto
- `Embedded.spec.ts`: casting since `encryptAndSendToDevices` is removed from `MatrixClient`. - `room.spec.ts`: remove deprecated usage of `MatrixClient.crypto` - `matrix-client.spec.ts` & `matrix-client-methods.spec.ts`: remove calls of deprecated methods of `MatrixClient`
1 parent 8a53e23 commit 808f7ee

File tree

4 files changed

+5
-138
lines changed

4 files changed

+5
-138
lines changed

spec/integ/matrix-client-methods.spec.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616
import HttpBackend from "matrix-mock-request";
17-
import { Mocked } from "jest-mock";
1817

1918
import * as utils from "../test-utils/test-utils";
2019
import { IStoredClientOpts, MatrixClient } from "../../src/client";
@@ -34,7 +33,6 @@ import { THREAD_RELATION_TYPE } from "../../src/models/thread";
3433
import { IFilterDefinition } from "../../src/filter";
3534
import { ISearchResults } from "../../src/@types/search";
3635
import { IStore } from "../../src/store";
37-
import { CryptoBackend } from "../../src/common-crypto/CryptoBackend";
3836
import { SetPresence } from "../../src/sync";
3937
import { KnownMembership } from "../../src/@types/membership";
4038

@@ -1508,49 +1506,6 @@ describe("MatrixClient", function () {
15081506
});
15091507
});
15101508

1511-
describe("uploadKeys", () => {
1512-
// uploadKeys() is a no-op nowadays, so there's not much to test here.
1513-
it("should complete successfully", async () => {
1514-
await client.uploadKeys();
1515-
});
1516-
});
1517-
1518-
describe("getCryptoTrustCrossSignedDevices", () => {
1519-
it("should throw if e2e is disabled", () => {
1520-
expect(() => client.getCryptoTrustCrossSignedDevices()).toThrow("End-to-end encryption disabled");
1521-
});
1522-
1523-
it("should proxy to the crypto backend", async () => {
1524-
const mockBackend = {
1525-
getTrustCrossSignedDevices: jest.fn().mockReturnValue(true),
1526-
} as unknown as Mocked<CryptoBackend>;
1527-
client["cryptoBackend"] = mockBackend;
1528-
1529-
expect(client.getCryptoTrustCrossSignedDevices()).toBe(true);
1530-
mockBackend.getTrustCrossSignedDevices.mockReturnValue(false);
1531-
expect(client.getCryptoTrustCrossSignedDevices()).toBe(false);
1532-
});
1533-
});
1534-
1535-
describe("setCryptoTrustCrossSignedDevices", () => {
1536-
it("should throw if e2e is disabled", () => {
1537-
expect(() => client.setCryptoTrustCrossSignedDevices(false)).toThrow("End-to-end encryption disabled");
1538-
});
1539-
1540-
it("should proxy to the crypto backend", async () => {
1541-
const mockBackend = {
1542-
setTrustCrossSignedDevices: jest.fn(),
1543-
} as unknown as Mocked<CryptoBackend>;
1544-
client["cryptoBackend"] = mockBackend;
1545-
1546-
client.setCryptoTrustCrossSignedDevices(true);
1547-
expect(mockBackend.setTrustCrossSignedDevices).toHaveBeenLastCalledWith(true);
1548-
1549-
client.setCryptoTrustCrossSignedDevices(false);
1550-
expect(mockBackend.setTrustCrossSignedDevices).toHaveBeenLastCalledWith(false);
1551-
});
1552-
});
1553-
15541509
describe("setSyncPresence", () => {
15551510
it("should pass calls through to the underlying sync api", () => {
15561511
const setPresence = jest.fn();

spec/unit/embedded.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ describe("RoomWidgetClient", () => {
752752
expect(widgetApi.requestCapabilityToSendToDevice).toHaveBeenCalledWith("org.example.foo");
753753

754754
const payload = { type: "org.example.foo", hello: "world" };
755-
await client.encryptAndSendToDevices(
755+
const embeddedClient = client as RoomWidgetClient;
756+
await embeddedClient.encryptAndSendToDevices(
756757
[
757758
{ userId: "@alice:example.org", deviceInfo: new DeviceInfo("aliceWeb") },
758759
{ userId: "@bob:example.org", deviceInfo: new DeviceInfo("bobDesktop") },

spec/unit/matrix-client.spec.ts

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ import {
6868
PolicyRecommendation,
6969
PolicyScope,
7070
} from "../../src/models/invites-ignorer";
71-
import { IOlmDevice } from "../../src/crypto/algorithms/megolm";
7271
import { defer, QueryDict } from "../../src/utils";
7372
import { SyncState } from "../../src/sync";
7473
import * as featureUtils from "../../src/feature";
7574
import { StubStore } from "../../src/store/stub";
76-
import { SecretStorageKeyDescriptionAesV1, ServerSideSecretStorageImpl } from "../../src/secret-storage";
77-
import { CryptoBackend } from "../../src/common-crypto/CryptoBackend";
75+
import { ServerSideSecretStorageImpl } from "../../src/secret-storage";
7876
import { KnownMembership } from "../../src/@types/membership";
7977
import { RoomMessageEventContent } from "../../src/@types/events";
8078
import { mockOpenIdConfiguration } from "../test-utils/oidc.ts";
@@ -1937,7 +1935,7 @@ describe("MatrixClient", function () {
19371935
encryptEvent: jest.fn(),
19381936
stop: jest.fn(),
19391937
} as unknown as Mocked<Crypto>;
1940-
client.crypto = client["cryptoBackend"] = mockCrypto;
1938+
client["cryptoBackend"] = mockCrypto;
19411939
});
19421940

19431941
function assertCancelled() {
@@ -2323,21 +2321,6 @@ describe("MatrixClient", function () {
23232321
});
23242322
});
23252323

2326-
describe("encryptAndSendToDevices", () => {
2327-
it("throws an error if crypto is unavailable", () => {
2328-
client.crypto = undefined;
2329-
expect(() => client.encryptAndSendToDevices([], {})).toThrow();
2330-
});
2331-
2332-
it("is an alias for the crypto method", async () => {
2333-
client.crypto = testUtils.mock(Crypto, "Crypto");
2334-
const deviceInfos: IOlmDevice[] = [];
2335-
const payload = {};
2336-
await client.encryptAndSendToDevices(deviceInfos, payload);
2337-
expect(client.crypto.encryptAndSendToDevices).toHaveBeenLastCalledWith(deviceInfos, payload);
2338-
});
2339-
});
2340-
23412324
describe("support for ignoring invites", () => {
23422325
beforeEach(() => {
23432326
// Mockup `getAccountData`/`setAccountData`.
@@ -3199,85 +3182,13 @@ describe("MatrixClient", function () {
31993182
client["_secretStorage"] = mockSecretStorage;
32003183
});
32013184

3202-
it("hasSecretStorageKey", async () => {
3203-
mockSecretStorage.hasKey.mockResolvedValue(false);
3204-
expect(await client.hasSecretStorageKey("mykey")).toBe(false);
3205-
expect(mockSecretStorage.hasKey).toHaveBeenCalledWith("mykey");
3206-
});
3207-
3208-
it("isSecretStored", async () => {
3209-
const mockResult = { key: {} as SecretStorageKeyDescriptionAesV1 };
3210-
mockSecretStorage.isStored.mockResolvedValue(mockResult);
3211-
expect(await client.isSecretStored("mysecret")).toBe(mockResult);
3212-
expect(mockSecretStorage.isStored).toHaveBeenCalledWith("mysecret");
3213-
});
3214-
3215-
it("getDefaultSecretStorageKeyId", async () => {
3216-
mockSecretStorage.getDefaultKeyId.mockResolvedValue("bzz");
3217-
expect(await client.getDefaultSecretStorageKeyId()).toEqual("bzz");
3218-
});
3219-
32203185
it("isKeyBackupKeyStored", async () => {
32213186
mockSecretStorage.isStored.mockResolvedValue(null);
32223187
expect(await client.isKeyBackupKeyStored()).toBe(null);
32233188
expect(mockSecretStorage.isStored).toHaveBeenCalledWith("m.megolm_backup.v1");
32243189
});
32253190
});
32263191

3227-
// these wrappers are deprecated, but we need coverage of them to pass the quality gate
3228-
describe("Crypto wrappers", () => {
3229-
describe("exception if no crypto", () => {
3230-
it("isCrossSigningReady", () => {
3231-
expect(() => client.isCrossSigningReady()).toThrow("End-to-end encryption disabled");
3232-
});
3233-
3234-
it("bootstrapCrossSigning", () => {
3235-
expect(() => client.bootstrapCrossSigning({})).toThrow("End-to-end encryption disabled");
3236-
});
3237-
3238-
it("isSecretStorageReady", () => {
3239-
expect(() => client.isSecretStorageReady()).toThrow("End-to-end encryption disabled");
3240-
});
3241-
});
3242-
3243-
describe("defer to crypto backend", () => {
3244-
let mockCryptoBackend: Mocked<CryptoBackend>;
3245-
3246-
beforeEach(() => {
3247-
mockCryptoBackend = {
3248-
isCrossSigningReady: jest.fn(),
3249-
bootstrapCrossSigning: jest.fn(),
3250-
isSecretStorageReady: jest.fn(),
3251-
stop: jest.fn().mockResolvedValue(undefined),
3252-
} as unknown as Mocked<CryptoBackend>;
3253-
client["cryptoBackend"] = mockCryptoBackend;
3254-
});
3255-
3256-
it("isCrossSigningReady", async () => {
3257-
const testResult = "test";
3258-
mockCryptoBackend.isCrossSigningReady.mockResolvedValue(testResult as unknown as boolean);
3259-
expect(await client.isCrossSigningReady()).toBe(testResult);
3260-
expect(mockCryptoBackend.isCrossSigningReady).toHaveBeenCalledTimes(1);
3261-
});
3262-
3263-
it("bootstrapCrossSigning", async () => {
3264-
const testOpts = {};
3265-
mockCryptoBackend.bootstrapCrossSigning.mockResolvedValue(undefined);
3266-
await client.bootstrapCrossSigning(testOpts);
3267-
expect(mockCryptoBackend.bootstrapCrossSigning).toHaveBeenCalledTimes(1);
3268-
expect(mockCryptoBackend.bootstrapCrossSigning).toHaveBeenCalledWith(testOpts);
3269-
});
3270-
3271-
it("isSecretStorageReady", async () => {
3272-
client["cryptoBackend"] = mockCryptoBackend;
3273-
const testResult = "test";
3274-
mockCryptoBackend.isSecretStorageReady.mockResolvedValue(testResult as unknown as boolean);
3275-
expect(await client.isSecretStorageReady()).toBe(testResult);
3276-
expect(mockCryptoBackend.isSecretStorageReady).toHaveBeenCalledTimes(1);
3277-
});
3278-
});
3279-
});
3280-
32813192
describe("paginateEventTimeline()", () => {
32823193
describe("notifications timeline", () => {
32833194
const unsafeNotification = {

spec/unit/room.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3774,7 +3774,7 @@ describe("Room", function () {
37743774

37753775
it("should load pending events from from the store and decrypt if needed", async () => {
37763776
const client = new TestClient(userA).client;
3777-
client.crypto = client["cryptoBackend"] = {
3777+
client["cryptoBackend"] = {
37783778
decryptEvent: jest.fn().mockResolvedValue({ clearEvent: { body: "enc" } }),
37793779
} as unknown as Crypto;
37803780
client.store.getPendingEvents = jest.fn(async (roomId) => [

0 commit comments

Comments
 (0)