summaryrefslogtreecommitdiffstats
path: root/browser/components/protections/test/browser/browser_protections_lockwise.js
blob: 93d10eb4b55f62363b895c8a5fef5eb57070103f (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

requestLongerTimeout(2);

const { AboutProtectionsParent } = ChromeUtils.importESModule(
  "resource:///actors/AboutProtectionsParent.sys.mjs"
);
const ABOUT_LOGINS_URL = "about:logins";

add_task(async function testNoLoginsLockwiseCardUI() {
  const tab = await BrowserTestUtils.openNewForegroundTab({
    url: "about:protections",
    gBrowser,
  });
  const aboutLoginsPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    ABOUT_LOGINS_URL
  );

  info(
    "Check that the correct lockwise card content is displayed for non-logged in users."
  );
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      const lockwiseCard = content.document.querySelector(".lockwise-card");
      return ContentTaskUtils.is_visible(lockwiseCard);
    }, "Lockwise card for user with no logins is visible.");

    const lockwiseHowItWorks = content.document.querySelector(
      "#lockwise-how-it-works"
    );
    ok(
      ContentTaskUtils.is_hidden(lockwiseHowItWorks),
      "How it works link is hidden"
    );

    const lockwiseHeaderContent = content.document.querySelector(
      "#lockwise-header-content span"
    );
    await content.document.l10n.translateElements([lockwiseHeaderContent]);
    is(
      lockwiseHeaderContent.dataset.l10nId,
      "passwords-header-content",
      "lockwiseHeaderContent contents should match l10n-id attribute set on the element"
    );

    const lockwiseScannedWrapper = content.document.querySelector(
      ".lockwise-scanned-wrapper"
    );
    ok(
      ContentTaskUtils.is_hidden(lockwiseScannedWrapper),
      "Lockwise scanned wrapper is hidden"
    );

    const managePasswordsButton = content.document.querySelector(
      "#manage-passwords-button"
    );
    ok(
      ContentTaskUtils.is_hidden(managePasswordsButton),
      "Manage passwords button is hidden"
    );

    const savePasswordsButton = content.document.querySelector(
      "#save-passwords-button"
    );
    ok(
      ContentTaskUtils.is_visible(savePasswordsButton),
      "Save passwords button is visible in the header"
    );
    info(
      "Click on the save passwords button and check that it opens about:logins in a new tab"
    );
    savePasswordsButton.click();
  });
  const loginsTab = await aboutLoginsPromise;
  info("about:logins was successfully opened in a new tab");
  gBrowser.removeTab(loginsTab);
  gBrowser.removeTab(tab);
});

add_task(async function testLockwiseCardUIWithLogins() {
  const tab = await BrowserTestUtils.openNewForegroundTab({
    url: "about:protections",
    gBrowser,
  });
  const aboutLoginsPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    ABOUT_LOGINS_URL
  );

  info(
    "Add a login and check that lockwise card content for a logged in user is displayed correctly"
  );
  await Services.logins.addLoginAsync(TEST_LOGIN1);
  await BrowserTestUtils.reloadTab(tab);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      const hasLogins = content.document.querySelector(".lockwise-card");
      return ContentTaskUtils.is_visible(hasLogins);
    }, "Lockwise card for user with logins is visible");

    const lockwiseTitle = content.document.querySelector("#lockwise-title");
    await content.document.l10n.translateElements([lockwiseTitle]);
    await ContentTaskUtils.waitForCondition(
      () => lockwiseTitle.textContent == "Manage your passwords",
      "Waiting for Fluent to provide the title translation"
    );
    is(
      lockwiseTitle.textContent,
      "Manage your passwords",
      "Correct passwords title is shown"
    );

    const lockwiseHowItWorks = content.document.querySelector(
      "#lockwise-how-it-works"
    );
    ok(
      ContentTaskUtils.is_visible(lockwiseHowItWorks),
      "How it works link is visible"
    );

    const lockwiseHeaderContent = content.document.querySelector(
      "#lockwise-header-content span"
    );
    await content.document.l10n.translateElements([lockwiseHeaderContent]);
    is(
      lockwiseHeaderContent.dataset.l10nId,
      "lockwise-header-content-logged-in",
      "lockwiseHeaderContent contents should match l10n-id attribute set on the element"
    );

    const lockwiseScannedWrapper = content.document.querySelector(
      ".lockwise-scanned-wrapper"
    );
    ok(
      ContentTaskUtils.is_visible(lockwiseScannedWrapper),
      "Lockwise scanned wrapper is visible"
    );

    const lockwiseScannedText = content.document.querySelector(
      "#lockwise-scanned-text"
    );
    await content.document.l10n.translateElements([lockwiseScannedText]);
    is(
      lockwiseScannedText.textContent,
      "1 password stored securely.",
      "Correct lockwise scanned text is shown"
    );

    const savePasswordsButton = content.document.querySelector(
      "#save-passwords-button"
    );
    ok(
      ContentTaskUtils.is_hidden(savePasswordsButton),
      "Save passwords button is hidden"
    );

    const managePasswordsButton = content.document.querySelector(
      "#manage-passwords-button"
    );
    ok(
      ContentTaskUtils.is_visible(managePasswordsButton),
      "Manage passwords button is visible"
    );
    info(
      "Click on the manage passwords button and check that it opens about:logins in a new tab"
    );
    managePasswordsButton.click();
  });
  const loginsTab = await aboutLoginsPromise;
  info("about:logins was successfully opened in a new tab");
  gBrowser.removeTab(loginsTab);

  info(
    "Add another login and check that the scanned text about stored logins is updated after reload."
  );
  await Services.logins.addLoginAsync(TEST_LOGIN2);
  await BrowserTestUtils.reloadTab(tab);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    const lockwiseScannedText = content.document.querySelector(
      "#lockwise-scanned-text"
    ).textContent;
    ContentTaskUtils.waitForCondition(
      () =>
        lockwiseScannedText.textContent ==
        "Your passwords are being stored securely.",
      "Correct lockwise scanned text is shown"
    );
  });

  Services.logins.removeLogin(TEST_LOGIN1);
  Services.logins.removeLogin(TEST_LOGIN2);

  gBrowser.removeTab(tab);
});

add_task(async function testLockwiseCardUIWithBreachedLogins() {
  info(
    "Add a breached login and test that the lockwise scanned text is displayed correctly"
  );
  const tab = await BrowserTestUtils.openNewForegroundTab({
    url: "about:protections",
    gBrowser,
  });
  await Services.logins.addLoginAsync(TEST_LOGIN1);

  info("Mock monitor data with a breached login to test the Lockwise UI");
  AboutProtectionsParent.setTestOverride(
    mockGetLoginDataWithSyncedDevices(false, 1)
  );
  await BrowserTestUtils.reloadTab(tab);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    const lockwiseScannedText = content.document.querySelector(
      "#lockwise-scanned-text"
    );
    ok(
      ContentTaskUtils.is_visible(lockwiseScannedText),
      "Lockwise scanned text is visible"
    );
    await ContentTaskUtils.waitForCondition(
      () =>
        lockwiseScannedText.textContent ==
        "1 password may have been exposed in a data breach."
    );
    info("Correct lockwise scanned text is shown");
  });

  info(
    "Mock monitor data with more than one breached logins to test the Lockwise UI"
  );
  AboutProtectionsParent.setTestOverride(
    mockGetLoginDataWithSyncedDevices(false, 2)
  );
  await BrowserTestUtils.reloadTab(tab);
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    const lockwiseScannedText = content.document.querySelector(
      "#lockwise-scanned-text"
    );
    ok(
      ContentTaskUtils.is_visible(lockwiseScannedText),
      "Lockwise scanned text is visible"
    );
    await ContentTaskUtils.waitForCondition(
      () =>
        lockwiseScannedText.textContent ==
        "2 passwords may have been exposed in a data breach."
    );
    info("Correct lockwise scanned text is shown");
  });

  AboutProtectionsParent.setTestOverride(null);
  Services.logins.removeLogin(TEST_LOGIN1);
  gBrowser.removeTab(tab);
});

add_task(async function testLockwiseCardPref() {
  const tab = await BrowserTestUtils.openNewForegroundTab({
    url: "about:protections",
    gBrowser,
  });

  info("Disable showing the Lockwise card.");
  Services.prefs.setBoolPref(
    "browser.contentblocking.report.lockwise.enabled",
    false
  );
  await BrowserTestUtils.reloadTab(tab);
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    const lockwiseCard = content.document.querySelector(".lockwise-card");
    await ContentTaskUtils.waitForCondition(() => {
      return !lockwiseCard["data-enabled"];
    }, "Lockwise card is not enabled.");

    ok(ContentTaskUtils.is_hidden(lockwiseCard), "Lockwise card is hidden.");
  });

  // Set the pref back to displaying the card.
  Services.prefs.setBoolPref(
    "browser.contentblocking.report.lockwise.enabled",
    true
  );
  gBrowser.removeTab(tab);
});