tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_IPProtectionService.js (5059B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { AddonTestUtils } = ChromeUtils.importESModule(
      7  "resource://testing-common/AddonTestUtils.sys.mjs"
      8 );
      9 const { ExtensionTestUtils } = ChromeUtils.importESModule(
     10  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
     11 );
     12 const { IPPEnrollAndEntitleManager } = ChromeUtils.importESModule(
     13  "moz-src:///browser/components/ipprotection/IPPEnrollAndEntitleManager.sys.mjs"
     14 );
     15 
     16 do_get_profile();
     17 
     18 AddonTestUtils.init(this);
     19 AddonTestUtils.createAppInfo(
     20  "xpcshell@tests.mozilla.org",
     21  "XPCShell",
     22  "1",
     23  "1"
     24 );
     25 
     26 ExtensionTestUtils.init(this);
     27 
     28 add_setup(async function () {
     29  await putServerInRemoteSettings();
     30  IPProtectionService.uninit();
     31 
     32  registerCleanupFunction(async () => {
     33    await IPProtectionService.init();
     34  });
     35 });
     36 
     37 /**
     38 * Tests that a signed in status sends a status changed event.
     39 */
     40 add_task(async function test_IPProtectionService_updateState_signedIn() {
     41  let sandbox = sinon.createSandbox();
     42  sandbox
     43    .stub(IPPEnrollAndEntitleManager, "isEnrolledAndEntitled")
     44    .get(() => true);
     45 
     46  await IPProtectionService.init();
     47 
     48  setupStubs(sandbox);
     49 
     50  let signedInEventPromise = waitForEvent(
     51    IPProtectionService,
     52    "IPProtectionService:StateChanged",
     53    () => IPProtectionService.state === IPProtectionStates.READY
     54  );
     55 
     56  IPProtectionService.updateState();
     57 
     58  await signedInEventPromise;
     59 
     60  Assert.ok(IPPSignInWatcher.isSignedIn, "Should be signed in after update");
     61 
     62  IPProtectionService.uninit();
     63  sandbox.restore();
     64 });
     65 
     66 /**
     67 * Tests that any other status sends a changed event event.
     68 */
     69 add_task(async function test_IPProtectionService_updateState_signedOut() {
     70  let sandbox = sinon.createSandbox();
     71  setupStubs(sandbox);
     72 
     73  await IPProtectionService.init();
     74 
     75  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => false);
     76 
     77  let signedOutEventPromise = waitForEvent(
     78    IPProtectionService,
     79    "IPProtectionService:StateChanged",
     80    () => IPProtectionService.state === IPProtectionStates.UNAVAILABLE
     81  );
     82 
     83  IPProtectionService.updateState();
     84 
     85  await signedOutEventPromise;
     86 
     87  Assert.ok(
     88    !IPPSignInWatcher.isSignedIn,
     89    "Should not be signed in after update"
     90  );
     91 
     92  IPProtectionService.uninit();
     93  sandbox.restore();
     94 });
     95 
     96 /**
     97 * Tests that refetchEntitlement works as expected if a linked VPN is found and sends an event.
     98 */
     99 add_task(
    100  async function test_IPProtectionService_refetchEntitlement_has_vpn_linked() {
    101    const sandbox = sinon.createSandbox();
    102    setupStubs(sandbox);
    103 
    104    const waitForReady = waitForEvent(
    105      IPProtectionService,
    106      "IPProtectionService:StateChanged",
    107      () => IPProtectionService.state === IPProtectionStates.READY
    108    );
    109 
    110    IPProtectionService.init();
    111    await waitForReady;
    112 
    113    IPProtectionService.guardian.fetchUserInfo.resolves({
    114      status: 200,
    115      error: null,
    116      entitlement: {
    117        subscribed: true,
    118        uid: 42,
    119        created_at: "2023-01-01T12:00:00.000Z",
    120      },
    121    });
    122 
    123    let hasUpgradedEventPromise = waitForEvent(
    124      IPPEnrollAndEntitleManager,
    125      "IPPEnrollAndEntitleManager:StateChanged",
    126      () => IPPEnrollAndEntitleManager.hasUpgraded
    127    );
    128 
    129    await IPPEnrollAndEntitleManager.refetchEntitlement();
    130 
    131    await hasUpgradedEventPromise;
    132 
    133    Assert.ok(
    134      IPPEnrollAndEntitleManager.hasUpgraded,
    135      "hasUpgraded should be true"
    136    );
    137 
    138    IPProtectionService.uninit();
    139    sandbox.restore();
    140  }
    141 );
    142 
    143 /**
    144 * Tests that refetchEntitlement returns errors if no linked VPN is found and
    145 * sends an event.
    146 */
    147 add_task(
    148  async function test_IPProtectionService_refetchEntitlement_no_vpn_linked() {
    149    const sandbox = sinon.createSandbox();
    150    setupStubs(sandbox);
    151 
    152    await IPProtectionService.init();
    153 
    154    IPProtectionService.guardian.fetchUserInfo.resolves({
    155      status: 404,
    156      error: "invalid_response",
    157      validEntitlement: false,
    158    });
    159 
    160    let hasUpgradedEventPromise = waitForEvent(
    161      IPPEnrollAndEntitleManager,
    162      "IPPEnrollAndEntitleManager:StateChanged"
    163    );
    164 
    165    await IPPEnrollAndEntitleManager.refetchEntitlement();
    166 
    167    await hasUpgradedEventPromise;
    168 
    169    Assert.ok(
    170      !IPPEnrollAndEntitleManager.hasUpgraded,
    171      "hasUpgraded should be false"
    172    );
    173 
    174    IPProtectionService.uninit();
    175    sandbox.restore();
    176  }
    177 );
    178 
    179 /**
    180 * Tests that signing off generates a reset of the entitlement and the sending
    181 * of an event.
    182 */
    183 add_task(async function test_IPProtectionService_hasUpgraded_signed_out() {
    184  let sandbox = sinon.createSandbox();
    185  setupStubs(sandbox);
    186 
    187  await IPProtectionService.init();
    188 
    189  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => false);
    190 
    191  let signedOutEventPromise = waitForEvent(
    192    IPProtectionService,
    193    "IPProtectionService:StateChanged"
    194  );
    195  IPProtectionService.updateState();
    196 
    197  await signedOutEventPromise;
    198 
    199  Assert.ok(
    200    !IPPEnrollAndEntitleManager.hasUpgraded,
    201    "hasUpgraded should be false in after signing out"
    202  );
    203 
    204  IPProtectionService.uninit();
    205  sandbox.restore();
    206 });