|
| 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) |
0 commit comments