From f0a27876682cc617dc5f412d4497f56590575783 Mon Sep 17 00:00:00 2001 From: Rushikesh Tote Date: Mon, 26 Apr 2021 10:56:49 +0530 Subject: [PATCH] fix: Busted shows similar tables as false Signed-off-by: Rushikesh Tote --- spec/persist/saved_policy.csv | 5 +++++ spec/rbac/role_manager_spec.lua | 7 ++++--- src/util/Util.lua | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 spec/persist/saved_policy.csv diff --git a/spec/persist/saved_policy.csv b/spec/persist/saved_policy.csv new file mode 100644 index 0000000..8479f3c --- /dev/null +++ b/spec/persist/saved_policy.csv @@ -0,0 +1,5 @@ +p, alice, data1, read +p, bob, data2, write +p, data2_admin, data2, read +p, data2_admin, data2, write +g, alice, data2_admin diff --git a/spec/rbac/role_manager_spec.lua b/spec/rbac/role_manager_spec.lua index 39b7f89..12a25c1 100644 --- a/spec/rbac/role_manager_spec.lua +++ b/spec/rbac/role_manager_spec.lua @@ -13,6 +13,7 @@ --limitations under the License. local role_manager_module = require("src.rbac.DefaultRoleManager") +local util_module = require("src.util.Util") -- test function for testing the matching function functionality in Role Manager -- true if n1=n2 or n1 matches the pattern of n2 or n2 matches the pattern of n1 @@ -77,9 +78,9 @@ describe("DefaultRoleManager tests", function () assert.are.same(rm:getUsers("u2"),{}) assert.are.same(rm:getUsers("u3"),{}) assert.are.same(rm:getUsers("u4"),{}) - assert.are.same(rm:getUsers("g1"),{"u2", "u1"}) - assert.are.same(rm:getUsers("g2"),{"u3", "u4"}) - assert.are.same(rm:getUsers("g3"),{"u4", "g1"}) + assert.is.True(Util.areTablesSame(rm:getUsers("g1"),{"u2", "u1"})) + assert.is.True(Util.areTablesSame(rm:getUsers("g2"),{"u3", "u4"})) + assert.is.True(Util.areTablesSame(rm:getUsers("g3"),{"u4", "g1"})) rm:deleteLink("g1", "g3") rm:deleteLink("u4", "g2") diff --git a/src/util/Util.lua b/src/util/Util.lua index fba844a..28870c4 100644 --- a/src/util/Util.lua +++ b/src/util/Util.lua @@ -182,4 +182,31 @@ function Util.isInstance(o, parent) return false end +-- Searches if all values in a table are present in the other table regardless of order +function Util.areTablesSame(a, b) + local c = {} + for _, v in pairs(a) do + if c[v] then + c[v] = c[v] + 1 + else + c[v] = 1 + end + end + + for _, v in pairs(b) do + if c[v] then + c[v] = c[v] - 1 + if c[v] == 0 then + c[v] = nil + end + else + return false + end + end + for _, v in pairs(c) do + return false + end + return true +end + return Util