-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added test for FileAdapter and fixed errors in it (#41)
Signed-off-by: Rushikesh Tote <[email protected]>
- Loading branch information
Showing
2 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters