summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_formless_submit.html
blob: cf1e82a85eb3620dfe556e199141dbc226381473 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test capturing of fields outside of a form</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="pwmgr_common.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="application/javascript">
const { LoginFormFactory } = SpecialPowers.ChromeUtils.import(
  "resource://gre/modules/LoginFormFactory.jsm"
);
const { LoginManagerChild } = SpecialPowers.ChromeUtils.import(
  "resource://gre/modules/LoginManagerChild.jsm"
);

function loadFrame() {
  return new Promise(resolve => {
    document.getElementById("loginFrame").addEventListener("load", (evt) => {
      if (evt.target.contentWindow.location.href.includes("blank.html")) {
        resolve();
      }
    });
  });
}

let loadPromise = new Promise(resolve => {
  document.addEventListener("DOMContentLoaded", () => {
    resolve(loadFrame());
  });
});

add_setup(async () => {
  info("Waiting for page and frame loads");
  await loadPromise;

  await loadRecipes({
    siteRecipes: [{
      hosts: ["mochi.test:8888"],
      usernameSelector: "input[name='recipeuname']",
      passwordSelector: "input[name='recipepword']",
    }],
  });
});

const DEFAULT_ORIGIN = "http://mochi.test:8888";
const TESTCASES = [
  {
    // Inputs
    document: `<input type=password value="">`,
    selectorValues: {
      "[type=password]": "pass1",
    },
    inputIndexForFormLike: 0,
    expectedFormsCount: 1,

    // Expected outputs similar to PasswordManager:onFormSubmit
    origin: DEFAULT_ORIGIN,
    formActionOrigin: DEFAULT_ORIGIN,
    usernameFieldValue: null,
    newPasswordFieldValue: "pass1",
    oldPasswordFieldValue: null,
  },
  {
    document: `<input id="u1" value="">
      <input type=password value="">`,
    selectorValues: {
      "#u1": "user1",
      "[type=password]": "pass1",
    },
    inputIndexForFormLike: 0,
    expectedFormsCount: 1,
    origin: DEFAULT_ORIGIN,
    formActionOrigin: DEFAULT_ORIGIN,
    usernameFieldValue: "user1",
    newPasswordFieldValue: "pass1",
    oldPasswordFieldValue: null,
  },
  {
    document: `<input id="u1" value="">
      <input type=password value="">`,
    selectorValues: {
      "#u1": "user1",
      "[type=password]": "pass1",
    },
    inputIndexForFormLike: 1,
    expectedFormsCount: 1,
    origin: DEFAULT_ORIGIN,
    formActionOrigin: DEFAULT_ORIGIN,
    usernameFieldValue: "user1",
    newPasswordFieldValue: "pass1",
    oldPasswordFieldValue: null,
  },
  {
    document: `<input id="u1" value="">
      <input id="p1" type=password value="">
      <input id="p2" type=password value="">`,
    selectorValues: {
      "#u1": "user1",
      "#p1": "pass1",
      "#p2": "pass2",
    },
    inputIndexForFormLike: 2,
    expectedFormsCount: 1,
    origin: DEFAULT_ORIGIN,
    formActionOrigin: DEFAULT_ORIGIN,
    usernameFieldValue: "user1",
    newPasswordFieldValue: "pass2",
    oldPasswordFieldValue: "pass1",
  },
  {
    document: `<input id="u1" value="">
      <input id="p1" type=password value="">
      <input id="p2" type=password value="">
      <input id="p3" type=password value="">`,
    selectorValues: {
      "#u1": "user1",
      "#p1": "pass1",
      "#p2": "pass2",
      "#p3": "pass2",
    },
    inputIndexForFormLike: 3,
    expectedFormsCount: 1,
    origin: DEFAULT_ORIGIN,
    formActionOrigin: DEFAULT_ORIGIN,
    usernameFieldValue: "user1",
    newPasswordFieldValue: "pass2",
    oldPasswordFieldValue: "pass1",
  },
  {
    document: `<input id="u1" value="">
      <input id="p1" type=password value="" form="form1">
      <input id="p2" type=password value="">
      <form id="form1">
        <input id="u2" value="">
        <input id="p3" type=password value="">
      </form>`,
    selectorValues: {
      "#u1": "user1",
      "#p1": "user2",
      "#p2": "pass1",
      "#u2": "user3",
      "#p3": "pass2",
    },
    inputIndexForFormLike: 2,
    expectedFormsCount: 2,
    origin: DEFAULT_ORIGIN,
    formActionOrigin: DEFAULT_ORIGIN,
    usernameFieldValue: "user1",
    newPasswordFieldValue: "pass1",
    oldPasswordFieldValue: null,
  },
  {
    document: `<!-- recipe field override -->
      <input name="recipeuname" value="">
      <input id="u1" value="">
      <input id="p1" type=password value="">
      <input name="recipepword" type=password value="">`,
    selectorValues: {
      "[name='recipeuname']": "username from recipe",
      "#u1": "default field username",
      "#p1": "pass1",
      "[name='recipepword']": "pass2",
    },
    inputIndexForFormLike: 2,
    expectedFormsCount: 1,
    origin: DEFAULT_ORIGIN,
    formActionOrigin: DEFAULT_ORIGIN,
    usernameFieldValue: "username from recipe",
    newPasswordFieldValue: "pass2",
    oldPasswordFieldValue: null,
  },
];

let count = 0;
async function testFormlessSubmit(tc) {
  let loginFrame = document.getElementById("loginFrame");

  let frameDoc = SpecialPowers.wrap(loginFrame.contentWindow).document;
  info("Starting testcase: " + JSON.stringify(tc));

  let formsProcessed = promiseFormsProcessedInSameProcess(tc.expectedFormsCount);
  // eslint-disable-next-line no-unsanitized/property
  frameDoc.documentElement.innerHTML = tc.document;
  await formsProcessed;
  // We eliminate no user input as a reason for not capturing by modifying the value
  setUserInputValues(frameDoc.documentElement, tc.selectorValues);

  let inputForFormLike = frameDoc.querySelectorAll("input")[tc.inputIndexForFormLike];

  let formLike = LoginFormFactory.createFromField(inputForFormLike);

  info("Calling _onFormSubmit with FormLike");
  let submitProcessed = getSubmitMessage();
  LoginManagerChild.forWindow(frameDoc.defaultView)._onFormSubmit(formLike);

  let { origin, data } = await submitProcessed;

  // Check data sent via PasswordManager:onFormSubmit
  is(origin, tc.origin, "Check origin");
  is(data.formActionOrigin, tc.formActionOrigin, "Check formActionOrigin");

  if (tc.usernameFieldValue === null) {
    is(data.usernameField, tc.usernameFieldValue, "Check usernameField");
  } else {
    is(data.usernameField.value, tc.usernameFieldValue, "Check usernameField");
  }

  is(data.newPasswordField.value, tc.newPasswordFieldValue, "Check newPasswordFieldValue");

  if (tc.oldPasswordFieldValue === null) {
    is(data.oldPasswordField, tc.oldPasswordFieldValue, "Check oldPasswordFieldValue");
  } else {
    is(data.oldPasswordField.value, tc.oldPasswordFieldValue, "Check oldPasswordFieldValue");
  }

  loadPromise = loadFrame();
  loginFrame.contentWindow.location =
    "http://mochi.test:8888/tests/toolkit/components/passwordmgr/test/mochitest/blank.html?" + count++;
  await loadPromise;
}

for (let tc of TESTCASES) {
  let taskName = "testcase-" + count++;
  let tmp = {
    async [taskName]() {
      await testFormlessSubmit(tc);
    },
  };
  add_task(tmp[taskName]);
}
</script>

<p id="display"></p>

<div id="content">
  <iframe id="loginFrame" src="http://mochi.test:8888/tests/toolkit/components/passwordmgr/test/mochitest/blank.html"></iframe>
</div>
<pre id="test"></pre>
</body>
</html>