From 19ea1e6b0da90932254532e9ae63f2b98dd5c7f2 Mon Sep 17 00:00:00 2001 From: Andrei Oprea Date: Thu, 28 May 2020 23:34:14 +0000 Subject: [PATCH] Bug 1639580 - Add schema validation for trigger actions r=k88hudson Differential Revision: https://phabricator.services.mozilla.com/D76475 --- .../newtab/test/browser/browser.ini | 2 + .../browser/browser_asrouter_trigger_docs.js | 69 ++++++++++ toolkit/components/messaging-system/moz.build | 1 + .../schemas/TriggerActionSchemas.js | 128 ++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 browser/components/newtab/test/browser/browser_asrouter_trigger_docs.js create mode 100644 toolkit/components/messaging-system/schemas/TriggerActionSchemas.js diff --git a/browser/components/newtab/test/browser/browser.ini b/browser/components/newtab/test/browser/browser.ini index 8ae65d2487b3..ad1dd0945099 100644 --- a/browser/components/newtab/test/browser/browser.ini +++ b/browser/components/newtab/test/browser/browser.ini @@ -6,6 +6,7 @@ support-files = snippet.json topstories.json ds_layout.json + ../../content-src/asrouter/docs/trigger-listeners.md prefs = browser.newtabpage.activity-stream.debug=false browser.newtabpage.activity-stream.discoverystream.enabled=true @@ -42,3 +43,4 @@ tags = remote-settings [browser_asrouter_momentspagehub.js] tags = remote-settings [browser_asrouter_experimentsAPILoader.js] +[browser_asrouter_trigger_docs.js] diff --git a/browser/components/newtab/test/browser/browser_asrouter_trigger_docs.js b/browser/components/newtab/test/browser/browser_asrouter_trigger_docs.js new file mode 100644 index 000000000000..8bbcf634bef8 --- /dev/null +++ b/browser/components/newtab/test/browser/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); + } +}); diff --git a/toolkit/components/messaging-system/moz.build b/toolkit/components/messaging-system/moz.build index 34d67f5b22d0..75b477454916 100644 --- a/toolkit/components/messaging-system/moz.build +++ b/toolkit/components/messaging-system/moz.build @@ -13,6 +13,7 @@ XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini'] TESTING_JS_MODULES += [ 'schemas/SpecialMessageActionSchemas.js', + 'schemas/TriggerActionSchemas.js', 'test/MSTestUtils.jsm', ] diff --git a/toolkit/components/messaging-system/schemas/TriggerActionSchemas.js b/toolkit/components/messaging-system/schemas/TriggerActionSchemas.js new file mode 100644 index 000000000000..5f1dc6659dfb --- /dev/null +++ b/toolkit/components/messaging-system/schemas/TriggerActionSchemas.js @@ -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"], + }, +};