tor-browser

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

browser_UITour_sync.js (8422B)


      1 "use strict";
      2 
      3 var gTestTab;
      4 var gContentAPI;
      5 
      6 const MOCK_FLOW_ID =
      7  "5445b28b8b7ba6cf71e345f8fff4bc59b2a514f78f3e2cc99b696449427fd445";
      8 const MOCK_FLOW_BEGIN_TIME = 1590780440325;
      9 const MOCK_DEVICE_ID = "7e450f3337d3479b8582ea1c9bb5ba6c";
     10 
     11 const gFxaParams = `context=${Services.prefs.getStringPref("identity.fxaccounts.contextParam")}`;
     12 
     13 registerCleanupFunction(function () {
     14  Services.prefs.clearUserPref("identity.fxaccounts.remote.root");
     15  Services.prefs.clearUserPref("services.sync.username");
     16 });
     17 
     18 add_task(setup_UITourTest);
     19 
     20 add_setup(async function () {
     21  Services.prefs.setCharPref(
     22    "identity.fxaccounts.remote.root",
     23    "https://example.com"
     24  );
     25 });
     26 
     27 add_UITour_task(async function test_checkSyncSetup_disabled() {
     28  let result = await getConfigurationPromise("sync");
     29  is(result.setup, false, "Sync shouldn't be setup by default");
     30 });
     31 
     32 add_UITour_task(async function test_checkSyncSetup_enabled() {
     33  Services.prefs.setCharPref(
     34    "services.sync.username",
     35    "uitour@tests.mozilla.org"
     36  );
     37  let result = await getConfigurationPromise("sync");
     38  is(result.setup, true, "Sync should be setup");
     39 });
     40 
     41 add_UITour_task(async function test_checkSyncCounts() {
     42  Services.prefs.setIntPref("services.sync.clients.devices.desktop", 4);
     43  Services.prefs.setIntPref("services.sync.clients.devices.mobile", 5);
     44  Services.prefs.setIntPref("services.sync.numClients", 9);
     45  let result = await getConfigurationPromise("sync");
     46  is(result.mobileDevices, 5, "mobileDevices should be set");
     47  is(result.desktopDevices, 4, "desktopDevices should be set");
     48  is(result.totalDevices, 9, "totalDevices should be set");
     49 
     50  Services.prefs.clearUserPref("services.sync.clients.devices.desktop");
     51  result = await getConfigurationPromise("sync");
     52  is(result.mobileDevices, 5, "mobileDevices should be set");
     53  is(result.desktopDevices, 0, "desktopDevices should be 0");
     54  is(result.totalDevices, 9, "totalDevices should be set");
     55 
     56  Services.prefs.clearUserPref("services.sync.clients.devices.mobile");
     57  result = await getConfigurationPromise("sync");
     58  is(result.mobileDevices, 0, "mobileDevices should be 0");
     59  is(result.desktopDevices, 0, "desktopDevices should be 0");
     60  is(result.totalDevices, 9, "totalDevices should be set");
     61 
     62  Services.prefs.clearUserPref("services.sync.numClients");
     63  result = await getConfigurationPromise("sync");
     64  is(result.mobileDevices, 0, "mobileDevices should be 0");
     65  is(result.desktopDevices, 0, "desktopDevices should be 0");
     66  is(result.totalDevices, 0, "totalDevices should be 0");
     67 });
     68 
     69 // The showFirefoxAccounts API is sync related, so we test that here too...
     70 add_UITour_task(async function test_firefoxAccountsNoParams() {
     71  info("Load https://accounts.firefox.com");
     72  await gContentAPI.showFirefoxAccounts();
     73  await BrowserTestUtils.browserLoaded(gTestTab.linkedBrowser, false, url =>
     74    url.startsWith(
     75      `https://example.com/?${gFxaParams}&entrypoint=uitour&action=email&service=sync`
     76    )
     77  );
     78 });
     79 
     80 add_UITour_task(async function test_firefoxAccountsValidParams() {
     81  info("Load https://accounts.firefox.com");
     82  await gContentAPI.showFirefoxAccounts({ utm_foo: "foo", utm_bar: "bar" });
     83  await BrowserTestUtils.browserLoaded(
     84    gTestTab.linkedBrowser,
     85    false,
     86    url =>
     87      url.startsWith(
     88        `https://example.com/?${gFxaParams}&entrypoint=uitour&action=email&service=sync`
     89      ) && url.includes("utm_foo=foo&utm_bar=bar")
     90  );
     91 });
     92 
     93 add_UITour_task(async function test_firefoxAccountsWithEmail() {
     94  info("Load https://accounts.firefox.com");
     95  await gContentAPI.showFirefoxAccounts(null, null, "foo@bar.com");
     96  await BrowserTestUtils.browserLoaded(gTestTab.linkedBrowser, false, url =>
     97    url.startsWith(
     98      `https://example.com/?${gFxaParams}&entrypoint=uitour&email=foo%40bar.com&service=sync`
     99    )
    100  );
    101 });
    102 
    103 add_UITour_task(async function test_firefoxAccountsWithEmailAndFlowParams() {
    104  info("Load https://accounts.firefox.com with flow params");
    105  const flowParams = {
    106    flow_id: MOCK_FLOW_ID,
    107    flow_begin_time: MOCK_FLOW_BEGIN_TIME,
    108    device_id: MOCK_DEVICE_ID,
    109  };
    110  await gContentAPI.showFirefoxAccounts(flowParams, null, "foo@bar.com");
    111  await BrowserTestUtils.browserLoaded(
    112    gTestTab.linkedBrowser,
    113    false,
    114    url =>
    115      url.startsWith(
    116        `https://example.com/?${gFxaParams}&entrypoint=uitour&email=foo%40bar.com&service=sync`
    117      ) &&
    118      url.includes(
    119        `flow_id=${MOCK_FLOW_ID}&flow_begin_time=${MOCK_FLOW_BEGIN_TIME}&device_id=${MOCK_DEVICE_ID}`
    120      )
    121  );
    122 });
    123 
    124 add_UITour_task(
    125  async function test_firefoxAccountsWithEmailAndBadFlowParamValues() {
    126    info("Load https://accounts.firefox.com with bad flow params");
    127    const BAD_MOCK_FLOW_ID = "1";
    128    const BAD_MOCK_FLOW_BEGIN_TIME = 100;
    129 
    130    await gContentAPI.showFirefoxAccounts(
    131      {
    132        flow_id: BAD_MOCK_FLOW_ID,
    133        flow_begin_time: MOCK_FLOW_BEGIN_TIME,
    134        device_id: MOCK_DEVICE_ID,
    135      },
    136      null,
    137      "foo@bar.com"
    138    );
    139    await checkFxANotLoaded();
    140 
    141    await gContentAPI.showFirefoxAccounts(
    142      {
    143        flow_id: MOCK_FLOW_ID,
    144        flow_begin_time: BAD_MOCK_FLOW_BEGIN_TIME,
    145        device_id: MOCK_DEVICE_ID,
    146      },
    147      null,
    148      "foo@bar.com"
    149    );
    150    await checkFxANotLoaded();
    151  }
    152 );
    153 
    154 add_UITour_task(
    155  async function test_firefoxAccountsWithEmailAndMissingFlowParamValues() {
    156    info("Load https://accounts.firefox.com with missing flow params");
    157 
    158    await gContentAPI.showFirefoxAccounts(
    159      {
    160        flow_id: MOCK_FLOW_ID,
    161        flow_begin_time: MOCK_FLOW_BEGIN_TIME,
    162      },
    163      null,
    164      "foo@bar.com"
    165    );
    166    await BrowserTestUtils.browserLoaded(
    167      gTestTab.linkedBrowser,
    168      false,
    169      url =>
    170        url.startsWith(
    171          `https://example.com/?${gFxaParams}&entrypoint=uitour&email=foo%40bar.com&service=sync`
    172        ) &&
    173        url.includes(
    174          `flow_id=${MOCK_FLOW_ID}&flow_begin_time=${MOCK_FLOW_BEGIN_TIME}`
    175        )
    176    );
    177  }
    178 );
    179 
    180 add_UITour_task(async function test_firefoxAccountsWithEmailAndEntrypoints() {
    181  info("Load https://accounts.firefox.com with entrypoint parameters");
    182 
    183  await gContentAPI.showFirefoxAccounts(
    184    {
    185      entrypoint_experiment: "exp",
    186      entrypoint_variation: "var",
    187    },
    188    "entry",
    189    "foo@bar.com"
    190  );
    191  await BrowserTestUtils.browserLoaded(
    192    gTestTab.linkedBrowser,
    193    false,
    194    url =>
    195      url.startsWith(
    196        `https://example.com/?${gFxaParams}&entrypoint=entry&email=foo%40bar.com&service=sync`
    197      ) && url.includes(`entrypoint_experiment=exp&entrypoint_variation=var`)
    198  );
    199 });
    200 
    201 add_UITour_task(async function test_firefoxAccountsNonAlphaValue() {
    202  // All characters in the value are allowed, but they must be automatically escaped.
    203  // (we throw a unicode character in there too - it's not auto-utf8 encoded,
    204  // but that's ok, so long as it is escaped correctly.)
    205  let value = "foo& /=?:\\\xa9";
    206  // encodeURIComponent encodes spaces to %20 but we want "+"
    207  let expected = encodeURIComponent(value).replace(/%20/g, "+");
    208  info("Load https://accounts.firefox.com");
    209  await gContentAPI.showFirefoxAccounts({ utm_foo: value });
    210  await BrowserTestUtils.browserLoaded(
    211    gTestTab.linkedBrowser,
    212    false,
    213    url =>
    214      url.startsWith(
    215        `https://example.com/?${gFxaParams}&entrypoint=uitour&action=email&service=sync`
    216      ) && url.includes(`&utm_foo=` + expected)
    217  );
    218 });
    219 
    220 // A helper to check the request was ignored due to invalid params.
    221 async function checkFxANotLoaded() {
    222  try {
    223    await waitForConditionPromise(() => {
    224      return gBrowser.selectedBrowser.currentURI.spec.startsWith(
    225        "https://example.com"
    226      );
    227    }, "Check if FxA opened");
    228    ok(false, "No FxA tab should have opened");
    229  } catch (ex) {
    230    ok(true, "No FxA tab opened");
    231  }
    232 }
    233 
    234 add_UITour_task(async function test_firefoxAccountsNonObject() {
    235  // non-string should be rejected.
    236  await gContentAPI.showFirefoxAccounts(99);
    237  await checkFxANotLoaded();
    238 });
    239 
    240 add_UITour_task(async function test_firefoxAccountsNonUtmPrefix() {
    241  // Any non "utm_" name should should be rejected.
    242  await gContentAPI.showFirefoxAccounts({ utm_foo: "foo", bar: "bar" });
    243  await checkFxANotLoaded();
    244 });
    245 
    246 add_UITour_task(async function test_firefoxAccountsNonAlphaName() {
    247  // Any "utm_" name which includes non-alpha chars should be rejected.
    248  await gContentAPI.showFirefoxAccounts({ utm_foo: "foo", "utm_bar=": "bar" });
    249  await checkFxANotLoaded();
    250 });