This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
browser.study.api.js
358 lines (317 loc) · 11.3 KB
/
browser.study.api.js
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* eslint-env node, mocha */
/* global browser */
const KEEPOPEN = process.env.KEEPOPEN;
const assert = require("assert");
const utils = require("./utils");
// const MINUTES_PER_DAY = 60 * 24;
// node's util, for printing a deeply nested object to node console
const { inspect } = require("util");
// eslint-disable-next-line no-unused-vars
function full(myObject) {
return inspect(myObject, { showHidden: false, depth: null });
}
// eslint-disable-next-line no-unused-vars
const delay = ms => new Promise(res => setTimeout(res, ms));
function publicApiTests(telemetryPipeline) {
// This gives Firefox time to start, and us a bit longer during some of the tests.
this.timeout(30000 + KEEPOPEN * 1000 * 2);
let driver;
let addonId;
// run in the extension page
let addonExec;
async function createAddonExec() {
driver = await utils.setupWebdriver.promiseSetupDriver(
utils.FIREFOX_PREFERENCES,
);
// Ensure that shield optout studies are enabled during testing
await utils.preferences.set(
driver,
`app.shield.optoutstudies.enabled`,
true,
);
// TODO: Possibly configure Normandy here - replaces study utils setup phase
await installAddon();
await utils.ui.openBrowserConsole(driver);
// make a shorter alias
addonExec = utils.executeJs.executeAsyncScriptInExtensionPageForTests.bind(
utils.executeJs,
driver,
);
}
async function installAddon() {
// beginTime = Date.now();
if (addonId) {
await utils.setupWebdriver.uninstallAddon(driver, addonId);
addonId = null;
}
if (telemetryPipeline === "pioneer") {
await utils.setupWebdriver.installPioneerOptInAddon(driver);
}
addonId = await utils.setupWebdriver.installAddon(driver);
}
before(createAddonExec);
// hint: skipping driver.quit() may be useful when debugging failed tests,
// leaving the browser open allowing inspection of the ui and browser logs
after(async () => {
if (KEEPOPEN) {
await driver.sleep(KEEPOPEN * 1000); // wait for KEEPOPEN seconds
}
driver.quit();
});
describe("testing infrastructure works", function() {
it("should be able to access window.browser from the extension page for tests", async () => {
const hasAccessToWebExtensionApi = await addonExec(async callback => {
callback(typeof browser === "object");
});
assert(hasAccessToWebExtensionApi);
});
it("should be able to access study WebExtensions API from the extension page for tests", async () => {
const hasAccessToShieldUtilsWebExtensionApi = await addonExec(
async callback => {
callback(browser && typeof browser.study === "object");
},
);
assert(hasAccessToShieldUtilsWebExtensionApi);
});
it("should be able to access studyDebug WebExtensions API from the extension page for tests", async () => {
const hasAccessToShieldUtilsWebExtensionApi = await addonExec(
async callback => {
callback(browser && typeof browser.studyDebug === "object");
},
);
assert(hasAccessToShieldUtilsWebExtensionApi);
});
it("should be able to catch exceptions thrown in the WebExtension", async () => {
const caughtError = await addonExec(async callback => {
let _caughtError = null;
try {
throw new Error("Local exception");
} catch (e) {
// console.debug("Caught error", e);
_caughtError = e.toString();
}
callback(_caughtError);
});
assert(caughtError === "Error: Local exception");
});
/*
TODO: Figure out why if/how/when we can catch this type of exception (currently it stops test execution completely)
it("should be able to catch exceptions thrown in the WebExtensions API", async() => {
const caughtError = await utils.executeJs.executeAsyncScriptInExtensionPageForTests(
driver,
async callback => {
let _caughtError = null;
try {
browser.studyDebug.throwAnException("An exception thrown for test purposes");
callback(false);
} catch (e) {
// console.debug("Caught error", e);
_caughtError = e.toString();
callback(_caughtError);
}
},
);
assert(caughtError === "Error: An exception thrown for test purposes");
});
*/
it("should be able to catch exceptions thrown in an async WebExtensions API method", async () => {
const caughtError = await utils.executeJs.executeAsyncScriptInExtensionPageForTests(
driver,
async callback => {
let _caughtError = null;
try {
await browser.studyDebug.throwAnExceptionAsync(
"An async exception thrown for test purposes",
);
callback(false);
} catch (e) {
// console.debug("Caught error", e);
_caughtError = e.toString();
callback(_caughtError);
}
},
);
assert.equal(
caughtError,
"Error: An async exception thrown for test purposes",
);
});
});
describe("getDataPermissions", function() {
it("returns correct and current list of permissions", async () => {
const dataPermissions = await addonExec(async cb => {
// this is what runs in the webExtension scope.
const $dataPermissions = await browser.study.getDataPermissions();
// call back with all the data we care about to Mocha / node
cb($dataPermissions);
});
console.debug({ dataPermissions });
// tests
assert(dataPermissions.shield, "shield optoutstudies should be enabled");
if (telemetryPipeline === "pioneer") {
assert(
dataPermissions.pioneer,
"user should have opted in for pioneer",
);
} else {
assert(
!dataPermissions.pioneer,
"user should not have opted in for pioneer",
);
}
});
});
describe("telemetry tests", function() {
describe("sendTelemetry, calculateTelemetryPingSize", function() {
let calculatedPingSize;
before(async function reinstallSetupDoTelemetryAndWait() {
await installAddon();
const _ = await addonExec(async (_telemetryPipeline, callback) => {
await browser.studyDebug.resetSeenTelemetry();
await browser.studyDebug.recordSeenTelemetry();
const samplePing = { foo: "bar" };
await browser.study.sendTelemetry(samplePing, _telemetryPipeline);
const _calculatedPingSize = await browser.study.calculateTelemetryPingSize(
samplePing,
_telemetryPipeline,
);
callback({
calculatedPingSize: _calculatedPingSize,
});
}, telemetryPipeline);
calculatedPingSize = _.calculatedPingSize;
await delay(1000); // wait a second to telemetry to settle on disk.
});
it("calculated ping size is as expected", async () => {
const expectedPingSizes = {
shield: 20,
pioneer: 682,
};
assert.strictEqual(
calculatedPingSize,
expectedPingSizes[telemetryPipeline],
);
});
describe("telemetry archive / controller effects", function() {
let studyPings;
before(async () => {
studyPings = await addonExec(async callback => {
const _studyPings = await browser.study.searchSentTelemetry({
type: [
"shield-study",
"shield-study-addon",
"shield-study-error",
"pioneer-study",
],
});
const seenTelemetry = await browser.studyDebug.getSeenTelemetry();
callback({
sent: _studyPings,
seen: seenTelemetry.reverse(),
}); // Using reverse() to mimic the default sorting of telemetry archive results
});
// For debugging tests
console.debug(
"Pings report: ",
utils.telemetry.pingsReport(studyPings.seen),
);
console.debug(
"Pings with id and payload: ",
utils.telemetry.pingsDebug(studyPings.seen),
);
});
it("should have seen at least one telemetry ping", async () => {
assert(studyPings.seen.length > 0, "at least one telemetry ping");
});
it("should have sent at least one telemetry ping", async () => {
assert(studyPings.sent.length > 0, "at least one telemetry ping");
});
it("should have sent expected telemetry", async () => {
const observed = utils.telemetry.summarizePings(
telemetryPipeline === "shield" ? studyPings.sent : studyPings.seen,
);
const expected = [
[
"shield-study-addon",
{
attributes: {
foo: "bar",
},
},
],
];
assert.deepStrictEqual(
observed,
expected,
"telemetry pings as expected",
);
});
});
});
});
describe("api: validateJSON", function() {
it("api: validateJSON should work as expected", async function() {
const answers = await addonExec(async callback => {
const validations = [];
validations.push(
await browser.study.validateJSON(
{ a: 1 },
{ type: "object", properties: { a: { type: "number" } } },
),
);
validations.push(
await browser.study.validateJSON(
{ a: 1 },
{ type: "object", properties: { a: { type: "string" } } },
),
);
callback(validations);
});
const expected = [
{ errors: [], valid: true },
{
errors: [
{
dataPath: ".a",
keyword: "type",
message: "should be string",
params: { type: "string" },
schemaPath: "#/properties/a/type",
},
],
valid: false,
},
];
assert.deepStrictEqual(answers, expected);
});
});
describe("api: fullSurveyUrl", function() {
describe("correctly constructs urls queryArgs", function() {
it("an example url is correct", async function() {
const actual = await addonExec(async (_telemetryPipeline, callback) => {
const result = await browser.study.fullSurveyUrl(
"https://foo.com/survey-foo/",
"mid-study-survey",
_telemetryPipeline,
);
callback(result);
}, telemetryPipeline);
console.debug({ actual });
const matchesExpectedExceptForVariableArguments =
actual.indexOf(
"https://foo.com/survey-foo/?shield=3&study=shield-utils-test-addon%40shield.mozilla.org",
) > -1 &&
actual.indexOf(
"&testing=-1&reason=mid-study-survey&fullreason=mid-study-survey",
) > -1;
assert(matchesExpectedExceptForVariableArguments);
});
});
});
}
describe("PUBLIC API `browser.study` (telemetryPipeline: shield)", function() {
publicApiTests.call(this, "shield");
});
describe("PUBLIC API `browser.study` (telemetryPipeline: pioneer)", function() {
publicApiTests.call(this, "pioneer");
});