2021-05-11 00:10:19 -05:00
|
|
|
package libraryelements
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2023-01-29 22:14:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/kinds/librarypanel"
|
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
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetLibraryElement(t *testing.T) {
|
|
|
|
scenarioWithPanel(t, "When an admin tries to get a library panel that does not exist, it should fail",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2021-05-14 09:03:37 -05:00
|
|
|
// by uid
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": "unknown"})
|
2021-05-11 00:10:19 -05:00
|
|
|
resp := sc.service.getHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 404, resp.Status())
|
2021-05-14 09:03:37 -05:00
|
|
|
|
|
|
|
// by name
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":name": "unknown"})
|
2021-05-14 09:03:37 -05:00
|
|
|
resp = sc.service.getByNameHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 404, resp.Status())
|
2021-05-11 00:10:19 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
scenarioWithPanel(t, "When an admin tries to get a library panel that exists, it should succeed and return correct result",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
2021-05-14 09:03:37 -05:00
|
|
|
var expected = func(res libraryElementResult) libraryElementResult {
|
|
|
|
return libraryElementResult{
|
|
|
|
Result: libraryElement{
|
|
|
|
ID: 1,
|
|
|
|
OrgID: 1,
|
2023-11-06 10:31:15 -06:00
|
|
|
FolderID: 1, // nolint:staticcheck
|
2021-05-14 09:03:37 -05:00
|
|
|
UID: res.Result.UID,
|
|
|
|
Name: "Text - Library Panel",
|
2023-02-01 10:32:05 -06:00
|
|
|
Kind: int64(model.PanelElement),
|
2021-05-14 09:03:37 -05:00
|
|
|
Type: "text",
|
|
|
|
Description: "A description",
|
2023-08-30 10:46:47 -05:00
|
|
|
Model: map[string]any{
|
2021-05-14 09:03:37 -05:00
|
|
|
"datasource": "${DS_GDEV-TESTDATA}",
|
|
|
|
"description": "A description",
|
|
|
|
"id": float64(1),
|
|
|
|
"title": "Text - Library Panel",
|
|
|
|
"type": "text",
|
2021-05-12 01:48:17 -05:00
|
|
|
},
|
2021-05-14 09:03:37 -05:00
|
|
|
Version: 1,
|
2023-02-01 10:32:05 -06:00
|
|
|
Meta: model.LibraryElementDTOMeta{
|
2021-05-14 09:03:37 -05:00
|
|
|
FolderName: "ScenarioFolder",
|
2022-11-11 07:28:24 -06:00
|
|
|
FolderUID: sc.folder.UID,
|
2021-05-14 09:03:37 -05:00
|
|
|
ConnectedDashboards: 0,
|
|
|
|
Created: res.Result.Meta.Created,
|
|
|
|
Updated: res.Result.Meta.Updated,
|
2023-01-29 22:14:12 -06:00
|
|
|
CreatedBy: librarypanel.LibraryElementDTOMetaUser{
|
|
|
|
Id: 1,
|
2021-05-14 09:03:37 -05:00
|
|
|
Name: userInDbName,
|
2023-01-29 22:14:12 -06:00
|
|
|
AvatarUrl: userInDbAvatar,
|
2021-05-14 09:03:37 -05:00
|
|
|
},
|
2023-01-29 22:14:12 -06:00
|
|
|
UpdatedBy: librarypanel.LibraryElementDTOMetaUser{
|
|
|
|
Id: 1,
|
2021-05-14 09:03:37 -05:00
|
|
|
Name: userInDbName,
|
2023-01-29 22:14:12 -06:00
|
|
|
AvatarUrl: userInDbAvatar,
|
2021-05-14 09:03:37 -05:00
|
|
|
},
|
2021-05-12 01:48:17 -05:00
|
|
|
},
|
|
|
|
},
|
2021-05-14 09:03:37 -05:00
|
|
|
}
|
2021-05-12 01:48:17 -05:00
|
|
|
}
|
2021-05-14 09:03:37 -05:00
|
|
|
|
2023-04-20 04:24:41 -05:00
|
|
|
sc.reqContext.SignedInUser.Permissions[sc.reqContext.OrgID][dashboards.ActionFoldersRead] = []string{dashboards.ScopeFoldersAll}
|
|
|
|
|
2021-05-14 09:03:37 -05:00
|
|
|
// by uid
|
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-14 09:03:37 -05:00
|
|
|
resp := sc.service.getHandler(sc.reqContext)
|
|
|
|
var result = validateAndUnMarshalResponse(t, resp)
|
|
|
|
|
|
|
|
if diff := cmp.Diff(expected(result), result, getCompareOptions()...); diff != "" {
|
|
|
|
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// by name
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":name": sc.initialResult.Result.Name})
|
2021-05-14 09:03:37 -05:00
|
|
|
resp = sc.service.getByNameHandler(sc.reqContext)
|
|
|
|
arrayResult := validateAndUnMarshalArrayResponse(t, resp)
|
|
|
|
|
|
|
|
if diff := cmp.Diff(libraryElementArrayResult{Result: []libraryElement{expected(result).Result}}, arrayResult, getCompareOptions()...); diff != "" {
|
2021-05-12 01:48:17 -05:00
|
|
|
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
scenarioWithPanel(t, "When an admin tries to get a connected library panel, it should succeed and return correct connected dashboards",
|
|
|
|
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-12 01:48:17 -05:00
|
|
|
"id": int64(1),
|
2023-08-30 10:46:47 -05:00
|
|
|
"gridPos": map[string]any{
|
2021-05-12 01:48:17 -05:00
|
|
|
"h": 6,
|
|
|
|
"w": 6,
|
|
|
|
"x": 0,
|
|
|
|
"y": 0,
|
|
|
|
},
|
|
|
|
},
|
2023-08-30 10:46:47 -05:00
|
|
|
map[string]any{
|
2021-05-12 01:48:17 -05:00
|
|
|
"id": int64(2),
|
2023-08-30 10:46:47 -05:00
|
|
|
"gridPos": map[string]any{
|
2021-05-12 01:48:17 -05:00
|
|
|
"h": 6,
|
|
|
|
"w": 6,
|
|
|
|
"x": 6,
|
|
|
|
"y": 0,
|
|
|
|
},
|
2023-08-30 10:46:47 -05:00
|
|
|
"libraryPanel": map[string]any{
|
2021-05-12 01:48:17 -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-12 01:48:17 -05:00
|
|
|
Title: "Testing getHandler",
|
|
|
|
Data: simplejson.NewFromAny(dashJSON),
|
|
|
|
}
|
2023-11-20 14:44:51 -06:00
|
|
|
// nolint:staticcheck
|
2022-11-11 07:28:24 -06:00
|
|
|
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.ID)
|
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-12 01:48:17 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-14 09:03:37 -05:00
|
|
|
expected := func(res libraryElementResult) libraryElementResult {
|
|
|
|
return libraryElementResult{
|
|
|
|
Result: libraryElement{
|
|
|
|
ID: 1,
|
|
|
|
OrgID: 1,
|
2023-11-06 10:31:15 -06:00
|
|
|
FolderID: 1, // nolint:staticcheck
|
2021-05-14 09:03:37 -05:00
|
|
|
UID: res.Result.UID,
|
|
|
|
Name: "Text - Library Panel",
|
2023-02-01 10:32:05 -06:00
|
|
|
Kind: int64(model.PanelElement),
|
2021-05-14 09:03:37 -05:00
|
|
|
Type: "text",
|
|
|
|
Description: "A description",
|
2023-08-30 10:46:47 -05:00
|
|
|
Model: map[string]any{
|
2021-05-14 09:03:37 -05:00
|
|
|
"datasource": "${DS_GDEV-TESTDATA}",
|
|
|
|
"description": "A description",
|
|
|
|
"id": float64(1),
|
|
|
|
"title": "Text - Library Panel",
|
|
|
|
"type": "text",
|
2021-05-11 00:10:19 -05:00
|
|
|
},
|
2021-05-14 09:03:37 -05:00
|
|
|
Version: 1,
|
2023-02-01 10:32:05 -06:00
|
|
|
Meta: model.LibraryElementDTOMeta{
|
2021-05-14 09:03:37 -05:00
|
|
|
FolderName: "ScenarioFolder",
|
2022-11-11 07:28:24 -06:00
|
|
|
FolderUID: sc.folder.UID,
|
2021-05-14 09:03:37 -05:00
|
|
|
ConnectedDashboards: 1,
|
|
|
|
Created: res.Result.Meta.Created,
|
|
|
|
Updated: res.Result.Meta.Updated,
|
2023-01-29 22:14:12 -06:00
|
|
|
CreatedBy: librarypanel.LibraryElementDTOMetaUser{
|
|
|
|
Id: 1,
|
2021-05-14 09:03:37 -05:00
|
|
|
Name: userInDbName,
|
2023-01-29 22:14:12 -06:00
|
|
|
AvatarUrl: userInDbAvatar,
|
2021-05-14 09:03:37 -05:00
|
|
|
},
|
2023-01-29 22:14:12 -06:00
|
|
|
UpdatedBy: librarypanel.LibraryElementDTOMetaUser{
|
|
|
|
Id: 1,
|
2021-05-14 09:03:37 -05:00
|
|
|
Name: userInDbName,
|
2023-01-29 22:14:12 -06:00
|
|
|
AvatarUrl: userInDbAvatar,
|
2021-05-14 09:03:37 -05:00
|
|
|
},
|
2021-05-11 00:10:19 -05:00
|
|
|
},
|
|
|
|
},
|
2021-05-14 09:03:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 04:24:41 -05:00
|
|
|
sc.reqContext.SignedInUser.Permissions[sc.reqContext.OrgID][dashboards.ActionFoldersRead] = []string{dashboards.ScopeFoldersAll}
|
|
|
|
|
2021-05-14 09:03:37 -05:00
|
|
|
// by uid
|
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-14 09:03:37 -05:00
|
|
|
resp := sc.service.getHandler(sc.reqContext)
|
|
|
|
result := validateAndUnMarshalResponse(t, resp)
|
|
|
|
|
|
|
|
if diff := cmp.Diff(expected(result), result, getCompareOptions()...); diff != "" {
|
|
|
|
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
2021-05-11 00:10:19 -05:00
|
|
|
}
|
2021-05-14 09:03:37 -05:00
|
|
|
|
|
|
|
// by name
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":name": sc.initialResult.Result.Name})
|
2021-05-14 09:03:37 -05:00
|
|
|
resp = sc.service.getByNameHandler(sc.reqContext)
|
|
|
|
arrayResult := validateAndUnMarshalArrayResponse(t, resp)
|
|
|
|
if diff := cmp.Diff(libraryElementArrayResult{Result: []libraryElement{expected(result).Result}}, arrayResult, getCompareOptions()...); diff != "" {
|
2021-05-11 00:10:19 -05:00
|
|
|
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
scenarioWithPanel(t, "When an admin tries to get a library panel that exists in an other org, it should fail",
|
|
|
|
func(t *testing.T, sc scenarioContext) {
|
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-14 09:03:37 -05:00
|
|
|
|
|
|
|
// by uid
|
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.getHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 404, resp.Status())
|
2021-05-14 09:03:37 -05:00
|
|
|
|
|
|
|
// by name
|
2021-10-11 07:30:59 -05:00
|
|
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":name": sc.initialResult.Result.Name})
|
2021-05-14 09:03:37 -05:00
|
|
|
resp = sc.service.getByNameHandler(sc.reqContext)
|
|
|
|
require.Equal(t, 404, resp.Status())
|
2021-05-11 00:10:19 -05:00
|
|
|
})
|
|
|
|
}
|