Folders: Introduce folder service function for fetching folders by org and UIDs that contain optionally the folder full path (#80716)

* Folders: Expose function for getting all org folders with specific UIDs

* Return all org folders if UIDs is empty

* Filter out not accessible folders by the user

* Modify query to optionally returning a string that contains the UIDs of all parent folders separated by slash.
This commit is contained in:
Sofia Papagiannaki
2024-01-25 09:27:13 +02:00
committed by GitHub
parent f154b2b855
commit 5e88d29814
11 changed files with 579 additions and 83 deletions
+251 -53
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/rand"
"strings"
"testing"
"time"
@@ -420,11 +421,11 @@ func TestIntegrationNestedFolderService(t *testing.T) {
lps, err := librarypanels.ProvideService(cfg, db, routeRegister, elementService, serviceWithFlagOn)
require.NoError(t, err)
ancestorUIDs := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "getDescendantCountsOn", createCmd)
ancestors := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "getDescendantCountsOn", createCmd)
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDs[0])
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestors[0].UID)
require.NoError(t, err)
subfolder, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDs[1])
subfolder, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestors[1].UID)
require.NoError(t, err)
// nolint:staticcheck
_ = insertTestDashboard(t, serviceWithFlagOn.dashboardStore, "dashboard in parent", orgID, parent.ID, parent.UID, "prod")
@@ -443,7 +444,7 @@ func TestIntegrationNestedFolderService(t *testing.T) {
require.NoError(t, err)
countCmd := folder.GetDescendantCountsQuery{
UID: &ancestorUIDs[0],
UID: &ancestors[0].UID,
OrgID: orgID,
SignedInUser: &signedInUser,
}
@@ -456,8 +457,8 @@ func TestIntegrationNestedFolderService(t *testing.T) {
t.Cleanup(func() {
guardian.New = origNewGuardian
for _, uid := range ancestorUIDs {
err := serviceWithFlagOn.store.Delete(context.Background(), uid, orgID)
for _, ancestor := range ancestors {
err := serviceWithFlagOn.store.Delete(context.Background(), ancestor.UID, orgID)
assert.NoError(t, err)
}
})
@@ -500,11 +501,11 @@ func TestIntegrationNestedFolderService(t *testing.T) {
lps, err := librarypanels.ProvideService(cfg, db, routeRegister, elementService, serviceWithFlagOff)
require.NoError(t, err)
ancestorUIDs := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "getDescendantCountsOff", createCmd)
ancestors := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "getDescendantCountsOff", createCmd)
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDs[0])
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestors[0].UID)
require.NoError(t, err)
subfolder, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDs[1])
subfolder, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestors[1].UID)
require.NoError(t, err)
// nolint:staticcheck
_ = insertTestDashboard(t, serviceWithFlagOn.dashboardStore, "dashboard in parent", orgID, parent.ID, parent.UID, "prod")
@@ -523,7 +524,7 @@ func TestIntegrationNestedFolderService(t *testing.T) {
require.NoError(t, err)
countCmd := folder.GetDescendantCountsQuery{
UID: &ancestorUIDs[0],
UID: &ancestors[0].UID,
OrgID: orgID,
SignedInUser: &signedInUser,
}
@@ -536,8 +537,8 @@ func TestIntegrationNestedFolderService(t *testing.T) {
t.Cleanup(func() {
guardian.New = origNewGuardian
for _, uid := range ancestorUIDs {
err := serviceWithFlagOn.store.Delete(context.Background(), uid, orgID)
for _, ancestor := range ancestors {
err := serviceWithFlagOn.store.Delete(context.Background(), ancestor.UID, orgID)
assert.NoError(t, err)
}
})
@@ -638,9 +639,9 @@ func TestIntegrationNestedFolderService(t *testing.T) {
alertStore, err := ngstore.ProvideDBStore(cfg, tc.featuresFlag, db, tc.service, dashSrv, ac)
require.NoError(t, err)
ancestorUIDs := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, tc.depth, tc.prefix, createCmd)
ancestors := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, tc.depth, tc.prefix, createCmd)
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDs[0])
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestors[0].UID)
require.NoError(t, err)
_ = createRule(t, alertStore, parent.UID, "parent alert")
@@ -649,7 +650,7 @@ func TestIntegrationNestedFolderService(t *testing.T) {
subPanel model.LibraryElementDTO
)
if tc.depth > 1 {
subfolder, err = serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDs[1])
subfolder, err = serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestors[1].UID)
require.NoError(t, err)
_ = createRule(t, alertStore, subfolder.UID, "sub alert")
// nolint:staticcheck
@@ -663,7 +664,7 @@ func TestIntegrationNestedFolderService(t *testing.T) {
require.NoError(t, err)
deleteCmd := folder.DeleteFolderCommand{
UID: ancestorUIDs[0],
UID: ancestors[0].UID,
OrgID: orgID,
SignedInUser: &signedInUser,
ForceDeleteRules: tc.forceDelete,
@@ -672,12 +673,12 @@ func TestIntegrationNestedFolderService(t *testing.T) {
err = tc.service.Delete(context.Background(), &deleteCmd)
require.ErrorIs(t, err, tc.deletionErr)
for i, uid := range ancestorUIDs {
for i, ancestor := range ancestors {
// dashboard table
_, err := tc.service.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, uid)
_, err := tc.service.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestor.UID)
require.ErrorIs(t, err, tc.dashboardErr)
// folder table
_, err = tc.service.store.Get(context.Background(), folder.GetFolderQuery{UID: &ancestorUIDs[i], OrgID: orgID})
_, err = tc.service.store.Get(context.Background(), folder.GetFolderQuery{UID: &ancestors[i].UID, OrgID: orgID})
require.ErrorIs(t, err, tc.folderErr)
}
@@ -1325,12 +1326,12 @@ func TestIntegrationNestedFolderSharedWithMe(t *testing.T) {
CanViewValue: true,
})
ancestorUIDsFolderWithPermissions := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "withPermissions", createCmd)
ancestorUIDsFolderWithoutPermissions := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "withoutPermissions", createCmd)
ancestorFoldersWithPermissions := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "withPermissions", createCmd)
ancestorFoldersWithoutPermissions := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "withoutPermissions", createCmd)
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDsFolderWithoutPermissions[0])
parent, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorFoldersWithoutPermissions[0].UID)
require.NoError(t, err)
subfolder, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorUIDsFolderWithoutPermissions[1])
subfolder, err := serviceWithFlagOn.dashboardFolderStore.GetFolderByUID(context.Background(), orgID, ancestorFoldersWithoutPermissions[1].UID)
require.NoError(t, err)
// nolint:staticcheck
dash1 := insertTestDashboard(t, serviceWithFlagOn.dashboardStore, "dashboard in parent", orgID, parent.ID, parent.UID, "prod")
@@ -1341,19 +1342,19 @@ func TestIntegrationNestedFolderSharedWithMe(t *testing.T) {
CanSaveValue: true,
CanViewValue: true,
CanViewUIDs: []string{
ancestorUIDsFolderWithPermissions[0],
ancestorUIDsFolderWithPermissions[1],
ancestorUIDsFolderWithoutPermissions[1],
ancestorFoldersWithPermissions[0].UID,
ancestorFoldersWithPermissions[1].UID,
ancestorFoldersWithoutPermissions[1].UID,
dash1.UID,
dash2.UID,
},
})
signedInUser.Permissions[orgID][dashboards.ActionFoldersRead] = []string{
dashboards.ScopeFoldersProvider.GetResourceScopeUID(ancestorUIDsFolderWithPermissions[0]),
dashboards.ScopeFoldersProvider.GetResourceScopeUID(ancestorFoldersWithPermissions[0].UID),
// Add permission to the subfolder of folder with permission (to check deduplication)
dashboards.ScopeFoldersProvider.GetResourceScopeUID(ancestorUIDsFolderWithPermissions[1]),
dashboards.ScopeFoldersProvider.GetResourceScopeUID(ancestorFoldersWithPermissions[1].UID),
// Add permission to the subfolder of folder without permission
dashboards.ScopeFoldersProvider.GetResourceScopeUID(ancestorUIDsFolderWithoutPermissions[1]),
dashboards.ScopeFoldersProvider.GetResourceScopeUID(ancestorFoldersWithoutPermissions[1].UID),
}
signedInUser.Permissions[orgID][dashboards.ActionDashboardsRead] = []string{
dashboards.ScopeDashboardsProvider.GetResourceScopeUID(dash1.UID),
@@ -1374,8 +1375,8 @@ func TestIntegrationNestedFolderSharedWithMe(t *testing.T) {
require.NoError(t, err)
require.Len(t, sharedFolders, 1)
require.Contains(t, sharedFoldersUIDs, ancestorUIDsFolderWithoutPermissions[1])
require.NotContains(t, sharedFoldersUIDs, ancestorUIDsFolderWithPermissions[1])
require.Contains(t, sharedFoldersUIDs, ancestorFoldersWithoutPermissions[1].UID)
require.NotContains(t, sharedFoldersUIDs, ancestorFoldersWithPermissions[1].UID)
sharedDashboards, err := dashboardService.GetDashboardsSharedWithUser(context.Background(), &signedInUser)
sharedDashboardsUIDs := make([]string, 0)
@@ -1390,28 +1391,236 @@ func TestIntegrationNestedFolderSharedWithMe(t *testing.T) {
t.Cleanup(func() {
guardian.New = origNewGuardian
for _, uid := range ancestorUIDsFolderWithPermissions {
err := serviceWithFlagOn.store.Delete(context.Background(), uid, orgID)
for _, ancestor := range ancestorFoldersWithPermissions {
err := serviceWithFlagOn.store.Delete(context.Background(), ancestor.UID, orgID)
assert.NoError(t, err)
}
})
t.Cleanup(func() {
guardian.New = origNewGuardian
for _, uid := range ancestorUIDsFolderWithoutPermissions {
err := serviceWithFlagOn.store.Delete(context.Background(), uid, orgID)
for _, ancestor := range ancestorFoldersWithoutPermissions {
err := serviceWithFlagOn.store.Delete(context.Background(), ancestor.UID, orgID)
assert.NoError(t, err)
}
})
})
t.Run("Should get org folders visible", func(t *testing.T) {
depth := 3
origNewGuardian := guardian.New
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{
CanSaveValue: true,
CanViewValue: true,
})
// create folder sctructure like this:
// tree1-folder-0
// └──tree1-folder-1
// └──tree1-folder-2
// tree2-folder-0
// └──tree2-folder-1
// └──tree2-folder-2
tree1 := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "tree1-", createCmd)
tree2 := CreateSubtreeInStore(t, nestedFolderStore, serviceWithFlagOn, depth, "tree2-", createCmd)
signedInUser.Permissions[orgID][dashboards.ActionFoldersRead] = []string{
// Add permission to tree1-folder-0
dashboards.ScopeFoldersProvider.GetResourceScopeUID(tree1[0].UID),
// Add permission to the subfolder of folder with permission (tree1-folder-1) to check deduplication
dashboards.ScopeFoldersProvider.GetResourceScopeUID(tree1[1].UID),
// Add permission to the subfolder of folder without permission (tree2-folder-1)
dashboards.ScopeFoldersProvider.GetResourceScopeUID(tree2[1].UID),
}
t.Cleanup(func() {
guardian.New = origNewGuardian
for _, f := range tree1 {
err := serviceWithFlagOn.store.Delete(context.Background(), f.UID, orgID)
assert.NoError(t, err)
}
for _, f := range tree2 {
err := serviceWithFlagOn.store.Delete(context.Background(), f.UID, orgID)
assert.NoError(t, err)
}
})
testCases := []struct {
name string
cmd folder.GetFoldersQuery
expected []*folder.Folder
}{
{
name: "Should get all org folders visible to the user",
cmd: folder.GetFoldersQuery{
OrgID: orgID,
SignedInUser: &signedInUser,
},
expected: []*folder.Folder{
{
UID: tree1[0].UID,
},
{
UID: tree1[1].UID,
},
{
UID: tree1[2].UID,
},
{
UID: tree2[1].UID,
},
{
UID: tree2[2].UID,
},
},
},
{
name: "Should get all org folders visible to the user with fullpath",
cmd: folder.GetFoldersQuery{
OrgID: orgID,
WithFullpath: true,
SignedInUser: &signedInUser,
},
expected: []*folder.Folder{
{
UID: tree1[0].UID,
Fullpath: "tree1-folder-0",
},
{
UID: tree1[1].UID,
Fullpath: "tree1-folder-0/tree1-folder-1",
},
{
UID: tree1[2].UID,
Fullpath: "tree1-folder-0/tree1-folder-1/tree1-folder-2",
},
{
UID: tree2[1].UID,
Fullpath: "tree2-folder-0/tree2-folder-1",
},
{
UID: tree2[2].UID,
Fullpath: "tree2-folder-0/tree2-folder-1/tree2-folder-2",
},
},
},
{
name: "Should get all org folders visible to the user with fullpath UIDs",
cmd: folder.GetFoldersQuery{
OrgID: orgID,
WithFullpathUIDs: true,
SignedInUser: &signedInUser,
},
expected: []*folder.Folder{
{
UID: tree1[0].UID,
FullpathUIDs: strings.Join([]string{tree1[0].UID}, "/"),
},
{
UID: tree1[1].UID,
FullpathUIDs: strings.Join([]string{tree1[0].UID, tree1[1].UID}, "/"),
},
{
UID: tree1[2].UID,
FullpathUIDs: strings.Join([]string{tree1[0].UID, tree1[1].UID, tree1[2].UID}, "/"),
},
{
UID: tree2[1].UID,
FullpathUIDs: strings.Join([]string{tree2[0].UID, tree2[1].UID}, "/"),
},
{
UID: tree2[2].UID,
FullpathUIDs: strings.Join([]string{tree2[0].UID, tree2[1].UID, tree2[2].UID}, "/"),
},
},
},
{
name: "Should get specific org folders visible to the user",
cmd: folder.GetFoldersQuery{
OrgID: orgID,
UIDs: []string{tree1[0].UID, tree2[0].UID, tree2[1].UID},
SignedInUser: &signedInUser,
},
expected: []*folder.Folder{
{
UID: tree1[0].UID,
},
{
UID: tree2[1].UID,
},
},
},
{
name: "Should get all org folders visible to the user with admin permissions",
cmd: folder.GetFoldersQuery{
OrgID: orgID,
SignedInUser: &signedInAdminUser,
},
expected: []*folder.Folder{
{
UID: tree1[0].UID,
Fullpath: "tree1-folder-0",
FullpathUIDs: strings.Join([]string{tree1[0].UID}, "/"),
},
{
UID: tree1[1].UID,
Fullpath: "tree1-folder-0/tree1-folder-1",
FullpathUIDs: strings.Join([]string{tree1[0].UID, tree1[1].UID}, "/"),
},
{
UID: tree1[2].UID,
Fullpath: "tree1-folder-0/tree1-folder-1/tree1-folder-2",
},
{
UID: tree2[0].UID,
Fullpath: "tree2-folder-0",
FullpathUIDs: strings.Join([]string{tree2[0].UID}, "/"),
},
{
UID: tree2[1].UID,
Fullpath: "tree2-folder-0/tree2-folder-1",
FullpathUIDs: strings.Join([]string{tree2[0].UID, tree2[1].UID}, "/"),
},
{
UID: tree2[2].UID,
Fullpath: "tree2-folder-0/tree2-folder-1/tree2-folder-2",
FullpathUIDs: strings.Join([]string{tree2[0].UID, tree2[1].UID, tree2[2].UID}, "/"),
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualFolders, err := serviceWithFlagOn.GetFolders(context.Background(), tc.cmd)
require.NoError(t, err)
require.NoError(t, err)
require.Len(t, actualFolders, len(tc.expected))
for i, expected := range tc.expected {
actualFolder := actualFolders[i]
require.Equal(t, expected.UID, actualFolder.UID)
if tc.cmd.WithFullpath {
require.Equal(t, expected.Fullpath, actualFolder.Fullpath)
} else {
require.Empty(t, actualFolder.Fullpath)
}
if tc.cmd.WithFullpathUIDs {
require.Equal(t, expected.FullpathUIDs, actualFolder.FullpathUIDs)
} else {
require.Empty(t, actualFolder.FullpathUIDs)
}
}
})
}
})
}
func CreateSubtreeInStore(t *testing.T, store *sqlStore, service *Service, depth int, prefix string, cmd folder.CreateFolderCommand) []string {
func CreateSubtreeInStore(t *testing.T, store *sqlStore, service *Service, depth int, prefix string, cmd folder.CreateFolderCommand) []*folder.Folder {
t.Helper()
ancestorUIDs := []string{}
if cmd.ParentUID != "" {
ancestorUIDs = append(ancestorUIDs, cmd.ParentUID)
}
folders := make([]*folder.Folder, 0, depth)
for i := 0; i < depth; i++ {
title := fmt.Sprintf("%sfolder-%d", prefix, i)
cmd.Title = title
@@ -1422,23 +1631,12 @@ func CreateSubtreeInStore(t *testing.T, store *sqlStore, service *Service, depth
require.Equal(t, title, f.Title)
require.NotEmpty(t, f.UID)
parents, err := store.GetParents(context.Background(), folder.GetParentsQuery{
UID: f.UID,
OrgID: cmd.OrgID,
})
require.NoError(t, err)
parentUIDs := []string{}
for _, p := range parents {
parentUIDs = append(parentUIDs, p.UID)
}
require.Equal(t, ancestorUIDs, parentUIDs)
ancestorUIDs = append(ancestorUIDs, f.UID)
folders = append(folders, f)
cmd.ParentUID = f.UID
}
return ancestorUIDs
return folders
}
func setup(t *testing.T, dashStore dashboards.Store, dashboardFolderStore folder.FolderStore, nestedFolderStore store, features featuremgmt.FeatureToggles, ac accesscontrol.AccessControl, db db.DB) folder.Service {