mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
PanelLibrary: Adds uid and renames title to name (#29944)
* PanelLibrary: Adds uid and renames title to name * Chore: removing lines * Chore: updates comments * Chore: changes after PR comments
This commit is contained in:
@@ -19,7 +19,6 @@ func TestCreateLibraryPanel(t *testing.T) {
|
||||
testScenario(t, "When an admin tries to create a library panel that already exists, it should fail",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
command := getCreateCommand(1, "Text - Library Panel")
|
||||
|
||||
response := sc.service.createHandler(sc.reqContext, command)
|
||||
require.Equal(t, 200, response.Status())
|
||||
|
||||
@@ -40,9 +39,15 @@ func TestDeleteLibraryPanel(t *testing.T) {
|
||||
command := getCreateCommand(1, "Text - Library Panel")
|
||||
response := sc.service.createHandler(sc.reqContext, command)
|
||||
require.Equal(t, 200, response.Status())
|
||||
|
||||
var result libraryPanelResult
|
||||
err := json.Unmarshal(response.Body(), &result)
|
||||
require.NoError(t, err)
|
||||
|
||||
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID})
|
||||
response = sc.service.deleteHandler(sc.reqContext)
|
||||
require.Equal(t, 200, response.Status())
|
||||
}, scenarioConfig{params: map[string]string{":panelId": "1"}})
|
||||
})
|
||||
|
||||
testScenario(t, "When an admin tries to delete a library panel in another org, it should fail",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
@@ -50,21 +55,25 @@ func TestDeleteLibraryPanel(t *testing.T) {
|
||||
response := sc.service.createHandler(sc.reqContext, command)
|
||||
require.Equal(t, 200, response.Status())
|
||||
|
||||
var result libraryPanelResult
|
||||
err := json.Unmarshal(response.Body(), &result)
|
||||
require.NoError(t, err)
|
||||
|
||||
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID})
|
||||
sc.reqContext.SignedInUser.OrgId = 2
|
||||
sc.reqContext.SignedInUser.OrgRole = models.ROLE_ADMIN
|
||||
response = sc.service.deleteHandler(sc.reqContext)
|
||||
require.Equal(t, 404, response.Status())
|
||||
}, scenarioConfig{
|
||||
params: map[string]string{":panelId": "1"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetLibraryPanel(t *testing.T) {
|
||||
testScenario(t, "When an admin tries to get a library panel that does not exist, it should fail",
|
||||
func(t *testing.T, sc scenarioContext) {
|
||||
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown"})
|
||||
response := sc.service.getHandler(sc.reqContext)
|
||||
require.Equal(t, 404, response.Status())
|
||||
}, scenarioConfig{params: map[string]string{":panelId": "74"}})
|
||||
})
|
||||
|
||||
testScenario(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) {
|
||||
@@ -72,29 +81,35 @@ func TestGetLibraryPanel(t *testing.T) {
|
||||
response := sc.service.createHandler(sc.reqContext, command)
|
||||
require.Equal(t, 200, response.Status())
|
||||
|
||||
var result libraryPanelResult
|
||||
err := json.Unmarshal(response.Body(), &result)
|
||||
require.NoError(t, err)
|
||||
|
||||
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID})
|
||||
response = sc.service.getHandler(sc.reqContext)
|
||||
require.Equal(t, 200, response.Status())
|
||||
err = json.Unmarshal(response.Body(), &result)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(1), result.Result.FolderID)
|
||||
require.Equal(t, "Text - Library Panel", result.Result.Name)
|
||||
})
|
||||
|
||||
testScenario(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) {
|
||||
command := getCreateCommand(1, "Text - Library Panel")
|
||||
response := sc.service.createHandler(sc.reqContext, command)
|
||||
require.Equal(t, 200, response.Status())
|
||||
|
||||
var result libraryPanelResult
|
||||
err := json.Unmarshal(response.Body(), &result)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(1), result.Result.FolderID)
|
||||
require.Equal(t, "Text - Library Panel", result.Result.Title)
|
||||
}, scenarioConfig{params: map[string]string{":panelId": "1"}})
|
||||
|
||||
testScenario(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) {
|
||||
command := getCreateCommand(1, "Text - Library Panel")
|
||||
|
||||
response := sc.service.createHandler(sc.reqContext, command)
|
||||
require.Equal(t, 200, response.Status())
|
||||
|
||||
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID})
|
||||
sc.reqContext.SignedInUser.OrgId = 2
|
||||
sc.reqContext.SignedInUser.OrgRole = models.ROLE_ADMIN
|
||||
|
||||
response = sc.service.getHandler(sc.reqContext)
|
||||
require.Equal(t, 404, response.Status())
|
||||
}, scenarioConfig{params: map[string]string{":panelId": "1"}})
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAllLibraryPanels(t *testing.T) {
|
||||
@@ -128,9 +143,9 @@ func TestGetAllLibraryPanels(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, len(result.Result))
|
||||
require.Equal(t, int64(1), result.Result[0].FolderID)
|
||||
require.Equal(t, "Text - Library Panel", result.Result[0].Title)
|
||||
require.Equal(t, "Text - Library Panel", result.Result[0].Name)
|
||||
require.Equal(t, int64(1), result.Result[1].FolderID)
|
||||
require.Equal(t, "Text - Library Panel2", result.Result[1].Title)
|
||||
require.Equal(t, "Text - Library Panel2", result.Result[1].Name)
|
||||
})
|
||||
|
||||
testScenario(t, "When an admin tries to get all library panels in a different org, none should be returned",
|
||||
@@ -148,11 +163,10 @@ func TestGetAllLibraryPanels(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(result.Result))
|
||||
require.Equal(t, int64(1), result.Result[0].FolderID)
|
||||
require.Equal(t, "Text - Library Panel", result.Result[0].Title)
|
||||
require.Equal(t, "Text - Library Panel", result.Result[0].Name)
|
||||
|
||||
sc.reqContext.SignedInUser.OrgId = 2
|
||||
sc.reqContext.SignedInUser.OrgRole = models.ROLE_ADMIN
|
||||
|
||||
response = sc.service.getAllHandler(sc.reqContext)
|
||||
require.Equal(t, 200, response.Status())
|
||||
|
||||
@@ -168,7 +182,8 @@ type libraryPanel struct {
|
||||
ID int64 `json:"ID"`
|
||||
OrgID int64 `json:"OrgID"`
|
||||
FolderID int64 `json:"FolderID"`
|
||||
Title string `json:"Title"`
|
||||
UID string `json:"UID"`
|
||||
Name string `json:"Name"`
|
||||
}
|
||||
|
||||
type libraryPanelResult struct {
|
||||
@@ -200,15 +215,15 @@ func overrideLibraryPanelServiceInRegistry(cfg *setting.Cfg) LibraryPanelService
|
||||
return lps
|
||||
}
|
||||
|
||||
func getCreateCommand(folderID int64, title string) createLibraryPanelCommand {
|
||||
func getCreateCommand(folderID int64, name string) createLibraryPanelCommand {
|
||||
command := createLibraryPanelCommand{
|
||||
FolderID: folderID,
|
||||
Title: title,
|
||||
Name: name,
|
||||
Model: []byte(`
|
||||
{
|
||||
"datasource": "${DS_GDEV-TESTDATA}",
|
||||
"id": 1,
|
||||
"title": "Text - Library Panel",
|
||||
"name": "Text - Library Panel",
|
||||
"type": "text"
|
||||
}
|
||||
`),
|
||||
@@ -224,15 +239,9 @@ type scenarioContext struct {
|
||||
user models.SignedInUser
|
||||
}
|
||||
|
||||
type scenarioConfig struct {
|
||||
params map[string]string
|
||||
orgID int64
|
||||
role models.RoleType
|
||||
}
|
||||
|
||||
// testScenario is a wrapper around t.Run performing common setup for library panel tests.
|
||||
// It takes your real test function as a callback.
|
||||
func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioContext), cfgs ...scenarioConfig) {
|
||||
func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioContext)) {
|
||||
t.Helper()
|
||||
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
@@ -241,17 +250,6 @@ func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioCo
|
||||
ctx := macaron.Context{}
|
||||
orgID := int64(1)
|
||||
role := models.ROLE_ADMIN
|
||||
for _, cfg := range cfgs {
|
||||
if len(cfg.params) > 0 {
|
||||
ctx.ReplaceAllParams(cfg.params)
|
||||
}
|
||||
if cfg.orgID != 0 {
|
||||
orgID = cfg.orgID
|
||||
}
|
||||
if cfg.role != "" {
|
||||
role = cfg.role
|
||||
}
|
||||
}
|
||||
|
||||
cfg := setting.NewCfg()
|
||||
// Everything in this service is behind the feature toggle "panelLibrary"
|
||||
|
||||
Reference in New Issue
Block a user