Bug 1152422 - Ask to save the new password in a change form with no username even if we have no logins for the site. r=dolske, a=lizzard
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Thu, 23 Apr 2015 17:58:42 -0700
changeset 267276 7a3b1127c88bb9d338f65f878f16959248bc662f
parent 267275 34c32b04a1d82813bdb22d68e86520d5834753fb
child 267277 81723b7cc67f61d51b6ac0aeb658b1d668bd7847
push id830
push userraliiev@mozilla.com
push dateFri, 19 Jun 2015 19:24:37 +0000
treeherdermozilla-release@932614382a68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdolske, lizzard
bugs1152422
milestone39.0a2
Bug 1152422 - Ask to save the new password in a change form with no username even if we have no logins for the site. r=dolske, a=lizzard
toolkit/components/passwordmgr/LoginManagerParent.jsm
toolkit/components/passwordmgr/test/mochitest.ini
toolkit/components/passwordmgr/test/subtst_notifications_2pw_0un.html
toolkit/components/passwordmgr/test/test_notifications.html
--- a/toolkit/components/passwordmgr/LoginManagerParent.jsm
+++ b/toolkit/components/passwordmgr/LoginManagerParent.jsm
@@ -416,30 +416,22 @@ var LoginManagerParent = {
     var formLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].
                     createInstance(Ci.nsILoginInfo);
     formLogin.init(hostname, formSubmitURL, null,
                    (usernameField ? usernameField.value : ""),
                    newPasswordField.value,
                    (usernameField ? usernameField.name  : ""),
                    newPasswordField.name);
 
+    let logins = Services.logins.findLogins({}, hostname, formSubmitURL, null);
+
     // If we didn't find a username field, but seem to be changing a
     // password, allow the user to select from a list of applicable
     // logins to update the password for.
-    if (!usernameField && oldPasswordField) {
-
-      var logins = Services.logins.findLogins({}, hostname, formSubmitURL, null);
-
-      if (logins.length == 0) {
-        // Could prompt to save this as a new password-only login.
-        // This seems uncommon, and might be wrong, so ignore.
-        log("(no logins for this host -- pwchange ignored)");
-        return;
-      }
-
+    if (!usernameField && oldPasswordField && logins.length > 0) {
       var prompter = getPrompter();
 
       if (logins.length == 1) {
         var oldLogin = logins[0];
         formLogin.username      = oldLogin.username;
         formLogin.usernameField = oldLogin.usernameField;
 
         prompter.promptToChangePassword(oldLogin, formLogin);
@@ -447,20 +439,18 @@ var LoginManagerParent = {
         prompter.promptToChangePasswordWithUsernames(
                             logins, logins.length, formLogin);
       }
 
       return;
     }
 
 
+    var existingLogin = null;
     // Look for an existing login that matches the form login.
-    var existingLogin = null;
-    var logins = Services.logins.findLogins({}, hostname, formSubmitURL, null);
-
     for (var i = 0; i < logins.length; i++) {
       var same, login = logins[i];
 
       // If one login has a username but the other doesn't, ignore
       // the username when comparing and only match if they have the
       // same password. Otherwise, compare the logins and match even
       // if the passwords differ.
       if (!login.username && formLogin.username) {
--- a/toolkit/components/passwordmgr/test/mochitest.ini
+++ b/toolkit/components/passwordmgr/test/mochitest.ini
@@ -8,16 +8,17 @@ support-files =
   prompt_common.js
   pwmgr_common.js
   subtst_master_pass.html
   subtst_notifications_1.html
   subtst_notifications_10.html
   subtst_notifications_11.html
   subtst_notifications_11_popup.html
   subtst_notifications_2.html
+  subtst_notifications_2pw_0un.html
   subtst_notifications_3.html
   subtst_notifications_4.html
   subtst_notifications_5.html
   subtst_notifications_6.html
   subtst_notifications_7.html
   subtst_notifications_8.html
   subtst_notifications_9.html
   subtst_privbrowsing_1.html
new file mode 100644
--- /dev/null
+++ b/toolkit/components/passwordmgr/test/subtst_notifications_2pw_0un.html
@@ -0,0 +1,25 @@
+<html>
+<head>
+  <title>Subtest for Login Manager notifications with 2 password fields and no username</title>
+</head>
+<body>
+<h2>Subtest 24</h2>
+<form id="form" action="formsubmit.sjs">
+  <input id="pass1" name="pass1" type="password" value="staticpw">
+  <input id="pass" name="pass" type="password">
+  <button type="submit">Submit</button>
+</form>
+
+<script>
+function submitForm() {
+  pass.value = "notifyp2";
+  form.submit();
+}
+
+window.onload = submitForm;
+var form      = document.getElementById("form");
+var pass      = document.getElementById("pass");
+
+</script>
+</body>
+</html>
--- a/toolkit/components/passwordmgr/test/test_notifications.html
+++ b/toolkit/components/passwordmgr/test/test_notifications.html
@@ -43,16 +43,19 @@ var subtests = [
                    "subtst_notifications_6.html", // 16
                    "subtst_notifications_8.html", // 17
                    "subtst_notifications_8.html", // 18
                    "subtst_notifications_9.html", // 19
                    "subtst_notifications_10.html",  // 20
                    "http://test1.example.org:80" + testpath + "subtst_notifications_1.html", // 21
                    "http://test1.example.org:80" + testpath + "subtst_notifications_7.html", // 22
                    "http://test1.example.org:80" + testpath + "subtst_notifications_6.html", // 23
+                   "subtst_notifications_2pw_0un.html",  // 24
+                   "subtst_notifications_2pw_0un.html",  // 25
+                   "subtst_notifications_2pw_0un.html",  // 26
                ];
 
 
 var ignoreLoad = false;
 function handleLoad(aEvent) {
     // ignore every other load event ... We get one for loading the subtest (which
     // we want to ignore), and another when the subtest's form submits itself
     // (which we want to handle, to start the next test).
@@ -373,16 +376,56 @@ function checkTest() {
         ok(popup, "got notification popup");
         // Check the text, which comes from the localized saveLoginTextNoUser string.
         notificationText = popup.message;
         expectedText = /^Would you like to remember the password on example.org\?$/;
         ok(expectedText.test(notificationText), "Checking text: " + notificationText);
         popup.remove();
         break;
 
+      case 24:
+        // Check for notification popup when a form with 2 password fields (no username) is
+        // submitted and there are no saved logins.
+        is(gotUser, "null", "Checking submitted username");
+        is(gotPass, "notifyp2", "Checking submitted password");
+        popup = getPopup(popupNotifications, "password-save");
+        ok(popup, "got notification popup");
+        popup.remove();
+
+        // Add login for the next test
+        pwmgr.addLogin(login1);
+        break;
+
+      case 25:
+        // Check for notification popup when a form with 2 password fields (no username) is
+        // submitted and there is a saved login with a username and different password.
+        is(gotUser, "null", "Checking submitted username");
+        is(gotPass, "notifyp2", "Checking submitted password");
+        popup = getPopup(popupNotifications, "password-change");
+        ok(popup, "got notification popup");
+        popup.remove();
+        // remove that login
+        pwmgr.removeLogin(login1);
+
+        // Add login for the next test
+        pwmgr.addLogin(login2B);
+        break;
+
+      case 26:
+        // Check for notification popup when a form with 2 password fields (no username) is
+        // submitted and there is a saved login with no username and a different password.
+        is(gotUser, "null", "Checking submitted username");
+        is(gotPass, "notifyp2", "Checking submitted password");
+        popup = getPopup(popupNotifications, "password-change");
+        ok(popup, "got notification popup");
+        popup.remove();
+        // remove that login
+        pwmgr.removeLogin(login2B);
+        break;
+
       default:
         ok(false, "Unexpected call to checkTest for test #" + testNum);
 
     }
 
     // TODO:
     // * existing login test, form has different password --> change password, no save prompt
 }