summaryrefslogtreecommitdiffstats
path: root/browser/components/enterprisepolicies/tests/xpcshell/test_macosparser_unflatten.js
blob: 096852612c63c6ec2ed14509f9fadb0ce4be6af6 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

let { macOSPoliciesParser } = ChromeUtils.importESModule(
  "resource://gre/modules/policies/macOSPoliciesParser.sys.mjs"
);

add_task(async function test_object_unflatten() {
  // Note: these policies are just examples and they won't actually
  // run through the policy engine on this test. We're just testing
  // that the unflattening algorithm produces the correct output.
  let input = {
    DisplayBookmarksToolbar: true,

    Homepage__URL: "https://www.mozilla.org",
    Homepage__Locked: "true",
    Homepage__Additional__0: "https://extra-homepage-1.example.com",
    Homepage__Additional__1: "https://extra-homepage-2.example.com",

    WebsiteFilter__Block__0: "*://*.example.org/*",
    WebsiteFilter__Block__1: "*://*.example.net/*",
    WebsiteFilter__Exceptions__0: "*://*.example.org/*exception*",

    Permissions__Camera__Allow__0: "https://www.example.com",

    Permissions__Notifications__Allow__0: "https://www.example.com",
    Permissions__Notifications__Allow__1: "https://www.example.org",
    Permissions__Notifications__Block__0: "https://www.example.net",

    Permissions__Notifications__BlockNewRequests: true,
    Permissions__Notifications__Locked: true,

    Bookmarks__0__Title: "Bookmark 1",
    Bookmarks__0__URL: "https://bookmark1.example.com",

    Bookmarks__1__Title: "Bookmark 2",
    Bookmarks__1__URL: "https://bookmark2.example.com",
    Bookmarks__1__Folder: "Folder",
  };

  let expected = {
    DisplayBookmarksToolbar: true,

    Homepage: {
      URL: "https://www.mozilla.org",
      Locked: "true",
      Additional: [
        "https://extra-homepage-1.example.com",
        "https://extra-homepage-2.example.com",
      ],
    },

    WebsiteFilter: {
      Block: ["*://*.example.org/*", "*://*.example.net/*"],
      Exceptions: ["*://*.example.org/*exception*"],
    },

    Permissions: {
      Camera: {
        Allow: ["https://www.example.com"],
      },

      Notifications: {
        Allow: ["https://www.example.com", "https://www.example.org"],
        Block: ["https://www.example.net"],
        BlockNewRequests: true,
        Locked: true,
      },
    },

    Bookmarks: [
      {
        Title: "Bookmark 1",
        URL: "https://bookmark1.example.com",
      },
      {
        Title: "Bookmark 2",
        URL: "https://bookmark2.example.com",
        Folder: "Folder",
      },
    ],
  };

  let unflattened = macOSPoliciesParser.unflatten(input);

  deepEqual(unflattened, expected, "Input was unflattened correctly.");
});

add_task(async function test_array_unflatten() {
  let input = {
    Foo__1: 1,
    Foo__5: 5,
    Foo__10: 10,
    Foo__30: 30,
    Foo__51: 51, // This one should not be included as the limit is 50
  };

  let unflattened = macOSPoliciesParser.unflatten(input);
  equal(unflattened.Foo.length, 31, "Array size is correct");

  let expected = {
    Foo: [, 1, , , , 5], // eslint-disable-line no-sparse-arrays
  };
  expected.Foo[10] = 10;
  expected.Foo[30] = 30;

  deepEqual(unflattened, expected, "Array was unflattened correctly.");
});