mirror of
https://github.com/grafana/grafana.git
synced 2024-12-02 05:29:42 -06:00
583b94557b
* Refactor: adds permissions for library panel creation * Refactor: checks folder permissions for patch requests * Chore: changes after PR comments * Refactor: adds permissions to delete * Refactor: moves get all permission tests out of get all tests * Chore: move out get all tests to a separate file * Refactor: adds permissions to get handler * Refactor: fixes a bug with getting library panels in General folder * Refactor: adds permissions for connect/disconnect * Refactor: adds permissions and tests for get connected dashboards * Tests: adds tests for connected dashboards in General Folder
98 lines
4.4 KiB
Go
98 lines
4.4 KiB
Go
package librarypanels
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestConnectLibraryPanel(t *testing.T) {
|
|
scenarioWithLibraryPanel(t, "When an admin tries to create a connection for a library panel that does not exist, it should fail",
|
|
func(t *testing.T, sc scenarioContext) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown", ":dashboardId": "1"})
|
|
resp := sc.service.connectHandler(sc.reqContext)
|
|
require.Equal(t, 404, resp.Status())
|
|
})
|
|
|
|
scenarioWithLibraryPanel(t, "When an admin tries to create a connection that already exists, it should succeed",
|
|
func(t *testing.T, sc scenarioContext) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": "1"})
|
|
resp := sc.service.connectHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
resp = sc.service.connectHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
})
|
|
}
|
|
|
|
func TestDisconnectLibraryPanel(t *testing.T) {
|
|
scenarioWithLibraryPanel(t, "When an admin tries to remove a connection with a library panel that does not exist, it should fail",
|
|
func(t *testing.T, sc scenarioContext) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown", ":dashboardId": "1"})
|
|
resp := sc.service.disconnectHandler(sc.reqContext)
|
|
require.Equal(t, 404, resp.Status())
|
|
})
|
|
|
|
scenarioWithLibraryPanel(t, "When an admin tries to remove a connection that does not exist, it should fail",
|
|
func(t *testing.T, sc scenarioContext) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": "1"})
|
|
resp := sc.service.disconnectHandler(sc.reqContext)
|
|
require.Equal(t, 404, resp.Status())
|
|
})
|
|
|
|
scenarioWithLibraryPanel(t, "When an admin tries to remove a connection that does exist, it should succeed",
|
|
func(t *testing.T, sc scenarioContext) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": "1"})
|
|
resp := sc.service.connectHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
resp = sc.service.disconnectHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
})
|
|
}
|
|
|
|
func TestGetConnectedDashboards(t *testing.T) {
|
|
scenarioWithLibraryPanel(t, "When an admin tries to get connected dashboards for 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.getConnectedDashboardsHandler(sc.reqContext)
|
|
require.Equal(t, 404, resp.Status())
|
|
})
|
|
|
|
scenarioWithLibraryPanel(t, "When an admin tries to get connected dashboards for a library panel that exists, but has no connections, it should return none",
|
|
func(t *testing.T, sc scenarioContext) {
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID})
|
|
resp := sc.service.getConnectedDashboardsHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
var dashResult libraryPanelDashboardsResult
|
|
err := json.Unmarshal(resp.Body(), &dashResult)
|
|
require.NoError(t, err)
|
|
require.Equal(t, 0, len(dashResult.Result))
|
|
})
|
|
|
|
scenarioWithLibraryPanel(t, "When an admin tries to get connected dashboards for a library panel that exists and has connections, it should return connected dashboard IDs",
|
|
func(t *testing.T, sc scenarioContext) {
|
|
firstDash := createDashboard(t, sc.user, "Dash 1", 0)
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": strconv.FormatInt(firstDash.Id, 10)})
|
|
resp := sc.service.connectHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
secondDash := createDashboard(t, sc.user, "Dash 2", 0)
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": strconv.FormatInt(secondDash.Id, 10)})
|
|
resp = sc.service.connectHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID})
|
|
resp = sc.service.getConnectedDashboardsHandler(sc.reqContext)
|
|
require.Equal(t, 200, resp.Status())
|
|
|
|
var dashResult libraryPanelDashboardsResult
|
|
err := json.Unmarshal(resp.Body(), &dashResult)
|
|
require.NoError(t, err)
|
|
require.Equal(t, 2, len(dashResult.Result))
|
|
require.Equal(t, firstDash.Id, dashResult.Result[0])
|
|
require.Equal(t, secondDash.Id, dashResult.Result[1])
|
|
})
|
|
}
|