dashboard_test.go 18 KB

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