Skip to content

Commit

Permalink
add delete home space test & refactor duplicate imports
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Aug 31, 2022
1 parent af31376 commit 6a33832
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions pkg/storage/utils/decomposedfs/spaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ package decomposedfs_test

import (
"context"
"fmt"
"os"

cs3permissions "github.com/cs3org/go-cs3apis/cs3/permissions/v1beta1"
permissionsv1beta1 "github.com/cs3org/go-cs3apis/cs3/permissions/v1beta1"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
ruser "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node"
helpers "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/testhelpers"
"github.com/cs3org/reva/v2/pkg/storagespace"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -51,13 +52,30 @@ var _ = Describe("Spaces", func() {
func(ctx context.Context, in *cs3permissions.CheckPermissionRequest, opts ...grpc.CallOption) *cs3permissions.CheckPermissionResponse {
if ctxpkg.ContextMustGetUser(ctx).Id.GetOpaqueId() == "25b69780-5f39-43be-a7ac-a9b9e9fe4230" {
// id of owner/admin
return &permissionsv1beta1.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK}}
return &cs3permissions.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK}}
}
// id of generic user
return &permissionsv1beta1.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_PERMISSION_DENIED}}
return &cs3permissions.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_PERMISSION_DENIED}}
},
nil)
env.Permissions.On("HasPermission", mock.Anything, mock.Anything, mock.Anything).Return(
func(ctx context.Context, n *node.Node, check func(*provider.ResourcePermissions) bool) bool {
if ctxpkg.ContextMustGetUser(ctx).Id.GetOpaqueId() == "25b69780-5f39-43be-a7ac-a9b9e9fe4230" {
// id of owner/admin
return true
}
// id of generic user
return false
},
func(ctx context.Context, n *node.Node, check func(*provider.ResourcePermissions) bool) error {
if ctxpkg.ContextMustGetUser(ctx).Id.GetOpaqueId() == "25b69780-5f39-43be-a7ac-a9b9e9fe4230" {
// id of owner/admin
return nil
}
// id of generic user
return errtypes.PermissionDenied(fmt.Sprintf("user is not allowed to delete home space %s", n.ID))

})
})

AfterEach(func() {
Expand Down Expand Up @@ -95,12 +113,12 @@ var _ = Describe("Spaces", func() {
Expect(resp).To(Equal(true))
})
It("returns true on requesting unrestricted as non-admin", func() {
ctx := ruser.ContextSetUser(context.Background(), env.Users[0])
ctx := ctxpkg.ContextSetUser(context.Background(), env.Users[0])
resp := env.Fs.MustCheckNodePermissions(ctx, true)
Expect(resp).To(Equal(true))
})
It("returns true on requesting for own spaces", func() {
ctx := ruser.ContextSetUser(context.Background(), env.Users[0])
ctx := ctxpkg.ContextSetUser(context.Background(), env.Users[0])
resp := env.Fs.MustCheckNodePermissions(ctx, false)
Expect(resp).To(Equal(true))
})
Expand All @@ -112,7 +130,7 @@ var _ = Describe("Spaces", func() {

Context("can list spaces of requested user", func() {
It("returns false on requesting for other user as non-admin", func() {
ctx := ruser.ContextSetUser(context.Background(), env.Users[0])
ctx := ctxpkg.ContextSetUser(context.Background(), env.Users[0])
res := env.Fs.CanListSpacesOfRequestedUser(ctx, helpers.User1ID)
Expect(res).To(Equal(false))
})
Expand All @@ -126,6 +144,35 @@ var _ = Describe("Spaces", func() {
})
})

Context("can delete homespace", func() {
It("fails on trying to delete a homespace as non-admin", func() {
ctx := ctxpkg.ContextSetUser(context.Background(), env.Users[1])
resp, err := env.Fs.ListStorageSpaces(env.Ctx, nil, false)
Expect(err).ToNot(HaveOccurred())
Expect(len(resp)).To(Equal(1))
Expect(string(resp[0].Opaque.GetMap()["spaceAlias"].Value)).To(Equal("personal/username"))
Expect(resp[0].Name).To(Equal("username"))
Expect(resp[0].SpaceType).To(Equal("personal"))
err = env.Fs.DeleteStorageSpace(ctx, &provider.DeleteStorageSpaceRequest{
Id: resp[0].GetId(),
})
Expect(err).To(HaveOccurred())
})
It("succeeds on trying to delete homespace as admin", func() {
ctx := ctxpkg.ContextSetUser(context.Background(), env.Owner)
resp, err := env.Fs.ListStorageSpaces(env.Ctx, nil, false)
Expect(err).ToNot(HaveOccurred())
Expect(len(resp)).To(Equal(1))
Expect(string(resp[0].Opaque.GetMap()["spaceAlias"].Value)).To(Equal("personal/username"))
Expect(resp[0].Name).To(Equal("username"))
Expect(resp[0].SpaceType).To(Equal("personal"))
err = env.Fs.DeleteStorageSpace(ctx, &provider.DeleteStorageSpaceRequest{
Id: resp[0].GetId(),
})
Expect(err).To(Not(HaveOccurred()))
})
})

Describe("Create Spaces with custom alias template", func() {
var (
env *helpers.TestEnv
Expand All @@ -138,7 +185,7 @@ var _ = Describe("Spaces", func() {
"generalspacealias_template": "{{.SpaceType}}:{{.SpaceName | replace \" \" \"-\" | upper}}",
})
Expect(err).ToNot(HaveOccurred())
env.PermissionsClient.On("CheckPermission", mock.Anything, mock.Anything, mock.Anything).Return(&permissionsv1beta1.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK}}, nil)
env.PermissionsClient.On("CheckPermission", mock.Anything, mock.Anything, mock.Anything).Return(&cs3permissions.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK}}, nil)
})

AfterEach(func() {
Expand Down Expand Up @@ -220,10 +267,10 @@ var _ = Describe("Spaces", func() {
func(ctx context.Context, in *cs3permissions.CheckPermissionRequest, opts ...grpc.CallOption) *cs3permissions.CheckPermissionResponse {
if ctxpkg.ContextMustGetUser(ctx).Id.GetOpaqueId() == "25b69780-5f39-43be-a7ac-a9b9e9fe4230" {
// id of owner/admin
return &permissionsv1beta1.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK}}
return &cs3permissions.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK}}
}
// id of generic user
return &permissionsv1beta1.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_PERMISSION_DENIED}}
return &cs3permissions.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_PERMISSION_DENIED}}
},
nil)

Expand Down Expand Up @@ -252,7 +299,7 @@ var _ = Describe("Spaces", func() {
Expect(updateResp.StorageSpace.Quota.QuotaMaxBytes, uint64(1000))
})
It("try to change quota as a non admin user", func() {
ctx := ruser.ContextSetUser(context.Background(), env.Users[0])
ctx := ctxpkg.ContextSetUser(context.Background(), env.Users[0])
updateResp, err := env.Fs.UpdateStorageSpace(
ctx,
&provider.UpdateStorageSpaceRequest{
Expand Down

0 comments on commit 6a33832

Please sign in to comment.