dashboard_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package api
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/models"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestDashboardApiEndpoint(t *testing.T) {
  11. Convey("Given a dashboard with a parent folder which does not have an acl", t, func() {
  12. fakeDash := models.NewDashboard("Child dash")
  13. fakeDash.ParentId = 1
  14. fakeDash.HasAcl = false
  15. bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
  16. query.Result = fakeDash
  17. return nil
  18. })
  19. Convey("When user is an Org Viewer", func() {
  20. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", models.ROLE_VIEWER, func(sc *scenarioContext) {
  21. sc.handlerFunc = GetDashboard
  22. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  23. So(sc.resp.Code, ShouldEqual, 200)
  24. dash := dtos.DashboardFullWithMeta{}
  25. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  26. So(err, ShouldBeNil)
  27. Convey("Should not be able to edit or save dashboard", func() {
  28. So(dash.Meta.CanEdit, ShouldBeFalse)
  29. So(dash.Meta.CanSave, ShouldBeFalse)
  30. })
  31. })
  32. })
  33. Convey("When user is an Org Read Only Editor", func() {
  34. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", models.ROLE_READ_ONLY_EDITOR, func(sc *scenarioContext) {
  35. sc.handlerFunc = GetDashboard
  36. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  37. So(sc.resp.Code, ShouldEqual, 200)
  38. dash := dtos.DashboardFullWithMeta{}
  39. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  40. So(err, ShouldBeNil)
  41. Convey("Should be able to edit but not save the dashboard", func() {
  42. So(dash.Meta.CanEdit, ShouldBeTrue)
  43. So(dash.Meta.CanSave, ShouldBeFalse)
  44. })
  45. })
  46. })
  47. Convey("When user is an Org Editor", func() {
  48. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", models.ROLE_EDITOR, func(sc *scenarioContext) {
  49. sc.handlerFunc = GetDashboard
  50. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  51. So(sc.resp.Code, ShouldEqual, 200)
  52. dash := dtos.DashboardFullWithMeta{}
  53. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  54. So(err, ShouldBeNil)
  55. Convey("Should be able to edit or save dashboard", func() {
  56. So(dash.Meta.CanEdit, ShouldBeTrue)
  57. So(dash.Meta.CanSave, ShouldBeTrue)
  58. })
  59. })
  60. })
  61. })
  62. Convey("Given a dashboard with a parent folder which has an acl", t, func() {
  63. fakeDash := models.NewDashboard("Child dash")
  64. fakeDash.ParentId = 1
  65. fakeDash.HasAcl = true
  66. bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
  67. query.Result = fakeDash
  68. return nil
  69. })
  70. bus.AddHandler("test", func(query *models.GetUserGroupsByUserQuery) error {
  71. query.Result = []*models.UserGroup{}
  72. return nil
  73. })
  74. Convey("When user is an Org Viewer and has no permissions for this dashboard", func() {
  75. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  76. query.Result = []*models.DashboardAclInfoDTO{}
  77. return nil
  78. })
  79. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", models.ROLE_VIEWER, func(sc *scenarioContext) {
  80. sc.handlerFunc = GetDashboard
  81. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  82. Convey("Should be denied access", func() {
  83. So(sc.resp.Code, ShouldEqual, 403)
  84. })
  85. })
  86. })
  87. Convey("When user is an Org Editor and has no permissions for this dashboard", func() {
  88. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  89. query.Result = []*models.DashboardAclInfoDTO{}
  90. return nil
  91. })
  92. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", models.ROLE_EDITOR, func(sc *scenarioContext) {
  93. sc.handlerFunc = GetDashboard
  94. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  95. Convey("Should be denied access", func() {
  96. So(sc.resp.Code, ShouldEqual, 403)
  97. })
  98. })
  99. })
  100. Convey("When user is an Org Viewer but has an edit permission", func() {
  101. mockResult := []*models.DashboardAclInfoDTO{
  102. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, PermissionType: models.PERMISSION_EDIT},
  103. }
  104. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  105. query.Result = mockResult
  106. return nil
  107. })
  108. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", models.ROLE_VIEWER, func(sc *scenarioContext) {
  109. sc.handlerFunc = GetDashboard
  110. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  111. So(sc.resp.Code, ShouldEqual, 200)
  112. dash := dtos.DashboardFullWithMeta{}
  113. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  114. So(err, ShouldBeNil)
  115. Convey("Should be able to get dashboard with edit rights", func() {
  116. So(dash.Meta.CanEdit, ShouldBeTrue)
  117. So(dash.Meta.CanSave, ShouldBeTrue)
  118. })
  119. })
  120. })
  121. Convey("When user is an Org Editor but has a view permission", func() {
  122. mockResult := []*models.DashboardAclInfoDTO{
  123. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, PermissionType: models.PERMISSION_VIEW},
  124. }
  125. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  126. query.Result = mockResult
  127. return nil
  128. })
  129. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", models.ROLE_VIEWER, func(sc *scenarioContext) {
  130. sc.handlerFunc = GetDashboard
  131. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  132. So(sc.resp.Code, ShouldEqual, 200)
  133. dash := dtos.DashboardFullWithMeta{}
  134. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  135. So(err, ShouldBeNil)
  136. Convey("Should not be able to edit or save dashboard", func() {
  137. So(dash.Meta.CanEdit, ShouldBeFalse)
  138. So(dash.Meta.CanSave, ShouldBeFalse)
  139. })
  140. })
  141. })
  142. })
  143. }