From 14efd17b7be628ccbde9b18633b5628d987b0dd4 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 6 Oct 2021 17:16:59 +0300 Subject: [PATCH] FIX: Hide form after password reset (#14526) When hide_email_address_taken was disabled, the forgot password modal showed a flash message and continued to display the form causing confusion. This change shows the flash message only when an error occurs and in all other cases it shows a success message and hides the form. --- .../app/controllers/forgot-password.js | 37 +++++++++---------- .../tests/acceptance/forgot-password-test.js | 34 +++++++++++++++++ 2 files changed, 51 insertions(+), 20 deletions(-) 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" + ); + }); + } +);