dashboard_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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/components/simplejson"
  8. "github.com/grafana/grafana/pkg/middleware"
  9. m "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/services/alerting"
  11. "github.com/grafana/grafana/pkg/services/dashboards"
  12. "github.com/grafana/grafana/pkg/setting"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. type fakeDashboardRepo struct {
  16. inserted []*dashboards.SaveDashboardItem
  17. getDashboard []*m.Dashboard
  18. }
  19. func (repo *fakeDashboardRepo) SaveDashboard(json *dashboards.SaveDashboardItem) (*m.Dashboard, error) {
  20. repo.inserted = append(repo.inserted, json)
  21. return json.Dashboard, nil
  22. }
  23. var fakeRepo *fakeDashboardRepo
  24. // This tests two main scenarios. If a user has access to execute an action on a dashboard:
  25. // 1. and the dashboard is in a folder which does not have an acl
  26. // 2. and the dashboard is in a folder which does have an acl
  27. func TestDashboardApiEndpoint(t *testing.T) {
  28. Convey("Given a dashboard with a parent folder which does not have an acl", t, func() {
  29. fakeDash := m.NewDashboard("Child dash")
  30. fakeDash.Id = 1
  31. fakeDash.FolderId = 1
  32. fakeDash.HasAcl = false
  33. var getDashboardQueries []*m.GetDashboardQuery
  34. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  35. query.Result = fakeDash
  36. getDashboardQueries = append(getDashboardQueries, query)
  37. return nil
  38. })
  39. viewerRole := m.ROLE_VIEWER
  40. editorRole := m.ROLE_EDITOR
  41. aclMockResp := []*m.DashboardAclInfoDTO{
  42. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  43. {Role: &editorRole, Permission: m.PERMISSION_EDIT},
  44. }
  45. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  46. query.Result = aclMockResp
  47. return nil
  48. })
  49. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  50. query.Result = []*m.Team{}
  51. return nil
  52. })
  53. cmd := m.SaveDashboardCommand{
  54. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  55. "folderId": fakeDash.FolderId,
  56. "title": fakeDash.Title,
  57. "id": fakeDash.Id,
  58. }),
  59. }
  60. // This tests two scenarios:
  61. // 1. user is an org viewer
  62. // 2. user is an org editor
  63. Convey("When user is an Org Viewer", func() {
  64. role := m.ROLE_VIEWER
  65. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  66. dash := GetDashboardShouldReturn200(sc)
  67. Convey("Should lookup dashboard by slug", func() {
  68. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  69. })
  70. Convey("Should not be able to edit or save dashboard", func() {
  71. So(dash.Meta.CanEdit, ShouldBeFalse)
  72. So(dash.Meta.CanSave, ShouldBeFalse)
  73. So(dash.Meta.CanAdmin, ShouldBeFalse)
  74. })
  75. })
  76. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  77. dash := GetDashboardShouldReturn200(sc)
  78. Convey("Should lookup dashboard by uid", func() {
  79. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  80. })
  81. Convey("Should not be able to edit or save dashboard", func() {
  82. So(dash.Meta.CanEdit, ShouldBeFalse)
  83. So(dash.Meta.CanSave, ShouldBeFalse)
  84. So(dash.Meta.CanAdmin, ShouldBeFalse)
  85. })
  86. })
  87. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  88. CallDeleteDashboard(sc)
  89. So(sc.resp.Code, ShouldEqual, 403)
  90. Convey("Should lookup dashboard by slug", func() {
  91. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  92. })
  93. })
  94. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  95. CallGetDashboardVersion(sc)
  96. So(sc.resp.Code, ShouldEqual, 403)
  97. })
  98. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  99. CallGetDashboardVersions(sc)
  100. So(sc.resp.Code, ShouldEqual, 403)
  101. })
  102. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  103. CallPostDashboard(sc)
  104. So(sc.resp.Code, ShouldEqual, 403)
  105. })
  106. })
  107. Convey("When user is an Org Editor", func() {
  108. role := m.ROLE_EDITOR
  109. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  110. dash := GetDashboardShouldReturn200(sc)
  111. Convey("Should lookup dashboard by slug", func() {
  112. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  113. })
  114. Convey("Should be able to edit or save dashboard", func() {
  115. So(dash.Meta.CanEdit, ShouldBeTrue)
  116. So(dash.Meta.CanSave, ShouldBeTrue)
  117. So(dash.Meta.CanAdmin, ShouldBeFalse)
  118. })
  119. })
  120. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  121. dash := GetDashboardShouldReturn200(sc)
  122. Convey("Should lookup dashboard by uid", func() {
  123. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  124. })
  125. Convey("Should be able to edit or save dashboard", func() {
  126. So(dash.Meta.CanEdit, ShouldBeTrue)
  127. So(dash.Meta.CanSave, ShouldBeTrue)
  128. So(dash.Meta.CanAdmin, ShouldBeFalse)
  129. })
  130. })
  131. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  132. CallDeleteDashboard(sc)
  133. So(sc.resp.Code, ShouldEqual, 200)
  134. Convey("Should lookup dashboard by slug", func() {
  135. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  136. })
  137. })
  138. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  139. CallGetDashboardVersion(sc)
  140. So(sc.resp.Code, ShouldEqual, 200)
  141. })
  142. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  143. CallGetDashboardVersions(sc)
  144. So(sc.resp.Code, ShouldEqual, 200)
  145. })
  146. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  147. CallPostDashboardShouldReturnSuccess(sc)
  148. })
  149. Convey("When saving a dashboard folder in another folder", func() {
  150. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  151. query.Result = fakeDash
  152. query.Result.IsFolder = true
  153. return nil
  154. })
  155. invalidCmd := m.SaveDashboardCommand{
  156. FolderId: fakeDash.FolderId,
  157. IsFolder: true,
  158. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  159. "folderId": fakeDash.FolderId,
  160. "title": fakeDash.Title,
  161. }),
  162. }
  163. Convey("Should return an error", func() {
  164. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, invalidCmd, func(sc *scenarioContext) {
  165. CallPostDashboard(sc)
  166. So(sc.resp.Code, ShouldEqual, 400)
  167. })
  168. })
  169. })
  170. })
  171. })
  172. Convey("Given a dashboard with a parent folder which has an acl", t, func() {
  173. fakeDash := m.NewDashboard("Child dash")
  174. fakeDash.Id = 1
  175. fakeDash.FolderId = 1
  176. fakeDash.HasAcl = true
  177. setting.ViewersCanEdit = false
  178. aclMockResp := []*m.DashboardAclInfoDTO{
  179. {
  180. DashboardId: 1,
  181. Permission: m.PERMISSION_EDIT,
  182. UserId: 200,
  183. },
  184. }
  185. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  186. query.Result = aclMockResp
  187. return nil
  188. })
  189. var getDashboardQueries []*m.GetDashboardQuery
  190. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  191. query.Result = fakeDash
  192. getDashboardQueries = append(getDashboardQueries, query)
  193. return nil
  194. })
  195. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  196. query.Result = []*m.Team{}
  197. return nil
  198. })
  199. cmd := m.SaveDashboardCommand{
  200. FolderId: fakeDash.FolderId,
  201. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  202. "id": fakeDash.Id,
  203. "folderId": fakeDash.FolderId,
  204. "title": fakeDash.Title,
  205. }),
  206. }
  207. // This tests six scenarios:
  208. // 1. user is an org viewer AND has no permissions for this dashboard
  209. // 2. user is an org editor AND has no permissions for this dashboard
  210. // 3. user is an org viewer AND has been granted edit permission for the dashboard
  211. // 4. user is an org viewer AND all viewers have edit permission for this dashboard
  212. // 5. user is an org viewer AND has been granted an admin permission
  213. // 6. user is an org editor AND has been granted a view permission
  214. Convey("When user is an Org Viewer and has no permissions for this dashboard", func() {
  215. role := m.ROLE_VIEWER
  216. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  217. sc.handlerFunc = GetDashboard
  218. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  219. Convey("Should lookup dashboard by slug", func() {
  220. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  221. })
  222. Convey("Should be denied access", func() {
  223. So(sc.resp.Code, ShouldEqual, 403)
  224. })
  225. })
  226. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  227. sc.handlerFunc = GetDashboard
  228. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  229. Convey("Should lookup dashboard by uid", func() {
  230. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  231. })
  232. Convey("Should be denied access", func() {
  233. So(sc.resp.Code, ShouldEqual, 403)
  234. })
  235. })
  236. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  237. CallDeleteDashboard(sc)
  238. So(sc.resp.Code, ShouldEqual, 403)
  239. Convey("Should lookup dashboard by slug", func() {
  240. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  241. })
  242. })
  243. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  244. CallGetDashboardVersion(sc)
  245. So(sc.resp.Code, ShouldEqual, 403)
  246. })
  247. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  248. CallGetDashboardVersions(sc)
  249. So(sc.resp.Code, ShouldEqual, 403)
  250. })
  251. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  252. CallPostDashboard(sc)
  253. So(sc.resp.Code, ShouldEqual, 403)
  254. })
  255. })
  256. Convey("When user is an Org Editor and has no permissions for this dashboard", func() {
  257. role := m.ROLE_EDITOR
  258. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  259. sc.handlerFunc = GetDashboard
  260. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  261. Convey("Should lookup dashboard by slug", func() {
  262. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  263. })
  264. Convey("Should be denied access", func() {
  265. So(sc.resp.Code, ShouldEqual, 403)
  266. })
  267. })
  268. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  269. sc.handlerFunc = GetDashboard
  270. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  271. Convey("Should lookup dashboard by uid", func() {
  272. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  273. })
  274. Convey("Should be denied access", func() {
  275. So(sc.resp.Code, ShouldEqual, 403)
  276. })
  277. })
  278. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  279. CallDeleteDashboard(sc)
  280. So(sc.resp.Code, ShouldEqual, 403)
  281. Convey("Should lookup dashboard by slug", func() {
  282. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  283. })
  284. })
  285. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  286. CallGetDashboardVersion(sc)
  287. So(sc.resp.Code, ShouldEqual, 403)
  288. })
  289. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  290. CallGetDashboardVersions(sc)
  291. So(sc.resp.Code, ShouldEqual, 403)
  292. })
  293. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  294. CallPostDashboard(sc)
  295. So(sc.resp.Code, ShouldEqual, 403)
  296. })
  297. })
  298. Convey("When user is an Org Viewer but has an edit permission", func() {
  299. role := m.ROLE_VIEWER
  300. mockResult := []*m.DashboardAclInfoDTO{
  301. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_EDIT},
  302. }
  303. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  304. query.Result = mockResult
  305. return nil
  306. })
  307. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  308. dash := GetDashboardShouldReturn200(sc)
  309. Convey("Should lookup dashboard by slug", func() {
  310. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  311. })
  312. Convey("Should be able to get dashboard with edit rights", func() {
  313. So(dash.Meta.CanEdit, ShouldBeTrue)
  314. So(dash.Meta.CanSave, ShouldBeTrue)
  315. So(dash.Meta.CanAdmin, ShouldBeFalse)
  316. })
  317. })
  318. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  319. dash := GetDashboardShouldReturn200(sc)
  320. Convey("Should lookup dashboard by uid", func() {
  321. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  322. })
  323. Convey("Should be able to get dashboard with edit rights", func() {
  324. So(dash.Meta.CanEdit, ShouldBeTrue)
  325. So(dash.Meta.CanSave, ShouldBeTrue)
  326. So(dash.Meta.CanAdmin, ShouldBeFalse)
  327. })
  328. })
  329. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  330. CallDeleteDashboard(sc)
  331. So(sc.resp.Code, ShouldEqual, 200)
  332. Convey("Should lookup dashboard by slug", func() {
  333. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  334. })
  335. })
  336. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  337. CallGetDashboardVersion(sc)
  338. So(sc.resp.Code, ShouldEqual, 200)
  339. })
  340. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  341. CallGetDashboardVersions(sc)
  342. So(sc.resp.Code, ShouldEqual, 200)
  343. })
  344. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  345. CallPostDashboardShouldReturnSuccess(sc)
  346. })
  347. })
  348. Convey("When user is an Org Viewer and viewers can edit", func() {
  349. role := m.ROLE_VIEWER
  350. setting.ViewersCanEdit = true
  351. mockResult := []*m.DashboardAclInfoDTO{
  352. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_VIEW},
  353. }
  354. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  355. query.Result = mockResult
  356. return nil
  357. })
  358. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  359. dash := GetDashboardShouldReturn200(sc)
  360. Convey("Should lookup dashboard by slug", func() {
  361. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  362. })
  363. Convey("Should be able to get dashboard with edit rights but can save should be false", func() {
  364. So(dash.Meta.CanEdit, ShouldBeTrue)
  365. So(dash.Meta.CanSave, ShouldBeFalse)
  366. So(dash.Meta.CanAdmin, ShouldBeFalse)
  367. })
  368. })
  369. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  370. dash := GetDashboardShouldReturn200(sc)
  371. Convey("Should lookup dashboard by uid", func() {
  372. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  373. })
  374. Convey("Should be able to get dashboard with edit rights but can save should be false", func() {
  375. So(dash.Meta.CanEdit, ShouldBeTrue)
  376. So(dash.Meta.CanSave, ShouldBeFalse)
  377. So(dash.Meta.CanAdmin, ShouldBeFalse)
  378. })
  379. })
  380. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  381. CallDeleteDashboard(sc)
  382. So(sc.resp.Code, ShouldEqual, 403)
  383. Convey("Should lookup dashboard by slug", func() {
  384. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  385. })
  386. })
  387. })
  388. Convey("When user is an Org Viewer but has an admin permission", func() {
  389. role := m.ROLE_VIEWER
  390. mockResult := []*m.DashboardAclInfoDTO{
  391. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_ADMIN},
  392. }
  393. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  394. query.Result = mockResult
  395. return nil
  396. })
  397. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  398. dash := GetDashboardShouldReturn200(sc)
  399. Convey("Should lookup dashboard by slug", func() {
  400. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  401. })
  402. Convey("Should be able to get dashboard with edit rights", func() {
  403. So(dash.Meta.CanEdit, ShouldBeTrue)
  404. So(dash.Meta.CanSave, ShouldBeTrue)
  405. So(dash.Meta.CanAdmin, ShouldBeTrue)
  406. })
  407. })
  408. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  409. dash := GetDashboardShouldReturn200(sc)
  410. Convey("Should lookup dashboard by uid", func() {
  411. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  412. })
  413. Convey("Should be able to get dashboard with edit rights", func() {
  414. So(dash.Meta.CanEdit, ShouldBeTrue)
  415. So(dash.Meta.CanSave, ShouldBeTrue)
  416. So(dash.Meta.CanAdmin, ShouldBeTrue)
  417. })
  418. })
  419. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  420. CallDeleteDashboard(sc)
  421. So(sc.resp.Code, ShouldEqual, 200)
  422. Convey("Should lookup dashboard by slug", func() {
  423. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  424. })
  425. })
  426. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  427. CallGetDashboardVersion(sc)
  428. So(sc.resp.Code, ShouldEqual, 200)
  429. })
  430. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  431. CallGetDashboardVersions(sc)
  432. So(sc.resp.Code, ShouldEqual, 200)
  433. })
  434. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  435. CallPostDashboardShouldReturnSuccess(sc)
  436. })
  437. })
  438. Convey("When user is an Org Editor but has a view permission", func() {
  439. role := m.ROLE_EDITOR
  440. mockResult := []*m.DashboardAclInfoDTO{
  441. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_VIEW},
  442. }
  443. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  444. query.Result = mockResult
  445. return nil
  446. })
  447. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  448. dash := GetDashboardShouldReturn200(sc)
  449. Convey("Should lookup dashboard by slug", func() {
  450. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  451. })
  452. Convey("Should not be able to edit or save dashboard", func() {
  453. So(dash.Meta.CanEdit, ShouldBeFalse)
  454. So(dash.Meta.CanSave, ShouldBeFalse)
  455. })
  456. })
  457. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  458. dash := GetDashboardShouldReturn200(sc)
  459. Convey("Should lookup dashboard by uid", func() {
  460. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  461. })
  462. Convey("Should not be able to edit or save dashboard", func() {
  463. So(dash.Meta.CanEdit, ShouldBeFalse)
  464. So(dash.Meta.CanSave, ShouldBeFalse)
  465. })
  466. })
  467. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  468. CallDeleteDashboard(sc)
  469. So(sc.resp.Code, ShouldEqual, 403)
  470. Convey("Should lookup dashboard by slug", func() {
  471. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  472. })
  473. })
  474. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  475. CallGetDashboardVersion(sc)
  476. So(sc.resp.Code, ShouldEqual, 403)
  477. })
  478. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  479. CallGetDashboardVersions(sc)
  480. So(sc.resp.Code, ShouldEqual, 403)
  481. })
  482. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  483. CallPostDashboard(sc)
  484. So(sc.resp.Code, ShouldEqual, 403)
  485. })
  486. })
  487. })
  488. }
  489. func GetDashboardShouldReturn200(sc *scenarioContext) dtos.DashboardFullWithMeta {
  490. sc.handlerFunc = GetDashboard
  491. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  492. So(sc.resp.Code, ShouldEqual, 200)
  493. dash := dtos.DashboardFullWithMeta{}
  494. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  495. So(err, ShouldBeNil)
  496. return dash
  497. }
  498. func CallGetDashboardVersion(sc *scenarioContext) {
  499. bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
  500. query.Result = &m.DashboardVersion{}
  501. return nil
  502. })
  503. sc.handlerFunc = GetDashboardVersion
  504. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  505. }
  506. func CallGetDashboardVersions(sc *scenarioContext) {
  507. bus.AddHandler("test", func(query *m.GetDashboardVersionsQuery) error {
  508. query.Result = []*m.DashboardVersionDTO{}
  509. return nil
  510. })
  511. sc.handlerFunc = GetDashboardVersions
  512. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  513. }
  514. func CallDeleteDashboard(sc *scenarioContext) {
  515. bus.AddHandler("test", func(cmd *m.DeleteDashboardCommand) error {
  516. return nil
  517. })
  518. sc.handlerFunc = DeleteDashboard
  519. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  520. }
  521. func CallPostDashboard(sc *scenarioContext) {
  522. bus.AddHandler("test", func(cmd *alerting.ValidateDashboardAlertsCommand) error {
  523. return nil
  524. })
  525. bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
  526. cmd.Result = &m.Dashboard{Id: 2, Slug: "Dash", Version: 2}
  527. return nil
  528. })
  529. bus.AddHandler("test", func(cmd *alerting.UpdateDashboardAlertsCommand) error {
  530. return nil
  531. })
  532. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  533. }
  534. func CallPostDashboardShouldReturnSuccess(sc *scenarioContext) {
  535. CallPostDashboard(sc)
  536. So(sc.resp.Code, ShouldEqual, 200)
  537. result := sc.ToJson()
  538. So(result.Get("status").MustString(), ShouldEqual, "success")
  539. So(result.Get("id").MustInt64(), ShouldBeGreaterThan, 0)
  540. So(result.Get("uid").MustString(), ShouldNotBeNil)
  541. So(result.Get("slug").MustString(), ShouldNotBeNil)
  542. So(result.Get("url").MustString(), ShouldNotBeNil)
  543. }
  544. func postDashboardScenario(desc string, url string, routePattern string, role m.RoleType, cmd m.SaveDashboardCommand, fn scenarioFunc) {
  545. Convey(desc+" "+url, func() {
  546. defer bus.ClearBusHandlers()
  547. sc := setupScenarioContext(url)
  548. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  549. sc.context = c
  550. sc.context.UserId = TestUserID
  551. sc.context.OrgId = TestOrgID
  552. sc.context.OrgRole = role
  553. return PostDashboard(c, cmd)
  554. })
  555. fakeRepo = &fakeDashboardRepo{}
  556. dashboards.SetRepository(fakeRepo)
  557. sc.m.Post(routePattern, sc.defaultHandler)
  558. fn(sc)
  559. })
  560. }
  561. func (sc *scenarioContext) ToJson() *simplejson.Json {
  562. var result *simplejson.Json
  563. err := json.NewDecoder(sc.resp.Body).Decode(&result)
  564. So(err, ShouldBeNil)
  565. return result
  566. }