dashboard_acl_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 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, Permissions: 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. Convey("Should not be able to access ACL", func() {
  78. sc.handlerFunc = GetDashboardAcl
  79. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  80. So(sc.resp.Code, ShouldEqual, 403)
  81. })
  82. })
  83. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/1/acl/user/1", "/api/dashboards/:id/acl/user/:userId", models.ROLE_EDITOR, func(sc *scenarioContext) {
  84. mockResult = append(mockResult, &models.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permissions: models.PERMISSION_VIEW})
  85. bus.AddHandler("test3", func(cmd *models.RemoveDashboardPermissionCommand) error {
  86. return nil
  87. })
  88. Convey("Should be not be able to delete permission", func() {
  89. sc.handlerFunc = DeleteDashboardAclByUser
  90. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  91. So(sc.resp.Code, ShouldEqual, 403)
  92. })
  93. })
  94. })
  95. })
  96. }