diff --git a/modules/tchap-translations/tchap_translations.json b/modules/tchap-translations/tchap_translations.json index e73f2c2d3b..9049b593e3 100644 --- a/modules/tchap-translations/tchap_translations.json +++ b/modules/tchap-translations/tchap_translations.json @@ -873,5 +873,9 @@ "auth|proconnect|or": { "en": "or", "fr": "ou" + }, + "auth|proconnect|error_sso_inactive": { + "en": "ProConnect is not activated for your domain", + "fr": "Vous ne pouvez pas vous connecter avec ProConnect avec votre domaine" } } diff --git a/src/tchap/components/views/sso/EmailVerificationPage.tsx b/src/tchap/components/views/sso/EmailVerificationPage.tsx index ddf6aec81d..3527f77d40 100644 --- a/src/tchap/components/views/sso/EmailVerificationPage.tsx +++ b/src/tchap/components/views/sso/EmailVerificationPage.tsx @@ -62,13 +62,18 @@ export default function EmailVerificationPage() { } + const isSSOFlowActive = async (login: Login): Promise => { + const flows = await login.getFlows(); + return !!flows?.find((flow: Record) => flow.type === "m.login.sso"); + } + const onSubmit = async (event: React.FormEvent): Promise => { event.preventDefault(); setLoading(true); const isFieldCorrect = await emailFieldRef.current?.validate({ allowEmpty: false }); if (!isFieldCorrect) { - displayError(_td("auth|proconnect|error_email")); + displayError(_t("auth|proconnect|error_email")); return; } @@ -81,16 +86,23 @@ export default function EmailVerificationPage() { return; } + const login = new Login(hs.base_url, hs.base_url, null, {}); + + const matrixClient= login.createTemporaryClient(); + const validatedServerConfig = await setUpCurrentHs(hs); if (!validatedServerConfig) { - displayError(_td("auth|proconnect|error_homeserver")); + displayError(_t("auth|proconnect|error_homeserver")); return } - - const login = new Login(hs.base_url, hs.base_url, null, {}); - const matrixClient= login.createTemporaryClient(); + // check if oidc is activated on HS + const canSSO = await isSSOFlowActive(login); + if (!canSSO) { + displayError(_t("auth|proconnect|error_sso_inactive")); + return + } // start SSO flow since we got the homeserver PlatformPeg.get()?.startSingleSignOn(matrixClient, "sso", "/home", "", SSOAction.LOGIN); @@ -98,7 +110,7 @@ export default function EmailVerificationPage() { setLoading(false); } catch(err) { - displayError(_td("auth|proconnect|error")); + displayError(_t("auth|proconnect|error")); } } diff --git a/test/unit-tests/tchap/components/views/sso/EmailVerificationPage-test.tsx b/test/unit-tests/tchap/components/views/sso/EmailVerificationPage-test.tsx index 6754080eb7..c7e8bde175 100644 --- a/test/unit-tests/tchap/components/views/sso/EmailVerificationPage-test.tsx +++ b/test/unit-tests/tchap/components/views/sso/EmailVerificationPage-test.tsx @@ -22,12 +22,7 @@ describe("", () => { const PlatformPegMocked: MockedObject = mockPlatformPeg(); const mockedClient: MatrixClient = stubClient(); const mockedTchapUtils = mocked(TchapUtils); - - const mockLoginObject = (hs: string = defaultHsUrl) => { - const mockLoginObject = mocked(new Login(hs, hs, null, {})); - mockLoginObject.createTemporaryClient.mockImplementation(() => mockedClient); - return mockLoginObject; - }; + const mockedLogin = Login as jest.Mock; const mockedFetchHomeserverFromEmail = (hs: string = defaultHsUrl) => { mockedTchapUtils.fetchHomeserverForEmail.mockImplementation(() => @@ -68,7 +63,11 @@ describe("", () => { const renderEmailVerificationPage = () => render(); beforeEach(() => { - mockLoginObject(defaultHsUrl); + mockedLogin.mockImplementation(() => ({ + hsUrl: defaultHsUrl, + createTemporaryClient: jest.fn().mockReturnValue(mockedClient), + getFlows: jest.fn().mockResolvedValue([{ type: "m.login.sso" }]), + })); }); afterEach(() => { @@ -207,4 +206,31 @@ describe("", () => { }); expect(PlatformPegMocked.startSingleSignOn).toHaveBeenCalledTimes(1); }); + + it("should display error when sso is not configured in homeserer", async () => { + const { container } = renderEmailVerificationPage(); + + // Mock the implementation without error, what we want is to be sure they are called with the correct parameters + mockedFetchHomeserverFromEmail(secondHsUrl); + mockedValidatedServerConfig(false, secondHsUrl); + mockedPlatformPegStartSSO(false); + // get flow without sso configured on homeserver + mockedLogin.mockImplementation(() => ({ + hsUrl: secondHsUrl, + createTemporaryClient: jest.fn().mockReturnValue(mockedClient), + getFlows: jest.fn().mockResolvedValue([{ type: "m.login.password" }]), + })); + // Put text in email field + const emailField = screen.getByRole("textbox"); + fireEvent.focus(emailField); + fireEvent.change(emailField, { target: { value: userEmail } }); + + // click on proconnect button + const proconnectButton = screen.getByTestId("proconnect-submit"); + await act(async () => { + await fireEvent.click(proconnectButton); + }); + + expect(container.getElementsByClassName("mx_ErrorMessage").length).toBe(1); + }); });