summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/head_abuse_report.js
blob: 89daa2a219a7343f3c61ce3c8b36d20f74ff5980 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* eslint max-len: ["error", 80] */

/* exported installTestExtension, addCommonAbuseReportTestTasks,
 *          createPromptConfirmEx, DEFAULT_BUILTIN_THEME_ID,
 *          gManagerWindow, handleSubmitRequest, makeWidgetId,
 *          waitForNewWindow, waitClosedWindow, AbuseReporter,
 *          AbuseReporterTestUtils, AddonTestUtils
 */

/* global MockProvider, loadInitialView, closeView */

const { AbuseReporter } = ChromeUtils.importESModule(
  "resource://gre/modules/AbuseReporter.sys.mjs"
);
const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);
const { ExtensionCommon } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionCommon.sys.mjs"
);

const { makeWidgetId } = ExtensionCommon;

const ADDON_ID = "test-extension-to-report@mochi.test";
const REPORT_ENTRY_POINT = "menu";
const BASE_TEST_MANIFEST = {
  name: "Fake extension to report",
  author: "Fake author",
  homepage_url: "https://fake.extension.url/",
};
const DEFAULT_BUILTIN_THEME_ID = "default-theme@mozilla.org";
const EXT_DICTIONARY_ADDON_ID = "fake-dictionary@mochi.test";
const EXT_LANGPACK_ADDON_ID = "fake-langpack@mochi.test";
const EXT_WITH_PRIVILEGED_URL_ID = "ext-with-privileged-url@mochi.test";
const EXT_SYSTEM_ADDON_ID = "test-system-addon@mochi.test";
const EXT_UNSUPPORTED_TYPE_ADDON_ID = "report-unsupported-type@mochi.test";
const THEME_NO_UNINSTALL_ID = "theme-without-perm-can-uninstall@mochi.test";

let gManagerWindow;

AddonTestUtils.initMochitest(this);

async function openAboutAddons(type = "extension") {
  gManagerWindow = await loadInitialView(type);
}

async function closeAboutAddons() {
  if (gManagerWindow) {
    await closeView(gManagerWindow);
    gManagerWindow = null;
  }
}

function waitForNewWindow() {
  return new Promise(resolve => {
    let listener = win => {
      Services.obs.removeObserver(listener, "toplevel-window-ready");
      resolve(win);
    };

    Services.obs.addObserver(listener, "toplevel-window-ready");
  });
}

function waitClosedWindow(win) {
  return new Promise((resolve, reject) => {
    function onWindowClosed() {
      if (win && !win.closed) {
        // If a specific window reference has been passed, then check
        // that the window is closed before resolving the promise.
        return;
      }
      Services.obs.removeObserver(onWindowClosed, "xul-window-destroyed");
      resolve();
    }
    Services.obs.addObserver(onWindowClosed, "xul-window-destroyed");
  });
}

async function installTestExtension(
  id = ADDON_ID,
  type = "extension",
  manifest = {}
) {
  let additionalProps = {
    icons: {
      32: "test-icon.png",
    },
  };

  switch (type) {
    case "theme":
      additionalProps = {
        ...additionalProps,
        theme: {
          colors: {
            frame: "#a14040",
            tab_background_text: "#fac96e",
          },
        },
      };
      break;

    // TODO(Bug 1789718): Remove after the deprecated XPIProvider-based
    // implementation is also removed.
    case "sitepermission-deprecated":
      additionalProps = {
        name: "WebMIDI test addon for https://mochi.test",
        install_origins: ["https://mochi.test"],
        site_permissions: ["midi"],
      };
      break;
    case "extension":
      break;
    default:
      throw new Error(`Unexpected addon type: ${type}`);
  }

  const extensionOpts = {
    manifest: {
      ...BASE_TEST_MANIFEST,
      ...additionalProps,
      ...manifest,
      browser_specific_settings: { gecko: { id } },
    },
    useAddonManager: "temporary",
  };

  // TODO(Bug 1789718): Remove after the deprecated XPIProvider-based
  // implementation is also removed.
  if (type === "sitepermission-deprecated") {
    const xpi = AddonTestUtils.createTempWebExtensionFile(extensionOpts);
    const addon = await AddonManager.installTemporaryAddon(xpi);
    // The extension object that ExtensionTestUtils.loadExtension returns for
    // mochitest is pretty tight to the Extension class, and so for now this
    // returns a more minimal `extension` test object which only provides the
    // `unload` method.
    //
    // For the purpose of the abuse reports tests that are using this helper
    // this should be already enough.
    return {
      addon,
      unload: () => addon.uninstall(),
    };
  }

  const extension = ExtensionTestUtils.loadExtension(extensionOpts);
  await extension.startup();
  return extension;
}

function handleSubmitRequest({ request, response }) {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "application/json", false);
  response.write("{}");
}

const AbuseReportTestUtils = {
  _mockProvider: null,
  _mockServer: null,
  _abuseRequestHandlers: [],

  // Mock addon details API endpoint.
  amoAddonDetailsMap: new Map(),

  // Setup the test environment by setting the expected prefs and
  // initializing MockProvider and the mock AMO server.
  async setup() {
    // Enable html about:addons and the abuse reporting and
    // set the api endpoints url to the mock service.
    await SpecialPowers.pushPrefEnv({
      set: [
        ["extensions.abuseReport.enabled", true],
        ["extensions.abuseReport.url", "http://test.addons.org/api/report/"],
        [
          "extensions.abuseReport.amoDetailsURL",
          "http://test.addons.org/api/addons/addon/",
        ],
      ],
    });

    this._setupMockProvider();
    this._setupMockServer();
  },

  // Returns the currently open abuse report dialog window (if any).
  getReportDialog() {
    return Services.ww.getWindowByName("addons-abuse-report-dialog");
  },

  // Returns the parameters related to the report dialog (if any).
  getReportDialogParams() {
    const win = this.getReportDialog();
    return win && win.arguments[0] && win.arguments[0].wrappedJSObject;
  },

  // Returns a reference to the addon-abuse-report element from the currently
  // open abuse report.
  getReportPanel() {
    const win = this.getReportDialog();
    ok(win, "Got an abuse report dialog open");
    return win && win.document.querySelector("addon-abuse-report");
  },

  // Returns the list of abuse report reasons.
  getReasons(abuseReportEl) {
    return Object.keys(abuseReportEl.ownerGlobal.ABUSE_REPORT_REASONS);
  },

  // Returns the info related to a given abuse report reason.
  getReasonInfo(abuseReportEl, reason) {
    return abuseReportEl.ownerGlobal.ABUSE_REPORT_REASONS[reason];
  },

  async promiseReportOpened({ addonId, reportEntryPoint, managerWindow }) {
    let abuseReportEl;

    if (!this.getReportDialog()) {
      info("Wait for the report dialog window");
      const dialog = await waitForNewWindow();
      is(dialog, this.getReportDialog(), "Report dialog opened");
    }

    info("Wait for the abuse report panel render");
    abuseReportEl = await AbuseReportTestUtils.promiseReportDialogRendered();

    ok(abuseReportEl, "Got an abuse report panel");
    is(
      abuseReportEl.addon && abuseReportEl.addon.id,
      addonId,
      "Abuse Report panel rendered for the expected addonId"
    );
    is(
      abuseReportEl._report && abuseReportEl._report.reportEntryPoint,
      reportEntryPoint,
      "Abuse Report panel rendered for the expected reportEntryPoint"
    );

    return abuseReportEl;
  },

  // Return a promise resolved when the currently open report panel
  // is closed.
  // Also asserts that a specific report panel element has been closed,
  // if one has been provided through the optional panel parameter.
  async promiseReportClosed(panel) {
    const win = panel ? panel.ownerGlobal : this.getReportDialog();
    if (!win || win.closed) {
      throw Error("Expected report dialog not found or already closed");
    }

    await waitClosedWindow(win);
    // Assert that the panel has been closed (if the caller has passed it).
    if (panel) {
      ok(!panel.ownerGlobal, "abuse report dialog closed");
    }
  },

  // Returns a promise resolved when the report panel has been rendered
  // (rejects is there is no dialog currently open).
  async promiseReportDialogRendered() {
    const params = this.getReportDialogParams();
    if (!params) {
      throw new Error("abuse report dialog not found");
    }
    return params.promiseReportPanel;
  },

  // Given a `requestHandler` function, an HTTP server handler function
  // to use to handle a report submit request received by the mock AMO server),
  // returns a promise resolved when the mock AMO server has received and
  // handled the report submit request.
  async promiseReportSubmitHandled(requestHandler) {
    if (typeof requestHandler !== "function") {
      throw new Error("requestHandler should be a function");
    }
    return new Promise((resolve, reject) => {
      this._abuseRequestHandlers.unshift({ resolve, reject, requestHandler });
    });
  },

  // Return a promise resolved to the abuse report panel element,
  // once its rendering is completed.
  // If abuseReportEl is undefined, it looks for the currently opened
  // report panel.
  async promiseReportRendered(abuseReportEl) {
    let el = abuseReportEl;

    if (!el) {
      const win = this.getReportDialog();
      if (!win) {
        await waitForNewWindow();
      }

      el = await this.promiseReportDialogRendered();
      ok(el, "Got an abuse report panel");
    }

    return el._radioCheckedReason
      ? el
      : BrowserTestUtils.waitForEvent(
          el,
          "abuse-report:updated",
          "Wait the abuse report panel to be rendered"
        ).then(() => el);
  },

  // A promise resolved when the given abuse report panel element
  // has been rendered. If a panel name ("reasons" or "submit") is
  // passed as a second parameter, it also asserts that the panel is
  // updated to the expected view mode.
  async promiseReportUpdated(abuseReportEl, panel) {
    const evt = await BrowserTestUtils.waitForEvent(
      abuseReportEl,
      "abuse-report:updated",
      "Wait abuse report panel update"
    );

    if (panel) {
      is(evt.detail.panel, panel, `Got a "${panel}" update event`);

      const el = abuseReportEl;
      switch (evt.detail.panel) {
        case "reasons":
          ok(!el._reasonsPanel.hidden, "Reasons panel should be visible");
          ok(el._submitPanel.hidden, "Submit panel should be hidden");
          break;
        case "submit":
          ok(el._reasonsPanel.hidden, "Reasons panel should be hidden");
          ok(!el._submitPanel.hidden, "Submit panel should be visible");
          break;
      }
    }
  },

  // Returns a promise resolved once the expected number of abuse report
  // message bars have been created.
  promiseMessageBars(expectedMessageBarCount) {
    return new Promise(resolve => {
      const details = [];
      function listener(evt) {
        details.push(evt.detail);
        if (details.length >= expectedMessageBarCount) {
          cleanup();
          resolve(details);
        }
      }
      function cleanup() {
        if (gManagerWindow) {
          gManagerWindow.document.removeEventListener(
            "abuse-report:new-message-bar",
            listener
          );
        }
      }
      gManagerWindow.document.addEventListener(
        "abuse-report:new-message-bar",
        listener
      );
    });
  },

  async assertFluentStrings(containerEl) {
    // Make sure all localized elements have defined Fluent strings.
    const localizedEls = Array.from(
      containerEl.querySelectorAll("[data-l10n-id]")
    );
    ok(localizedEls.length, "Got localized elements");
    for (let el of localizedEls) {
      const l10nId = el.getAttribute("data-l10n-id");
      await TestUtils.waitForCondition(
        () => el.textContent !== "",
        `Element with Fluent id '${l10nId}' should not be empty`
      );
    }
  },

  // Assert that the report action is hidden on the addon card
  // for the given about:addons windows and extension id.
  async assertReportActionHidden(gManagerWindow, extId) {
    let addonCard = gManagerWindow.document.querySelector(
      `addon-list addon-card[addon-id="${extId}"]`
    );
    ok(addonCard, `Got the addon-card for the ${extId} test extension`);

    let reportButton = addonCard.querySelector("[action=report]");
    ok(reportButton, `Got the report action for ${extId}`);
    ok(reportButton.hidden, `${extId} report action should be hidden`);
  },

  // Assert that the report panel is hidden (or closed if the report
  // panel is opened in its own dialog window).
  async assertReportPanelHidden() {
    const win = this.getReportDialog();
    ok(!win, "Abuse Report dialog should be initially hidden");
  },

  createMockAddons(mockProviderAddons) {
    this._mockProvider.createAddons(mockProviderAddons);
  },

  async clickPanelButton(buttonEl, { label = undefined } = {}) {
    info(`Clicking the '${buttonEl.textContent.trim() || label}' button`);
    // NOTE: ideally this should synthesize the mouse event,
    // we call the click method to prevent intermittent timeouts
    // due to the mouse event not received by the target element.
    buttonEl.click();
  },

  triggerNewReport(addonId, reportEntryPoint) {
    gManagerWindow.openAbuseReport({ addonId, reportEntryPoint });
  },

  triggerSubmit(reason, message) {
    const reportEl =
      this.getReportDialog().document.querySelector("addon-abuse-report");
    reportEl._form.elements.message.value = message;
    reportEl._form.elements.reason.value = reason;
    reportEl.submit();
  },

  async openReport(addonId, reportEntryPoint = REPORT_ENTRY_POINT) {
    // Close the current about:addons window if it has been leaved open from
    // a previous test case failure.
    if (gManagerWindow) {
      await closeAboutAddons();
    }

    await openAboutAddons();

    let promiseReportPanel = waitForNewWindow().then(() =>
      this.promiseReportDialogRendered()
    );

    this.triggerNewReport(addonId, reportEntryPoint);

    const panelEl = await promiseReportPanel;
    await this.promiseReportRendered(panelEl);
    is(panelEl.addonId, addonId, `Got Abuse Report panel for ${addonId}`);

    return panelEl;
  },

  async closeReportPanel(panelEl) {
    const onceReportClosed = AbuseReportTestUtils.promiseReportClosed(panelEl);

    info("Cancel report and wait the dialog to be closed");
    panelEl.dispatchEvent(new CustomEvent("abuse-report:cancel"));

    await onceReportClosed;
  },

  // Internal helper methods.

  _setupMockProvider() {
    this._mockProvider = new MockProvider();
    this._mockProvider.createAddons([
      {
        id: THEME_NO_UNINSTALL_ID,
        name: "This theme cannot be uninstalled",
        version: "1.1",
        creator: { name: "Theme creator", url: "http://example.com/creator" },
        type: "theme",
        permissions: 0,
      },
      {
        id: EXT_WITH_PRIVILEGED_URL_ID,
        name: "This extension has an unexpected privileged creator URL",
        version: "1.1",
        creator: { name: "creator", url: "about:config" },
        type: "extension",
      },
      {
        id: EXT_SYSTEM_ADDON_ID,
        name: "This is a system addon",
        version: "1.1",
        creator: { name: "creator", url: "http://example.com/creator" },
        type: "extension",
        isSystem: true,
      },
      {
        id: EXT_UNSUPPORTED_TYPE_ADDON_ID,
        name: "This is a fake unsupported addon type",
        version: "1.1",
        type: "unsupported_addon_type",
      },
      {
        id: EXT_LANGPACK_ADDON_ID,
        name: "This is a fake langpack",
        version: "1.1",
        type: "locale",
      },
      {
        id: EXT_DICTIONARY_ADDON_ID,
        name: "This is a fake dictionary",
        version: "1.1",
        type: "dictionary",
      },
    ]);
  },

  _setupMockServer() {
    if (this._mockServer) {
      return;
    }

    // Init test report api server.
    const server = AddonTestUtils.createHttpServer({
      hosts: ["test.addons.org"],
    });
    this._mockServer = server;

    server.registerPathHandler("/api/report/", (request, response) => {
      const stream = request.bodyInputStream;
      const buffer = NetUtil.readInputStream(stream, stream.available());
      const data = new TextDecoder().decode(buffer);
      const promisedHandler = this._abuseRequestHandlers.pop();
      if (promisedHandler) {
        const { requestHandler, resolve, reject } = promisedHandler;
        try {
          requestHandler({ data, request, response });
          resolve();
        } catch (err) {
          ok(false, `Unexpected requestHandler error ${err} ${err.stack}\n`);
          reject(err);
        }
      } else {
        ok(false, `Unexpected request: ${request.path} ${data}`);
      }
    });

    server.registerPrefixHandler("/api/addons/addon/", (request, response) => {
      const addonId = request.path.split("/").pop();
      if (!this.amoAddonDetailsMap.has(addonId)) {
        response.setStatusLine(request.httpVersion, 404, "Not Found");
        response.write(JSON.stringify({ detail: "Not found." }));
      } else {
        response.setStatusLine(request.httpVersion, 200, "Success");
        response.write(JSON.stringify(this.amoAddonDetailsMap.get(addonId)));
      }
    });
    server.registerPathHandler(
      "/assets/fake-icon-url.png",
      (request, response) => {
        response.setStatusLine(request.httpVersion, 200, "Success");
        response.write("");
        response.finish();
      }
    );
  },
};

function createPromptConfirmEx({
  remove = false,
  report = false,
  expectCheckboxHidden = false,
} = {}) {
  return (...args) => {
    const checkboxState = args.pop();
    const checkboxMessage = args.pop();
    is(
      checkboxState && checkboxState.value,
      false,
      "checkboxState should be initially false"
    );
    if (expectCheckboxHidden) {
      ok(
        !checkboxMessage,
        "Should not have a checkboxMessage in promptService.confirmEx call"
      );
    } else {
      ok(
        checkboxMessage,
        "Got a checkboxMessage in promptService.confirmEx call"
      );
    }

    // Report checkbox selected.
    checkboxState.value = report;

    // Remove accepted.
    return remove ? 0 : 1;
  };
}