MCPcopy Create free account
hub / github.com/apache/casbin-server / TestABACModel

Function TestABACModel

server/model_test.go:84–115  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

82}
83
84func TestABACModel(t *testing.T) {
85 s := NewServer()
86 ctx := context.Background()
87
88 modelText, err := os.ReadFile("../examples/abac_model.conf")
89 if err != nil {
90 t.Error(err)
91 }
92
93 resp, err := s.NewEnforcer(ctx, &pb.NewEnforcerRequest{ModelText: string(modelText), AdapterHandle: -1, EnableAcceptJsonRequest: false})
94 if err != nil {
95 t.Error(err)
96 }
97 type ABACModel struct {
98 Name string
99 Owner string
100 }
101 e := resp.Handler
102
103 data1, _ := MakeABAC(ABACModel{Name: "data1", Owner: "alice"})
104 data2, _ := MakeABAC(ABACModel{Name: "data2", Owner: "bob"})
105
106 testModel(t, s, e, "alice", data1, "read", true)
107 testModel(t, s, e, "alice", data1, "write", true)
108 testModel(t, s, e, "alice", data2, "read", false)
109 testModel(t, s, e, "alice", data2, "write", false)
110 testModel(t, s, e, "bob", data1, "read", false)
111 testModel(t, s, e, "bob", data1, "write", false)
112 testModel(t, s, e, "bob", data2, "read", true)
113 testModel(t, s, e, "bob", data2, "write", true)
114
115}
116
117func testModel(t *testing.T, s *Server, enforcerHandler int32, sub string, obj string, act string, res bool) {
118 t.Helper()

Callers

nothing calls this directly

Calls 4

NewEnforcerMethod · 0.95
NewServerFunction · 0.85
MakeABACFunction · 0.85
testModelFunction · 0.85

Tested by

no test coverage detected