Bug 1550131 - Automated test for master password prompt from about:logins. r=MattN
authorJared Wein <jwein@mozilla.com>
Tue, 21 May 2019 13:16:03 +0000
changeset 474737 8518f578b1636b86a817ba7307a9ed47c39a6548
parent 474736 4ff5f1d8f9c43dcd1d127554729f4c2c4efd6d9f
child 474738 7b5f1a48d4e90a393618cd8e7302e22e7ea1d42b
push id36046
push useraiakab@mozilla.com
push dateTue, 21 May 2019 21:45:52 +0000
treeherdermozilla-central@257f2c96cef5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1550131
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1550131 - Automated test for master password prompt from about:logins. r=MattN Differential Revision: https://phabricator.services.mozilla.com/D31125
browser/components/aboutlogins/tests/browser/browser.ini
browser/components/aboutlogins/tests/browser/browser_masterPassword.js
--- a/browser/components/aboutlogins/tests/browser/browser.ini
+++ b/browser/components/aboutlogins/tests/browser/browser.ini
@@ -1,9 +1,10 @@
 [DEFAULT]
 prefs =
   signon.management.page.enabled=true
 
 [browser_copyToClipboardButton.js]
 [browser_deleteLogin.js]
 [browser_loginListChanges.js]
+[browser_masterPassword.js]
 [browser_openSite.js]
 [browser_updateLogin.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/aboutlogins/tests/browser/browser_masterPassword.js
@@ -0,0 +1,81 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+ChromeUtils.import("resource://testing-common/LoginTestUtils.jsm", this);
+
+/**
+ * Waits for the master password prompt and performs an action.
+ * @param {string} action Set to "authenticate" to log in or "cancel" to
+ *        close the dialog without logging in.
+ */
+function waitForMPDialog(action) {
+  let dialogShown = TestUtils.topicObserved("common-dialog-loaded");
+  return dialogShown.then(function([subject]) {
+    let dialog = subject.Dialog;
+    is(dialog.args.title, "Password Required", "Dialog is the Master Password dialog");
+    if (action == "authenticate") {
+      SpecialPowers.wrap(dialog.ui.password1Textbox)
+                   .setUserInput(LoginTestUtils.masterPassword.masterPassword);
+      dialog.ui.button0.click();
+    } else if (action == "cancel") {
+      dialog.ui.button1.click();
+    }
+    return BrowserTestUtils.waitForEvent(window, "DOMModalDialogClosed");
+  });
+}
+
+function getNumberOfLoginsDisplayed(browser) {
+  return ContentTask.spawn(browser, null, async () => {
+    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
+    return loginList._logins.length;
+  });
+}
+
+add_task(async function test() {
+  let login = LoginTestUtils.testData.formLogin({
+    hostname: "https://example.com",
+    formSubmitURL: "https://example.com",
+    username: "username",
+    password: "password",
+  });
+
+  Services.logins.addLogin(login);
+  LoginTestUtils.masterPassword.enable();
+
+  let mpDialogShown = waitForMPDialog("cancel");
+  await BrowserTestUtils.openNewForegroundTab({gBrowser, url: "about:logins"});
+  await mpDialogShown;
+
+  registerCleanupFunction(function() {
+    LoginTestUtils.masterPassword.disable();
+    Services.logins.removeAllLogins();
+    BrowserTestUtils.removeTab(gBrowser.selectedTab);
+  });
+
+  let browser = gBrowser.selectedBrowser;
+  let logins = await getNumberOfLoginsDisplayed(browser);
+  is(logins, 0, "No logins should be displayed when MP is set and unauthenticated");
+
+  let notification;
+  await BrowserTestUtils.waitForCondition(() =>
+    notification = gBrowser.getNotificationBox().getNotificationWithValue("master-password-login-required"));
+
+  ok(notification, "master-password-login-required notification should be visible");
+
+  let buttons = notification.querySelectorAll(".notification-button");
+  is(buttons.length, 1, "Should have one button.");
+
+  let refreshPromise = BrowserTestUtils.browserLoaded(browser);
+  // Sign in with the Master Password this time the dialog is shown
+  mpDialogShown = waitForMPDialog("authenticate");
+  // Click the button to reload the page.
+  buttons[0].click();
+  await refreshPromise;
+  info("Page reloaded");
+
+  await mpDialogShown;
+  info("Master Password dialog shown and authenticated");
+
+  logins = await getNumberOfLoginsDisplayed(browser);
+  is(logins, 1, "Logins should be displayed when MP is set and authenticated");
+});