-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adding additional unit tests for query
- Loading branch information
Showing
8 changed files
with
614 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package query | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
mock_client "github.com/openfga/cli/mocks" | ||
openfga "github.com/openfga/go-sdk" | ||
"github.com/openfga/go-sdk/client" | ||
) | ||
|
||
var errMockCheck = errors.New("mock error") | ||
|
||
func TestCheckWithError(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
mockFgaClient := mock_client.NewMockSdkClient(mockCtrl) | ||
|
||
mockExecute := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl) | ||
|
||
var expectedResponse client.ClientCheckResponse | ||
|
||
mockExecute.EXPECT().Execute().Return(&expectedResponse, errMockCheck) | ||
|
||
mockRequest := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl) | ||
options := client.ClientCheckOptions{} | ||
mockRequest.EXPECT().Options(options).Return(mockExecute) | ||
|
||
mockBody := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl) | ||
|
||
body := client.ClientCheckRequest{ | ||
User: "user:foo", | ||
Relation: "writer", | ||
Object: "doc:doc1", | ||
} | ||
mockBody.EXPECT().Body(body).Return(mockRequest) | ||
|
||
mockFgaClient.EXPECT().Check(context.Background()).Return(mockBody) | ||
|
||
_, err := check(mockFgaClient, "user:foo", "writer", "doc:doc1") | ||
if err == nil { | ||
t.Error("Expect error but there is none") | ||
} | ||
} | ||
|
||
func TestCheckWithNoError(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
mockFgaClient := mock_client.NewMockSdkClient(mockCtrl) | ||
|
||
mockExecute := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl) | ||
|
||
expectedResponse := client.ClientCheckResponse{ | ||
CheckResponse: openfga.CheckResponse{ | ||
Allowed: openfga.PtrBool(true), | ||
}, | ||
HttpResponse: nil, | ||
} | ||
|
||
mockExecute.EXPECT().Execute().Return(&expectedResponse, nil) | ||
|
||
mockRequest := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl) | ||
options := client.ClientCheckOptions{} | ||
mockRequest.EXPECT().Options(options).Return(mockExecute) | ||
|
||
mockBody := mock_client.NewMockSdkClientCheckRequestInterface(mockCtrl) | ||
|
||
body := client.ClientCheckRequest{ | ||
User: "user:foo", | ||
Relation: "writer", | ||
Object: "doc:doc1", | ||
} | ||
mockBody.EXPECT().Body(body).Return(mockRequest) | ||
|
||
mockFgaClient.EXPECT().Check(context.Background()).Return(mockBody) | ||
|
||
output, err := check(mockFgaClient, "user:foo", "writer", "doc:doc1") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
expectedOutput := `{"allowed":true}` | ||
if output != expectedOutput { | ||
t.Errorf("Expected output %v actual %v", expectedOutput, output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package query | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
mock_client "github.com/openfga/cli/mocks" | ||
"github.com/openfga/go-sdk/client" | ||
) | ||
|
||
var errMockExpand = errors.New("mock error") | ||
|
||
func TestExpandWithError(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
mockFgaClient := mock_client.NewMockSdkClient(mockCtrl) | ||
|
||
mockExecute := mock_client.NewMockSdkClientExpandRequestInterface(mockCtrl) | ||
|
||
var expectedResponse client.ClientExpandResponse | ||
|
||
mockExecute.EXPECT().Execute().Return(&expectedResponse, errMockExpand) | ||
|
||
mockBody := mock_client.NewMockSdkClientExpandRequestInterface(mockCtrl) | ||
|
||
body := client.ClientExpandRequest{ | ||
Relation: "writer", | ||
Object: "doc:doc1", | ||
} | ||
mockBody.EXPECT().Body(body).Return(mockExecute) | ||
|
||
mockFgaClient.EXPECT().Expand(context.Background()).Return(mockBody) | ||
|
||
_, err := expand(mockFgaClient, "writer", "doc:doc1") | ||
if err == nil { | ||
t.Error("Expect error but there is none") | ||
} | ||
} | ||
|
||
func TestExpandWithNoError(t *testing.T) { | ||
t.Parallel() | ||
|
||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
mockFgaClient := mock_client.NewMockSdkClient(mockCtrl) | ||
|
||
mockExecute := mock_client.NewMockSdkClientExpandRequestInterface(mockCtrl) | ||
|
||
expandResponseTxt := `{"tree":{"root":{"name":"document:roadmap#viewer","union":{"nodes":[{"name": "document:roadmap#viewer","leaf":{"users":{"users":["user:81684243-9356-4421-8fbf-a4f8d36aa31b"]}}}]}}}}` //nolint:all | ||
|
||
expectedResponse := client.ClientExpandResponse{} | ||
if err := json.Unmarshal([]byte(expandResponseTxt), &expectedResponse); err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
|
||
mockExecute.EXPECT().Execute().Return(&expectedResponse, nil) | ||
|
||
mockBody := mock_client.NewMockSdkClientExpandRequestInterface(mockCtrl) | ||
|
||
body := client.ClientExpandRequest{ | ||
Relation: "writer", | ||
Object: "doc:doc1", | ||
} | ||
mockBody.EXPECT().Body(body).Return(mockExecute) | ||
|
||
mockFgaClient.EXPECT().Expand(context.Background()).Return(mockBody) | ||
|
||
output, err := expand(mockFgaClient, "writer", "doc:doc1") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
outputResponse := client.ClientExpandResponse{} | ||
if err := json.Unmarshal([]byte(output), &outputResponse); err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
if !(reflect.DeepEqual(outputResponse, expectedResponse)) { | ||
t.Errorf("Expect output response %v actual response %v", expandResponseTxt, output) | ||
} | ||
} |
Oops, something went wrong.