summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_isProbablyANewPasswordField.js
blob: 167c160da2455fe031d37543cc1019e6b99aa69c (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
/**
 * Test for LoginAutoComplete.isProbablyANewPasswordField.
 */

"use strict";

const LoginAutoComplete = Cc[
  "@mozilla.org/login-manager/autocompletesearch;1"
].getService(Ci.nsILoginAutoCompleteSearch).wrappedJSObject;
// TODO: create a fake window for the test document to pass fathom.isVisible check.
// We should consider moving these tests to mochitest because many fathom
// signals rely on visibility, position, etc., of the test element (See Bug 1712699),
// which is not supported in xpcshell-test.
function makeDocumentVisibleToFathom(doc) {
  let win = {
    getComputedStyle() {
      return {
        overflow: "visible",
        visibility: "visible",
      };
    },
  };
  Object.defineProperty(doc, "defaultView", {
    value: win,
  });
  return doc;
}

function labelledByDocument() {
  let doc = MockDocument.createTestDocument(
    "http://localhost:8080/test/",
    `<div>
       <label id="paper-input-label-2">Password</label>
       <input aria-labelledby="paper-input-label-2" type="password">
     </div>`
  );
  let div = doc.querySelector("div");
  // Put the div contents inside shadow DOM.
  div.attachShadow({ mode: "open" }).append(...div.children);
  return doc;
}
const LABELLEDBY_SHADOW_TESTCASE = labelledByDocument();

const TESTCASES = [
  // Note there is no test case for `<input type="password" autocomplete="new-password">`
  // since isProbablyANewPasswordField explicitly does not run in that case.
  {
    description: "Basic login form",
    document: `
      <h1>Sign in</h1>
      <form>
        <label>Username: <input type="text" name="username"></label>
        <label>Password: <input type="password" name="password"></label>
        <input type="submit" value="Sign in">
      </form>
    `,
    expectedResult: [false],
  },
  {
    description: "Basic registration form",
    document: `
      <h1>Create account</h1>
      <form>
        <label>Username: <input type="text" name="username"></label>
        <label>Password: <input type="password" name="new-password"></label>
        <input type="submit" value="Register">
      </form>
    `,
    expectedResult: [true],
  },
  {
    // TODO: Add <placeholder="confirm"> to "confirm-passowrd" password field so fathom can recognize it
    // as a new password field. Currently, the fathom rules don't really work well in xpcshell-test
    // because signals rely on visibility, position doesn't work. If we move this test to mochitest, we should
    // be able to remove the interim solution (See Bug 1712699).
    description: "Basic password change form",
    document: `
      <h1>Change password</h1>
      <form>
        <label>Current Password: <input type="password" name="current-password"></label>
        <label>New Password: <input type="password" name="new-password"></label>
        <label>Confirm Password: <input type="password" name="confirm-password" placeholder="confirm"></label>
        <input type="submit" value="Save">
      </form>
    `,
    expectedResult: [false, true, true],
  },
  {
    description: "Basic login 'form' without a form element",
    document: `
      <h1>Sign in</h1>
      <label>Username: <input type="text" name="username"></label>
      <label>Password: <input type="password" name="password"></label>
      <input type="submit" value="Sign in">
    `,
    expectedResult: [false],
  },
  {
    description: "Basic registration 'form' without a form element",
    document: `
      <h1>Create account</h1>
      <label>Username: <input type="text" name="username"></label>
      <label>Password: <input type="password" name="new-password"></label>
      <input type="submit" value="Register">
    `,
    expectedResult: [true],
  },
  {
    description: "Basic password change 'form' without a form element",
    document: `
      <h1>Change password</h1>
      <label>Current Password: <input type="password" name="current-password"></label>
      <label>New Password: <input type="password" name="new-password"></label>
      <label>Confirm Password: <input type="password" name="confirm-password"></label>
      <input type="submit" value="Save">
    `,
    expectedResult: [false, true, true],
  },
  {
    description: "Password field with aria-labelledby inside shadow DOM",
    document: LABELLEDBY_SHADOW_TESTCASE,
    inputs: LABELLEDBY_SHADOW_TESTCASE.querySelector(
      "div"
    ).shadowRoot.querySelectorAll("input[type='password']"),
    expectedResult: [false],
  },
];

add_task(async function test_returns_false_when_pref_disabled() {
  const threshold = Services.prefs.getStringPref(
    NEW_PASSWORD_HEURISTIC_ENABLED_PREF
  );

  info("Temporarily disabling new-password heuristic pref");
  Services.prefs.setStringPref(NEW_PASSWORD_HEURISTIC_ENABLED_PREF, "-1");

  // Use registration form test case, where we know it should return true if enabled
  const testcase = TESTCASES[1];
  info("Starting testcase: " + testcase.description);
  const document = Document.isInstance(testcase.document)
    ? testcase.document
    : MockDocument.createTestDocument(
        "http://localhost:8080/test/",
        testcase.document
      );
  for (let [i, input] of testcase.inputs ||
    document.querySelectorAll(`input[type="password"]`).entries()) {
    const result = LoginAutoComplete.isProbablyANewPasswordField(input);
    Assert.strictEqual(
      result,
      false,
      `When the pref is set to disable, the result is always false, e.g. for the testcase, ${testcase.description} ${i}`
    );
  }

  info("Re-enabling new-password heuristic pref");
  Services.prefs.setStringPref(NEW_PASSWORD_HEURISTIC_ENABLED_PREF, threshold);
});

for (let testcase of TESTCASES) {
  info("Sanity checking the testcase: " + testcase.description);

  (function () {
    add_task(async function () {
      info("Starting testcase: " + testcase.description);
      let document = Document.isInstance(testcase.document)
        ? testcase.document
        : MockDocument.createTestDocument(
            "http://localhost:8080/test/",
            testcase.document
          );

      document = makeDocumentVisibleToFathom(document);

      const results = [];
      for (let input of testcase.inputs ||
        document.querySelectorAll(`input[type="password"]`)) {
        const result = LoginAutoComplete.isProbablyANewPasswordField(input);
        results.push(result);
      }

      for (let i = 0; i < testcase.expectedResult.length; i++) {
        let expectedResult = testcase.expectedResult[i];
        Assert.strictEqual(
          results[i],
          expectedResult,
          `In the test case, ${testcase.description}, check if password field #${i} is a new password field.`
        );
      }
    });
  })();
}