69d9f427e1
* WIP: intial structure * Refactor: adds create library element endpoint * Feature: adds delete library element * wip * Refactor: adds get api * Refactor: adds get all api * Refactor: adds patch api * Refactor: changes to library_element_connection * Refactor: add get connections api * wip: in the middle of refactor * wip * Refactor: consolidating both api:s * Refactor: points front end to library elements api * Tests: Fixes broken test * LibraryPanels: removes feature toggle * Fix: fixes delete library elements in folder and adds tests * Tests: fixes snapshot * Refactor: adds service interfaces so they can be easily mocked * Refactor: changes order of tabs in manage folder * Refactor: fixes so link does not cover whole card * Refactor: fixes index string name * Update pkg/services/libraryelements/libraryelements.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Update pkg/services/libraryelements/libraryelements_permissions_test.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Update pkg/services/libraryelements/database.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Chore: changes after PR comments * Update libraryelements.go * Update libraryelements.go * Chore: updates after PR comments * Chore: trying to fix build error * Refactor: fixed stupid mistake * Update libraryelements.go * Chore: tries to fix build errors * Refactor: trying to fix MySQL key length * Update libraryelements.go * Update pkg/services/libraryelements/libraryelements.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Update pkg/services/librarypanels/librarypanels.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Refactor: changes after PR comments * Refactor: changes after PR comments * Tests: fixes tests * Refactor: renames connections to connectedDashboards Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
159 lines
5.0 KiB
Go
159 lines
5.0 KiB
Go
package libraryelements
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
)
|
|
|
|
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) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown"})
|
|
resp := sc.service.getHandler(sc.reqContext)
|
|
require.Equal(t, 404, resp.Status())
|
|
})
|
|
|
|
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) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID})
|
|
resp := sc.service.getHandler(sc.reqContext)
|
|
var result = validateAndUnMarshalResponse(t, resp)
|
|
var expected = libraryElementResult{
|
|
Result: libraryElement{
|
|
ID: 1,
|
|
OrgID: 1,
|
|
FolderID: 1,
|
|
UID: result.Result.UID,
|
|
Name: "Text - Library Panel",
|
|
Kind: int64(Panel),
|
|
Type: "text",
|
|
Description: "A description",
|
|
Model: map[string]interface{}{
|
|
"datasource": "${DS_GDEV-TESTDATA}",
|
|
"description": "A description",
|
|
"id": float64(1),
|
|
"title": "Text - Library Panel",
|
|
"type": "text",
|
|
},
|
|
Version: 1,
|
|
Meta: LibraryElementDTOMeta{
|
|
FolderName: "ScenarioFolder",
|
|
FolderUID: sc.folder.Uid,
|
|
ConnectedDashboards: 0,
|
|
Created: result.Result.Meta.Created,
|
|
Updated: result.Result.Meta.Updated,
|
|
CreatedBy: LibraryElementDTOMetaUser{
|
|
ID: 1,
|
|
Name: userInDbName,
|
|
AvatarURL: userInDbAvatar,
|
|
},
|
|
UpdatedBy: LibraryElementDTOMetaUser{
|
|
ID: 1,
|
|
Name: userInDbName,
|
|
AvatarURL: userInDbAvatar,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
|
|
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) {
|
|
dashJSON := map[string]interface{}{
|
|
"panels": []interface{}{
|
|
map[string]interface{}{
|
|
"id": int64(1),
|
|
"gridPos": map[string]interface{}{
|
|
"h": 6,
|
|
"w": 6,
|
|
"x": 0,
|
|
"y": 0,
|
|
},
|
|
},
|
|
map[string]interface{}{
|
|
"id": int64(2),
|
|
"gridPos": map[string]interface{}{
|
|
"h": 6,
|
|
"w": 6,
|
|
"x": 6,
|
|
"y": 0,
|
|
},
|
|
"libraryPanel": map[string]interface{}{
|
|
"uid": sc.initialResult.Result.UID,
|
|
"name": sc.initialResult.Result.Name,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
dash := models.Dashboard{
|
|
Title: "Testing getHandler",
|
|
Data: simplejson.NewFromAny(dashJSON),
|
|
}
|
|
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
|
|
err := sc.service.ConnectElementsToDashboard(sc.reqContext, []string{sc.initialResult.Result.UID}, dashInDB.Id)
|
|
require.NoError(t, err)
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID})
|
|
resp := sc.service.getHandler(sc.reqContext)
|
|
var result = validateAndUnMarshalResponse(t, resp)
|
|
var expected = libraryElementResult{
|
|
Result: libraryElement{
|
|
ID: 1,
|
|
OrgID: 1,
|
|
FolderID: 1,
|
|
UID: result.Result.UID,
|
|
Name: "Text - Library Panel",
|
|
Kind: int64(Panel),
|
|
Type: "text",
|
|
Description: "A description",
|
|
Model: map[string]interface{}{
|
|
"datasource": "${DS_GDEV-TESTDATA}",
|
|
"description": "A description",
|
|
"id": float64(1),
|
|
"title": "Text - Library Panel",
|
|
"type": "text",
|
|
},
|
|
Version: 1,
|
|
Meta: LibraryElementDTOMeta{
|
|
FolderName: "ScenarioFolder",
|
|
FolderUID: sc.folder.Uid,
|
|
ConnectedDashboards: 1,
|
|
Created: result.Result.Meta.Created,
|
|
Updated: result.Result.Meta.Updated,
|
|
CreatedBy: LibraryElementDTOMetaUser{
|
|
ID: 1,
|
|
Name: userInDbName,
|
|
AvatarURL: userInDbAvatar,
|
|
},
|
|
UpdatedBy: LibraryElementDTOMetaUser{
|
|
ID: 1,
|
|
Name: userInDbName,
|
|
AvatarURL: userInDbAvatar,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" {
|
|
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) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID})
|
|
sc.reqContext.SignedInUser.OrgId = 2
|
|
sc.reqContext.SignedInUser.OrgRole = models.ROLE_ADMIN
|
|
resp := sc.service.getHandler(sc.reqContext)
|
|
require.Equal(t, 404, resp.Status())
|
|
})
|
|
}
|