2023-01-24 12:02:07 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-01-24 12:02:07 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
Copyright 2022 Callum Brown
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2016 OpenMarket Ltd
|
2023-01-24 12:02:07 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-01-24 12:02:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2023-02-28 08:58:23 +00:00
|
|
|
import { fireEvent, render, RenderResult } from "@testing-library/react";
|
2023-01-24 12:02:07 +00:00
|
|
|
|
|
|
|
import InteractiveAuthComponent from "../../../../src/components/structures/InteractiveAuth";
|
|
|
|
import { flushPromises, getMockClientWithEventEmitter, unmockClientPeg } from "../../../test-utils";
|
|
|
|
|
|
|
|
describe("InteractiveAuthComponent", function () {
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
generateClientSecret: jest.fn().mockReturnValue("t35tcl1Ent5ECr3T"),
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
matrixClient: mockClient,
|
|
|
|
makeRequest: jest.fn().mockResolvedValue(undefined),
|
|
|
|
onAuthFinished: jest.fn(),
|
|
|
|
};
|
2023-02-28 08:58:23 +00:00
|
|
|
const getComponent = (props = {}) => render(<InteractiveAuthComponent {...defaultProps} {...props} />);
|
2023-01-24 12:02:07 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
unmockClientPeg();
|
|
|
|
});
|
|
|
|
|
2023-02-28 08:58:23 +00:00
|
|
|
const getSubmitButton = ({ container }: RenderResult) =>
|
|
|
|
container.querySelector(".mx_AccessibleButton_kind_primary");
|
|
|
|
const getRegistrationTokenInput = ({ container }: RenderResult) =>
|
|
|
|
container.querySelector('input[name="registrationTokenField"]');
|
2023-01-24 12:02:07 +00:00
|
|
|
|
|
|
|
it("Should successfully complete a registration token flow", async () => {
|
|
|
|
const onAuthFinished = jest.fn();
|
|
|
|
const makeRequest = jest.fn().mockResolvedValue({ a: 1 });
|
|
|
|
|
|
|
|
const authData = {
|
|
|
|
session: "sess",
|
|
|
|
flows: [{ stages: ["m.login.registration_token"] }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = getComponent({ makeRequest, onAuthFinished, authData });
|
|
|
|
|
|
|
|
const registrationTokenNode = getRegistrationTokenInput(wrapper);
|
|
|
|
const submitNode = getSubmitButton(wrapper);
|
2023-02-28 08:58:23 +00:00
|
|
|
const formNode = wrapper.container.querySelector("form");
|
2023-01-24 12:02:07 +00:00
|
|
|
|
|
|
|
expect(registrationTokenNode).toBeTruthy();
|
|
|
|
expect(submitNode).toBeTruthy();
|
|
|
|
expect(formNode).toBeTruthy();
|
|
|
|
|
|
|
|
// submit should be disabled
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(submitNode).toHaveAttribute("disabled");
|
|
|
|
expect(submitNode).toHaveAttribute("aria-disabled", "true");
|
2023-01-24 12:02:07 +00:00
|
|
|
|
|
|
|
// put something in the registration token box
|
2023-02-28 08:58:23 +00:00
|
|
|
fireEvent.change(registrationTokenNode!, { target: { value: "s3kr3t" } });
|
2023-01-24 12:02:07 +00:00
|
|
|
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(getRegistrationTokenInput(wrapper)).toHaveValue("s3kr3t");
|
|
|
|
expect(submitNode).not.toHaveAttribute("disabled");
|
|
|
|
expect(submitNode).not.toHaveAttribute("aria-disabled", "true");
|
2023-01-24 12:02:07 +00:00
|
|
|
|
|
|
|
// hit enter; that should trigger a request
|
2023-02-28 08:58:23 +00:00
|
|
|
fireEvent.submit(formNode!);
|
2023-01-24 12:02:07 +00:00
|
|
|
|
|
|
|
// wait for auth request to resolve
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(makeRequest).toHaveBeenCalledTimes(1);
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(makeRequest).toHaveBeenCalledWith(
|
2023-01-24 12:02:07 +00:00
|
|
|
expect.objectContaining({
|
|
|
|
session: "sess",
|
|
|
|
type: "m.login.registration_token",
|
|
|
|
token: "s3kr3t",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(onAuthFinished).toHaveBeenCalledTimes(1);
|
|
|
|
expect(onAuthFinished).toHaveBeenCalledWith(
|
2023-01-24 12:02:07 +00:00
|
|
|
true,
|
|
|
|
{ a: 1 },
|
|
|
|
{ clientSecret: "t35tcl1Ent5ECr3T", emailSid: undefined },
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|