Bug 1639580 - Add schema validation for trigger actions r=k88hudson

Differential Revision: https://phabricator.services.mozilla.com/D76475
This commit is contained in:
Andrei Oprea 2020-05-28 23:34:14 +00:00
Родитель 577303a05c
Коммит 19ea1e6b0d
4 изменённых файлов: 200 добавлений и 0 удалений

Просмотреть файл

@ -6,6 +6,7 @@ support-files =
snippet.json snippet.json
topstories.json topstories.json
ds_layout.json ds_layout.json
../../content-src/asrouter/docs/trigger-listeners.md
prefs = prefs =
browser.newtabpage.activity-stream.debug=false browser.newtabpage.activity-stream.debug=false
browser.newtabpage.activity-stream.discoverystream.enabled=true browser.newtabpage.activity-stream.discoverystream.enabled=true
@ -42,3 +43,4 @@ tags = remote-settings
[browser_asrouter_momentspagehub.js] [browser_asrouter_momentspagehub.js]
tags = remote-settings tags = remote-settings
[browser_asrouter_experimentsAPILoader.js] [browser_asrouter_experimentsAPILoader.js]
[browser_asrouter_trigger_docs.js]

Просмотреть файл

@ -0,0 +1,69 @@
const TEST_URL =
"https://example.com/browser/browser/components/newtab/test/browser/trigger-listeners.md";
const { TriggerActionSchemas } = ChromeUtils.import(
"resource://testing-common/TriggerActionSchemas.js"
);
const { ASRouterTriggerListeners } = ChromeUtils.import(
"resource://activity-stream/lib/ASRouterTriggerListeners.jsm"
);
const { CFRMessageProvider } = ChromeUtils.import(
"resource://activity-stream/lib/CFRMessageProvider.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"JsonSchemaValidator",
"resource://gre/modules/components-utils/JsonSchemaValidator.jsm"
);
// TODO: docs
async function validateTrigger(trigger) {
const schema = TriggerActionSchemas[trigger.id];
ok(schema, `should have a schema for ${trigger.id}`);
const { valid, error } = JsonSchemaValidator.validate(trigger, schema);
if (!valid) {
throw new Error(
`Trigger with id ${trigger.id} was not valid: ${error.message}`
);
}
ok(valid, `should be a valid action of type ${trigger.id}`);
}
function getHeadingsFromDocs(docs) {
const re = /### `(\w+)`/g;
const found = [];
let match = 1;
while (match) {
match = re.exec(docs);
if (match) {
found.push(match[1]);
}
}
return found;
}
add_task(async function test_trigger_docs() {
let request = await fetch(TEST_URL, { credentials: "omit" });
let docs = await request.text();
let headings = getHeadingsFromDocs(docs);
for (let triggerName of ASRouterTriggerListeners.keys()) {
Assert.ok(
headings.includes(triggerName),
`${triggerName} not found in trigger-listeners.md`
);
}
});
add_task(async function test_message_triggers() {
const messages = CFRMessageProvider.getMessages();
for (let message of messages) {
if (message.id === "MILESTONE_MESSAGE") {
// JsonSchemaValidator.jsm doesn't support mixed schema definitions.
// `contentBlocking` CFRs all have integer params as arguments except
// this one which we can't correctly validate with the schema.
continue;
}
validateTrigger(message.trigger);
}
});

Просмотреть файл

@ -13,6 +13,7 @@ XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini']
TESTING_JS_MODULES += [ TESTING_JS_MODULES += [
'schemas/SpecialMessageActionSchemas.js', 'schemas/SpecialMessageActionSchemas.js',
'schemas/TriggerActionSchemas.js',
'test/MSTestUtils.jsm', 'test/MSTestUtils.jsm',
] ]

Просмотреть файл

@ -0,0 +1,128 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const EXPORTED_SYMBOLS = ["TriggerActionSchemas"];
const TriggerActionSchemas = {
openURL: {
description:
"Happens every time the user loads a new URL that matches the provided `hosts` or `patterns`",
properties: {
id: {
type: "string",
enum: ["openURL"],
},
params: {
type: "array",
items: {
type: "string",
},
description: "List of urls we should match against",
},
patterns: {
type: "array",
items: {
type: "string",
},
description: "List of Match pattern compatible strings to match agaist",
},
},
type: "object",
anyOf: [{ required: ["id", "params"] }, { required: ["id", "patterns"] }],
},
openArticleURL: {
description:
"Happens every time the user loads a document that is Reader Mode compatible.",
properties: {
id: {
type: "string",
enum: ["openArticleURL"],
},
params: {
type: "array",
items: {
type: "string",
},
description: "List of urls we should match against",
},
patterns: {
type: "array",
items: {
type: "string",
},
description: "List of Match pattern compatible strings to match agaist",
},
},
type: "object",
anyOf: [{ required: ["id", "params"] }, { required: ["id", "patterns"] }],
},
openBookmarkedURL: {
description:
"Happens every time the user adds a bookmark from the URL bar star icon",
properties: {
id: {
type: "string",
enum: ["openBookmarkedURL"],
},
},
type: "object",
},
frequentVisits: {
description:
"Happens every time a user navigates (or switches tab to) to any of the `hosts` or `patterns` arguments but additionally provides information about the number of accesses to the matched domain.",
properties: {
id: {
type: "string",
enum: ["frequentVisits"],
},
params: {
type: "array",
items: {
type: "string",
},
description: "List of urls we should match against",
},
patterns: {
type: "array",
items: {
type: "string",
},
description: "List of Match pattern compatible strings to match agaist",
},
},
type: "object",
anyOf: [{ required: ["id", "params"] }, { required: ["id", "patterns"] }],
},
newSavedLogin: {
description: "Happens every time the user adds or updates a login.",
properties: {
id: {
type: "string",
enum: ["newSavedLogin"],
},
},
type: "object",
},
contentBlocking: {
description:
"Happens every time Firefox blocks the loading of a page script/asset/resource that matches the one of the tracking behaviours specifid through params",
type: "object",
properties: {
id: {
type: "string",
enum: ["contentBlocking"],
},
params: {
type: "array",
items: {
type: "number",
},
minItems: 1,
},
},
required: ["id", "params"],
},
};