summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_context_menu_generated_password.js
blob: 4e4edb7b14a06633a2d74d6b4b10d74a02f556e3 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
 * Test the password manager context menu item can fill password fields with a generated password.
 */

/* eslint no-shadow:"off" */

"use strict";

// The origin for the test URIs.
const TEST_ORIGIN = "https://example.com";
const FORM_PAGE_PATH =
  "/browser/toolkit/components/passwordmgr/test/browser/form_basic_login.html";
const CONTEXT_MENU = document.getElementById("contentAreaContextMenu");

const passwordInputSelector = "#form-basic-password";

registerCleanupFunction(async function cleanup_resetPrefs() {
  await SpecialPowers.popPrefEnv();
});

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["signon.generation.available", true],
      ["signon.generation.enabled", true],
    ],
  });
  // assert that there are no logins
  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 0, "There are no logins");
});

add_task(async function test_hidden_by_prefs() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["signon.generation.available", true],
      ["signon.generation.enabled", false],
    ],
  });

  // test that the generated password option is not present when the feature is not enabled
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);

      await openPasswordContextMenu(browser, passwordInputSelector);
      let generatedPasswordItem = document.getElementById(
        "fill-login-generated-password"
      );
      Assert.ok(
        !BrowserTestUtils.is_visible(generatedPasswordItem),
        "generated password item is hidden"
      );

      CONTEXT_MENU.hidePopup();
    }
  );
  await SpecialPowers.popPrefEnv();
});

add_task(async function test_fill_hidden_by_login_saving_disabled() {
  // test that the generated password option is not present when the user
  // disabled password saving for the site.
  Services.logins.setLoginSavingEnabled(TEST_ORIGIN, false);

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);

      await openPasswordContextMenu(browser, passwordInputSelector);
      let generatedPasswordItem = document.getElementById(
        "fill-login-generated-password"
      );
      Assert.ok(
        !BrowserTestUtils.is_visible(generatedPasswordItem),
        "generated password item is hidden"
      );

      CONTEXT_MENU.hidePopup();
    }
  );

  Services.logins.setLoginSavingEnabled(TEST_ORIGIN, true);
});

add_task(async function test_fill_hidden_by_locked_primary_password() {
  // test that the generated password option is not present when the user
  // didn't unlock the primary password.
  LoginTestUtils.primaryPassword.enable();

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);

      await openPasswordContextMenu(
        browser,
        passwordInputSelector,
        () => false
      );
      let generatedPasswordItem = document.getElementById(
        "fill-login-generated-password"
      );
      Assert.ok(
        BrowserTestUtils.is_visible(generatedPasswordItem),
        "generated password item is visible"
      );
      Assert.ok(
        generatedPasswordItem.disabled,
        "generated password item is disabled"
      );

      CONTEXT_MENU.hidePopup();
    }
  );

  LoginTestUtils.primaryPassword.disable();
});

add_task(async function fill_generated_password_empty_field() {
  // test that we can fill with generated password into an empty password field
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkInitialFieldValue(inputSelector) {
          const input = content.document.querySelector(inputSelector);
          Assert.equal(input.value.length, 0, "Password field is empty");
          Assert.ok(
            !input.matches(":autofill"),
            "Password field should not be highlighted"
          );
        }
      );

      await doFillGeneratedPasswordContextMenuItem(
        browser,
        passwordInputSelector
      );
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkFinalFieldValue(inputSelector) {
          let { LoginTestUtils: LTU } = ChromeUtils.importESModule(
            "resource://testing-common/LoginTestUtils.sys.mjs"
          );
          const input = content.document.querySelector(inputSelector);
          Assert.equal(
            input.value.length,
            LTU.generation.LENGTH,
            "Password field was filled with generated password"
          );
          Assert.ok(
            input.matches(":autofill"),
            "Password field should be highlighted"
          );
          LTU.loginField.checkPasswordMasked(input, false, "after fill");

          info("cleaing the field");
          input.setUserInput("");
        }
      );

      let acPopup = document.getElementById("PopupAutoComplete");
      await openACPopup(acPopup, browser, passwordInputSelector);

      let pwgenItem = acPopup.querySelector(
        `[originaltype="generatedPassword"]`
      );
      Assert.ok(
        !pwgenItem || EventUtils.isHidden(pwgenItem),
        "pwgen item should no longer be shown"
      );

      await closePopup(acPopup);
    }
  );
});

add_task(async function fill_generated_password_nonempty_field() {
  // test that we can fill with generated password into an non-empty password field
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);
      await changeContentFormValues(browser, {
        [passwordInputSelector]: "aa",
      });
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkInitialFieldValue(inputSelector) {
          const input = content.document.querySelector(inputSelector);
          Assert.ok(
            !input.matches(":autofill"),
            "Password field should not be highlighted"
          );
        }
      );

      await doFillGeneratedPasswordContextMenuItem(
        browser,
        passwordInputSelector
      );
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkFinalFieldValue(inputSelector) {
          let { LoginTestUtils: LTU } = ChromeUtils.importESModule(
            "resource://testing-common/LoginTestUtils.sys.mjs"
          );
          const input = content.document.querySelector(inputSelector);
          Assert.equal(
            input.value.length,
            LTU.generation.LENGTH,
            "Password field was filled with generated password"
          );
          Assert.ok(
            input.matches(":autofill"),
            "Password field should be highlighted"
          );
          LTU.loginField.checkPasswordMasked(input, false, "after fill");
        }
      );
    }
  );
  LoginTestUtils.clearData();
  LoginTestUtils.resetGeneratedPasswordsCache();
});

add_task(async function fill_generated_password_with_matching_logins() {
  // test that we can fill a generated password when there are matching logins
  let login = LoginTestUtils.testData.formLogin({
    origin: "https://example.com",
    formActionOrigin: "https://example.com",
    username: "username",
    password: "pass1",
  });
  let storageChangedPromised = TestUtils.topicObserved(
    "passwordmgr-storage-changed",
    (_, data) => data == "addLogin"
  );
  await Services.logins.addLoginAsync(login);
  await storageChangedPromised;

  let formFilled = listenForTestNotification("FormProcessed");

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);
      await formFilled;
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkInitialFieldValue(inputSelector) {
          Assert.equal(
            content.document.querySelector(inputSelector).value,
            "pass1",
            "Password field has initial value"
          );
        }
      );

      await doFillGeneratedPasswordContextMenuItem(
        browser,
        passwordInputSelector
      );
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkFinalFieldValue(inputSelector) {
          let { LoginTestUtils: LTU } = ChromeUtils.importESModule(
            "resource://testing-common/LoginTestUtils.sys.mjs"
          );
          const input = content.document.querySelector(inputSelector);
          Assert.equal(
            input.value.length,
            LTU.generation.LENGTH,
            "Password field was filled with generated password"
          );
          Assert.ok(
            input.matches(":autofill"),
            "Password field should be highlighted"
          );
          LTU.loginField.checkPasswordMasked(input, false, "after fill");
        }
      );

      await openPasswordContextMenu(browser, passwordInputSelector);

      // Execute the command of the first login menuitem found at the context menu.
      let passwordChangedPromise = ContentTask.spawn(
        browser,
        null,
        async function () {
          let passwordInput = content.document.getElementById(
            "form-basic-password"
          );
          await ContentTaskUtils.waitForEvent(passwordInput, "input");
        }
      );

      let popupMenu = document.getElementById("fill-login-popup");
      let firstLoginItem =
        popupMenu.getElementsByClassName("context-login-item")[0];
      firstLoginItem.doCommand();

      await passwordChangedPromise;

      let contextMenu = document.getElementById("contentAreaContextMenu");
      contextMenu.hidePopup();

      // Blur the field to trigger a 'change' event.
      await BrowserTestUtils.synthesizeKey("KEY_Tab", undefined, browser);
      await BrowserTestUtils.synthesizeKey(
        "KEY_Tab",
        { shiftKey: true },
        browser
      );

      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkFieldNotGeneratedPassword(inputSelector) {
          let { LoginTestUtils: LTU } = ChromeUtils.importESModule(
            "resource://testing-common/LoginTestUtils.sys.mjs"
          );
          const input = content.document.querySelector(inputSelector);
          Assert.equal(
            input.value,
            "pass1",
            "Password field was filled with the saved password"
          );
          LTU.loginField.checkPasswordMasked(
            input,
            true,
            "after fill of a saved login"
          );
        }
      );
    }
  );

  let logins = Services.logins.getAllLogins();
  Assert.equal(logins.length, 2, "Check 2 logins");
  isnot(
    logins[0].password,
    logins[1].password,
    "Generated password shouldn't have changed to match the filled password"
  );

  Services.logins.removeAllUserFacingLogins();
  LoginTestUtils.resetGeneratedPasswordsCache();
});

add_task(async function test_edited_generated_password_in_new_tab() {
  // test that we can fill the generated password into an empty password field,
  // edit it, and then fill the edited password.
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkInitialFieldValue(inputSelector) {
          const input = content.document.querySelector(inputSelector);
          Assert.equal(input.value.length, 0, "Password field is empty");
          Assert.ok(
            !input.matches(":autofill"),
            "Password field should not be highlighted"
          );
        }
      );

      await doFillGeneratedPasswordContextMenuItem(
        browser,
        passwordInputSelector
      );
      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkAndEditFieldValue(inputSelector) {
          let { LoginTestUtils: LTU } = ChromeUtils.importESModule(
            "resource://testing-common/LoginTestUtils.sys.mjs"
          );
          const input = content.document.querySelector(inputSelector);
          Assert.equal(
            input.value.length,
            LTU.generation.LENGTH,
            "Password field was filled with generated password"
          );
          Assert.ok(
            input.matches(":autofill"),
            "Password field should be highlighted"
          );
          LTU.loginField.checkPasswordMasked(input, false, "after fill");
        }
      );

      await BrowserTestUtils.sendChar("!", browser);
      await BrowserTestUtils.sendChar("@", browser);
      let storageChangedPromised = TestUtils.topicObserved(
        "passwordmgr-storage-changed",
        (_, data) => data == "modifyLogin"
      );
      await BrowserTestUtils.synthesizeKey("KEY_Tab", undefined, browser);
      info("Waiting for storage update");
      await storageChangedPromised;
    }
  );

  info("Now fill again in a new tab and ensure the edited password is used");

  // Disable autofill in the new tab
  await SpecialPowers.pushPrefEnv({
    set: [["signon.autofillForms", false]],
  });

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);

      await doFillGeneratedPasswordContextMenuItem(
        browser,
        passwordInputSelector
      );

      await SpecialPowers.spawn(
        browser,
        [[passwordInputSelector]],
        function checkAndEditFieldValue(inputSelector) {
          let { LoginTestUtils: LTU } = ChromeUtils.importESModule(
            "resource://testing-common/LoginTestUtils.sys.mjs"
          );
          const input = content.document.querySelector(inputSelector);
          Assert.equal(
            input.value.length,
            LTU.generation.LENGTH + 2,
            "Password field was filled with edited generated password"
          );
          LTU.loginField.checkPasswordMasked(input, false, "after fill");
        }
      );
    }
  );

  LoginTestUtils.clearData();
  LoginTestUtils.resetGeneratedPasswordsCache();
  await SpecialPowers.popPrefEnv();
});