dashboard_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. m "github.com/grafana/grafana/pkg/models"
  13. "github.com/grafana/grafana/pkg/services/alerting"
  14. "github.com/grafana/grafana/pkg/services/dashboards"
  15. "github.com/grafana/grafana/pkg/setting"
  16. . "github.com/smartystreets/goconvey/convey"
  17. )
  18. type fakeDashboardRepo struct {
  19. inserted []*dashboards.SaveDashboardItem
  20. getDashboard []*m.Dashboard
  21. }
  22. func (repo *fakeDashboardRepo) SaveDashboard(json *dashboards.SaveDashboardItem) (*m.Dashboard, error) {
  23. repo.inserted = append(repo.inserted, json)
  24. return json.Dashboard, nil
  25. }
  26. var fakeRepo *fakeDashboardRepo
  27. func TestDashboardApiEndpoint(t *testing.T) {
  28. Convey("Given a folder", t, func() {
  29. fakeFolder := m.NewDashboardFolder("Folder")
  30. fakeFolder.Id = 1
  31. fakeFolder.HasAcl = false
  32. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  33. query.Result = fakeFolder
  34. return nil
  35. })
  36. cmd := m.SaveDashboardCommand{
  37. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  38. "title": fakeFolder.Title,
  39. "id": fakeFolder.Id,
  40. }),
  41. IsFolder: true,
  42. }
  43. Convey("When user is an Org Editor", func() {
  44. role := m.ROLE_EDITOR
  45. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/1", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  46. CallGetDashboard(sc)
  47. So(sc.resp.Code, ShouldEqual, 404)
  48. })
  49. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  50. CallPostDashboard(sc)
  51. So(sc.resp.Code, ShouldEqual, 404)
  52. })
  53. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/1", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  54. CallDeleteDashboard(sc)
  55. So(sc.resp.Code, ShouldEqual, 404)
  56. })
  57. })
  58. })
  59. Convey("Given a dashboard with a parent folder which does not have an acl", t, func() {
  60. fakeDash := m.NewDashboard("Child dash")
  61. fakeDash.Id = 1
  62. fakeDash.FolderId = 1
  63. fakeDash.HasAcl = false
  64. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  65. query.Result = fakeDash
  66. return nil
  67. })
  68. viewerRole := m.ROLE_VIEWER
  69. editorRole := m.ROLE_EDITOR
  70. aclMockResp := []*m.DashboardAclInfoDTO{
  71. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  72. {Role: &editorRole, Permission: m.PERMISSION_EDIT},
  73. }
  74. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  75. query.Result = aclMockResp
  76. return nil
  77. })
  78. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  79. query.Result = []*m.Team{}
  80. return nil
  81. })
  82. cmd := m.SaveDashboardCommand{
  83. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  84. "folderId": fakeDash.FolderId,
  85. "title": fakeDash.Title,
  86. "id": fakeDash.Id,
  87. }),
  88. }
  89. Convey("When user is an Org Viewer", func() {
  90. role := m.ROLE_VIEWER
  91. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  92. dash := GetDashboardShouldReturn200(sc)
  93. Convey("Should not be able to edit or save dashboard", func() {
  94. So(dash.Meta.CanEdit, ShouldBeFalse)
  95. So(dash.Meta.CanSave, ShouldBeFalse)
  96. So(dash.Meta.CanAdmin, ShouldBeFalse)
  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, 403)
  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, 403)
  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, 403)
  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, 403)
  114. })
  115. })
  116. Convey("When user is an Org Editor", func() {
  117. role := m.ROLE_EDITOR
  118. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  119. dash := GetDashboardShouldReturn200(sc)
  120. Convey("Should be able to edit or save dashboard", func() {
  121. So(dash.Meta.CanEdit, ShouldBeTrue)
  122. So(dash.Meta.CanSave, ShouldBeTrue)
  123. So(dash.Meta.CanAdmin, ShouldBeFalse)
  124. })
  125. })
  126. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  127. CallDeleteDashboard(sc)
  128. So(sc.resp.Code, ShouldEqual, 200)
  129. })
  130. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  131. CallGetDashboardVersion(sc)
  132. So(sc.resp.Code, ShouldEqual, 200)
  133. })
  134. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  135. CallGetDashboardVersions(sc)
  136. So(sc.resp.Code, ShouldEqual, 200)
  137. })
  138. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  139. CallPostDashboard(sc)
  140. So(sc.resp.Code, ShouldEqual, 200)
  141. })
  142. Convey("When saving a dashboard folder in another folder", func() {
  143. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  144. query.Result = fakeDash
  145. query.Result.IsFolder = true
  146. return nil
  147. })
  148. invalidCmd := m.SaveDashboardCommand{
  149. FolderId: fakeDash.FolderId,
  150. IsFolder: true,
  151. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  152. "folderId": fakeDash.FolderId,
  153. "title": fakeDash.Title,
  154. }),
  155. }
  156. Convey("Should return an error", func() {
  157. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, invalidCmd, func(sc *scenarioContext) {
  158. CallPostDashboard(sc)
  159. So(sc.resp.Code, ShouldEqual, 400)
  160. })
  161. })
  162. })
  163. })
  164. })
  165. Convey("Given a dashboard with a parent folder which has an acl", t, func() {
  166. fakeDash := m.NewDashboard("Child dash")
  167. fakeDash.Id = 1
  168. fakeDash.FolderId = 1
  169. fakeDash.HasAcl = true
  170. setting.ViewersCanEdit = false
  171. aclMockResp := []*m.DashboardAclInfoDTO{
  172. {
  173. DashboardId: 1,
  174. Permission: m.PERMISSION_EDIT,
  175. UserId: 200,
  176. },
  177. }
  178. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  179. query.Result = aclMockResp
  180. return nil
  181. })
  182. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  183. query.Result = fakeDash
  184. return nil
  185. })
  186. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  187. query.Result = []*m.Team{}
  188. return nil
  189. })
  190. cmd := m.SaveDashboardCommand{
  191. FolderId: fakeDash.FolderId,
  192. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  193. "id": fakeDash.Id,
  194. "folderId": fakeDash.FolderId,
  195. "title": fakeDash.Title,
  196. }),
  197. }
  198. Convey("When user is an Org Viewer and has no permissions for this dashboard", func() {
  199. role := m.ROLE_VIEWER
  200. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  201. sc.handlerFunc = GetDashboard
  202. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  203. Convey("Should be denied access", func() {
  204. So(sc.resp.Code, ShouldEqual, 403)
  205. })
  206. })
  207. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  208. CallDeleteDashboard(sc)
  209. So(sc.resp.Code, ShouldEqual, 403)
  210. })
  211. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  212. CallGetDashboardVersion(sc)
  213. So(sc.resp.Code, ShouldEqual, 403)
  214. })
  215. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  216. CallGetDashboardVersions(sc)
  217. So(sc.resp.Code, ShouldEqual, 403)
  218. })
  219. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  220. CallPostDashboard(sc)
  221. So(sc.resp.Code, ShouldEqual, 403)
  222. })
  223. })
  224. Convey("When user is an Org Editor and has no permissions for this dashboard", func() {
  225. role := m.ROLE_EDITOR
  226. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  227. sc.handlerFunc = GetDashboard
  228. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  229. Convey("Should be denied access", func() {
  230. So(sc.resp.Code, ShouldEqual, 403)
  231. })
  232. })
  233. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  234. CallDeleteDashboard(sc)
  235. So(sc.resp.Code, ShouldEqual, 403)
  236. })
  237. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  238. CallGetDashboardVersion(sc)
  239. So(sc.resp.Code, ShouldEqual, 403)
  240. })
  241. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  242. CallGetDashboardVersions(sc)
  243. So(sc.resp.Code, ShouldEqual, 403)
  244. })
  245. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  246. CallPostDashboard(sc)
  247. So(sc.resp.Code, ShouldEqual, 403)
  248. })
  249. })
  250. Convey("When user is an Org Viewer but has an edit permission", func() {
  251. role := m.ROLE_VIEWER
  252. mockResult := []*m.DashboardAclInfoDTO{
  253. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_EDIT},
  254. }
  255. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  256. query.Result = mockResult
  257. return nil
  258. })
  259. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  260. dash := GetDashboardShouldReturn200(sc)
  261. Convey("Should be able to get dashboard with edit rights", func() {
  262. So(dash.Meta.CanEdit, ShouldBeTrue)
  263. So(dash.Meta.CanSave, ShouldBeTrue)
  264. So(dash.Meta.CanAdmin, ShouldBeFalse)
  265. })
  266. })
  267. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  268. CallDeleteDashboard(sc)
  269. So(sc.resp.Code, ShouldEqual, 200)
  270. })
  271. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  272. CallGetDashboardVersion(sc)
  273. So(sc.resp.Code, ShouldEqual, 200)
  274. })
  275. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  276. CallGetDashboardVersions(sc)
  277. So(sc.resp.Code, ShouldEqual, 200)
  278. })
  279. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  280. CallPostDashboard(sc)
  281. So(sc.resp.Code, ShouldEqual, 200)
  282. })
  283. })
  284. Convey("When user is an Org Viewer and viewers can edit", func() {
  285. role := m.ROLE_VIEWER
  286. setting.ViewersCanEdit = true
  287. mockResult := []*m.DashboardAclInfoDTO{
  288. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_VIEW},
  289. }
  290. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  291. query.Result = mockResult
  292. return nil
  293. })
  294. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  295. dash := GetDashboardShouldReturn200(sc)
  296. Convey("Should be able to get dashboard with edit rights but can save should be false", func() {
  297. So(dash.Meta.CanEdit, ShouldBeTrue)
  298. So(dash.Meta.CanSave, ShouldBeFalse)
  299. So(dash.Meta.CanAdmin, ShouldBeFalse)
  300. })
  301. })
  302. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  303. CallDeleteDashboard(sc)
  304. So(sc.resp.Code, ShouldEqual, 403)
  305. })
  306. })
  307. Convey("When user is an Org Viewer but has an admin permission", func() {
  308. role := m.ROLE_VIEWER
  309. mockResult := []*m.DashboardAclInfoDTO{
  310. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_ADMIN},
  311. }
  312. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  313. query.Result = mockResult
  314. return nil
  315. })
  316. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  317. dash := GetDashboardShouldReturn200(sc)
  318. Convey("Should be able to get dashboard with edit rights", func() {
  319. So(dash.Meta.CanEdit, ShouldBeTrue)
  320. So(dash.Meta.CanSave, ShouldBeTrue)
  321. So(dash.Meta.CanAdmin, ShouldBeTrue)
  322. })
  323. })
  324. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  325. CallDeleteDashboard(sc)
  326. So(sc.resp.Code, ShouldEqual, 200)
  327. })
  328. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  329. CallGetDashboardVersion(sc)
  330. So(sc.resp.Code, ShouldEqual, 200)
  331. })
  332. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  333. CallGetDashboardVersions(sc)
  334. So(sc.resp.Code, ShouldEqual, 200)
  335. })
  336. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  337. CallPostDashboard(sc)
  338. So(sc.resp.Code, ShouldEqual, 200)
  339. })
  340. })
  341. Convey("When user is an Org Editor but has a view permission", func() {
  342. role := m.ROLE_EDITOR
  343. mockResult := []*m.DashboardAclInfoDTO{
  344. {Id: 1, OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_VIEW},
  345. }
  346. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  347. query.Result = mockResult
  348. return nil
  349. })
  350. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  351. dash := GetDashboardShouldReturn200(sc)
  352. Convey("Should not be able to edit or save dashboard", func() {
  353. So(dash.Meta.CanEdit, ShouldBeFalse)
  354. So(dash.Meta.CanSave, ShouldBeFalse)
  355. })
  356. })
  357. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/2", "/api/dashboards/:id", role, func(sc *scenarioContext) {
  358. CallDeleteDashboard(sc)
  359. So(sc.resp.Code, ShouldEqual, 403)
  360. })
  361. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  362. CallGetDashboardVersion(sc)
  363. So(sc.resp.Code, ShouldEqual, 403)
  364. })
  365. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  366. CallGetDashboardVersions(sc)
  367. So(sc.resp.Code, ShouldEqual, 403)
  368. })
  369. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", role, cmd, func(sc *scenarioContext) {
  370. CallPostDashboard(sc)
  371. So(sc.resp.Code, ShouldEqual, 403)
  372. })
  373. })
  374. })
  375. }
  376. func GetDashboardShouldReturn200(sc *scenarioContext) dtos.DashboardFullWithMeta {
  377. CallGetDashboard(sc)
  378. So(sc.resp.Code, ShouldEqual, 200)
  379. dash := dtos.DashboardFullWithMeta{}
  380. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  381. So(err, ShouldBeNil)
  382. return dash
  383. }
  384. func CallGetDashboard(sc *scenarioContext) {
  385. sc.handlerFunc = GetDashboard
  386. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  387. }
  388. func CallGetDashboardVersion(sc *scenarioContext) {
  389. bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
  390. query.Result = &m.DashboardVersion{}
  391. return nil
  392. })
  393. sc.handlerFunc = GetDashboardVersion
  394. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  395. }
  396. func CallGetDashboardVersions(sc *scenarioContext) {
  397. bus.AddHandler("test", func(query *m.GetDashboardVersionsQuery) error {
  398. query.Result = []*m.DashboardVersionDTO{}
  399. return nil
  400. })
  401. sc.handlerFunc = GetDashboardVersions
  402. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  403. }
  404. func CallDeleteDashboard(sc *scenarioContext) {
  405. bus.AddHandler("test", func(cmd *m.DeleteDashboardCommand) error {
  406. return nil
  407. })
  408. sc.handlerFunc = DeleteDashboard
  409. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  410. }
  411. func CallPostDashboard(sc *scenarioContext) {
  412. bus.AddHandler("test", func(cmd *alerting.ValidateDashboardAlertsCommand) error {
  413. return nil
  414. })
  415. bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
  416. cmd.Result = &m.Dashboard{Id: 2, Slug: "Dash", Version: 2}
  417. return nil
  418. })
  419. bus.AddHandler("test", func(cmd *alerting.UpdateDashboardAlertsCommand) error {
  420. return nil
  421. })
  422. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  423. }
  424. func postDashboardScenario(desc string, url string, routePattern string, role m.RoleType, cmd m.SaveDashboardCommand, fn scenarioFunc) {
  425. Convey(desc+" "+url, func() {
  426. defer bus.ClearBusHandlers()
  427. sc := &scenarioContext{
  428. url: url,
  429. }
  430. viewsPath, _ := filepath.Abs("../../public/views")
  431. sc.m = macaron.New()
  432. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  433. Directory: viewsPath,
  434. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  435. }))
  436. sc.m.Use(middleware.GetContextHandler())
  437. sc.m.Use(middleware.Sessioner(&session.Options{}))
  438. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  439. sc.context = c
  440. sc.context.UserId = TestUserID
  441. sc.context.OrgId = TestOrgID
  442. sc.context.OrgRole = role
  443. return PostDashboard(c, cmd)
  444. })
  445. fakeRepo = &fakeDashboardRepo{}
  446. dashboards.SetRepository(fakeRepo)
  447. sc.m.Post(routePattern, sc.defaultHandler)
  448. fn(sc)
  449. })
  450. }