Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 950ab19

Browse files
authored
Replace MatrixClient.isCryptoEnabled by MatrixClient.getCrypto (#140)
* Replace `MatrixClient.isCryptoEnabled` by `MatrixClient.getCrypto` * Cast `cryptoEnabled` as `boolean` * Fix `MatrixChat-test`
1 parent 7448bd5 commit 950ab19

File tree

14 files changed

+11
-36
lines changed

14 files changed

+11
-36
lines changed

src/components/structures/MatrixChat.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
413413

414414
private async postLoginSetup(): Promise<void> {
415415
const cli = MatrixClientPeg.safeGet();
416-
const cryptoEnabled = cli.isCryptoEnabled();
416+
const cryptoEnabled = Boolean(cli.getCrypto());
417417
if (!cryptoEnabled) {
418418
this.onLoggedIn();
419419
}
@@ -1619,7 +1619,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
16191619
.catch((e) => logger.error("Unable to start DecryptionFailureTracker", e));
16201620

16211621
cli.on(ClientEvent.Room, (room) => {
1622-
if (cli.isCryptoEnabled()) {
1622+
if (cli.getCrypto()) {
16231623
const blacklistEnabled = SettingsStore.getValueAt(
16241624
SettingLevel.ROOM_DEVICE,
16251625
"blacklistUnverifiedDevices",
@@ -1707,7 +1707,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
17071707
}
17081708
}
17091709

1710-
if (cli.isCryptoEnabled()) {
1710+
if (cli.getCrypto()) {
17111711
const blacklistEnabled = SettingsStore.getValueAt(SettingLevel.DEVICE, "blacklistUnverifiedDevices");
17121712
cli.setGlobalBlacklistUnverifiedDevices(blacklistEnabled);
17131713

src/components/structures/RoomView.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
14611461
// set the state immediately then update, so we don't scare the user into thinking the room is unencrypted
14621462
this.setState({ e2eStatus });
14631463

1464-
if (this.context.client.isCryptoEnabled()) {
1464+
if (this.context.client.getCrypto()) {
14651465
/* At this point, the user has encryption on and cross-signing on */
14661466
e2eStatus = await shieldStatusForRoom(this.context.client, room);
14671467
RoomView.e2eStatusCache.set(room.roomId, e2eStatus);

src/components/views/rooms/SendMessageComposer.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
265265
public constructor(props: ISendMessageComposerProps, context: React.ContextType<typeof RoomContext>) {
266266
super(props, context);
267267

268-
if (this.props.mxClient.isCryptoEnabled() && this.props.mxClient.isRoomEncrypted(this.props.room.roomId)) {
268+
if (this.props.mxClient.getCrypto() && this.props.mxClient.isRoomEncrypted(this.props.room.roomId)) {
269269
this.prepareToEncrypt = throttle(
270270
() => {
271271
this.props.mxClient.getCrypto()?.prepareToEncrypt(this.props.room);

src/hooks/useEncryptionStatus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function useEncryptionStatus(client: MatrixClient, room: Room): E2EStatus
1515
const [e2eStatus, setE2eStatus] = useState<E2EStatus | null>(null);
1616

1717
useEffect(() => {
18-
if (client.isCryptoEnabled()) {
18+
if (client.getCrypto()) {
1919
shieldStatusForRoom(client, room).then((e2eStatus) => {
2020
setE2eStatus(e2eStatus);
2121
});

test/components/structures/MatrixChat-test.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ describe("<MatrixChat />", () => {
9898
getThirdpartyProtocols: jest.fn().mockResolvedValue({}),
9999
getClientWellKnown: jest.fn().mockReturnValue({}),
100100
isVersionSupported: jest.fn().mockResolvedValue(false),
101-
isCryptoEnabled: jest.fn().mockReturnValue(false),
102101
initRustCrypto: jest.fn(),
103102
getRoom: jest.fn(),
104103
getMediaHandler: jest.fn().mockReturnValue({
@@ -1012,16 +1011,15 @@ describe("<MatrixChat />", () => {
10121011
setDeviceIsolationMode: jest.fn(),
10131012
userHasCrossSigningKeys: jest.fn().mockResolvedValue(false),
10141013
};
1015-
loginClient.isCryptoEnabled.mockReturnValue(true);
10161014
loginClient.getCrypto.mockReturnValue(mockCrypto as any);
10171015
});
10181016

10191017
it("should go straight to logged in view when crypto is not enabled", async () => {
1020-
loginClient.isCryptoEnabled.mockReturnValue(false);
1018+
loginClient.getCrypto.mockReturnValue(undefined);
10211019

10221020
await getComponentAndLogin(true);
10231021

1024-
expect(loginClient.getCrypto()!.userHasCrossSigningKeys).not.toHaveBeenCalled();
1022+
expect(screen.getByRole("heading", { name: "Welcome Ernie" })).toBeInTheDocument();
10251023
});
10261024

10271025
it("should go straight to logged in view when user does not have cross signing keys and server does not support cross signing", async () => {

test/components/structures/RoomView-test.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe("RoomView", () => {
9797
stores.rightPanelStore.useUnitTestClient(cli);
9898

9999
jest.spyOn(VoipUserMapper.sharedInstance(), "getVirtualRoomForRoom").mockResolvedValue(undefined);
100+
jest.spyOn(cli, "getCrypto").mockReturnValue(undefined);
100101
});
101102

102103
afterEach(() => {
@@ -230,7 +231,6 @@ describe("RoomView", () => {
230231
it("updates url preview visibility on encryption state change", async () => {
231232
room.getMyMembership = jest.fn().mockReturnValue(KnownMembership.Join);
232233
// we should be starting unencrypted
233-
expect(cli.isCryptoEnabled()).toEqual(false);
234234
expect(cli.isRoomEncrypted(room.roomId)).toEqual(false);
235235

236236
const roomViewInstance = await getRoomViewInstance();
@@ -246,7 +246,6 @@ describe("RoomView", () => {
246246
expect(roomViewInstance.state.showUrlPreview).toBe(true);
247247

248248
// now enable encryption
249-
cli.isCryptoEnabled.mockReturnValue(true);
250249
cli.isRoomEncrypted.mockReturnValue(true);
251250

252251
// and fake an encryption event into the room to prompt it to re-check

test/components/views/dialogs/security/CreateSecretStorageDialog-test.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ describe("CreateSecretStorageDialog", () => {
134134
});
135135

136136
it("calls bootstrapSecretStorage once keys are restored if the backup is now trusted", async () => {
137-
mockClient.isCryptoEnabled.mockReturnValue(true);
138-
139137
const result = renderComponent();
140138
await result.findByText(/Enter your account password to confirm the upgrade/);
141139
expect(result.container).toMatchSnapshot();

test/components/views/right_panel/UserInfo-test.tsx

-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ beforeEach(() => {
142142
isUserIgnored: jest.fn(),
143143
getIgnoredUsers: jest.fn(),
144144
setIgnoredUsers: jest.fn(),
145-
isCryptoEnabled: jest.fn(),
146145
getUserId: jest.fn(),
147146
getSafeUserId: jest.fn(),
148147
getDomain: jest.fn(),
@@ -424,7 +423,6 @@ describe("<UserInfo />", () => {
424423

425424
describe("with crypto enabled", () => {
426425
beforeEach(() => {
427-
mockClient.isCryptoEnabled.mockReturnValue(true);
428426
mockClient.doesServerSupportUnstableFeature.mockResolvedValue(true);
429427
mockCrypto.getUserVerificationStatus.mockResolvedValue(new UserVerificationStatus(false, false, false));
430428

@@ -663,7 +661,6 @@ describe("<UserInfo />", () => {
663661

664662
describe("with an encrypted room", () => {
665663
beforeEach(() => {
666-
mockClient.isCryptoEnabled.mockReturnValue(true);
667664
mockClient.isRoomEncrypted.mockReturnValue(true);
668665
});
669666

test/components/views/rooms/RoomHeader-test.tsx

+2-14
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@ Please see LICENSE files in the repository root for full details.
88

99
import React from "react";
1010
import { CallType, MatrixCall } from "matrix-js-sdk/src/webrtc/call";
11-
import {
12-
EventType,
13-
JoinRule,
14-
MatrixClient,
15-
MatrixEvent,
16-
PendingEventOrdering,
17-
Room,
18-
RoomMember,
19-
} from "matrix-js-sdk/src/matrix";
11+
import { EventType, JoinRule, MatrixEvent, PendingEventOrdering, Room, RoomMember } from "matrix-js-sdk/src/matrix";
2012
import { KnownMembership } from "matrix-js-sdk/src/types";
2113
import {
2214
createEvent,
@@ -86,6 +78,7 @@ describe("RoomHeader", () => {
8678
} as unknown as DMRoomMap);
8779

8880
setCardSpy = jest.spyOn(RightPanelStore.instance, "setCard");
81+
jest.spyOn(ShieldUtils, "shieldStatusForRoom").mockResolvedValue(ShieldUtils.E2EStatus.Normal);
8982
});
9083

9184
afterEach(() => {
@@ -595,10 +588,7 @@ describe("RoomHeader", () => {
595588
});
596589

597590
describe("dm", () => {
598-
let client: MatrixClient;
599591
beforeEach(() => {
600-
client = MatrixClientPeg.get()!;
601-
602592
// Make the mocked room a DM
603593
mocked(DMRoomMap.shared().getUserIdForRoomId).mockImplementation((roomId) => {
604594
if (roomId === room.roomId) return "@user:example.com";
@@ -624,8 +614,6 @@ describe("RoomHeader", () => {
624614
getMxcAvatarUrl: () => "mxc://avatar.url/image.png",
625615
},
626616
]);
627-
jest.spyOn(client, "isCryptoEnabled").mockReturnValue(true);
628-
jest.spyOn(ShieldUtils, "shieldStatusForRoom").mockResolvedValue(ShieldUtils.E2EStatus.Normal);
629617
});
630618

631619
it.each([

test/components/views/rooms/SendMessageComposer-test.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ describe("<SendMessageComposer/>", () => {
571571

572572
it("should call prepareToEncrypt when the user is typing", async () => {
573573
const cli = stubClient();
574-
cli.isCryptoEnabled = jest.fn().mockReturnValue(true);
575574
cli.isRoomEncrypted = jest.fn().mockReturnValue(true);
576575
const room = mkStubRoom("!roomId:server", "Room", cli);
577576

test/components/views/settings/devices/LoginWithQR-test.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ function makeClient() {
3838
getUser: jest.fn(),
3939
isGuest: jest.fn().mockReturnValue(false),
4040
isUserIgnored: jest.fn(),
41-
isCryptoEnabled: jest.fn(),
4241
getUserId: jest.fn(),
4342
on: jest.fn(),
4443
isSynapseAdministrator: jest.fn().mockResolvedValue(false),

test/components/views/settings/devices/LoginWithQRSection-test.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ function makeClient(wellKnown: IClientWellKnown) {
2525
getUser: jest.fn(),
2626
isGuest: jest.fn().mockReturnValue(false),
2727
isUserIgnored: jest.fn(),
28-
isCryptoEnabled: jest.fn(),
2928
getUserId: jest.fn(),
3029
on: jest.fn(),
3130
isSynapseAdministrator: jest.fn().mockResolvedValue(false),

test/test-utils/client.ts

-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ export const mockClientMethodsDevice = (
141141
export const mockClientMethodsCrypto = (): Partial<
142142
Record<MethodLikeKeys<MatrixClient> & PropertyLikeKeys<MatrixClient>, unknown>
143143
> => ({
144-
isCryptoEnabled: jest.fn(),
145144
isCrossSigningReady: jest.fn(),
146145
isKeyBackupKeyStored: jest.fn(),
147146
getCrossSigningCacheCallbacks: jest.fn().mockReturnValue({ getCrossSigningKeyCache: jest.fn() }),

test/test-utils/test-utils.ts

-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ export function createTestClient(): MatrixClient {
212212
relations: jest.fn().mockResolvedValue({
213213
events: [],
214214
}),
215-
isCryptoEnabled: jest.fn().mockReturnValue(false),
216215
hasLazyLoadMembersEnabled: jest.fn().mockReturnValue(false),
217216
isInitialSyncComplete: jest.fn().mockReturnValue(true),
218217
downloadKeys: jest.fn(),

0 commit comments

Comments
 (0)