From 466462de37fba9913fcaa34e0d15914cc4b37ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Wed, 24 Feb 2021 14:06:22 +0100 Subject: [PATCH] LibraryPanels: Adds permissions to getAllHandler (#31416) * LibraryPanels: Adds permissions to getAllHandler * Chore: adds a test to verify the permissions * Chore: tests refactor --- pkg/services/librarypanels/api.go | 2 +- pkg/services/librarypanels/database.go | 22 +- .../librarypanels/librarypanels_test.go | 946 +++++++----------- pkg/services/sqlstore/alert.go | 2 +- pkg/services/sqlstore/dashboard.go | 4 +- pkg/services/sqlstore/sqlbuilder.go | 6 +- pkg/services/sqlstore/sqlbuilder_test.go | 4 +- 7 files changed, 412 insertions(+), 574 deletions(-) diff --git a/pkg/services/librarypanels/api.go b/pkg/services/librarypanels/api.go index b60f28f3f494..b393d45e61c7 100644 --- a/pkg/services/librarypanels/api.go +++ b/pkg/services/librarypanels/api.go @@ -97,7 +97,7 @@ func (lps *LibraryPanelService) getHandler(c *models.ReqContext) response.Respon // getAllHandler handles GET /api/library-panels/. func (lps *LibraryPanelService) getAllHandler(c *models.ReqContext) response.Response { - libraryPanels, err := lps.getAllLibraryPanels(c) + libraryPanels, err := lps.getAllLibraryPanels(c, c.QueryInt64("limit")) if err != nil { return response.Error(500, "Failed to get library panels", err) } diff --git a/pkg/services/librarypanels/database.go b/pkg/services/librarypanels/database.go index 6d3256e98dea..8382d2094f76 100644 --- a/pkg/services/librarypanels/database.go +++ b/pkg/services/librarypanels/database.go @@ -14,7 +14,8 @@ import ( var ( sqlStatmentLibrayPanelDTOWithMeta = ` -SELECT lp.id, lp.org_id, lp.folder_id, lp.uid, lp.name, lp.model, lp.created, lp.created_by, lp.updated, lp.updated_by +SELECT DISTINCT + lp.id, lp.org_id, lp.folder_id, lp.uid, lp.name, lp.model, lp.created, lp.created_by, lp.updated, lp.updated_by , 0 AS can_edit , u1.login AS created_by_name , u1.email AS created_by_email @@ -275,14 +276,21 @@ func (lps *LibraryPanelService) getLibraryPanel(c *models.ReqContext, uid string } // getAllLibraryPanels gets all library panels. -func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext) ([]LibraryPanelDTO, error) { - orgID := c.SignedInUser.OrgId +func (lps *LibraryPanelService) getAllLibraryPanels(c *models.ReqContext, limit int64) ([]LibraryPanelDTO, error) { libraryPanels := make([]LibraryPanelWithMeta, 0) err := lps.SQLStore.WithDbSession(c.Context.Req.Context(), func(session *sqlstore.DBSession) error { - sql := sqlStatmentLibrayPanelDTOWithMeta + "WHERE lp.org_id=?" - sess := session.SQL(sql, orgID) - err := sess.Find(&libraryPanels) - if err != nil { + builder := sqlstore.SQLBuilder{} + builder.Write(sqlStatmentLibrayPanelDTOWithMeta) + builder.Write(" LEFT JOIN dashboard AS dashboard on lp.folder_id = dashboard.id") + builder.Write(` WHERE lp.org_id = ?`, c.SignedInUser.OrgId) + if c.SignedInUser.OrgRole != models.ROLE_ADMIN { + builder.WriteDashboardPermissionFilter(c.SignedInUser, models.PERMISSION_VIEW) + } + if limit == 0 { + limit = 1000 + } + builder.Write(lps.SQLStore.Dialect.Limit(limit)) + if err := session.SQL(builder.GetSQLString(), builder.GetParams()...).Find(&libraryPanels); err != nil { return err } diff --git a/pkg/services/librarypanels/librarypanels_test.go b/pkg/services/librarypanels/librarypanels_test.go index 82bfbc77c28b..e08999b60196 100644 --- a/pkg/services/librarypanels/librarypanels_test.go +++ b/pkg/services/librarypanels/librarypanels_test.go @@ -12,39 +12,32 @@ import ( "github.com/stretchr/testify/require" "gopkg.in/macaron.v1" + "github.com/grafana/grafana/pkg/api/response" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/registry" + "github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/services/sqlstore" "github.com/grafana/grafana/pkg/setting" ) func TestCreateLibraryPanel(t *testing.T) { - testScenario(t, "When an admin tries to create a library panel that already exists, it should fail", + scenarioWithLibraryPanel(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()) - - response = sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 400, response.Status()) + command := getCreateCommand(sc.folder.Id, "Text - Library Panel") + resp := sc.service.createHandler(sc.reqContext, command) + require.Equal(t, 400, resp.Status()) }) - testScenario(t, "When an admin tries to create a library panel that does not exists, it should succeed", + scenarioWithLibraryPanel(t, "When an admin tries to create a library panel that does not exists, it should succeed", 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) var expected = libraryPanelResult{ Result: libraryPanel{ ID: 1, OrgID: 1, FolderID: 1, - UID: result.Result.UID, + UID: sc.initialResult.Result.UID, Name: "Text - Library Panel", Model: map[string]interface{}{ "datasource": "${DS_GDEV-TESTDATA}", @@ -55,8 +48,8 @@ func TestCreateLibraryPanel(t *testing.T) { Meta: LibraryPanelDTOMeta{ CanEdit: true, ConnectedDashboards: 0, - Created: result.Result.Meta.Created, - Updated: result.Result.Meta.Updated, + Created: sc.initialResult.Result.Meta.Created, + Updated: sc.initialResult.Result.Meta.Updated, CreatedBy: LibraryPanelDTOMetaUser{ ID: 1, Name: "signed_in_user", @@ -70,7 +63,7 @@ func TestCreateLibraryPanel(t *testing.T) { }, }, } - if diff := cmp.Diff(expected, result, getCompareOptions()...); diff != "" { + if diff := cmp.Diff(expected, sc.initialResult, getCompareOptions()...); diff != "" { t.Fatalf("Result mismatch (-want +got):\n%s", diff) } }) @@ -78,12 +71,8 @@ func TestCreateLibraryPanel(t *testing.T) { testScenario(t, "When an admin tries to create a library panel where name and panel title differ, it should update panel title", func(t *testing.T, sc scenarioContext) { command := getCreateCommand(1, "Library Panel Name") - 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) + resp := sc.service.createHandler(sc.reqContext, command) + var result = validateAndUnMarshalResponse(t, resp) var expected = libraryPanelResult{ Result: libraryPanel{ ID: 1, @@ -122,136 +111,86 @@ func TestCreateLibraryPanel(t *testing.T) { } func TestConnectLibraryPanel(t *testing.T) { - testScenario(t, "When an admin tries to create a connection for a library panel that does not exist, it should fail", + 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"}) - response := sc.service.connectHandler(sc.reqContext) - require.Equal(t, 404, response.Status()) + resp := sc.service.connectHandler(sc.reqContext) + require.Equal(t, 404, resp.Status()) }) - testScenario(t, "When an admin tries to create a connection that already exists, it should succeed", + scenarioWithLibraryPanel(t, "When an admin tries to create a connection that already exists, it should succeed", 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": sc.initialResult.Result.UID, ":dashboardId": "1"}) + resp := sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) - var result libraryPanelResult - err := json.Unmarshal(response.Body(), &result) - require.NoError(t, err) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp = sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) }) } func TestDeleteLibraryPanel(t *testing.T) { - testScenario(t, "When an admin tries to delete a library panel that does not exist, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to delete a library panel that does not exist, it should fail", func(t *testing.T, sc scenarioContext) { - response := sc.service.deleteHandler(sc.reqContext) - require.Equal(t, 404, response.Status()) + resp := sc.service.deleteHandler(sc.reqContext) + require.Equal(t, 404, resp.Status()) }) - testScenario(t, "When an admin tries to delete a library panel that exists, it should succeed", + scenarioWithLibraryPanel(t, "When an admin tries to delete a library panel that exists, it should succeed", 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) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID}) - response = sc.service.deleteHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID}) + resp := sc.service.deleteHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) }) - testScenario(t, "When an admin tries to delete a library panel in another org, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to delete a library panel in another 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) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID}) + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.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()) + resp := sc.service.deleteHandler(sc.reqContext) + require.Equal(t, 404, resp.Status()) }) } func TestDisconnectLibraryPanel(t *testing.T) { - testScenario(t, "When an admin tries to remove a connection with a library panel that does not exist, it should fail", + 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"}) - response := sc.service.disconnectHandler(sc.reqContext) - require.Equal(t, 404, response.Status()) + resp := sc.service.disconnectHandler(sc.reqContext) + require.Equal(t, 404, resp.Status()) }) - testScenario(t, "When an admin tries to remove a connection that does not exist, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to remove a connection that does not exist, 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) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) - response = sc.service.disconnectHandler(sc.reqContext) - require.Equal(t, 404, response.Status()) + 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()) }) - testScenario(t, "When an admin tries to remove a connection that does exist, it should succeed", + scenarioWithLibraryPanel(t, "When an admin tries to remove a connection that does exist, it should succeed", 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) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - response = sc.service.disconnectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + 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 TestGetLibraryPanel(t *testing.T) { - testScenario(t, "When an admin tries to get a library panel that does not exist, it should fail", + scenarioWithLibraryPanel(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()) + resp := sc.service.getHandler(sc.reqContext) + require.Equal(t, 404, resp.Status()) }) - testScenario(t, "When an admin tries to get a library panel that exists, it should succeed and return correct result", + scenarioWithLibraryPanel(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) { - 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.getHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - err = json.Unmarshal(response.Body(), &result) - 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 = libraryPanelResult{ Result: libraryPanel{ ID: 1, @@ -288,45 +227,28 @@ func TestGetLibraryPanel(t *testing.T) { } }) - testScenario(t, "When an admin tries to get a library panel that exists in an other org, it should fail", + scenarioWithLibraryPanel(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) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID}) + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.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()) + resp := sc.service.getHandler(sc.reqContext) + require.Equal(t, 404, resp.Status()) }) - testScenario(t, "When an admin tries to get a library panel with 2 connected dashboards, it should succeed and return correct connected dashboards", + scenarioWithLibraryPanel(t, "When an admin tries to get a library panel with 2 connected dashboards, it should succeed and return correct connected dashboards", 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) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "2"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - - 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) + 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()) + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": "2"}) + 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.getHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) + var result = validateAndUnMarshalResponse(t, resp) require.Equal(t, int64(2), result.Result.Meta.ConnectedDashboards) }) } @@ -334,31 +256,27 @@ func TestGetLibraryPanel(t *testing.T) { func TestGetAllLibraryPanels(t *testing.T) { testScenario(t, "When an admin tries to get all library panels and none exists, it should return none", func(t *testing.T, sc scenarioContext) { - response := sc.service.getAllHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp := sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) var result libraryPanelsResult - err := json.Unmarshal(response.Body(), &result) + err := json.Unmarshal(resp.Body(), &result) require.NoError(t, err) require.NotNil(t, result.Result) require.Equal(t, 0, len(result.Result)) }) - testScenario(t, "When an admin tries to get all library panels and two exist, it should succeed", + scenarioWithLibraryPanel(t, "When an admin tries to get all library panels and two exist, it should succeed", 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()) - - command = getCreateCommand(1, "Text - Library Panel2") - response = sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) + command := getCreateCommand(sc.folder.Id, "Text - Library Panel2") + resp := sc.service.createHandler(sc.reqContext, command) + require.Equal(t, 200, resp.Status()) - response = sc.service.getAllHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp = sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) var result libraryPanelsResult - err := json.Unmarshal(response.Body(), &result) + err := json.Unmarshal(resp.Body(), &result) require.NoError(t, err) var expected = libraryPanelsResult{ Result: []libraryPanel{ @@ -427,49 +345,37 @@ func TestGetAllLibraryPanels(t *testing.T) { } }) - testScenario(t, "When an admin tries to get all library panels and two exist but only one is connected, it should succeed and return correct connected dashboards", + scenarioWithLibraryPanel(t, "When an admin tries to get all library panels and two exist but only one is connected, it should succeed and return correct connected dashboards", 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()) - - command = getCreateCommand(1, "Text - Library Panel2") - 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) + command := getCreateCommand(sc.folder.Id, "Text - Library Panel2") + resp := sc.service.createHandler(sc.reqContext, command) + require.Equal(t, 200, resp.Status()) + var result = validateAndUnMarshalResponse(t, resp) sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp = sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "2"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp = sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) - response = sc.service.getAllHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp = sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) var results libraryPanelsResult - err = json.Unmarshal(response.Body(), &results) + err := json.Unmarshal(resp.Body(), &results) require.NoError(t, err) require.Equal(t, int64(0), results.Result[0].Meta.ConnectedDashboards) require.Equal(t, int64(2), results.Result[1].Meta.ConnectedDashboards) }) - testScenario(t, "When an admin tries to get all library panels in a different org, none should be returned", + scenarioWithLibraryPanel(t, "When an admin tries to get all library panels in a different org, none should be returned", 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()) - - response = sc.service.getAllHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp := sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) var result libraryPanelsResult - err := json.Unmarshal(response.Body(), &result) + err := json.Unmarshal(resp.Body(), &result) require.NoError(t, err) require.Equal(t, 1, len(result.Result)) require.Equal(t, int64(1), result.Result[0].FolderID) @@ -477,11 +383,64 @@ func TestGetAllLibraryPanels(t *testing.T) { sc.reqContext.SignedInUser.OrgId = 2 sc.reqContext.SignedInUser.OrgRole = models.ROLE_ADMIN - response = sc.service.getAllHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp = sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) result = libraryPanelsResult{} - err = json.Unmarshal(response.Body(), &result) + err = json.Unmarshal(resp.Body(), &result) + require.NoError(t, err) + require.NotNil(t, result.Result) + require.Equal(t, 0, len(result.Result)) + }) + + testScenario(t, "When an user tries to get all library panels, library panels in folders where the user has no access should not be returned", + func(t *testing.T, sc scenarioContext) { + updateFolderACL(t, sc, models.ROLE_EDITOR, models.PERMISSION_EDIT) + + command := getCreateCommand(sc.folder.Id, "Editor - Library Panel") + resp := sc.service.createHandler(sc.reqContext, command) + require.Equal(t, 200, resp.Status()) + + cmd := models.CreateFolderCommand{ + Uid: "AdminOnlyFolder", + Title: "Admin Only Folder", + } + createFolder(t, &sc, &cmd) + updateFolderACL(t, sc, models.ROLE_ADMIN, models.PERMISSION_ADMIN) + + command = getCreateCommand(sc.folder.Id, "Admin - Library Panel") + resp = sc.service.createHandler(sc.reqContext, command) + require.Equal(t, 200, resp.Status()) + + resp = sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) + + var result libraryPanelsResult + err := json.Unmarshal(resp.Body(), &result) + require.NoError(t, err) + require.Equal(t, 2, len(result.Result)) + require.Equal(t, int64(1), result.Result[0].FolderID) + require.Equal(t, int64(2), cmd.Result.Id) + require.Equal(t, "Editor - Library Panel", result.Result[0].Name) + require.Equal(t, "Admin - Library Panel", result.Result[1].Name) + + sc.reqContext.SignedInUser.OrgRole = models.ROLE_EDITOR + resp = sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) + + result = libraryPanelsResult{} + err = json.Unmarshal(resp.Body(), &result) + require.NoError(t, err) + require.Equal(t, 1, len(result.Result)) + require.Equal(t, int64(1), result.Result[0].FolderID) + require.Equal(t, "Editor - Library Panel", result.Result[0].Name) + + sc.reqContext.SignedInUser.OrgRole = models.ROLE_VIEWER + resp = sc.service.getAllHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) + + result = libraryPanelsResult{} + err = json.Unmarshal(resp.Body(), &result) require.NoError(t, err) require.NotNil(t, result.Result) require.Equal(t, 0, len(result.Result)) @@ -489,56 +448,40 @@ func TestGetAllLibraryPanels(t *testing.T) { } func TestGetConnectedDashboards(t *testing.T) { - testScenario(t, "When an admin tries to get connected dashboards for a library panel that does not exist, it should fail", + 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"}) - response := sc.service.getConnectedDashboardsHandler(sc.reqContext) - require.Equal(t, 404, response.Status()) + resp := sc.service.getConnectedDashboardsHandler(sc.reqContext) + require.Equal(t, 404, resp.Status()) }) - testScenario(t, "When an admin tries to get connected dashboards for a library panel that exists, but has no connections, it should return none", + 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) { - 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.getConnectedDashboardsHandler(sc.reqContext) - require.Equal(t, 200, response.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(response.Body(), &dashResult) + err := json.Unmarshal(resp.Body(), &dashResult) require.NoError(t, err) require.Equal(t, 0, len(dashResult.Result)) }) - testScenario(t, "When an admin tries to get connected dashboards for a library panel that exists and has connections, it should return connected dashboard IDs", + 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) { - 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": sc.initialResult.Result.UID, ":dashboardId": "11"}) + resp := sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": "12"}) + resp = sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "11"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "12"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID}) - response = sc.service.getConnectedDashboardsHandler(sc.reqContext) - require.Equal(t, 200, response.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(response.Body(), &dashResult) + err := json.Unmarshal(resp.Body(), &dashResult) require.NoError(t, err) require.Equal(t, 2, len(dashResult.Result)) require.Equal(t, int64(11), dashResult.Result[0]) @@ -547,30 +490,22 @@ func TestGetConnectedDashboards(t *testing.T) { } func TestPatchLibraryPanel(t *testing.T) { - testScenario(t, "When an admin tries to patch a library panel that does not exist, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel that does not exist, it should fail", func(t *testing.T, sc scenarioContext) { cmd := patchLibraryPanelCommand{} sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown"}) - response := sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 404, response.Status()) + resp := sc.service.patchHandler(sc.reqContext, cmd) + require.Equal(t, 404, resp.Status()) }) - testScenario(t, "When an admin tries to patch a library panel that exists, it should succeed", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel that exists, it should succeed", 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 existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "2"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + 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()) + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": "2"}) + resp = sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) cmd := patchLibraryPanelCommand{ FolderID: 2, @@ -584,19 +519,16 @@ func TestPatchLibraryPanel(t *testing.T) { } `), } - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - 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": sc.initialResult.Result.UID}) + resp = sc.service.patchHandler(sc.reqContext, cmd) + require.Equal(t, 200, resp.Status()) + var result = validateAndUnMarshalResponse(t, resp) var expected = libraryPanelResult{ Result: libraryPanel{ ID: 1, OrgID: 1, FolderID: 2, - UID: existing.Result.UID, + UID: sc.initialResult.Result.UID, Name: "Panel - New name", Model: map[string]interface{}{ "datasource": "${DS_GDEV-TESTDATA}", @@ -607,7 +539,7 @@ func TestPatchLibraryPanel(t *testing.T) { Meta: LibraryPanelDTOMeta{ CanEdit: true, ConnectedDashboards: 2, - Created: existing.Result.Meta.Created, + Created: sc.initialResult.Result.Meta.Created, Updated: result.Result.Meta.Updated, CreatedBy: LibraryPanelDTOMetaUser{ ID: 1, @@ -627,198 +559,122 @@ func TestPatchLibraryPanel(t *testing.T) { } }) - testScenario(t, "When an admin tries to patch a library panel with folder only, it should change folder successfully and return correct result", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel with folder only, it should change folder successfully and return correct result", 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 existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - cmd := patchLibraryPanelCommand{ FolderID: 100, } - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 200, response.Status()) - - var result libraryPanelResult - err = json.Unmarshal(response.Body(), &result) - require.NoError(t, err) - existing.Result.FolderID = int64(100) - existing.Result.Meta.CreatedBy.Name = "user_in_db" - existing.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" - if diff := cmp.Diff(existing.Result, result.Result, getCompareOptions()...); diff != "" { + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID}) + resp := sc.service.patchHandler(sc.reqContext, cmd) + require.Equal(t, 200, resp.Status()) + var result = validateAndUnMarshalResponse(t, resp) + sc.initialResult.Result.FolderID = int64(100) + sc.initialResult.Result.Meta.CreatedBy.Name = "user_in_db" + sc.initialResult.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" + if diff := cmp.Diff(sc.initialResult.Result, result.Result, getCompareOptions()...); diff != "" { t.Fatalf("Result mismatch (-want +got):\n%s", diff) } }) - testScenario(t, "When an admin tries to patch a library panel with name only, it should change name successfully, sync title and return correct result", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel with name only, it should change name successfully, sync title and return correct result", 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 existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - cmd := patchLibraryPanelCommand{ Name: "New Name", } - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 200, response.Status()) - - var result libraryPanelResult - err = json.Unmarshal(response.Body(), &result) - require.NoError(t, err) - - existing.Result.Name = "New Name" - existing.Result.Meta.CreatedBy.Name = "user_in_db" - existing.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" - existing.Result.Model["title"] = "New Name" - if diff := cmp.Diff(existing.Result, result.Result, getCompareOptions()...); diff != "" { + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID}) + resp := sc.service.patchHandler(sc.reqContext, cmd) + var result = validateAndUnMarshalResponse(t, resp) + sc.initialResult.Result.Name = "New Name" + sc.initialResult.Result.Meta.CreatedBy.Name = "user_in_db" + sc.initialResult.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" + sc.initialResult.Result.Model["title"] = "New Name" + if diff := cmp.Diff(sc.initialResult.Result, result.Result, getCompareOptions()...); diff != "" { t.Fatalf("Result mismatch (-want +got):\n%s", diff) } }) - testScenario(t, "When an admin tries to patch a library panel with model only, it should change model successfully and return correct result", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel with model only, it should change model successfully and return correct result", 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 existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - cmd := patchLibraryPanelCommand{ Model: []byte(`{ "title": "New Model Title", "name": "New Model Name" }`), } - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 200, response.Status()) - var result libraryPanelResult - err = json.Unmarshal(response.Body(), &result) - require.NoError(t, err) - existing.Result.Model = map[string]interface{}{ + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID}) + resp := sc.service.patchHandler(sc.reqContext, cmd) + var result = validateAndUnMarshalResponse(t, resp) + sc.initialResult.Result.Model = map[string]interface{}{ "title": "Text - Library Panel", "name": "New Model Name", } - existing.Result.Meta.CreatedBy.Name = "user_in_db" - existing.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" - if diff := cmp.Diff(existing.Result, result.Result, getCompareOptions()...); diff != "" { + sc.initialResult.Result.Meta.CreatedBy.Name = "user_in_db" + sc.initialResult.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" + if diff := cmp.Diff(sc.initialResult.Result, result.Result, getCompareOptions()...); diff != "" { t.Fatalf("Result mismatch (-want +got):\n%s", diff) } }) - testScenario(t, "When another admin tries to patch a library panel, it should change UpdatedBy successfully and return correct result", + scenarioWithLibraryPanel(t, "When another admin tries to patch a library panel, it should change UpdatedBy successfully and return correct result", 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 existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - cmd := patchLibraryPanelCommand{} sc.reqContext.UserId = 2 - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 200, response.Status()) - var result libraryPanelResult - err = json.Unmarshal(response.Body(), &result) - require.NoError(t, err) - existing.Result.Meta.UpdatedBy.ID = int64(2) - existing.Result.Meta.CreatedBy.Name = "user_in_db" - existing.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" - if diff := cmp.Diff(existing.Result, result.Result, getCompareOptions()...); diff != "" { + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID}) + resp := sc.service.patchHandler(sc.reqContext, cmd) + var result = validateAndUnMarshalResponse(t, resp) + sc.initialResult.Result.Meta.UpdatedBy.ID = int64(2) + sc.initialResult.Result.Meta.CreatedBy.Name = "user_in_db" + sc.initialResult.Result.Meta.CreatedBy.AvatarUrl = "/avatar/402d08de060496d6b6874495fe20f5ad" + if diff := cmp.Diff(sc.initialResult.Result, result.Result, getCompareOptions()...); diff != "" { t.Fatalf("Result mismatch (-want +got):\n%s", diff) } }) - testScenario(t, "When an admin tries to patch a library panel with a name that already exists, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel with a name that already exists, it should fail", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Existing") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - 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) - + command := getCreateCommand(sc.folder.Id, "Another Panel") + resp := sc.service.createHandler(sc.reqContext, command) + var result = validateAndUnMarshalResponse(t, resp) cmd := patchLibraryPanelCommand{ - Name: "Existing", + Name: "Text - Library Panel", } sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 400, response.Status()) + resp = sc.service.patchHandler(sc.reqContext, cmd) + require.Equal(t, 400, resp.Status()) }) - testScenario(t, "When an admin tries to patch a library panel with a folder where a library panel with the same name already exists, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel with a folder where a library panel with the same name already exists, it should fail", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(2, "Text - Library Panel") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - 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) - + createFolder(t, &sc, &models.CreateFolderCommand{ + Uid: "NewTestFolder", + Title: "New Test Folder", + }) + command := getCreateCommand(sc.folder.Id, "Text - Library Panel") + resp := sc.service.createHandler(sc.reqContext, command) + var result = validateAndUnMarshalResponse(t, resp) cmd := patchLibraryPanelCommand{ - FolderID: 2, + FolderID: 1, } sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 400, response.Status()) + resp = sc.service.patchHandler(sc.reqContext, cmd) + require.Equal(t, 400, resp.Status()) }) - testScenario(t, "When an admin tries to patch a library panel in another org, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to patch a library panel in another 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) - cmd := patchLibraryPanelCommand{ - FolderID: 2, + FolderID: sc.folder.Id, } sc.reqContext.OrgId = 2 - sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID}) - response = sc.service.patchHandler(sc.reqContext, cmd) - require.Equal(t, 404, response.Status()) + sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID}) + resp := sc.service.patchHandler(sc.reqContext, cmd) + require.Equal(t, 404, resp.Status()) }) } func TestLoadLibraryPanelsForDashboard(t *testing.T) { - testScenario(t, "When an admin tries to load a dashboard with a library panel, it should copy JSON properties from library panel", + scenarioWithLibraryPanel(t, "When an admin tries to load a dashboard with a library panel, it should copy JSON properties from library panel", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + 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()) dashJSON := map[string]interface{}{ "panels": []interface{}{ @@ -840,8 +696,8 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { "y": 0, }, "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, }, }, @@ -851,7 +707,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.LoadLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.LoadLibraryPanelsForDashboard(sc.reqContext, &dash) require.NoError(t, err) expectedJSON := map[string]interface{}{ "panels": []interface{}{ @@ -874,26 +730,26 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, "meta": map[string]interface{}{ "canEdit": false, "connectedDashboards": int64(1), - "created": existing.Result.Meta.Created, - "updated": existing.Result.Meta.Updated, + "created": sc.initialResult.Result.Meta.Created, + "updated": sc.initialResult.Result.Meta.Updated, "createdBy": map[string]interface{}{ - "id": existing.Result.Meta.CreatedBy.ID, + "id": sc.initialResult.Result.Meta.CreatedBy.ID, "name": "user_in_db", "avatarUrl": "/avatar/402d08de060496d6b6874495fe20f5ad", }, "updatedBy": map[string]interface{}{ - "id": existing.Result.Meta.UpdatedBy.ID, + "id": sc.initialResult.Result.Meta.UpdatedBy.ID, "name": "user_in_db", "avatarUrl": "/avatar/402d08de060496d6b6874495fe20f5ad", }, }, }, - "title": "Text - Library Panel1", + "title": "Text - Library Panel", "type": "text", }, }, @@ -904,19 +760,11 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { } }) - testScenario(t, "When an admin tries to load a dashboard with a library panel without uid, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to load a dashboard with a library panel without uid, it should fail", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + 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()) dashJSON := map[string]interface{}{ "panels": []interface{}{ @@ -938,7 +786,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { "y": 0, }, "libraryPanel": map[string]interface{}{ - "name": existing.Result.Name, + "name": sc.initialResult.Result.Name, }, }, }, @@ -948,20 +796,12 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.LoadLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.LoadLibraryPanelsForDashboard(sc.reqContext, &dash) require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) }) - testScenario(t, "When an admin tries to load a dashboard with a library panel that is not connected, it should set correct JSON and continue", + scenarioWithLibraryPanel(t, "When an admin tries to load a dashboard with a library panel that is not connected, it should set correct JSON and continue", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - dashJSON := map[string]interface{}{ "panels": []interface{}{ map[string]interface{}{ @@ -982,8 +822,8 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { "y": 0, }, "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, }, }, @@ -993,7 +833,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.LoadLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.LoadLibraryPanelsForDashboard(sc.reqContext, &dash) require.NoError(t, err) expectedJSON := map[string]interface{}{ "panels": []interface{}{ @@ -1015,10 +855,10 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { "y": 0, }, "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, - "type": fmt.Sprintf("Name: \"%s\", UID: \"%s\"", existing.Result.Name, existing.Result.UID), + "type": fmt.Sprintf("Name: \"%s\", UID: \"%s\"", sc.initialResult.Result.Name, sc.initialResult.Result.UID), }, }, } @@ -1030,16 +870,8 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { } func TestCleanLibraryPanelsForDashboard(t *testing.T) { - testScenario(t, "When an admin tries to store a dashboard with a library panel, it should just keep the correct JSON properties in library panel", + scenarioWithLibraryPanel(t, "When an admin tries to store a dashboard with a library panel, it should just keep the correct JSON properties in library panel", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - dashJSON := map[string]interface{}{ "panels": []interface{}{ map[string]interface{}{ @@ -1061,8 +893,8 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, "title": "Text - Library Panel", "type": "text", @@ -1074,7 +906,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.CleanLibraryPanelsForDashboard(&dash) + err := sc.service.CleanLibraryPanelsForDashboard(&dash) require.NoError(t, err) expectedJSON := map[string]interface{}{ "panels": []interface{}{ @@ -1096,8 +928,8 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { "y": 0, }, "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, }, }, @@ -1108,16 +940,8 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { } }) - testScenario(t, "When an admin tries to store a dashboard with a library panel without uid, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to store a dashboard with a library panel without uid, it should fail", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - dashJSON := map[string]interface{}{ "panels": []interface{}{ map[string]interface{}{ @@ -1139,7 +963,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "name": existing.Result.Name, + "name": sc.initialResult.Result.Name, }, "title": "Text - Library Panel", "type": "text", @@ -1151,20 +975,12 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.CleanLibraryPanelsForDashboard(&dash) + err := sc.service.CleanLibraryPanelsForDashboard(&dash) require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) }) - testScenario(t, "When an admin tries to store a dashboard with a library panel without name, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to store a dashboard with a library panel without name, it should fail", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - dashJSON := map[string]interface{}{ "panels": []interface{}{ map[string]interface{}{ @@ -1186,7 +1002,7 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, + "uid": sc.initialResult.Result.UID, }, "title": "Text - Library Panel", "type": "text", @@ -1198,22 +1014,14 @@ func TestCleanLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.CleanLibraryPanelsForDashboard(&dash) + err := sc.service.CleanLibraryPanelsForDashboard(&dash) require.EqualError(t, err, errLibraryPanelHeaderNameMissing.Error()) }) } func TestConnectLibraryPanelsForDashboard(t *testing.T) { - testScenario(t, "When an admin tries to store a dashboard with a library panel, it should connect the two", + scenarioWithLibraryPanel(t, "When an admin tries to store a dashboard with a library panel, it should connect the two", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - dashJSON := map[string]interface{}{ "panels": []interface{}{ map[string]interface{}{ @@ -1235,8 +1043,8 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, "title": "Text - Library Panel", "type": "text", @@ -1248,30 +1056,22 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) require.NoError(t, err) - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.getConnectedDashboardsHandler(sc.reqContext) - require.Equal(t, 200, response.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(response.Body(), &dashResult) + err = json.Unmarshal(resp.Body(), &dashResult) require.NoError(t, err) require.Len(t, dashResult.Result, 1) require.Equal(t, int64(1), dashResult.Result[0]) }) - testScenario(t, "When an admin tries to store a dashboard with a library panel without uid, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to store a dashboard with a library panel without uid, it should fail", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - dashJSON := map[string]interface{}{ "panels": []interface{}{ map[string]interface{}{ @@ -1293,7 +1093,7 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "name": existing.Result.Name, + "name": sc.initialResult.Result.Name, }, "title": "Text - Library Panel", "type": "text", @@ -1305,31 +1105,18 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) }) - testScenario(t, "When an admin tries to store a dashboard with unusused/removed library panels, it should disconnect unusused/removed library panels", + scenarioWithLibraryPanel(t, "When an admin tries to store a dashboard with unused/removed library panels, it should disconnect unused/removed library panels", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Unused Libray Panel") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var unused libraryPanelResult - err := json.Unmarshal(response.Body(), &unused) - require.NoError(t, err) - + command := getCreateCommand(sc.folder.Id, "Unused Libray Panel") + resp := sc.service.createHandler(sc.reqContext, command) + var unused = validateAndUnMarshalResponse(t, resp) sc.reqContext.ReplaceAllParams(map[string]string{":uid": unused.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) - - command = getCreateCommand(1, "Text - Library Panel1") - response = sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err = json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) + resp = sc.service.connectHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) dashJSON := map[string]interface{}{ "panels": []interface{}{ @@ -1352,8 +1139,8 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, "title": "Text - Library Panel", "type": "text", @@ -1365,44 +1152,36 @@ func TestConnectLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) require.NoError(t, err) - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.getConnectedDashboardsHandler(sc.reqContext) - require.Equal(t, 200, response.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 existingResult libraryPanelDashboardsResult - err = json.Unmarshal(response.Body(), &existingResult) + err = json.Unmarshal(resp.Body(), &existingResult) require.NoError(t, err) require.Len(t, existingResult.Result, 1) require.Equal(t, int64(1), existingResult.Result[0]) sc.reqContext.ReplaceAllParams(map[string]string{":uid": unused.Result.UID}) - response = sc.service.getConnectedDashboardsHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + resp = sc.service.getConnectedDashboardsHandler(sc.reqContext) + require.Equal(t, 200, resp.Status()) var unusedResult libraryPanelDashboardsResult - err = json.Unmarshal(response.Body(), &unusedResult) + err = json.Unmarshal(resp.Body(), &unusedResult) require.NoError(t, err) require.Len(t, unusedResult.Result, 0) }) } func TestDisconnectLibraryPanelsForDashboard(t *testing.T) { - testScenario(t, "When an admin tries to delete a dashboard with a library panel, it should disconnect the two", + scenarioWithLibraryPanel(t, "When an admin tries to delete a dashboard with a library panel, it should disconnect the two", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + 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()) dashJSON := map[string]interface{}{ "panels": []interface{}{ @@ -1425,8 +1204,8 @@ func TestDisconnectLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "uid": existing.Result.UID, - "name": existing.Result.Name, + "uid": sc.initialResult.Result.UID, + "name": sc.initialResult.Result.Name, }, "title": "Text - Library Panel", "type": "text", @@ -1438,32 +1217,24 @@ func TestDisconnectLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.DisconnectLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.DisconnectLibraryPanelsForDashboard(sc.reqContext, &dash) require.NoError(t, err) - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) - response = sc.service.getConnectedDashboardsHandler(sc.reqContext) - require.Equal(t, 200, response.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(response.Body(), &dashResult) + err = json.Unmarshal(resp.Body(), &dashResult) require.NoError(t, err) require.Empty(t, dashResult.Result) }) - testScenario(t, "When an admin tries to delete a dashboard with a library panel without uid, it should fail", + scenarioWithLibraryPanel(t, "When an admin tries to delete a dashboard with a library panel without uid, it should fail", func(t *testing.T, sc scenarioContext) { - command := getCreateCommand(1, "Text - Library Panel1") - response := sc.service.createHandler(sc.reqContext, command) - require.Equal(t, 200, response.Status()) - - var existing libraryPanelResult - err := json.Unmarshal(response.Body(), &existing) - require.NoError(t, err) - - sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) - response = sc.service.connectHandler(sc.reqContext) - require.Equal(t, 200, response.Status()) + 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()) dashJSON := map[string]interface{}{ "panels": []interface{}{ @@ -1486,7 +1257,7 @@ func TestDisconnectLibraryPanelsForDashboard(t *testing.T) { }, "datasource": "${DS_GDEV-TESTDATA}", "libraryPanel": map[string]interface{}{ - "name": existing.Result.Name, + "name": sc.initialResult.Result.Name, }, "title": "Text - Library Panel", "type": "text", @@ -1498,7 +1269,7 @@ func TestDisconnectLibraryPanelsForDashboard(t *testing.T) { Data: simplejson.NewFromAny(dashJSON), } - err = sc.service.DisconnectLibraryPanelsForDashboard(sc.reqContext, &dash) + err := sc.service.DisconnectLibraryPanelsForDashboard(sc.reqContext, &dash) require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) }) } @@ -1564,10 +1335,56 @@ func getCreateCommand(folderID int64, name string) createLibraryPanelCommand { } type scenarioContext struct { - ctx *macaron.Context - service *LibraryPanelService - reqContext *models.ReqContext - user models.SignedInUser + ctx *macaron.Context + service *LibraryPanelService + reqContext *models.ReqContext + user models.SignedInUser + folder *models.Folder + initialResult libraryPanelResult +} + +func createFolder(t *testing.T, sc *scenarioContext, cmd *models.CreateFolderCommand) { + s := dashboards.NewFolderService(sc.user.OrgId, &sc.user) + err := s.CreateFolder(cmd) + require.NoError(t, err) + sc.folder = cmd.Result +} + +func updateFolderACL(t *testing.T, sc scenarioContext, roleType models.RoleType, permission models.PermissionType) { + cmd := models.UpdateDashboardAclCommand{ + DashboardID: sc.folder.Id, + Items: []*models.DashboardAcl{ + { + DashboardID: sc.folder.Id, + Role: &roleType, + Permission: permission, + Created: time.Now(), + Updated: time.Now(), + }, + }, + } + err := bus.Dispatch(&cmd) + require.NoError(t, err) +} + +func validateAndUnMarshalResponse(t *testing.T, resp response.Response) libraryPanelResult { + require.Equal(t, 200, resp.Status()) + + var result = libraryPanelResult{} + err := json.Unmarshal(resp.Body(), &result) + require.NoError(t, err) + + return result +} + +func scenarioWithLibraryPanel(t *testing.T, desc string, fn func(t *testing.T, sc scenarioContext)) { + testScenario(t, desc, func(t *testing.T, sc scenarioContext) { + command := getCreateCommand(sc.folder.Id, "Text - Library Panel") + resp := sc.service.createHandler(sc.reqContext, command) + sc.initialResult = validateAndUnMarshalResponse(t, resp) + + fn(t, sc) + }) } // testScenario is a wrapper around t.Run performing common setup for library panel tests. @@ -1604,6 +1421,7 @@ func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioCo OrgRole: role, LastSeenAt: time.Now(), } + // deliberate difference between signed in user and user in db to make it crystal clear // what to expect in the tests // In the real world these are identical @@ -1624,6 +1442,14 @@ func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioCo SignedInUser: &user, }, } + + folderCmd := models.CreateFolderCommand{ + Uid: "testFolder", + Title: "TestFolder", + } + + createFolder(t, &sc, &folderCmd) + fn(t, sc) }) } diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 39f6e4569fc0..12f955a9ac2e 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -125,7 +125,7 @@ func HandleAlertsQuery(query *models.GetAlertsQuery) error { } if query.User.OrgRole != models.ROLE_ADMIN { - builder.writeDashboardPermissionFilter(query.User, models.PERMISSION_VIEW) + builder.WriteDashboardPermissionFilter(query.User, models.PERMISSION_VIEW) } builder.Write(" ORDER BY name ASC") diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 81d458f0ba2b..12a4c52f2e7b 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -720,7 +720,7 @@ func HasEditPermissionInFolders(query *models.HasEditPermissionInFoldersQuery) e builder := &SQLBuilder{} builder.Write("SELECT COUNT(dashboard.id) AS count FROM dashboard WHERE dashboard.org_id = ? AND dashboard.is_folder = ?", query.SignedInUser.OrgId, dialect.BooleanStr(true)) - builder.writeDashboardPermissionFilter(query.SignedInUser, models.PERMISSION_EDIT) + builder.WriteDashboardPermissionFilter(query.SignedInUser, models.PERMISSION_EDIT) type folderCount struct { Count int64 @@ -744,7 +744,7 @@ func HasAdminPermissionInFolders(query *models.HasAdminPermissionInFoldersQuery) builder := &SQLBuilder{} builder.Write("SELECT COUNT(dashboard.id) AS count FROM dashboard WHERE dashboard.org_id = ? AND dashboard.is_folder = ?", query.SignedInUser.OrgId, dialect.BooleanStr(true)) - builder.writeDashboardPermissionFilter(query.SignedInUser, models.PERMISSION_ADMIN) + builder.WriteDashboardPermissionFilter(query.SignedInUser, models.PERMISSION_ADMIN) type folderCount struct { Count int64 diff --git a/pkg/services/sqlstore/sqlbuilder.go b/pkg/services/sqlstore/sqlbuilder.go index 083d336df3a7..0101a3052ca7 100644 --- a/pkg/services/sqlstore/sqlbuilder.go +++ b/pkg/services/sqlstore/sqlbuilder.go @@ -24,11 +24,15 @@ func (sb *SQLBuilder) GetSQLString() string { return sb.sql.String() } +func (sb *SQLBuilder) GetParams() []interface{} { + return sb.params +} + func (sb *SQLBuilder) AddParams(params ...interface{}) { sb.params = append(sb.params, params...) } -func (sb *SQLBuilder) writeDashboardPermissionFilter(user *models.SignedInUser, permission models.PermissionType) { +func (sb *SQLBuilder) WriteDashboardPermissionFilter(user *models.SignedInUser, permission models.PermissionType) { if user.OrgRole == models.ROLE_ADMIN { return } diff --git a/pkg/services/sqlstore/sqlbuilder_test.go b/pkg/services/sqlstore/sqlbuilder_test.go index bee11a8b5f02..71e0a72eb3db 100644 --- a/pkg/services/sqlstore/sqlbuilder_test.go +++ b/pkg/services/sqlstore/sqlbuilder_test.go @@ -15,7 +15,7 @@ import ( ) func TestSQLBuilder(t *testing.T) { - t.Run("writeDashboardPermissionFilter", func(t *testing.T) { + t.Run("WriteDashboardPermissionFilter", func(t *testing.T) { t.Run("user ACL", func(t *testing.T) { test(t, DashboardProps{}, @@ -340,7 +340,7 @@ func getDashboards(sqlStore *SQLStore, search Search, aclUserId int64) ([]*dashb var res []*dashboardResponse builder.Write("SELECT * FROM dashboard WHERE true") - builder.writeDashboardPermissionFilter(signedInUser, search.RequiredPermission) + builder.WriteDashboardPermissionFilter(signedInUser, search.RequiredPermission) err := sqlStore.engine.SQL(builder.GetSQLString(), builder.params...).Find(&res) return res, err }