tor-browser

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

browser_aboutwelcome_fxa_signin_flow.js (10240B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { UIState } = ChromeUtils.importESModule(
      7  "resource://services-sync/UIState.sys.mjs"
      8 );
      9 
     10 const TEST_ROOT = "https://example.com/";
     11 
     12 add_setup(async () => {
     13  await SpecialPowers.pushPrefEnv({
     14    set: [["identity.fxaccounts.remote.root", TEST_ROOT]],
     15  });
     16 });
     17 
     18 /**
     19 * Tests that the FXA_SIGNIN_FLOW special action resolves to `true` and
     20 * closes the FxA sign-in tab if sign-in is successful.
     21 */
     22 add_task(async function test_fxa_sign_success() {
     23  let sandbox = sinon.createSandbox();
     24  registerCleanupFunction(() => {
     25    sandbox.restore();
     26  });
     27 
     28  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
     29    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
     30    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
     31      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
     32        type: "FXA_SIGNIN_FLOW",
     33      });
     34    });
     35    let fxaTab = await fxaTabPromise;
     36    let fxaTabClosing = BrowserTestUtils.waitForTabClosing(fxaTab);
     37 
     38    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
     39    // and not test the actual FxA sign-in mechanism.
     40    sandbox.stub(UIState, "get").returns({
     41      status: UIState.STATUS_SIGNED_IN,
     42      syncEnabled: true,
     43      email: "email@example.com",
     44    });
     45 
     46    Services.obs.notifyObservers(null, UIState.ON_UPDATE);
     47 
     48    await fxaTabClosing;
     49    Assert.ok(true, "FxA tab automatically closed.");
     50    let result = await resultPromise;
     51    Assert.ok(result, "FXA_SIGNIN_FLOW action's result should be true");
     52  });
     53 
     54  sandbox.restore();
     55 });
     56 
     57 /**
     58 * Tests that the FXA_SIGNIN_FLOW action's data.autoClose parameter can
     59 * disable the autoclose behavior.
     60 */
     61 add_task(async function test_fxa_sign_success_no_autoclose() {
     62  let sandbox = sinon.createSandbox();
     63  registerCleanupFunction(() => {
     64    sandbox.restore();
     65  });
     66 
     67  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
     68    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
     69    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
     70      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
     71        type: "FXA_SIGNIN_FLOW",
     72        data: { autoClose: false },
     73      });
     74    });
     75    let fxaTab = await fxaTabPromise;
     76 
     77    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
     78    // and not test the actual FxA sign-in mechanism.
     79    sandbox.stub(UIState, "get").returns({
     80      status: UIState.STATUS_SIGNED_IN,
     81      syncEnabled: true,
     82      email: "email@example.com",
     83    });
     84 
     85    Services.obs.notifyObservers(null, UIState.ON_UPDATE);
     86 
     87    let result = await resultPromise;
     88    Assert.ok(result, "FXA_SIGNIN_FLOW should have resolved to true");
     89    Assert.ok(!fxaTab.closing, "FxA tab was not asked to close.");
     90    BrowserTestUtils.removeTab(fxaTab);
     91  });
     92 
     93  sandbox.restore();
     94 });
     95 
     96 /**
     97 * Tests that the FXA_SIGNIN_FLOW action resolves to `false` if the tab
     98 * closes before sign-in completes.
     99 */
    100 add_task(async function test_fxa_signin_aborted() {
    101  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    102    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
    103    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
    104      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
    105        type: "FXA_SIGNIN_FLOW",
    106      });
    107    });
    108    let fxaTab = await fxaTabPromise;
    109    Assert.ok(!fxaTab.closing, "FxA tab was not asked to close yet.");
    110 
    111    BrowserTestUtils.removeTab(fxaTab);
    112    let result = await resultPromise;
    113    Assert.ok(!result, "FXA_SIGNIN_FLOW action's result should be false");
    114  });
    115 });
    116 
    117 /**
    118 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
    119 * be, and that if that window closes, the flow is considered aborted.
    120 */
    121 add_task(async function test_fxa_signin_window_aborted() {
    122  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    123    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    124    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
    125      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
    126        type: "FXA_SIGNIN_FLOW",
    127        data: {
    128          where: "window",
    129        },
    130      });
    131    });
    132    let fxaWindow = await fxaWindowPromise;
    133    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");
    134 
    135    await BrowserTestUtils.closeWindow(fxaWindow);
    136    let result = await resultPromise;
    137    Assert.ok(!result, "FXA_SIGNIN_FLOW action's result should be false");
    138  });
    139 });
    140 
    141 /**
    142 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
    143 * be, and that if sign-in completes, that new window will close automatically.
    144 */
    145 add_task(async function test_fxa_signin_window_success() {
    146  let sandbox = sinon.createSandbox();
    147  registerCleanupFunction(() => {
    148    sandbox.restore();
    149  });
    150 
    151  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    152    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    153    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
    154      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
    155        type: "FXA_SIGNIN_FLOW",
    156        data: {
    157          where: "window",
    158        },
    159      });
    160    });
    161    let fxaWindow = await fxaWindowPromise;
    162    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");
    163 
    164    let windowClosed = BrowserTestUtils.windowClosed(fxaWindow);
    165 
    166    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
    167    // and not test the actual FxA sign-in mechanism.
    168    sandbox.stub(UIState, "get").returns({
    169      status: UIState.STATUS_SIGNED_IN,
    170      syncEnabled: true,
    171      email: "email@example.com",
    172    });
    173 
    174    Services.obs.notifyObservers(null, UIState.ON_UPDATE);
    175 
    176    let result = await resultPromise;
    177    Assert.ok(result, "FXA_SIGNIN_FLOW action's result should be true");
    178 
    179    await windowClosed;
    180    Assert.ok(fxaWindow.closed, "Sign-in window was automatically closed.");
    181  });
    182 
    183  sandbox.restore();
    184 });
    185 
    186 /**
    187 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
    188 * be, and that if a new tab is opened in that window and the sign-in tab
    189 * is closed:
    190 *
    191 * 1. The new window isn't closed
    192 * 2. The sign-in is considered aborted.
    193 */
    194 add_task(async function test_fxa_signin_window_multiple_tabs_aborted() {
    195  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    196    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    197    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
    198      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
    199        type: "FXA_SIGNIN_FLOW",
    200        data: {
    201          where: "window",
    202        },
    203      });
    204    });
    205    let fxaWindow = await fxaWindowPromise;
    206    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");
    207    let fxaTab = fxaWindow.gBrowser.selectedTab;
    208    await BrowserTestUtils.openNewForegroundTab(
    209      fxaWindow.gBrowser,
    210      "about:blank"
    211    );
    212    BrowserTestUtils.removeTab(fxaTab);
    213 
    214    let result = await resultPromise;
    215    Assert.ok(!result, "FXA_SIGNIN_FLOW action's result should be false");
    216    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close.");
    217    await BrowserTestUtils.closeWindow(fxaWindow);
    218  });
    219 });
    220 
    221 /**
    222 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
    223 * be, and that if a new tab is opened in that window but then sign-in
    224 * completes
    225 *
    226 * 1. The new window isn't closed, but the sign-in tab is.
    227 * 2. The sign-in is considered a success.
    228 */
    229 add_task(async function test_fxa_signin_window_multiple_tabs_success() {
    230  let sandbox = sinon.createSandbox();
    231  registerCleanupFunction(() => {
    232    sandbox.restore();
    233  });
    234 
    235  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    236    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    237    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
    238      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
    239        type: "FXA_SIGNIN_FLOW",
    240        data: {
    241          where: "window",
    242        },
    243      });
    244    });
    245    let fxaWindow = await fxaWindowPromise;
    246    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");
    247    let fxaTab = fxaWindow.gBrowser.selectedTab;
    248 
    249    // This will open an about:blank tab in the background.
    250    await BrowserTestUtils.addTab(fxaWindow.gBrowser);
    251    let fxaTabClosed = BrowserTestUtils.waitForTabClosing(fxaTab);
    252 
    253    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
    254    // and not test the actual FxA sign-in mechanism.
    255    sandbox.stub(UIState, "get").returns({
    256      status: UIState.STATUS_SIGNED_IN,
    257      syncEnabled: true,
    258      email: "email@example.com",
    259    });
    260 
    261    Services.obs.notifyObservers(null, UIState.ON_UPDATE);
    262 
    263    let result = await resultPromise;
    264    Assert.ok(result, "FXA_SIGNIN_FLOW action's result should be true");
    265    await fxaTabClosed;
    266 
    267    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close.");
    268    await BrowserTestUtils.closeWindow(fxaWindow);
    269  });
    270 
    271  sandbox.restore();
    272 });
    273 
    274 /**
    275 * Tests that we can pass an entrypoint and UTM parameters to the FxA sign-in
    276 * page.
    277 */
    278 add_task(async function test_fxa_signin_flow_entrypoint_utm_params() {
    279  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    280    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
    281    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
    282      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
    283        type: "FXA_SIGNIN_FLOW",
    284        data: {
    285          entrypoint: "test-entrypoint",
    286          extraParams: {
    287            utm_test1: "utm_test1",
    288            utm_test2: "utm_test2",
    289          },
    290        },
    291      });
    292    });
    293    let fxaTab = await fxaTabPromise;
    294 
    295    let uriParams = new URLSearchParams(fxaTab.linkedBrowser.currentURI.query);
    296    Assert.equal(uriParams.get("entrypoint"), "test-entrypoint");
    297    Assert.equal(uriParams.get("utm_test1"), "utm_test1");
    298    Assert.equal(uriParams.get("utm_test2"), "utm_test2");
    299 
    300    BrowserTestUtils.removeTab(fxaTab);
    301    await resultPromise;
    302  });
    303 });