NestedFolders: Add API endpoint for descendant count in a folder (#66550)

* Add CountInFolder to RegistryService interface
* Add folder children counts api route
* Update fake GetFolderChildrenCounts
* Add test for getting folder children counts
* Add validation to folder children counts handler
* Update openapi specs
* Update pkg/services/folder/folderimpl/folder.go
Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>

---------

Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
This commit is contained in:
Arati R
2023-04-24 15:57:28 +02:00
committed by GitHub
co-authored by Sofia Papagiannaki
parent 990b3c07ab
commit fd434cab58
14 changed files with 821 additions and 36 deletions
@@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/appcontext"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/db/dbtest"
"github.com/grafana/grafana/pkg/infra/log"
@@ -22,6 +23,7 @@ import (
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/dashboards/database"
"github.com/grafana/grafana/pkg/services/dashboards/service"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/folder/foldertest"
@@ -930,6 +932,65 @@ func TestNestedFolderService(t *testing.T) {
})
}
func TestGetChildrenCounts(t *testing.T) {
g := guardian.New
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{CanViewValue: true})
t.Cleanup(func() {
guardian.New = g
})
folderId := rand.Int63()
folderUID := util.GenerateShortUID()
f := folder.NewFolder("Folder", "")
f.ID = folderId
f.UID = folderUID
f.OrgID = orgID
nestedFolderStore := NewFakeStore()
nestedFolderStore.ExpectedFolder = f
dashStore := dashboards.FakeDashboardStore{}
dashboardCount := int64(2)
countCmd := dashboards.CountDashboardsInFolderRequest{
FolderID: f.ID,
OrgID: f.OrgID,
}
dashStore.On("CountDashboardsInFolder", mock.Anything, &countCmd).Return(dashboardCount, nil)
dashboardFolderStore := foldertest.NewFakeFolderStore(t)
dashboardFolderStore.On("GetFolderByUID", mock.Anything, orgID, folderUID).Return(f, nil)
cfg := setting.NewCfg()
cfg.RBACEnabled = false
features := featuremgmt.WithFeatures(featuremgmt.FlagNestedFolders)
folderService := &Service{
cfg: cfg,
store: nestedFolderStore,
dashboardStore: &dashStore,
dashboardFolderStore: dashboardFolderStore,
features: features,
log: log.New("test-folder-service"),
accessControl: acimpl.ProvideAccessControl(cfg),
registry: make(map[string]folder.RegistryService),
}
ac := acmock.New()
folderPermissions := acmock.NewMockedPermissionsService()
dashboardPermissions := acmock.NewMockedPermissionsService()
_, err := service.ProvideDashboardServiceImpl(cfg, &dashStore, dashboardFolderStore, nil, features, folderPermissions, dashboardPermissions, ac, folderService)
require.NoError(t, err)
signedInUser := user.SignedInUser{UserID: 1, OrgID: orgID}
ctx := appcontext.WithUser(context.Background(), &signedInUser)
res, err := folderService.GetChildrenCounts(ctx, &folder.GetChildrenCountsQuery{
SignedInUser: usr,
UID: &folderUID,
OrgID: orgID,
})
require.NoError(t, err)
require.Equal(t, res["dashboard"], dashboardCount)
}
func CreateSubtreeInStore(t *testing.T, store *sqlStore, service *Service, depth int, prefix string, cmd folder.CreateFolderCommand) []string {
t.Helper()