diff options
Diffstat (limited to 'toolkit/components/search/tests/xpcshell/searchconfigs')
4 files changed, 143 insertions, 120 deletions
diff --git a/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_ui_schemas_valid.js b/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_ui_schemas_valid.js new file mode 100644 index 0000000000..3315bf974f --- /dev/null +++ b/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_ui_schemas_valid.js @@ -0,0 +1,36 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +let schemas = [ + ["search-config-schema.json", "search-config-ui-schema.json"], + ["search-config-v2-schema.json", "search-config-v2-ui-schema.json"], + ["search-config-icons-schema.json", "search-config-icons-ui-schema.json"], + [ + "search-config-overrides-schema.json", + "search-config-overrides-ui-schema.json", + ], + [ + "search-config-overrides-v2-schema.json", + "search-config-overrides-v2-ui-schema.json", + ], + [ + "search-default-override-allowlist-schema.json", + "search-default-override-allowlist-ui-schema.json", + ], +]; + +add_task(async function test_ui_schemas_valid() { + for (let [schema, uiSchema] of schemas) { + info(`Validating ${uiSchema} has every top-level from ${schema}`); + let schemaData = await IOUtils.readJSON( + PathUtils.join(do_get_cwd().path, schema) + ); + let uiSchemaData = await IOUtils.readJSON( + PathUtils.join(do_get_cwd().path, uiSchema) + ); + + await checkUISchemaValid(schemaData, uiSchemaData); + } +}); diff --git a/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js b/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js index 51e71ff573..86686b62f7 100644 --- a/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js +++ b/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchconfig_validates.js @@ -65,103 +65,79 @@ function disallowAdditionalProperties(section) { } } -let searchConfigSchemaV1; -let searchConfigSchema; - -add_setup(async function () { - searchConfigSchemaV1 = await IOUtils.readJSON( - PathUtils.join(do_get_cwd().path, "search-config-schema.json") - ); - searchConfigSchema = await IOUtils.readJSON( - PathUtils.join(do_get_cwd().path, "search-config-v2-schema.json") +/** + * Asserts the remote setting collection validates against the schema. + * + * @param {object} options + * The options for the assertion. + * @param {string} options.collectionName + * The name of the collection under validation. + * @param {object[]} options.collectionData + * The collection data to validate. + * @param {string[]} [options.ignoreFields=[]] + * A list of fields to ignore in the collection data, e.g. where remote + * settings itself adds extra fields. `schema`, `id`, and `last_modified` are + * always ignored. + * @param {Function} [options.extraAssertsFn] + * An optional function to run additional assertions on each entry in the + * collection. + * @param {Function} options.getEntryId + * A function to get the identifier for each entry in the collection. + */ +async function assertSearchConfigValidates({ + collectionName, + collectionData, + ignoreFields = [], + extraAssertsFn, + getEntryId, +}) { + let schema = await IOUtils.readJSON( + PathUtils.join(do_get_cwd().path, `${collectionName}-schema.json`) ); -}); -async function checkSearchConfigValidates(schema, searchConfig) { disallowAdditionalProperties(schema); let validator = new JsonSchema.Validator(schema); - for (let entry of searchConfig) { + for (let entry of collectionData) { // Records in Remote Settings contain additional properties independent of // the schema. Hence, we don't want to validate their presence. - delete entry.schema; - delete entry.id; - delete entry.last_modified; + for (let field of [...ignoreFields, "schema", "id", "last_modified"]) { + delete entry[field]; + } let result = validator.validate(entry); - // entry.webExtension.id supports search-config v1. - let message = `Should validate ${ - entry.identifier ?? entry.recordType ?? entry.webExtension.id - }`; + let message = `Should validate ${getEntryId(entry)}`; if (!result.valid) { message += `:\n${JSON.stringify(result.errors, null, 2)}`; } Assert.ok(result.valid, message); - // All engine objects should have the base URL defined for each entry in - // entry.base.urls. - // Unfortunately this is difficult to enforce in the schema as it would - // need a `required` field that works across multiple levels. - if (entry.recordType == "engine") { - for (let urlEntry of Object.values(entry.base.urls)) { - Assert.ok( - urlEntry.base, - "Should have a base url for every URL defined on the top-level base object." - ); - } - } + extraAssertsFn?.(entry); } } -async function checkSearchConfigOverrideValidates( - schema, - searchConfigOverride -) { - let validator = new JsonSchema.Validator(schema); - - for (let entry of searchConfigOverride) { - // Records in Remote Settings contain additional properties independent of - // the schema. Hence, we don't want to validate their presence. - delete entry.schema; - delete entry.id; - delete entry.last_modified; - - let result = validator.validate(entry); - - let message = `Should validate ${entry.identifier ?? entry.telemetryId}`; - if (!result.valid) { - message += `:\n${JSON.stringify(result.errors, null, 2)}`; - } - Assert.ok(result.valid, message); - } -} +add_setup(async function () { + updateAppInfo({ ID: "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}" }); +}); add_task(async function test_search_config_validates_to_schema_v1() { let selector = new SearchEngineSelectorOld(() => {}); - let searchConfig = await selector.getEngineConfiguration(); - await checkSearchConfigValidates(searchConfigSchemaV1, searchConfig); -}); - -add_task(async function test_ui_schema_valid_v1() { - let uiSchema = await IOUtils.readJSON( - PathUtils.join(do_get_cwd().path, "search-config-ui-schema.json") - ); - - await checkUISchemaValid(searchConfigSchemaV1, uiSchema); + await assertSearchConfigValidates({ + collectionName: "search-config", + collectionData: await selector.getEngineConfiguration(), + getEntryId: entry => entry.webExtension.id, + }); }); add_task(async function test_search_config_override_validates_to_schema_v1() { let selector = new SearchEngineSelectorOld(() => {}); - let searchConfigOverrides = await selector.getEngineConfigurationOverrides(); - let overrideSchema = await IOUtils.readJSON( - PathUtils.join(do_get_cwd().path, "search-config-overrides-schema.json") - ); - await checkSearchConfigOverrideValidates( - overrideSchema, - searchConfigOverrides - ); + await assertSearchConfigValidates({ + collectionName: "search-config-overrides", + collectionData: await selector.getEngineConfigurationOverrides(), + getEntryId: entry => entry.telemetryId, + }); }); add_task( @@ -171,20 +147,26 @@ add_task( SearchUtils.newSearchConfigEnabled = true; let selector = new SearchEngineSelector(() => {}); - let searchConfig = await selector.getEngineConfiguration(); - - await checkSearchConfigValidates(searchConfigSchema, searchConfig); - } -); - -add_task( - { skip_if: () => !SearchUtils.newSearchConfigEnabled }, - async function test_ui_schema_valid() { - let uiSchema = await IOUtils.readJSON( - PathUtils.join(do_get_cwd().path, "search-config-v2-ui-schema.json") - ); - await checkUISchemaValid(searchConfigSchema, uiSchema); + await assertSearchConfigValidates({ + collectionName: "search-config-v2", + collectionData: await selector.getEngineConfiguration(), + getEntryId: entry => entry.identifier, + extraAssertsFn: entry => { + // All engine objects should have the base URL defined for each entry in + // entry.base.urls. + // Unfortunately this is difficult to enforce in the schema as it would + // need a `required` field that works across multiple levels. + if (entry.recordType == "engine") { + for (let urlEntry of Object.values(entry.base.urls)) { + Assert.ok( + urlEntry.base, + "Should have a base url for every URL defined on the top-level base object." + ); + } + } + }, + }); } ); @@ -192,18 +174,33 @@ add_task( { skip_if: () => !SearchUtils.newSearchConfigEnabled }, async function test_search_config_override_validates_to_schema() { let selector = new SearchEngineSelector(() => {}); - let searchConfigOverrides = - await selector.getEngineConfigurationOverrides(); - let overrideSchema = await IOUtils.readJSON( - PathUtils.join( - do_get_cwd().path, - "search-config-overrides-v2-schema.json" - ) - ); - - await checkSearchConfigOverrideValidates( - overrideSchema, - searchConfigOverrides - ); + + await assertSearchConfigValidates({ + collectionName: "search-config-overrides-v2", + collectionData: await selector.getEngineConfigurationOverrides(), + getEntryId: entry => entry.identifier, + }); } ); + +add_task(async function test_search_config_icons_validates_to_schema() { + let searchIcons = RemoteSettings("search-config-icons"); + + await assertSearchConfigValidates({ + collectionName: "search-config-icons", + collectionData: await searchIcons.get(), + ignoreFields: ["attachment"], + getEntryId: entry => entry.engineIdentifiers[0], + }); +}); + +add_task(async function test_search_default_override_allowlist_validates() { + let allowlist = RemoteSettings("search-default-override-allowlist"); + + await assertSearchConfigValidates({ + collectionName: "search-default-override-allowlist", + collectionData: await allowlist.get(), + ignoreFields: ["attachment"], + getEntryId: entry => entry.engineName || entry.thirdPartyId, + }); +}); diff --git a/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchicons_validates.js b/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchicons_validates.js deleted file mode 100644 index c830bb7ade..0000000000 --- a/toolkit/components/search/tests/xpcshell/searchconfigs/test_searchicons_validates.js +++ /dev/null @@ -1,20 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - http://creativecommons.org/publicdomain/zero/1.0/ */ - -"use strict"; - -let searchIconsSchema; - -add_setup(async function () { - searchIconsSchema = await IOUtils.readJSON( - PathUtils.join(do_get_cwd().path, "search-config-icons-schema.json") - ); -}); - -add_task(async function test_ui_schema_valid() { - let uiSchema = await IOUtils.readJSON( - PathUtils.join(do_get_cwd().path, "search-config-icons-ui-schema.json") - ); - - await checkUISchemaValid(searchIconsSchema, uiSchema); -}); diff --git a/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.toml b/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.toml index 8baff2a38d..07567005d6 100644 --- a/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.toml +++ b/toolkit/components/search/tests/xpcshell/searchconfigs/xpcshell.toml @@ -40,20 +40,30 @@ requesttimeoutfactor = 2 ["test_rakuten.js"] -["test_searchconfig_validates.js"] +["test_searchconfig_ui_schemas_valid.js"] support-files = [ + "../../../schema/search-config-icons-schema.json", + "../../../schema/search-config-icons-ui-schema.json", "../../../schema/search-config-overrides-schema.json", + "../../../schema/search-config-overrides-ui-schema.json", "../../../schema/search-config-overrides-v2-schema.json", + "../../../schema/search-config-overrides-v2-ui-schema.json", "../../../schema/search-config-schema.json", "../../../schema/search-config-ui-schema.json", "../../../schema/search-config-v2-schema.json", "../../../schema/search-config-v2-ui-schema.json", + "../../../schema/search-default-override-allowlist-schema.json", + "../../../schema/search-default-override-allowlist-ui-schema.json", ] -["test_searchicons_validates.js"] +["test_searchconfig_validates.js"] support-files = [ "../../../schema/search-config-icons-schema.json", - "../../../schema/search-config-icons-ui-schema.json", + "../../../schema/search-config-overrides-schema.json", + "../../../schema/search-config-overrides-v2-schema.json", + "../../../schema/search-config-schema.json", + "../../../schema/search-config-v2-schema.json", + "../../../schema/search-default-override-allowlist-schema.json", ] ["test_selector_db_out_of_date.js"] |