dashboard_acl_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. {Id: 1, OrgId: 1, DashboardId: 1, UserId: 2, Permission: m.PERMISSION_VIEW},
  15. {Id: 2, OrgId: 1, DashboardId: 1, UserId: 3, Permission: m.PERMISSION_EDIT},
  16. {Id: 3, OrgId: 1, DashboardId: 1, UserId: 4, Permission: m.PERMISSION_ADMIN},
  17. {Id: 4, OrgId: 1, DashboardId: 1, TeamId: 1, Permission: m.PERMISSION_VIEW},
  18. {Id: 5, OrgId: 1, DashboardId: 1, TeamId: 2, Permission: m.PERMISSION_ADMIN},
  19. }
  20. dtoRes := transformDashboardAclsToDTOs(mockResult)
  21. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  22. query.Result = dtoRes
  23. return nil
  24. })
  25. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  26. query.Result = mockResult
  27. return nil
  28. })
  29. teamResp := []*m.Team{}
  30. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  31. query.Result = teamResp
  32. return nil
  33. })
  34. // This tests four scenarios:
  35. // 1. user is an org admin
  36. // 2. user is an org editor AND has been granted admin permission for the dashboard
  37. // 3. user is an org viewer AND has been granted edit permission for the dashboard
  38. // 4. user is an org editor AND has no permissions for the dashboard
  39. Convey("When user is org admin", func() {
  40. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_ADMIN, func(sc *scenarioContext) {
  41. Convey("Should be able to access ACL", func() {
  42. sc.handlerFunc = GetDashboardAclList
  43. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  44. So(sc.resp.Code, ShouldEqual, 200)
  45. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  46. So(err, ShouldBeNil)
  47. So(len(respJSON.MustArray()), ShouldEqual, 5)
  48. So(respJSON.GetIndex(0).Get("userId").MustInt(), ShouldEqual, 2)
  49. So(respJSON.GetIndex(0).Get("permission").MustInt(), ShouldEqual, m.PERMISSION_VIEW)
  50. })
  51. })
  52. })
  53. Convey("When user is org editor and has admin permission in the ACL", func() {
  54. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  55. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  56. Convey("Should be able to access ACL", func() {
  57. sc.handlerFunc = GetDashboardAclList
  58. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  59. So(sc.resp.Code, ShouldEqual, 200)
  60. })
  61. })
  62. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  63. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  64. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  65. return nil
  66. })
  67. Convey("Should be able to delete permission", func() {
  68. sc.handlerFunc = DeleteDashboardAcl
  69. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  70. So(sc.resp.Code, ShouldEqual, 200)
  71. })
  72. })
  73. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/6", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  74. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  75. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  76. return nil
  77. })
  78. Convey("Should not be able to delete their own Admin permission", func() {
  79. sc.handlerFunc = DeleteDashboardAcl
  80. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  81. So(sc.resp.Code, ShouldEqual, 403)
  82. })
  83. })
  84. Convey("Should not be able to downgrade their own Admin permission", func() {
  85. cmd := dtos.UpdateDashboardAclCommand{
  86. Items: []dtos.DashboardAclUpdateItem{
  87. {UserId: TestUserID, Permission: m.PERMISSION_EDIT},
  88. },
  89. }
  90. postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  91. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  92. CallPostAcl(sc)
  93. So(sc.resp.Code, ShouldEqual, 403)
  94. })
  95. })
  96. Convey("Should be able to update permissions", func() {
  97. cmd := dtos.UpdateDashboardAclCommand{
  98. Items: []dtos.DashboardAclUpdateItem{
  99. {UserId: TestUserID, Permission: m.PERMISSION_ADMIN},
  100. {UserId: 2, Permission: m.PERMISSION_EDIT},
  101. },
  102. }
  103. postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  104. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  105. CallPostAcl(sc)
  106. So(sc.resp.Code, ShouldEqual, 200)
  107. })
  108. })
  109. Convey("When user is a member of a team in the ACL with admin permission", func() {
  110. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardsId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  111. teamResp = append(teamResp, &m.Team{Id: 2, OrgId: 1, Name: "UG2"})
  112. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  113. return nil
  114. })
  115. Convey("Should be able to delete permission", func() {
  116. sc.handlerFunc = DeleteDashboardAcl
  117. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  118. So(sc.resp.Code, ShouldEqual, 200)
  119. })
  120. })
  121. })
  122. })
  123. Convey("When user is org viewer and has edit permission in the ACL", func() {
  124. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_VIEWER, func(sc *scenarioContext) {
  125. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
  126. // Getting the permissions is an Admin permission
  127. Convey("Should not be able to get list of permissions from 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. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_VIEWER, func(sc *scenarioContext) {
  134. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
  135. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  136. return nil
  137. })
  138. Convey("Should be not be able to delete permission", func() {
  139. sc.handlerFunc = DeleteDashboardAcl
  140. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  141. So(sc.resp.Code, ShouldEqual, 403)
  142. })
  143. })
  144. })
  145. Convey("When user is org editor and not in the ACL", func() {
  146. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  147. Convey("Should not be able to access ACL", func() {
  148. sc.handlerFunc = GetDashboardAclList
  149. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  150. So(sc.resp.Code, ShouldEqual, 403)
  151. })
  152. })
  153. 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) {
  154. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_VIEW})
  155. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  156. return nil
  157. })
  158. Convey("Should be not be able to delete permission", func() {
  159. sc.handlerFunc = DeleteDashboardAcl
  160. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  161. So(sc.resp.Code, ShouldEqual, 403)
  162. })
  163. })
  164. })
  165. })
  166. }
  167. func transformDashboardAclsToDTOs(acls []*m.DashboardAclInfoDTO) []*m.DashboardAclInfoDTO {
  168. dtos := make([]*m.DashboardAclInfoDTO, 0)
  169. for _, acl := range acls {
  170. dto := &m.DashboardAclInfoDTO{
  171. Id: acl.Id,
  172. OrgId: acl.OrgId,
  173. DashboardId: acl.DashboardId,
  174. Permission: acl.Permission,
  175. UserId: acl.UserId,
  176. TeamId: acl.TeamId,
  177. }
  178. dtos = append(dtos, dto)
  179. }
  180. return dtos
  181. }
  182. func CallPostAcl(sc *scenarioContext) {
  183. bus.AddHandler("test", func(cmd *m.UpdateDashboardAclCommand) error {
  184. return nil
  185. })
  186. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  187. }
  188. func postAclScenario(desc string, url string, routePattern string, role m.RoleType, cmd dtos.UpdateDashboardAclCommand, fn scenarioFunc) {
  189. Convey(desc+" "+url, func() {
  190. defer bus.ClearBusHandlers()
  191. sc := setupScenarioContext(url)
  192. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  193. sc.context = c
  194. sc.context.UserId = TestUserID
  195. sc.context.OrgId = TestOrgID
  196. sc.context.OrgRole = role
  197. return UpdateDashboardAcl(c, cmd)
  198. })
  199. sc.m.Post(routePattern, sc.defaultHandler)
  200. fn(sc)
  201. })
  202. }