diff --git a/app/assets/javascripts/discourse/app/controllers/forgot-password.js b/app/assets/javascripts/discourse/app/controllers/forgot-password.js index 8a7d814a7b5..d2887d43a62 100644 --- a/app/assets/javascripts/discourse/app/controllers/forgot-password.js +++ b/app/assets/javascripts/discourse/app/controllers/forgot-password.js @@ -54,14 +54,23 @@ export default Controller.extend(ModalFunctionality, { const accountEmailOrUsername = escapeExpression( this.accountEmailOrUsername ); - const isEmail = accountEmailOrUsername.match(/@/); - let key = `forgot_password.complete_${ - isEmail ? "email" : "username" - }`; - let extraClass; - if (data.user_found === true) { - key += "_found"; + let key = "forgot_password.complete"; + key += accountEmailOrUsername.match(/@/) ? "_email" : "_username"; + + if (data.user_found === false) { + key += "_not_found"; + + this.flash( + I18n.t(key, { + email: accountEmailOrUsername, + username: accountEmailOrUsername, + }), + "error" + ); + } else { + key += data.user_found ? "_found" : ""; + this.set("accountEmailOrUsername", ""); this.set( "offerHelp", @@ -70,19 +79,7 @@ export default Controller.extend(ModalFunctionality, { username: accountEmailOrUsername, }) ); - } else { - if (data.user_found === false) { - key += "_not_found"; - extraClass = "error"; - } - - this.flash( - I18n.t(key, { - email: accountEmailOrUsername, - username: accountEmailOrUsername, - }), - extraClass - ); + this.set("helpSeen", !data.user_found); } }) .catch((e) => { diff --git a/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js b/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js index 0905fd351df..278f57b676f 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js @@ -85,3 +85,37 @@ acceptance("Forgot password", function (needs) { ); }); }); + +acceptance( + "Forgot password - hide_email_address_taken enabled", + function (needs) { + needs.pretender((server, helper) => { + server.post("/session/forgot_password", () => { + return helper.response({}); + }); + }); + + test("requesting password reset", async function (assert) { + await visit("/"); + await click("header .login-button"); + await click("#forgot-password-link"); + + assert.equal( + queryAll(".forgot-password-reset").attr("disabled"), + "disabled", + "it should disable the button until the field is filled" + ); + + await fillIn("#username-or-email", "someuser"); + await click(".forgot-password-reset"); + + assert.equal( + queryAll(".modal-body").html().trim(), + I18n.t("forgot_password.complete_username", { + username: "someuser", + }), + "it should display a success message" + ); + }); + } +);