graph/sharedbyme: Use existing method to initialize driveitem properties

getDriveItem does a Stat() call on the resource and sets the
corresponding DriveItem properties.
This commit is contained in:
Ralf Haferkamp
2023-10-18 14:22:20 +02:00
committed by Ralf Haferkamp
parent b0538559c2
commit 454ec52955
2 changed files with 90 additions and 64 deletions

View File

@@ -7,6 +7,7 @@ import (
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/share"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/go-chi/render"
@@ -108,13 +109,15 @@ func (g Graph) cs3UserSharesToDriveItems(ctx context.Context, shares []*collabor
resIDStr := storagespace.FormatResourceID(*s.ResourceId)
item, ok := driveItems[resIDStr]
if !ok {
item = libregraph.DriveItem{
Id: libregraph.PtrString(resIDStr),
itemptr, err := g.getDriveItem(ctx, storageprovider.Reference{ResourceId: s.ResourceId})
if err != nil {
g.logger.Debug().Err(err).Interface("Share", s.ResourceId).Msg("could not stat share, skipping")
continue
}
item = *itemptr
}
driveItems[resIDStr] = item
}
return driveItems, nil
}
@@ -124,9 +127,12 @@ func (g Graph) cs3PublicSharesToDriveItems(ctx context.Context, shares []*link.P
resIDStr := storagespace.FormatResourceID(*s.ResourceId)
item, ok := driveItems[resIDStr]
if !ok {
item = libregraph.DriveItem{
Id: libregraph.PtrString(resIDStr),
itemptr, err := g.getDriveItem(ctx, storageprovider.Reference{ResourceId: s.ResourceId})
if err != nil {
g.logger.Debug().Err(err).Interface("Share", s.ResourceId).Msg("could not stat share, skipping")
continue
}
item = *itemptr
}
driveItems[resIDStr] = item
}

View File

@@ -33,7 +33,7 @@ import (
"google.golang.org/grpc"
)
var _ = Describe("Driveitems", func() {
var _ = Describe("sharedbyme", func() {
var (
svc service.Service
ctx context.Context
@@ -47,6 +47,61 @@ var _ = Describe("Driveitems", func() {
newGroup *libregraph.Group
)
userShare := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "share-id",
},
ResourceId: &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "opaqueid",
},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
Id: &provider.Grantee_UserId{
UserId: &userpb.UserId{
OpaqueId: "user-id",
},
},
},
}
groupShare := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "share-id",
},
ResourceId: &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "opaqueid",
},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_GROUP,
Id: &provider.Grantee_GroupId{
GroupId: &grouppb.GroupId{
OpaqueId: "group-id",
},
},
},
}
userShareWithExpiration := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "expire-share-id",
},
ResourceId: &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "expire-opaqueid",
},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
Id: &provider.Grantee_UserId{
UserId: &userpb.UserId{
OpaqueId: "user-id",
},
},
},
Expiration: utils.TimeToTS(time.Now()),
}
BeforeEach(func() {
eventsPublisher.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
@@ -62,6 +117,28 @@ var _ = Describe("Driveitems", func() {
},
nil,
)
// no stat for the image
gatewayClient.On("Stat",
mock.Anything,
mock.MatchedBy(
func(req *provider.StatRequest) bool {
return req.Ref.ResourceId.OpaqueId == userShareWithExpiration.ResourceId.OpaqueId
})).
Return(&provider.StatResponse{
Status: status.NewOK(ctx),
Info: &provider.ResourceInfo{
Id: userShareWithExpiration.ResourceId,
},
}, nil)
gatewayClient.On("Stat",
mock.Anything,
mock.Anything).
Return(&provider.StatResponse{
Status: status.NewOK(ctx),
Info: &provider.ResourceInfo{
Id: userShare.ResourceId,
},
}, nil)
gatewaySelector = pool.GetSelector[gateway.GatewayAPIClient](
"GatewaySelector",
"com.owncloud.api.gateway",
@@ -93,66 +170,8 @@ var _ = Describe("Driveitems", func() {
})
Describe("GetSharedByMe", func() {
expiration := time.Now()
userShare := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "share-id",
},
ResourceId: &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "opaqueid",
},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
Id: &provider.Grantee_UserId{
UserId: &userpb.UserId{
OpaqueId: "user-id",
},
},
},
}
groupShare := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "share-id",
},
ResourceId: &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "opaqueid",
},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_GROUP,
Id: &provider.Grantee_GroupId{
GroupId: &grouppb.GroupId{
OpaqueId: "group-id",
},
},
},
}
userShareWithExpiration := collaboration.Share{
Id: &collaboration.ShareId{
OpaqueId: "expire-share-id",
},
ResourceId: &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "expire-opaqueid",
},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
Id: &provider.Grantee_UserId{
UserId: &userpb.UserId{
OpaqueId: "user-id",
},
},
},
Expiration: utils.TimeToTS(expiration),
}
It("handles a failing ListShares", func() {
gatewayClient.On("ListShares", mock.Anything, mock.Anything).Return(nil, errors.New("some error"))
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me/drives/sharedByMe", nil)
svc.GetSharedByMe(rr, r)
Expect(rr.Code).To(Equal(http.StatusInternalServerError))
@@ -202,6 +221,7 @@ var _ = Describe("Driveitems", func() {
},
nil,
)
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me/drives/sharedByMe", nil)
svc.GetSharedByMe(rr, r)
Expect(rr.Code).To(Equal(http.StatusOK))