toolkit/components/passwordmgr/test/mochitest/test_xhr.html
author Jared Wein <jwein@mozilla.com>
Fri, 01 Feb 2019 20:15:02 +0000
changeset 456530 365e026a163adcc3a86b7ce4e1da477af78ae822
parent 453333 toolkit/components/passwordmgr/test/test_xhr.html@16ce9a71ff04850dd34a71759852bca9a62a413d
child 456534 16803b8b2997b8a0b8fe9c54d86185814970d0e0
permissions -rw-r--r--
Bug 1520966 - Make toolkit/components/passwordmgr/test/text_xhr.html work with e10s. r=MattN I didn't use the checkPromptModal function because the implementation in chromeScript was getting a null document reference. Since handlePrompt already has access to this information it made sense to extend handlePrompt to cover more cases. Differential Revision: https://phabricator.services.mozilla.com/D18267

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for XHR prompts</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/AddTask.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <script type="text/javascript" src="prompt_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Login Manager test: XHR prompt
<p id="display"></p>

<div id="content" style="display: none">
  <iframe id="iframe"></iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Login Manager: XHR prompts. **/
function makeRequest(uri) {
  return new Promise((resolve, reject) => {
    let request = new XMLHttpRequest();
    request.open("GET", uri, true);
    request.addEventListener("loadend", function onLoadEnd() {
      let result = xhrLoad(request.responseXML);
      resolve(result);
    });
    request.send(null);
  });
}

function xhrLoad(xmlDoc) {
  // The server echos back the user/pass it received.
  var username = xmlDoc.getElementById("user").textContent;
  var password = xmlDoc.getElementById("pass").textContent;
  var authok = xmlDoc.getElementById("ok").textContent;
  return {username, password, authok};
}

// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
isTabModal = false;

let prompterParent = runInParent(() => {
  const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
  const promptFac = Cc["@mozilla.org/passwordmanager/authpromptfactory;1"].
                    getService(Ci.nsIPromptFactory);

  let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
  let prompt = promptFac.getPrompt(chromeWin, Ci.nsIAuthPrompt);

  addMessageListener("proxyPrompter", function onMessage(msg) {
    let rv = prompt[msg.methodName](...msg.args);
    return {
      rv,
      // Send the args back to content so out/inout args can be checked.
      args: msg.args,
    };
  });
});

let prompter1 = new PrompterProxy(prompterParent);

add_task(function setup() {
  runInParent(function initLogins() {
    const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
    let nsLoginInfo = Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
                                             Ci.nsILoginInfo, "init");
    let login1 = new nsLoginInfo("http://mochi.test:8888", null, "xhr",
                                 "xhruser1", "xhrpass1", "", "");
    let login2 = new nsLoginInfo("http://mochi.test:8888", null, "xhr2",
                                 "xhruser2", "xhrpass2", "", "");

    try {
      Services.logins.addLogin(login1);
      Services.logins.addLogin(login2);
    } catch (e) {
      assert.ok(false, "addLogin threw: " + e);
    }
  });
});

add_task(async function test1() {
  let state = {
    msg: "http://mochi.test:8888 is requesting your username and password. The site says: “xhr”",
    title: "Authentication Required",
    textValue: "xhruser1",
    passValue: "xhrpass1",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: false,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "textField",
    defButton: "button0",
  };
  let action = {
    buttonClick: "ok",
  };
  let promptDone = handlePrompt(state, action);
  let requestPromise = makeRequest("authenticate.sjs?user=xhruser1&pass=xhrpass1&realm=xhr");
  await promptDone;
  let result = await requestPromise;

  is(result.authok, "PASS", "Checking for successful authentication");
  is(result.username, "xhruser1", "Checking for username");
  is(result.password, "xhrpass1", "Checking for password");
});

add_task(async function test2() {
  // Test correct parenting, by opening another tab in the foreground,
  // and making sure the prompt re-focuses the original tab when shown:
  newWin = window.open();
  newWin.focus();

  let state = {
    msg: "http://mochi.test:8888 is requesting your username and password. The site says: “xhr2”",
    title: "Authentication Required",
    textValue: "xhruser2",
    passValue: "xhrpass2",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: false,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "textField",
    defButton: "button0",
    // Check that the dialog is modal, chrome and dependent;
    // We can't just check window.opener because that'll be
    // a content window, which therefore isn't exposed (it'll lie and
    // be null).
    chrome: true,
    dialog: true,
    chromeDependent: true,
    isWindowModal: true,
  };
  let action = {
    buttonClick: "ok",
  };
  let promptDone = handlePrompt(state, action);
  let requestPromise = makeRequest("authenticate.sjs?user=xhruser2&pass=xhrpass2&realm=xhr2");
  await promptDone;
  let result = await requestPromise;

  runInParent(() => {
    const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

    // Check that the right tab is focused:
    let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
    let spec = browserWin.gBrowser.selectedBrowser.currentURI.spec;
    assert.ok(spec.startsWith("http://mochi.test:8888"),
              `Tab with remote URI (rather than about:blank)
               should be focused (${spec})`);
  });

  is(result.authok, "PASS", "Checking for successful authentication");
  is(result.username, "xhruser2", "Checking for username");
  is(result.password, "xhrpass2", "Checking for password");

  newWin.close();
});
</script>
</pre>
</body>
</html>