forked from casbin/casbin-pg-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter_grouping_test.go
73 lines (65 loc) · 2.55 KB
/
adapter_grouping_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package bunadapter_test
import (
bunadapter "github.com/msales/casbin-bun-adapter"
)
func (suite *AdapterTestSuite) TestLoadFilteredGroupingPolicy() {
err := suite.enforcer.LoadFilteredPolicy(&bunadapter.Filter{
G: []string{"bob"},
})
suite.Require().NoError(err)
suite.Assert().True(suite.enforcer.IsFiltered())
suite.assertEnforcerGroupingPolicy([][]string{})
err = suite.enforcer.LoadFilteredPolicy(&bunadapter.Filter{
G: []string{"alice"},
})
suite.Require().NoError(err)
suite.Assert().True(suite.enforcer.IsFiltered())
suite.assertEnforcerGroupingPolicy([][]string{{"alice", "data2_admin"}})
}
func (suite *AdapterTestSuite) TestAddExistingGroupingPolicy() {
suite.False(suite.enforcer.AddGroupingPolicy("alice", "data2_admin"))
suite.assertAllowed("alice", "data2", "write")
suite.assertDisallowed("alice", "data1", "write")
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
})
}
func (suite *AdapterTestSuite) TestAddNotExistingGroupingPolicy() {
suite.True(suite.enforcer.AddGroupingPolicy("alice", "data1_admin"))
suite.True(suite.enforcer.AddPolicy("data1_admin", "data1", "read"))
suite.True(suite.enforcer.AddPolicy("data1_admin", "data1", "write"))
suite.assertAllowed("alice", "data2", "write")
suite.assertAllowed("alice", "data1", "write")
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"data1_admin", "data1", "read"},
{"data1_admin", "data1", "write"},
})
}
func (suite *AdapterTestSuite) TestAddNewHierarchicalGroupingPolicy() {
suite.True(suite.enforcer.AddGroupingPolicy("admin", "data1_admin"))
suite.True(suite.enforcer.AddPolicy("data1_admin", "data1", "read"))
suite.True(suite.enforcer.AddPolicy("data1_admin", "data1", "write"))
suite.True(suite.enforcer.AddGroupingPolicy("admin", "data2_admin"))
suite.True(suite.enforcer.AddGroupingPolicy("joe", "admin"))
suite.assertAllowed("joe", "data1", "read")
suite.assertAllowed("joe", "data1", "write")
suite.assertAllowed("joe", "data2", "read")
suite.assertAllowed("joe", "data2", "write")
suite.assertDisallowed("joe", "data3", "read")
suite.assertDisallowed("joe", "data3", "write")
suite.assertEnforcerPolicy([][]string{
{"alice", "data1", "read"},
{"bob", "data2", "write"},
{"data2_admin", "data2", "read"},
{"data2_admin", "data2", "write"},
{"data1_admin", "data1", "read"},
{"data1_admin", "data1", "write"},
})
}