summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_doorhanger_password_edits.js
blob: 414208eaa5ca2ab0849b2d07aabe9e664b6480b0 (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
/**
 * Test changing the password inside the doorhanger notification for passwords.
 *
 * We check the following cases:
 *   - Editing the password of a new login.
 *   - Editing the password of an existing login.
 *   - Changing both username and password to an existing login.
 *   - Changing the username to an existing login.
 *   - Editing username to an empty one and a new password.
 *
 * If both the username and password matches an already existing login, we should not
 * update it's password, but only it's usage timestamp and count.
 */
add_task(async function test_edit_password() {
  let testCases = [
    {
      description: "No saved logins, update password in doorhanger",
      usernameInPage: "username",
      passwordInPage: "password",
      passwordChangedTo: "newPassword",
      timesUsed: 1,
    },
    {
      description: "Login is saved, update password in doorhanger",
      usernameInPage: "username",
      usernameInPageExists: true,
      passwordInPage: "password",
      passwordInStorage: "oldPassword",
      passwordChangedTo: "newPassword",
      timesUsed: 2,
    },
    {
      description:
        "Change username in doorhanger to match saved login, update password in doorhanger",
      usernameInPage: "username",
      usernameChangedTo: "newUsername",
      usernameChangedToExists: true,
      passwordInPage: "password",
      passwordChangedTo: "newPassword",
      timesUsed: 2,
    },
    {
      description:
        "Change username in doorhanger to match saved login, dont update password in doorhanger",
      usernameInPage: "username",
      usernameChangedTo: "newUsername",
      usernameChangedToExists: true,
      passwordInPage: "password",
      passwordChangedTo: "password",
      timesUsed: 2,
      checkPasswordNotUpdated: true,
    },
    {
      description:
        "Change username and password in doorhanger to match saved empty-username login",
      usernameInPage: "newUsername",
      usernameChangedTo: "",
      usernameChangedToExists: true,
      passwordInPage: "password",
      passwordChangedTo: "newPassword",
      timesUsed: 2,
    },
  ];

  for (let testCase of testCases) {
    info("Test case: " + JSON.stringify(testCase));
    // Clean state before the test case is executed.
    await LoginTestUtils.clearData();
    await cleanupDoorhanger();
    await cleanupPasswordNotifications();

    // Create the pre-existing logins when needed.
    if (testCase.usernameInPageExists) {
      await Services.logins.addLoginAsync(
        LoginTestUtils.testData.formLogin({
          origin: "https://example.com",
          formActionOrigin: "https://example.com",
          username: testCase.usernameInPage,
          password: testCase.passwordInStorage,
        })
      );
    }

    if (testCase.usernameChangedToExists) {
      await Services.logins.addLoginAsync(
        LoginTestUtils.testData.formLogin({
          origin: "https://example.com",
          formActionOrigin: "https://example.com",
          username: testCase.usernameChangedTo,
          password: testCase.passwordChangedTo,
        })
      );
    }

    let formFilledPromise = listenForTestNotification("FormProcessed");

    await BrowserTestUtils.withNewTab(
      {
        gBrowser,
        url:
          "https://example.com/browser/toolkit/components/" +
          "passwordmgr/test/browser/form_basic.html",
      },
      async function (browser) {
        await formFilledPromise;

        // Set the form to a known state so we can expect a single PasswordEditedOrGenerated message
        await initForm(browser, {
          "#form-basic-username": testCase.usernameInPage,
          "#form-basic-password": "",
        });

        let passwordEditedPromise = listenForTestNotification(
          "PasswordEditedOrGenerated"
        );
        info("Editing the form");
        await changeContentFormValues(browser, {
          "#form-basic-password": testCase.passwordInPage,
        });
        info("Waiting for passwordEditedPromise");
        await passwordEditedPromise;

        // reset doorhanger/notifications, we're only interested in the submit outcome
        await cleanupDoorhanger();
        await cleanupPasswordNotifications();
        // reset message cache, we're only interested in the submit outcome
        await clearMessageCache(browser);

        // Submit the form in the content page with the credentials from the test
        // case. This will cause the doorhanger notification to be displayed.
        info("Submitting the form");
        let formSubmittedPromise = listenForTestNotification("ShowDoorhanger");
        let promiseShown = BrowserTestUtils.waitForEvent(
          PopupNotifications.panel,
          "popupshown",
          event => event.target == PopupNotifications.panel
        );
        await SpecialPowers.spawn(browser, [], function () {
          content.document.getElementById("form-basic").submit();
        });
        await formSubmittedPromise;

        let notif = await waitForDoorhanger(browser, "any");
        Assert.ok(!notif.dismissed, "Doorhanger is not dismissed");
        await promiseShown;

        // Modify the username & password in the dialog if requested.
        await updateDoorhangerInputValues({
          username: testCase.usernameChangedTo,
          password: testCase.passwordChangedTo,
        });

        // We expect a modifyLogin notification if the final username used by the
        // dialog exists in the logins database, otherwise an addLogin one.
        let expectModifyLogin =
          typeof testCase.usernameChangedTo !== "undefined"
            ? testCase.usernameChangedToExists
            : testCase.usernameInPageExists;

        // Simulate the action on the notification to request the login to be
        // saved, and wait for the data to be updated or saved based on the type
        // of operation we expect.
        let expectedNotification = expectModifyLogin
          ? "modifyLogin"
          : "addLogin";
        let promiseLogin = TestUtils.topicObserved(
          "passwordmgr-storage-changed",
          (_, data) => data == expectedNotification
        );

        let promiseHidden = BrowserTestUtils.waitForEvent(
          PopupNotifications.panel,
          "popuphidden"
        );
        clickDoorhangerButton(notif, CHANGE_BUTTON);
        await promiseHidden;
        info("Waiting for storage changed");
        let [result] = await promiseLogin;

        // Check that the values in the database match the expected values.
        let login = expectModifyLogin
          ? result
              .QueryInterface(Ci.nsIArray)
              .queryElementAt(1, Ci.nsILoginInfo)
          : result.QueryInterface(Ci.nsILoginInfo);
        let meta = login.QueryInterface(Ci.nsILoginMetaInfo);

        let expectedLogin = {
          username:
            "usernameChangedTo" in testCase
              ? testCase.usernameChangedTo
              : testCase.usernameInPage,
          password:
            "passwordChangedTo" in testCase
              ? testCase.passwordChangedTo
              : testCase.passwordInPage,
          timesUsed: testCase.timesUsed,
        };
        // Check that the password was not updated if the user is empty
        if (testCase.checkPasswordNotUpdated) {
          expectedLogin.usedSince = meta.timeCreated;
          expectedLogin.timeCreated = meta.timePasswordChanged;
        }
        await verifyLogins([expectedLogin]);
      }
    );
  }
});

async function initForm(browser, formDefaults = {}) {
  await ContentTask.spawn(
    browser,
    formDefaults,
    async function (selectorValues) {
      for (let [sel, value] of Object.entries(selectorValues)) {
        content.document.querySelector(sel).value = value;
      }
    }
  );
}