79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
/*
|
|
* Copyright 2024 The Dragonfly Authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/mock/gomock"
|
|
|
|
"d7y.io/dragonfly/v2/manager/permission/rbac"
|
|
"d7y.io/dragonfly/v2/manager/service/mocks"
|
|
)
|
|
|
|
var (
|
|
mockRbacPermission = []rbac.Permission{{Object: "object", Action: "action"}}
|
|
)
|
|
|
|
func mockPermissionRouter(h *Handlers) *gin.Engine {
|
|
r := gin.Default()
|
|
apiv1 := r.Group("/api/v1")
|
|
pm := apiv1.Group("/permissions")
|
|
pm.GET("", h.GetPermissions(r))
|
|
return r
|
|
}
|
|
|
|
func TestHandlers_GetPermissions(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
req *http.Request
|
|
mock func(ms *mocks.MockServiceMockRecorder)
|
|
expect func(t *testing.T, w *httptest.ResponseRecorder)
|
|
}{
|
|
{
|
|
name: "success",
|
|
req: httptest.NewRequest(http.MethodGet, "/api/v1/permissions", nil),
|
|
mock: func(ms *mocks.MockServiceMockRecorder) {
|
|
ms.GetPermissions(gomock.Any(), gomock.Any()).Return(mockRbacPermission).Times(1)
|
|
},
|
|
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
|
|
assert := assert.New(t)
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
assert.Equal(w.Body.String(), `[{"object":"object","action":"action"}]`)
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctl := gomock.NewController(t)
|
|
defer ctl.Finish()
|
|
svc := mocks.NewMockService(ctl)
|
|
w := httptest.NewRecorder()
|
|
h := New(svc)
|
|
mockRouter := mockPermissionRouter(h)
|
|
|
|
tc.mock(svc.EXPECT())
|
|
mockRouter.ServeHTTP(w, tc.req)
|
|
tc.expect(t, w)
|
|
})
|
|
}
|
|
}
|