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

Commit

Permalink
Add test for createCrossSigning
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Oct 10, 2024
1 parent 8b455e0 commit 94fd783
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
96 changes: 96 additions & 0 deletions test/CreateCrossSigning-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2018-2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/

import { MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";

import { createCrossSigning } from "../src/CreateCrossSigning";
import { createTestClient } from "./test-utils";
import Modal from "../src/Modal";

describe("CreateCrossSigning", () => {
let client: MatrixClient;

beforeEach(() => {
client = createTestClient();
});

afterEach(() => {});

it("should call bootstrapCrossSigning with an authUploadDeviceSigningKeys function", async () => {
await createCrossSigning(client, false, "password");

expect(client.getCrypto()?.bootstrapCrossSigning).toHaveBeenCalledWith({
authUploadDeviceSigningKeys: expect.any(Function),
});
});

it("should upload with password auth if possible", async () => {
client.uploadDeviceSigningKeys = jest.fn().mockRejectedValueOnce(
new MatrixError({
flows: [
{
stages: ["m.login.password"],
},
],
}),
);

await createCrossSigning(client, false, "password");

const { authUploadDeviceSigningKeys } = mocked(client.getCrypto()!).bootstrapCrossSigning.mock.calls[0][0];

const makeRequest = jest.fn();
await authUploadDeviceSigningKeys!(makeRequest);
expect(makeRequest).toHaveBeenCalledWith({
type: "m.login.password",
identifier: {
type: "m.id.user",
user: client.getUserId(),
},
password: "password",
});
});

it("should attempt to upload keys without auth if using token login", async () => {
await createCrossSigning(client, true, undefined);

const { authUploadDeviceSigningKeys } = mocked(client.getCrypto()!).bootstrapCrossSigning.mock.calls[0][0];

const makeRequest = jest.fn();
await authUploadDeviceSigningKeys!(makeRequest);
expect(makeRequest).toHaveBeenCalledWith({});
});

it("should prompt user if password upload not possible", async () => {
const createDialog = jest.spyOn(Modal, "createDialog").mockReturnValue({
finished: Promise.resolve([true]),
close: jest.fn(),
});
jest.mock;

client.uploadDeviceSigningKeys = jest.fn().mockRejectedValueOnce(
new MatrixError({
flows: [
{
stages: ["dummy.mystery_flow_nobody_knows"],
},
],
}),
);

await createCrossSigning(client, false, "password");

const { authUploadDeviceSigningKeys } = mocked(client.getCrypto()!).bootstrapCrossSigning.mock.calls[0][0];

const makeRequest = jest.fn();
await authUploadDeviceSigningKeys!(makeRequest);
expect(makeRequest).not.toHaveBeenCalledWith();
expect(createDialog).toHaveBeenCalled();
});
});
1 change: 1 addition & 0 deletions test/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function createTestClient(): MatrixClient {
isEncryptionEnabledInRoom: jest.fn(),
getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]),
setDeviceIsolationMode: jest.fn(),
bootstrapCrossSigning: jest.fn(),
}),

getPushActionsForEvent: jest.fn(),
Expand Down

0 comments on commit 94fd783

Please sign in to comment.