From 8577784532593657e5d223274fcef6e0ac845432 Mon Sep 17 00:00:00 2001 From: Mikhail Rogushkov Date: Tue, 7 Jun 2022 17:08:57 +0300 Subject: [PATCH] fix: avoid panic on server exception (#21) --- client/enforce.go | 3 +++ client/enforce_test.go | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/client/enforce.go b/client/enforce.go index b9b7029..6753ec4 100644 --- a/client/enforce.go +++ b/client/enforce.go @@ -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 } diff --git a/client/enforce_test.go b/client/enforce_test.go index 726f18b..8babbaa 100644 --- a/client/enforce_test.go +++ b/client/enforce_test.go @@ -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) } @@ -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) } @@ -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) }