summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/tests/browser/browser_safari_passwords.js
blob: 299695f9e335b3289ef1e8aaa4b448efee80ede2 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { SafariProfileMigrator } = ChromeUtils.importESModule(
  "resource:///modules/SafariProfileMigrator.sys.mjs"
);
const { LoginCSVImport } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginCSVImport.sys.mjs"
);

const TEST_FILE_PATH = getTestFilePath("dummy_file.csv");

// We use MockFilePicker to simulate a native file picker, and prepare it
// to return a dummy file pointed at TEST_FILE_PATH. The file at
// TEST_FILE_PATH is not required (nor expected) to exist.
const { MockFilePicker } = SpecialPowers;

add_setup(async function () {
  MockFilePicker.init(window);
  registerCleanupFunction(() => {
    MockFilePicker.cleanup();
  });
  await SpecialPowers.pushPrefEnv({
    set: [["signon.management.page.fileImport.enabled", true]],
  });
});

/**
 * A helper function that does most of the heavy lifting for the tests in
 * this file. Specfically, it takes care of:
 *
 * 1. Stubbing out the various hunks of the SafariProfileMigrator in order
 *    to simulate a migration without actually performing one, since the
 *    migrator itself isn't being tested here.
 * 2. Stubbing out parts of MigrationUtils and LoginCSVImport to have a
 *    consistent reporting on how many things are imported.
 * 3. Setting up the MockFilePicker if expectsFilePicker is true to return
 *    the TEST_FILE_PATH.
 * 4. Opens up the migration wizard, and chooses to import both BOOKMARKS
 *    and PASSWORDS, and then clicks "Import".
 * 5. Waits for the migration wizard to show the Safari password import
 *    instructions.
 * 6. Runs taskFn
 * 7. Closes the migration dialog.
 *
 * @param {boolean} expectsFilePicker
 *   True if the MockFilePicker should be set up to return TEST_FILE_PATH.
 * @param {boolean} migrateBookmarks
 *   True if bookmarks should be migrated alongside passwords. If not, only
 *   passwords will be migrated.
 * @param {Function} taskFn
 *   An asynchronous function that takes the following parameters in this
 *   order:
 *
 *    {Element} wizard
 *      The opened migration wizard
 *    {Promise} filePickerShownPromise
 *      A Promise that resolves once the MockFilePicker has closed. This is
 *      undefined if expectsFilePicker was false.
 *    {object} importFromCSVStub
 *      The Sinon stub object for LoginCSVImport.importFromCSV. This can be
 *      used to check to see whether it was called.
 *    {Promise} didMigration
 *      A Promise that resolves to true once the migration completes.
 *    {Promise} wizardDone
 *      A Promise that resolves once the migration wizard reports that a
 *      migration has completed.
 * @returns {Promise<undefined>}
 */
async function testSafariPasswordHelper(
  expectsFilePicker,
  migrateBookmarks,
  taskFn
) {
  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  let safariMigrator = new SafariProfileMigrator();
  sandbox.stub(MigrationUtils, "getMigrator").resolves(safariMigrator);

  // We're not testing the permission flow here, so let's pretend that we
  // always have permission to read resources from the disk.
  sandbox
    .stub(SafariProfileMigrator.prototype, "hasPermissions")
    .resolves(true);

  // Have the migrator claim that only BOOKMARKS are only available.
  sandbox
    .stub(SafariProfileMigrator.prototype, "getMigrateData")
    .resolves(MigrationUtils.resourceTypes.BOOKMARKS);

  let migrateStub;
  let didMigration = new Promise(resolve => {
    migrateStub = sandbox
      .stub(SafariProfileMigrator.prototype, "migrate")
      .callsFake((aResourceTypes, aStartup, aProfile, aProgressCallback) => {
        if (!migrateBookmarks) {
          Assert.ok(
            false,
            "Should not have called migrate when only migrating Safari passwords."
          );
        }

        Assert.ok(
          !aStartup,
          "Migrator should not have been called as a startup migration."
        );
        Assert.ok(
          aResourceTypes & MigrationUtils.resourceTypes.BOOKMARKS,
          "Should have requested to migrate the BOOKMARKS resource."
        );
        Assert.ok(
          !(aResourceTypes & MigrationUtils.resourceTypes.PASSWORDS),
          "Should not have requested to migrate the PASSWORDS resource."
        );

        aProgressCallback(MigrationUtils.resourceTypes.BOOKMARKS);
        Services.obs.notifyObservers(null, "Migration:Ended");
        resolve();
      });
  });

  // We'll pretend we added EXPECTED_QUANTITY passwords from the Safari
  // password file.
  let results = [];
  for (let i = 0; i < EXPECTED_QUANTITY; ++i) {
    results.push({ result: "added" });
  }
  let importFromCSVStub = sandbox
    .stub(LoginCSVImport, "importFromCSV")
    .resolves(results);

  sandbox.stub(MigrationUtils, "_importQuantities").value({
    bookmarks: EXPECTED_QUANTITY,
  });

  let filePickerShownPromise;

  if (expectsFilePicker) {
    MockFilePicker.reset();

    let dummyFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
    dummyFile.initWithPath(TEST_FILE_PATH);
    filePickerShownPromise = new Promise(resolve => {
      MockFilePicker.showCallback = () => {
        Assert.ok(true, "Filepicker shown.");
        MockFilePicker.setFiles([dummyFile]);
        resolve();
      };
    });
    MockFilePicker.returnValue = MockFilePicker.returnOK;
  }

  await withMigrationWizardDialog(async prefsWin => {
    let dialogBody = prefsWin.document.body;
    let wizard = dialogBody.querySelector("migration-wizard");
    let wizardDone = BrowserTestUtils.waitForEvent(
      wizard,
      "MigrationWizard:DoneMigration"
    );

    let shadow = wizard.openOrClosedShadowRoot;

    info("Choosing Safari");
    let panelItem = wizard.querySelector(
      `panel-item[key="${SafariProfileMigrator.key}"]`
    );
    panelItem.click();

    let resourceTypeList = shadow.querySelector("#resource-type-list");

    // Let's choose whether to import BOOKMARKS first.
    let bookmarksNode = resourceTypeList.querySelector(
      `label[data-resource-type="${MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.BOOKMARKS}"]`
    );
    bookmarksNode.control.checked = migrateBookmarks;

    // Let's make sure that PASSWORDS is displayed despite the migrator only
    // (currently) returning BOOKMARKS as an available resource to migrate.
    let passwordsNode = resourceTypeList.querySelector(
      `label[data-resource-type="${MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS}"]`
    );
    Assert.ok(
      !passwordsNode.hidden,
      "PASSWORDS should be available to import from."
    );
    passwordsNode.control.checked = true;

    let deck = shadow.querySelector("#wizard-deck");
    let switchedToSafariPermissionPage =
      BrowserTestUtils.waitForMutationCondition(
        deck,
        { attributeFilter: ["selected-view"] },
        () => {
          return (
            deck.getAttribute("selected-view") ==
            "page-" + MigrationWizardConstants.PAGES.SAFARI_PASSWORD_PERMISSION
          );
        }
      );

    let importButton = shadow.querySelector("#import");
    importButton.click();
    await switchedToSafariPermissionPage;
    Assert.ok(true, "Went to Safari permission page after attempting import.");

    await taskFn(
      wizard,
      filePickerShownPromise,
      importFromCSVStub,
      didMigration,
      migrateStub,
      wizardDone
    );

    let dialog = prefsWin.document.querySelector("#migrationWizardDialog");
    let doneButton = shadow.querySelector(
      "div[name='page-progress'] .done-button"
    );
    let dialogClosed = BrowserTestUtils.waitForEvent(dialog, "close");

    doneButton.click();
    await dialogClosed;
  });

  sandbox.restore();
  MockFilePicker.reset();
}

/**
 * Tests the flow of importing passwords from Safari via an
 * exported CSV file.
 */
add_task(async function test_safari_password_do_import() {
  await testSafariPasswordHelper(
    true,
    true,
    async (
      wizard,
      filePickerShownPromise,
      importFromCSVStub,
      didMigration,
      migrateStub,
      wizardDone
    ) => {
      let shadow = wizard.openOrClosedShadowRoot;
      let safariPasswordImportSelect = shadow.querySelector(
        "#safari-password-import-select"
      );
      safariPasswordImportSelect.click();
      await filePickerShownPromise;
      Assert.ok(true, "File picker was shown.");

      await didMigration;
      Assert.ok(importFromCSVStub.called, "Importing from CSV was called.");

      await wizardDone;

      assertQuantitiesShown(wizard, [
        MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.BOOKMARKS,
        MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS,
      ]);
    }
  );
});

/**
 * Tests that only passwords get imported if the user only opts
 * to import passwords, and that nothing else gets imported.
 */
add_task(async function test_safari_password_only_do_import() {
  await testSafariPasswordHelper(
    true,
    false,
    async (
      wizard,
      filePickerShownPromise,
      importFromCSVStub,
      didMigration,
      migrateStub,
      wizardDone
    ) => {
      let shadow = wizard.openOrClosedShadowRoot;
      let safariPasswordImportSelect = shadow.querySelector(
        "#safari-password-import-select"
      );
      safariPasswordImportSelect.click();
      await filePickerShownPromise;
      Assert.ok(true, "File picker was shown.");

      await wizardDone;

      assertQuantitiesShown(wizard, [
        MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS,
      ]);

      Assert.ok(importFromCSVStub.called, "Importing from CSV was called.");
      Assert.ok(
        !migrateStub.called,
        "SafariProfileMigrator.migrate was not called."
      );
    }
  );
});

/**
 * Tests that the user can skip importing passwords from Safari.
 */
add_task(async function test_safari_password_skip() {
  await testSafariPasswordHelper(
    false,
    true,
    async (
      wizard,
      filePickerShownPromise,
      importFromCSVStub,
      didMigration,
      migrateStub,
      wizardDone
    ) => {
      let shadow = wizard.openOrClosedShadowRoot;
      let safariPasswordImportSkip = shadow.querySelector(
        "#safari-password-import-skip"
      );
      safariPasswordImportSkip.click();

      await didMigration;
      Assert.ok(!MockFilePicker.shown, "Never showed the file picker.");
      Assert.ok(
        !importFromCSVStub.called,
        "Importing from CSV was never called."
      );

      await wizardDone;

      assertQuantitiesShown(wizard, [
        MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.BOOKMARKS,
      ]);
    }
  );
});

/**
 * Tests that importing from passwords for Safari doesn't exist if
 * signon.management.page.fileImport.enabled is false.
 */
add_task(async function test_safari_password_disabled() {
  await SpecialPowers.pushPrefEnv({
    set: [["signon.management.page.fileImport.enabled", false]],
  });

  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  let safariMigrator = new SafariProfileMigrator();
  sandbox.stub(MigrationUtils, "getMigrator").resolves(safariMigrator);

  // We're not testing the permission flow here, so let's pretend that we
  // always have permission to read resources from the disk.
  sandbox
    .stub(SafariProfileMigrator.prototype, "hasPermissions")
    .resolves(true);

  // Have the migrator claim that only BOOKMARKS are only available.
  sandbox
    .stub(SafariProfileMigrator.prototype, "getMigrateData")
    .resolves(MigrationUtils.resourceTypes.BOOKMARKS);

  await withMigrationWizardDialog(async prefsWin => {
    let dialogBody = prefsWin.document.body;
    let wizard = dialogBody.querySelector("migration-wizard");

    let shadow = wizard.openOrClosedShadowRoot;

    info("Choosing Safari");
    let panelItem = wizard.querySelector(
      `panel-item[key="${SafariProfileMigrator.key}"]`
    );
    panelItem.click();

    let resourceTypeList = shadow.querySelector("#resource-type-list");

    // Let's make sure that PASSWORDS is displayed despite the migrator only
    // (currently) returning BOOKMARKS as an available resource to migrate.
    let passwordsNode = resourceTypeList.querySelector(
      `label[data-resource-type="${MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS}"]`
    );
    Assert.ok(
      passwordsNode.hidden,
      "PASSWORDS should not be available to import from."
    );
  });

  await SpecialPowers.popPrefEnv();
});