dashboard_acl_test.go 9.2 KB

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