From 1d0cca258f2bcf756e2afe778fd6293daa5dbb31 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Wed, 6 Jun 2012 14:22:12 +0100
Subject: [PATCH] Clear the password if the user types a password then changes
 the email address.

issue #1540
---
 .../static/dialog/controllers/authenticate.js |  2 ++
 .../test/cases/controllers/authenticate.js    | 28 +++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/resources/static/dialog/controllers/authenticate.js b/resources/static/dialog/controllers/authenticate.js
index c8de422b8..ac413aa99 100644
--- a/resources/static/dialog/controllers/authenticate.js
+++ b/resources/static/dialog/controllers/authenticate.js
@@ -120,6 +120,8 @@ BrowserID.Modules.Authenticate = (function() {
   function enterPasswordState(callback) {
     var self=this;
 
+    dom.setInner("#password", "");
+
     self.publish("enter_password", addressInfo);
     self.submit = authenticate;
     showHint("returning", function() {
diff --git a/resources/static/test/cases/controllers/authenticate.js b/resources/static/test/cases/controllers/authenticate.js
index e410de5e5..6827fce0d 100644
--- a/resources/static/test/cases/controllers/authenticate.js
+++ b/resources/static/test/cases/controllers/authenticate.js
@@ -117,6 +117,34 @@
     controller.checkEmail();
   });
 
+  asyncTest("clear password if user changes email address", function() {
+    xhr.useResult("known_secondary");
+    $("#email").val("registered@testuser.com");
+
+    var enterPasswordCount = 0;
+    mediator.subscribe("enter_password", function() {
+      // The first time the password is shown, change the email address.  The
+      // second time the password is shown, make sure the password was cleared.
+
+      if(enterPasswordCount == 0) {
+        // simulate the user changing the email address.  This should clear the
+        // password.
+        $("#password").val("password");
+        $("#email").val("testuser@testuser.com");
+        $("#email").keyup();
+        controller.checkEmail();
+      }
+      else {
+        equal($("#password").val(), "", "password field was cleared");
+        start();
+      }
+
+      enterPasswordCount++;
+    });
+
+    controller.checkEmail();
+  });
+
   asyncTest("checkEmail with email that has IdP support - 'primary_user' message", function() {
     $("#email").val("unregistered@testuser.com");
     xhr.useResult("primary");
-- 
GitLab