Skip to content

Commit c870998

Browse files
chore: fix check permission request [FLOW-BE-56] (#77)
* fix check permission request * fix TestResourceBuilder_Build * fix TestClient_CheckPermission * fix CheckPermission * UT comment out * Revert "UT comment out" This reverts commit 3186bde.
1 parent 5cedba0 commit c870998

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

cerbos/client/check.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"io"
89
"net/http"
910

1011
"github.com/reearth/reearthx/appx"
@@ -34,6 +35,7 @@ func NewClient(dashboardURL string) *Client {
3435
}
3536

3637
type CheckPermissionInput struct {
38+
UserId string `json:"userId"`
3739
Service string `json:"service"`
3840
Resource string `json:"resource"`
3941
Action string `json:"action"`
@@ -45,6 +47,9 @@ type CheckPermissionResponse struct {
4547
Allowed bool `json:"allowed"`
4648
} `json:"checkPermission"`
4749
} `json:"data"`
50+
Errors []struct {
51+
Message string `json:"message"`
52+
} `json:"errors"`
4853
}
4954

5055
type GraphQLQuery struct {
@@ -106,10 +111,31 @@ func (c *Client) executeRequest(req *http.Request) (bool, error) {
106111
}
107112
defer resp.Body.Close()
108113

114+
if resp.StatusCode != http.StatusOK {
115+
return false, fmt.Errorf("server returned non-OK status: %d", resp.StatusCode)
116+
}
117+
118+
bodyBytes, err := io.ReadAll(resp.Body)
119+
if err != nil {
120+
return false, fmt.Errorf("failed to read response body: %w", err)
121+
}
122+
123+
fmt.Printf("Response body: %s\n", string(bodyBytes))
124+
125+
resp.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
126+
109127
var response CheckPermissionResponse
110128
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
111129
return false, fmt.Errorf("failed to decode response: %w", err)
112130
}
113131

114-
return response.Data.CheckPermission.Allowed, nil
132+
if len(response.Errors) > 0 {
133+
return false, fmt.Errorf("GraphQL error: %s", response.Errors[0].Message)
134+
}
135+
136+
if response.Data.CheckPermission.Allowed {
137+
return true, nil
138+
}
139+
140+
return false, nil
115141
}

cerbos/client/check_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"net/http"
78
"net/http/httptest"
89
"testing"
@@ -103,7 +104,7 @@ func TestClient_CheckPermission(t *testing.T) {
103104
Action: "read",
104105
},
105106
serverStatus: http.StatusInternalServerError,
106-
wantErr: "failed to decode response",
107+
wantErr: fmt.Sprint("server returned non-OK status: ", http.StatusInternalServerError),
107108
},
108109
}
109110

cerbos/client/permission_checker.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ func NewPermissionChecker(service string, dashboardURL string) *PermissionChecke
1919
}
2020
}
2121

22-
func (p *PermissionChecker) CheckPermission(ctx context.Context, authInfo *appx.AuthInfo, resource string, action string) (bool, error) {
22+
func (p *PermissionChecker) CheckPermission(ctx context.Context, authInfo *appx.AuthInfo, userId string, resource string, action string) (bool, error) {
2323
if p == nil {
2424
return false, fmt.Errorf("permission checker not found")
2525
}
2626

27-
if authInfo == nil {
28-
return false, fmt.Errorf("auth info not found")
29-
}
30-
3127
input := CheckPermissionInput{
28+
UserId: userId,
3229
Service: p.Service,
3330
Resource: resource,
3431
Action: action,

cerbos/generator/builder_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,26 @@ func TestResourceBuilder_Build(t *testing.T) {
176176
resources: tt.resources,
177177
}
178178
result := builder.Build()
179-
assert.Equal(t, tt.expected, result)
179+
180+
assert.Equal(t, len(tt.expected), len(result))
181+
182+
expectedMap := make(map[string]ResourceDefinition)
183+
for _, res := range tt.expected {
184+
expectedMap[res.Resource] = res
185+
}
186+
187+
resultMap := make(map[string]ResourceDefinition)
188+
for _, res := range result {
189+
resultMap[res.Resource] = res
190+
}
191+
192+
for resource, expectedDef := range expectedMap {
193+
resultDef, exists := resultMap[resource]
194+
assert.True(t, exists)
195+
if exists {
196+
assert.ElementsMatch(t, expectedDef.Actions, resultDef.Actions)
197+
}
198+
}
180199
})
181200
}
182201
}

0 commit comments

Comments
 (0)