From 15223a4b85733b25c2190f940d54157ff388fbfb Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Tue, 6 Feb 2024 09:18:40 -0500 Subject: [PATCH] Folders: Set FullPath and FullPathUIDs when feature flag is off and query requests (#81896) * set FullPath and FullPathUIDs if feature flag is off and query requests Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> --- pkg/services/folder/folderimpl/folder.go | 18 +++++ pkg/services/folder/folderimpl/folder_test.go | 76 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/pkg/services/folder/folderimpl/folder.go b/pkg/services/folder/folderimpl/folder.go index 884bd0d9045..92d599091ac 100644 --- a/pkg/services/folder/folderimpl/folder.go +++ b/pkg/services/folder/folderimpl/folder.go @@ -148,11 +148,29 @@ func (s *Service) GetFolders(ctx context.Context, q folder.GetFoldersQuery) ([]* } } + if !s.features.IsEnabled(ctx, featuremgmt.FlagNestedFolders) { + qry.WithFullpath = false // do not request full path if nested folders are disabled + qry.WithFullpathUIDs = false + } + dashFolders, err := s.store.GetFolders(ctx, qry) if err != nil { return nil, folder.ErrInternal.Errorf("failed to fetch subfolders: %w", err) } + if !s.features.IsEnabled(ctx, featuremgmt.FlagNestedFolders) { + if q.WithFullpathUIDs || q.WithFullpath { + for _, f := range dashFolders { // and fix the full path with folder title (unescaped) + if q.WithFullpath { + f.Fullpath = f.Title + } + if q.WithFullpathUIDs { + f.FullpathUIDs = f.UID + } + } + } + } + return dashFolders, nil } diff --git a/pkg/services/folder/folderimpl/folder_test.go b/pkg/services/folder/folderimpl/folder_test.go index f88ba017862..bb6c8f2c758 100644 --- a/pkg/services/folder/folderimpl/folder_test.go +++ b/pkg/services/folder/folderimpl/folder_test.go @@ -1611,6 +1611,82 @@ func TestIntegrationNestedFolderSharedWithMe(t *testing.T) { }) } +func TestFolderServiceGetFolders(t *testing.T) { + db := sqlstore.InitTestDB(t) + quotaService := quotatest.New(false, nil) + folderStore := ProvideDashboardFolderStore(db) + + cfg := setting.NewCfg() + + featuresFlagOff := featuremgmt.WithFeatures() + dashStore, err := database.ProvideDashboardStore(db, db.Cfg, featuresFlagOff, tagimpl.ProvideService(db), quotaService) + require.NoError(t, err) + nestedFolderStore := ProvideStore(db, db.Cfg) + + b := bus.ProvideBus(tracing.InitializeTracerForTest()) + ac := acimpl.ProvideAccessControl(cfg) + + serviceWithFlagOff := &Service{ + cfg: cfg, + log: log.New("test-folder-service"), + dashboardStore: dashStore, + dashboardFolderStore: folderStore, + store: nestedFolderStore, + features: featuresFlagOff, + bus: b, + db: db, + accessControl: ac, + registry: make(map[string]folder.RegistryService), + metrics: newFoldersMetrics(nil), + } + + signedInAdminUser := user.SignedInUser{UserID: 1, OrgID: orgID, Permissions: map[int64]map[string][]string{ + orgID: { + dashboards.ActionFoldersCreate: {}, + dashboards.ActionFoldersWrite: {dashboards.ScopeFoldersAll}, + dashboards.ActionFoldersRead: {dashboards.ScopeFoldersAll}, + }, + }} + + createCmd := folder.CreateFolderCommand{ + OrgID: orgID, + ParentUID: "", + SignedInUser: &signedInAdminUser, + } + + guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{ + CanSaveValue: true, + CanViewValue: true, + }) + + prefix := "getfolders/ff/off" + folders := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOff, 5, prefix, createCmd) + f := folders[rand.Intn(len(folders))] + + t.Run("when flag is off", func(t *testing.T) { + t.Run("full path should be a title", func(t *testing.T) { + q := folder.GetFoldersQuery{ + OrgID: orgID, + WithFullpath: true, + WithFullpathUIDs: true, + SignedInUser: &signedInAdminUser, + UIDs: []string{f.UID}, + } + fldrs, err := serviceWithFlagOff.GetFolders(context.Background(), q) + require.NoError(t, err) + require.Len(t, fldrs, 1) + require.Equal(t, f.UID, fldrs[0].UID) + require.Equal(t, f.Title, fldrs[0].Title) + require.Equal(t, f.Title, fldrs[0].Fullpath) + + t.Run("path should not be escaped", func(t *testing.T) { + require.Contains(t, fldrs[0].Fullpath, prefix) + require.Contains(t, fldrs[0].Title, prefix) + }) + }) + }) +} + func CreateSubtreeInStore(t *testing.T, store *sqlStore, service *Service, depth int, prefix string, cmd folder.CreateFolderCommand) []*folder.Folder { t.Helper()