2021-05-11 13:10:19 +08:00
|
|
|
package libraryelements
|
|
|
|
|
|
|
|
import (
|
2021-11-05 22:06:14 +08:00
|
|
|
"encoding/json"
|
2021-05-11 13:10:19 +08:00
|
|
|
"testing"
|
|
|
|
|
2025-07-30 05:52:57 +08:00
|
|
|
"github.com/stretchr/testify/mock"
|
2025-04-24 22:10:03 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-01-16 23:33:55 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2023-02-02 00:32:05 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/libraryelements/model"
|
2022-08-10 17:56:48 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2021-10-11 20:30:59 +08:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2021-05-11 13:10:19 +08:00
|
|
|
)
|
|
|
|
|
2025-04-24 22:10:03 +08:00
|
|
|
func TestIntegration_DeleteLibraryElement(t *testing.T) {
|
2025-07-28 19:38:54 +08:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test in short mode")
|
|
|
|
}
|
2021-05-11 13:10:19 +08:00
|
|
|
scenarioWithPanel(t, "When an admin tries to delete a library panel that does not exist, it should fail",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 404, resp.Status())
|
|
|
|
})
|
|
|
|
|
2021-11-05 22:06:14 +08:00
|
|
|
scenarioWithPanel(t, "When an admin tries to delete a library panel that exists, it should succeed and return correct ID",
|
2021-05-11 13:10:19 +08:00
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2025-07-30 05:52:57 +08:00
|
|
|
sc.dashboardSvc.On("FindDashboards", mock.Anything, mock.Anything).Return([]dashboards.DashboardSearchProjection{}, nil)
|
2021-10-11 20:30:59 +08:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
2021-05-11 13:10:19 +08:00
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 200, resp.Status())
|
2021-11-05 22:06:14 +08:00
|
|
|
|
2023-02-02 00:32:05 +08:00
|
|
|
var result model.DeleteLibraryElementResponse
|
2021-11-05 22:06:14 +08:00
|
|
|
err := json.Unmarshal(resp.Body(), &result)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sc.initialResult.Result.ID, result.ID)
|
2021-05-11 13:10:19 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
scenarioWithPanel(t, "When an admin tries to delete a library panel in another org, it should fail",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2021-10-11 20:30:59 +08:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
2025-04-10 20:42:23 +08:00
|
|
|
sc.reqContext.OrgID = 2
|
|
|
|
sc.reqContext.OrgRole = org.RoleAdmin
|
2021-05-11 13:10:19 +08:00
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 404, resp.Status())
|
|
|
|
})
|
|
|
|
|
|
|
|
scenarioWithPanel(t, "When an admin tries to delete a library panel that is connected, it should fail",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2025-07-30 05:52:57 +08:00
|
|
|
sc.dashboardSvc.On("FindDashboards", mock.Anything, mock.Anything).Return([]dashboards.DashboardSearchProjection{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
UID: "test",
|
2021-05-11 13:10:19 +08:00
|
|
|
},
|
2025-07-30 05:52:57 +08:00
|
|
|
}, nil)
|
2023-11-21 04:44:51 +08:00
|
|
|
// nolint:staticcheck
|
2025-07-30 05:52:57 +08:00
|
|
|
err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, 1)
|
2021-05-11 13:10:19 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-11 20:30:59 +08:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
2021-05-11 13:10:19 +08:00
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 403, resp.Status())
|
|
|
|
})
|
2025-01-28 04:37:38 +08:00
|
|
|
|
|
|
|
scenarioWithPanel(t, "When an admin tries to delete a library panel that is connected to a non-existent dashboard, it should succeed",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2025-07-30 05:52:57 +08:00
|
|
|
sc.dashboardSvc.On("FindDashboards", mock.Anything, mock.Anything).Return([]dashboards.DashboardSearchProjection{}, nil)
|
2025-01-28 04:37:38 +08:00
|
|
|
err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, 9999999)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
})
|
2021-05-11 13:10:19 +08:00
|
|
|
}
|