dashboard_acl_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package api
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/middleware"
  8. m "github.com/grafana/grafana/pkg/models"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestDashboardAclApiEndpoint(t *testing.T) {
  12. Convey("Given a dashboard acl", t, func() {
  13. mockResult := []*m.DashboardAclInfoDTO{
  14. {OrgId: 1, DashboardId: 1, UserId: 2, Permission: m.PERMISSION_VIEW},
  15. {OrgId: 1, DashboardId: 1, UserId: 3, Permission: m.PERMISSION_EDIT},
  16. {OrgId: 1, DashboardId: 1, UserId: 4, Permission: m.PERMISSION_ADMIN},
  17. {OrgId: 1, DashboardId: 1, TeamId: 1, Permission: m.PERMISSION_VIEW},
  18. {OrgId: 1, DashboardId: 1, TeamId: 2, Permission: m.PERMISSION_ADMIN},
  19. }
  20. dtoRes := transformDashboardAclsToDTOs(mockResult)
  21. getDashboardQueryResult := m.NewDashboard("Dash")
  22. var getDashboardNotFoundError error
  23. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  24. query.Result = getDashboardQueryResult
  25. return getDashboardNotFoundError
  26. })
  27. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  28. query.Result = dtoRes
  29. return nil
  30. })
  31. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  32. query.Result = mockResult
  33. return nil
  34. })
  35. teamResp := []*m.Team{}
  36. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  37. query.Result = teamResp
  38. return nil
  39. })
  40. // This tests four scenarios:
  41. // 1. user is an org admin
  42. // 2. user is an org editor AND has been granted admin permission for the dashboard
  43. // 3. user is an org viewer AND has been granted edit permission for the dashboard
  44. // 4. user is an org editor AND has no permissions for the dashboard
  45. Convey("When user is org admin", func() {
  46. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_ADMIN, func(sc *scenarioContext) {
  47. Convey("Should be able to access ACL", func() {
  48. sc.handlerFunc = GetDashboardAclList
  49. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  50. So(sc.resp.Code, ShouldEqual, 200)
  51. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  52. So(err, ShouldBeNil)
  53. So(len(respJSON.MustArray()), ShouldEqual, 5)
  54. So(respJSON.GetIndex(0).Get("userId").MustInt(), ShouldEqual, 2)
  55. So(respJSON.GetIndex(0).Get("permission").MustInt(), ShouldEqual, m.PERMISSION_VIEW)
  56. })
  57. })
  58. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_ADMIN, func(sc *scenarioContext) {
  59. getDashboardNotFoundError = m.ErrDashboardNotFound
  60. sc.handlerFunc = GetDashboardAclList
  61. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  62. Convey("Should not be able to access ACL", func() {
  63. So(sc.resp.Code, ShouldEqual, 404)
  64. })
  65. })
  66. Convey("Should not be able to update permissions for non-existing dashboard", func() {
  67. cmd := dtos.UpdateDashboardAclCommand{
  68. Items: []dtos.DashboardAclUpdateItem{
  69. {UserId: 1000, Permission: m.PERMISSION_ADMIN},
  70. },
  71. }
  72. postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_ADMIN, cmd, func(sc *scenarioContext) {
  73. getDashboardNotFoundError = m.ErrDashboardNotFound
  74. CallPostAcl(sc)
  75. So(sc.resp.Code, ShouldEqual, 404)
  76. })
  77. })
  78. })
  79. Convey("When user is org editor and has admin permission in the ACL", func() {
  80. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  81. mockResult = append(mockResult, &m.DashboardAclInfoDTO{OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  82. Convey("Should be able to access ACL", func() {
  83. sc.handlerFunc = GetDashboardAclList
  84. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  85. So(sc.resp.Code, ShouldEqual, 200)
  86. })
  87. })
  88. Convey("Should not be able to downgrade their own Admin permission", func() {
  89. cmd := dtos.UpdateDashboardAclCommand{
  90. Items: []dtos.DashboardAclUpdateItem{
  91. {UserId: TestUserID, Permission: m.PERMISSION_EDIT},
  92. },
  93. }
  94. postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  95. mockResult = append(mockResult, &m.DashboardAclInfoDTO{OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  96. CallPostAcl(sc)
  97. So(sc.resp.Code, ShouldEqual, 403)
  98. })
  99. })
  100. Convey("Should be able to update permissions", func() {
  101. cmd := dtos.UpdateDashboardAclCommand{
  102. Items: []dtos.DashboardAclUpdateItem{
  103. {UserId: TestUserID, Permission: m.PERMISSION_ADMIN},
  104. {UserId: 2, Permission: m.PERMISSION_EDIT},
  105. },
  106. }
  107. postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  108. mockResult = append(mockResult, &m.DashboardAclInfoDTO{OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  109. CallPostAcl(sc)
  110. So(sc.resp.Code, ShouldEqual, 200)
  111. })
  112. })
  113. })
  114. Convey("When user is org viewer and has edit permission in the ACL", func() {
  115. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_VIEWER, func(sc *scenarioContext) {
  116. mockResult = append(mockResult, &m.DashboardAclInfoDTO{OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
  117. // Getting the permissions is an Admin permission
  118. Convey("Should not be able to get list of permissions from ACL", func() {
  119. sc.handlerFunc = GetDashboardAclList
  120. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  121. So(sc.resp.Code, ShouldEqual, 403)
  122. })
  123. })
  124. })
  125. Convey("When user is org editor and not in the ACL", func() {
  126. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  127. Convey("Should not be able to access ACL", func() {
  128. sc.handlerFunc = GetDashboardAclList
  129. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  130. So(sc.resp.Code, ShouldEqual, 403)
  131. })
  132. })
  133. })
  134. })
  135. }
  136. func transformDashboardAclsToDTOs(acls []*m.DashboardAclInfoDTO) []*m.DashboardAclInfoDTO {
  137. dtos := make([]*m.DashboardAclInfoDTO, 0)
  138. for _, acl := range acls {
  139. dto := &m.DashboardAclInfoDTO{
  140. OrgId: acl.OrgId,
  141. DashboardId: acl.DashboardId,
  142. Permission: acl.Permission,
  143. UserId: acl.UserId,
  144. TeamId: acl.TeamId,
  145. }
  146. dtos = append(dtos, dto)
  147. }
  148. return dtos
  149. }
  150. func CallPostAcl(sc *scenarioContext) {
  151. bus.AddHandler("test", func(cmd *m.UpdateDashboardAclCommand) error {
  152. return nil
  153. })
  154. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  155. }
  156. func postAclScenario(desc string, url string, routePattern string, role m.RoleType, cmd dtos.UpdateDashboardAclCommand, fn scenarioFunc) {
  157. Convey(desc+" "+url, func() {
  158. defer bus.ClearBusHandlers()
  159. sc := setupScenarioContext(url)
  160. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  161. sc.context = c
  162. sc.context.UserId = TestUserID
  163. sc.context.OrgId = TestOrgID
  164. sc.context.OrgRole = role
  165. return UpdateDashboardAcl(c, cmd)
  166. })
  167. sc.m.Post(routePattern, sc.defaultHandler)
  168. fn(sc)
  169. })
  170. }