summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/tests/browser/browser_createLogin.js
blob: a876320ecf5c98674b58faee9ed390b8776da3da (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_setup(async function () {
  let aboutLoginsTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: "about:logins",
  });
  registerCleanupFunction(() => {
    BrowserTestUtils.removeTab(aboutLoginsTab);
    Services.logins.removeAllUserFacingLogins();
  });
});

add_task(async function test_create_login() {
  let browser = gBrowser.selectedBrowser;
  await SpecialPowers.spawn(browser, [], async () => {
    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
    Assert.ok(
      !loginList._selectedGuid,
      "should not be a selected guid by default"
    );
    Assert.ok(
      content.document.documentElement.classList.contains("no-logins"),
      "Should initially be in no logins view"
    );
    Assert.ok(
      loginList.classList.contains("no-logins"),
      "login-list should initially be in no logins view"
    );
    Assert.equal(
      loginList._loginGuidsSortedOrder.length,
      0,
      "login list should be empty"
    );
  });

  let testCases = [
    ["ftp://ftp.example.com/", "ftp://ftp.example.com"],
    ["https://example.com/foo", "https://example.com"],
    ["http://example.com/", "http://example.com"],
    [
      "https://testuser1:testpass1@bugzilla.mozilla.org/show_bug.cgi?id=1556934",
      "https://bugzilla.mozilla.org",
    ],
    ["https://www.example.com/bar", "https://www.example.com"],
  ];

  for (let i = 0; i < testCases.length; i++) {
    let originTuple = testCases[i];
    info("Testcase " + i);
    let storageChangedPromised = TestUtils.topicObserved(
      "passwordmgr-storage-changed",
      (_, data) => data == "addLogin"
    );

    await SpecialPowers.spawn(
      browser,
      [[originTuple, i]],
      async ([aOriginTuple, index]) => {
        let loginList = Cu.waiveXrays(
          content.document.querySelector("login-list")
        );
        let createButton = loginList._createLoginButton;
        Assert.ok(
          ContentTaskUtils.is_hidden(loginList._blankLoginListItem),
          "the blank login list item should be hidden initially"
        );
        Assert.ok(
          !createButton.disabled,
          "Create button should not be disabled initially"
        );

        let loginItem = Cu.waiveXrays(
          content.document.querySelector("login-item")
        );
        let usernameInput = loginItem.shadowRoot.querySelector(
          "input[name='username']"
        );
        usernameInput.placeholder = "dummy placeholder";

        createButton.click();

        Assert.ok(
          ContentTaskUtils.is_visible(loginList._blankLoginListItem),
          "the blank login list item should be visible after clicking on the create button"
        );
        Assert.ok(
          createButton.disabled,
          "Create button should be disabled after being clicked"
        );

        let cancelButton = loginItem.shadowRoot.querySelector(".cancel-button");
        Assert.ok(
          ContentTaskUtils.is_visible(cancelButton),
          "cancel button should be visible in create mode with no logins saved"
        );

        let originInput = loginItem.shadowRoot.querySelector(
          "input[name='origin']"
        );
        let passwordInput = loginItem.shadowRoot.querySelector(
          "input[name='password']"
        );

        Assert.equal(
          content.document.l10n.getAttributes(usernameInput).id,
          null,
          "there should be no placeholder id on the username input in edit mode"
        );
        Assert.equal(
          usernameInput.placeholder,
          "",
          "there should be no placeholder on the username input in edit mode"
        );
        originInput.value = aOriginTuple[0];
        usernameInput.value = "testuser1";
        passwordInput.value = "testpass1";

        let saveChangesButton = loginItem.shadowRoot.querySelector(
          ".save-changes-button"
        );
        saveChangesButton.click();
      }
    );

    info("waiting for login to get added to storage");
    await storageChangedPromised;
    info("login added to storage");

    let canTestOSKeyStoreLogin = OSKeyStoreTestUtils.canTestOSKeyStoreLogin();
    if (canTestOSKeyStoreLogin) {
      storageChangedPromised = TestUtils.topicObserved(
        "passwordmgr-storage-changed",
        (_, data) => data == "modifyLogin"
      );
    }
    await SpecialPowers.spawn(browser, [originTuple], async aOriginTuple => {
      await ContentTaskUtils.waitForCondition(() => {
        return !content.document.documentElement.classList.contains(
          "no-logins"
        );
      }, "waiting for no-logins view to exit");
      Assert.ok(
        !content.document.documentElement.classList.contains("no-logins"),
        "Should no longer be in no logins view"
      );
      let loginList = Cu.waiveXrays(
        content.document.querySelector("login-list")
      );
      Assert.ok(
        !loginList.classList.contains("no-logins"),
        "login-list should no longer be in no logins view"
      );
      Assert.ok(
        ContentTaskUtils.is_hidden(loginList._blankLoginListItem),
        "the blank login list item should be hidden after adding new login"
      );
      Assert.ok(
        !loginList._createLoginButton.disabled,
        "Create button shouldn't be disabled after exiting create login view"
      );

      let loginGuid = await ContentTaskUtils.waitForCondition(() => {
        return loginList._loginGuidsSortedOrder.find(
          guid => loginList._logins[guid].login.origin == aOriginTuple[1]
        );
      }, "Waiting for login to be displayed");
      Assert.ok(loginGuid, "Expected login found in login-list");

      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      Assert.equal(loginItem._login.guid, loginGuid, "login-item should match");

      let { login, listItem } = loginList._logins[loginGuid];
      Assert.ok(
        listItem.classList.contains("selected"),
        "list item should be selected"
      );
      Assert.equal(
        login.origin,
        aOriginTuple[1],
        "Stored login should only include the origin of the URL provided during creation"
      );
      Assert.equal(
        login.username,
        "testuser1",
        "Stored login should have username provided during creation"
      );
      Assert.equal(
        login.password,
        "testpass1",
        "Stored login should have password provided during creation"
      );

      let usernameInput = loginItem.shadowRoot.querySelector(
        "input[name='username']"
      );
      await ContentTaskUtils.waitForCondition(
        () => usernameInput.placeholder,
        "waiting for placeholder to get set"
      );
      Assert.ok(
        usernameInput.placeholder,
        "there should be a placeholder on the username input when not in edit mode"
      );
    });

    if (!canTestOSKeyStoreLogin) {
      continue;
    }

    let reauthObserved = forceAuthTimeoutAndWaitForOSKeyStoreLogin({
      loginResult: true,
    });
    await SpecialPowers.spawn(browser, [originTuple], async aOriginTuple => {
      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      let editButton = loginItem.shadowRoot.querySelector(".edit-button");
      info("clicking on edit button");
      editButton.click();
    });
    info("waiting for oskeystore auth");
    await reauthObserved;

    await SpecialPowers.spawn(browser, [originTuple], async aOriginTuple => {
      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      await ContentTaskUtils.waitForCondition(
        () => loginItem.dataset.editing,
        "waiting for 'edit' mode"
      );
      info("in edit mode");

      let usernameInput = loginItem.shadowRoot.querySelector(
        "input[name='username']"
      );
      let passwordInput = loginItem.shadowRoot.querySelector(
        "input[type='password']"
      );
      passwordInput.focus();
      passwordInput = loginItem.shadowRoot.querySelector(
        "input[name='password']"
      );
      usernameInput.value = "testuser2";
      passwordInput.value = "testpass2";

      let saveChangesButton = loginItem.shadowRoot.querySelector(
        ".save-changes-button"
      );
      info("clicking save changes button");
      saveChangesButton.click();
    });

    info("waiting for login to get modified in storage");
    await storageChangedPromised;
    info("login modified in storage");

    await SpecialPowers.spawn(browser, [originTuple], async aOriginTuple => {
      let loginList = Cu.waiveXrays(
        content.document.querySelector("login-list")
      );
      let login;
      await ContentTaskUtils.waitForCondition(() => {
        login = Object.values(loginList._logins).find(
          obj => obj.login.origin == aOriginTuple[1]
        ).login;
        info(`${login.origin} / ${login.username} / ${login.password}`);
        return (
          login.origin == aOriginTuple[1] &&
          login.username == "testuser2" &&
          login.password == "testpass2"
        );
      }, "waiting for the login to get updated");
      Assert.equal(
        login.origin,
        aOriginTuple[1],
        "Stored login should only include the origin of the URL provided during creation"
      );
      Assert.equal(
        login.username,
        "testuser2",
        "Stored login should have modified username"
      );
      Assert.equal(
        login.password,
        "testpass2",
        "Stored login should have modified password"
      );
    });
  }

  await SpecialPowers.spawn(
    browser,
    [testCases.length],
    async testCasesLength => {
      let loginList = Cu.waiveXrays(
        content.document.querySelector("login-list")
      );
      Assert.equal(
        loginList._loginGuidsSortedOrder.length,
        5,
        "login list should have a login per testcase"
      );
    }
  );
});

add_task(async function test_cancel_create_login() {
  let browser = gBrowser.selectedBrowser;
  await SpecialPowers.spawn(browser, [], async () => {
    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
    Assert.ok(
      loginList._selectedGuid,
      "there should be a selected guid before create mode"
    );
    Assert.ok(
      ContentTaskUtils.is_hidden(loginList._blankLoginListItem),
      "the blank login list item should be hidden before create mode"
    );

    let createButton = content.document
      .querySelector("login-list")
      .shadowRoot.querySelector(".create-login-button");
    createButton.click();

    Assert.ok(
      !loginList._selectedGuid,
      "there should be no selected guid when in create mode"
    );
    Assert.ok(
      ContentTaskUtils.is_visible(loginList._blankLoginListItem),
      "the blank login list item should be visible in create mode"
    );

    let loginItem = Cu.waiveXrays(content.document.querySelector("login-item"));
    let cancelButton = loginItem.shadowRoot.querySelector(".cancel-button");
    cancelButton.click();

    Assert.ok(
      loginList._selectedGuid,
      "there should be a selected guid after canceling create mode"
    );
    Assert.ok(
      ContentTaskUtils.is_hidden(loginList._blankLoginListItem),
      "the blank login list item should be hidden after canceling create mode"
    );
  });
});

add_task(
  async function test_cancel_create_login_with_filter_showing_one_login() {
    const browser = gBrowser.selectedBrowser;
    await SpecialPowers.spawn(browser, [], async () => {
      const loginList = Cu.waiveXrays(
        content.document.querySelector("login-list")
      );

      const loginFilter = Cu.waiveXrays(
        loginList.shadowRoot.querySelector("login-filter")
      );
      loginFilter.value = "bugzilla.mozilla.org";
      Assert.equal(
        loginList._list.querySelectorAll(
          ".login-list-item[data-guid]:not([hidden])"
        ).length,
        1,
        "filter should have one login showing"
      );
      let visibleLoginGuid = loginList.shadowRoot.querySelectorAll(
        ".login-list-item[data-guid]:not([hidden])"
      )[0].dataset.guid;

      let createButton = loginList._createLoginButton;
      createButton.click();

      let loginItem = Cu.waiveXrays(
        content.document.querySelector("login-item")
      );
      let cancelButton = loginItem.shadowRoot.querySelector(".cancel-button");
      Assert.ok(
        ContentTaskUtils.is_visible(cancelButton),
        "cancel button should be visible in create mode with one login showing"
      );
      cancelButton.click();

      Assert.equal(
        loginFilter.value,
        "bugzilla.mozilla.org",
        "login-filter should not be cleared if there was a login in the list"
      );
      Assert.equal(
        loginList.shadowRoot.querySelectorAll(
          ".login-list-item[data-guid]:not([hidden])"
        )[0].dataset.guid,
        visibleLoginGuid,
        "the same login should still be visible"
      );
    });
  }
);

add_task(async function test_cancel_create_login_with_logins_filtered_out() {
  const browser = gBrowser.selectedBrowser;
  await SpecialPowers.spawn(browser, [], async () => {
    const loginList = Cu.waiveXrays(
      content.document.querySelector("login-list")
    );
    const loginFilter = Cu.waiveXrays(
      loginList.shadowRoot.querySelector("login-filter")
    );
    loginFilter.value = "XXX-no-logins-should-match-this-XXX";
    await Promise.resolve();
    Assert.equal(
      loginList._list.querySelectorAll(
        ".login-list-item[data-guid]:not([hidden])"
      ).length,
      0,
      "filter should have no logins showing"
    );

    let createButton = loginList._createLoginButton;
    createButton.click();

    let loginItem = Cu.waiveXrays(content.document.querySelector("login-item"));
    let cancelButton = loginItem.shadowRoot.querySelector(".cancel-button");
    Assert.ok(
      ContentTaskUtils.is_visible(cancelButton),
      "cancel button should be visible in create mode with no logins showing"
    );
    cancelButton.click();
    await Promise.resolve();

    Assert.equal(
      loginFilter.value,
      "",
      "login-filter should be cleared if there were no logins in the list"
    );
    let visibleLoginItems = loginList.shadowRoot.querySelectorAll(
      ".login-list-item[data-guid]:not([hidden])"
    );
    Assert.equal(
      visibleLoginItems.length,
      5,
      "all logins should be visible with blank filter"
    );
    Assert.equal(
      loginList._selectedGuid,
      visibleLoginItems[0].dataset.guid,
      "the first item in the list should be selected"
    );
  });
});

add_task(async function test_create_duplicate_login() {
  if (!OSKeyStoreTestUtils.canTestOSKeyStoreLogin()) {
    return;
  }

  let browser = gBrowser.selectedBrowser;
  EXPECTED_ERROR_MESSAGE = "This login already exists.";
  await SpecialPowers.spawn(browser, [], async () => {
    let loginList = Cu.waiveXrays(content.document.querySelector("login-list"));
    let createButton = loginList._createLoginButton;
    createButton.click();

    let loginItem = Cu.waiveXrays(content.document.querySelector("login-item"));
    let originInput = loginItem.shadowRoot.querySelector(
      "input[name='origin']"
    );
    let usernameInput = loginItem.shadowRoot.querySelector(
      "input[name='username']"
    );
    let passwordInput = loginItem.shadowRoot.querySelector(
      "input[name='password']"
    );
    const EXISTING_USERNAME = "testuser2";
    const EXISTING_ORIGIN = "https://example.com";
    originInput.value = EXISTING_ORIGIN;
    usernameInput.value = EXISTING_USERNAME;
    passwordInput.value = "different password value";

    let saveChangesButton = loginItem.shadowRoot.querySelector(
      ".save-changes-button"
    );
    saveChangesButton.click();

    await ContentTaskUtils.waitForCondition(
      () => !loginItem._errorMessage.hidden,
      "waiting until the error message is visible"
    );
    let duplicatedGuid = Object.values(loginList._logins).find(
      v =>
        v.login.origin == EXISTING_ORIGIN &&
        v.login.username == EXISTING_USERNAME
    ).login.guid;
    Assert.equal(
      loginItem._errorMessageLink.dataset.errorGuid,
      duplicatedGuid,
      "Error message has GUID of existing duplicated login set on it"
    );

    let confirmationDialog = Cu.waiveXrays(
      content.document.querySelector("confirmation-dialog")
    );
    Assert.ok(
      confirmationDialog.hidden,
      "the discard-changes dialog should be hidden before clicking the error-message-text"
    );
    loginItem._errorMessageLink.querySelector("a").click();
    Assert.ok(
      !confirmationDialog.hidden,
      "the discard-changes dialog should be visible"
    );
    let discardChangesButton =
      confirmationDialog.shadowRoot.querySelector(".confirm-button");
    discardChangesButton.click();

    await ContentTaskUtils.waitForCondition(
      () =>
        Object.keys(loginItem._login).length > 1 &&
        loginItem._login.guid == duplicatedGuid,
      "waiting until the existing duplicated login is selected"
    );
    Assert.equal(
      loginList._selectedGuid,
      duplicatedGuid,
      "the duplicated login should be selected in the list"
    );
  });
  EXPECTED_ERROR_MESSAGE = null;
});