|
| 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 | +import { mocked, Mocked } from "jest-mock"; |
| 18 | +import { IBootstrapCrossSigningOpts } from "matrix-js-sdk/src/crypto"; |
| 19 | +import { MatrixClient } from "matrix-js-sdk/src/matrix"; |
| 20 | + |
| 21 | +import { SdkContextClass } from "../../src/contexts/SDKContext"; |
| 22 | +import { accessSecretStorage } from "../../src/SecurityManager"; |
| 23 | +import { SetupEncryptionStore } from "../../src/stores/SetupEncryptionStore"; |
| 24 | +import { stubClient } from "../test-utils"; |
| 25 | + |
| 26 | +jest.mock("../../src/SecurityManager", () => ({ |
| 27 | + accessSecretStorage: jest.fn(), |
| 28 | +})); |
| 29 | + |
| 30 | +describe("SetupEncryptionStore", () => { |
| 31 | + const cachedPassword = "p4assword"; |
| 32 | + let client: Mocked<MatrixClient>; |
| 33 | + let setupEncryptionStore: SetupEncryptionStore; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + client = mocked(stubClient()); |
| 37 | + setupEncryptionStore = new SetupEncryptionStore(); |
| 38 | + SdkContextClass.instance.accountPasswordStore.setPassword(cachedPassword); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + SdkContextClass.instance.accountPasswordStore.clearPassword(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("resetConfirm should work with a cached account password", async () => { |
| 46 | + const makeRequest = jest.fn(); |
| 47 | + client.hasSecretStorageKey.mockResolvedValue(true); |
| 48 | + client.bootstrapCrossSigning.mockImplementation(async (opts: IBootstrapCrossSigningOpts) => { |
| 49 | + await opts.authUploadDeviceSigningKeys(makeRequest); |
| 50 | + }); |
| 51 | + mocked(accessSecretStorage).mockImplementation(async (func) => { |
| 52 | + await func(); |
| 53 | + }); |
| 54 | + |
| 55 | + await setupEncryptionStore.resetConfirm(); |
| 56 | + |
| 57 | + expect(mocked(accessSecretStorage)).toHaveBeenCalledWith(expect.any(Function), true); |
| 58 | + expect(makeRequest).toHaveBeenCalledWith({ |
| 59 | + identifier: { |
| 60 | + type: "m.id.user", |
| 61 | + user: "@userId:matrix.org", |
| 62 | + }, |
| 63 | + password: cachedPassword, |
| 64 | + type: "m.login.password", |
| 65 | + user: "@userId:matrix.org", |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments