2019-02-05 12:26:45 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2022-05-16 11:23:51 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2019-02-05 12:26:45 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import React from "react";
|
2022-11-18 12:22:43 +03:00
|
|
|
import { fireEvent, render, screen, waitForElementToBeRemoved } from "@testing-library/react";
|
2023-07-05 13:53:22 +03:00
|
|
|
import { createClient, MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix";
|
2022-12-12 14:24:14 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2022-11-18 12:22:43 +03:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2021-12-08 12:34:24 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import SdkConfig, { DEFAULTS } from "../../../../src/SdkConfig";
|
2022-11-18 12:22:43 +03:00
|
|
|
import { mkServerConfig, mockPlatformPeg, unmockPlatformPeg } from "../../../test-utils";
|
2022-03-03 00:09:24 +03:00
|
|
|
import Registration from "../../../../src/components/structures/auth/Registration";
|
2021-12-08 12:34:24 +03:00
|
|
|
|
2023-07-05 13:53:22 +03:00
|
|
|
jest.mock("matrix-js-sdk/src/matrix", () => ({
|
|
|
|
...jest.requireActual("matrix-js-sdk/src/matrix"),
|
|
|
|
createClient: jest.fn(),
|
|
|
|
}));
|
2021-12-08 12:34:24 +03:00
|
|
|
jest.useFakeTimers();
|
2019-02-05 12:26:45 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("Registration", function () {
|
2022-11-18 12:22:43 +03:00
|
|
|
const registerRequest = jest.fn();
|
|
|
|
const mockClient = mocked({
|
|
|
|
registerRequest,
|
|
|
|
loginFlows: jest.fn(),
|
|
|
|
} as unknown as MatrixClient);
|
2019-02-05 12:26:45 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
beforeEach(function () {
|
2022-03-18 19:12:36 +03:00
|
|
|
SdkConfig.put({
|
2022-02-21 19:57:44 +03:00
|
|
|
...DEFAULTS,
|
|
|
|
disable_custom_urls: true,
|
|
|
|
});
|
2022-12-12 14:24:14 +03:00
|
|
|
mockClient.registerRequest.mockRejectedValueOnce(
|
|
|
|
new MatrixError(
|
|
|
|
{
|
|
|
|
flows: [{ stages: [] }],
|
|
|
|
},
|
|
|
|
401,
|
|
|
|
),
|
|
|
|
);
|
2022-11-18 12:22:43 +03:00
|
|
|
mockClient.loginFlows.mockClear().mockResolvedValue({ flows: [{ type: "m.login.password" }] });
|
2022-12-12 14:24:14 +03:00
|
|
|
mocked(createClient).mockImplementation((opts) => {
|
2022-11-18 12:22:43 +03:00
|
|
|
mockClient.idBaseUrl = opts.idBaseUrl;
|
|
|
|
mockClient.baseUrl = opts.baseUrl;
|
|
|
|
return mockClient;
|
|
|
|
});
|
|
|
|
fetchMock.get("https://matrix.org/_matrix/client/versions", {
|
|
|
|
unstable_features: {},
|
|
|
|
versions: [],
|
|
|
|
});
|
|
|
|
mockPlatformPeg({
|
|
|
|
startSingleSignOn: jest.fn(),
|
|
|
|
});
|
2019-02-05 12:26:45 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
afterEach(function () {
|
2022-11-18 12:22:43 +03:00
|
|
|
fetchMock.restore();
|
2023-04-26 12:36:00 +03:00
|
|
|
SdkConfig.reset(); // we touch the config, so clean up
|
2022-11-18 12:22:43 +03:00
|
|
|
unmockPlatformPeg();
|
2019-02-05 12:26:45 +03:00
|
|
|
});
|
|
|
|
|
2022-05-16 11:23:51 +03:00
|
|
|
const defaultProps = {
|
2022-12-12 14:24:14 +03:00
|
|
|
defaultDeviceDisplayName: "test-device-display-name",
|
2022-05-16 11:23:51 +03:00
|
|
|
makeRegistrationUrl: jest.fn(),
|
|
|
|
onLoggedIn: jest.fn(),
|
|
|
|
onLoginClick: jest.fn(),
|
|
|
|
onServerConfigChange: jest.fn(),
|
|
|
|
};
|
2022-11-18 12:22:43 +03:00
|
|
|
|
|
|
|
function getRawComponent(hsUrl = "https://matrix.org", isUrl = "https://vector.im") {
|
2022-12-12 14:24:14 +03:00
|
|
|
return <Registration {...defaultProps} serverConfig={mkServerConfig(hsUrl, isUrl)} />;
|
2022-11-18 12:22:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getComponent(hsUrl?: string, isUrl?: string) {
|
|
|
|
return render(getRawComponent(hsUrl, isUrl));
|
2019-02-05 12:26:45 +03:00
|
|
|
}
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should show server picker", async function () {
|
2022-11-18 12:22:43 +03:00
|
|
|
const { container } = getComponent();
|
|
|
|
expect(container.querySelector(".mx_ServerPicker")).toBeTruthy();
|
2019-02-05 12:26:45 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should show form when custom URLs disabled", async function () {
|
2022-11-18 12:22:43 +03:00
|
|
|
const { container } = getComponent();
|
2023-02-14 12:05:01 +03:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2022-11-18 12:22:43 +03:00
|
|
|
expect(container.querySelector("form")).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show SSO options if those are available", async () => {
|
|
|
|
mockClient.loginFlows.mockClear().mockResolvedValue({ flows: [{ type: "m.login.sso" }] });
|
|
|
|
const { container } = getComponent();
|
2023-02-14 12:05:01 +03:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2019-02-05 12:26:45 +03:00
|
|
|
|
2022-11-18 12:22:43 +03:00
|
|
|
const ssoButton = container.querySelector(".mx_SSOButton");
|
|
|
|
expect(ssoButton).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle serverConfig updates correctly", async () => {
|
|
|
|
mockClient.loginFlows.mockResolvedValue({
|
2022-12-12 14:24:14 +03:00
|
|
|
flows: [
|
|
|
|
{
|
|
|
|
type: "m.login.sso",
|
|
|
|
},
|
|
|
|
],
|
2019-02-05 12:26:45 +03:00
|
|
|
});
|
|
|
|
|
2022-11-18 12:22:43 +03:00
|
|
|
const { container, rerender } = render(getRawComponent());
|
2023-02-14 12:05:01 +03:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2020-11-25 13:22:16 +03:00
|
|
|
|
2023-02-16 12:38:44 +03:00
|
|
|
fireEvent.click(container.querySelector(".mx_SSOButton")!);
|
2022-11-18 12:22:43 +03:00
|
|
|
expect(registerRequest.mock.instances[0].baseUrl).toBe("https://matrix.org");
|
2020-11-25 13:22:16 +03:00
|
|
|
|
2022-11-18 12:22:43 +03:00
|
|
|
fetchMock.get("https://server2/_matrix/client/versions", {
|
|
|
|
unstable_features: {},
|
|
|
|
versions: [],
|
2020-11-25 13:22:16 +03:00
|
|
|
});
|
2022-11-18 12:22:43 +03:00
|
|
|
rerender(getRawComponent("https://server2"));
|
2023-02-14 12:05:01 +03:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2020-11-25 13:22:16 +03:00
|
|
|
|
2023-02-16 12:38:44 +03:00
|
|
|
fireEvent.click(container.querySelector(".mx_SSOButton")!);
|
2022-11-18 12:22:43 +03:00
|
|
|
expect(registerRequest.mock.instances[1].baseUrl).toBe("https://server2");
|
2020-11-25 13:22:16 +03:00
|
|
|
});
|
2019-02-05 12:26:45 +03:00
|
|
|
});
|