From 78bb1d5c542a3ffd94055c41f72a20463167383f Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 2 Sep 2022 11:12:28 +0100 Subject: [PATCH 1/3] Handle M_INVALID_USERNAME on /register/available --- src/components/views/auth/RegistrationForm.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/components/views/auth/RegistrationForm.tsx b/src/components/views/auth/RegistrationForm.tsx index 690acd5c318..6607dfecd5e 100644 --- a/src/components/views/auth/RegistrationForm.tsx +++ b/src/components/views/auth/RegistrationForm.tsx @@ -48,6 +48,7 @@ enum UsernameAvailableStatus { Available, Unavailable, Error, + Invalid, } export const PASSWORD_MIN_SCORE = 3; // safely unguessable: moderate protection from offline slow-hash scenario. @@ -363,7 +364,9 @@ export default class RegistrationForm extends React.PureComponent !value || SAFE_LOCALPART_REGEX.test(value), + test: ({ value }, usernameAvailable) => (!value || SAFE_LOCALPART_REGEX.test(value)) + && usernameAvailable !== UsernameAvailableStatus.Invalid, invalid: () => _t("Some characters not allowed"), }, { From 0f82c9c8281c7cd892f2a84f52f0ea6f27ea414e Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 7 Sep 2022 15:28:48 +0100 Subject: [PATCH 2/3] Add tests --- cypress/e2e/register/register.spec.ts | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/cypress/e2e/register/register.spec.ts b/cypress/e2e/register/register.spec.ts index 8d1076f0b78..a0c120414d2 100644 --- a/cypress/e2e/register/register.spec.ts +++ b/cypress/e2e/register/register.spec.ts @@ -85,4 +85,48 @@ describe("Registration", () => { cy.get(".mx_DevicesPanel_myDevice .mx_DevicesPanel_deviceTrust .mx_E2EIcon") .should("have.class", "mx_E2EIcon_verified"); }); + + it("should require username to fulfil requirements and be available", () => { + cy.get(".mx_ServerPicker_change", { timeout: 15000 }).click(); + cy.get(".mx_ServerPickerDialog_continue").should("be.visible"); + cy.get(".mx_ServerPickerDialog_otherHomeserver").type(synapse.baseUrl); + cy.get(".mx_ServerPickerDialog_continue").click(); + // wait for the dialog to go away + cy.get('.mx_ServerPickerDialog').should('not.exist'); + + cy.get("#mx_RegistrationForm_username").should("be.visible"); + + cy.intercept("**/_matrix/client/*/register/available?username=_alice", { + statusCode: 400, + headers: { + "Content-Type": "application/json", + }, + body: { + errcode: "M_INVALID_USERNAME", + error: "User ID may not begin with _", + }, + }); + cy.get("#mx_RegistrationForm_username").type("_alice"); + cy.get(".mx_Field_tooltip") + .should("have.class", "mx_Tooltip_visible") + .should("contain.text", "Some characters not allowed"); + + cy.intercept("**/_matrix/client/*/register/available?username=bob", { + statusCode: 400, + headers: { + "Content-Type": "application/json", + }, + body: { + errcode: "M_USER_IN_USE", + error: "The desired username is already taken", + }, + }); + cy.get("#mx_RegistrationForm_username").type("{selectAll}{backspace}bob"); + cy.get(".mx_Field_tooltip") + .should("have.class", "mx_Tooltip_visible") + .should("contain.text", "Someone already has that username"); + + cy.get("#mx_RegistrationForm_username").type("{selectAll}{backspace}foobar"); + cy.get(".mx_Field_tooltip").should("not.have.class", "mx_Tooltip_visible"); + }); }); From cd90f118f34be01a8495e7cd489dd2b7131fd339 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 7 Sep 2022 15:31:10 +0100 Subject: [PATCH 3/3] Make typescript check happier --- src/components/views/auth/RegistrationForm.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/views/auth/RegistrationForm.tsx b/src/components/views/auth/RegistrationForm.tsx index 6607dfecd5e..d1f7c9acc45 100644 --- a/src/components/views/auth/RegistrationForm.tsx +++ b/src/components/views/auth/RegistrationForm.tsx @@ -18,6 +18,7 @@ limitations under the License. import React from 'react'; import { MatrixClient } from 'matrix-js-sdk/src/client'; import { logger } from "matrix-js-sdk/src/logger"; +import { MatrixError } from 'matrix-js-sdk/src/matrix'; import * as Email from '../../../email'; import { looksValid as phoneNumberLooksValid } from '../../../phonenumber'; @@ -364,9 +365,10 @@ export default class RegistrationForm extends React.PureComponent