summaryrefslogtreecommitdiffstats
path: root/browser/components/protections/test/browser/browser_protections_monitor.js
blob: 572f6c8b0706bb1b61a7e2b7a4d7ad3cd5e2e522 (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
/* 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";

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

const monitorErrorData = {
  error: true,
};

const mockMonitorData = {
  numBreaches: 11,
  numBreachesResolved: 0,
};

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

  await BrowserTestUtils.reloadTab(tab);

  const monitorCardEnabled = Services.prefs.getBoolPref(
    "browser.contentblocking.report.monitor.enabled"
  );

  // Only run monitor card tests if it's enabled.
  if (monitorCardEnabled) {
    info(
      "Check that the correct content is displayed for users with no logins."
    );
    await checkNoLoginsContentIsDisplayed(tab, "monitor-sign-up");

    info(
      "Check that the correct content is displayed for users with monitor data."
    );
    await Services.logins.addLoginAsync(TEST_LOGIN1);
    AboutProtectionsParent.setTestOverride(mockGetMonitorData(mockMonitorData));
    await BrowserTestUtils.reloadTab(tab);

    Assert.ok(
      true,
      "Error was not thrown for trying to reach the Monitor endpoint, the cache has worked."
    );

    await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
      await ContentTaskUtils.waitForCondition(() => {
        const hasLogins = content.document.querySelector(
          ".monitor-card.has-logins"
        );
        return hasLogins && ContentTaskUtils.is_visible(hasLogins);
      }, "Monitor card for user with stored logins is shown.");

      const hasLoginsHeaderContent = content.document.querySelector(
        "#monitor-header-content span"
      );
      const cardBody = content.document.querySelector(
        ".monitor-card .card-body"
      );

      ok(
        ContentTaskUtils.is_visible(cardBody),
        "Card body is shown for users monitor data."
      );
      await ContentTaskUtils.waitForCondition(() => {
        return (
          hasLoginsHeaderContent.textContent ==
          "Firefox Monitor warns you if your info has appeared in a known data breach."
        );
      }, "Header content for user with monitor data is correct.");

      info("Make sure correct numbers for monitor stats are displayed.");
      const emails = content.document.querySelector(
        ".monitor-stat span[data-type='stored-emails']"
      );
      const passwords = content.document.querySelector(
        ".monitor-stat span[data-type='exposed-passwords']"
      );
      const breaches = content.document.querySelector(
        ".monitor-stat span[data-type='known-breaches']"
      );

      is(emails.textContent, 1, "1 monitored email is displayed");
      is(passwords.textContent, 8, "8 exposed passwords are displayed");
      is(breaches.textContent, 11, "11 known data breaches are displayed.");
    });

    info(
      "Check that correct content is displayed when monitor data contains an error message."
    );
    AboutProtectionsParent.setTestOverride(
      mockGetMonitorData(monitorErrorData)
    );
    await BrowserTestUtils.reloadTab(tab);
    await checkNoLoginsContentIsDisplayed(tab);

    info("Disable showing the Monitor card.");
    Services.prefs.setBoolPref(
      "browser.contentblocking.report.monitor.enabled",
      false
    );
    await BrowserTestUtils.reloadTab(tab);
  }

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      const monitorCard = content.document.querySelector(".monitor-card");
      return !monitorCard["data-enabled"];
    }, "Monitor card is not enabled.");

    const monitorCard = content.document.querySelector(".monitor-card");
    ok(ContentTaskUtils.is_hidden(monitorCard), "Monitor card is hidden.");
  });

  if (monitorCardEnabled) {
    // set the pref back to displaying the card.
    Services.prefs.setBoolPref(
      "browser.contentblocking.report.monitor.enabled",
      true
    );

    // remove logins
    Services.logins.removeLogin(TEST_LOGIN1);

    // restore original test functions
    AboutProtectionsParent.setTestOverride(null);
  }

  await BrowserTestUtils.removeTab(tab);
});

async function checkNoLoginsContentIsDisplayed(tab, expectedLinkContent) {
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      const noLogins = content.document.querySelector(
        ".monitor-card.no-logins"
      );
      return noLogins && ContentTaskUtils.is_visible(noLogins);
    }, "Monitor card for user with no logins is shown.");

    const noLoginsHeaderContent = content.document.querySelector(
      "#monitor-header-content span"
    );
    const cardBody = content.document.querySelector(".monitor-card .card-body");

    ok(
      ContentTaskUtils.is_hidden(cardBody),
      "Card body is hidden for users with no logins."
    );
    is(
      noLoginsHeaderContent.getAttribute("data-l10n-id"),
      "monitor-header-content-no-account",
      "Header content for user with no logins is correct"
    );
  });
}