Skip to content

Commit

Permalink
Merge pull request #5041 from owncloud/graph-unit-test
Browse files Browse the repository at this point in the history
[tests-only] add more unit tests for the drives operations
  • Loading branch information
micbar committed Nov 18, 2022
2 parents 9504cea + d1ff976 commit 1329daf
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 34 deletions.
3 changes: 0 additions & 3 deletions protogen/gen/ocis/messages/settings/v0/settings.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion protogen/gen/ocis/services/thumbnails/v0/thumbnails.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions services/graph/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ci-go-generate: $(MOCKERY) # CI runs ci-node-generate automatically before this
$(MOCKERY) --dir pkg/service/v0 --case underscore --name GatewayClient
$(MOCKERY) --dir pkg/service/v0 --case underscore --name HTTPClient
$(MOCKERY) --dir pkg/service/v0 --case underscore --name Publisher
$(MOCKERY) --dir pkg/service/v0 --case underscore --name Permissions
$(MOCKERY) --srcpkg github.com/go-ldap/ldap/v3 --case underscore --filename ldapclient.go --name Client


Expand Down
78 changes: 78 additions & 0 deletions services/graph/mocks/permissions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions services/graph/pkg/service/v0/drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,8 @@ func (g Graph) GetSingleDrive(w http.ResponseWriter, r *http.Request) {
}
}

func canCreateSpace(ctx context.Context, ownPersonalHome bool) bool {
s := settingssvc.NewPermissionService("com.owncloud.api.settings", grpc.DefaultClient())

pr, err := s.GetPermissionByID(ctx, &settingssvc.GetPermissionByIDRequest{
func (g Graph) canCreateSpace(ctx context.Context, ownPersonalHome bool) bool {
pr, err := g.permissionsService.GetPermissionByID(ctx, &settingssvc.GetPermissionByIDRequest{
PermissionId: settingsServiceExt.CreateSpacePermissionID,
})
if err != nil || pr.Permission == nil {
Expand All @@ -228,7 +226,7 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
}

// TODO determine if the user tries to create his own personal space and pass that as a boolean
canCreateSpace := canCreateSpace(r.Context(), false)
canCreateSpace := g.canCreateSpace(r.Context(), false)
if !canCreateSpace {
logger.Debug().Bool("cancreatespace", canCreateSpace).Msg("could not create drive: insufficient permissions")
// if the permission is not existing for the user in context we can assume we don't have it. Return 401.
Expand Down Expand Up @@ -708,7 +706,7 @@ func (g Graph) getDriveQuota(ctx context.Context, space *storageprovider.Storage
return nil, nil
case res.GetStatus().GetCode() != cs3rpc.Code_CODE_OK:
logger.Debug().Str("grpc", res.GetStatus().GetMessage()).Msg("error sending get quota grpc request")
return nil, err
return nil, errors.New(res.GetStatus().GetMessage())
}

var remaining int64
Expand Down
3 changes: 3 additions & 0 deletions services/graph/pkg/service/v0/drives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,7 @@ func TestSpaceNameValidation(t *testing.T) {
err := validateSpaceName(tc.SpaceName)
require.Equal(t, tc.ExpectedError, err, tc.Alias)
}

// set max length back to protect other tests
_maxSpaceNameLength = 255
}
8 changes: 8 additions & 0 deletions services/graph/pkg/service/v0/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/graph/pkg/config"
"github.com/owncloud/ocis/v2/services/graph/pkg/identity"
"go-micro.dev/v4/client"
mevents "go-micro.dev/v4/events"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -62,6 +63,12 @@ type Publisher interface {
Publish(string, interface{}, ...mevents.PublishOption) error
}

// Permissions is the interface used to access the permissions service
type Permissions interface {
GetPermissionByID(ctx context.Context, request *settingssvc.GetPermissionByIDRequest, opts ...client.CallOption) (*settingssvc.GetPermissionByIDResponse, error)
ListPermissionsByResource(ctx context.Context, in *settingssvc.ListPermissionsByResourceRequest, opts ...client.CallOption) (*settingssvc.ListPermissionsByResourceResponse, error)
}

// HTTPClient is the subset of the http.Client that is being used to interact with the download gateway
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
Expand All @@ -78,6 +85,7 @@ type Graph struct {
identityBackend identity.Backend
gatewayClient GatewayClient
roleService settingssvc.RoleService
permissionsService Permissions
spacePropertiesCache *ttlcache.Cache
eventsPublisher events.Publisher
}
Expand Down
Loading

0 comments on commit 1329daf

Please sign in to comment.