add mockery for minimal graph testing
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
@@ -21,12 +21,7 @@ func (g Graph) GetRootDriveChildren(w http.ResponseWriter, r *http.Request) {
|
||||
g.logger.Info().Msg("Calling GetRootDriveChildren")
|
||||
ctx := r.Context()
|
||||
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
g.logger.Error().Err(err).Msg("could not get client")
|
||||
errorcode.ServiceNotAvailable.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
client := g.GetClient()
|
||||
|
||||
res, err := client.GetHome(ctx, &storageprovider.GetHomeRequest{})
|
||||
switch {
|
||||
@@ -82,11 +77,7 @@ func (g Graph) GetRootDriveChildren(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (g Graph) getDriveItem(ctx context.Context, root *storageprovider.ResourceId, relativePath string) (*libregraph.DriveItem, error) {
|
||||
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
g.logger.Error().Err(err).Msg("error creating grpc client")
|
||||
return nil, err
|
||||
}
|
||||
client := g.GetClient()
|
||||
|
||||
ref := &storageprovider.Reference{
|
||||
ResourceId: root,
|
||||
|
||||
@@ -47,12 +47,7 @@ func (g Graph) GetDrives(w http.ResponseWriter, r *http.Request) {
|
||||
g.logger.Info().Msg("Calling GetDrives")
|
||||
ctx := r.Context()
|
||||
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
g.logger.Err(err).Msg("error getting grpc client")
|
||||
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
client := g.GetClient()
|
||||
|
||||
permissions := make(map[string]struct{}, 1)
|
||||
s := sproto.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient)
|
||||
@@ -138,11 +133,7 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
client := g.GetClient()
|
||||
drive := libregraph.Drive{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&drive); err != nil {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, "invalid schema definition")
|
||||
@@ -233,11 +224,7 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
client := g.GetClient()
|
||||
|
||||
updateSpaceRequest := &storageprovider.UpdateStorageSpaceRequest{
|
||||
// Prepare the object to apply the diff from. The properties on StorageSpace will overwrite
|
||||
@@ -410,11 +397,7 @@ func cs3StorageSpaceToDrive(baseURL *url.URL, space *storageprovider.StorageSpac
|
||||
}
|
||||
|
||||
func (g Graph) getDriveQuota(ctx context.Context, space *storageprovider.StorageSpace) (*libregraph.Quota, error) {
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
g.logger.Error().Err(err).Msg("error creating grpc client")
|
||||
return nil, err
|
||||
}
|
||||
client := g.GetClient()
|
||||
|
||||
req := &gateway.GetQuotaRequest{
|
||||
Ref: &storageprovider.Reference{
|
||||
@@ -489,10 +472,8 @@ func (g Graph) getExtendedSpaceProperties(ctx context.Context, space *storagepro
|
||||
}
|
||||
}
|
||||
|
||||
client, err := g.GetClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := g.GetClient()
|
||||
|
||||
dlReq := &storageprovider.InitiateFileDownloadRequest{
|
||||
Ref: &storageprovider.Reference{
|
||||
ResourceId: &storageprovider.ResourceId{
|
||||
|
||||
@@ -1,23 +1,63 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/ReneKroon/ttlcache/v2"
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/owncloud/ocis/graph/pkg/config"
|
||||
"github.com/owncloud/ocis/graph/pkg/identity"
|
||||
"github.com/owncloud/ocis/ocis-pkg/log"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
//go:generate make generate
|
||||
|
||||
// GatewayClient is the subset of the gateway.GatewayAPIClient that's being uses to interact with the gateway
|
||||
type GatewayClient interface {
|
||||
//gateway.GatewayAPIClient
|
||||
|
||||
// Returns the home path for the given authenticated user.
|
||||
// When a user has access to multiple storage providers, one of them is the home.
|
||||
GetHome(ctx context.Context, in *provider.GetHomeRequest, opts ...grpc.CallOption) (*provider.GetHomeResponse, error)
|
||||
// Returns a list of resource information
|
||||
// for the provided reference.
|
||||
// MUST return CODE_NOT_FOUND if the reference does not exists.
|
||||
ListContainer(ctx context.Context, in *provider.ListContainerRequest, opts ...grpc.CallOption) (*provider.ListContainerResponse, error)
|
||||
// Returns the resource information at the provided reference.
|
||||
// MUST return CODE_NOT_FOUND if the reference does not exist.
|
||||
Stat(ctx context.Context, in *provider.StatRequest, opts ...grpc.CallOption) (*provider.StatResponse, error)
|
||||
// Initiates the download of a file using an
|
||||
// out-of-band data transfer mechanism.
|
||||
InitiateFileDownload(ctx context.Context, in *provider.InitiateFileDownloadRequest, opts ...grpc.CallOption) (*gateway.InitiateFileDownloadResponse, error)
|
||||
// Creates a storage space.
|
||||
CreateStorageSpace(ctx context.Context, in *provider.CreateStorageSpaceRequest, opts ...grpc.CallOption) (*provider.CreateStorageSpaceResponse, error)
|
||||
// Lists storage spaces.
|
||||
ListStorageSpaces(ctx context.Context, in *provider.ListStorageSpacesRequest, opts ...grpc.CallOption) (*provider.ListStorageSpacesResponse, error)
|
||||
// Updates a storage space.
|
||||
UpdateStorageSpace(ctx context.Context, in *provider.UpdateStorageSpaceRequest, opts ...grpc.CallOption) (*provider.UpdateStorageSpaceResponse, error)
|
||||
// Deletes a storage space.
|
||||
DeleteStorageSpace(ctx context.Context, in *provider.DeleteStorageSpaceRequest, opts ...grpc.CallOption) (*provider.DeleteStorageSpaceResponse, error)
|
||||
// Returns the quota available under the provided
|
||||
// reference.
|
||||
// MUST return CODE_NOT_FOUND if the reference does not exist
|
||||
// MUST return CODE_RESOURCE_EXHAUSTED on exceeded quota limits.
|
||||
GetQuota(ctx context.Context, in *gateway.GetQuotaRequest, opts ...grpc.CallOption) (*provider.GetQuotaResponse, error)
|
||||
}
|
||||
|
||||
// GetGatewayServiceClientFunc is a callback used to pass in a mock during testing
|
||||
type GetGatewayServiceClientFunc func() (GatewayClient, error)
|
||||
|
||||
// Graph defines implements the business logic for Service.
|
||||
type Graph struct {
|
||||
config *config.Config
|
||||
mux *chi.Mux
|
||||
logger *log.Logger
|
||||
identityBackend identity.Backend
|
||||
client GatewayClient
|
||||
spacePropertiesCache *ttlcache.Cache
|
||||
}
|
||||
|
||||
@@ -27,8 +67,8 @@ func (g Graph) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// GetClient returns a gateway client to talk to reva
|
||||
func (g Graph) GetClient() (gateway.GatewayAPIClient, error) {
|
||||
return pool.GetGatewayServiceClient(g.config.Reva.Address)
|
||||
func (g Graph) GetClient() GatewayClient {
|
||||
return g.client
|
||||
}
|
||||
|
||||
type listResponse struct {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package svc_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestGraph(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Graph Suite")
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package svc_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
"github.com/cs3org/reva/pkg/rgrpc/status"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/owncloud/ocis/graph/mocks"
|
||||
"github.com/owncloud/ocis/graph/pkg/config"
|
||||
service "github.com/owncloud/ocis/graph/pkg/service/v0"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
var _ = Describe("Graph", func() {
|
||||
var (
|
||||
svc service.Service
|
||||
client *mocks.GatewayClient
|
||||
ctx context.Context
|
||||
)
|
||||
|
||||
JustBeforeEach(func() {
|
||||
ctx = context.Background()
|
||||
client = &mocks.GatewayClient{}
|
||||
svc = service.NewService(
|
||||
service.Config(config.DefaultConfig()),
|
||||
service.GatewayServiceClient(client),
|
||||
)
|
||||
})
|
||||
|
||||
Describe("NewService", func() {
|
||||
It("returns a service", func() {
|
||||
Expect(svc).ToNot(BeNil())
|
||||
})
|
||||
})
|
||||
Describe("drive", func() {
|
||||
It("can list an empty list of spaces", func() {
|
||||
client.On("ListStorageSpaces", mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{
|
||||
Status: status.NewOK(ctx),
|
||||
StorageSpaces: []*provider.StorageSpace{},
|
||||
}, nil)
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me/drives", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
svc.GetDrives(rr, r)
|
||||
Expect(rr.Code).To(Equal(http.StatusOK))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -53,3 +53,8 @@ func (i instrument) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (i instrument) PatchUser(w http.ResponseWriter, r *http.Request) {
|
||||
i.next.PatchUser(w, r)
|
||||
}
|
||||
|
||||
// GetDrives implements the Service interface.
|
||||
func (i instrument) GetDrives(w http.ResponseWriter, r *http.Request) {
|
||||
i.next.GetDrives(w, r)
|
||||
}
|
||||
|
||||
@@ -53,3 +53,8 @@ func (l logging) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (l logging) PatchUser(w http.ResponseWriter, r *http.Request) {
|
||||
l.next.PatchUser(w, r)
|
||||
}
|
||||
|
||||
// GetDrives implements the Service interface.
|
||||
func (l logging) GetDrives(w http.ResponseWriter, r *http.Request) {
|
||||
l.next.GetDrives(w, r)
|
||||
}
|
||||
|
||||
@@ -12,9 +12,10 @@ type Option func(o *Options)
|
||||
|
||||
// Options defines the available options for this package.
|
||||
type Options struct {
|
||||
Logger log.Logger
|
||||
Config *config.Config
|
||||
Middleware []func(http.Handler) http.Handler
|
||||
Logger log.Logger
|
||||
Config *config.Config
|
||||
Middleware []func(http.Handler) http.Handler
|
||||
GatewayServiceClient GatewayClient
|
||||
}
|
||||
|
||||
// newOptions initializes the available default options.
|
||||
@@ -48,3 +49,10 @@ func Middleware(val ...func(http.Handler) http.Handler) Option {
|
||||
o.Middleware = val
|
||||
}
|
||||
}
|
||||
|
||||
// GatewayServiceClient provides a function to set the middleware option.
|
||||
func GatewayServiceClient(val GatewayClient) Option {
|
||||
return func(o *Options) {
|
||||
o.GatewayServiceClient = val
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/ReneKroon/ttlcache/v2"
|
||||
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
|
||||
@@ -22,6 +23,8 @@ type Service interface {
|
||||
PostUser(http.ResponseWriter, *http.Request)
|
||||
DeleteUser(http.ResponseWriter, *http.Request)
|
||||
PatchUser(http.ResponseWriter, *http.Request)
|
||||
|
||||
GetDrives(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// NewService returns a service implementation for Service.
|
||||
@@ -61,6 +64,16 @@ func NewService(opts ...Option) Service {
|
||||
identityBackend: backend,
|
||||
spacePropertiesCache: ttlcache.NewCache(),
|
||||
}
|
||||
if options.GatewayServiceClient == nil {
|
||||
var err error
|
||||
svc.client, err = pool.GetGatewayServiceClient(options.Config.Reva.Address)
|
||||
if err != nil {
|
||||
options.Logger.Error().Err(err).Msg("Could not get gateway client")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
svc.client = options.GatewayServiceClient
|
||||
}
|
||||
|
||||
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
|
||||
r.Use(middleware.StripSlashes)
|
||||
|
||||
@@ -49,3 +49,8 @@ func (t tracing) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
func (t tracing) PatchUser(w http.ResponseWriter, r *http.Request) {
|
||||
t.next.PatchUser(w, r)
|
||||
}
|
||||
|
||||
// GetDrives implements the Service interface.
|
||||
func (t tracing) GetDrives(w http.ResponseWriter, r *http.Request) {
|
||||
t.next.GetDrives(w, r)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user