summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/tests/browser/browser_primaryPassword.js
blob: 79a1e9a1da4a7a3312fc1fd3b1c7ef34add3ac63 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

function waitForLoginCountToReach(browser, loginCount) {
  return SpecialPowers.spawn(
    browser,
    [loginCount],
    async expectedLoginCount => {
      let loginList = Cu.waiveXrays(
        content.document.querySelector("login-list")
      );
      await ContentTaskUtils.waitForCondition(() => {
        return loginList._loginGuidsSortedOrder.length == expectedLoginCount;
      });
      return loginList._loginGuidsSortedOrder.length;
    }
  );
}

add_setup(async function () {
  await addLogin(TEST_LOGIN1);
  registerCleanupFunction(() => {
    Services.logins.removeAllUserFacingLogins();
    LoginTestUtils.primaryPassword.disable();
  });
});

add_task(async function test() {
  // Confirm that the mocking of the OS auth dialog isn't enabled so the
  // test will timeout if a real OS auth dialog is shown. We don't show
  // the OS auth dialog when Primary Password is enabled.
  Assert.equal(
    Services.prefs.getStringPref(
      "toolkit.osKeyStore.unofficialBuildOnlyLogin",
      ""
    ),
    "",
    "Pref should be set to default value of empty string to start the test"
  );
  LoginTestUtils.primaryPassword.enable();

  let mpDialogShown = forceAuthTimeoutAndWaitForMPDialog("cancel");
  await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: "about:logins",
  });
  await mpDialogShown;

  let browser = gBrowser.selectedBrowser;
  let logins = await waitForLoginCountToReach(browser, 0);
  Assert.equal(
    logins,
    0,
    "No logins should be displayed when MP is set and unauthenticated"
  );

  let notification;
  await TestUtils.waitForCondition(
    () =>
      (notification = gBrowser
        .getNotificationBox()
        .getNotificationWithValue("primary-password-login-required")),
    "waiting for primary-password-login-required notification"
  );

  Assert.ok(
    notification,
    "primary-password-login-required notification should be visible"
  );

  let buttons = notification.buttonContainer.querySelectorAll(
    ".notification-button"
  );
  Assert.equal(buttons.length, 1, "Should have one button.");

  let refreshPromise = BrowserTestUtils.browserLoaded(browser);
  // Sign in with the Primary Password this time the dialog is shown
  mpDialogShown = forceAuthTimeoutAndWaitForMPDialog("authenticate");
  // Click the button to reload the page.
  buttons[0].click();
  await refreshPromise;
  info("Page reloaded");

  await mpDialogShown;
  info("Primary Password dialog shown and authenticated");

  logins = await waitForLoginCountToReach(browser, 1);
  Assert.equal(
    logins,
    1,
    "Logins should be displayed when MP is set and authenticated"
  );

  // Show MP dialog when Copy Password button clicked
  mpDialogShown = forceAuthTimeoutAndWaitForMPDialog("cancel");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    let copyButton = loginItem.shadowRoot.querySelector(
      ".copy-password-button"
    );
    copyButton.click();
  });
  await mpDialogShown;
  info("Primary Password dialog shown and canceled");
  mpDialogShown = forceAuthTimeoutAndWaitForMPDialog("authenticate");
  info("Clicking copy password button again");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    let copyButton = loginItem.shadowRoot.querySelector(
      ".copy-password-button"
    );
    copyButton.click();
  });
  await mpDialogShown;
  info("Primary Password dialog shown and authenticated");
  await SpecialPowers.spawn(browser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    let copyButton = loginItem.shadowRoot.querySelector(
      ".copy-password-button"
    );
    await ContentTaskUtils.waitForCondition(() => {
      return copyButton.disabled;
    }, "Waiting for copy button to be disabled");
    info("Password was copied to clipboard");
  });

  // Show MP dialog when Reveal Password checkbox is checked if not on a new login
  mpDialogShown = forceAuthTimeoutAndWaitForMPDialog("cancel");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    let revealCheckbox = loginItem.shadowRoot.querySelector(
      ".reveal-password-checkbox"
    );
    revealCheckbox.click();
  });
  await mpDialogShown;
  info("Primary Password dialog shown and canceled");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    let revealCheckbox = loginItem.shadowRoot.querySelector(
      ".reveal-password-checkbox"
    );
    Assert.ok(
      !revealCheckbox.checked,
      "reveal checkbox should be unchecked if MP dialog canceled"
    );
  });
  mpDialogShown = forceAuthTimeoutAndWaitForMPDialog("authenticate");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    let revealCheckbox = loginItem.shadowRoot.querySelector(
      ".reveal-password-checkbox"
    );
    revealCheckbox.click();
  });
  await mpDialogShown;
  info("Primary Password dialog shown and authenticated");
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    let revealCheckbox = loginItem.shadowRoot.querySelector(
      ".reveal-password-checkbox"
    );
    Assert.ok(
      revealCheckbox.checked,
      "reveal checkbox should be checked if MP dialog authenticated"
    );
  });

  info("Test toggling the password visibility on a new login");
  await SpecialPowers.spawn(browser, [], async function createNewToggle() {
    let createButton = content.document
      .querySelector("login-list")
      .shadowRoot.querySelector(".create-login-button");
    createButton.click();

    let loginItem = Cu.waiveXrays(content.document.querySelector("login-item"));
    let passwordField = loginItem.shadowRoot.querySelector(
      "input[name='password']"
    );
    let revealCheckbox = loginItem.shadowRoot.querySelector(
      ".reveal-password-checkbox"
    );
    Assert.ok(ContentTaskUtils.is_visible(revealCheckbox), "Toggle visible");
    Assert.ok(!revealCheckbox.checked, "Not revealed initially");
    Assert.equal(passwordField.type, "password", "type is password");
    revealCheckbox.click();

    await ContentTaskUtils.waitForCondition(() => {
      return passwordField.type == "text";
    }, "Waiting for type='text'");
    Assert.ok(revealCheckbox.checked, "Not revealed after click");

    let cancelButton = loginItem.shadowRoot.querySelector(".cancel-button");
    cancelButton.click();
  });

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

    const loginFilter = Cu.waiveXrays(
      loginList.shadowRoot.querySelector("login-filter")
    );
    loginFilter.value = "pass1";
    Assert.equal(
      loginList._list.querySelectorAll(
        ".login-list-item[data-guid]:not([hidden])"
      ).length,
      1,
      "login-list should show corresponding result when primary password is enabled"
    );
    loginFilter.value = "";
    Assert.equal(
      loginList._list.querySelectorAll(
        ".login-list-item[data-guid]:not([hidden])"
      ).length,
      1,
      "login-list should show all results since the filter is empty"
    );
  });
  LoginTestUtils.primaryPassword.disable();
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    Cu.waiveXrays(content).AboutLoginsUtils.primaryPasswordEnabled = false;
    const loginList = Cu.waiveXrays(
      content.document.querySelector("login-list")
    );
    const loginFilter = Cu.waiveXrays(
      loginList.shadowRoot.querySelector("login-filter")
    );
    loginFilter.value = "pass1";
    Assert.equal(
      loginList._list.querySelectorAll(
        ".login-list-item[data-guid]:not([hidden])"
      ).length,
      1,
      "login-list should show login with matching password since MP is disabled"
    );
  });

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

add_task(async function test_login_item_after_successful_auth() {
  // Confirm that the mocking of the OS auth dialog isn't enabled so the
  // test will timeout if a real OS auth dialog is shown. We don't show
  // the OS auth dialog when Primary Password is enabled.
  Assert.equal(
    Services.prefs.getStringPref(
      "toolkit.osKeyStore.unofficialBuildOnlyLogin",
      ""
    ),
    "",
    "Pref should be set to default value of empty string to start the test"
  );
  LoginTestUtils.primaryPassword.enable();

  let mpDialogShown = forceAuthTimeoutAndWaitForMPDialog("authenticate");
  await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: "about:logins",
  });
  await mpDialogShown;

  let browser = gBrowser.selectedBrowser;
  let logins = await waitForLoginCountToReach(browser, 1);
  Assert.equal(
    logins,
    1,
    "Logins should be displayed when MP is set and authenticated"
  );
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    let loginItem = content.document.querySelector("login-item");
    Assert.ok(
      !loginItem.classList.contains("no-logins"),
      "Login item should have content after MP is authenticated"
    );
  });

  LoginTestUtils.primaryPassword.disable();
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});