dashboard_acl_test.go 5.0 KB

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