Skip to content

Commit f267af7

Browse files
authored
feat: Added test for FileAdapter and fixed errors in it (#41)
Signed-off-by: Rushikesh Tote <rushi.tote@gmail.com>
1 parent 98dc848 commit f267af7

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

spec/persist/file_adapter_spec.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 file_adapter_module = require("src.persist.file_adapter.FileAdapter")
16+
local model_module = require("src.model.Model")
17+
18+
local path = os.getenv("PWD") or io.popen("cd"):read()
19+
local basic_path = path .. "/examples/basic_model.conf"
20+
local basic_policy_path = path .. "/examples/basic_policy.csv"
21+
local rbac_path = path .. "/examples/rbac_model.conf"
22+
local rbac_policy_path = path .. "/examples/rbac_policy.csv"
23+
24+
local save_policy_path = path .. "/spec/persist/saved_policy.csv"
25+
26+
describe("FileAdapter tests", function()
27+
28+
it("test initialize", function ()
29+
local f = FileAdapter:new(basic_policy_path)
30+
assert.are.same(f.filePath, basic_policy_path)
31+
end)
32+
33+
it("test loadPolicy: basic_policy", function ()
34+
local f = FileAdapter:new(basic_policy_path)
35+
local m = Model:new()
36+
m:loadModel(basic_path)
37+
f:loadPolicy(m)
38+
39+
local rule = {{"alice", "data1", "read"},
40+
{"bob", "data2", "write"}}
41+
42+
assert.is.True(m:hasPolicy("p", "p", rule[1]))
43+
assert.are.same(m:getPolicy("p","p"), rule)
44+
end)
45+
46+
it("test loadPolicy: rbac_policy", function ()
47+
local f = FileAdapter:new(rbac_policy_path)
48+
local m = Model:new()
49+
m:loadModel(rbac_path)
50+
f:loadPolicy(m)
51+
52+
local rule = {"alice", "data1", "read"}
53+
local g_rule = {"alice", "data2_admin"}
54+
55+
assert.is.True(m:hasPolicy("p", "p", rule))
56+
assert.is.True(m:hasPolicy("g", "g", g_rule))
57+
end)
58+
59+
it("test savePolicy", function ()
60+
local f = FileAdapter:new(rbac_policy_path)
61+
local m = Model:new()
62+
m:loadModel(rbac_path)
63+
f:loadPolicy(m)
64+
f:savePolicy(m, save_policy_path)
65+
66+
local new_file_adapter = FileAdapter:new(save_policy_path)
67+
local new_model = Model:new()
68+
new_model:loadModel(rbac_path)
69+
new_file_adapter:loadPolicy(new_model)
70+
71+
assert.are.same(m:getPolicy("p", "p"),new_model:getPolicy("p", "p"))
72+
assert.are.same(m:getPolicy("g", "g"),new_model:getPolicy("g", "g"))
73+
end)
74+
75+
it("test not implemented functions", function ()
76+
local f = FileAdapter:new(basic_policy_path)
77+
local m = Model:new()
78+
m:loadModel(basic_path)
79+
f:loadPolicy(m)
80+
81+
assert.has_error(function () f:addPolicy() end, "not implemented")
82+
assert.has_error(function () f:removePolicy() end, "not implemented")
83+
assert.has_error(function () f:removeFilteredPolicy() end, "not implemented")
84+
end)
85+
end)

src/persist/file_adapter/FileAdapter.lua

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,28 @@ end
5656
--[[
5757
* savePolicy saves all policy rules to the storage.
5858
]]
59-
function FileAdapter:savePolicy(filePath)
60-
local f = assert(io.open(self.filePath,"w"))
59+
function FileAdapter:savePolicy(model, saveToFilePath)
60+
local filePath = saveToFilePath
61+
if not filePath then filePath = self.filePath end
6162

62-
for ptype, ast in pairs(model.model["p"]) do
63-
local str = ptype
64-
for _, rule in pairs(ast.policy) do
65-
str = str .. Util.arrayToString(rule) .. "\n"
63+
local f = assert(io.open(filePath,"w"))
64+
65+
if model.model["p"] then
66+
for ptype, ast in pairs(model.model["p"]) do
67+
for _, rule in pairs(ast.policy) do
68+
local str = ptype .. ", " .. Util.arrayToString(rule) .. "\n"
69+
f:write(str)
70+
end
6671
end
67-
f:write(str)
6872
end
6973

70-
for ptype, ast in pairs(model.model["g"]) do
71-
local str = ptype
72-
for _, rule in pairs(ast.policy) do
73-
str = str .. Util.arrayToString(rule) .. "\n"
74+
if model.model["g"] then
75+
for ptype, ast in pairs(model.model["g"]) do
76+
for _, rule in pairs(ast.policy) do
77+
local str = ptype .. ", " .. Util.arrayToString(rule) .. "\n"
78+
f:write(str)
79+
end
7480
end
75-
f:write(str)
7681
end
7782

7883
f:close()

0 commit comments

Comments
 (0)