Skip to content

Commit

Permalink
fix: avoid panic on server exception (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
FizikRoot authored Jun 7, 2022
1 parent 4c70268 commit 8577784
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions client/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func (e *Enforcer) Enforce(ctx context.Context, params ...interface{}) (bool, er
EnforcerHandler: e.handler,
Params: data,
})
if err != nil {
return false, err
}
return res.Res, err
}

Expand Down
25 changes: 21 additions & 4 deletions client/enforce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func testGetPolicy(t *testing.T, myRes, res [][]string) {
func testAddPolicy(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := e.AddPolicy(ctx, "alice", "data1", "read" )
_, err := e.AddPolicy(ctx, "alice", "data1", "read")
if err != nil {
t.Fatalf("GetPolicy err: %v", err)
}
Expand All @@ -63,7 +63,7 @@ func testAddPolicy(t *testing.T) {
func testRemovePolicy(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_,err := e.RemovePolicy(ctx,"alice","data1","read")
_, err := e.RemovePolicy(ctx, "alice", "data1", "read")
if err != nil {
t.Fatalf("Remove err: %v", err)
}
Expand All @@ -75,13 +75,30 @@ func testRemovePolicy(t *testing.T) {
t.Fatalf("GetPolicy err: %v", err)
}

testGetPolicy(t, policies, [][]string{
})
testGetPolicy(t, policies, [][]string{})
}

func testEnforce(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := e.Enforce(ctx, "alice")
if err == nil {
t.Fatalf("Not found error: invalid request size")
}

res, err := e.Enforce(ctx, "alice", "data1", "read")
if err != nil {
t.Fatalf("Remove err: %v", err)
}
if !res {
t.Fatalf("Matched policies")
}
}

func TestEnforcer(t *testing.T) {
testNewEnforcer(t)

testAddPolicy(t)
testEnforce(t)
testRemovePolicy(t)
}

0 comments on commit 8577784

Please sign in to comment.