dragonfly/manager/handlers/oauth_test.go

324 lines
9.0 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 (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"d7y.io/dragonfly/v2/manager/models"
"d7y.io/dragonfly/v2/manager/service/mocks"
"d7y.io/dragonfly/v2/manager/types"
)
var (
mockOauthReqBody = `
{
"bio": "bio",
"client_id": "2",
"client_secret": "secret",
"name": "google"
}`
mockCreateOauthRequest = types.CreateOauthRequest{
Name: "google",
BIO: "bio",
ClientID: "2",
ClientSecret: "secret",
}
mockUpdateOauthRequest = types.UpdateOauthRequest{
Name: "google",
BIO: "bio",
ClientID: "2",
ClientSecret: "secret",
}
mockOauthModel = &models.Oauth{
BaseModel: mockBaseModel,
Name: "google",
BIO: "bio",
ClientID: "2",
ClientSecret: "secret",
}
)
func mockOauthRouter(h *Handlers) *gin.Engine {
r := gin.Default()
apiv1 := r.Group("/api/v1")
oa := apiv1.Group("/oauth")
oa.POST("", h.CreateOauth)
oa.DELETE(":id", h.DestroyOauth)
oa.PATCH(":id", h.UpdateOauth)
oa.GET(":id", h.GetOauth)
oa.GET("", h.GetOauths)
return r
}
func TestHandlers_CreateOauth(t *testing.T) {
tests := []struct {
name string
req *http.Request
mock func(ms *mocks.MockServiceMockRecorder)
expect func(t *testing.T, w *httptest.ResponseRecorder)
}{
{
name: "unprocessable entity",
req: httptest.NewRequest(http.MethodPost, "/api/v1/oauth", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusUnprocessableEntity, w.Code)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodPost, "/api/v1/oauth", strings.NewReader(mockOauthReqBody)),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.CreateOauth(gomock.Any(), gomock.Eq(mockCreateOauthRequest)).Return(mockOauthModel, nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
oauth := models.Oauth{}
err := json.Unmarshal(w.Body.Bytes(), &oauth)
assert.NoError(err)
assert.Equal(mockOauthModel, &oauth)
},
},
}
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 := mockOauthRouter(h)
tc.mock(svc.EXPECT())
mockRouter.ServeHTTP(w, tc.req)
tc.expect(t, w)
})
}
}
func TestHandlers_DestroyOauth(t *testing.T) {
tests := []struct {
name string
req *http.Request
mock func(ms *mocks.MockServiceMockRecorder)
expect func(t *testing.T, w *httptest.ResponseRecorder)
}{
{
name: "unprocessable entity",
req: httptest.NewRequest(http.MethodDelete, "/api/v1/oauth/test", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusUnprocessableEntity, w.Code)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodDelete, "/api/v1/oauth/2", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.DestroyOauth(gomock.Any(), gomock.Eq(uint(2))).Return(nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
},
},
}
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 := mockOauthRouter(h)
tc.mock(svc.EXPECT())
mockRouter.ServeHTTP(w, tc.req)
tc.expect(t, w)
})
}
}
func TestHandlers_UpdateOauth(t *testing.T) {
tests := []struct {
name string
req *http.Request
mock func(ms *mocks.MockServiceMockRecorder)
expect func(t *testing.T, w *httptest.ResponseRecorder)
}{
{
name: "unprocessable entity caused by uri",
req: httptest.NewRequest(http.MethodPatch, "/api/v1/oauth/test", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusUnprocessableEntity, w.Code)
},
},
{
name: "unprocessable entity caused by body",
req: httptest.NewRequest(http.MethodPatch, "/api/v1/oauth/2", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusUnprocessableEntity, w.Code)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodPatch, "/api/v1/oauth/2", strings.NewReader(mockOauthReqBody)),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.UpdateOauth(gomock.Any(), gomock.Eq(uint(2)), gomock.Eq(mockUpdateOauthRequest)).Return(mockOauthModel, nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
oauth := models.Oauth{}
err := json.Unmarshal(w.Body.Bytes(), &oauth)
assert.NoError(err)
assert.Equal(mockOauthModel, &oauth)
},
},
}
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 := mockOauthRouter(h)
tc.mock(svc.EXPECT())
mockRouter.ServeHTTP(w, tc.req)
tc.expect(t, w)
})
}
}
func TestHandlers_GetOauth(t *testing.T) {
tests := []struct {
name string
req *http.Request
mock func(ms *mocks.MockServiceMockRecorder)
expect func(t *testing.T, w *httptest.ResponseRecorder)
}{
{
name: "unprocessable entity",
req: httptest.NewRequest(http.MethodGet, "/api/v1/oauth/test", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusUnprocessableEntity, w.Code)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodGet, "/api/v1/oauth/2", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.GetOauth(gomock.Any(), gomock.Eq(uint(2))).Return(mockOauthModel, nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
oauth := models.Oauth{}
err := json.Unmarshal(w.Body.Bytes(), &oauth)
assert.NoError(err)
assert.Equal(mockOauthModel, &oauth)
},
},
}
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 := mockOauthRouter(h)
tc.mock(svc.EXPECT())
mockRouter.ServeHTTP(w, tc.req)
tc.expect(t, w)
})
}
}
func TestHandlers_GetOauths(t *testing.T) {
tests := []struct {
name string
req *http.Request
mock func(ms *mocks.MockServiceMockRecorder)
expect func(t *testing.T, w *httptest.ResponseRecorder)
}{
{
name: "unprocessable entity",
req: httptest.NewRequest(http.MethodGet, "/api/v1/oauth?page=-1", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusUnprocessableEntity, w.Code)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodGet, "/api/v1/oauth", nil),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.GetOauths(gomock.Any(), gomock.Eq(types.GetOauthsQuery{
Name: "",
Page: 1,
PerPage: 10,
})).Return([]models.Oauth{*mockOauthModel}, int64(1), nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
oauth := models.Oauth{}
err := json.Unmarshal(w.Body.Bytes()[1:w.Body.Len()-1], &oauth)
assert.NoError(err)
assert.Equal(mockOauthModel, &oauth)
},
},
}
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 := mockOauthRouter(h)
tc.mock(svc.EXPECT())
mockRouter.ServeHTTP(w, tc.req)
tc.expect(t, w)
})
}
}