|
@@ -3,8 +3,10 @@ package api
|
|
|
import (
|
|
import (
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
+ "github.com/grafana/grafana/pkg/api/dtos"
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
|
+ "github.com/grafana/grafana/pkg/middleware"
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
@@ -37,6 +39,12 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
|
|
|
return nil
|
|
return nil
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ // This tests four scenarios:
|
|
|
|
|
+ // 1. user is an org admin
|
|
|
|
|
+ // 2. user is an org editor AND has been granted admin permission for the dashboard
|
|
|
|
|
+ // 3. user is an org viewer AND has been granted edit permission for the dashboard
|
|
|
|
|
+ // 4. user is an org editor AND has no permissions for the dashboard
|
|
|
|
|
+
|
|
|
Convey("When user is org admin", func() {
|
|
Convey("When user is org admin", func() {
|
|
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_ADMIN, func(sc *scenarioContext) {
|
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_ADMIN, func(sc *scenarioContext) {
|
|
|
Convey("Should be able to access ACL", func() {
|
|
Convey("Should be able to access ACL", func() {
|
|
@@ -54,9 +62,9 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- Convey("When user is editor and has admin permission in the ACL", func() {
|
|
|
|
|
|
|
+ Convey("When user is org editor and has admin permission in the ACL", func() {
|
|
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
|
- mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
|
|
|
|
|
|
|
+ mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
|
|
|
|
|
|
|
|
Convey("Should be able to access ACL", func() {
|
|
Convey("Should be able to access ACL", func() {
|
|
|
sc.handlerFunc = GetDashboardAclList
|
|
sc.handlerFunc = GetDashboardAclList
|
|
@@ -67,7 +75,7 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
|
- mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
|
|
|
|
|
|
|
+ mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
|
|
|
|
|
|
|
|
bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
|
|
bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
|
|
|
return nil
|
|
return nil
|
|
@@ -81,6 +89,52 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/6", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
|
|
|
+ mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
|
|
|
|
|
+
|
|
|
|
|
+ bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ Convey("Should not be able to delete their own Admin permission", func() {
|
|
|
|
|
+ sc.handlerFunc = DeleteDashboardAcl
|
|
|
|
|
+ sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
|
|
|
|
|
+
|
|
|
|
|
+ So(sc.resp.Code, ShouldEqual, 403)
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ Convey("Should not be able to downgrade their own Admin permission", func() {
|
|
|
|
|
+ cmd := dtos.UpdateDashboardAclCommand{
|
|
|
|
|
+ Items: []dtos.DashboardAclUpdateItem{
|
|
|
|
|
+ {UserId: TestUserID, Permission: m.PERMISSION_EDIT},
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
|
|
|
|
|
+ mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
|
|
|
|
|
+
|
|
|
|
|
+ CallPostAcl(sc)
|
|
|
|
|
+ So(sc.resp.Code, ShouldEqual, 403)
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ Convey("Should be able to update permissions", func() {
|
|
|
|
|
+ cmd := dtos.UpdateDashboardAclCommand{
|
|
|
|
|
+ Items: []dtos.DashboardAclUpdateItem{
|
|
|
|
|
+ {UserId: TestUserID, Permission: m.PERMISSION_ADMIN},
|
|
|
|
|
+ {UserId: 2, Permission: m.PERMISSION_EDIT},
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
|
|
|
|
|
+ mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
|
|
|
|
|
+
|
|
|
|
|
+ CallPostAcl(sc)
|
|
|
|
|
+ So(sc.resp.Code, ShouldEqual, 200)
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
Convey("When user is a member of a team in the ACL with admin permission", func() {
|
|
Convey("When user is a member of a team in the ACL with admin permission", func() {
|
|
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardsId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardsId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
|
teamResp = append(teamResp, &m.Team{Id: 2, OrgId: 1, Name: "UG2"})
|
|
teamResp = append(teamResp, &m.Team{Id: 2, OrgId: 1, Name: "UG2"})
|
|
@@ -99,11 +153,12 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- Convey("When user is editor and has edit permission in the ACL", func() {
|
|
|
|
|
- loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
|
|
|
|
|
+ Convey("When user is org viewer and has edit permission in the ACL", func() {
|
|
|
|
|
+ loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_VIEWER, func(sc *scenarioContext) {
|
|
|
mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
|
|
mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
|
|
|
|
|
|
|
|
- Convey("Should not be able to access ACL", func() {
|
|
|
|
|
|
|
+ // Getting the permissions is an Admin permission
|
|
|
|
|
+ Convey("Should not be able to get list of permissions from ACL", func() {
|
|
|
sc.handlerFunc = GetDashboardAclList
|
|
sc.handlerFunc = GetDashboardAclList
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
@@ -111,7 +166,7 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
|
|
|
|
|
+ loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_VIEWER, func(sc *scenarioContext) {
|
|
|
mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
|
|
mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
|
|
|
|
|
|
|
|
bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
|
|
bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
|
|
@@ -127,7 +182,7 @@ func TestDashboardAclApiEndpoint(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- Convey("When user is editor and not in the ACL", func() {
|
|
|
|
|
|
|
+ Convey("When user is org editor and not in the ACL", func() {
|
|
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
|
|
|
|
|
|
|
|
Convey("Should not be able to access ACL", func() {
|
|
Convey("Should not be able to access ACL", func() {
|
|
@@ -172,3 +227,32 @@ func transformDashboardAclsToDTOs(acls []*m.DashboardAclInfoDTO) []*m.DashboardA
|
|
|
|
|
|
|
|
return dtos
|
|
return dtos
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func CallPostAcl(sc *scenarioContext) {
|
|
|
|
|
+ bus.AddHandler("test", func(cmd *m.UpdateDashboardAclCommand) error {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func postAclScenario(desc string, url string, routePattern string, role m.RoleType, cmd dtos.UpdateDashboardAclCommand, fn scenarioFunc) {
|
|
|
|
|
+ Convey(desc+" "+url, func() {
|
|
|
|
|
+ defer bus.ClearBusHandlers()
|
|
|
|
|
+
|
|
|
|
|
+ sc := setupScenarioContext(url)
|
|
|
|
|
+
|
|
|
|
|
+ sc.defaultHandler = wrap(func(c *middleware.Context) Response {
|
|
|
|
|
+ sc.context = c
|
|
|
|
|
+ sc.context.UserId = TestUserID
|
|
|
|
|
+ sc.context.OrgId = TestOrgID
|
|
|
|
|
+ sc.context.OrgRole = role
|
|
|
|
|
+
|
|
|
|
|
+ return UpdateDashboardAcl(c, cmd)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ sc.m.Post(routePattern, sc.defaultHandler)
|
|
|
|
|
+
|
|
|
|
|
+ fn(sc)
|
|
|
|
|
+ })
|
|
|
|
|
+}
|