Chore: Remove bus from folder service (#46840)

* Remove bus from folder service

* Fix tests
This commit is contained in:
Selene
2022-03-23 19:40:22 +01:00
committed by GitHub
parent 7839fadf00
commit d57c94fb6a
5 changed files with 23 additions and 48 deletions

View File

@@ -6,15 +6,14 @@ import (
"strconv"
"strings"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
)
@@ -26,12 +25,13 @@ type FolderServiceImpl struct {
searchService *search.SearchService
features featuremgmt.FeatureToggles
permissions accesscontrol.PermissionsService
sqlStore sqlstore.Store
}
func ProvideFolderService(
cfg *setting.Cfg, dashboardService dashboards.DashboardService, dashboardStore dashboards.Store,
searchService *search.SearchService, features featuremgmt.FeatureToggles, permissionsServices accesscontrol.PermissionsServices,
ac accesscontrol.AccessControl,
ac accesscontrol.AccessControl, sqlStore sqlstore.Store,
) *FolderServiceImpl {
ac.RegisterAttributeScopeResolver(dashboards.NewNameScopeResolver(dashboardStore))
ac.RegisterAttributeScopeResolver(dashboards.NewUidScopeResolver(dashboardStore))
@@ -44,6 +44,7 @@ func ProvideFolderService(
searchService: searchService,
features: features,
permissions: permissionsServices.GetFolderService(),
sqlStore: sqlStore,
}
}
@@ -185,11 +186,16 @@ func (f *FolderServiceImpl) CreateFolder(ctx context.Context, user *models.Signe
func (f *FolderServiceImpl) UpdateFolder(ctx context.Context, user *models.SignedInUser, orgID int64, existingUid string, cmd *models.UpdateFolderCommand) error {
query := models.GetDashboardQuery{OrgId: orgID, Uid: existingUid}
dashFolder, err := getFolder(ctx, query)
if err != nil {
if err := f.sqlStore.GetDashboard(ctx, &query); err != nil {
return toFolderError(err)
}
dashFolder := query.Result
if !dashFolder.IsFolder {
return models.ErrFolderNotFound
}
cmd.UpdateDashboardModel(dashFolder, orgID, user.UserId)
dto := &dashboards.SaveDashboardDTO{
@@ -245,18 +251,6 @@ func (f *FolderServiceImpl) MakeUserAdmin(ctx context.Context, orgID int64, user
return f.dashboardService.MakeUserAdmin(ctx, orgID, userID, folderID, setViewAndEditPermissions)
}
func getFolder(ctx context.Context, query models.GetDashboardQuery) (*models.Dashboard, error) {
if err := bus.Dispatch(ctx, &query); err != nil {
return nil, toFolderError(err)
}
if !query.Result.IsFolder {
return nil, models.ErrFolderNotFound
}
return query.Result, nil
}
func toFolderError(err error) error {
if errors.Is(err, models.ErrDashboardTitleEmpty) {
return models.ErrFolderTitleEmpty

View File

@@ -12,13 +12,13 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
@@ -37,7 +37,7 @@ func TestProvideFolderService(t *testing.T) {
ProvideFolderService(
cfg, &dashboards.FakeDashboardService{DashboardService: dashboardService},
store, nil, features, permissionsServices, ac,
store, nil, features, permissionsServices, ac, mockstore.NewSQLStoreMock(),
)
require.Len(t, ac.Calls.RegisterAttributeScopeResolver, 2)
@@ -51,6 +51,7 @@ func TestFolderService(t *testing.T) {
features := featuremgmt.WithFeatures()
permissionsServices := acmock.NewPermissionsServicesMock()
dashboardService := ProvideDashboardService(cfg, store, nil, features, permissionsServices)
mockStore := mockstore.NewSQLStoreMock()
service := FolderServiceImpl{
cfg: cfg,
@@ -60,6 +61,7 @@ func TestFolderService(t *testing.T) {
searchService: nil,
features: features,
permissions: permissionsServices.GetFolderService(),
sqlStore: mockStore,
}
t.Run("Given user has no permissions", func(t *testing.T) {
@@ -99,12 +101,7 @@ func TestFolderService(t *testing.T) {
})
t.Run("When updating folder should return access denied error", func(t *testing.T) {
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
query.Result = models.NewDashboardFolder("Folder")
return nil
})
defer bus.ClearBusHandlers()
mockStore.ExpectedDashboard = models.NewDashboardFolder("Folder")
err := service.UpdateFolder(context.Background(), user, orgID, folderUID, &models.UpdateFolderCommand{
Uid: folderUID,
Title: "Folder-TEST",
@@ -132,12 +129,6 @@ func TestFolderService(t *testing.T) {
dash.Id = rand.Int63()
f := models.DashboardToFolder(dash)
bus.AddHandler("test", func(ctx context.Context, cmd *models.SaveDashboardCommand) error {
cmd.Result = dash
return nil
})
defer bus.ClearBusHandlers()
store.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil)
store.On("SaveDashboard", mock.Anything).Return(dash, nil).Once()
store.On("GetFolderByID", mock.Anything, orgID, dash.Id).Return(f, nil)
@@ -153,17 +144,7 @@ func TestFolderService(t *testing.T) {
dashboardFolder.Uid = util.GenerateShortUID()
f := models.DashboardToFolder(dashboardFolder)
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
query.Result = dashboardFolder
return nil
})
bus.AddHandler("test", func(ctx context.Context, cmd *models.SaveDashboardCommand) error {
cmd.Result = dashboardFolder
return nil
})
defer bus.ClearBusHandlers()
mockStore.ExpectedDashboard = dashboardFolder
store.On("ValidateDashboardBeforeSave", mock.Anything, mock.Anything).Return(true, nil)
store.On("SaveDashboard", mock.Anything).Return(dashboardFolder, nil)