summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_doorhanger_autofill_then_save_password.js
blob: b30b575752626802c957fd2b0c2fb34b056d2a01 (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
/**
 * Test that after we autofill, the site makes changes to the login, and then the
 * user modifies their login, a save/update doorhanger is shown.
 *
 * This is a regression test for Bug 1632405.
 */

const testCases = [
  {
    name: "autofill, then delete u/p, then fill new u/p should show 'save'",
    oldUsername: "oldUsername",
    oldPassword: "oldPassword",
    actions: [
      {
        setUsername: "newUsername",
      },
      {
        setPassword: "newPassword",
      },
    ],
    expectedNotification: "addLogin",
    expectedDoorhanger: "password-save",
  },
  {
    name: "autofill, then delete password, then fill new password should show 'update'",
    oldUsername: "oldUsername",
    oldPassword: "oldPassword",
    actions: [
      {
        setPassword: "newPassword",
      },
    ],
    expectedNotification: "modifyLogin",
    expectedDoorhanger: "password-change",
  },
];

for (let testData of testCases) {
  let tmp = {
    async [testData.name]() {
      info("testing with: " + JSON.stringify(testData));
      await test_save_change(testData);
    },
  };
  add_task(tmp[testData.name]);
}

async function test_save_change({
  name,
  oldUsername,
  oldPassword,
  actions,
  expectedNotification,
  expectedDoorhanger,
}) {
  let originalPrefValue = await Services.prefs.getBoolPref(
    "signon.testOnlyUserHasInteractedByPrefValue"
  );
  Services.prefs.setBoolPref(
    "signon.testOnlyUserHasInteractedByPrefValue",
    false
  );

  info("Starting test: " + name);

  await LoginTestUtils.addLogin({
    username: oldUsername,
    password: oldPassword,
    origin: "https://example.com",
    formActionOrigin: "https://example.com",
  });

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

      await ContentTask.spawn(
        browser,
        { oldUsername, oldPassword },
        async function awaitAutofill({ oldUsername, oldPassword }) {
          await ContentTaskUtils.waitForCondition(
            () =>
              content.document.querySelector("#form-basic-username").value ==
                oldUsername &&
              content.document.querySelector("#form-basic-password").value ==
                oldPassword,
            "Await and verify autofill"
          );

          info(
            "Triggering a page navigation that is not initiated by the user"
          );
          content.history.replaceState({}, "", "");
        }
      );

      Services.prefs.setBoolPref(
        "signon.testOnlyUserHasInteractedByPrefValue",
        true
      );

      for (let action of actions) {
        info(`As the user, update form with action: ${JSON.stringify(action)}`);
        if (typeof action.setUsername !== "undefined") {
          await changeContentFormValues(browser, {
            "#form-basic-username": action.setUsername,
          });
        }
        if (typeof action.setPassword !== "undefined") {
          await changeContentFormValues(browser, {
            "#form-basic-password": action.setPassword,
          });
        }
      }

      let expectedUsername =
        [...actions]
          .reverse()
          .map(action => action.setUsername)
          .find(username => !!username) ?? oldUsername;
      let expectedPassword =
        [...actions]
          .reverse()
          .map(action => action.setPassword)
          .find(username => !!username) ?? oldPassword;

      await ContentTask.spawn(
        browser,
        { expectedUsername, expectedPassword },
        async function awaitAutofill({ expectedUsername, expectedPassword }) {
          info("Validating updated fields");
          Assert.equal(
            expectedUsername,
            content.document.querySelector("#form-basic-username").value,
            "Verify username field updated"
          );
          Assert.equal(
            expectedPassword,
            content.document.querySelector("#form-basic-password").value,
            "Verify password field updated"
          );
        }
      );

      const formSubmittedPromise = listenForTestNotification("ShowDoorhanger");
      await SpecialPowers.spawn(browser, [], async function () {
        let doc = this.content.document;
        doc.getElementById("form-basic").submit();
      });
      await formSubmittedPromise;

      info("Waiting for doorhanger of type: " + expectedDoorhanger);
      const notif = await waitForDoorhanger(browser, expectedDoorhanger);

      await checkDoorhangerUsernamePassword(expectedUsername, expectedPassword);

      const promiseLogin = TestUtils.topicObserved(
        "passwordmgr-storage-changed",
        (_, data) => data == expectedNotification
      );

      await clickDoorhangerButton(notif, REMEMBER_BUTTON);
      await promiseLogin;
      await cleanupDoorhanger(notif); // clean slate for the next test

      Services.prefs.setBoolPref(
        "signon.testOnlyUserHasInteractedByPrefValue",
        originalPrefValue
      );
    }
  );

  // Clean up the database before the next test case is executed.
  Services.logins.removeAllUserFacingLogins();
}