DeviceListener: replace calls to deprecated APIs (#10883)

* DeviceListener: replace calls to deprecated APIs

* fix tests
This commit is contained in:
Richard van der Hoff 2023-05-12 16:55:01 +01:00 committed by GitHub
parent f3534b42df
commit a597da26a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 30 deletions

View file

@ -267,14 +267,16 @@ export default class DeviceListener {
// cross-signing support was added to Matrix in MSC1756, which landed in spec v1.1 // cross-signing support was added to Matrix in MSC1756, which landed in spec v1.1
if (!(await cli.isVersionSupported("v1.1"))) return; if (!(await cli.isVersionSupported("v1.1"))) return;
if (!cli.isCryptoEnabled()) return; const crypto = cli.getCrypto();
if (!crypto) return;
// don't recheck until the initial sync is complete: lots of account data events will fire // don't recheck until the initial sync is complete: lots of account data events will fire
// while the initial sync is processing and we don't need to recheck on each one of them // while the initial sync is processing and we don't need to recheck on each one of them
// (we add a listener on sync to do once check after the initial sync is done) // (we add a listener on sync to do once check after the initial sync is done)
if (!cli.isInitialSyncComplete()) return; if (!cli.isInitialSyncComplete()) return;
const crossSigningReady = await cli.isCrossSigningReady(); const crossSigningReady = await crypto.isCrossSigningReady();
const secretStorageReady = await cli.isSecretStorageReady(); const secretStorageReady = await crypto.isSecretStorageReady();
const allSystemsReady = crossSigningReady && secretStorageReady; const allSystemsReady = crossSigningReady && secretStorageReady;
if (this.dismissedThisDeviceToast || allSystemsReady) { if (this.dismissedThisDeviceToast || allSystemsReady) {
@ -283,7 +285,8 @@ export default class DeviceListener {
this.checkKeyBackupStatus(); this.checkKeyBackupStatus();
} else if (this.shouldShowSetupEncryptionToast()) { } else if (this.shouldShowSetupEncryptionToast()) {
// make sure our keys are finished downloading // make sure our keys are finished downloading
await cli.downloadKeys([cli.getUserId()!]); await crypto.getUserDeviceInfo([cli.getUserId()!]);
// cross signing isn't enabled - nag to enable it // cross signing isn't enabled - nag to enable it
// There are 3 different toasts for: // There are 3 different toasts for:
if (!cli.getCrossSigningId() && cli.getStoredCrossSigningForUser(cli.getUserId()!)) { if (!cli.getCrossSigningId() && cli.getStoredCrossSigningForUser(cli.getUserId()!)) {
@ -310,7 +313,7 @@ export default class DeviceListener {
} }
} }
// This needs to be done after awaiting on downloadKeys() above, so // This needs to be done after awaiting on getUserDeviceInfo() above, so
// we make sure we get the devices after the fetch is done. // we make sure we get the devices after the fetch is done.
await this.ensureDeviceIdsAtStartPopulated(); await this.ensureDeviceIdsAtStartPopulated();
@ -324,10 +327,7 @@ export default class DeviceListener {
const isCurrentDeviceTrusted = const isCurrentDeviceTrusted =
crossSigningReady && crossSigningReady &&
Boolean( Boolean((await crypto.getDeviceVerificationStatus(cli.getUserId()!, cli.deviceId!))?.crossSigningVerified);
(await cli.getCrypto()?.getDeviceVerificationStatus(cli.getUserId()!, cli.deviceId!))
?.crossSigningVerified,
);
// as long as cross-signing isn't ready, // as long as cross-signing isn't ready,
// you can't see or dismiss any device toasts // you can't see or dismiss any device toasts
@ -336,7 +336,7 @@ export default class DeviceListener {
for (const deviceId of devices) { for (const deviceId of devices) {
if (deviceId === cli.deviceId) continue; if (deviceId === cli.deviceId) continue;
const deviceTrust = await cli.getCrypto()!.getDeviceVerificationStatus(cli.getUserId()!, deviceId); const deviceTrust = await crypto.getDeviceVerificationStatus(cli.getUserId()!, deviceId);
if (!deviceTrust?.crossSigningVerified && !this.dismissed.has(deviceId)) { if (!deviceTrust?.crossSigningVerified && !this.dismissed.has(deviceId)) {
if (this.ourDeviceIdsAtStart?.has(deviceId)) { if (this.ourDeviceIdsAtStart?.has(deviceId)) {
oldUnverifiedDeviceIds.add(deviceId); oldUnverifiedDeviceIds.add(deviceId);

View file

@ -81,6 +81,8 @@ describe("DeviceListener", () => {
crossSigningVerified: false, crossSigningVerified: false,
}), }),
getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()), getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()),
isCrossSigningReady: jest.fn().mockResolvedValue(true),
isSecretStorageReady: jest.fn().mockResolvedValue(true),
} as unknown as Mocked<CryptoApi>; } as unknown as Mocked<CryptoApi>;
mockClient = getMockClientWithEventEmitter({ mockClient = getMockClientWithEventEmitter({
isGuest: jest.fn(), isGuest: jest.fn(),
@ -89,15 +91,11 @@ describe("DeviceListener", () => {
getKeyBackupVersion: jest.fn().mockResolvedValue(undefined), getKeyBackupVersion: jest.fn().mockResolvedValue(undefined),
getRooms: jest.fn().mockReturnValue([]), getRooms: jest.fn().mockReturnValue([]),
isVersionSupported: jest.fn().mockResolvedValue(true), isVersionSupported: jest.fn().mockResolvedValue(true),
isCrossSigningReady: jest.fn().mockResolvedValue(true),
isSecretStorageReady: jest.fn().mockResolvedValue(true),
isCryptoEnabled: jest.fn().mockReturnValue(true),
isInitialSyncComplete: jest.fn().mockReturnValue(true), isInitialSyncComplete: jest.fn().mockReturnValue(true),
getKeyBackupEnabled: jest.fn(), getKeyBackupEnabled: jest.fn(),
getCrossSigningId: jest.fn(), getCrossSigningId: jest.fn(),
getStoredCrossSigningForUser: jest.fn(), getStoredCrossSigningForUser: jest.fn(),
waitForClientWellKnown: jest.fn(), waitForClientWellKnown: jest.fn(),
downloadKeys: jest.fn(),
isRoomEncrypted: jest.fn(), isRoomEncrypted: jest.fn(),
getClientWellKnown: jest.fn(), getClientWellKnown: jest.fn(),
getDeviceId: jest.fn().mockReturnValue(deviceId), getDeviceId: jest.fn().mockReturnValue(deviceId),
@ -245,34 +243,34 @@ describe("DeviceListener", () => {
await createAndStart(); await createAndStart();
expect(mockClient!.isVersionSupported).toHaveBeenCalledWith("v1.1"); expect(mockClient!.isVersionSupported).toHaveBeenCalledWith("v1.1");
expect(mockClient!.isCrossSigningReady).not.toHaveBeenCalled(); expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled();
}); });
it("does nothing when crypto is not enabled", async () => { it("does nothing when crypto is not enabled", async () => {
mockClient!.isCryptoEnabled.mockReturnValue(false); mockClient!.getCrypto.mockReturnValue(undefined);
await createAndStart(); await createAndStart();
expect(mockClient!.isCrossSigningReady).not.toHaveBeenCalled(); expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled();
}); });
it("does nothing when initial sync is not complete", async () => { it("does nothing when initial sync is not complete", async () => {
mockClient!.isInitialSyncComplete.mockReturnValue(false); mockClient!.isInitialSyncComplete.mockReturnValue(false);
await createAndStart(); await createAndStart();
expect(mockClient!.isCrossSigningReady).not.toHaveBeenCalled(); expect(mockCrypto!.isCrossSigningReady).not.toHaveBeenCalled();
}); });
describe("set up encryption", () => { describe("set up encryption", () => {
const rooms = [{ roomId: "!room1" }, { roomId: "!room2" }] as unknown as Room[]; const rooms = [{ roomId: "!room1" }, { roomId: "!room2" }] as unknown as Room[];
beforeEach(() => { beforeEach(() => {
mockClient!.isCrossSigningReady.mockResolvedValue(false); mockCrypto!.isCrossSigningReady.mockResolvedValue(false);
mockClient!.isSecretStorageReady.mockResolvedValue(false); mockCrypto!.isSecretStorageReady.mockResolvedValue(false);
mockClient!.getRooms.mockReturnValue(rooms); mockClient!.getRooms.mockReturnValue(rooms);
mockClient!.isRoomEncrypted.mockReturnValue(true); mockClient!.isRoomEncrypted.mockReturnValue(true);
}); });
it("hides setup encryption toast when cross signing and secret storage are ready", async () => { it("hides setup encryption toast when cross signing and secret storage are ready", async () => {
mockClient!.isCrossSigningReady.mockResolvedValue(true); mockCrypto!.isCrossSigningReady.mockResolvedValue(true);
mockClient!.isSecretStorageReady.mockResolvedValue(true); mockCrypto!.isSecretStorageReady.mockResolvedValue(true);
await createAndStart(); await createAndStart();
expect(SetupEncryptionToast.hideToast).toHaveBeenCalled(); expect(SetupEncryptionToast.hideToast).toHaveBeenCalled();
}); });
@ -284,19 +282,17 @@ describe("DeviceListener", () => {
expect(SetupEncryptionToast.hideToast).toHaveBeenCalled(); expect(SetupEncryptionToast.hideToast).toHaveBeenCalled();
}); });
it("does not do any checks or show any toasts when secret storage is being accessed", async () => { it("does not show any toasts when secret storage is being accessed", async () => {
mocked(isSecretStorageBeingAccessed).mockReturnValue(true); mocked(isSecretStorageBeingAccessed).mockReturnValue(true);
await createAndStart(); await createAndStart();
expect(mockClient!.downloadKeys).not.toHaveBeenCalled();
expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled(); expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled();
}); });
it("does not do any checks or show any toasts when no rooms are encrypted", async () => { it("does not show any toasts when no rooms are encrypted", async () => {
mockClient!.isRoomEncrypted.mockReturnValue(false); mockClient!.isRoomEncrypted.mockReturnValue(false);
await createAndStart(); await createAndStart();
expect(mockClient!.downloadKeys).not.toHaveBeenCalled();
expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled(); expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled();
}); });
@ -309,7 +305,7 @@ describe("DeviceListener", () => {
mockClient!.getStoredCrossSigningForUser.mockReturnValue(new CrossSigningInfo(userId)); mockClient!.getStoredCrossSigningForUser.mockReturnValue(new CrossSigningInfo(userId));
await createAndStart(); await createAndStart();
expect(mockClient!.downloadKeys).toHaveBeenCalled(); expect(mockCrypto!.getUserDeviceInfo).toHaveBeenCalled();
expect(SetupEncryptionToast.showToast).toHaveBeenCalledWith( expect(SetupEncryptionToast.showToast).toHaveBeenCalledWith(
SetupEncryptionToast.Kind.VERIFY_THIS_SESSION, SetupEncryptionToast.Kind.VERIFY_THIS_SESSION,
); );
@ -350,7 +346,7 @@ describe("DeviceListener", () => {
}); });
it("checks keybackup status when setup encryption toast has been dismissed", async () => { it("checks keybackup status when setup encryption toast has been dismissed", async () => {
mockClient!.isCrossSigningReady.mockResolvedValue(false); mockCrypto!.isCrossSigningReady.mockResolvedValue(false);
const instance = await createAndStart(); const instance = await createAndStart();
instance.dismissEncryptionSetup(); instance.dismissEncryptionSetup();
@ -402,7 +398,7 @@ describe("DeviceListener", () => {
const deviceTrustUnverified = new DeviceVerificationStatus({}); const deviceTrustUnverified = new DeviceVerificationStatus({});
beforeEach(() => { beforeEach(() => {
mockClient!.isCrossSigningReady.mockResolvedValue(true); mockCrypto!.isCrossSigningReady.mockResolvedValue(true);
mockCrypto!.getUserDeviceInfo.mockResolvedValue( mockCrypto!.getUserDeviceInfo.mockResolvedValue(
new Map([[userId, new Map([currentDevice, device2, device3].map((d) => [d.deviceId, d]))]]), new Map([[userId, new Map([currentDevice, device2, device3].map((d) => [d.deviceId, d]))]]),
); );
@ -415,7 +411,7 @@ describe("DeviceListener", () => {
}); });
describe("bulk unverified sessions toasts", () => { describe("bulk unverified sessions toasts", () => {
it("hides toast when cross signing is not ready", async () => { it("hides toast when cross signing is not ready", async () => {
mockClient!.isCrossSigningReady.mockResolvedValue(false); mockCrypto!.isCrossSigningReady.mockResolvedValue(false);
await createAndStart(); await createAndStart();
expect(BulkUnverifiedSessionsToast.hideToast).toHaveBeenCalled(); expect(BulkUnverifiedSessionsToast.hideToast).toHaveBeenCalled();
expect(BulkUnverifiedSessionsToast.showToast).not.toHaveBeenCalled(); expect(BulkUnverifiedSessionsToast.showToast).not.toHaveBeenCalled();