diff --git a/services/graph/pkg/service/v0/api_drives_drive_item_test.go b/services/graph/pkg/service/v0/api_drives_drive_item_test.go index f9c8820e58..132f7b954e 100644 --- a/services/graph/pkg/service/v0/api_drives_drive_item_test.go +++ b/services/graph/pkg/service/v0/api_drives_drive_item_test.go @@ -23,7 +23,6 @@ import ( ) var _ = Describe("DrivesDriveItemApi", func() { - var ( mockProvider *mocks.DrivesDriveItemProvider httpAPI svc.DrivesDriveItemApi @@ -44,23 +43,68 @@ var _ = Describe("DrivesDriveItemApi", func() { rCTX.URLParams.Add("itemID", "a0ca6a90-a365-4782-871e-d44447bbc668$a0ca6a90-a365-4782-871e-d44447bbc668!a0ca6a90-a365-4782-871e-d44447bbc668") }) + checkDriveIDAndItemIDValidation := func(handler http.HandlerFunc) { + rCTX.URLParams.Add("driveID", "1$2") + rCTX.URLParams.Add("itemID", "3$4!5") + + responseRecorder := httptest.NewRecorder() + request := httptest.NewRequest(http.MethodPost, "/", nil). + WithContext( + context.WithValue(context.Background(), chi.RouteCtxKey, rCTX), + ) + + handler(responseRecorder, request) + + Expect(responseRecorder.Code).To(Equal(http.StatusUnprocessableEntity)) + + jsonData := gjson.Get(responseRecorder.Body.String(), "error") + Expect(jsonData.Get("message").String()).To(Equal("invalid driveID or itemID")) + } + Describe("CreateDriveItem", func() { It("validates the driveID and itemID url param", func() { - rCTX.URLParams.Add("driveID", "1$2") - rCTX.URLParams.Add("itemID", "3$4!5") + checkDriveIDAndItemIDValidation(httpAPI.DeleteDriveItem) + }) + It("uses the UnmountShare provider implementation", func() { responseRecorder := httptest.NewRecorder() - request := httptest.NewRequest(http.MethodPost, "/", nil). + + request := httptest.NewRequest(http.MethodDelete, "/", nil). WithContext( context.WithValue(context.Background(), chi.RouteCtxKey, rCTX), ) - httpAPI.CreateDriveItem(responseRecorder, request) + onUnmountShare := mockProvider.On("UnmountShare", mock.Anything, mock.Anything) + onUnmountShare. + Return(func(ctx context.Context, resourceID storageprovider.ResourceId) error { + return errors.New("any") + }).Once() - Expect(responseRecorder.Code).To(Equal(http.StatusUnprocessableEntity)) + httpAPI.DeleteDriveItem(responseRecorder, request) + + Expect(responseRecorder.Code).To(Equal(http.StatusFailedDependency)) jsonData := gjson.Get(responseRecorder.Body.String(), "error") - Expect(jsonData.Get("message").String()).To(Equal("invalid driveID or itemID")) + Expect(jsonData.Get("message").String()).To(Equal("unmounting share failed")) + + // happy path + responseRecorder = httptest.NewRecorder() + + onUnmountShare. + Return(func(ctx context.Context, resourceID storageprovider.ResourceId) error { + Expect(storagespace.FormatResourceID(resourceID)).To(Equal("a0ca6a90-a365-4782-871e-d44447bbc668$a0ca6a90-a365-4782-871e-d44447bbc668!a0ca6a90-a365-4782-871e-d44447bbc668")) + return nil + }).Once() + + httpAPI.DeleteDriveItem(responseRecorder, request) + + Expect(responseRecorder.Code).To(Equal(http.StatusOK)) + }) + }) + + Describe("CreateDriveItem", func() { + It("validates the driveID and itemID url param", func() { + checkDriveIDAndItemIDValidation(httpAPI.CreateDriveItem) }) It("checks if the idemID and driveID is in share jail", func() { @@ -116,7 +160,7 @@ var _ = Describe("DrivesDriveItemApi", func() { Expect(jsonData.Get("message").String()).To(Equal("invalid remote item id")) }) - It("uses the provider implementation", func() { + It("uses the MountShare provider implementation", func() { driveItemName := "a name" remoteItemID := "d66d28d8-3558-4f0f-ba2a-34a7185b806d$831997cf-a531-491b-ae72-9037739f04e9!c131a84c-7506-46b4-8e5e-60c56382da3b" driveItem := libregraph.DriveItem{ @@ -150,7 +194,6 @@ var _ = Describe("DrivesDriveItemApi", func() { Expect(jsonData.Get("message").String()).To(Equal("mounting share failed")) // happy path - responseRecorder = httptest.NewRecorder() request = httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer(driveItemJson)). diff --git a/services/graph/pkg/service/v0/utils.go b/services/graph/pkg/service/v0/utils.go index b8fd3dd1c3..0894cf18b5 100644 --- a/services/graph/pkg/service/v0/utils.go +++ b/services/graph/pkg/service/v0/utils.go @@ -75,7 +75,7 @@ func (g Graph) GetGatewayClient(w http.ResponseWriter, r *http.Request) (gateway return gatewayClient, true } -// IsShareJail returns true if the resourceID is a share jail. +// IsShareJail returns true if given id is a share jail id. func IsShareJail(id storageprovider.ResourceId) bool { return id.GetStorageId() == utils.ShareStorageProviderID && id.GetSpaceId() == utils.ShareStorageSpaceID } diff --git a/services/graph/pkg/service/v0/utils_test.go b/services/graph/pkg/service/v0/utils_test.go index c1cfa722b7..24f97497e9 100644 --- a/services/graph/pkg/service/v0/utils_test.go +++ b/services/graph/pkg/service/v0/utils_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" + "github.com/cs3org/reva/v2/pkg/utils" "github.com/go-chi/chi/v5" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -74,4 +75,33 @@ var _ = Describe("Utils", func() { SpaceId: "3", }, false), ) + + DescribeTable("IsShareJail", + func(resourceID provider.ResourceId, isShareJail bool) { + Expect(service.IsShareJail(resourceID)).To(Equal(isShareJail)) + }, + Entry("valid: share jail", provider.ResourceId{ + StorageId: utils.ShareStorageProviderID, + SpaceId: utils.ShareStorageSpaceID, + }, true), + Entry("invalid: empty storageId", provider.ResourceId{ + SpaceId: utils.ShareStorageSpaceID, + }, false), + Entry("invalid: empty spaceId", provider.ResourceId{ + StorageId: utils.ShareStorageProviderID, + }, false), + Entry("invalid: empty storageId and spaceId", provider.ResourceId{}, false), + Entry("invalid: non share jail storageId", provider.ResourceId{ + StorageId: "123", + SpaceId: utils.ShareStorageSpaceID, + }, false), + Entry("invalid: non share jail spaceId", provider.ResourceId{ + StorageId: utils.ShareStorageProviderID, + SpaceId: "123", + }, false), + Entry("invalid: non share jail storageID and spaceId", provider.ResourceId{ + StorageId: "123", + SpaceId: "123", + }, false), + ) })