Skip to content

Commit

Permalink
add other tests and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Feb 8, 2023
1 parent 1a1b2e7 commit 174c247
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 12 deletions.
29 changes: 17 additions & 12 deletions pkg/ocm/share/repository/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,30 @@ func genID() string {
return uuid.New().String()
}

func (m *mgr) StoreShare(ctx context.Context, share *ocm.Share) (*ocm.Share, error) {
func (m *mgr) StoreShare(ctx context.Context, ocmshare *ocm.Share) (*ocm.Share, error) {
m.Lock()
defer m.Unlock()

if err := m.load(); err != nil {
return nil, err
}

share.Id = &ocm.ShareId{OpaqueId: genID()}
m.model.Shares[share.Id.OpaqueId] = cloneShare(share)
if _, err := m.getByKey(ctx, &ocm.ShareKey{
Owner: ocmshare.Owner,
ResourceId: ocmshare.ResourceId,
Grantee: ocmshare.Grantee,
}); err == nil {
return nil, share.ErrShareAlreadyExisting
}

ocmshare.Id = &ocm.ShareId{OpaqueId: genID()}
m.model.Shares[ocmshare.Id.OpaqueId] = cloneShare(ocmshare)

if err := m.save(); err != nil {
return nil, errors.Wrap(err, "error saving share")
}

return share, nil
return ocmshare, nil
}

func cloneShare(s *ocm.Share) *ocm.Share {
Expand Down Expand Up @@ -272,6 +280,9 @@ func cloneReceivedShare(s *ocm.ReceivedShare) *ocm.ReceivedShare {
}

func (m *mgr) GetShare(ctx context.Context, user *userpb.User, ref *ocm.ShareReference) (*ocm.Share, error) {
m.Lock()
defer m.Unlock()

var (
s *ocm.Share
err error
Expand Down Expand Up @@ -299,30 +310,24 @@ func (m *mgr) GetShare(ctx context.Context, user *userpb.User, ref *ocm.ShareRef
return s, nil
}

return nil, errtypes.NotFound(ref.String())
return nil, share.ErrShareNotFound
}

func (m *mgr) getByID(ctx context.Context, id *ocm.ShareId) (*ocm.Share, error) {
m.Lock()
defer m.Unlock()

if share, ok := m.model.Shares[id.OpaqueId]; ok {
return share, nil
}
return nil, errtypes.NotFound(id.String())
}

func (m *mgr) getByKey(ctx context.Context, key *ocm.ShareKey) (*ocm.Share, error) {
m.Lock()
defer m.Unlock()

for _, share := range m.model.Shares {
if (utils.UserEqual(key.Owner, share.Owner) || utils.UserEqual(key.Owner, share.Creator)) &&
utils.ResourceIDEqual(key.ResourceId, share.ResourceId) && utils.GranteeEqual(key.Grantee, share.Grantee) {
return share, nil
}
}
return nil, errtypes.NotFound(key.String())
return nil, share.ErrShareNotFound
}

func (m *mgr) DeleteShare(ctx context.Context, user *userpb.User, ref *ocm.ShareReference) error {
Expand Down
75 changes: 75 additions & 0 deletions tests/integration/grpc/ocm_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,81 @@ var _ = Describe("ocm share", func() {
})
})

Context("einstein creates twice the share to marie", func() {
It("fail with already existing error", func() {
fileToShare := &provider.Reference{Path: "/home/double-share"}
Expect(helpers.CreateFolder(ctxEinstein, cernboxgw, fileToShare.Path)).To(Succeed())

By("share the file with marie")

info, err := stat(ctxEinstein, cernboxgw, fileToShare)
Expect(err).ToNot(HaveOccurred())

cesnet, err := cernboxgw.GetInfoByDomain(ctxEinstein, &ocmproviderpb.GetInfoByDomainRequest{
Domain: "cesnet.cz",
})
Expect(err).ToNot(HaveOccurred())
Expect(cesnet.Status.Code).To(Equal(rpcv1beta1.Code_CODE_OK))

createShareRes, err := cernboxgw.CreateOCMShare(ctxEinstein, &ocmv1beta1.CreateOCMShareRequest{
ResourceId: info.Id,
Grantee: &provider.Grantee{
Id: &provider.Grantee_UserId{
UserId: marie.Id,
},
},
AccessMethods: []*ocmv1beta1.AccessMethod{
share.NewWebDavAccessMethod(conversions.NewEditorRole().CS3ResourcePermissions()),
},
RecipientMeshProvider: cesnet.ProviderInfo,
})
Expect(err).ToNot(HaveOccurred())
Expect(createShareRes.Status.Code).To(Equal(rpcv1beta1.Code_CODE_OK))

By("resharing the same file with marie")

createShareRes2, err := cernboxgw.CreateOCMShare(ctxEinstein, &ocmv1beta1.CreateOCMShareRequest{
ResourceId: info.Id,
Grantee: &provider.Grantee{
Id: &provider.Grantee_UserId{
UserId: marie.Id,
},
},
AccessMethods: []*ocmv1beta1.AccessMethod{
share.NewWebDavAccessMethod(conversions.NewEditorRole().CS3ResourcePermissions()),
},
RecipientMeshProvider: cesnet.ProviderInfo,
})
Expect(err).ToNot(HaveOccurred())
Expect(createShareRes2.Status.Code).To(Equal(rpcv1beta1.Code_CODE_ALREADY_EXISTS))
})
})

Context("einstein creates a share on a not existing resource", func() {
It("fail with not found error", func() {
cesnet, err := cernboxgw.GetInfoByDomain(ctxEinstein, &ocmproviderpb.GetInfoByDomainRequest{
Domain: "cesnet.cz",
})
Expect(err).ToNot(HaveOccurred())
Expect(cesnet.Status.Code).To(Equal(rpcv1beta1.Code_CODE_OK))

createShareRes, err := cernboxgw.CreateOCMShare(ctxEinstein, &ocmv1beta1.CreateOCMShareRequest{
ResourceId: &provider.ResourceId{StorageId: "123e4567-e89b-12d3-a456-426655440000", OpaqueId: "NON_EXISTING_FILE"},
Grantee: &provider.Grantee{
Id: &provider.Grantee_UserId{
UserId: marie.Id,
},
},
AccessMethods: []*ocmv1beta1.AccessMethod{
share.NewWebDavAccessMethod(conversions.NewEditorRole().CS3ResourcePermissions()),
},
RecipientMeshProvider: cesnet.ProviderInfo,
})
Expect(err).ToNot(HaveOccurred())
Expect(createShareRes.Status.Code).To(Equal(rpcv1beta1.Code_CODE_NOT_FOUND))
})
})

})
})

Expand Down

0 comments on commit 174c247

Please sign in to comment.