summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_xhr.html
blob: bfd89e5e71a1083b94baff8afccc37af63403186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for XHR prompts</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <script type="text/javascript" src="../../../prompts/test/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};
}

// Let prompt_common know what kind of modal type is enabled for auth prompts.
modalType = authPromptModalType;

let prompterParent = runInParent(() => {
  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() {
    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: "This site is asking you to sign in.",
    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:
  let newWin = window.open();
  newWin.focus();

  let state = {
    msg: "This site is asking you to sign in.",
    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",
  };

  // For window prompts 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).
  if (authPromptModalType === SpecialPowers.Services.prompt.MODAL_TYPE_WINDOW) {
    state.chrome = true;
    state.dialog = true;
    state.chromeDependent = true;
    state.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(() => {
    // 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(window.location.origin),
              `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");

  // Wait for the assert from the parent script to run and send back its reply,
  // so it's processed before the test ends.
  await SpecialPowers.executeAfterFlushingMessageQueue();

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