Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 60 additions & 29 deletions src/app/hooks/useDeviceVerificationStatus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ vi.mock('@sentry/react', () => sentry);
const USER_ID = '@me:example.org';
const DEVICE_ID = 'DEVICEONE';

const getDeviceVerificationStatus =
vi.fn<
(
userId: string,
deviceId: string
) => Promise<{ crossSigningVerified: boolean; localVerified: boolean } | null>
>();
const deviceVerificationStatus = (status: {
crossSigningVerified: boolean;
localVerified: boolean;
signedByOwner?: boolean;
}) => ({ signedByOwner: false, ...status });

const getDeviceVerificationStatus = vi.fn<
(
userId: string,
deviceId: string
) => Promise<{
crossSigningVerified: boolean;
localVerified: boolean;
signedByOwner: boolean;
} | null>
>();
const crypto = { getDeviceVerificationStatus } as unknown as CryptoApi;

const createWrapper = () => {
Expand All @@ -71,10 +80,9 @@ const createWrapper = () => {
describe('useDeviceVerificationStatus', () => {
beforeEach(() => {
getDeviceVerificationStatus.mockReset();
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: true,
localVerified: false,
});
getDeviceVerificationStatus.mockResolvedValue(
deviceVerificationStatus({ crossSigningVerified: true, localVerified: false })
);
sentry.addBreadcrumb.mockClear();
sentry.metrics.count.mockClear();
});
Expand All @@ -89,11 +97,26 @@ describe('useDeviceVerificationStatus', () => {
});

it('reports a locally verified device as verified', async () => {
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: true,
getDeviceVerificationStatus.mockResolvedValue(
deviceVerificationStatus({ crossSigningVerified: false, localVerified: true })
);

const { result } = renderHook(() => useDeviceVerificationStatus(crypto, USER_ID, DEVICE_ID), {
wrapper: createWrapper(),
});

await waitFor(() => expect(result.current).toBe(VerificationStatus.Verified));
});

it('reports an owner-signed device as verified', async () => {
getDeviceVerificationStatus.mockResolvedValue(
deviceVerificationStatus({
crossSigningVerified: false,
localVerified: false,
signedByOwner: true,
})
);

const { result } = renderHook(() => useDeviceVerificationStatus(crypto, USER_ID, DEVICE_ID), {
wrapper: createWrapper(),
});
Expand Down Expand Up @@ -194,10 +217,9 @@ describe('useDeviceVerificationStatus', () => {
await waitFor(() => expect(result.current).toBe(VerificationStatus.Verified));
expect(getDeviceVerificationStatus).toHaveBeenCalledTimes(1);

getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: false,
});
getDeviceVerificationStatus.mockResolvedValue(
deviceVerificationStatus({ crossSigningVerified: false, localVerified: false })
);
await act(async () => {
mockMx.emit(event, ...(args as never[]));
});
Expand All @@ -212,10 +234,9 @@ describe('useDeviceVerificationStatus', () => {
});

await waitFor(() => expect(result.current).toBe(VerificationStatus.Verified));
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: false,
});
getDeviceVerificationStatus.mockResolvedValue(
deviceVerificationStatus({ crossSigningVerified: false, localVerified: false })
);

await act(async () => {
mockMx.emit(CryptoEvent.KeysChanged, ...([{}] as never[]));
Expand Down Expand Up @@ -249,9 +270,15 @@ describe('useDeviceVerificationStatus', () => {
(
userId: string,
deviceId: string
) => Promise<{ crossSigningVerified: boolean; localVerified: boolean } | null>
) => Promise<{
crossSigningVerified: boolean;
localVerified: boolean;
signedByOwner: boolean;
} | null>
>()
.mockResolvedValue({ crossSigningVerified: true, localVerified: false });
.mockResolvedValue(
deviceVerificationStatus({ crossSigningVerified: true, localVerified: false })
);
const crypto2 = {
getDeviceVerificationStatus: getDeviceVerificationStatus2,
} as unknown as CryptoApi;
Expand Down Expand Up @@ -290,10 +317,9 @@ describe('useDeviceVerificationStatus', () => {
await waitFor(() => expect(resultB.current).toBe(VerificationStatus.Verified));
expect(getDeviceVerificationStatus).toHaveBeenCalledTimes(2);

getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: false,
});
getDeviceVerificationStatus.mockResolvedValue(
deviceVerificationStatus({ crossSigningVerified: false, localVerified: false })
);

await act(async () => {
mockMx.emit(CryptoEvent.UserTrustStatusChanged, ...([USER_B, {}] as never[]));
Expand Down Expand Up @@ -335,7 +361,12 @@ describe('useUnverifiedDeviceCount', () => {

it('counts only devices that are not cross-signing verified', async () => {
getDeviceVerificationStatus.mockImplementation((_userId: string, deviceId: string) =>
Promise.resolve({ crossSigningVerified: deviceId === 'VERIFIED', localVerified: false })
Promise.resolve(
deviceVerificationStatus({
crossSigningVerified: deviceId === 'VERIFIED',
localVerified: false,
})
)
);

const { result } = renderHook(
Expand Down
3 changes: 1 addition & 2 deletions src/app/utils/matrix-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ export const verifiedDevice = async (

if (!status) return null;

const verified = status.crossSigningVerified || status.localVerified;
return verified;
return !!(status.crossSigningVerified || status.localVerified || status.signedByOwner);
};
Loading