summaryrefslogtreecommitdiffstats
path: root/browser/components/uitour/test/browser_UITour_sync.js
blob: e7393966ce59a99fdbfe9a8c6d4bbf4b99c19dc1 (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
"use strict";

var gTestTab;
var gContentAPI;

const MOCK_FLOW_ID =
  "5445b28b8b7ba6cf71e345f8fff4bc59b2a514f78f3e2cc99b696449427fd445";
const MOCK_FLOW_BEGIN_TIME = 1590780440325;
const MOCK_DEVICE_ID = "7e450f3337d3479b8582ea1c9bb5ba6c";

registerCleanupFunction(function () {
  Services.prefs.clearUserPref("identity.fxaccounts.remote.root");
  Services.prefs.clearUserPref("services.sync.username");
});

add_task(setup_UITourTest);

add_setup(async function () {
  Services.prefs.setCharPref(
    "identity.fxaccounts.remote.root",
    "https://example.com"
  );
});

add_UITour_task(async function test_checkSyncSetup_disabled() {
  let result = await getConfigurationPromise("sync");
  is(result.setup, false, "Sync shouldn't be setup by default");
});

add_UITour_task(async function test_checkSyncSetup_enabled() {
  Services.prefs.setCharPref(
    "services.sync.username",
    "uitour@tests.mozilla.org"
  );
  let result = await getConfigurationPromise("sync");
  is(result.setup, true, "Sync should be setup");
});

add_UITour_task(async function test_checkSyncCounts() {
  Services.prefs.setIntPref("services.sync.clients.devices.desktop", 4);
  Services.prefs.setIntPref("services.sync.clients.devices.mobile", 5);
  Services.prefs.setIntPref("services.sync.numClients", 9);
  let result = await getConfigurationPromise("sync");
  is(result.mobileDevices, 5, "mobileDevices should be set");
  is(result.desktopDevices, 4, "desktopDevices should be set");
  is(result.totalDevices, 9, "totalDevices should be set");

  Services.prefs.clearUserPref("services.sync.clients.devices.desktop");
  result = await getConfigurationPromise("sync");
  is(result.mobileDevices, 5, "mobileDevices should be set");
  is(result.desktopDevices, 0, "desktopDevices should be 0");
  is(result.totalDevices, 9, "totalDevices should be set");

  Services.prefs.clearUserPref("services.sync.clients.devices.mobile");
  result = await getConfigurationPromise("sync");
  is(result.mobileDevices, 0, "mobileDevices should be 0");
  is(result.desktopDevices, 0, "desktopDevices should be 0");
  is(result.totalDevices, 9, "totalDevices should be set");

  Services.prefs.clearUserPref("services.sync.numClients");
  result = await getConfigurationPromise("sync");
  is(result.mobileDevices, 0, "mobileDevices should be 0");
  is(result.desktopDevices, 0, "desktopDevices should be 0");
  is(result.totalDevices, 0, "totalDevices should be 0");
});

// The showFirefoxAccounts API is sync related, so we test that here too...
add_UITour_task(async function test_firefoxAccountsNoParams() {
  info("Load https://accounts.firefox.com");
  await gContentAPI.showFirefoxAccounts();
  await BrowserTestUtils.browserLoaded(
    gTestTab.linkedBrowser,
    false,
    "https://example.com/?context=fx_desktop_v3&entrypoint=uitour&action=email&service=sync"
  );
});

add_UITour_task(async function test_firefoxAccountsValidParams() {
  info("Load https://accounts.firefox.com");
  await gContentAPI.showFirefoxAccounts({ utm_foo: "foo", utm_bar: "bar" });
  await BrowserTestUtils.browserLoaded(
    gTestTab.linkedBrowser,
    false,
    "https://example.com/?context=fx_desktop_v3&entrypoint=uitour&action=email&service=sync&utm_foo=foo&utm_bar=bar"
  );
});

add_UITour_task(async function test_firefoxAccountsWithEmail() {
  info("Load https://accounts.firefox.com");
  await gContentAPI.showFirefoxAccounts(null, null, "foo@bar.com");
  await BrowserTestUtils.browserLoaded(
    gTestTab.linkedBrowser,
    false,
    "https://example.com/?context=fx_desktop_v3&entrypoint=uitour&email=foo%40bar.com&service=sync"
  );
});

add_UITour_task(async function test_firefoxAccountsWithEmailAndFlowParams() {
  info("Load https://accounts.firefox.com with flow params");
  const flowParams = {
    flow_id: MOCK_FLOW_ID,
    flow_begin_time: MOCK_FLOW_BEGIN_TIME,
    device_id: MOCK_DEVICE_ID,
  };
  await gContentAPI.showFirefoxAccounts(flowParams, null, "foo@bar.com");
  await BrowserTestUtils.browserLoaded(
    gTestTab.linkedBrowser,
    false,
    "https://example.com/?context=fx_desktop_v3&entrypoint=uitour&email=foo%40bar.com&service=sync&" +
      `flow_id=${MOCK_FLOW_ID}&flow_begin_time=${MOCK_FLOW_BEGIN_TIME}&device_id=${MOCK_DEVICE_ID}`
  );
});

add_UITour_task(
  async function test_firefoxAccountsWithEmailAndBadFlowParamValues() {
    info("Load https://accounts.firefox.com with bad flow params");
    const BAD_MOCK_FLOW_ID = "1";
    const BAD_MOCK_FLOW_BEGIN_TIME = 100;

    await gContentAPI.showFirefoxAccounts(
      {
        flow_id: BAD_MOCK_FLOW_ID,
        flow_begin_time: MOCK_FLOW_BEGIN_TIME,
        device_id: MOCK_DEVICE_ID,
      },
      null,
      "foo@bar.com"
    );
    await checkFxANotLoaded();

    await gContentAPI.showFirefoxAccounts(
      {
        flow_id: MOCK_FLOW_ID,
        flow_begin_time: BAD_MOCK_FLOW_BEGIN_TIME,
        device_id: MOCK_DEVICE_ID,
      },
      null,
      "foo@bar.com"
    );
    await checkFxANotLoaded();
  }
);

add_UITour_task(
  async function test_firefoxAccountsWithEmailAndMissingFlowParamValues() {
    info("Load https://accounts.firefox.com with missing flow params");

    await gContentAPI.showFirefoxAccounts(
      {
        flow_id: MOCK_FLOW_ID,
        flow_begin_time: MOCK_FLOW_BEGIN_TIME,
      },
      null,
      "foo@bar.com"
    );
    await BrowserTestUtils.browserLoaded(
      gTestTab.linkedBrowser,
      false,
      "https://example.com/?context=fx_desktop_v3&entrypoint=uitour&email=foo%40bar.com&service=sync&" +
        `flow_id=${MOCK_FLOW_ID}&flow_begin_time=${MOCK_FLOW_BEGIN_TIME}`
    );
  }
);

add_UITour_task(async function test_firefoxAccountsWithEmailAndEntrypoints() {
  info("Load https://accounts.firefox.com with entrypoint parameters");

  await gContentAPI.showFirefoxAccounts(
    {
      entrypoint_experiment: "exp",
      entrypoint_variation: "var",
    },
    "entry",
    "foo@bar.com"
  );
  await BrowserTestUtils.browserLoaded(
    gTestTab.linkedBrowser,
    false,
    "https://example.com/?context=fx_desktop_v3&entrypoint=entry&email=foo%40bar.com&service=sync&" +
      `entrypoint_experiment=exp&entrypoint_variation=var`
  );
});

add_UITour_task(async function test_firefoxAccountsNonAlphaValue() {
  // All characters in the value are allowed, but they must be automatically escaped.
  // (we throw a unicode character in there too - it's not auto-utf8 encoded,
  // but that's ok, so long as it is escaped correctly.)
  let value = "foo& /=?:\\\xa9";
  // encodeURIComponent encodes spaces to %20 but we want "+"
  let expected = encodeURIComponent(value).replace(/%20/g, "+");
  info("Load https://accounts.firefox.com");
  await gContentAPI.showFirefoxAccounts({ utm_foo: value });
  await BrowserTestUtils.browserLoaded(
    gTestTab.linkedBrowser,
    false,
    "https://example.com/?context=fx_desktop_v3&entrypoint=uitour&action=email&service=sync&utm_foo=" +
      expected
  );
});

// A helper to check the request was ignored due to invalid params.
async function checkFxANotLoaded() {
  try {
    await waitForConditionPromise(() => {
      return gBrowser.selectedBrowser.currentURI.spec.startsWith(
        "https://example.com"
      );
    }, "Check if FxA opened");
    ok(false, "No FxA tab should have opened");
  } catch (ex) {
    ok(true, "No FxA tab opened");
  }
}

add_UITour_task(async function test_firefoxAccountsNonObject() {
  // non-string should be rejected.
  await gContentAPI.showFirefoxAccounts(99);
  await checkFxANotLoaded();
});

add_UITour_task(async function test_firefoxAccountsNonUtmPrefix() {
  // Any non "utm_" name should should be rejected.
  await gContentAPI.showFirefoxAccounts({ utm_foo: "foo", bar: "bar" });
  await checkFxANotLoaded();
});

add_UITour_task(async function test_firefoxAccountsNonAlphaName() {
  // Any "utm_" name which includes non-alpha chars should be rejected.
  await gContentAPI.showFirefoxAccounts({ utm_foo: "foo", "utm_bar=": "bar" });
  await checkFxANotLoaded();
});