summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/captivePortal/browser_captivePortal_https_only.js
blob: 789d392107033abfeafe7a43e265949ee634692d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";
const { PermissionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PermissionTestUtils.sys.mjs"
);
const testPath = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  "http://example.com"
);
const CANONICAL_URI = Services.io.newURI(testPath);
const PERMISSION_NAME = "https-only-load-insecure";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    // That changes the canoncicalURL from "http://{server}/captive-detect/success.txt"
    // to http://example.com
    set: [
      ["captivedetect.canonicalURL", testPath],
      ["dom.security.https_only_mode", true],
    ],
  });
});

// This test checks if https-only exempts the canoncial uri.
add_task(async function checkCaptivePortalExempt() {
  await portalDetected();
  info("Checking that the canonical uri is exempt by https-only mode");
  let tab = await openCaptivePortalErrorTab();
  let browser = tab.linkedBrowser;
  let portalTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, testPath);

  await SpecialPowers.spawn(browser, [], async () => {
    let doc = content.document;
    let loginButton = doc.getElementById("openPortalLoginPageButton");
    await ContentTaskUtils.waitForCondition(
      () => ContentTaskUtils.is_visible(loginButton),
      "Captive portal error page UI is visible"
    );

    if (!Services.focus.focusedElement == loginButton) {
      await ContentTaskUtils.waitForEvent(loginButton, "focus");
    }

    Assert.ok(true, "openPortalLoginPageButton has focus");
    info("Clicking the Open Login Page button");
    await EventUtils.synthesizeMouseAtCenter(loginButton, {}, content);
  });
  is(
    PermissionTestUtils.testPermission(CANONICAL_URI, PERMISSION_NAME),
    Services.perms.ALLOW_ACTION,
    "Check permission in perm. manager if canoncial uri is set as exempt."
  );
  let portalTab = await portalTabPromise;
  is(
    gBrowser.selectedTab,
    portalTab,
    "Login page should be open in a new foreground tab."
  );
  is(
    gBrowser.currentURI.spec,
    testPath,
    "Opened the right URL without upgrading it."
  );
  // Close all tabs
  await BrowserTestUtils.removeTab(portalTab);
  let tabReloaded = BrowserTestUtils.waitForErrorPage(tab.linkedBrowser);
  Services.obs.notifyObservers(null, "captive-portal-login-success");
  await tabReloaded;
  await BrowserTestUtils.removeTab(tab);
});