2019-02-05 10:26:45 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-05-16 10:23:51 +02:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2019 New Vector Ltd
|
2019-02-05 10:26:45 +01:00
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-02-05 10:26:45 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import React from "react";
|
2024-09-20 13:24:39 +02:00
|
|
|
import { fireEvent, render, screen, waitFor, waitForElementToBeRemoved } from "@testing-library/react";
|
2023-10-11 23:44:46 +02:00
|
|
|
import { createClient, MatrixClient, MatrixError, OidcClientConfig } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
2022-11-18 10:22:43 +01:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2021-12-08 10:34:24 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import SdkConfig, { DEFAULTS } from "../../../../src/SdkConfig";
|
2023-10-11 23:44:46 +02:00
|
|
|
import { getMockClientWithEventEmitter, mkServerConfig, mockPlatformPeg, unmockPlatformPeg } from "../../../test-utils";
|
2022-03-02 22:09:24 +01:00
|
|
|
import Registration from "../../../../src/components/structures/auth/Registration";
|
2023-10-11 23:44:46 +02:00
|
|
|
import { makeDelegatedAuthConfig } from "../../../test-utils/oidc";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { Features } from "../../../../src/settings/Settings";
|
|
|
|
import { startOidcLogin } from "../../../../src/utils/oidc/authorize";
|
|
|
|
|
|
|
|
jest.mock("../../../../src/utils/oidc/authorize", () => ({
|
|
|
|
startOidcLogin: jest.fn(),
|
|
|
|
}));
|
2021-12-08 10:34:24 +01:00
|
|
|
|
2023-07-05 12:53:22 +02:00
|
|
|
jest.mock("matrix-js-sdk/src/matrix", () => ({
|
|
|
|
...jest.requireActual("matrix-js-sdk/src/matrix"),
|
|
|
|
createClient: jest.fn(),
|
|
|
|
}));
|
2019-02-05 10:26:45 +01:00
|
|
|
|
2024-01-05 14:24:00 +01:00
|
|
|
/** The matrix versions our mock server claims to support */
|
|
|
|
const SERVER_SUPPORTED_MATRIX_VERSIONS = ["v1.1", "v1.5", "v1.6", "v1.8", "v1.9"];
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Registration", function () {
|
2023-10-11 23:44:46 +02:00
|
|
|
let mockClient!: MockedObject<MatrixClient>;
|
2019-02-05 10:26:45 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
beforeEach(function () {
|
2022-03-18 17:12:36 +01:00
|
|
|
SdkConfig.put({
|
2022-02-21 17:57:44 +01:00
|
|
|
...DEFAULTS,
|
|
|
|
disable_custom_urls: true,
|
|
|
|
});
|
2023-10-11 23:44:46 +02:00
|
|
|
mockClient = getMockClientWithEventEmitter({
|
|
|
|
registerRequest: jest.fn(),
|
|
|
|
loginFlows: jest.fn(),
|
2024-01-05 14:24:00 +01:00
|
|
|
getVersions: jest.fn().mockResolvedValue({ versions: SERVER_SUPPORTED_MATRIX_VERSIONS }),
|
2023-10-11 23:44:46 +02:00
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
mockClient.registerRequest.mockRejectedValueOnce(
|
|
|
|
new MatrixError(
|
|
|
|
{
|
|
|
|
flows: [{ stages: [] }],
|
|
|
|
},
|
|
|
|
401,
|
|
|
|
),
|
|
|
|
);
|
2023-10-11 23:44:46 +02:00
|
|
|
mockClient.loginFlows.mockResolvedValue({ flows: [{ type: "m.login.password" }] });
|
2022-12-12 12:24:14 +01:00
|
|
|
mocked(createClient).mockImplementation((opts) => {
|
2022-11-18 10:22:43 +01:00
|
|
|
mockClient.idBaseUrl = opts.idBaseUrl;
|
|
|
|
mockClient.baseUrl = opts.baseUrl;
|
|
|
|
return mockClient;
|
|
|
|
});
|
2023-10-11 23:44:46 +02:00
|
|
|
fetchMock.catch(404);
|
2022-11-18 10:22:43 +01:00
|
|
|
fetchMock.get("https://matrix.org/_matrix/client/versions", {
|
|
|
|
unstable_features: {},
|
2024-01-05 14:24:00 +01:00
|
|
|
versions: SERVER_SUPPORTED_MATRIX_VERSIONS,
|
2022-11-18 10:22:43 +01:00
|
|
|
});
|
|
|
|
mockPlatformPeg({
|
|
|
|
startSingleSignOn: jest.fn(),
|
|
|
|
});
|
2019-02-05 10:26:45 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
afterEach(function () {
|
2023-10-11 23:44:46 +02:00
|
|
|
jest.restoreAllMocks();
|
2022-11-18 10:22:43 +01:00
|
|
|
fetchMock.restore();
|
2023-04-26 11:36:00 +02:00
|
|
|
SdkConfig.reset(); // we touch the config, so clean up
|
2022-11-18 10:22:43 +01:00
|
|
|
unmockPlatformPeg();
|
2019-02-05 10:26:45 +01:00
|
|
|
});
|
|
|
|
|
2022-05-16 10:23:51 +02:00
|
|
|
const defaultProps = {
|
2022-12-12 12:24:14 +01:00
|
|
|
defaultDeviceDisplayName: "test-device-display-name",
|
2022-05-16 10:23:51 +02:00
|
|
|
onLoggedIn: jest.fn(),
|
|
|
|
onLoginClick: jest.fn(),
|
|
|
|
onServerConfigChange: jest.fn(),
|
|
|
|
};
|
2022-11-18 10:22:43 +01:00
|
|
|
|
2023-10-11 23:44:46 +02:00
|
|
|
const defaultHsUrl = "https://matrix.org";
|
|
|
|
const defaultIsUrl = "https://vector.im";
|
|
|
|
|
2024-09-20 13:24:39 +02:00
|
|
|
function getRawComponent(
|
|
|
|
hsUrl = defaultHsUrl,
|
|
|
|
isUrl = defaultIsUrl,
|
|
|
|
authConfig?: OidcClientConfig,
|
|
|
|
mobileRegister?: boolean,
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<Registration
|
|
|
|
{...defaultProps}
|
|
|
|
serverConfig={mkServerConfig(hsUrl, isUrl, authConfig)}
|
|
|
|
mobileRegister={mobileRegister}
|
|
|
|
/>
|
|
|
|
);
|
2022-11-18 10:22:43 +01:00
|
|
|
}
|
|
|
|
|
2024-09-20 13:24:39 +02:00
|
|
|
function getComponent(hsUrl?: string, isUrl?: string, authConfig?: OidcClientConfig, mobileRegister?: boolean) {
|
|
|
|
return render(getRawComponent(hsUrl, isUrl, authConfig, mobileRegister));
|
2019-02-05 10:26:45 +01:00
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should show server picker", async function () {
|
2022-11-18 10:22:43 +01:00
|
|
|
const { container } = getComponent();
|
|
|
|
expect(container.querySelector(".mx_ServerPicker")).toBeTruthy();
|
2019-02-05 10:26:45 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should show form when custom URLs disabled", async function () {
|
2022-11-18 10:22:43 +01:00
|
|
|
const { container } = getComponent();
|
2023-02-14 10:05:01 +01:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2022-11-18 10:22:43 +01: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 10:05:01 +01:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2019-02-05 10:26:45 +01:00
|
|
|
|
2022-11-18 10:22:43 +01:00
|
|
|
const ssoButton = container.querySelector(".mx_SSOButton");
|
|
|
|
expect(ssoButton).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle serverConfig updates correctly", async () => {
|
|
|
|
mockClient.loginFlows.mockResolvedValue({
|
2022-12-12 12:24:14 +01:00
|
|
|
flows: [
|
|
|
|
{
|
|
|
|
type: "m.login.sso",
|
|
|
|
},
|
|
|
|
],
|
2019-02-05 10:26:45 +01:00
|
|
|
});
|
|
|
|
|
2022-11-18 10:22:43 +01:00
|
|
|
const { container, rerender } = render(getRawComponent());
|
2023-02-14 10:05:01 +01:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2020-11-25 11:22:16 +01:00
|
|
|
|
2023-02-16 10:38:44 +01:00
|
|
|
fireEvent.click(container.querySelector(".mx_SSOButton")!);
|
2023-10-11 23:44:46 +02:00
|
|
|
expect(mockClient.baseUrl).toBe("https://matrix.org");
|
2020-11-25 11:22:16 +01:00
|
|
|
|
2022-11-18 10:22:43 +01:00
|
|
|
fetchMock.get("https://server2/_matrix/client/versions", {
|
|
|
|
unstable_features: {},
|
2024-01-05 14:24:00 +01:00
|
|
|
versions: SERVER_SUPPORTED_MATRIX_VERSIONS,
|
2020-11-25 11:22:16 +01:00
|
|
|
});
|
2022-11-18 10:22:43 +01:00
|
|
|
rerender(getRawComponent("https://server2"));
|
2023-02-14 10:05:01 +01:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
2020-11-25 11:22:16 +01:00
|
|
|
|
2023-02-16 10:38:44 +01:00
|
|
|
fireEvent.click(container.querySelector(".mx_SSOButton")!);
|
2023-10-11 23:44:46 +02:00
|
|
|
expect(mockClient.baseUrl).toBe("https://server2");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when delegated authentication is configured and enabled", () => {
|
|
|
|
const authConfig = makeDelegatedAuthConfig();
|
|
|
|
const clientId = "test-client-id";
|
|
|
|
// @ts-ignore
|
|
|
|
authConfig.metadata["prompt_values_supported"] = ["create"];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// mock a statically registered client to avoid dynamic registration
|
|
|
|
SdkConfig.put({
|
|
|
|
oidc_static_clients: {
|
2024-02-23 17:43:14 +01:00
|
|
|
[authConfig.metadata.issuer]: {
|
2023-10-11 23:44:46 +02:00
|
|
|
client_id: clientId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-02-23 17:43:14 +01:00
|
|
|
|
|
|
|
fetchMock.get(`${defaultHsUrl}/_matrix/client/unstable/org.matrix.msc2965/auth_issuer`, {
|
|
|
|
issuer: authConfig.metadata.issuer,
|
|
|
|
});
|
|
|
|
fetchMock.get("https://auth.org/.well-known/openid-configuration", authConfig.metadata);
|
|
|
|
fetchMock.get(authConfig.metadata.jwks_uri!, { keys: [] });
|
2023-10-11 23:44:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when oidc native flow is not enabled in settings", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should display user/pass registration form", async () => {
|
|
|
|
const { container } = getComponent(defaultHsUrl, defaultIsUrl, authConfig);
|
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
|
|
|
expect(container.querySelector("form")).toBeTruthy();
|
|
|
|
expect(mockClient.loginFlows).toHaveBeenCalled();
|
|
|
|
expect(mockClient.registerRequest).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when oidc native flow is enabled in settings", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((key) => key === Features.OidcNativeFlow);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should display oidc-native continue button", async () => {
|
|
|
|
const { container } = getComponent(defaultHsUrl, defaultIsUrl, authConfig);
|
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
|
|
|
// no form
|
|
|
|
expect(container.querySelector("form")).toBeFalsy();
|
|
|
|
|
2024-02-23 17:43:14 +01:00
|
|
|
expect(await screen.findByText("Continue")).toBeTruthy();
|
2023-10-11 23:44:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should start OIDC login flow as registration on button click", async () => {
|
|
|
|
getComponent(defaultHsUrl, defaultIsUrl, authConfig);
|
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByLabelText("Loading…"));
|
|
|
|
|
2024-02-23 17:43:14 +01:00
|
|
|
fireEvent.click(await screen.findByText("Continue"));
|
2023-10-11 23:44:46 +02:00
|
|
|
|
|
|
|
expect(startOidcLogin).toHaveBeenCalledWith(
|
|
|
|
authConfig,
|
|
|
|
clientId,
|
|
|
|
defaultHsUrl,
|
|
|
|
defaultIsUrl,
|
|
|
|
// isRegistration
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2024-09-20 13:24:39 +02:00
|
|
|
|
|
|
|
describe("when is mobile registeration", () => {
|
|
|
|
it("should not show server picker", async function () {
|
|
|
|
const { container } = getComponent(defaultHsUrl, defaultIsUrl, undefined, true);
|
|
|
|
expect(container.querySelector(".mx_ServerPicker")).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show username field with autocaps disabled", async function () {
|
|
|
|
const { container } = getComponent(defaultHsUrl, defaultIsUrl, undefined, true);
|
|
|
|
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(container.querySelector("#mx_RegistrationForm_username")).toHaveAttribute(
|
|
|
|
"autocapitalize",
|
|
|
|
"none",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show password and confirm password fields in separate rows", async function () {
|
|
|
|
const { container } = getComponent(defaultHsUrl, defaultIsUrl, undefined, true);
|
|
|
|
|
|
|
|
await waitFor(() => expect(container.querySelector("#mx_RegistrationForm_username")).toBeTruthy());
|
|
|
|
// when password and confirm password fields are in separate rows there should be 4 rather than 3
|
|
|
|
expect(container.querySelectorAll(".mx_AuthBody_fieldRow")).toHaveLength(4);
|
|
|
|
});
|
|
|
|
});
|
2020-11-25 11:22:16 +01:00
|
|
|
});
|
2019-02-05 10:26:45 +01:00
|
|
|
});
|