test: improve graph share accept/decline test coverage

This commit is contained in:
Florian Schade
2024-01-31 13:05:32 +01:00
committed by Ralf Haferkamp
parent 7384297728
commit 0bd97c9178
3 changed files with 83 additions and 10 deletions

View File

@@ -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)).

View File

@@ -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
}

View File

@@ -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),
)
})