Kill off references to deprecated getStoredDevice and getStoredDevicesForUser (#11152)

* Use new `CryptoEvent.VerificationRequestReceived` event

https://github.com/matrix-org/matrix-js-sdk/pull/3514 deprecates
`CryptoEvent.VerificationRequest` in favour of
`CryptoEvent.VerificationRequestReceived`. Use the new event.

* Factor out `getDeviceCryptoInfo` function

I seem to be writing this logic several times, so let's factor it out.

* Factor out `getUserDeviceIds` function

Another utility function

* VerificationRequestToast: `getStoredDevice` -> `getDeviceCryptoInfo`

* SlashCommands: `getStoredDevice` -> `getDeviceCryptoInfo`

* MemberTile: `getStoredDevicesForUser` -> `getUserDeviceIds`

* Remove redundant mock of `getStoredDevicesForUser`
This commit is contained in:
Richard van der Hoff 2023-06-28 13:39:34 +01:00 committed by GitHub
parent 0a3a111327
commit 46eb34a55d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 189 additions and 34 deletions

View file

@ -160,7 +160,6 @@ beforeEach(() => {
credentials: {},
setPowerLevel: jest.fn(),
downloadKeys: jest.fn(),
getStoredDevicesForUser: jest.fn(),
getCrypto: jest.fn().mockReturnValue(mockCrypto),
getStoredCrossSigningForUser: jest.fn(),
} as unknown as MatrixClient);

View file

@ -15,18 +15,23 @@ limitations under the License.
*/
import React, { ComponentProps } from "react";
import { Mocked } from "jest-mock";
import { mocked, Mocked } from "jest-mock";
import { act, render, RenderResult } from "@testing-library/react";
import {
VerificationRequest,
VerificationRequestEvent,
} from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo";
import { IMyDevice, MatrixClient } from "matrix-js-sdk/src/client";
import { TypedEventEmitter } from "matrix-js-sdk/src/models/typed-event-emitter";
import { Device } from "matrix-js-sdk/src/matrix";
import VerificationRequestToast from "../../../../src/components/views/toasts/VerificationRequestToast";
import { flushPromises, getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils";
import {
flushPromises,
getMockClientWithEventEmitter,
mockClientMethodsCrypto,
mockClientMethodsUser,
} from "../../../test-utils";
import ToastStore from "../../../../src/stores/ToastStore";
function renderComponent(
@ -46,7 +51,7 @@ describe("VerificationRequestToast", () => {
beforeEach(() => {
client = getMockClientWithEventEmitter({
...mockClientMethodsUser(),
getStoredDevice: jest.fn(),
...mockClientMethodsCrypto(),
getDevice: jest.fn(),
});
});
@ -56,9 +61,15 @@ describe("VerificationRequestToast", () => {
const otherIDevice: IMyDevice = { device_id: otherDeviceId, last_seen_ip: "1.1.1.1" };
client.getDevice.mockResolvedValue(otherIDevice);
const otherDeviceInfo = new DeviceInfo(otherDeviceId);
otherDeviceInfo.unsigned = { device_display_name: "my other device" };
client.getStoredDevice.mockReturnValue(otherDeviceInfo);
const otherDeviceInfo = new Device({
algorithms: [],
keys: new Map(),
userId: "",
deviceId: otherDeviceId,
displayName: "my other device",
});
const deviceMap = new Map([[client.getSafeUserId(), new Map([[otherDeviceId, otherDeviceInfo]])]]);
mocked(client.getCrypto()!.getUserDeviceInfo).mockResolvedValue(deviceMap);
const request = makeMockVerificationRequest({
isSelfVerification: true,

View file

@ -64,6 +64,8 @@ export class MockClientWithEventEmitter extends EventEmitter {
getUserId: jest.fn().mockReturnValue(aliceId),
});
* ```
*
* See also `stubClient()` which does something similar but uses a more complete mock client.
*/
export const getMockClientWithEventEmitter = (
mockProperties: Partial<Record<keyof MatrixClient, unknown>>,
@ -158,6 +160,7 @@ export const mockClientMethodsCrypto = (): Partial<
getSessionBackupPrivateKey: jest.fn(),
},
getCrypto: jest.fn().mockReturnValue({
getUserDeviceInfo: jest.fn(),
getCrossSigningStatus: jest.fn().mockResolvedValue({
publicKeysOnDevice: true,
privateKeysInSecretStorage: false,

View file

@ -60,6 +60,8 @@ import MatrixClientBackedSettingsHandler from "../../src/settings/handlers/Matri
* TODO: once the components are updated to get their MatrixClients from
* the react context, we can get rid of this and just inject a test client
* via the context instead.
*
* See also `getMockClientWithEventEmitter` which does something similar but different.
*/
export function stubClient(): MatrixClient {
const client = createTestClient();

View file

@ -0,0 +1,80 @@
/*
Copyright 2023 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 { Mocked, mocked } from "jest-mock";
import { Device, MatrixClient } from "matrix-js-sdk/src/matrix";
import { getDeviceCryptoInfo, getUserDeviceIds } from "../../../src/utils/crypto/deviceInfo";
import { getMockClientWithEventEmitter, mockClientMethodsCrypto } from "../../test-utils";
describe("getDeviceCryptoInfo()", () => {
let mockClient: Mocked<MatrixClient>;
beforeEach(() => {
mockClient = getMockClientWithEventEmitter({ ...mockClientMethodsCrypto() });
});
it("should return undefined on clients with no crypto", async () => {
jest.spyOn(mockClient, "getCrypto").mockReturnValue(undefined);
await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBeUndefined();
});
it("should return undefined for unknown users", async () => {
mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(new Map());
await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBeUndefined();
});
it("should return undefined for unknown devices", async () => {
mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(new Map([["@user:id", new Map()]]));
await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBeUndefined();
});
it("should return the right result for known devices", async () => {
const mockDevice = { deviceId: "device_id" } as Device;
mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(
new Map([["@user:id", new Map([["device_id", mockDevice]])]]),
);
await expect(getDeviceCryptoInfo(mockClient, "@user:id", "device_id")).resolves.toBe(mockDevice);
expect(mockClient.getCrypto()!.getUserDeviceInfo).toHaveBeenCalledWith(["@user:id"], undefined);
});
});
describe("getUserDeviceIds", () => {
let mockClient: Mocked<MatrixClient>;
beforeEach(() => {
mockClient = getMockClientWithEventEmitter({ ...mockClientMethodsCrypto() });
});
it("should return empty set on clients with no crypto", async () => {
jest.spyOn(mockClient, "getCrypto").mockReturnValue(undefined);
await expect(getUserDeviceIds(mockClient, "@user:id")).resolves.toEqual(new Set());
});
it("should return empty set for unknown users", async () => {
mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(new Map());
await expect(getUserDeviceIds(mockClient, "@user:id")).resolves.toEqual(new Set());
});
it("should return the right result for known users", async () => {
const mockDevice = { deviceId: "device_id" } as Device;
mocked(mockClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(
new Map([["@user:id", new Map([["device_id", mockDevice]])]]),
);
await expect(getUserDeviceIds(mockClient, "@user:id")).resolves.toEqual(new Set(["device_id"]));
expect(mockClient.getCrypto()!.getUserDeviceInfo).toHaveBeenCalledWith(["@user:id"]);
});
});