Skip to content

Commit

Permalink
fix: Busted shows similar tables as false
Browse files Browse the repository at this point in the history
Signed-off-by: Rushikesh Tote <[email protected]>
  • Loading branch information
rushitote committed Apr 26, 2021
1 parent e07c484 commit f0a2787
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions spec/persist/saved_policy.csv
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions spec/rbac/role_manager_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
27 changes: 27 additions & 0 deletions src/util/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f0a2787

Please sign in to comment.