Skip to content

Commit

Permalink
feat: Added test for FileAdapter and fixed errors in it (#41)
Browse files Browse the repository at this point in the history
Signed-off-by: Rushikesh Tote <[email protected]>
  • Loading branch information
rushitote authored Apr 16, 2021
1 parent 98dc848 commit f267af7
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
85 changes: 85 additions & 0 deletions spec/persist/file_adapter_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
--Copyright 2021 The casbin Authors. All Rights Reserved.
--
--Licensed under the Apache License, Version 2.0 (the "License");
--you may not use this file except in compliance with the License.
--You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
--Unless required by applicable law or agreed to in writing, software
--distributed under the License is distributed on an "AS IS" BASIS,
--WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--See the License for the specific language governing permissions and
--limitations under the License.

local file_adapter_module = require("src.persist.file_adapter.FileAdapter")
local model_module = require("src.model.Model")

local path = os.getenv("PWD") or io.popen("cd"):read()
local basic_path = path .. "/examples/basic_model.conf"
local basic_policy_path = path .. "/examples/basic_policy.csv"
local rbac_path = path .. "/examples/rbac_model.conf"
local rbac_policy_path = path .. "/examples/rbac_policy.csv"

local save_policy_path = path .. "/spec/persist/saved_policy.csv"

describe("FileAdapter tests", function()

it("test initialize", function ()
local f = FileAdapter:new(basic_policy_path)
assert.are.same(f.filePath, basic_policy_path)
end)

it("test loadPolicy: basic_policy", function ()
local f = FileAdapter:new(basic_policy_path)
local m = Model:new()
m:loadModel(basic_path)
f:loadPolicy(m)

local rule = {{"alice", "data1", "read"},
{"bob", "data2", "write"}}

assert.is.True(m:hasPolicy("p", "p", rule[1]))
assert.are.same(m:getPolicy("p","p"), rule)
end)

it("test loadPolicy: rbac_policy", function ()
local f = FileAdapter:new(rbac_policy_path)
local m = Model:new()
m:loadModel(rbac_path)
f:loadPolicy(m)

local rule = {"alice", "data1", "read"}
local g_rule = {"alice", "data2_admin"}

assert.is.True(m:hasPolicy("p", "p", rule))
assert.is.True(m:hasPolicy("g", "g", g_rule))
end)

it("test savePolicy", function ()
local f = FileAdapter:new(rbac_policy_path)
local m = Model:new()
m:loadModel(rbac_path)
f:loadPolicy(m)
f:savePolicy(m, save_policy_path)

local new_file_adapter = FileAdapter:new(save_policy_path)
local new_model = Model:new()
new_model:loadModel(rbac_path)
new_file_adapter:loadPolicy(new_model)

assert.are.same(m:getPolicy("p", "p"),new_model:getPolicy("p", "p"))
assert.are.same(m:getPolicy("g", "g"),new_model:getPolicy("g", "g"))
end)

it("test not implemented functions", function ()
local f = FileAdapter:new(basic_policy_path)
local m = Model:new()
m:loadModel(basic_path)
f:loadPolicy(m)

assert.has_error(function () f:addPolicy() end, "not implemented")
assert.has_error(function () f:removePolicy() end, "not implemented")
assert.has_error(function () f:removeFilteredPolicy() end, "not implemented")
end)
end)
29 changes: 17 additions & 12 deletions src/persist/file_adapter/FileAdapter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,28 @@ end
--[[
* savePolicy saves all policy rules to the storage.
]]
function FileAdapter:savePolicy(filePath)
local f = assert(io.open(self.filePath,"w"))
function FileAdapter:savePolicy(model, saveToFilePath)
local filePath = saveToFilePath
if not filePath then filePath = self.filePath end

for ptype, ast in pairs(model.model["p"]) do
local str = ptype
for _, rule in pairs(ast.policy) do
str = str .. Util.arrayToString(rule) .. "\n"
local f = assert(io.open(filePath,"w"))

if model.model["p"] then
for ptype, ast in pairs(model.model["p"]) do
for _, rule in pairs(ast.policy) do
local str = ptype .. ", " .. Util.arrayToString(rule) .. "\n"
f:write(str)
end
end
f:write(str)
end

for ptype, ast in pairs(model.model["g"]) do
local str = ptype
for _, rule in pairs(ast.policy) do
str = str .. Util.arrayToString(rule) .. "\n"
if model.model["g"] then
for ptype, ast in pairs(model.model["g"]) do
for _, rule in pairs(ast.policy) do
local str = ptype .. ", " .. Util.arrayToString(rule) .. "\n"
f:write(str)
end
end
f:write(str)
end

f:close()
Expand Down

0 comments on commit f267af7

Please sign in to comment.