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

Commit

Permalink
Replace MatrixClient.isCryptoEnabled by MatrixClient.getCrypto (#140
Browse files Browse the repository at this point in the history
)

* Replace `MatrixClient.isCryptoEnabled` by `MatrixClient.getCrypto`

* Cast `cryptoEnabled` as `boolean`

* Fix `MatrixChat-test`
  • Loading branch information
florianduros authored Oct 15, 2024
1 parent 7448bd5 commit 950ab19
Show file tree
Hide file tree
Showing 14 changed files with 11 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {

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

cli.on(ClientEvent.Room, (room) => {
if (cli.isCryptoEnabled()) {
if (cli.getCrypto()) {
const blacklistEnabled = SettingsStore.getValueAt(
SettingLevel.ROOM_DEVICE,
"blacklistUnverifiedDevices",
Expand Down Expand Up @@ -1707,7 +1707,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}
}

if (cli.isCryptoEnabled()) {
if (cli.getCrypto()) {
const blacklistEnabled = SettingsStore.getValueAt(SettingLevel.DEVICE, "blacklistUnverifiedDevices");
cli.setGlobalBlacklistUnverifiedDevices(blacklistEnabled);

Expand Down
2 changes: 1 addition & 1 deletion src/components/structures/RoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
// set the state immediately then update, so we don't scare the user into thinking the room is unencrypted
this.setState({ e2eStatus });

if (this.context.client.isCryptoEnabled()) {
if (this.context.client.getCrypto()) {
/* At this point, the user has encryption on and cross-signing on */
e2eStatus = await shieldStatusForRoom(this.context.client, room);
RoomView.e2eStatusCache.set(room.roomId, e2eStatus);
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/rooms/SendMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
public constructor(props: ISendMessageComposerProps, context: React.ContextType<typeof RoomContext>) {
super(props, context);

if (this.props.mxClient.isCryptoEnabled() && this.props.mxClient.isRoomEncrypted(this.props.room.roomId)) {
if (this.props.mxClient.getCrypto() && this.props.mxClient.isRoomEncrypted(this.props.room.roomId)) {
this.prepareToEncrypt = throttle(
() => {
this.props.mxClient.getCrypto()?.prepareToEncrypt(this.props.room);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useEncryptionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function useEncryptionStatus(client: MatrixClient, room: Room): E2EStatus
const [e2eStatus, setE2eStatus] = useState<E2EStatus | null>(null);

useEffect(() => {
if (client.isCryptoEnabled()) {
if (client.getCrypto()) {
shieldStatusForRoom(client, room).then((e2eStatus) => {
setE2eStatus(e2eStatus);
});
Expand Down
6 changes: 2 additions & 4 deletions test/components/structures/MatrixChat-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ describe("<MatrixChat />", () => {
getThirdpartyProtocols: jest.fn().mockResolvedValue({}),
getClientWellKnown: jest.fn().mockReturnValue({}),
isVersionSupported: jest.fn().mockResolvedValue(false),
isCryptoEnabled: jest.fn().mockReturnValue(false),
initRustCrypto: jest.fn(),
getRoom: jest.fn(),
getMediaHandler: jest.fn().mockReturnValue({
Expand Down Expand Up @@ -1012,16 +1011,15 @@ describe("<MatrixChat />", () => {
setDeviceIsolationMode: jest.fn(),
userHasCrossSigningKeys: jest.fn().mockResolvedValue(false),
};
loginClient.isCryptoEnabled.mockReturnValue(true);
loginClient.getCrypto.mockReturnValue(mockCrypto as any);
});

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

await getComponentAndLogin(true);

expect(loginClient.getCrypto()!.userHasCrossSigningKeys).not.toHaveBeenCalled();
expect(screen.getByRole("heading", { name: "Welcome Ernie" })).toBeInTheDocument();
});

it("should go straight to logged in view when user does not have cross signing keys and server does not support cross signing", async () => {
Expand Down
3 changes: 1 addition & 2 deletions test/components/structures/RoomView-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe("RoomView", () => {
stores.rightPanelStore.useUnitTestClient(cli);

jest.spyOn(VoipUserMapper.sharedInstance(), "getVirtualRoomForRoom").mockResolvedValue(undefined);
jest.spyOn(cli, "getCrypto").mockReturnValue(undefined);
});

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

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

// now enable encryption
cli.isCryptoEnabled.mockReturnValue(true);
cli.isRoomEncrypted.mockReturnValue(true);

// and fake an encryption event into the room to prompt it to re-check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ describe("CreateSecretStorageDialog", () => {
});

it("calls bootstrapSecretStorage once keys are restored if the backup is now trusted", async () => {
mockClient.isCryptoEnabled.mockReturnValue(true);

const result = renderComponent();
await result.findByText(/Enter your account password to confirm the upgrade/);
expect(result.container).toMatchSnapshot();
Expand Down
3 changes: 0 additions & 3 deletions test/components/views/right_panel/UserInfo-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ beforeEach(() => {
isUserIgnored: jest.fn(),
getIgnoredUsers: jest.fn(),
setIgnoredUsers: jest.fn(),
isCryptoEnabled: jest.fn(),
getUserId: jest.fn(),
getSafeUserId: jest.fn(),
getDomain: jest.fn(),
Expand Down Expand Up @@ -424,7 +423,6 @@ describe("<UserInfo />", () => {

describe("with crypto enabled", () => {
beforeEach(() => {
mockClient.isCryptoEnabled.mockReturnValue(true);
mockClient.doesServerSupportUnstableFeature.mockResolvedValue(true);
mockCrypto.getUserVerificationStatus.mockResolvedValue(new UserVerificationStatus(false, false, false));

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

describe("with an encrypted room", () => {
beforeEach(() => {
mockClient.isCryptoEnabled.mockReturnValue(true);
mockClient.isRoomEncrypted.mockReturnValue(true);
});

Expand Down
16 changes: 2 additions & 14 deletions test/components/views/rooms/RoomHeader-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ Please see LICENSE files in the repository root for full details.

import React from "react";
import { CallType, MatrixCall } from "matrix-js-sdk/src/webrtc/call";
import {
EventType,
JoinRule,
MatrixClient,
MatrixEvent,
PendingEventOrdering,
Room,
RoomMember,
} from "matrix-js-sdk/src/matrix";
import { EventType, JoinRule, MatrixEvent, PendingEventOrdering, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
import {
createEvent,
Expand Down Expand Up @@ -86,6 +78,7 @@ describe("RoomHeader", () => {
} as unknown as DMRoomMap);

setCardSpy = jest.spyOn(RightPanelStore.instance, "setCard");
jest.spyOn(ShieldUtils, "shieldStatusForRoom").mockResolvedValue(ShieldUtils.E2EStatus.Normal);
});

afterEach(() => {
Expand Down Expand Up @@ -595,10 +588,7 @@ describe("RoomHeader", () => {
});

describe("dm", () => {
let client: MatrixClient;
beforeEach(() => {
client = MatrixClientPeg.get()!;

// Make the mocked room a DM
mocked(DMRoomMap.shared().getUserIdForRoomId).mockImplementation((roomId) => {
if (roomId === room.roomId) return "@user:example.com";
Expand All @@ -624,8 +614,6 @@ describe("RoomHeader", () => {
getMxcAvatarUrl: () => "mxc://avatar.url/image.png",
},
]);
jest.spyOn(client, "isCryptoEnabled").mockReturnValue(true);
jest.spyOn(ShieldUtils, "shieldStatusForRoom").mockResolvedValue(ShieldUtils.E2EStatus.Normal);
});

it.each([
Expand Down
1 change: 0 additions & 1 deletion test/components/views/rooms/SendMessageComposer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,6 @@ describe("<SendMessageComposer/>", () => {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function makeClient() {
getUser: jest.fn(),
isGuest: jest.fn().mockReturnValue(false),
isUserIgnored: jest.fn(),
isCryptoEnabled: jest.fn(),
getUserId: jest.fn(),
on: jest.fn(),
isSynapseAdministrator: jest.fn().mockResolvedValue(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function makeClient(wellKnown: IClientWellKnown) {
getUser: jest.fn(),
isGuest: jest.fn().mockReturnValue(false),
isUserIgnored: jest.fn(),
isCryptoEnabled: jest.fn(),
getUserId: jest.fn(),
on: jest.fn(),
isSynapseAdministrator: jest.fn().mockResolvedValue(false),
Expand Down
1 change: 0 additions & 1 deletion test/test-utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export const mockClientMethodsDevice = (
export const mockClientMethodsCrypto = (): Partial<
Record<MethodLikeKeys<MatrixClient> & PropertyLikeKeys<MatrixClient>, unknown>
> => ({
isCryptoEnabled: jest.fn(),
isCrossSigningReady: jest.fn(),
isKeyBackupKeyStored: jest.fn(),
getCrossSigningCacheCallbacks: jest.fn().mockReturnValue({ getCrossSigningKeyCache: jest.fn() }),
Expand Down
1 change: 0 additions & 1 deletion test/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ export function createTestClient(): MatrixClient {
relations: jest.fn().mockResolvedValue({
events: [],
}),
isCryptoEnabled: jest.fn().mockReturnValue(false),
hasLazyLoadMembersEnabled: jest.fn().mockReturnValue(false),
isInitialSyncComplete: jest.fn().mockReturnValue(true),
downloadKeys: jest.fn(),
Expand Down

0 comments on commit 950ab19

Please sign in to comment.