summaryrefslogtreecommitdiffstats
path: root/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js
blob: 8efcdee316d0af0a1d296f92a6fa5753319805f6 (plain)
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
/* 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.importESModule(
    "resource:///modules/policies/Policies.sys.mjs"
  );

  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;
});

add_task(async function test_policy_cleanup() {
  await EnterprisePolicyTesting.setupPolicyEngineWithJson("");
  is(
    Services.policies.getActivePolicies(),
    undefined,
    "No policies should be defined"
  );
  is(
    Services.policies.status,
    Ci.nsIEnterprisePolicies.INACTIVE,
    "Engine is inactive at the end of the test"
  );
});