Annotate

spec/util_roles_spec.lua @ 12747:9d6d64fb7641

util.roles: Add tests
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Oct 2022 16:58:52 +0100
child 12753:2eb02b32bb4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12747
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 describe("util.roles", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 randomize(false);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local roles;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 it("can be loaded", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 roles = require "util.roles";
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local test_role;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 it("can create a new role", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 test_role = roles.new();
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 assert.is_not_nil(test_role);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 assert.is_truthy(roles.is_role(test_role));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 describe("role object", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 it("is restrictive by default", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 assert.falsy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 it("allows you to set permissions", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 test_role:set_permission("my-permission", true);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 assert.truthy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 it("allows you to set negative permissions", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 test_role:set_permission("my-other-permission", false);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 assert.falsy(test_role:may("my-other-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 it("does not allows you to override previously set permissions by default", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local ok, err = test_role:set_permission("my-permission", false);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 assert.falsy(ok);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 assert.is_equal("policy-already-exists", err);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- Confirm old permission still in place
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 assert.truthy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 it("allows you to explicitly override previously set permissions", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 assert.truthy(test_role:set_permission("my-permission", false, true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 assert.falsy(test_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 describe("inheritance", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local child_role;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 it("works", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 test_role:set_permission("inherited-permission", true);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 child_role = roles.new({
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 inherits = { test_role };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 });
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 assert.truthy(child_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 assert.falsy(child_role:may("my-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 it("allows listing policies", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local expected = {
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 ["my-permission"] = false;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 ["my-other-permission"] = false;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 ["inherited-permission"] = true;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local received = {};
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 for permission_name, permission_policy in child_role:policies() do
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 received[permission_name] = permission_policy;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 assert.same(expected, received);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 it("supports multiple depths of inheritance", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local grandchild_role = roles.new({
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 inherits = { child_role };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 });
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 assert.truthy(grandchild_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 describe("supports ordered inheritance from multiple roles", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local parent_role = roles.new();
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local final_role = roles.new({
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 -- Yes, the names are getting confusing.
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 -- btw, test_role is inherited through child_role.
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 inherits = { parent_role, child_role };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 });
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local test_cases = {
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 -- { <final_role policy>, <parent_role policy>, <test_role policy> }
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 { true, nil, false, result = true };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 { nil, false, true, result = false };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 { nil, true, false, result = true };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 { nil, nil, false, result = false };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 { nil, nil, true, result = true };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 };
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 for n, test_case in ipairs(test_cases) do
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 it("(case "..n..")", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local perm_name = ("multi-inheritance-perm-%d"):format(n);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 assert.truthy(final_role:set_permission(perm_name, test_case[1]));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 assert.truthy(parent_role:set_permission(perm_name, test_case[2]));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 assert.truthy(test_role:set_permission(perm_name, test_case[3]));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 assert.equal(test_case.result, final_role:may(perm_name));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 it("updates child roles when parent roles change", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 assert.truthy(child_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 assert.truthy(test_role:set_permission("inherited-permission", false, true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 assert.falsy(child_role:may("inherited-permission"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 describe("cloning", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local cloned_role;
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 it("works", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 assert.truthy(test_role:set_permission("perm-1", true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 cloned_role = test_role:clone();
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 assert.truthy(cloned_role:may("perm-1"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 it("isolates changes", function ()
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 -- After cloning, changes in either the original or the clone
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 -- should not appear in the other.
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 assert.truthy(test_role:set_permission("perm-1", false, true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 assert.truthy(test_role:set_permission("perm-2", true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 assert.truthy(cloned_role:set_permission("perm-3", true));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 assert.truthy(cloned_role:may("perm-1"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 assert.falsy(cloned_role:may("perm-2"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 assert.falsy(test_role:may("perm-3"));
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end);
9d6d64fb7641 util.roles: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end);