2022-05-06 11:25:18 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-05-06 11:25:18 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2016 OpenMarket Ltd
|
2022-05-06 11:25:18 +03:00
|
|
|
|
2024-09-09 16:57:16 +03: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-05-06 11:25:18 +03:00
|
|
|
*/
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import React from "react";
|
2023-05-08 00:12:45 +03:00
|
|
|
import { fireEvent, render, screen, act } from "@testing-library/react";
|
2023-02-10 16:00:02 +03:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2023-05-08 00:12:45 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2023-07-04 16:49:27 +03:00
|
|
|
import { MatrixError } from "matrix-js-sdk/src/matrix";
|
2022-05-06 11:25:18 +03:00
|
|
|
|
|
|
|
import InteractiveAuthDialog from "../../../../src/components/views/dialogs/InteractiveAuthDialog";
|
2023-05-08 00:12:45 +03:00
|
|
|
import { clearAllModals, flushPromises, getMockClientWithEventEmitter, unmockClientPeg } from "../../../test-utils";
|
2022-05-06 11:25:18 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("InteractiveAuthDialog", function () {
|
2023-05-08 00:12:45 +03:00
|
|
|
const homeserverUrl = "https://matrix.org";
|
|
|
|
const authUrl = "https://auth.com";
|
2022-05-06 11:25:18 +03:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2022-12-12 14:24:14 +03:00
|
|
|
generateClientSecret: jest.fn().mockReturnValue("t35tcl1Ent5ECr3T"),
|
2023-05-08 00:12:45 +03:00
|
|
|
getFallbackAuthUrl: jest.fn().mockReturnValue(authUrl),
|
|
|
|
getHomeserverUrl: jest.fn().mockReturnValue(homeserverUrl),
|
2022-05-06 11:25:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
matrixClient: mockClient,
|
|
|
|
makeRequest: jest.fn().mockResolvedValue(undefined),
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
};
|
2023-02-10 16:00:02 +03:00
|
|
|
|
|
|
|
const renderComponent = (props = {}) => render(<InteractiveAuthDialog {...defaultProps} {...props} />);
|
|
|
|
const getPasswordField = () => screen.getByLabelText("Password");
|
|
|
|
const getSubmitButton = () => screen.getByRole("button", { name: "Continue" });
|
2022-05-06 11:25:18 +03:00
|
|
|
|
2023-05-08 00:12:45 +03:00
|
|
|
beforeEach(async function () {
|
2022-05-06 11:25:18 +03:00
|
|
|
jest.clearAllMocks();
|
2023-02-24 18:28:40 +03:00
|
|
|
mockClient.credentials = { userId: null };
|
2023-05-08 00:12:45 +03:00
|
|
|
await clearAllModals();
|
2022-05-06 11:25:18 +03:00
|
|
|
});
|
|
|
|
|
2023-05-08 00:12:45 +03:00
|
|
|
afterAll(async () => {
|
2022-05-06 11:25:18 +03:00
|
|
|
unmockClientPeg();
|
2023-05-08 00:12:45 +03:00
|
|
|
await clearAllModals();
|
2022-05-06 11:25:18 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("Should successfully complete a password flow", async () => {
|
2022-05-06 11:25:18 +03:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
const makeRequest = jest.fn().mockResolvedValue({ a: 1 });
|
|
|
|
|
|
|
|
mockClient.credentials = { userId: "@user:id" };
|
|
|
|
const authData = {
|
|
|
|
session: "sess",
|
2022-12-12 14:24:14 +03:00
|
|
|
flows: [{ stages: ["m.login.password"] }],
|
2022-05-06 11:25:18 +03:00
|
|
|
};
|
|
|
|
|
2023-02-10 16:00:02 +03:00
|
|
|
renderComponent({ makeRequest, onFinished, authData });
|
2022-05-06 11:25:18 +03:00
|
|
|
|
2023-02-10 16:00:02 +03:00
|
|
|
const passwordField = getPasswordField();
|
|
|
|
const submitButton = getSubmitButton();
|
2022-05-06 11:25:18 +03:00
|
|
|
|
2023-02-10 16:00:02 +03:00
|
|
|
expect(passwordField).toBeTruthy();
|
|
|
|
expect(submitButton).toBeTruthy();
|
2022-05-06 11:25:18 +03:00
|
|
|
|
|
|
|
// submit should be disabled
|
2023-02-10 16:00:02 +03:00
|
|
|
expect(submitButton).toBeDisabled();
|
2022-05-06 11:25:18 +03:00
|
|
|
|
|
|
|
// put something in the password box
|
2023-02-10 16:00:02 +03:00
|
|
|
await userEvent.type(passwordField, "s3kr3t");
|
2022-05-06 11:25:18 +03:00
|
|
|
|
2023-02-10 16:00:02 +03:00
|
|
|
expect(submitButton).not.toBeDisabled();
|
2022-05-06 11:25:18 +03:00
|
|
|
|
|
|
|
// hit enter; that should trigger a request
|
2023-02-10 16:00:02 +03:00
|
|
|
await userEvent.click(submitButton);
|
2022-05-06 11:25:18 +03:00
|
|
|
|
|
|
|
// wait for auth request to resolve
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(makeRequest).toHaveBeenCalledTimes(1);
|
2023-03-01 18:23:35 +03:00
|
|
|
expect(makeRequest).toHaveBeenCalledWith(
|
2022-12-12 14:24:14 +03:00
|
|
|
expect.objectContaining({
|
|
|
|
session: "sess",
|
|
|
|
type: "m.login.password",
|
|
|
|
password: "s3kr3t",
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: "@user:id",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2022-05-06 11:25:18 +03:00
|
|
|
|
2023-03-01 18:23:35 +03:00
|
|
|
expect(onFinished).toHaveBeenCalledTimes(1);
|
|
|
|
expect(onFinished).toHaveBeenCalledWith(true, { a: 1 });
|
2022-05-06 11:25:18 +03:00
|
|
|
});
|
2023-05-08 00:12:45 +03:00
|
|
|
|
|
|
|
describe("SSO flow", () => {
|
|
|
|
it("should close on cancel", () => {
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const makeRequest = jest.fn().mockResolvedValue({ a: 1 });
|
|
|
|
|
|
|
|
mockClient.credentials = { userId: "@user:id" };
|
|
|
|
const authData = {
|
|
|
|
session: "sess",
|
|
|
|
flows: [{ stages: ["m.login.sso"] }],
|
|
|
|
};
|
|
|
|
|
|
|
|
renderComponent({ makeRequest, onFinished, authData });
|
|
|
|
|
|
|
|
expect(screen.getByText("To continue, use Single Sign On to prove your identity.")).toBeInTheDocument();
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByText("Cancel"));
|
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalledWith(false, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should complete an sso flow", async () => {
|
|
|
|
jest.spyOn(global.window, "addEventListener");
|
|
|
|
// @ts-ignore
|
|
|
|
jest.spyOn(global.window, "open").mockImplementation(() => {});
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const successfulResult = { test: 1 };
|
|
|
|
const makeRequest = jest
|
|
|
|
.fn()
|
2023-07-05 13:53:22 +03:00
|
|
|
.mockRejectedValueOnce(new MatrixError({ flows: [{ stages: ["m.login.sso"] }] }, 401))
|
2023-05-08 00:12:45 +03:00
|
|
|
.mockResolvedValue(successfulResult);
|
|
|
|
|
|
|
|
mockClient.credentials = { userId: "@user:id" };
|
|
|
|
const authData = {
|
|
|
|
session: "sess",
|
|
|
|
flows: [{ stages: ["m.login.sso"] }],
|
|
|
|
};
|
|
|
|
|
|
|
|
renderComponent({ makeRequest, onFinished, authData });
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(screen.getByText("To continue, use Single Sign On to prove your identity.")).toBeInTheDocument();
|
|
|
|
fireEvent.click(screen.getByText("Single Sign On"));
|
|
|
|
|
|
|
|
// no we're on the sso auth screen
|
|
|
|
expect(screen.getByText("Click the button below to confirm your identity.")).toBeInTheDocument();
|
|
|
|
|
|
|
|
// launch sso
|
|
|
|
fireEvent.click(screen.getByText("Confirm"));
|
|
|
|
expect(global.window.open).toHaveBeenCalledWith(authUrl, "_blank");
|
|
|
|
|
|
|
|
const onWindowReceiveMessageCall = mocked(window.addEventListener).mock.calls.find(
|
|
|
|
(args) => args[0] === "message",
|
|
|
|
);
|
|
|
|
expect(onWindowReceiveMessageCall).toBeTruthy();
|
|
|
|
// get the handle from SSO auth component
|
|
|
|
// so we can pretend sso auth was completed
|
|
|
|
const onWindowReceiveMessage = onWindowReceiveMessageCall![1];
|
|
|
|
|
|
|
|
// complete sso successfully
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
onWindowReceiveMessage({ data: "authDone", origin: homeserverUrl });
|
|
|
|
});
|
|
|
|
|
|
|
|
// expect(makeRequest).toHaveBeenCalledWith({ session: authData.session })
|
|
|
|
|
|
|
|
// spinner displayed
|
|
|
|
expect(screen.getByRole("progressbar")).toBeInTheDocument();
|
|
|
|
// cancel/confirm buttons hidden while request in progress
|
|
|
|
expect(screen.queryByText("Confirm")).not.toBeInTheDocument();
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// nothing in progress
|
|
|
|
expect(screen.queryByRole("progressbar")).not.toBeInTheDocument();
|
|
|
|
|
|
|
|
// auth completed, now make the request again with auth
|
|
|
|
fireEvent.click(screen.getByText("Confirm"));
|
|
|
|
// loading while making request
|
|
|
|
expect(screen.getByRole("progressbar")).toBeInTheDocument();
|
|
|
|
|
|
|
|
expect(makeRequest).toHaveBeenCalledTimes(2);
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalledWith(true, successfulResult);
|
|
|
|
});
|
|
|
|
});
|
2022-05-06 11:25:18 +03:00
|
|
|
});
|