2022-11-18 15:36:20 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-11-18 15:36:20 +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-11-18 15:36:20 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { act, render, RenderResult, screen, waitFor } from "jest-matrix-react";
|
2024-06-06 16:57:28 +08:00
|
|
|
import { DEVICE_CODE_SCOPE, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { CryptoApi } from "matrix-js-sdk/src/crypto-api";
|
2024-02-02 19:43:59 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2024-06-06 16:57:28 +08:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2022-11-18 15:36:20 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import UnwrappedUserMenu from "../../../../src/components/structures/UserMenu";
|
|
|
|
import { stubClient, wrapInSdkContext } from "../../../test-utils";
|
2022-11-18 15:36:20 +08:00
|
|
|
import {
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecording,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
2024-10-15 21:57:26 +08:00
|
|
|
} from "../../../../src/voice-broadcast";
|
2022-11-18 15:36:20 +08:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "../../voice-broadcast/utils/test-utils";
|
2022-12-27 15:39:26 +08:00
|
|
|
import { TestSdkContext } from "../../TestSdkContext";
|
2024-10-15 21:57:26 +08:00
|
|
|
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import LogoutDialog from "../../../../src/components/views/dialogs/LogoutDialog";
|
|
|
|
import Modal from "../../../../src/Modal";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { Features } from "../../../../src/settings/Settings";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
|
|
|
import { mockOpenIdConfiguration } from "../../../test-utils/oidc";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
|
|
|
import { UserTab } from "../../../../src/components/views/dialogs/UserTab";
|
2022-11-18 15:36:20 +08:00
|
|
|
|
|
|
|
describe("<UserMenu>", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let renderResult: RenderResult;
|
2022-12-27 15:39:26 +08:00
|
|
|
let sdkContext: TestSdkContext;
|
2022-11-18 15:36:20 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-27 15:39:26 +08:00
|
|
|
sdkContext = new TestSdkContext();
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|
|
|
|
|
2024-02-02 19:43:59 +08:00
|
|
|
describe("<UserMenu> when video broadcast", () => {
|
|
|
|
let voiceBroadcastInfoEvent: MatrixEvent;
|
|
|
|
let voiceBroadcastRecording: VoiceBroadcastRecording;
|
|
|
|
let voiceBroadcastRecordingsStore: VoiceBroadcastRecordingsStore;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client = stubClient();
|
|
|
|
voiceBroadcastInfoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
"!room:example.com",
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getUserId() || "",
|
|
|
|
client.getDeviceId() || "",
|
|
|
|
);
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|
|
|
|
|
2024-02-02 19:43:59 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
voiceBroadcastRecordingsStore = new VoiceBroadcastRecordingsStore();
|
|
|
|
sdkContext._VoiceBroadcastRecordingsStore = voiceBroadcastRecordingsStore;
|
|
|
|
|
|
|
|
voiceBroadcastRecording = new VoiceBroadcastRecording(voiceBroadcastInfoEvent, client);
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|
|
|
|
|
2024-02-02 19:43:59 +08:00
|
|
|
describe("when rendered", () => {
|
2022-11-18 15:36:20 +08:00
|
|
|
beforeEach(() => {
|
2024-02-02 19:43:59 +08:00
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|
|
|
|
|
2024-02-02 19:43:59 +08:00
|
|
|
it("should render as expected", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|
|
|
|
|
2024-02-02 19:43:59 +08:00
|
|
|
describe("and a live voice broadcast starts", () => {
|
2022-11-18 15:36:20 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
act(() => {
|
2024-02-02 19:43:59 +08:00
|
|
|
voiceBroadcastRecordingsStore.setCurrent(voiceBroadcastRecording);
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-02-02 19:43:59 +08:00
|
|
|
it("should render the live voice broadcast avatar addon", () => {
|
|
|
|
expect(renderResult.queryByTestId("user-menu-live-vb")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the broadcast ends", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
act(() => {
|
|
|
|
voiceBroadcastRecordingsStore.clearCurrent();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not render the live voice broadcast avatar addon", () => {
|
|
|
|
expect(renderResult.queryByTestId("user-menu-live-vb")).not.toBeInTheDocument();
|
|
|
|
});
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-02-02 19:43:59 +08:00
|
|
|
|
|
|
|
describe("<UserMenu> logout", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should logout directly if no crypto", async () => {
|
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
|
|
|
|
|
|
|
mocked(client.getRooms).mockReturnValue([
|
|
|
|
{
|
|
|
|
roomId: "!room0",
|
|
|
|
} as unknown as Room,
|
|
|
|
{
|
|
|
|
roomId: "!room1",
|
|
|
|
} as unknown as Room,
|
|
|
|
]);
|
|
|
|
jest.spyOn(client, "getCrypto").mockReturnValue(undefined);
|
|
|
|
|
|
|
|
const spy = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
screen.getByRole("button", { name: /User menu/i }).click();
|
2024-08-07 01:22:02 +08:00
|
|
|
(await screen.findByRole("menuitem", { name: /Sign out/i })).click();
|
2024-02-02 19:43:59 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(spy).toHaveBeenCalledWith({ action: "logout" });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should logout directly if no encrypted rooms", async () => {
|
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
|
|
|
|
|
|
|
mocked(client.getRooms).mockReturnValue([
|
|
|
|
{
|
|
|
|
roomId: "!room0",
|
|
|
|
} as unknown as Room,
|
|
|
|
{
|
|
|
|
roomId: "!room1",
|
|
|
|
} as unknown as Room,
|
|
|
|
]);
|
|
|
|
const crypto = client.getCrypto()!;
|
|
|
|
|
|
|
|
jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockResolvedValue(false);
|
|
|
|
|
|
|
|
const spy = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
screen.getByRole("button", { name: /User menu/i }).click();
|
2024-08-07 01:22:02 +08:00
|
|
|
(await screen.findByRole("menuitem", { name: /Sign out/i })).click();
|
2024-02-02 19:43:59 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(spy).toHaveBeenCalledWith({ action: "logout" });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show dialog if some encrypted rooms", async () => {
|
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
|
|
|
|
|
|
|
mocked(client.getRooms).mockReturnValue([
|
|
|
|
{
|
|
|
|
roomId: "!room0",
|
|
|
|
} as unknown as Room,
|
|
|
|
{
|
|
|
|
roomId: "!room1",
|
|
|
|
} as unknown as Room,
|
|
|
|
]);
|
|
|
|
const crypto = client.getCrypto()!;
|
|
|
|
|
|
|
|
jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockImplementation(async (roomId: string) => {
|
|
|
|
return roomId === "!room0";
|
|
|
|
});
|
|
|
|
|
|
|
|
const spy = jest.spyOn(Modal, "createDialog");
|
|
|
|
screen.getByRole("button", { name: /User menu/i }).click();
|
2024-08-07 01:22:02 +08:00
|
|
|
(await screen.findByRole("menuitem", { name: /Sign out/i })).click();
|
2024-02-02 19:43:59 +08:00
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(spy).toHaveBeenCalledWith(LogoutDialog);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-06-06 16:57:28 +08:00
|
|
|
|
|
|
|
it("should render 'Link new device' button in OIDC native mode", async () => {
|
|
|
|
sdkContext.client = stubClient();
|
|
|
|
mocked(sdkContext.client.getAuthIssuer).mockResolvedValue({ issuer: "https://issuer/" });
|
|
|
|
const openIdMetadata = mockOpenIdConfiguration("https://issuer/");
|
|
|
|
openIdMetadata.grant_types_supported.push(DEVICE_CODE_SCOPE);
|
|
|
|
fetchMock.get("https://issuer/.well-known/openid-configuration", openIdMetadata);
|
|
|
|
fetchMock.get("https://issuer/jwks", {
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
keys: [],
|
|
|
|
});
|
|
|
|
mocked(sdkContext.client.getVersions).mockResolvedValue({
|
|
|
|
versions: [],
|
|
|
|
unstable_features: {
|
|
|
|
"org.matrix.msc4108": true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
mocked(sdkContext.client.waitForClientWellKnown).mockResolvedValue({});
|
|
|
|
mocked(sdkContext.client.getCrypto).mockReturnValue({
|
|
|
|
isCrossSigningReady: jest.fn().mockResolvedValue(true),
|
|
|
|
exportSecretsBundle: jest.fn().mockResolvedValue({}),
|
|
|
|
} as unknown as CryptoApi);
|
|
|
|
await SettingsStore.setValue(Features.OidcNativeFlow, null, SettingLevel.DEVICE, true);
|
|
|
|
const spy = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
|
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
render(<UserMenu isPanelCollapsed={true} />);
|
|
|
|
|
|
|
|
screen.getByRole("button", { name: /User menu/i }).click();
|
|
|
|
await expect(screen.findByText("Link new device")).resolves.toBeInTheDocument();
|
|
|
|
|
|
|
|
// Assert the QR code is shown directly
|
|
|
|
screen.getByRole("menuitem", { name: "Link new device" }).click();
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(spy).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewUserSettings,
|
|
|
|
initialTabId: UserTab.SessionManager,
|
|
|
|
props: { showMsc4108QrCode: true },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-11-18 15:36:20 +08:00
|
|
|
});
|