From 911ba4b999a9c621a13531dcb97a0c9be2ab7271 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Thu, 5 Apr 2012 16:08:43 +0100
Subject: [PATCH] Change Password fixes.

* Show a tooltip if the passwords are the same.
* Clear old and new passwords if the password is successfully changed.

issue #1388
issue #1389
---
 resources/static/pages/manage_account.js       |  6 ++++++
 .../static/test/cases/pages/manage_account.js  | 18 +++++++++++++++++-
 resources/views/index.ejs                      |  1 +
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/resources/static/pages/manage_account.js b/resources/static/pages/manage_account.js
index 59bc9ccc3..d32521a15 100644
--- a/resources/static/pages/manage_account.js
+++ b/resources/static/pages/manage_account.js
@@ -124,6 +124,8 @@ BrowserID.manageAccount = (function() {
       user.changePassword(oldPassword, newPassword, function(status) {
         if(status) {
           dom.removeClass("#edit_password", "edit");
+          dom.setInner("#old_password", "");
+          dom.setInner("#new_password", "");
         }
         else {
           tooltip.showTooltip("#tooltipInvalidPassword");
@@ -141,6 +143,10 @@ BrowserID.manageAccount = (function() {
       tooltip.showTooltip("#tooltipNewRequired");
       complete(false);
     }
+    else if(newPassword === oldPassword) {
+      tooltip.showTooltip("#tooltipPasswordsSame");
+      complete(false);
+    }
     else if(newPassword.length < 8 || 80 < newPassword.length) {
       tooltip.showTooltip("#tooltipPasswordLength");
       complete(false);
diff --git a/resources/static/test/cases/pages/manage_account.js b/resources/static/test/cases/pages/manage_account.js
index 5e1ddb2cc..fc01f8f09 100644
--- a/resources/static/test/cases/pages/manage_account.js
+++ b/resources/static/test/cases/pages/manage_account.js
@@ -227,6 +227,19 @@
     });
   });
 
+  asyncTest("changePassword with same old and new password - tooltip", function() {
+    bid.manageAccount(mocks, function() {
+      $("#old_password").val("password");
+      $("#new_password").val("password");
+
+      bid.manageAccount.changePassword(function(status) {
+        equal(status, false, "do not update when old and new passwords are the same");
+        testHelpers.testTooltipVisible();
+        start();
+      });
+    });
+  });
+
   asyncTest("changePassword with XHR error - error message", function() {
     bid.manageAccount(mocks, function() {
       xhr.useResult("invalid");
@@ -242,7 +255,6 @@
   });
 
   asyncTest("changePassword with user authenticated to password level, happy case", function() {
-
     bid.manageAccount(mocks, function() {
       $("#old_password").val("oldpassword");
       $("#new_password").val("newpassword");
@@ -250,6 +262,10 @@
       bid.manageAccount.changePassword(function(status) {
         equal(status, true, "on proper completion, status is true");
         equal(tooltip.shown, false, "on proper completion, tooltip is not shown");
+
+        equal($("#old_password").val(), "", "old_password field is cleared");
+        equal($("#new_password").val(), "", "new_password field is cleared");
+
         start();
       });
     });
diff --git a/resources/views/index.ejs b/resources/views/index.ejs
index 6b1f49aec..ae7f6f49c 100644
--- a/resources/views/index.ejs
+++ b/resources/views/index.ejs
@@ -52,6 +52,7 @@
               <div class="tooltip" for="old_password" id="tooltipOldRequired">Old password is required.</div>
               <div class="tooltip" for="old_password" id="tooltipInvalidPassword">Incorrect old password, password not updated.</div>
               <div class="tooltip" for="new_password" id="tooltipNewRequired">New password is required.</div>
+              <div class="tooltip" for="new_password" id="tooltipPasswordsSame">Old and new passwords are the same.</div>
               <div class="tooltip" for="new_password" id="tooltipPasswordLength">Password must be between 8 and 80 characters long.</div>
             </form>
           </section>
-- 
GitLab