1+ -- Copyright 2021 The casbin Authors. All Rights Reserved.
2+ --
3+ -- Licensed under the Apache License, Version 2.0 (the "License");
4+ -- you may not use this file except in compliance with the License.
5+ -- You may obtain a copy of the License at
6+ --
7+ -- http://www.apache.org/licenses/LICENSE-2.0
8+ --
9+ -- Unless required by applicable law or agreed to in writing, software
10+ -- distributed under the License is distributed on an "AS IS" BASIS,
11+ -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ -- See the License for the specific language governing permissions and
13+ -- limitations under the License.
14+
15+ local enforcer_module = require (" src.main.Enforcer" )
16+ local path = os.getenv (" PWD" ) or io.popen (" cd" ):read ()
17+
18+ describe (" Management API tests" , function ()
19+ it (" Get Subjects, Objects, Actions, Roles test" , function ()
20+ local model = path .. " /examples/rbac_model.conf"
21+ local policy = path .. " /examples/rbac_policy.csv"
22+
23+ local e = Enforcer :new (model , policy )
24+
25+ assert .is .Same (e :GetAllSubjects (), {" alice" , " bob" , " data2_admin" })
26+ assert .is .Same (e :GetAllObjects (), {" data1" , " data2" })
27+ assert .is .Same (e :GetAllActions (), {" read" , " write" })
28+ assert .is .Same (e :GetAllRoles (), {" data2_admin" })
29+
30+ assert .is .Same (e :GetAllNamedSubjects (" p" ), {" alice" , " bob" , " data2_admin" })
31+ assert .is .Same (e :GetAllNamedObjects (" p" ), {" data1" , " data2" })
32+ assert .is .Same (e :GetAllNamedActions (" p" ), {" read" , " write" })
33+ assert .is .Same (e :GetAllNamedRoles (" g" ), {" data2_admin" })
34+ end )
35+
36+ it (" Get Policy test" , function ()
37+ local model = path .. " /examples/rbac_model.conf"
38+ local policy = path .. " /examples/rbac_policy.csv"
39+
40+ local e = Enforcer :new (model , policy )
41+ local res = {
42+ {" alice" , " data1" , " read" },
43+ {" bob" , " data2" , " write" },
44+ {" data2_admin" , " data2" , " read" },
45+ {" data2_admin" , " data2" , " write" }
46+ }
47+ assert .is .True (Util .array2DEquals (e :GetPolicy (), res ))
48+ end )
49+
50+ it (" Get Filtered Policy test" , function ()
51+ local model = path .. " /examples/rbac_model.conf"
52+ local policy = path .. " /examples/rbac_policy.csv"
53+
54+ local e = Enforcer :new (model , policy )
55+
56+ local res = {
57+ {" bob" , " data2" , " write" }
58+ }
59+ assert .is .Same (e :GetFilteredPolicy (0 , " bob" ), res )
60+
61+ res = {
62+ {" bob" , " data2" , " write" },
63+ {" data2_admin" , " data2" , " read" },
64+ {" data2_admin" , " data2" , " write" }
65+ }
66+
67+ assert .is .Same (e :GetFilteredPolicy (1 , " data2" ), res )
68+ end )
69+
70+ it (" Get Grouping Policy test" , function ()
71+ local model = path .. " /examples/rbac_model.conf"
72+ local policy = path .. " /examples/rbac_policy.csv"
73+
74+ local e = Enforcer :new (model , policy )
75+
76+ local res = {
77+ {" alice" , " data2_admin" }
78+ }
79+
80+ assert .is .Same (e :GetGroupingPolicy (), res )
81+ end )
82+
83+ it (" Get Filtered Grouping Policy test" , function ()
84+ local model = path .. " /examples/rbac_model.conf"
85+ local policy = path .. " /examples/rbac_policy.csv"
86+
87+ local e = Enforcer :new (model , policy )
88+
89+ local res = {
90+ {" alice" , " data2_admin" }
91+ }
92+
93+ assert .is .Same (e :GetFilteredGroupingPolicy (0 , " alice" ), res )
94+ end )
95+
96+ it (" Has Policy test" , function ()
97+ local model = path .. " /examples/rbac_model.conf"
98+ local policy = path .. " /examples/rbac_policy.csv"
99+
100+ local e = Enforcer :new (model , policy )
101+
102+ assert .is .True (e :HasPolicy (" alice" , " data1" , " read" ))
103+ assert .is .False (e :HasPolicy (" bob" , " data2" , " read" ))
104+ assert .is .True (e :HasPolicy (" bob" , " data2" , " write" ))
105+ end )
106+
107+ it (" Has Grouping Policy test" , function ()
108+ local model = path .. " /examples/rbac_model.conf"
109+ local policy = path .. " /examples/rbac_policy.csv"
110+
111+ local e = Enforcer :new (model , policy )
112+
113+ assert .is .True (e :HasGroupingPolicy (" alice" , " data2_admin" ))
114+ assert .is .False (e :HasGroupingPolicy (" bob" , " data2_admin" ))
115+ end )
116+
117+ it (" Modify Policy test" , function ()
118+ local model = path .. " /examples/rbac_model.conf"
119+ local policy = path .. " /examples/rbac_policy.csv"
120+
121+ local e = Enforcer :new (model , policy )
122+
123+ local res = {
124+ {" alice" , " data1" , " read" },
125+ {" bob" , " data2" , " write" },
126+ {" data2_admin" , " data2" , " read" },
127+ {" data2_admin" , " data2" , " write" }
128+ }
129+
130+ assert .is .Same (e :GetPolicy (), res )
131+
132+ e :RemovePolicy (" alice" , " data1" , " read" )
133+ e :RemovePolicy (" bob" , " data2" , " write" )
134+ e :RemovePolicy (" alice" , " data1" , " read" )
135+ e :AddPolicy (" eve" , " data3" , " read" )
136+ e :AddPolicy (" eve" , " data3" , " read" )
137+
138+ local rules = {
139+ {" jack" , " data4" , " read" },
140+ {" jack" , " data4" , " read" },
141+ {" jack" , " data4" , " read" },
142+ {" katy" , " data4" , " write" },
143+ {" leyo" , " data4" , " read" },
144+ {" katy" , " data4" , " write" },
145+ {" katy" , " data4" , " write" },
146+ {" ham" , " data4" , " write" }
147+ }
148+
149+ e :AddPolicies (rules )
150+ e :AddPolicies (rules )
151+
152+ res = {
153+ {" data2_admin" , " data2" , " read" },
154+ {" data2_admin" , " data2" , " write" },
155+ {" eve" , " data3" , " read" },
156+ {" jack" , " data4" , " read" },
157+ {" katy" , " data4" , " write" },
158+ {" leyo" , " data4" , " read" },
159+ {" ham" , " data4" , " write" }
160+ }
161+
162+ assert .is .Same (e :GetPolicy (), res )
163+
164+ e :RemovePolicies (rules )
165+ e :RemovePolicies (rules )
166+
167+ local namedPolicy = {" eve" , " data3" , " read" }
168+ e :RemoveNamedPolicy (" p" , namedPolicy )
169+ e :AddNamedPolicy (" p" , namedPolicy )
170+
171+ res = {
172+ {" data2_admin" , " data2" , " read" },
173+ {" data2_admin" , " data2" , " write" },
174+ {" eve" , " data3" , " read" }
175+ }
176+
177+ assert .is .Same (e :GetPolicy (), res )
178+
179+ e :RemoveFilteredPolicy (1 , " data2" )
180+ assert .is .Same (e :GetPolicy (), {{" eve" , " data3" , " read" }})
181+
182+ e :UpdatePolicy ({" eve" , " data3" , " read" }, {" eve" , " data3" , " write" })
183+ assert .is .Same (e :GetPolicy (), {{" eve" , " data3" , " write" }})
184+
185+ e :AddPolicies (rules )
186+ e :RemovePolicies ({{" eve" , " data3" , " write" }, {" leyo" , " data4" , " read" }, {" katy" , " data4" , " write" }})
187+ e :AddPolicies ({{" eve" , " data3" , " read" }, {" leyo" , " data4" , " write" }, {" katy" , " data1" , " write" }})
188+
189+ assert .is .True (e :HasPolicy (" eve" , " data3" , " read" ))
190+ assert .is .True (e :HasPolicy (" jack" , " data4" , " read" ))
191+ assert .is .True (e :HasPolicy (" katy" , " data1" , " write" ))
192+ assert .is .True (e :HasPolicy (" leyo" , " data4" , " write" ))
193+ assert .is .True (e :HasPolicy (" ham" , " data4" , " write" ))
194+ end )
195+
196+ it (" Modify Grouping Policy test" , function ()
197+ local model = path .. " /examples/rbac_model.conf"
198+ local policy = path .. " /examples/rbac_policy.csv"
199+
200+ local e = Enforcer :new (model , policy )
201+
202+ assert .is .True (e :HasGroupingPolicy (" alice" , " data2_admin" ))
203+
204+ local res = {
205+ {" alice" , " data2_admin" }
206+ }
207+ assert .is .Same (e :GetGroupingPolicy (), res )
208+
209+ e :AddGroupingPolicy (" bob" , " data2_admin" )
210+ res = {
211+ {" alice" , " data2_admin" },
212+ {" bob" , " data2_admin" }
213+ }
214+ assert .is .Same (e :GetGroupingPolicy (), res )
215+
216+ e :RemoveGroupingPolicy (" bob" , " data2_admin" )
217+
218+ local rules = {
219+ {" cathy" , " data2_admin" },
220+ {" eve" , " data2_admin" }
221+ }
222+
223+ e :AddGroupingPolicies (rules )
224+
225+ res = {
226+ {" alice" , " data2_admin" },
227+ {" cathy" , " data2_admin" },
228+ {" eve" , " data2_admin" }
229+ }
230+ assert .is .Same (e :GetGroupingPolicy (), res )
231+
232+ e :RemoveGroupingPolicies (rules )
233+ assert .is .Same (e :GetGroupingPolicy (), {{" alice" , " data2_admin" }})
234+
235+ e :UpdateGroupingPolicy ({" alice" , " data2_admin" }, {" bob" , " data2_admin" })
236+ assert .is .Same (e :GetGroupingPolicy (), {{" bob" , " data2_admin" }})
237+
238+ e :UpdateGroupingPolicy ({" bob" , " data2_admin" }, {" alice" , " data2_admin" })
239+
240+ rules = {
241+ {" alice" , " data1_admin" },
242+ {" bob" , " data1_admin" },
243+ {" eve" , " data2_admin" }
244+ }
245+
246+ e :AddGroupingPolicies (rules )
247+
248+ res = {
249+ {" alice" , " data1_admin" },
250+ {" bob" , " data1_admin" },
251+ }
252+ assert .is .Same (e :GetFilteredGroupingPolicy (1 , " data1_admin" ), res )
253+
254+ e :RemoveFilteredGroupingPolicy (1 , " data1_admin" )
255+
256+ res = {
257+ {" alice" , " data2_admin" },
258+ {" eve" , " data2_admin" }
259+ }
260+ assert .is .Same (e :GetGroupingPolicy (), res )
261+ end )
262+ end )
0 commit comments