summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_LoginManagerParent_doAutocompleteSearch.js
blob: 2e182a70642140c251b2ca4fd56abba1fe7c96a3 (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
/**
 * Test LoginManagerParent.doAutocompleteSearch()
 */

"use strict";

const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);
const { LoginManagerParent } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginManagerParent.sys.mjs"
);

// new-password to the happy path
const NEW_PASSWORD_TEMPLATE_ARG = {
  actionOrigin: "https://mozilla.org",
  searchString: "",
  previousResult: null,
  requestId: "foo",
  hasBeenTypePassword: true,
  isSecure: true,
  isProbablyANewPasswordField: true,
};

add_setup(async () => {
  Services.prefs.setBoolPref("signon.generation.available", true);
  Services.prefs.setBoolPref("signon.generation.enabled", true);

  await LoginTestUtils.remoteSettings.setupImprovedPasswordRules();

  sinon
    .stub(LoginManagerParent._browsingContextGlobal, "get")
    .withArgs(123)
    .callsFake(() => {
      return {
        currentWindowGlobal: {
          documentPrincipal:
            Services.scriptSecurityManager.createContentPrincipalFromOrigin(
              "https://www.example.com^userContextId=1"
            ),
          documentURI: Services.io.newURI("https://www.example.com"),
        },
      };
    });
});

add_task(async function test_generated_noLogins() {
  let LMP = new LoginManagerParent();
  LMP.useBrowsingContext(123);

  Assert.ok(LMP.doAutocompleteSearch, "doAutocompleteSearch exists");

  let result1 = await LMP.doAutocompleteSearch(
    "https://example.com",
    NEW_PASSWORD_TEMPLATE_ARG
  );
  equal(result1.logins.length, 0, "no logins");
  Assert.ok(result1.generatedPassword, "has a generated password");
  equal(result1.generatedPassword.length, 15, "generated password length");
  Assert.ok(
    result1.willAutoSaveGeneratedPassword,
    "will auto-save when storage is empty"
  );

  info("repeat the search and ensure the same password was used");
  let result2 = await LMP.doAutocompleteSearch(
    "https://example.com",
    NEW_PASSWORD_TEMPLATE_ARG
  );
  equal(result2.logins.length, 0, "no logins");
  equal(
    result2.generatedPassword,
    result1.generatedPassword,
    "same generated password"
  );
  Assert.ok(
    result1.willAutoSaveGeneratedPassword,
    "will auto-save when storage is still empty"
  );

  info("Check cases where a password shouldn't be generated");

  let result3 = await LMP.doAutocompleteSearch("https://example.com", {
    ...NEW_PASSWORD_TEMPLATE_ARG,
    ...{
      hasBeenTypePassword: false,
      isProbablyANewPasswordField: false,
    },
  });
  equal(
    result3.generatedPassword,
    null,
    "no generated password when not a pw. field"
  );

  let result4 = await LMP.doAutocompleteSearch("https://example.com", {
    ...NEW_PASSWORD_TEMPLATE_ARG,
    ...{
      // This is false when there is no autocomplete="new-password" attribute &&
      // LoginAutoComplete.isProbablyANewPasswordField returns false
      isProbablyANewPasswordField: false,
    },
  });
  equal(
    result4.generatedPassword,
    null,
    "no generated password when isProbablyANewPasswordField is false"
  );

  LMP.useBrowsingContext(999);
  let result5 = await LMP.doAutocompleteSearch("https://example.com", {
    ...NEW_PASSWORD_TEMPLATE_ARG,
  });
  equal(
    result5.generatedPassword,
    null,
    "no generated password with a missing browsingContextId"
  );
});

add_task(async function test_generated_emptyUsernameSavedLogin() {
  info("Test with a login that will prevent auto-saving");
  await LoginTestUtils.addLogin({
    username: "",
    password: "my-saved-password",
    origin: "https://example.com",
    formActionOrigin: NEW_PASSWORD_TEMPLATE_ARG.actionOrigin,
  });

  let LMP = new LoginManagerParent();
  LMP.useBrowsingContext(123);

  Assert.ok(LMP.doAutocompleteSearch, "doAutocompleteSearch exists");

  let result1 = await LMP.doAutocompleteSearch(
    "https://example.com",
    NEW_PASSWORD_TEMPLATE_ARG
  );
  equal(result1.logins.length, 1, "1 login");
  Assert.ok(result1.generatedPassword, "has a generated password");
  equal(result1.generatedPassword.length, 15, "generated password length");
  Assert.ok(
    !result1.willAutoSaveGeneratedPassword,
    "won't auto-save when an empty-username match is found"
  );

  LoginTestUtils.clearData();
});