dashboard_acl_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package api
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestDashboardAclApiEndpoint(t *testing.T) {
  10. Convey("Given a dashboard acl", t, func() {
  11. mockResult := []*models.DashboardAclInfoDTO{
  12. {Id: 1, OrgId: 1, DashboardId: 1, UserId: 2, PermissionType: models.PERMISSION_EDIT},
  13. {Id: 2, OrgId: 1, DashboardId: 1, UserId: 3, PermissionType: models.PERMISSION_VIEW},
  14. {Id: 3, OrgId: 1, DashboardId: 1, UserGroupId: 1, PermissionType: models.PERMISSION_EDIT},
  15. {Id: 4, OrgId: 1, DashboardId: 1, UserGroupId: 2, PermissionType: models.PERMISSION_READ_ONLY_EDIT},
  16. }
  17. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  18. query.Result = mockResult
  19. return nil
  20. })
  21. Convey("When user is org admin", func() {
  22. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/1/acl", "/api/dashboards/:id/acl", models.ROLE_ADMIN, func(sc *scenarioContext) {
  23. Convey("Should be able to access ACL", func() {
  24. sc.handlerFunc = GetDashboardAcl
  25. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  26. So(sc.resp.Code, ShouldEqual, 200)
  27. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  28. So(err, ShouldBeNil)
  29. So(respJSON.GetIndex(0).Get("userId").MustInt(), ShouldEqual, 2)
  30. So(respJSON.GetIndex(0).Get("permissionType").MustInt(), ShouldEqual, models.PERMISSION_EDIT)
  31. })
  32. })
  33. })
  34. Convey("When user is editor and in the ACL", func() {
  35. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/1/acl", "/api/dashboards/:id/acl", models.ROLE_EDITOR, func(sc *scenarioContext) {
  36. mockResult = append(mockResult, &models.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, PermissionType: models.PERMISSION_EDIT})
  37. bus.AddHandler("test2", func(query *models.GetAllowedDashboardsQuery) error {
  38. query.Result = []int64{1}
  39. return nil
  40. })
  41. Convey("Should be able to access ACL", func() {
  42. sc.handlerFunc = GetDashboardAcl
  43. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  44. So(sc.resp.Code, ShouldEqual, 200)
  45. })
  46. })
  47. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/1/acl/user/1", "/api/dashboards/:id/acl/user/:userId", models.ROLE_EDITOR, func(sc *scenarioContext) {
  48. mockResult = append(mockResult, &models.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, PermissionType: models.PERMISSION_EDIT})
  49. bus.AddHandler("test3", func(cmd *models.RemoveDashboardPermissionCommand) error {
  50. return nil
  51. })
  52. Convey("Should be able to delete permission", func() {
  53. sc.handlerFunc = DeleteDashboardAclByUser
  54. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  55. So(sc.resp.Code, ShouldEqual, 200)
  56. })
  57. })
  58. Convey("When user is a member of a user group in the ACL with edit permission", func() {
  59. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/1/acl/user/1", "/api/dashboards/:id/acl/user/:userId", models.ROLE_EDITOR, func(sc *scenarioContext) {
  60. bus.AddHandler("test3", func(query *models.GetUserGroupsByUserQuery) error {
  61. query.Result = []*models.UserGroup{{Id: 1, OrgId: 1, Name: "UG1"}}
  62. return nil
  63. })
  64. bus.AddHandler("test3", func(cmd *models.RemoveDashboardPermissionCommand) error {
  65. return nil
  66. })
  67. Convey("Should be able to delete permission", func() {
  68. sc.handlerFunc = DeleteDashboardAclByUser
  69. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  70. So(sc.resp.Code, ShouldEqual, 200)
  71. })
  72. })
  73. })
  74. })
  75. Convey("When user is editor and not in the ACL", func() {
  76. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/1/acl", "/api/dashboards/:id/acl", models.ROLE_EDITOR, func(sc *scenarioContext) {
  77. bus.AddHandler("test2", func(query *models.GetAllowedDashboardsQuery) error {
  78. query.Result = []int64{}
  79. return nil
  80. })
  81. Convey("Should not be able to access ACL", func() {
  82. sc.handlerFunc = GetDashboardAcl
  83. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  84. So(sc.resp.Code, ShouldEqual, 403)
  85. })
  86. })
  87. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/1/acl/user/1", "/api/dashboards/:id/acl/user/:userId", models.ROLE_EDITOR, func(sc *scenarioContext) {
  88. mockResult = append(mockResult, &models.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, PermissionType: models.PERMISSION_VIEW})
  89. bus.AddHandler("test3", func(cmd *models.RemoveDashboardPermissionCommand) error {
  90. return nil
  91. })
  92. Convey("Should be not be able to delete permission", func() {
  93. sc.handlerFunc = DeleteDashboardAclByUser
  94. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  95. So(sc.resp.Code, ShouldEqual, 403)
  96. })
  97. })
  98. })
  99. })
  100. }