From e13e6a7bd74b2012e9117ff72df2750623ff31f3 Mon Sep 17 00:00:00 2001 From: ying-jeanne <74549700+ying-jeanne@users.noreply.github.com> Date: Tue, 30 Nov 2021 14:07:04 +0100 Subject: [PATCH] fix error code for delete folder (#42470) --- pkg/services/libraryelements/database.go | 6 ++++++ pkg/services/libraryelements/libraryelements_test.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/pkg/services/libraryelements/database.go b/pkg/services/libraryelements/database.go index 53487f0ce84..98ccfdf485e 100644 --- a/pkg/services/libraryelements/database.go +++ b/pkg/services/libraryelements/database.go @@ -692,9 +692,15 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Conte if err != nil { return err } + + if len(folderUIDs) == 0 { + return models.ErrFolderNotFound + } + if len(folderUIDs) != 1 { return fmt.Errorf("found %d folders, while expecting at most one", len(folderUIDs)) } + folderID := folderUIDs[0].ID if err := l.requirePermissionsOnFolder(c, signedInUser, folderID); err != nil { diff --git a/pkg/services/libraryelements/libraryelements_test.go b/pkg/services/libraryelements/libraryelements_test.go index 132a1457f7f..52fd64e4e45 100644 --- a/pkg/services/libraryelements/libraryelements_test.go +++ b/pkg/services/libraryelements/libraryelements_test.go @@ -67,6 +67,12 @@ func TestDeleteLibraryPanelsInFolder(t *testing.T) { require.EqualError(t, err, ErrFolderHasConnectedLibraryElements.Error()) }) + scenarioWithPanel(t, "When an admin tries to delete a folder uid that doesn't exist, it should fail", + func(t *testing.T, sc scenarioContext) { + err := sc.service.DeleteLibraryElementsInFolder(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, sc.folder.Uid+"xxxx") + require.EqualError(t, err, models.ErrFolderNotFound.Error()) + }) + scenarioWithPanel(t, "When an admin tries to delete a folder that contains disconnected elements, it should delete all disconnected elements too", func(t *testing.T, sc scenarioContext) { command := getCreateVariableCommand(sc.folder.Id, "query0")