diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/enterprisepolicies/tests/browser | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/enterprisepolicies/tests/browser')
7 files changed, 267 insertions, 0 deletions
diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser.ini b/toolkit/components/enterprisepolicies/tests/browser/browser.ini new file mode 100644 index 0000000000..23a860dd8b --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser.ini @@ -0,0 +1,9 @@ +[DEFAULT] +head = head.js +support-files = + config_broken_json.json + +[browser_policies_basic_tests.js] +[browser_policies_broken_json.js] +[browser_policies_enterprise_only.js] +[browser_policies_mistyped_json.js] diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js new file mode 100644 index 0000000000..24de9c2f23 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js @@ -0,0 +1,126 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_simple_policies() { + let { Policies } = ChromeUtils.import( + "resource:///modules/policies/Policies.jsm" + ); + + let policy0Ran = false, + policy1Ran = false, + policy2Ran = false, + policy3Ran = false; + + // Implement functions to handle the four simple policies that will be added + // to the schema. + Policies.simple_policy0 = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was passed in config file"); + policy0Ran = true; + }, + }; + + Policies.simple_policy1 = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was passed in config file"); + manager.disallowFeature("feature1", /* needed in content process */ true); + policy1Ran = true; + }, + }; + + Policies.simple_policy2 = { + onBeforeUIStartup(manager, param) { + is(param, true, "Param matches what was passed in config file"); + manager.disallowFeature( + "feature2", + /* needed in content process */ false + ); + policy2Ran = true; + }, + }; + + Policies.simple_policy3 = { + onAllWindowsRestored(manager, param) { + is(param, false, "Param matches what was passed in config file"); + policy3Ran = true; + }, + }; + + await setupPolicyEngineWithJson( + // policies.json + { + policies: { + simple_policy0: true, + simple_policy1: true, + simple_policy2: true, + simple_policy3: false, + }, + }, + + // custom schema + { + properties: { + simple_policy0: { + type: "boolean", + }, + + simple_policy1: { + type: "boolean", + }, + + simple_policy2: { + type: "boolean", + }, + + simple_policy3: { + type: "boolean", + }, + }, + } + ); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.ACTIVE, + "Engine is active" + ); + is( + Services.policies.isAllowed("feature1"), + false, + "Dummy feature was disallowed" + ); + is( + Services.policies.isAllowed("feature2"), + false, + "Dummy feature was disallowed" + ); + + ok(policy0Ran, "Policy 0 ran correctly through BeforeAddons"); + ok(policy1Ran, "Policy 1 ran correctly through onProfileAfterChange"); + ok(policy2Ran, "Policy 2 ran correctly through onBeforeUIStartup"); + ok(policy3Ran, "Policy 3 ran correctly through onAllWindowsRestored"); + + await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function() { + if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) { + is( + Services.policies.isAllowed("feature1"), + false, + "Correctly disallowed in the content process" + ); + // Feature 2 wasn't explictly marked as needed in the content process, so it is not marked + // as disallowed there. + is( + Services.policies.isAllowed("feature2"), + true, + "Correctly missing in the content process" + ); + } + }); + + delete Policies.simple_policy0; + delete Policies.simple_policy1; + delete Policies.simple_policy2; + delete Policies.simple_policy3; +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js new file mode 100644 index 0000000000..a4a274ab08 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js @@ -0,0 +1,14 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_broken_json() { + await setupPolicyEngineWithJson("config_broken_json.json"); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.FAILED, + "Engine was correctly set to the error state" + ); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_enterprise_only.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_enterprise_only.js new file mode 100644 index 0000000000..85bec8191f --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_enterprise_only.js @@ -0,0 +1,70 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const PREF_DISALLOW_ENTERPRISE = "browser.policies.testing.disallowEnterprise"; + +add_task(async function test_enterprise_only_policies() { + let { Policies } = ChromeUtils.import( + "resource:///modules/policies/Policies.jsm" + ); + + let normalPolicyRan = false, + enterprisePolicyRan = false; + + Policies.NormalPolicy = { + onProfileAfterChange(manager, param) { + normalPolicyRan = true; + }, + }; + + Policies.EnterpriseOnlyPolicy = { + onProfileAfterChange(manager, param) { + enterprisePolicyRan = true; + }, + }; + + Services.prefs.setBoolPref(PREF_DISALLOW_ENTERPRISE, true); + + await setupPolicyEngineWithJson( + // policies.json + { + policies: { + NormalPolicy: true, + EnterpriseOnlyPolicy: true, + }, + }, + + // custom schema + { + properties: { + NormalPolicy: { + type: "boolean", + }, + + EnterpriseOnlyPolicy: { + type: "boolean", + enterprise_only: true, + }, + }, + } + ); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.ACTIVE, + "Engine is active" + ); + is(normalPolicyRan, true, "Normal policy ran as expected"); + is( + enterprisePolicyRan, + false, + "Enterprise-only policy was prevented from running" + ); + + // Clean-up + delete Policies.NormalPolicy; + delete Policies.EnterpriseOnlyPolicy; + Services.prefs.clearUserPref(PREF_DISALLOW_ENTERPRISE); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_mistyped_json.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_mistyped_json.js new file mode 100644 index 0000000000..0b82a11377 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_mistyped_json.js @@ -0,0 +1,17 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_json_with_mistyped_policies() { + // Note: The "polcies" string is intentionally mistyped + await setupPolicyEngineWithJson({ + polcies: {}, + }); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.FAILED, + "Engine was correctly set to the error state" + ); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/config_broken_json.json b/toolkit/components/enterprisepolicies/tests/browser/config_broken_json.json new file mode 100644 index 0000000000..7e13efdd88 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/config_broken_json.json @@ -0,0 +1,3 @@ +{ + "policies +} diff --git a/toolkit/components/enterprisepolicies/tests/browser/head.js b/toolkit/components/enterprisepolicies/tests/browser/head.js new file mode 100644 index 0000000000..f099749df1 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/head.js @@ -0,0 +1,28 @@ +/* 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 { EnterprisePolicyTesting, PoliciesPrefTracker } = ChromeUtils.import( + "resource://testing-common/EnterprisePolicyTesting.jsm", + null +); +const { TestUtils } = ChromeUtils.import( + "resource://testing-common/TestUtils.jsm", + null +); + +PoliciesPrefTracker.start(); + +async function setupPolicyEngineWithJson(json, customSchema) { + PoliciesPrefTracker.restoreDefaultValues(); + if (typeof json != "object") { + let filePath = getTestFilePath(json ? json : "non-existing-file.json"); + return EnterprisePolicyTesting.setupPolicyEngineWithJson( + filePath, + customSchema + ); + } + return EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema); +} |