Skip to content

Commit

Permalink
fix: correctly set consistency on batch check
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris committed Oct 9, 2024
1 parent 0a690fc commit 498167b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
20 changes: 13 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2018,17 +2018,23 @@ func (client *OpenFgaClient) BatchCheckExecute(request SdkClientBatchCheckReques
return nil, err
}

checkOptions := &ClientCheckOptions{
AuthorizationModelId: authorizationModelId,
StoreId: storeId,
}

if request.GetOptions() != nil && request.GetOptions().Consistency != nil {
checkOptions.Consistency = request.GetOptions().Consistency
}

for index, checkBody := range *request.GetBody() {
index, checkBody := index, checkBody
group.Go(func() error {
singleResponse, err := client.CheckExecute(&SdkClientCheckRequest{
ctx: ctx,
Client: client,
body: &checkBody,
options: &ClientCheckOptions{
AuthorizationModelId: authorizationModelId,
StoreId: storeId,
},
ctx: ctx,
Client: client,
body: &checkBody,
options: checkOptions,
})

if _, ok := err.(fgaSdk.FgaApiAuthenticationError); ok {
Expand Down
12 changes: 9 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,7 @@ func TestOpenFgaClient(t *testing.T) {
options := ClientBatchCheckOptions{
AuthorizationModelId: openfga.PtrString(authModelId),
MaxParallelRequests: openfga.PtrInt32(5),
Consistency: openfga.CONSISTENCYPREFERENCE_UNSPECIFIED.Ptr(),
Consistency: openfga.CONSISTENCYPREFERENCE_HIGHER_CONSISTENCY.Ptr(),
}

var expectedResponse openfga.CheckResponse
Expand All @@ -2213,7 +2213,7 @@ func TestOpenFgaClient(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterMatcherResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", fgaClient.GetConfig().ApiUrl, getStoreId(t, fgaClient), test.RequestPath),
httpmock.BodyContainsString(`"consistency":"UNSPECIFIED"`),
httpmock.BodyContainsString(`"consistency":"HIGHER_CONSISTENCY"`),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand All @@ -2223,10 +2223,16 @@ func TestOpenFgaClient(t *testing.T) {
},
)

_, err := fgaClient.BatchCheck(context.Background()).Body(requestBody).Options(options).Execute()
checks, err := fgaClient.BatchCheck(context.Background()).Body(requestBody).Options(options).Execute()
if err != nil {
t.Fatalf("%v", err)
}

for _, check := range *checks {
if check.Error != nil {
t.Fatalf("a check failed %v", check.Error)
}
}
})

t.Run("Expand", func(t *testing.T) {
Expand Down

0 comments on commit 498167b

Please sign in to comment.