2022-04-14 02:05:08 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-14 02:05:08 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-04-14 02:05:08 +08:00
|
|
|
*/
|
|
|
|
|
2022-12-17 01:10:26 +08:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2024-10-10 22:08:43 +08:00
|
|
|
import fetchMockJest from "fetch-mock-jest";
|
2022-12-17 01:10:26 +08:00
|
|
|
|
2022-04-14 02:05:08 +08:00
|
|
|
import { advanceDateAndTime, stubClient } from "./test-utils";
|
2022-12-17 01:10:26 +08:00
|
|
|
import { IMatrixClientPeg, MatrixClientPeg as peg } from "../src/MatrixClientPeg";
|
|
|
|
import SettingsStore from "../src/settings/SettingsStore";
|
2023-03-28 02:03:17 +08:00
|
|
|
import { SettingLevel } from "../src/settings/SettingLevel";
|
2022-04-14 02:05:08 +08:00
|
|
|
|
2022-04-19 20:44:15 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2023-03-31 17:08:45 +08:00
|
|
|
const PegClass = Object.getPrototypeOf(peg).constructor;
|
|
|
|
|
2022-04-14 02:05:08 +08:00
|
|
|
describe("MatrixClientPeg", () => {
|
2023-03-31 17:08:45 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
// stub out Logger.log which gets called a lot and clutters up the test output
|
|
|
|
jest.spyOn(logger, "log").mockImplementation(() => {});
|
|
|
|
});
|
|
|
|
|
2022-04-14 02:05:08 +08:00
|
|
|
afterEach(() => {
|
|
|
|
localStorage.clear();
|
2022-04-19 20:44:15 +08:00
|
|
|
jest.restoreAllMocks();
|
2024-01-18 19:18:55 +08:00
|
|
|
|
|
|
|
// some of the tests assign `MatrixClientPeg.matrixClient`: clear it, to prevent leakage between tests
|
|
|
|
peg.unset();
|
2022-04-14 02:05:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("setJustRegisteredUserId", () => {
|
2024-10-10 22:08:43 +08:00
|
|
|
stubClient();
|
2022-04-14 02:05:08 +08:00
|
|
|
(peg as any).matrixClient = peg.get();
|
2022-07-13 13:56:36 +08:00
|
|
|
peg.setJustRegisteredUserId("@userId:matrix.org");
|
2023-06-06 01:12:23 +08:00
|
|
|
expect(peg.safeGet().credentials.userId).toBe("@userId:matrix.org");
|
2022-04-14 02:05:08 +08:00
|
|
|
expect(peg.currentUserIsJustRegistered()).toBe(true);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
|
2022-04-19 20:44:15 +08:00
|
|
|
advanceDateAndTime(1 * 60 * 60 * 1000 + 1);
|
2022-04-14 02:05:08 +08:00
|
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
|
|
|
|
advanceDateAndTime(24 * 60 * 60 * 1000);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("setJustRegisteredUserId(null)", () => {
|
2024-10-10 22:08:43 +08:00
|
|
|
stubClient();
|
2022-04-14 02:05:08 +08:00
|
|
|
(peg as any).matrixClient = peg.get();
|
|
|
|
peg.setJustRegisteredUserId(null);
|
|
|
|
expect(peg.currentUserIsJustRegistered()).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
|
2022-04-19 20:44:15 +08:00
|
|
|
advanceDateAndTime(1 * 60 * 60 * 1000 + 1);
|
2022-04-14 02:05:08 +08:00
|
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
|
|
|
|
});
|
2022-12-17 01:10:26 +08:00
|
|
|
|
|
|
|
describe(".start", () => {
|
|
|
|
let testPeg: IMatrixClientPeg;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// instantiate a MatrixClientPegClass instance, with a new MatrixClient
|
|
|
|
testPeg = new PegClass();
|
2024-10-10 22:08:43 +08:00
|
|
|
fetchMockJest.get("http://example.com/_matrix/client/versions", {});
|
2022-12-17 01:10:26 +08:00
|
|
|
testPeg.replaceUsingCreds({
|
|
|
|
accessToken: "SEKRET",
|
|
|
|
homeserverUrl: "http://example.com",
|
|
|
|
userId: "@user:example.com",
|
|
|
|
deviceId: "TEST_DEVICE_ID",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-02-02 20:20:13 +08:00
|
|
|
it("should initialise the rust crypto library by default", async () => {
|
|
|
|
const mockSetValue = jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined);
|
|
|
|
|
2024-10-10 22:08:43 +08:00
|
|
|
const mockInitCrypto = jest.spyOn(testPeg.safeGet(), "initCrypto").mockResolvedValue(undefined);
|
|
|
|
const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined);
|
|
|
|
|
2024-06-05 16:52:28 +08:00
|
|
|
const cryptoStoreKey = new Uint8Array([1, 2, 3, 4]);
|
|
|
|
await testPeg.start({ rustCryptoStoreKey: cryptoStoreKey });
|
2024-10-10 22:08:43 +08:00
|
|
|
expect(mockInitCrypto).not.toHaveBeenCalled();
|
|
|
|
expect(mockInitRustCrypto).toHaveBeenCalledWith({ storageKey: cryptoStoreKey });
|
2024-02-02 20:20:13 +08:00
|
|
|
|
|
|
|
// we should have stashed the setting in the settings store
|
|
|
|
expect(mockSetValue).toHaveBeenCalledWith("feature_rust_crypto", null, SettingLevel.DEVICE, true);
|
2022-12-17 01:10:26 +08:00
|
|
|
});
|
|
|
|
|
2024-06-24 17:14:42 +08:00
|
|
|
it("Should migrate existing login", async () => {
|
2023-03-28 02:03:17 +08:00
|
|
|
const mockSetValue = jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined);
|
2024-10-10 22:08:43 +08:00
|
|
|
const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined);
|
2022-12-17 01:10:26 +08:00
|
|
|
|
|
|
|
await testPeg.start();
|
2024-10-10 22:08:43 +08:00
|
|
|
expect(mockInitRustCrypto).toHaveBeenCalledTimes(1);
|
2023-03-28 02:03:17 +08:00
|
|
|
|
|
|
|
// we should have stashed the setting in the settings store
|
2024-06-24 17:14:42 +08:00
|
|
|
expect(mockSetValue).toHaveBeenCalledWith("feature_rust_crypto", null, SettingLevel.DEVICE, true);
|
2024-01-31 23:52:23 +08:00
|
|
|
});
|
2022-12-17 01:10:26 +08:00
|
|
|
});
|
2022-04-14 02:05:08 +08:00
|
|
|
});
|