Skip to content

Commit

Permalink
feat: add test for admin.DescribeCluster
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaxuyang committed Jun 3, 2024
1 parent a93871a commit d8a8849
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ package accesscontrolled
import (
"context"
"errors"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/service/frontend/admin"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -92,3 +94,54 @@ func TestIsAuthorized(t *testing.T) {
})
}
}

func TestDescribeCluster(t *testing.T) {
someErr := errors.New("some random err")
testCases := []struct {
name string
mockSetup func(*authorization.MockAuthorizer, *admin.MockHandler)
wantErr error
}{
{
name: "Success case",
mockSetup: func(authorizer *authorization.MockAuthorizer, adminHandler *admin.MockHandler) {
authorizer.EXPECT().Authorize(gomock.Any(), gomock.Any()).Return(authorization.Result{Decision: authorization.DecisionAllow}, nil)
adminHandler.EXPECT().DescribeCluster(gomock.Any()).Return(&types.DescribeClusterResponse{}, nil)
},
wantErr: nil,
},
{
name: "Error case - unauthorized",
mockSetup: func(authorizer *authorization.MockAuthorizer, adminHandler *admin.MockHandler) {
authorizer.EXPECT().Authorize(gomock.Any(), gomock.Any()).Return(authorization.Result{Decision: authorization.DecisionDeny}, nil)
},
wantErr: errUnauthorized,
},
{
name: "Error case - authorization error",
mockSetup: func(authorizer *authorization.MockAuthorizer, adminHandler *admin.MockHandler) {
authorizer.EXPECT().Authorize(gomock.Any(), gomock.Any()).Return(authorization.Result{}, someErr)
},
wantErr: someErr,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
controller := gomock.NewController(t)

mockAuthorizer := authorization.NewMockAuthorizer(controller)
mockAdminHandler := admin.NewMockHandler(controller)
tc.mockSetup(mockAuthorizer, mockAdminHandler)

handler := &adminHandler{authorizer: mockAuthorizer, handler: mockAdminHandler}
_, err := handler.DescribeCluster(context.Background())
if tc.wantErr != nil {
assert.Error(t, err)
assert.ErrorIs(t, err, tc.wantErr)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit d8a8849

Please sign in to comment.