dashboard_acl_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. 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. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/2/acl/6", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_ADMIN, func(sc *scenarioContext) {
  79. getDashboardNotFoundError = m.ErrDashboardNotFound
  80. sc.handlerFunc = DeleteDashboardAcl
  81. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  82. Convey("Should not be able to delete non-existing dashboard", func() {
  83. So(sc.resp.Code, ShouldEqual, 404)
  84. })
  85. })
  86. })
  87. Convey("When user is org editor and has admin permission in the ACL", func() {
  88. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  89. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  90. Convey("Should be able to access ACL", func() {
  91. sc.handlerFunc = GetDashboardAclList
  92. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  93. So(sc.resp.Code, ShouldEqual, 200)
  94. })
  95. })
  96. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  97. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  98. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  99. return nil
  100. })
  101. Convey("Should be able to delete permission", func() {
  102. sc.handlerFunc = DeleteDashboardAcl
  103. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  104. So(sc.resp.Code, ShouldEqual, 200)
  105. })
  106. })
  107. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/6", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  108. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  109. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  110. return nil
  111. })
  112. Convey("Should not be able to delete their own Admin permission", func() {
  113. sc.handlerFunc = DeleteDashboardAcl
  114. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  115. So(sc.resp.Code, ShouldEqual, 403)
  116. })
  117. })
  118. Convey("Should not be able to downgrade their own Admin permission", func() {
  119. cmd := dtos.UpdateDashboardAclCommand{
  120. Items: []dtos.DashboardAclUpdateItem{
  121. {UserId: TestUserID, Permission: m.PERMISSION_EDIT},
  122. },
  123. }
  124. postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  125. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  126. CallPostAcl(sc)
  127. So(sc.resp.Code, ShouldEqual, 403)
  128. })
  129. })
  130. Convey("Should be able to update permissions", func() {
  131. cmd := dtos.UpdateDashboardAclCommand{
  132. Items: []dtos.DashboardAclUpdateItem{
  133. {UserId: TestUserID, Permission: m.PERMISSION_ADMIN},
  134. {UserId: 2, Permission: m.PERMISSION_EDIT},
  135. },
  136. }
  137. postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  138. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 6, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_ADMIN})
  139. CallPostAcl(sc)
  140. So(sc.resp.Code, ShouldEqual, 200)
  141. })
  142. })
  143. Convey("When user is a member of a team in the ACL with admin permission", func() {
  144. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardsId/acl/:aclId", m.ROLE_EDITOR, func(sc *scenarioContext) {
  145. teamResp = append(teamResp, &m.Team{Id: 2, OrgId: 1, Name: "UG2"})
  146. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  147. return nil
  148. })
  149. Convey("Should be able to delete permission", func() {
  150. sc.handlerFunc = DeleteDashboardAcl
  151. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  152. So(sc.resp.Code, ShouldEqual, 200)
  153. })
  154. })
  155. })
  156. })
  157. Convey("When user is org viewer and has edit permission in the ACL", func() {
  158. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_VIEWER, func(sc *scenarioContext) {
  159. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
  160. // Getting the permissions is an Admin permission
  161. Convey("Should not be able to get list of permissions from ACL", func() {
  162. sc.handlerFunc = GetDashboardAclList
  163. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  164. So(sc.resp.Code, ShouldEqual, 403)
  165. })
  166. })
  167. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/1/acl/1", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_VIEWER, func(sc *scenarioContext) {
  168. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_EDIT})
  169. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  170. return nil
  171. })
  172. Convey("Should be not be able to delete permission", func() {
  173. sc.handlerFunc = DeleteDashboardAcl
  174. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  175. So(sc.resp.Code, ShouldEqual, 403)
  176. })
  177. })
  178. })
  179. Convey("When user is org editor and not in the ACL", func() {
  180. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardsId/acl", m.ROLE_EDITOR, func(sc *scenarioContext) {
  181. Convey("Should not be able to access ACL", func() {
  182. sc.handlerFunc = GetDashboardAclList
  183. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  184. So(sc.resp.Code, ShouldEqual, 403)
  185. })
  186. })
  187. 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) {
  188. mockResult = append(mockResult, &m.DashboardAclInfoDTO{Id: 1, OrgId: 1, DashboardId: 1, UserId: 1, Permission: m.PERMISSION_VIEW})
  189. bus.AddHandler("test3", func(cmd *m.RemoveDashboardAclCommand) error {
  190. return nil
  191. })
  192. Convey("Should be not be able to delete permission", func() {
  193. sc.handlerFunc = DeleteDashboardAcl
  194. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  195. So(sc.resp.Code, ShouldEqual, 403)
  196. })
  197. })
  198. })
  199. })
  200. }
  201. func transformDashboardAclsToDTOs(acls []*m.DashboardAclInfoDTO) []*m.DashboardAclInfoDTO {
  202. dtos := make([]*m.DashboardAclInfoDTO, 0)
  203. for _, acl := range acls {
  204. dto := &m.DashboardAclInfoDTO{
  205. Id: acl.Id,
  206. OrgId: acl.OrgId,
  207. DashboardId: acl.DashboardId,
  208. Permission: acl.Permission,
  209. UserId: acl.UserId,
  210. TeamId: acl.TeamId,
  211. }
  212. dtos = append(dtos, dto)
  213. }
  214. return dtos
  215. }
  216. func CallPostAcl(sc *scenarioContext) {
  217. bus.AddHandler("test", func(cmd *m.UpdateDashboardAclCommand) error {
  218. return nil
  219. })
  220. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  221. }
  222. func postAclScenario(desc string, url string, routePattern string, role m.RoleType, cmd dtos.UpdateDashboardAclCommand, fn scenarioFunc) {
  223. Convey(desc+" "+url, func() {
  224. defer bus.ClearBusHandlers()
  225. sc := setupScenarioContext(url)
  226. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  227. sc.context = c
  228. sc.context.UserId = TestUserID
  229. sc.context.OrgId = TestOrgID
  230. sc.context.OrgRole = role
  231. return UpdateDashboardAcl(c, cmd)
  232. })
  233. sc.m.Post(routePattern, sc.defaultHandler)
  234. fn(sc)
  235. })
  236. }