Skip to content

Commit

Permalink
fix creating the drive item
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 committed Apr 9, 2024
1 parent 88f1c32 commit 4bf99eb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
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
16 changes: 13 additions & 3 deletions services/graph/pkg/service/v0/api_drives_drive_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ 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

Expand Down Expand Up @@ -284,7 +287,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 +296,14 @@ 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
}

if requestDriveItem.GetName() == "" {
msg := "name is required"
api.logger.Debug().Err(err).Msg(msg)
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, msg)
return
}

Expand All @@ -302,7 +312,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

0 comments on commit 4bf99eb

Please sign in to comment.