mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 17:44:59 +09:00
GO-4459: Add router_test.go
This commit is contained in:
parent
286b18797c
commit
891c346836
3 changed files with 79 additions and 13 deletions
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anyproto/anytype-heart/core/anytype/account"
|
||||
"github.com/anyproto/anytype-heart/core/anytype/account/mock_account"
|
||||
"github.com/anyproto/anytype-heart/core/api/util"
|
||||
"github.com/anyproto/anytype-heart/pb"
|
||||
|
@ -19,12 +18,6 @@ import (
|
|||
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
|
||||
)
|
||||
|
||||
type fixture struct {
|
||||
*Server
|
||||
accountService account.Service
|
||||
mwMock *mock_service.MockClientCommandsServer
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) *fixture {
|
||||
mwMock := mock_service.NewMockClientCommandsServer(t)
|
||||
accountService := mock_account.NewMockService(t)
|
||||
|
@ -168,9 +161,7 @@ func TestEnsureAccountInfo(t *testing.T) {
|
|||
expectedInfo := &model.AccountInfo{
|
||||
GatewayUrl: "http://localhost:31006",
|
||||
}
|
||||
fx.accountService.(*mock_account.MockService).
|
||||
On("GetInfo", mock.Anything).
|
||||
Return(expectedInfo, nil).Once()
|
||||
fx.accountService.(*mock_account.MockService).On("GetInfo", mock.Anything).Return(expectedInfo, nil).Once()
|
||||
|
||||
// when
|
||||
middleware := fx.ensureAccountInfo(fx.accountService)
|
||||
|
|
75
core/api/server/router_test.go
Normal file
75
core/api/server/router_test.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gogo/protobuf/types"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anyproto/anytype-heart/core/anytype/account/mock_account"
|
||||
"github.com/anyproto/anytype-heart/pb"
|
||||
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
|
||||
)
|
||||
|
||||
func TestRouter_Unauthenticated(t *testing.T) {
|
||||
t.Run("GET /v1/spaces without auth returns 401", func(t *testing.T) {
|
||||
// given
|
||||
fx := newServerFixture(t)
|
||||
engine := fx.NewRouter(fx.accountService, fx.mwMock)
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/v1/spaces", nil)
|
||||
|
||||
// when
|
||||
engine.ServeHTTP(w, req)
|
||||
|
||||
// then
|
||||
require.Equal(t, http.StatusUnauthorized, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRouter_AuthRoute(t *testing.T) {
|
||||
t.Run("POST /v1/auth/token is accessible without auth", func(t *testing.T) {
|
||||
// given
|
||||
fx := newServerFixture(t)
|
||||
engine := fx.NewRouter(fx.accountService, fx.mwMock)
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/v1/auth/token", nil)
|
||||
|
||||
// when
|
||||
engine.ServeHTTP(w, req)
|
||||
|
||||
// then
|
||||
require.NotEqual(t, http.StatusUnauthorized, w.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRouter_MetadataHeader(t *testing.T) {
|
||||
t.Run("Response includes Anytype-Version header", func(t *testing.T) {
|
||||
// given
|
||||
fx := newServerFixture(t)
|
||||
engine := fx.NewRouter(fx.accountService, fx.mwMock)
|
||||
fx.KeyToToken = map[string]string{"validKey": "dummyToken"}
|
||||
fx.accountService.(*mock_account.MockService).On("GetInfo", mock.Anything).
|
||||
Return(&model.AccountInfo{
|
||||
GatewayUrl: "http://localhost:31006",
|
||||
}, nil).Once()
|
||||
fx.mwMock.On("ObjectSearch", mock.Anything, mock.Anything).
|
||||
Return(&pb.RpcObjectSearchResponse{
|
||||
Records: []*types.Struct{},
|
||||
Error: &pb.RpcObjectSearchResponseError{Code: pb.RpcObjectSearchResponseError_NULL},
|
||||
}, nil).Once()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/v1/spaces", nil)
|
||||
req.Header.Set("Authorization", "Bearer validKey")
|
||||
|
||||
// when
|
||||
engine.ServeHTTP(w, req)
|
||||
|
||||
// then
|
||||
require.Equal(t, "2025-03-17", w.Header().Get("Anytype-Version"))
|
||||
})
|
||||
}
|
|
@ -10,18 +10,18 @@ import (
|
|||
"github.com/anyproto/anytype-heart/pb/service/mock_service"
|
||||
)
|
||||
|
||||
type srvFixture struct {
|
||||
type fixture struct {
|
||||
*Server
|
||||
accountService account.Service
|
||||
mwMock *mock_service.MockClientCommandsServer
|
||||
}
|
||||
|
||||
func newServerFixture(t *testing.T) *srvFixture {
|
||||
func newServerFixture(t *testing.T) *fixture {
|
||||
mwMock := mock_service.NewMockClientCommandsServer(t)
|
||||
accountService := mock_account.NewMockService(t)
|
||||
server := NewServer(accountService, mwMock)
|
||||
|
||||
return &srvFixture{
|
||||
return &fixture{
|
||||
Server: server,
|
||||
accountService: accountService,
|
||||
mwMock: mwMock,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue