From d8a8849a19850c5b66ec5507ea8f5066cc059413 Mon Sep 17 00:00:00 2001 From: jiaxuyang Date: Mon, 3 Jun 2024 12:55:47 +0800 Subject: [PATCH] feat: add test for admin.DescribeCluster --- .../access_controlled_test.go | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/service/frontend/wrappers/accesscontrolled/access_controlled_test.go b/service/frontend/wrappers/accesscontrolled/access_controlled_test.go index 7ee8c402938..dddc2fd1618 100644 --- a/service/frontend/wrappers/accesscontrolled/access_controlled_test.go +++ b/service/frontend/wrappers/accesscontrolled/access_controlled_test.go @@ -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" @@ -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) + } + }) + } +}