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>
This commit is contained in:
Yuri Tseretyan 2024-02-06 09:18:40 -05:00 committed by GitHub
parent 92e05ec8e9
commit 15223a4b85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 94 additions and 0 deletions

View File

@ -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) dashFolders, err := s.store.GetFolders(ctx, qry)
if err != nil { if err != nil {
return nil, folder.ErrInternal.Errorf("failed to fetch subfolders: %w", err) 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 return dashFolders, nil
} }

View File

@ -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 { func CreateSubtreeInStore(t *testing.T, store *sqlStore, service *Service, depth int, prefix string, cmd folder.CreateFolderCommand) []*folder.Folder {
t.Helper() t.Helper()