Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix creating the drive item #8817

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/fix-graph-create-drive-item.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: Fix creating the drive item

We fixed the issue when creating a drive item with random item id was allowed

https://github.com/owncloud/ocis/pull/8817
https://github.com/owncloud/ocis/issues/8724
14 changes: 10 additions & 4 deletions services/graph/pkg/service/v0/api_drives_drive_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ func (s DrivesDriveItemService) MountShare(ctx context.Context, resourceID stora
if filepath.IsAbs(name) {
return libregraph.DriveItem{}, errorcode.New(errorcode.InvalidRequest, "name cannot be an absolute path")
}
name = filepath.Clean(name)
if name != "" {
name = filepath.Clean(name)
}

gatewayClient, err := s.gatewaySelector.Next()
if err != nil {
Expand Down Expand Up @@ -158,13 +160,17 @@ func (s DrivesDriveItemService) MountShare(ctx context.Context, resourceID stora
if err != nil {
return libregraph.DriveItem{}, err
}
if len(receivedSharesResponse.GetShares()) == 0 {
return libregraph.DriveItem{}, errorcode.New(errorcode.InvalidRequest, "invalid itemID")
}

var errs []error

var acceptedShares []*collaboration.ReceivedShare

// try to accept all the received shares for this resource. So that the stat is in sync across all
// shares

for _, receivedShare := range receivedSharesResponse.GetShares() {
updateMask := &fieldmaskpb.FieldMask{Paths: []string{_fieldMaskPathState}}
receivedShare.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
Expand Down Expand Up @@ -284,7 +290,7 @@ func (api DrivesDriveItemApi) CreateDriveItem(w http.ResponseWriter, r *http.Req
if err := StrictJSONUnmarshal(r.Body, &requestDriveItem); err != nil {
msg := "invalid request body"
api.logger.Debug().Err(err).Msg(msg)
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, msg)
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, msg)
return
}

Expand All @@ -293,7 +299,7 @@ func (api DrivesDriveItemApi) CreateDriveItem(w http.ResponseWriter, r *http.Req
if err != nil {
msg := "invalid remote item id"
api.logger.Debug().Err(err).Msg(msg)
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, msg)
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, msg)
return
}

Expand All @@ -302,7 +308,7 @@ func (api DrivesDriveItemApi) CreateDriveItem(w http.ResponseWriter, r *http.Req
if err != nil {
msg := "mounting share failed"
api.logger.Debug().Err(err).Msg(msg)
errorcode.InvalidRequest.Render(w, r, http.StatusFailedDependency, msg)
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, msg)
return
}

Expand Down
48 changes: 44 additions & 4 deletions services/graph/pkg/service/v0/api_drives_drive_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var _ = Describe("DrivesDriveItemService", func() {
OpaqueId: "2",
SpaceId: "3",
}
expectedShareID := collaborationv1beta1.ShareId{
OpaqueId: "1:2:3",
}
gatewayClient.
On("ListReceivedShares", mock.Anything, mock.Anything, mock.Anything).
Return(func(ctx context.Context, in *collaborationv1beta1.ListReceivedSharesRequest, opts ...grpc.CallOption) (*collaborationv1beta1.ListReceivedSharesResponse, error) {
Expand All @@ -106,7 +109,44 @@ var _ = Describe("DrivesDriveItemService", func() {
Expect(resourceIDs).To(HaveLen(1))
Expect(resourceIDs[0]).To(Equal(&expectedResourceID))

return nil, nil
return &collaborationv1beta1.ListReceivedSharesResponse{
Shares: []*collaborationv1beta1.ReceivedShare{
{
State: collaborationv1beta1.ShareState_SHARE_STATE_PENDING,
Share: &collaborationv1beta1.Share{
Id: &expectedShareID,
},
},
},
}, nil
})
gatewayClient.
On("UpdateReceivedShare", mock.Anything, mock.Anything, mock.Anything).
Return(func(ctx context.Context, in *collaborationv1beta1.UpdateReceivedShareRequest, opts ...grpc.CallOption) (*collaborationv1beta1.UpdateReceivedShareResponse, error) {
Expect(in.GetUpdateMask().GetPaths()).To(Equal([]string{"state"}))
Expect(in.GetShare().GetState()).To(Equal(collaborationv1beta1.ShareState_SHARE_STATE_ACCEPTED))
Expect(in.GetShare().GetShare().GetId().GetOpaqueId()).To(Equal(expectedShareID.GetOpaqueId()))
return &collaborationv1beta1.UpdateReceivedShareResponse{
Status: status.NewOK(ctx),
Share: &collaborationv1beta1.ReceivedShare{
State: collaborationv1beta1.ShareState_SHARE_STATE_ACCEPTED,
Share: &collaborationv1beta1.Share{
Id: &expectedShareID,
ResourceId: &expectedResourceID,
},
},
}, nil
})
gatewayClient.
On("Stat", mock.Anything, mock.Anything, mock.Anything).
Return(func(ctx context.Context, in *storageprovider.StatRequest, opts ...grpc.CallOption) (*storageprovider.StatResponse, error) {
return &storageprovider.StatResponse{
Status: status.NewOK(ctx),
Info: &storageprovider.ResourceInfo{
Id: &expectedResourceID,
Name: "name",
},
}, nil
})

_, err := drivesDriveItemService.MountShare(context.Background(), expectedResourceID, "")
Expand Down Expand Up @@ -634,7 +674,7 @@ var _ = Describe("DrivesDriveItemApi", func() {

httpAPI.CreateDriveItem(responseRecorder, request)

Expect(responseRecorder.Code).To(Equal(http.StatusUnprocessableEntity))
Expect(responseRecorder.Code).To(Equal(http.StatusBadRequest))

jsonData := gjson.Get(responseRecorder.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal("invalid request body"))
Expand All @@ -654,7 +694,7 @@ var _ = Describe("DrivesDriveItemApi", func() {

httpAPI.CreateDriveItem(responseRecorder, request)

Expect(responseRecorder.Code).To(Equal(http.StatusUnprocessableEntity))
Expect(responseRecorder.Code).To(Equal(http.StatusBadRequest))

jsonData = gjson.Get(responseRecorder.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal("invalid remote item id"))
Expand Down Expand Up @@ -688,7 +728,7 @@ var _ = Describe("DrivesDriveItemApi", func() {

httpAPI.CreateDriveItem(responseRecorder, request)

Expect(responseRecorder.Code).To(Equal(http.StatusFailedDependency))
Expect(responseRecorder.Code).To(Equal(http.StatusBadRequest))

jsonData := gjson.Get(responseRecorder.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal("mounting share failed"))
Expand Down