summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/test_xhr.html
blob: 296371685256f0297191a4a6a0580edc693ef6a4 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!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="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. **/
var pwmgr, login1, login2;

function initLogins() {
  pwmgr = Cc["@mozilla.org/login-manager;1"].
          getService(Ci.nsILoginManager);

  login1 = Cc["@mozilla.org/login-manager/loginInfo;1"].
            createInstance(Ci.nsILoginInfo);
  login2 = Cc["@mozilla.org/login-manager/loginInfo;1"].
            createInstance(Ci.nsILoginInfo);

  login1.init("http://mochi.test:8888", null, "xhr",
               "xhruser1", "xhrpass1", "", "");
  login2.init("http://mochi.test:8888", null, "xhr2",
               "xhruser2", "xhrpass2", "", "");

  pwmgr.addLogin(login1);
  pwmgr.addLogin(login2);
}

function finishTest() {
  ok(true, "finishTest removing testing logins...");
  pwmgr.removeLogin(login1);
  pwmgr.removeLogin(login2);

  SimpleTest.finish();
}

function handleDialog(doc, testNum) {
  ok(true, "handleDialog running for test " + testNum);

  var clickOK = true;
  var userfield = doc.getElementById("loginTextbox");
  var passfield = doc.getElementById("password1Textbox");
  var username = userfield.getAttribute("value");
  var password = passfield.getAttribute("value");
  var dialog    = doc.getElementById("commonDialog");

  switch (testNum) {
    case 1:
        is(username, "xhruser1", "Checking provided username");
        is(password, "xhrpass1", "Checking provided password");
        break;

    case 2:
        is(username, "xhruser2", "Checking provided username");
        is(password, "xhrpass2", "Checking provided password");

        // 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).
        var win = doc.defaultView;
        var Ci = SpecialPowers.Ci;
        var treeOwner = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).
                            QueryInterface(Ci.nsIDocShellTreeItem).treeOwner;
        treeOwner.QueryInterface(Ci.nsIInterfaceRequestor);
        var flags = treeOwner.getInterface(Ci.nsIXULWindow).chromeFlags;
        var wbc = treeOwner.getInterface(Ci.nsIWebBrowserChrome);
        info("Flags: " + flags);
        ok((flags & Ci.nsIWebBrowserChrome.CHROME_OPENAS_CHROME) != 0,
           "Dialog should be opened as chrome");
        ok((flags & Ci.nsIWebBrowserChrome.CHROME_OPENAS_DIALOG) != 0,
           "Dialog should be opened as a dialog");
        ok((flags & Ci.nsIWebBrowserChrome.CHROME_DEPENDENT) != 0,
           "Dialog should be opened as dependent.");
        ok(wbc.isWindowModal(), "Dialog should be modal");

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


        break;

    default:
        ok(false, "Uhh, unhandled switch for testNum #" + testNum);
        break;
  }

  // Explicitly cancel the dialog and report a fail in this failure
  // case, rather than letting the dialog get stuck due to an auth
  // failure and having the test timeout.
  if (!username && !password) {
      ok(false, "No values prefilled");
      clickOK = false;
  }

  if (clickOK)
    dialog.acceptDialog();
  else
    dialog.cancelDialog();

  ok(true, "handleDialog done");
  didDialog = true;
}

var newWin;
function xhrLoad(xmlDoc) {
  ok(true, "xhrLoad running for test " + testNum);

  // 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;


  switch (testNum) {
    case 1:
        is(username, "xhruser1", "Checking provided username");
        is(password, "xhrpass1", "Checking provided password");
        break;

    case 2:
        is(username, "xhruser2", "Checking provided username");
        is(password, "xhrpass2", "Checking provided password");

        newWin.close();
        break;

    default:
        ok(false, "Uhh, unhandled switch for testNum #" + testNum);
        break;
  }

  doTest();
}

function doTest() {
  switch (++testNum) {
    case 1:
        startCallbackTimer();
        makeRequest("authenticate.sjs?user=xhruser1&pass=xhrpass1&realm=xhr");
        break;

    case 2:
        // 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();
        startCallbackTimer();
        makeRequest("authenticate.sjs?user=xhruser2&pass=xhrpass2&realm=xhr2");
        break;

    default:
        finishTest();
  }
}

function makeRequest(uri) {
  var request = new XMLHttpRequest();
  request.open("GET", uri, true);
  request.onreadystatechange = function () {
    if (request.readyState == 4)
      xhrLoad(request.responseXML);
  };
  request.send(null);
}


initLogins();

// clear plain HTTP auth sessions before the test, to allow
// running them more than once.
var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
                        .getService(SpecialPowers.Ci.nsIHttpAuthManager);
authMgr.clearAll();

// start the tests
testNum = 0;
doTest();

SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>