dashboard_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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.Id = 1
  20. fakeDash.ParentId = 1
  21. fakeDash.HasAcl = false
  22. bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
  23. query.Result = fakeDash
  24. return nil
  25. })
  26. aclMockResp := []*models.DashboardAcl{}
  27. bus.AddHandler("test", func(query *models.GetInheritedDashboardAclQuery) error {
  28. query.Result = aclMockResp
  29. return nil
  30. })
  31. cmd := models.SaveDashboardCommand{
  32. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  33. "parentId": fakeDash.ParentId,
  34. "title": fakeDash.Title,
  35. "id": fakeDash.Id,
  36. }),
  37. }
  38. Convey("When user is an Org Viewer", func() {
  39. role := models.ROLE_VIEWER
  40. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  41. dash := GetDashboardShouldReturn200(sc)
  42. Convey("Should not be able to edit or save dashboard", func() {
  43. So(dash.Meta.CanEdit, ShouldBeFalse)
  44. So(dash.Meta.CanSave, ShouldBeFalse)
  45. })
  46. })
  47. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  48. CallDeleteDashboard(sc)
  49. So(sc.resp.Code, ShouldEqual, 403)
  50. })
  51. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  52. CallGetDashboardVersion(sc)
  53. So(sc.resp.Code, ShouldEqual, 403)
  54. })
  55. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  56. CallGetDashboardVersions(sc)
  57. So(sc.resp.Code, ShouldEqual, 403)
  58. })
  59. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  60. CallPostDashboard(sc)
  61. So(sc.resp.Code, ShouldEqual, 403)
  62. })
  63. })
  64. Convey("When user is an Org Read Only Editor", func() {
  65. role := models.ROLE_READ_ONLY_EDITOR
  66. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  67. dash := GetDashboardShouldReturn200(sc)
  68. Convey("Should be able to edit but not save the dashboard", func() {
  69. So(dash.Meta.CanEdit, ShouldBeTrue)
  70. So(dash.Meta.CanSave, ShouldBeFalse)
  71. })
  72. })
  73. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  74. CallDeleteDashboard(sc)
  75. So(sc.resp.Code, ShouldEqual, 403)
  76. })
  77. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  78. CallGetDashboardVersion(sc)
  79. So(sc.resp.Code, ShouldEqual, 403)
  80. })
  81. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  82. CallGetDashboardVersions(sc)
  83. So(sc.resp.Code, ShouldEqual, 403)
  84. })
  85. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  86. CallPostDashboard(sc)
  87. So(sc.resp.Code, ShouldEqual, 403)
  88. })
  89. })
  90. Convey("When user is an Org Editor", func() {
  91. role := models.ROLE_EDITOR
  92. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  93. dash := GetDashboardShouldReturn200(sc)
  94. Convey("Should be able to edit or save dashboard", func() {
  95. So(dash.Meta.CanEdit, ShouldBeTrue)
  96. So(dash.Meta.CanSave, ShouldBeTrue)
  97. })
  98. })
  99. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  100. CallDeleteDashboard(sc)
  101. So(sc.resp.Code, ShouldEqual, 200)
  102. })
  103. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  104. CallGetDashboardVersion(sc)
  105. So(sc.resp.Code, ShouldEqual, 200)
  106. })
  107. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  108. CallGetDashboardVersions(sc)
  109. So(sc.resp.Code, ShouldEqual, 200)
  110. })
  111. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  112. CallPostDashboard(sc)
  113. So(sc.resp.Code, ShouldEqual, 200)
  114. })
  115. Convey("When saving a dashboard folder in another folder", func() {
  116. bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
  117. query.Result = fakeDash
  118. query.Result.IsFolder = true
  119. return nil
  120. })
  121. invalidCmd := models.SaveDashboardCommand{
  122. ParentId: fakeDash.ParentId,
  123. IsFolder: true,
  124. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  125. "parentId": fakeDash.ParentId,
  126. "title": fakeDash.Title,
  127. }),
  128. }
  129. Convey("Should return an error", func() {
  130. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, invalidCmd, func(sc *scenarioContext) {
  131. CallPostDashboard(sc)
  132. So(sc.resp.Code, ShouldEqual, 400)
  133. })
  134. })
  135. })
  136. })
  137. })
  138. Convey("Given a dashboard with a parent folder which has an acl", t, func() {
  139. fakeDash := models.NewDashboard("Child dash")
  140. fakeDash.Id = 1
  141. fakeDash.ParentId = 1
  142. fakeDash.HasAcl = true
  143. aclMockResp := []*models.DashboardAcl{
  144. {
  145. DashboardId: 1,
  146. Permission: models.PERMISSION_EDIT,
  147. UserId: 200,
  148. },
  149. }
  150. bus.AddHandler("test", func(query *models.GetInheritedDashboardAclQuery) error {
  151. query.Result = aclMockResp
  152. return nil
  153. })
  154. bus.AddHandler("test", func(query *models.GetDashboardQuery) error {
  155. query.Result = fakeDash
  156. return nil
  157. })
  158. bus.AddHandler("test", func(query *models.GetUserGroupsByUserQuery) error {
  159. query.Result = []*models.UserGroup{}
  160. return nil
  161. })
  162. cmd := models.SaveDashboardCommand{
  163. ParentId: fakeDash.ParentId,
  164. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  165. "id": fakeDash.Id,
  166. "parentId": fakeDash.ParentId,
  167. "title": fakeDash.Title,
  168. }),
  169. }
  170. Convey("When user is an Org Viewer and has no permissions for this dashboard", func() {
  171. role := models.ROLE_VIEWER
  172. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  173. sc.handlerFunc = GetDashboard
  174. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  175. Convey("Should be denied access", func() {
  176. So(sc.resp.Code, ShouldEqual, 403)
  177. })
  178. })
  179. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  180. CallDeleteDashboard(sc)
  181. So(sc.resp.Code, ShouldEqual, 403)
  182. })
  183. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  184. CallGetDashboardVersion(sc)
  185. So(sc.resp.Code, ShouldEqual, 403)
  186. })
  187. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  188. CallGetDashboardVersions(sc)
  189. So(sc.resp.Code, ShouldEqual, 403)
  190. })
  191. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  192. CallPostDashboard(sc)
  193. So(sc.resp.Code, ShouldEqual, 403)
  194. })
  195. })
  196. Convey("When user is an Org Editor and has no permissions for this dashboard", func() {
  197. role := models.ROLE_EDITOR
  198. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  199. sc.handlerFunc = GetDashboard
  200. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  201. Convey("Should be denied access", func() {
  202. So(sc.resp.Code, ShouldEqual, 403)
  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, 403)
  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, 403)
  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, 403)
  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, 403)
  220. })
  221. })
  222. Convey("When user is an Org Viewer but has an edit permission", func() {
  223. role := models.ROLE_VIEWER
  224. mockResult := []*models.DashboardAcl{
  225. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: models.PERMISSION_EDIT},
  226. }
  227. bus.AddHandler("test", func(query *models.GetInheritedDashboardAclQuery) 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 be able to get dashboard with edit rights", func() {
  234. So(dash.Meta.CanEdit, ShouldBeTrue)
  235. So(dash.Meta.CanSave, ShouldBeTrue)
  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, 200)
  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, 200)
  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, 200)
  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, 200)
  253. })
  254. })
  255. Convey("When user is an Org Editor but has a view permission", func() {
  256. role := models.ROLE_EDITOR
  257. mockResult := []*models.DashboardAcl{
  258. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: models.PERMISSION_VIEW},
  259. }
  260. bus.AddHandler("test", func(query *models.GetInheritedDashboardAclQuery) error {
  261. query.Result = mockResult
  262. return nil
  263. })
  264. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  265. dash := GetDashboardShouldReturn200(sc)
  266. Convey("Should not be able to edit or save dashboard", func() {
  267. So(dash.Meta.CanEdit, ShouldBeFalse)
  268. So(dash.Meta.CanSave, ShouldBeFalse)
  269. })
  270. })
  271. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  272. CallDeleteDashboard(sc)
  273. So(sc.resp.Code, ShouldEqual, 403)
  274. })
  275. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  276. CallGetDashboardVersion(sc)
  277. So(sc.resp.Code, ShouldEqual, 403)
  278. })
  279. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  280. CallGetDashboardVersions(sc)
  281. So(sc.resp.Code, ShouldEqual, 403)
  282. })
  283. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  284. CallPostDashboard(sc)
  285. So(sc.resp.Code, ShouldEqual, 403)
  286. })
  287. })
  288. })
  289. }
  290. func GetDashboardShouldReturn200(sc *scenarioContext) dtos.DashboardFullWithMeta {
  291. sc.handlerFunc = GetDashboard
  292. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  293. So(sc.resp.Code, ShouldEqual, 200)
  294. dash := dtos.DashboardFullWithMeta{}
  295. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  296. So(err, ShouldBeNil)
  297. return dash
  298. }
  299. func CallGetDashboardVersion(sc *scenarioContext) {
  300. bus.AddHandler("test", func(query *models.GetDashboardVersionQuery) error {
  301. query.Result = &models.DashboardVersion{}
  302. return nil
  303. })
  304. sc.handlerFunc = GetDashboardVersion
  305. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  306. }
  307. func CallGetDashboardVersions(sc *scenarioContext) {
  308. bus.AddHandler("test", func(query *models.GetDashboardVersionsQuery) error {
  309. query.Result = []*models.DashboardVersionDTO{}
  310. return nil
  311. })
  312. sc.handlerFunc = GetDashboardVersions
  313. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  314. }
  315. func CallDeleteDashboard(sc *scenarioContext) {
  316. bus.AddHandler("test", func(cmd *models.DeleteDashboardCommand) error {
  317. return nil
  318. })
  319. sc.handlerFunc = DeleteDashboard
  320. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  321. }
  322. func CallPostDashboard(sc *scenarioContext) {
  323. bus.AddHandler("test", func(cmd *alerting.ValidateDashboardAlertsCommand) error {
  324. return nil
  325. })
  326. bus.AddHandler("test", func(cmd *models.SaveDashboardCommand) error {
  327. cmd.Result = &models.Dashboard{Id: 2, Slug: "Dash", Version: 2}
  328. return nil
  329. })
  330. bus.AddHandler("test", func(cmd *alerting.UpdateDashboardAlertsCommand) error {
  331. return nil
  332. })
  333. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  334. }
  335. func postDashboardScenario(desc string, url string, routePattern string, role models.RoleType, cmd models.SaveDashboardCommand, fn scenarioFunc) {
  336. Convey(desc+" "+url, func() {
  337. defer bus.ClearBusHandlers()
  338. sc := &scenarioContext{
  339. url: url,
  340. }
  341. viewsPath, _ := filepath.Abs("../../public/views")
  342. sc.m = macaron.New()
  343. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  344. Directory: viewsPath,
  345. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  346. }))
  347. sc.m.Use(middleware.GetContextHandler())
  348. sc.m.Use(middleware.Sessioner(&session.Options{}))
  349. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  350. sc.context = c
  351. sc.context.UserId = TestUserID
  352. sc.context.OrgId = TestOrgID
  353. sc.context.OrgRole = role
  354. return PostDashboard(c, cmd)
  355. })
  356. sc.m.Post(routePattern, sc.defaultHandler)
  357. fn(sc)
  358. })
  359. }