summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/browser/browser_aboutwelcome_fxa_signin_flow.js
blob: 9de9acb7b32c56653c61f868d289dce339deedf2 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { UIState } = ChromeUtils.importESModule(
  "resource://services-sync/UIState.sys.mjs"
);

const TEST_ROOT = "https://example.com/";

add_setup(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [["identity.fxaccounts.remote.root", TEST_ROOT]],
  });
});

/**
 * Tests that the FXA_SIGNIN_FLOW special action resolves to `true` and
 * closes the FxA sign-in tab if sign-in is successful.
 */
add_task(async function test_fxa_sign_success() {
  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
      });
    });
    let fxaTab = await fxaTabPromise;
    let fxaTabClosing = BrowserTestUtils.waitForTabClosing(fxaTab);

    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
    // and not test the actual FxA sign-in mechanism.
    sandbox.stub(UIState, "get").returns({
      status: UIState.STATUS_SIGNED_IN,
      syncEnabled: true,
      email: "email@example.com",
    });

    Services.obs.notifyObservers(null, UIState.ON_UPDATE);

    await fxaTabClosing;
    Assert.ok(true, "FxA tab automatically closed.");
    let result = await resultPromise;
    Assert.ok(result, "FXA_SIGNIN_FLOW action's result should be true");
  });

  sandbox.restore();
});

/**
 * Tests that the FXA_SIGNIN_FLOW action's data.autoClose parameter can
 * disable the autoclose behavior.
 */
add_task(async function test_fxa_sign_success_no_autoclose() {
  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
        data: { autoClose: false },
      });
    });
    let fxaTab = await fxaTabPromise;

    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
    // and not test the actual FxA sign-in mechanism.
    sandbox.stub(UIState, "get").returns({
      status: UIState.STATUS_SIGNED_IN,
      syncEnabled: true,
      email: "email@example.com",
    });

    Services.obs.notifyObservers(null, UIState.ON_UPDATE);

    let result = await resultPromise;
    Assert.ok(result, "FXA_SIGNIN_FLOW should have resolved to true");
    Assert.ok(!fxaTab.closing, "FxA tab was not asked to close.");
    BrowserTestUtils.removeTab(fxaTab);
  });

  sandbox.restore();
});

/**
 * Tests that the FXA_SIGNIN_FLOW action resolves to `false` if the tab
 * closes before sign-in completes.
 */
add_task(async function test_fxa_signin_aborted() {
  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
      });
    });
    let fxaTab = await fxaTabPromise;
    Assert.ok(!fxaTab.closing, "FxA tab was not asked to close yet.");

    BrowserTestUtils.removeTab(fxaTab);
    let result = await resultPromise;
    Assert.ok(!result, "FXA_SIGNIN_FLOW action's result should be false");
  });
});

/**
 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
 * be, and that if that window closes, the flow is considered aborted.
 */
add_task(async function test_fxa_signin_window_aborted() {
  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
        data: {
          where: "window",
        },
      });
    });
    let fxaWindow = await fxaWindowPromise;
    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");

    await BrowserTestUtils.closeWindow(fxaWindow);
    let result = await resultPromise;
    Assert.ok(!result, "FXA_SIGNIN_FLOW action's result should be false");
  });
});

/**
 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
 * be, and that if sign-in completes, that new window will close automatically.
 */
add_task(async function test_fxa_signin_window_success() {
  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
        data: {
          where: "window",
        },
      });
    });
    let fxaWindow = await fxaWindowPromise;
    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");

    let windowClosed = BrowserTestUtils.windowClosed(fxaWindow);

    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
    // and not test the actual FxA sign-in mechanism.
    sandbox.stub(UIState, "get").returns({
      status: UIState.STATUS_SIGNED_IN,
      syncEnabled: true,
      email: "email@example.com",
    });

    Services.obs.notifyObservers(null, UIState.ON_UPDATE);

    let result = await resultPromise;
    Assert.ok(result, "FXA_SIGNIN_FLOW action's result should be true");

    await windowClosed;
    Assert.ok(fxaWindow.closed, "Sign-in window was automatically closed.");
  });

  sandbox.restore();
});

/**
 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
 * be, and that if a new tab is opened in that window and the sign-in tab
 * is closed:
 *
 * 1. The new window isn't closed
 * 2. The sign-in is considered aborted.
 */
add_task(async function test_fxa_signin_window_multiple_tabs_aborted() {
  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
        data: {
          where: "window",
        },
      });
    });
    let fxaWindow = await fxaWindowPromise;
    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");
    let fxaTab = fxaWindow.gBrowser.selectedTab;
    await BrowserTestUtils.openNewForegroundTab(
      fxaWindow.gBrowser,
      "about:blank"
    );
    BrowserTestUtils.removeTab(fxaTab);

    let result = await resultPromise;
    Assert.ok(!result, "FXA_SIGNIN_FLOW action's result should be false");
    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close.");
    await BrowserTestUtils.closeWindow(fxaWindow);
  });
});

/**
 * Tests that the FXA_SIGNIN_FLOW action can open a separate window, if need
 * be, and that if a new tab is opened in that window but then sign-in
 * completes
 *
 * 1. The new window isn't closed, but the sign-in tab is.
 * 2. The sign-in is considered a success.
 */
add_task(async function test_fxa_signin_window_multiple_tabs_success() {
  let sandbox = sinon.createSandbox();
  registerCleanupFunction(() => {
    sandbox.restore();
  });

  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaWindowPromise = BrowserTestUtils.waitForNewWindow();
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
        data: {
          where: "window",
        },
      });
    });
    let fxaWindow = await fxaWindowPromise;
    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close yet.");
    let fxaTab = fxaWindow.gBrowser.selectedTab;

    // This will open an about:blank tab in the background.
    await BrowserTestUtils.addTab(fxaWindow.gBrowser);
    let fxaTabClosed = BrowserTestUtils.waitForTabClosing(fxaTab);

    // We'll fake-out the UIState being in the STATUS_SIGNED_IN status
    // and not test the actual FxA sign-in mechanism.
    sandbox.stub(UIState, "get").returns({
      status: UIState.STATUS_SIGNED_IN,
      syncEnabled: true,
      email: "email@example.com",
    });

    Services.obs.notifyObservers(null, UIState.ON_UPDATE);

    let result = await resultPromise;
    Assert.ok(result, "FXA_SIGNIN_FLOW action's result should be true");
    await fxaTabClosed;

    Assert.ok(!fxaWindow.closed, "FxA window was not asked to close.");
    await BrowserTestUtils.closeWindow(fxaWindow);
  });

  sandbox.restore();
});

/**
 * Tests that we can pass an entrypoint and UTM parameters to the FxA sign-in
 * page.
 */
add_task(async function test_fxa_signin_flow_entrypoint_utm_params() {
  await BrowserTestUtils.withNewTab("about:welcome", async browser => {
    let fxaTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
    let resultPromise = SpecialPowers.spawn(browser, [], async () => {
      return content.wrappedJSObject.AWSendToParent("SPECIAL_ACTION", {
        type: "FXA_SIGNIN_FLOW",
        data: {
          entrypoint: "test-entrypoint",
          extraParams: {
            utm_test1: "utm_test1",
            utm_test2: "utm_test2",
          },
        },
      });
    });
    let fxaTab = await fxaTabPromise;

    let uriParams = new URLSearchParams(fxaTab.linkedBrowser.currentURI.query);
    Assert.equal(uriParams.get("entrypoint"), "test-entrypoint");
    Assert.equal(uriParams.get("utm_test1"), "utm_test1");
    Assert.equal(uriParams.get("utm_test2"), "utm_test2");

    BrowserTestUtils.removeTab(fxaTab);
    await resultPromise;
  });
});