dashboard_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package api
  2. import (
  3. "encoding/json"
  4. "path/filepath"
  5. "testing"
  6. macaron "gopkg.in/macaron.v1"
  7. "github.com/go-macaron/session"
  8. "github.com/grafana/grafana/pkg/api/dtos"
  9. "github.com/grafana/grafana/pkg/bus"
  10. "github.com/grafana/grafana/pkg/components/simplejson"
  11. "github.com/grafana/grafana/pkg/middleware"
  12. "github.com/grafana/grafana/pkg/models"
  13. "github.com/grafana/grafana/pkg/services/alerting"
  14. . "github.com/smartystreets/goconvey/convey"
  15. )
  16. func TestDashboardApiEndpoint(t *testing.T) {
  17. Convey("Given a dashboard with a parent folder which does not have an acl", t, func() {
  18. fakeDash := models.NewDashboard("Child dash")
  19. fakeDash.ParentId = 1
  20. fakeDash.HasAcl = false
  21. bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
  22. query.Result = fakeDash
  23. return nil
  24. })
  25. cmd := models.SaveDashboardCommand{
  26. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  27. "parentId": fakeDash.ParentId,
  28. "title": fakeDash.Title,
  29. }),
  30. }
  31. Convey("When user is an Org Viewer", func() {
  32. role := models.ROLE_VIEWER
  33. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  34. dash := GetDashboardShouldReturn200(sc)
  35. Convey("Should not be able to edit or save dashboard", func() {
  36. So(dash.Meta.CanEdit, ShouldBeFalse)
  37. So(dash.Meta.CanSave, ShouldBeFalse)
  38. })
  39. })
  40. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  41. CallDeleteDashboard(sc)
  42. So(sc.resp.Code, ShouldEqual, 403)
  43. })
  44. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  45. CallGetDashboardVersion(sc)
  46. So(sc.resp.Code, ShouldEqual, 403)
  47. })
  48. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  49. CallGetDashboardVersions(sc)
  50. So(sc.resp.Code, ShouldEqual, 403)
  51. })
  52. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  53. CallPostDashboard(sc)
  54. So(sc.resp.Code, ShouldEqual, 403)
  55. })
  56. })
  57. Convey("When user is an Org Read Only Editor", func() {
  58. role := models.ROLE_READ_ONLY_EDITOR
  59. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  60. dash := GetDashboardShouldReturn200(sc)
  61. Convey("Should be able to edit but not save the dashboard", func() {
  62. So(dash.Meta.CanEdit, ShouldBeTrue)
  63. So(dash.Meta.CanSave, ShouldBeFalse)
  64. })
  65. })
  66. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  67. CallDeleteDashboard(sc)
  68. So(sc.resp.Code, ShouldEqual, 403)
  69. })
  70. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  71. CallGetDashboardVersion(sc)
  72. So(sc.resp.Code, ShouldEqual, 403)
  73. })
  74. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  75. CallGetDashboardVersions(sc)
  76. So(sc.resp.Code, ShouldEqual, 403)
  77. })
  78. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  79. CallPostDashboard(sc)
  80. So(sc.resp.Code, ShouldEqual, 403)
  81. })
  82. })
  83. Convey("When user is an Org Editor", func() {
  84. role := models.ROLE_EDITOR
  85. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  86. dash := GetDashboardShouldReturn200(sc)
  87. Convey("Should be able to edit or save dashboard", func() {
  88. So(dash.Meta.CanEdit, ShouldBeTrue)
  89. So(dash.Meta.CanSave, ShouldBeTrue)
  90. })
  91. })
  92. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  93. CallDeleteDashboard(sc)
  94. So(sc.resp.Code, ShouldEqual, 200)
  95. })
  96. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  97. CallGetDashboardVersion(sc)
  98. So(sc.resp.Code, ShouldEqual, 200)
  99. })
  100. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  101. CallGetDashboardVersions(sc)
  102. So(sc.resp.Code, ShouldEqual, 200)
  103. })
  104. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  105. CallPostDashboard(sc)
  106. So(sc.resp.Code, ShouldEqual, 200)
  107. })
  108. })
  109. })
  110. Convey("Given a dashboard with a parent folder which has an acl", t, func() {
  111. fakeDash := models.NewDashboard("Child dash")
  112. fakeDash.ParentId = 1
  113. fakeDash.HasAcl = true
  114. bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
  115. query.Result = fakeDash
  116. return nil
  117. })
  118. bus.AddHandler("test", func(query *models.GetUserGroupsByUserQuery) error {
  119. query.Result = []*models.UserGroup{}
  120. return nil
  121. })
  122. cmd := models.SaveDashboardCommand{
  123. ParentId: fakeDash.ParentId,
  124. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  125. "parentId": fakeDash.ParentId,
  126. "title": fakeDash.Title,
  127. }),
  128. }
  129. Convey("When user is an Org Viewer and has no permissions for this dashboard", func() {
  130. role := models.ROLE_VIEWER
  131. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  132. query.Result = []*models.DashboardAclInfoDTO{}
  133. return nil
  134. })
  135. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  136. sc.handlerFunc = GetDashboard
  137. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  138. Convey("Should be denied access", func() {
  139. So(sc.resp.Code, ShouldEqual, 403)
  140. })
  141. })
  142. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  143. CallDeleteDashboard(sc)
  144. So(sc.resp.Code, ShouldEqual, 403)
  145. })
  146. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  147. CallGetDashboardVersion(sc)
  148. So(sc.resp.Code, ShouldEqual, 403)
  149. })
  150. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  151. CallGetDashboardVersions(sc)
  152. So(sc.resp.Code, ShouldEqual, 403)
  153. })
  154. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  155. CallPostDashboard(sc)
  156. So(sc.resp.Code, ShouldEqual, 403)
  157. })
  158. })
  159. Convey("When user is an Org Editor and has no permissions for this dashboard", func() {
  160. role := models.ROLE_EDITOR
  161. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  162. query.Result = []*models.DashboardAclInfoDTO{}
  163. return nil
  164. })
  165. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  166. sc.handlerFunc = GetDashboard
  167. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  168. Convey("Should be denied access", func() {
  169. So(sc.resp.Code, ShouldEqual, 403)
  170. })
  171. })
  172. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  173. CallDeleteDashboard(sc)
  174. So(sc.resp.Code, ShouldEqual, 403)
  175. })
  176. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  177. CallGetDashboardVersion(sc)
  178. So(sc.resp.Code, ShouldEqual, 403)
  179. })
  180. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  181. CallGetDashboardVersions(sc)
  182. So(sc.resp.Code, ShouldEqual, 403)
  183. })
  184. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  185. CallPostDashboard(sc)
  186. So(sc.resp.Code, ShouldEqual, 403)
  187. })
  188. })
  189. Convey("When user is an Org Viewer but has an edit permission", func() {
  190. role := models.ROLE_VIEWER
  191. mockResult := []*models.DashboardAclInfoDTO{
  192. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, PermissionType: models.PERMISSION_EDIT},
  193. }
  194. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  195. query.Result = mockResult
  196. return nil
  197. })
  198. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  199. dash := GetDashboardShouldReturn200(sc)
  200. Convey("Should be able to get dashboard with edit rights", func() {
  201. So(dash.Meta.CanEdit, ShouldBeTrue)
  202. So(dash.Meta.CanSave, ShouldBeTrue)
  203. })
  204. })
  205. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  206. CallDeleteDashboard(sc)
  207. So(sc.resp.Code, ShouldEqual, 200)
  208. })
  209. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  210. CallGetDashboardVersion(sc)
  211. So(sc.resp.Code, ShouldEqual, 200)
  212. })
  213. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  214. CallGetDashboardVersions(sc)
  215. So(sc.resp.Code, ShouldEqual, 200)
  216. })
  217. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  218. CallPostDashboard(sc)
  219. So(sc.resp.Code, ShouldEqual, 200)
  220. })
  221. })
  222. Convey("When user is an Org Editor but has a view permission", func() {
  223. role := models.ROLE_EDITOR
  224. mockResult := []*models.DashboardAclInfoDTO{
  225. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, PermissionType: models.PERMISSION_VIEW},
  226. }
  227. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  228. query.Result = mockResult
  229. return nil
  230. })
  231. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  232. dash := GetDashboardShouldReturn200(sc)
  233. Convey("Should not be able to edit or save dashboard", func() {
  234. So(dash.Meta.CanEdit, ShouldBeFalse)
  235. So(dash.Meta.CanSave, ShouldBeFalse)
  236. })
  237. })
  238. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  239. CallDeleteDashboard(sc)
  240. So(sc.resp.Code, ShouldEqual, 403)
  241. })
  242. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  243. CallGetDashboardVersion(sc)
  244. So(sc.resp.Code, ShouldEqual, 403)
  245. })
  246. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  247. CallGetDashboardVersions(sc)
  248. So(sc.resp.Code, ShouldEqual, 403)
  249. })
  250. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  251. CallPostDashboard(sc)
  252. So(sc.resp.Code, ShouldEqual, 403)
  253. })
  254. })
  255. })
  256. }
  257. func GetDashboardShouldReturn200(sc *scenarioContext) dtos.DashboardFullWithMeta {
  258. sc.handlerFunc = GetDashboard
  259. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  260. So(sc.resp.Code, ShouldEqual, 200)
  261. dash := dtos.DashboardFullWithMeta{}
  262. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  263. So(err, ShouldBeNil)
  264. return dash
  265. }
  266. func CallGetDashboardVersion(sc *scenarioContext) {
  267. bus.AddHandler("test", func(query *models.GetDashboardVersionQuery) error {
  268. query.Result = &models.DashboardVersion{}
  269. return nil
  270. })
  271. sc.handlerFunc = GetDashboardVersion
  272. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  273. }
  274. func CallGetDashboardVersions(sc *scenarioContext) {
  275. bus.AddHandler("test", func(query *models.GetDashboardVersionsQuery) error {
  276. query.Result = []*models.DashboardVersionDTO{}
  277. return nil
  278. })
  279. sc.handlerFunc = GetDashboardVersions
  280. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  281. }
  282. func CallDeleteDashboard(sc *scenarioContext) {
  283. bus.AddHandler("test", func(cmd *models.DeleteDashboardCommand) error {
  284. return nil
  285. })
  286. sc.handlerFunc = DeleteDashboard
  287. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  288. }
  289. func CallPostDashboard(sc *scenarioContext) {
  290. bus.AddHandler("test", func(cmd *alerting.ValidateDashboardAlertsCommand) error {
  291. return nil
  292. })
  293. bus.AddHandler("test", func(cmd *models.SaveDashboardCommand) error {
  294. cmd.Result = &models.Dashboard{Id: 2, Slug: "Dash", Version: 2}
  295. return nil
  296. })
  297. bus.AddHandler("test", func(cmd *alerting.UpdateDashboardAlertsCommand) error {
  298. return nil
  299. })
  300. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  301. }
  302. func postDashboardScenario(desc string, url string, routePattern string, role models.RoleType, cmd models.SaveDashboardCommand, fn scenarioFunc) {
  303. Convey(desc+" "+url, func() {
  304. defer bus.ClearBusHandlers()
  305. sc := &scenarioContext{
  306. url: url,
  307. }
  308. viewsPath, _ := filepath.Abs("../../public/views")
  309. sc.m = macaron.New()
  310. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  311. Directory: viewsPath,
  312. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  313. }))
  314. sc.m.Use(middleware.GetContextHandler())
  315. sc.m.Use(middleware.Sessioner(&session.Options{}))
  316. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  317. sc.context = c
  318. sc.context.UserId = TestUserID
  319. sc.context.OrgId = TestOrgID
  320. sc.context.OrgRole = role
  321. return PostDashboard(c, cmd)
  322. })
  323. sc.m.Post(routePattern, sc.defaultHandler)
  324. fn(sc)
  325. })
  326. }