2021-05-11 00:10:19 -05:00
|
|
|
package libraryelements
|
|
|
|
|
|
|
|
import (
|
2021-11-05 09:06:14 -05:00
|
|
|
"encoding/json"
|
2021-05-11 00:10:19 -05:00
|
|
|
"testing"
|
|
|
|
|
2024-07-12 09:47:49 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-05-11 00:10:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2023-01-16 09:33:55 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2023-02-01 10:32:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/libraryelements/model"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2021-05-11 00:10:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeleteLibraryElement(t *testing.T) {
|
|
|
|
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 09:06:14 -05:00
|
|
|
scenarioWithPanel(t, "When an admin tries to delete a library panel that exists, it should succeed and return correct ID",
|
2021-05-11 00:10:19 -05:00
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
2021-05-11 00:10:19 -05:00
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 200, resp.Status())
|
2021-11-05 09:06:14 -05:00
|
|
|
|
2023-02-01 10:32:05 -06:00
|
|
|
var result model.DeleteLibraryElementResponse
|
2021-11-05 09:06:14 -05:00
|
|
|
err := json.Unmarshal(resp.Body(), &result)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, sc.initialResult.Result.ID, result.ID)
|
2021-05-11 00:10:19 -05: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 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
2022-08-11 06:28:55 -05:00
|
|
|
sc.reqContext.SignedInUser.OrgID = 2
|
2022-08-10 04:56:48 -05:00
|
|
|
sc.reqContext.SignedInUser.OrgRole = org.RoleAdmin
|
2021-05-11 00:10:19 -05: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) {
|
2023-08-30 10:46:47 -05:00
|
|
|
dashJSON := map[string]any{
|
|
|
|
"panels": []any{
|
|
|
|
map[string]any{
|
2021-05-11 00:10:19 -05:00
|
|
|
"id": int64(1),
|
2023-08-30 10:46:47 -05:00
|
|
|
"gridPos": map[string]any{
|
2021-05-11 00:10:19 -05:00
|
|
|
"h": 6,
|
|
|
|
"w": 6,
|
|
|
|
"x": 0,
|
|
|
|
"y": 0,
|
|
|
|
},
|
|
|
|
},
|
2023-08-30 10:46:47 -05:00
|
|
|
map[string]any{
|
2021-05-11 00:10:19 -05:00
|
|
|
"id": int64(2),
|
2023-08-30 10:46:47 -05:00
|
|
|
"gridPos": map[string]any{
|
2021-05-11 00:10:19 -05:00
|
|
|
"h": 6,
|
|
|
|
"w": 6,
|
|
|
|
"x": 6,
|
|
|
|
"y": 0,
|
|
|
|
},
|
2023-08-30 10:46:47 -05:00
|
|
|
"libraryPanel": map[string]any{
|
2021-05-11 00:10:19 -05:00
|
|
|
"uid": sc.initialResult.Result.UID,
|
|
|
|
"name": sc.initialResult.Result.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-01-16 09:33:55 -06:00
|
|
|
dash := dashboards.Dashboard{
|
2021-05-11 00:10:19 -05:00
|
|
|
Title: "Testing deleteHandler ",
|
|
|
|
Data: simplejson.NewFromAny(dashJSON),
|
|
|
|
}
|
2023-11-20 14:44:51 -06:00
|
|
|
// nolint:staticcheck
|
2024-07-12 09:47:49 -05:00
|
|
|
dashInDB := createDashboard(t, sc.replStore, sc.user, &dash, sc.folder.ID, sc.folder.UID)
|
2023-01-16 09:33:55 -06:00
|
|
|
err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, dashInDB.ID)
|
2021-05-11 00:10:19 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
|
2021-05-11 00:10:19 -05:00
|
|
|
resp := sc.service.deleteHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 403, resp.Status())
|
|
|
|
})
|
|
|
|
}
|