summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_getUserNameAndPasswordFields.js
blob: 2e845bef5dabfcab6aefba8ec0602df9b19a11f4 (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
/**
 * Test for LoginFormState.getUserNameAndPasswordFields
 */

"use strict";

const { LoginManagerChild } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginManagerChild.sys.mjs"
);
const TESTCASES = [
  {
    description: "1 password field outside of a <form>",
    document: `<input id="pw1" type=password>`,
    returnedFieldIDs: [null, "pw1", null],
  },
  {
    description: "1 text field in a <form> without a password field",
    document: `<form>
      <input id="un1">
      </form>`,
    returnedFieldIDs: [null, null, null],
  },
  {
    description: "1 text field outside of a <form> without a password field",
    document: `<input id="un1">`,
    returnedFieldIDs: [null, null, null],
  },
  {
    description: "1 username & password field outside of a <form>",
    document: `<input id="un1">
      <input id="pw1" type=password>`,
    returnedFieldIDs: ["un1", "pw1", null],
  },
  {
    description: "1 username & password field in a <form>",
    document: `<form>
      <input id="un1">
      <input id="pw1" type=password>
      </form>`,
    returnedFieldIDs: ["un1", "pw1", null],
  },
  {
    description: "5 empty password fields outside of a <form>",
    document: `<input id="pw1" type=password>
      <input id="pw2" type=password>
      <input id="pw3" type=password>
      <input id="pw4" type=password>
      <input id="pw5" type=password>`,
    returnedFieldIDs: [null, "pw1", null],
  },
  {
    description: "6 empty password fields outside of a <form>",
    document: `<input id="pw1" type=password>
      <input id="pw2" type=password>
      <input id="pw3" type=password>
      <input id="pw4" type=password>
      <input id="pw5" type=password>
      <input id="pw6" type=password>`,
    returnedFieldIDs: [null, null, null],
  },
  {
    description: "Form with 1 password field",
    document: `<form><input id="pw1" type=password></form>`,
    returnedFieldIDs: [null, "pw1", null],
  },
  {
    description: "Form with 2 password fields",
    document: `<form><input id="pw1" type=password><input id='pw2' type=password></form>`,
    returnedFieldIDs: [null, "pw1", null],
  },
  {
    description: "1 password field in a form, 1 outside (not processed)",
    document: `<form><input id="pw1" type=password></form><input id="pw2" type=password>`,
    returnedFieldIDs: [null, "pw1", null],
  },
  {
    description:
      "1 password field in a form, 1 text field outside (not processed)",
    document: `<form><input id="pw1" type=password></form><input>`,
    returnedFieldIDs: [null, "pw1", null],
  },
  {
    description:
      "1 text field in a form, 1 password field outside (not processed)",
    document: `<form><input></form><input id="pw1" type=password>`,
    returnedFieldIDs: [null, null, null],
  },
  {
    description:
      "2 password fields outside of a <form> with 1 linked via @form",
    document: `<input id="pw1" type=password><input id="pw2" type=password form='form1'>
      <form id="form1"></form>`,
    returnedFieldIDs: [null, "pw1", null],
  },
  {
    description: "1 username field in a <form>",
    document: `<form>
      <input id="un1" autocomplete=username>
      </form>`,
    returnedFieldIDs: ["un1", null, null],
  },
  {
    description: "1 username field outside of a <form>",
    document: `<input id="un1" autocomplete=username>`,
    returnedFieldIDs: [null, null, null],
  },
  {
    description: "username with type=user",
    document: `<form><input id="un1" type="user"><input id="pwd" type="password"></form>`,
    returnedFieldIDs: ["un1", "pwd", null],
  },
  {
    description: "username with type=username",
    document: `<form><input id="un1" type="username"><input id="pwd" type="password"></form>`,
    returnedFieldIDs: ["un1", "pwd", null],
  },
];

function _setPrefs() {
  Services.prefs.setBoolPref("signon.usernameOnlyForm.enabled", true);
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("signon.usernameOnlyForm.enabled");
  });
}

_setPrefs();

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

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

      let input = document.querySelector("input");
      MockDocument.mockOwnerDocumentProperty(
        input,
        document,
        "http://localhost:8080/test/"
      );
      MockDocument.mockNodePrincipalProperty(
        input,
        "http://localhost:8080/test/"
      );

      // Additional mock to cache recipes
      let win = {};
      Object.defineProperty(document, "defaultView", {
        value: win,
      });
      let formOrigin = LoginHelper.getLoginOrigin(document.documentURI);
      LoginRecipesContent.cacheRecipes(formOrigin, win, new Set());

      const loginManagerChild = new LoginManagerChild();
      const docState = loginManagerChild.stateForDocument(document);
      let actual = docState.getUserNameAndPasswordFields(input);

      Assert.strictEqual(
        testcase.returnedFieldIDs.length,
        3,
        "getUserNameAndPasswordFields returns 3 elements"
      );

      for (let i = 0; i < testcase.returnedFieldIDs.length; i++) {
        let expectedID = testcase.returnedFieldIDs[i];
        if (expectedID === null) {
          Assert.strictEqual(
            actual[i],
            expectedID,
            "Check returned field " + i + " is null"
          );
        } else {
          Assert.strictEqual(
            actual[i].id,
            expectedID,
            "Check returned field " + i + " ID"
          );
        }
      }
    });
  })();
}