dashboard_acl_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package api
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "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 := []*m.DashboardAclInfoDTO{
  12. {Id: 1, OrgId: 1, DashboardId: 1, UserId: 2, Permission: m.PERMISSION_VIEW},
  13. {Id: 2, OrgId: 1, DashboardId: 1, UserId: 3, Permission: m.PERMISSION_EDIT},
  14. {Id: 3, OrgId: 1, DashboardId: 1, UserId: 4, Permission: m.PERMISSION_ADMIN},
  15. {Id: 4, OrgId: 1, DashboardId: 1, TeamId: 1, Permission: m.PERMISSION_VIEW},
  16. {Id: 5, OrgId: 1, DashboardId: 1, TeamId: 2, Permission: m.PERMISSION_ADMIN},
  17. }
  18. dtoRes := transformDashboardAclsToDTOs(mockResult)
  19. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  20. query.Result = dtoRes
  21. return nil
  22. })
  23. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  24. query.Result = mockResult
  25. return nil
  26. })
  27. teamResp := []*m.Team{}
  28. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  29. query.Result = teamResp
  30. return nil
  31. })
  32. Convey("When user is org admin", func() {
  33. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_ADMIN, func(sc *scenarioContext) {
  34. Convey("Should be able to access ACL", func() {
  35. sc.handlerFunc = GetDashboardAclList
  36. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  37. So(sc.resp.Code, ShouldEqual, 200)
  38. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  39. So(err, ShouldBeNil)
  40. So(len(respJSON.MustArray()), ShouldEqual, 5)
  41. So(respJSON.GetIndex(0).Get("userId").MustInt(), ShouldEqual, 2)
  42. So(respJSON.GetIndex(0).Get("permission").MustInt(), ShouldEqual, m.PERMISSION_VIEW)
  43. })
  44. })
  45. })
  46. Convey("When user is editor and has admin permission in the ACL", func() {
  47. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  48. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  49. Convey("Should be able to access ACL", func() {
  50. sc.handlerFunc = GetDashboardAclList
  51. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  52. So(sc.resp.Code, ShouldEqual, 200)
  53. })
  54. })
  55. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  56. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  57. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  58. return nil
  59. })
  60. Convey("Should be able to delete permission", func() {
  61. sc.handlerFunc = DeleteDashboardAcl
  62. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  63. So(sc.resp.Code, ShouldEqual, 200)
  64. })
  65. })
  66. Convey("When user is a member of a team in the ACL with admin permission", func() {
  67. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardsId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  68. teamResp = append(teamResp, &m.Team{Id: 2, OrgId: 1, Name: "UG2"})
  69. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  70. return nil
  71. })
  72. Convey("Should be able to delete permission", func() {
  73. sc.handlerFunc = DeleteDashboardAcl
  74. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  75. So(sc.resp.Code, ShouldEqual, 200)
  76. })
  77. })
  78. })
  79. })
  80. Convey("When user is editor and has edit permission in the ACL", func() {
  81. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  82. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
  83. Convey("Should not be able to access ACL", func() {
  84. sc.handlerFunc = GetDashboardAclList
  85. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  86. So(sc.resp.Code, ShouldEqual, 403)
  87. })
  88. })
  89. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  90. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
  91. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  92. return nil
  93. })
  94. Convey("Should be not be able to delete permission", func() {
  95. sc.handlerFunc = DeleteDashboardAcl
  96. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  97. So(sc.resp.Code, ShouldEqual, 403)
  98. })
  99. })
  100. })
  101. Convey("When user is editor and not in the ACL", func() {
  102. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  103. Convey("Should not be able to access ACL", func() {
  104. sc.handlerFunc = GetDashboardAclList
  105. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  106. So(sc.resp.Code, ShouldEqual, 403)
  107. })
  108. })
  109. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/user/1", "/api/dashboards/id/:dashboardsId/acl/user/:userId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  110. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_VIEW})
  111. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  112. return nil
  113. })
  114. Convey("Should be not be able to delete permission", func() {
  115. sc.handlerFunc = DeleteDashboardAcl
  116. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  117. So(sc.resp.Code, ShouldEqual, 403)
  118. })
  119. })
  120. })
  121. })
  122. }
  123. func transformDashboardAclsToDTOs(acls []*m.DashboardAclInfoDTO) []*m.DashboardAclInfoDTO {
  124. dtos := make([]*m.DashboardAclInfoDTO, 0)
  125. for _, acl := range acls {
  126. dto := &m.DashboardAclInfoDTO{
  127. Id: acl.Id,
  128. OrgId: acl.OrgId,
  129. DashboardId: acl.DashboardId,
  130. Permission: acl.Permission,
  131. UserId: acl.UserId,
  132. TeamId: acl.TeamId,
  133. }
  134. dtos = append(dtos, dto)
  135. }
  136. return dtos
  137. }