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

Commit d40092c

Browse files
authored
Element-R: Use MatrixClient.CryptoApi.getUserVerificationStatus instead of MatrixClient.checkUserTrust in MemberTile.tsx (#11701)
* Use `MatrixClient.CryptoApi.getUserVerificationStatus` instead of `MatrixClient.checkUserTrust` in `MemberTile.tsx` * Add tests to `MemberTile.tsx` * Use `mocked` instead of `spyOn`
1 parent 241df0b commit d40092c

File tree

4 files changed

+255
-4
lines changed

4 files changed

+255
-4
lines changed

src/components/views/rooms/MemberTile.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ export default class MemberTile extends React.Component<IProps, IState> {
117117
const cli = MatrixClientPeg.safeGet();
118118
const { userId } = this.props.member;
119119
const isMe = userId === cli.getUserId();
120-
const userTrust = cli.checkUserTrust(userId);
121-
if (!userTrust.isCrossSigningVerified()) {
120+
const userTrust = await cli.getCrypto()?.getUserVerificationStatus(userId);
121+
if (!userTrust?.isCrossSigningVerified()) {
122122
this.setState({
123-
e2eStatus: userTrust.wasCrossSigningVerified() ? E2EState.Warning : E2EState.Normal,
123+
e2eStatus: userTrust?.wasCrossSigningVerified() ? E2EState.Warning : E2EState.Normal,
124124
});
125125
return;
126126
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2023 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* /
16+
*/
17+
18+
import React from "react";
19+
import { render, screen, waitFor } from "@testing-library/react";
20+
import { MatrixClient, RoomMember, Device } from "matrix-js-sdk/src/matrix";
21+
import { UserVerificationStatus, DeviceVerificationStatus } from "matrix-js-sdk/src/crypto-api";
22+
import { mocked } from "jest-mock";
23+
24+
import * as TestUtils from "../../../test-utils";
25+
import MemberTile from "../../../../src/components/views/rooms/MemberTile";
26+
27+
describe("MemberTile", () => {
28+
let matrixClient: MatrixClient;
29+
let member: RoomMember;
30+
31+
beforeEach(() => {
32+
matrixClient = TestUtils.stubClient();
33+
mocked(matrixClient.isRoomEncrypted).mockReturnValue(true);
34+
member = new RoomMember("roomId", matrixClient.getUserId()!);
35+
});
36+
37+
it("should not display an E2EIcon when the e2E status = normal", () => {
38+
const { container } = render(<MemberTile member={member} />);
39+
40+
expect(container).toMatchSnapshot();
41+
});
42+
43+
it("should display an warning E2EIcon when the e2E status = Warning", async () => {
44+
mocked(matrixClient.getCrypto()!.getUserVerificationStatus).mockResolvedValue({
45+
isCrossSigningVerified: jest.fn().mockReturnValue(false),
46+
wasCrossSigningVerified: jest.fn().mockReturnValue(true),
47+
} as unknown as UserVerificationStatus);
48+
49+
const { container } = render(<MemberTile member={member} />);
50+
51+
await waitFor(() =>
52+
expect(screen.getByLabelText("This user has not verified all of their sessions.")).toBeInTheDocument(),
53+
);
54+
expect(container).toMatchSnapshot();
55+
});
56+
57+
it("should display an verified E2EIcon when the e2E status = Verified", async () => {
58+
// Mock all the required crypto methods
59+
const deviceMap = new Map<string, Map<string, Device>>();
60+
deviceMap.set(member.userId, new Map([["deviceId", {} as Device]]));
61+
// Return a DeviceMap = Map<string, Map<string, Device>>
62+
mocked(matrixClient.getCrypto()!.getUserDeviceInfo).mockResolvedValue(deviceMap);
63+
mocked(matrixClient.getCrypto()!.getUserVerificationStatus).mockResolvedValue({
64+
isCrossSigningVerified: jest.fn().mockReturnValue(true),
65+
} as unknown as UserVerificationStatus);
66+
mocked(matrixClient.getCrypto()!.getDeviceVerificationStatus).mockResolvedValue({
67+
crossSigningVerified: true,
68+
} as DeviceVerificationStatus);
69+
70+
const { container } = render(<MemberTile member={member} />);
71+
72+
await waitFor(() =>
73+
expect(
74+
screen.getByLabelText("You have verified this user. This user has verified all of their sessions."),
75+
).toBeInTheDocument(),
76+
);
77+
expect(container).toMatchSnapshot();
78+
});
79+
});
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`MemberTile should display an verified E2EIcon when the e2E status = Verified 1`] = `
4+
<div>
5+
<div>
6+
<div
7+
class="mx_AccessibleButton mx_EntityTile mx_EntityTile_offline_neveractive"
8+
role="button"
9+
tabindex="0"
10+
title="@userId:matrix.org (power 0)"
11+
>
12+
<div
13+
class="mx_EntityTile_avatar"
14+
>
15+
<span
16+
aria-hidden="true"
17+
class="_avatar_ylj7w_17 mx_BaseAvatar _avatar-imageless_ylj7w_56"
18+
data-color="2"
19+
data-testid="avatar-img"
20+
data-type="round"
21+
role="presentation"
22+
style="--cpd-avatar-size: 36px;"
23+
title="@userId:matrix.org"
24+
>
25+
u
26+
</span>
27+
<div
28+
aria-label="You have verified this user. This user has verified all of their sessions."
29+
class="mx_E2EIcon mx_E2EIcon_bordered mx_E2EIcon_verified"
30+
/>
31+
</div>
32+
<div
33+
class="mx_EntityTile_details"
34+
>
35+
<div
36+
class="mx_EntityTile_name"
37+
>
38+
<div
39+
class="mx_DisambiguatedProfile"
40+
>
41+
<span
42+
class=""
43+
dir="auto"
44+
>
45+
@userId:matrix.org
46+
</span>
47+
</div>
48+
</div>
49+
<div
50+
class="mx_PresenceLabel"
51+
>
52+
Offline
53+
</div>
54+
</div>
55+
</div>
56+
</div>
57+
</div>
58+
`;
59+
60+
exports[`MemberTile should display an warning E2EIcon when the e2E status = Warning 1`] = `
61+
<div>
62+
<div>
63+
<div
64+
class="mx_AccessibleButton mx_EntityTile mx_EntityTile_offline_neveractive"
65+
role="button"
66+
tabindex="0"
67+
title="@userId:matrix.org (power 0)"
68+
>
69+
<div
70+
class="mx_EntityTile_avatar"
71+
>
72+
<span
73+
aria-hidden="true"
74+
class="_avatar_ylj7w_17 mx_BaseAvatar _avatar-imageless_ylj7w_56"
75+
data-color="2"
76+
data-testid="avatar-img"
77+
data-type="round"
78+
role="presentation"
79+
style="--cpd-avatar-size: 36px;"
80+
title="@userId:matrix.org"
81+
>
82+
u
83+
</span>
84+
<div
85+
aria-label="This user has not verified all of their sessions."
86+
class="mx_E2EIcon mx_E2EIcon_bordered mx_E2EIcon_warning"
87+
/>
88+
</div>
89+
<div
90+
class="mx_EntityTile_details"
91+
>
92+
<div
93+
class="mx_EntityTile_name"
94+
>
95+
<div
96+
class="mx_DisambiguatedProfile"
97+
>
98+
<span
99+
class=""
100+
dir="auto"
101+
>
102+
@userId:matrix.org
103+
</span>
104+
</div>
105+
</div>
106+
<div
107+
class="mx_PresenceLabel"
108+
>
109+
Offline
110+
</div>
111+
</div>
112+
</div>
113+
</div>
114+
</div>
115+
`;
116+
117+
exports[`MemberTile should not display an E2EIcon when the e2E status = normal 1`] = `
118+
<div>
119+
<div>
120+
<div
121+
class="mx_AccessibleButton mx_EntityTile mx_EntityTile_offline_neveractive"
122+
role="button"
123+
tabindex="0"
124+
title="@userId:matrix.org (power 0)"
125+
>
126+
<div
127+
class="mx_EntityTile_avatar"
128+
>
129+
<span
130+
aria-hidden="true"
131+
class="_avatar_ylj7w_17 mx_BaseAvatar _avatar-imageless_ylj7w_56"
132+
data-color="2"
133+
data-testid="avatar-img"
134+
data-type="round"
135+
role="presentation"
136+
style="--cpd-avatar-size: 36px;"
137+
title="@userId:matrix.org"
138+
>
139+
u
140+
</span>
141+
</div>
142+
<div
143+
class="mx_EntityTile_details"
144+
>
145+
<div
146+
class="mx_EntityTile_name"
147+
>
148+
<div
149+
class="mx_DisambiguatedProfile"
150+
>
151+
<span
152+
class=""
153+
dir="auto"
154+
>
155+
@userId:matrix.org
156+
</span>
157+
</div>
158+
</div>
159+
<div
160+
class="mx_PresenceLabel"
161+
>
162+
Offline
163+
</div>
164+
</div>
165+
</div>
166+
</div>
167+
</div>
168+
`;

test/test-utils/test-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ export function createTestClient(): MatrixClient {
122122
downloadKeys: jest.fn(),
123123
},
124124
},
125-
getCrypto: jest.fn().mockReturnValue({ getUserDeviceInfo: jest.fn() }),
125+
getCrypto: jest.fn().mockReturnValue({
126+
getUserDeviceInfo: jest.fn(),
127+
getUserVerificationStatus: jest.fn(),
128+
getDeviceVerificationStatus: jest.fn(),
129+
}),
126130

127131
getPushActionsForEvent: jest.fn(),
128132
getRoom: jest.fn().mockImplementation((roomId) => mkStubRoom(roomId, "My room", client)),

0 commit comments

Comments
 (0)