dashboard_acl_test.go 5.3 KB

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