dashboard_test.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "github.com/grafana/grafana/pkg/api/dtos"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/components/simplejson"
  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. // This tests three main scenarios.
  16. // If a user has access to execute an action on a dashboard:
  17. // 1. and the dashboard is in a folder which does not have an acl
  18. // 2. and the dashboard is in a folder which does have an acl
  19. // 3. Post dashboard response tests
  20. func TestDashboardApiEndpoint(t *testing.T) {
  21. Convey("Given a dashboard with a parent folder which does not have an acl", t, func() {
  22. fakeDash := m.NewDashboard("Child dash")
  23. fakeDash.Id = 1
  24. fakeDash.FolderId = 1
  25. fakeDash.HasAcl = false
  26. bus.AddHandler("test", func(query *m.GetDashboardsBySlugQuery) error {
  27. dashboards := []*m.Dashboard{fakeDash}
  28. query.Result = dashboards
  29. return nil
  30. })
  31. var getDashboardQueries []*m.GetDashboardQuery
  32. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  33. query.Result = fakeDash
  34. getDashboardQueries = append(getDashboardQueries, query)
  35. return nil
  36. })
  37. bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
  38. query.Result = false
  39. return nil
  40. })
  41. viewerRole := m.ROLE_VIEWER
  42. editorRole := m.ROLE_EDITOR
  43. aclMockResp := []*m.DashboardAclInfoDTO{
  44. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  45. {Role: &editorRole, Permission: m.PERMISSION_EDIT},
  46. }
  47. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  48. query.Result = aclMockResp
  49. return nil
  50. })
  51. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  52. query.Result = []*m.TeamDTO{}
  53. return nil
  54. })
  55. // This tests two scenarios:
  56. // 1. user is an org viewer
  57. // 2. user is an org editor
  58. Convey("When user is an Org Viewer", func() {
  59. role := m.ROLE_VIEWER
  60. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  61. dash := GetDashboardShouldReturn200(sc)
  62. Convey("Should lookup dashboard by slug", func() {
  63. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  64. })
  65. Convey("Should not be able to edit or save dashboard", func() {
  66. So(dash.Meta.CanEdit, ShouldBeFalse)
  67. So(dash.Meta.CanSave, ShouldBeFalse)
  68. So(dash.Meta.CanAdmin, ShouldBeFalse)
  69. })
  70. })
  71. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  72. dash := GetDashboardShouldReturn200(sc)
  73. Convey("Should lookup dashboard by uid", func() {
  74. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  75. })
  76. Convey("Should not be able to edit or save dashboard", func() {
  77. So(dash.Meta.CanEdit, ShouldBeFalse)
  78. So(dash.Meta.CanSave, ShouldBeFalse)
  79. So(dash.Meta.CanAdmin, ShouldBeFalse)
  80. })
  81. })
  82. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  83. CallDeleteDashboard(sc)
  84. So(sc.resp.Code, ShouldEqual, 403)
  85. Convey("Should lookup dashboard by slug", func() {
  86. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  87. })
  88. })
  89. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  90. CallDeleteDashboardByUID(sc)
  91. So(sc.resp.Code, ShouldEqual, 403)
  92. Convey("Should lookup dashboard by uid", func() {
  93. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  94. })
  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, 403)
  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, 403)
  103. })
  104. })
  105. Convey("When user is an Org Editor", func() {
  106. role := m.ROLE_EDITOR
  107. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  108. dash := GetDashboardShouldReturn200(sc)
  109. Convey("Should lookup dashboard by slug", func() {
  110. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  111. })
  112. Convey("Should be able to edit or save dashboard", func() {
  113. So(dash.Meta.CanEdit, ShouldBeTrue)
  114. So(dash.Meta.CanSave, ShouldBeTrue)
  115. So(dash.Meta.CanAdmin, ShouldBeFalse)
  116. })
  117. })
  118. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  119. dash := GetDashboardShouldReturn200(sc)
  120. Convey("Should lookup dashboard by uid", func() {
  121. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  122. })
  123. Convey("Should be able to edit or save dashboard", func() {
  124. So(dash.Meta.CanEdit, ShouldBeTrue)
  125. So(dash.Meta.CanSave, ShouldBeTrue)
  126. So(dash.Meta.CanAdmin, ShouldBeFalse)
  127. })
  128. })
  129. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  130. CallDeleteDashboard(sc)
  131. So(sc.resp.Code, ShouldEqual, 200)
  132. Convey("Should lookup dashboard by slug", func() {
  133. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  134. })
  135. })
  136. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  137. CallDeleteDashboardByUID(sc)
  138. So(sc.resp.Code, ShouldEqual, 200)
  139. Convey("Should lookup dashboard by uid", func() {
  140. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  141. })
  142. })
  143. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  144. CallGetDashboardVersion(sc)
  145. So(sc.resp.Code, ShouldEqual, 200)
  146. })
  147. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  148. CallGetDashboardVersions(sc)
  149. So(sc.resp.Code, ShouldEqual, 200)
  150. })
  151. })
  152. })
  153. Convey("Given a dashboard with a parent folder which has an acl", t, func() {
  154. fakeDash := m.NewDashboard("Child dash")
  155. fakeDash.Id = 1
  156. fakeDash.FolderId = 1
  157. fakeDash.HasAcl = true
  158. setting.ViewersCanEdit = false
  159. bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
  160. query.Result = false
  161. return nil
  162. })
  163. bus.AddHandler("test", func(query *m.GetDashboardsBySlugQuery) error {
  164. dashboards := []*m.Dashboard{fakeDash}
  165. query.Result = dashboards
  166. return nil
  167. })
  168. aclMockResp := []*m.DashboardAclInfoDTO{
  169. {
  170. DashboardId: 1,
  171. Permission: m.PERMISSION_EDIT,
  172. UserId: 200,
  173. },
  174. }
  175. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  176. query.Result = aclMockResp
  177. return nil
  178. })
  179. var getDashboardQueries []*m.GetDashboardQuery
  180. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  181. query.Result = fakeDash
  182. getDashboardQueries = append(getDashboardQueries, query)
  183. return nil
  184. })
  185. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  186. query.Result = []*m.TeamDTO{}
  187. return nil
  188. })
  189. // This tests six scenarios:
  190. // 1. user is an org viewer AND has no permissions for this dashboard
  191. // 2. user is an org editor AND has no permissions for this dashboard
  192. // 3. user is an org viewer AND has been granted edit permission for the dashboard
  193. // 4. user is an org viewer AND all viewers have edit permission for this dashboard
  194. // 5. user is an org viewer AND has been granted an admin permission
  195. // 6. user is an org editor AND has been granted a view permission
  196. Convey("When user is an Org Viewer and has no permissions for this dashboard", func() {
  197. role := m.ROLE_VIEWER
  198. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  199. sc.handlerFunc = GetDashboard
  200. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  201. Convey("Should lookup dashboard by slug", func() {
  202. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  203. })
  204. Convey("Should be denied access", func() {
  205. So(sc.resp.Code, ShouldEqual, 403)
  206. })
  207. })
  208. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  209. sc.handlerFunc = GetDashboard
  210. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  211. Convey("Should lookup dashboard by uid", func() {
  212. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  213. })
  214. Convey("Should be denied access", func() {
  215. So(sc.resp.Code, ShouldEqual, 403)
  216. })
  217. })
  218. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  219. CallDeleteDashboard(sc)
  220. So(sc.resp.Code, ShouldEqual, 403)
  221. Convey("Should lookup dashboard by slug", func() {
  222. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  223. })
  224. })
  225. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  226. CallDeleteDashboardByUID(sc)
  227. So(sc.resp.Code, ShouldEqual, 403)
  228. Convey("Should lookup dashboard by uid", func() {
  229. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  230. })
  231. })
  232. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  233. CallGetDashboardVersion(sc)
  234. So(sc.resp.Code, ShouldEqual, 403)
  235. })
  236. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  237. CallGetDashboardVersions(sc)
  238. So(sc.resp.Code, ShouldEqual, 403)
  239. })
  240. })
  241. Convey("When user is an Org Editor and has no permissions for this dashboard", func() {
  242. role := m.ROLE_EDITOR
  243. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  244. sc.handlerFunc = GetDashboard
  245. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  246. Convey("Should lookup dashboard by slug", func() {
  247. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  248. })
  249. Convey("Should be denied access", func() {
  250. So(sc.resp.Code, ShouldEqual, 403)
  251. })
  252. })
  253. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  254. sc.handlerFunc = GetDashboard
  255. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  256. Convey("Should lookup dashboard by uid", func() {
  257. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  258. })
  259. Convey("Should be denied access", func() {
  260. So(sc.resp.Code, ShouldEqual, 403)
  261. })
  262. })
  263. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  264. CallDeleteDashboard(sc)
  265. So(sc.resp.Code, ShouldEqual, 403)
  266. Convey("Should lookup dashboard by slug", func() {
  267. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  268. })
  269. })
  270. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  271. CallDeleteDashboardByUID(sc)
  272. So(sc.resp.Code, ShouldEqual, 403)
  273. Convey("Should lookup dashboard by uid", func() {
  274. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  275. })
  276. })
  277. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  278. CallGetDashboardVersion(sc)
  279. So(sc.resp.Code, ShouldEqual, 403)
  280. })
  281. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  282. CallGetDashboardVersions(sc)
  283. So(sc.resp.Code, ShouldEqual, 403)
  284. })
  285. })
  286. Convey("When user is an Org Viewer but has an edit permission", func() {
  287. role := m.ROLE_VIEWER
  288. mockResult := []*m.DashboardAclInfoDTO{
  289. {OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_EDIT},
  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/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  296. dash := GetDashboardShouldReturn200(sc)
  297. Convey("Should lookup dashboard by slug", func() {
  298. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  299. })
  300. Convey("Should be able to get dashboard with edit rights", func() {
  301. So(dash.Meta.CanEdit, ShouldBeTrue)
  302. So(dash.Meta.CanSave, ShouldBeTrue)
  303. So(dash.Meta.CanAdmin, ShouldBeFalse)
  304. })
  305. })
  306. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  307. dash := GetDashboardShouldReturn200(sc)
  308. Convey("Should lookup dashboard by uid", func() {
  309. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  310. })
  311. Convey("Should be able to get dashboard with edit rights", func() {
  312. So(dash.Meta.CanEdit, ShouldBeTrue)
  313. So(dash.Meta.CanSave, ShouldBeTrue)
  314. So(dash.Meta.CanAdmin, ShouldBeFalse)
  315. })
  316. })
  317. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  318. CallDeleteDashboard(sc)
  319. So(sc.resp.Code, ShouldEqual, 200)
  320. Convey("Should lookup dashboard by slug", func() {
  321. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  322. })
  323. })
  324. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  325. CallDeleteDashboardByUID(sc)
  326. So(sc.resp.Code, ShouldEqual, 200)
  327. Convey("Should lookup dashboard by uid", func() {
  328. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  329. })
  330. })
  331. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  332. CallGetDashboardVersion(sc)
  333. So(sc.resp.Code, ShouldEqual, 200)
  334. })
  335. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  336. CallGetDashboardVersions(sc)
  337. So(sc.resp.Code, ShouldEqual, 200)
  338. })
  339. })
  340. Convey("When user is an Org Viewer and viewers can edit", func() {
  341. role := m.ROLE_VIEWER
  342. setting.ViewersCanEdit = true
  343. mockResult := []*m.DashboardAclInfoDTO{
  344. {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/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  351. dash := GetDashboardShouldReturn200(sc)
  352. Convey("Should lookup dashboard by slug", func() {
  353. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  354. })
  355. Convey("Should be able to get dashboard with edit rights but can save should be false", func() {
  356. So(dash.Meta.CanEdit, ShouldBeTrue)
  357. So(dash.Meta.CanSave, ShouldBeFalse)
  358. So(dash.Meta.CanAdmin, ShouldBeFalse)
  359. })
  360. })
  361. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  362. dash := GetDashboardShouldReturn200(sc)
  363. Convey("Should lookup dashboard by uid", func() {
  364. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  365. })
  366. Convey("Should be able to get dashboard with edit rights but can save should be false", func() {
  367. So(dash.Meta.CanEdit, ShouldBeTrue)
  368. So(dash.Meta.CanSave, ShouldBeFalse)
  369. So(dash.Meta.CanAdmin, ShouldBeFalse)
  370. })
  371. })
  372. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  373. CallDeleteDashboard(sc)
  374. So(sc.resp.Code, ShouldEqual, 403)
  375. Convey("Should lookup dashboard by slug", func() {
  376. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  377. })
  378. })
  379. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  380. CallDeleteDashboardByUID(sc)
  381. So(sc.resp.Code, ShouldEqual, 403)
  382. Convey("Should lookup dashboard by uid", func() {
  383. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  384. })
  385. })
  386. })
  387. Convey("When user is an Org Viewer but has an admin permission", func() {
  388. role := m.ROLE_VIEWER
  389. mockResult := []*m.DashboardAclInfoDTO{
  390. {OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_ADMIN},
  391. }
  392. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  393. query.Result = mockResult
  394. return nil
  395. })
  396. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  397. dash := GetDashboardShouldReturn200(sc)
  398. Convey("Should lookup dashboard by slug", func() {
  399. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  400. })
  401. Convey("Should be able to get dashboard with edit rights", func() {
  402. So(dash.Meta.CanEdit, ShouldBeTrue)
  403. So(dash.Meta.CanSave, ShouldBeTrue)
  404. So(dash.Meta.CanAdmin, ShouldBeTrue)
  405. })
  406. })
  407. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  408. dash := GetDashboardShouldReturn200(sc)
  409. Convey("Should lookup dashboard by uid", func() {
  410. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  411. })
  412. Convey("Should be able to get dashboard with edit rights", func() {
  413. So(dash.Meta.CanEdit, ShouldBeTrue)
  414. So(dash.Meta.CanSave, ShouldBeTrue)
  415. So(dash.Meta.CanAdmin, ShouldBeTrue)
  416. })
  417. })
  418. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  419. CallDeleteDashboard(sc)
  420. So(sc.resp.Code, ShouldEqual, 200)
  421. Convey("Should lookup dashboard by slug", func() {
  422. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  423. })
  424. })
  425. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  426. CallDeleteDashboardByUID(sc)
  427. So(sc.resp.Code, ShouldEqual, 200)
  428. Convey("Should lookup dashboard by uid", func() {
  429. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  430. })
  431. })
  432. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  433. CallGetDashboardVersion(sc)
  434. So(sc.resp.Code, ShouldEqual, 200)
  435. })
  436. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  437. CallGetDashboardVersions(sc)
  438. So(sc.resp.Code, ShouldEqual, 200)
  439. })
  440. })
  441. Convey("When user is an Org Editor but has a view permission", func() {
  442. role := m.ROLE_EDITOR
  443. mockResult := []*m.DashboardAclInfoDTO{
  444. {OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_VIEW},
  445. }
  446. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  447. query.Result = mockResult
  448. return nil
  449. })
  450. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  451. dash := GetDashboardShouldReturn200(sc)
  452. Convey("Should lookup dashboard by slug", func() {
  453. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  454. })
  455. Convey("Should not be able to edit or save dashboard", func() {
  456. So(dash.Meta.CanEdit, ShouldBeFalse)
  457. So(dash.Meta.CanSave, ShouldBeFalse)
  458. })
  459. })
  460. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  461. dash := GetDashboardShouldReturn200(sc)
  462. Convey("Should lookup dashboard by uid", func() {
  463. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  464. })
  465. Convey("Should not be able to edit or save dashboard", func() {
  466. So(dash.Meta.CanEdit, ShouldBeFalse)
  467. So(dash.Meta.CanSave, ShouldBeFalse)
  468. })
  469. })
  470. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  471. CallDeleteDashboard(sc)
  472. So(sc.resp.Code, ShouldEqual, 403)
  473. Convey("Should lookup dashboard by slug", func() {
  474. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  475. })
  476. })
  477. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  478. CallDeleteDashboardByUID(sc)
  479. So(sc.resp.Code, ShouldEqual, 403)
  480. Convey("Should lookup dashboard by uid", func() {
  481. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  482. })
  483. })
  484. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  485. CallGetDashboardVersion(sc)
  486. So(sc.resp.Code, ShouldEqual, 403)
  487. })
  488. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  489. CallGetDashboardVersions(sc)
  490. So(sc.resp.Code, ShouldEqual, 403)
  491. })
  492. })
  493. })
  494. Convey("Given two dashboards with the same title in different folders", t, func() {
  495. dashOne := m.NewDashboard("dash")
  496. dashOne.Id = 2
  497. dashOne.FolderId = 1
  498. dashOne.HasAcl = false
  499. dashTwo := m.NewDashboard("dash")
  500. dashTwo.Id = 4
  501. dashTwo.FolderId = 3
  502. dashTwo.HasAcl = false
  503. bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
  504. query.Result = false
  505. return nil
  506. })
  507. bus.AddHandler("test", func(query *m.GetDashboardsBySlugQuery) error {
  508. dashboards := []*m.Dashboard{dashOne, dashTwo}
  509. query.Result = dashboards
  510. return nil
  511. })
  512. role := m.ROLE_EDITOR
  513. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  514. CallDeleteDashboard(sc)
  515. Convey("Should result in 412 Precondition failed", func() {
  516. So(sc.resp.Code, ShouldEqual, 412)
  517. result := sc.ToJSON()
  518. So(result.Get("status").MustString(), ShouldEqual, "multiple-slugs-exists")
  519. So(result.Get("message").MustString(), ShouldEqual, m.ErrDashboardsWithSameSlugExists.Error())
  520. })
  521. })
  522. })
  523. Convey("Post dashboard response tests", t, func() {
  524. // This tests that a valid request returns correct response
  525. Convey("Given a correct request for creating a dashboard", func() {
  526. cmd := m.SaveDashboardCommand{
  527. OrgId: 1,
  528. UserId: 5,
  529. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  530. "title": "Dash",
  531. }),
  532. Overwrite: true,
  533. FolderId: 3,
  534. IsFolder: false,
  535. Message: "msg",
  536. }
  537. mock := &dashboards.FakeDashboardService{
  538. SaveDashboardResult: &m.Dashboard{
  539. Id: 2,
  540. Uid: "uid",
  541. Title: "Dash",
  542. Slug: "dash",
  543. Version: 2,
  544. },
  545. }
  546. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", mock, cmd, func(sc *scenarioContext) {
  547. CallPostDashboardShouldReturnSuccess(sc)
  548. Convey("It should call dashboard service with correct data", func() {
  549. dto := mock.SavedDashboards[0]
  550. So(dto.OrgId, ShouldEqual, cmd.OrgId)
  551. So(dto.User.UserId, ShouldEqual, cmd.UserId)
  552. So(dto.Dashboard.FolderId, ShouldEqual, 3)
  553. So(dto.Dashboard.Title, ShouldEqual, "Dash")
  554. So(dto.Overwrite, ShouldBeTrue)
  555. So(dto.Message, ShouldEqual, "msg")
  556. })
  557. Convey("It should return correct response data", func() {
  558. result := sc.ToJSON()
  559. So(result.Get("status").MustString(), ShouldEqual, "success")
  560. So(result.Get("id").MustInt64(), ShouldEqual, 2)
  561. So(result.Get("uid").MustString(), ShouldEqual, "uid")
  562. So(result.Get("slug").MustString(), ShouldEqual, "dash")
  563. So(result.Get("url").MustString(), ShouldEqual, "/d/uid/dash")
  564. })
  565. })
  566. })
  567. // This tests that invalid requests returns expected error responses
  568. Convey("Given incorrect requests for creating a dashboard", func() {
  569. testCases := []struct {
  570. SaveError error
  571. ExpectedStatusCode int
  572. }{
  573. {SaveError: m.ErrDashboardNotFound, ExpectedStatusCode: 404},
  574. {SaveError: m.ErrFolderNotFound, ExpectedStatusCode: 400},
  575. {SaveError: m.ErrDashboardWithSameUIDExists, ExpectedStatusCode: 400},
  576. {SaveError: m.ErrDashboardWithSameNameInFolderExists, ExpectedStatusCode: 412},
  577. {SaveError: m.ErrDashboardVersionMismatch, ExpectedStatusCode: 412},
  578. {SaveError: m.ErrDashboardTitleEmpty, ExpectedStatusCode: 400},
  579. {SaveError: m.ErrDashboardFolderCannotHaveParent, ExpectedStatusCode: 400},
  580. {SaveError: alerting.ValidationError{Reason: "Mu"}, ExpectedStatusCode: 422},
  581. {SaveError: m.ErrDashboardFailedGenerateUniqueUid, ExpectedStatusCode: 500},
  582. {SaveError: m.ErrDashboardTypeMismatch, ExpectedStatusCode: 400},
  583. {SaveError: m.ErrDashboardFolderWithSameNameAsDashboard, ExpectedStatusCode: 400},
  584. {SaveError: m.ErrDashboardWithSameNameAsFolder, ExpectedStatusCode: 400},
  585. {SaveError: m.ErrDashboardFolderNameExists, ExpectedStatusCode: 400},
  586. {SaveError: m.ErrDashboardUpdateAccessDenied, ExpectedStatusCode: 403},
  587. {SaveError: m.ErrDashboardInvalidUid, ExpectedStatusCode: 400},
  588. {SaveError: m.ErrDashboardUidToLong, ExpectedStatusCode: 400},
  589. {SaveError: m.ErrDashboardCannotSaveProvisionedDashboard, ExpectedStatusCode: 400},
  590. {SaveError: m.UpdatePluginDashboardError{PluginId: "plug"}, ExpectedStatusCode: 412},
  591. }
  592. cmd := m.SaveDashboardCommand{
  593. OrgId: 1,
  594. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  595. "title": "",
  596. }),
  597. }
  598. for _, tc := range testCases {
  599. mock := &dashboards.FakeDashboardService{
  600. SaveDashboardError: tc.SaveError,
  601. }
  602. postDashboardScenario(fmt.Sprintf("Expect '%s' error when calling POST on", tc.SaveError.Error()), "/api/dashboards", "/api/dashboards", mock, cmd, func(sc *scenarioContext) {
  603. CallPostDashboard(sc)
  604. So(sc.resp.Code, ShouldEqual, tc.ExpectedStatusCode)
  605. })
  606. }
  607. })
  608. })
  609. Convey("Given two dashboards being compared", t, func() {
  610. mockResult := []*m.DashboardAclInfoDTO{}
  611. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  612. query.Result = mockResult
  613. return nil
  614. })
  615. bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
  616. query.Result = false
  617. return nil
  618. })
  619. bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
  620. query.Result = &m.DashboardVersion{
  621. Data: simplejson.NewFromAny(map[string]interface{}{
  622. "title": "Dash" + string(query.DashboardId),
  623. }),
  624. }
  625. return nil
  626. })
  627. cmd := dtos.CalculateDiffOptions{
  628. Base: dtos.CalculateDiffTarget{
  629. DashboardId: 1,
  630. Version: 1,
  631. },
  632. New: dtos.CalculateDiffTarget{
  633. DashboardId: 2,
  634. Version: 2,
  635. },
  636. DiffType: "basic",
  637. }
  638. Convey("when user does not have permission", func() {
  639. role := m.ROLE_VIEWER
  640. postDiffScenario("When calling POST on", "/api/dashboards/calculate-diff", "/api/dashboards/calculate-diff", cmd, role, func(sc *scenarioContext) {
  641. CallPostDashboard(sc)
  642. So(sc.resp.Code, ShouldEqual, 403)
  643. })
  644. })
  645. Convey("when user does have permission", func() {
  646. role := m.ROLE_ADMIN
  647. postDiffScenario("When calling POST on", "/api/dashboards/calculate-diff", "/api/dashboards/calculate-diff", cmd, role, func(sc *scenarioContext) {
  648. CallPostDashboard(sc)
  649. So(sc.resp.Code, ShouldEqual, 200)
  650. })
  651. })
  652. })
  653. Convey("Given dashboard in folder being restored should restore to folder", t, func() {
  654. fakeDash := m.NewDashboard("Child dash")
  655. fakeDash.Id = 2
  656. fakeDash.FolderId = 1
  657. fakeDash.HasAcl = false
  658. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  659. query.Result = fakeDash
  660. return nil
  661. })
  662. bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
  663. query.Result = &m.DashboardVersion{
  664. DashboardId: 2,
  665. Version: 1,
  666. Data: fakeDash.Data,
  667. }
  668. return nil
  669. })
  670. mock := &dashboards.FakeDashboardService{
  671. SaveDashboardResult: &m.Dashboard{
  672. Id: 2,
  673. Uid: "uid",
  674. Title: "Dash",
  675. Slug: "dash",
  676. Version: 1,
  677. },
  678. }
  679. cmd := dtos.RestoreDashboardVersionCommand{
  680. Version: 1,
  681. }
  682. restoreDashboardVersionScenario("When calling POST on", "/api/dashboards/id/1/restore", "/api/dashboards/id/:dashboardId/restore", mock, cmd, func(sc *scenarioContext) {
  683. CallRestoreDashboardVersion(sc)
  684. So(sc.resp.Code, ShouldEqual, 200)
  685. dto := mock.SavedDashboards[0]
  686. So(dto.Dashboard.FolderId, ShouldEqual, 1)
  687. So(dto.Dashboard.Title, ShouldEqual, "Child dash")
  688. So(dto.Message, ShouldEqual, "Restored from version 1")
  689. })
  690. })
  691. Convey("Given dashboard in general folder being restored should restore to general folder", t, func() {
  692. fakeDash := m.NewDashboard("Child dash")
  693. fakeDash.Id = 2
  694. fakeDash.HasAcl = false
  695. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  696. query.Result = fakeDash
  697. return nil
  698. })
  699. bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
  700. query.Result = &m.DashboardVersion{
  701. DashboardId: 2,
  702. Version: 1,
  703. Data: fakeDash.Data,
  704. }
  705. return nil
  706. })
  707. mock := &dashboards.FakeDashboardService{
  708. SaveDashboardResult: &m.Dashboard{
  709. Id: 2,
  710. Uid: "uid",
  711. Title: "Dash",
  712. Slug: "dash",
  713. Version: 1,
  714. },
  715. }
  716. cmd := dtos.RestoreDashboardVersionCommand{
  717. Version: 1,
  718. }
  719. restoreDashboardVersionScenario("When calling POST on", "/api/dashboards/id/1/restore", "/api/dashboards/id/:dashboardId/restore", mock, cmd, func(sc *scenarioContext) {
  720. CallRestoreDashboardVersion(sc)
  721. So(sc.resp.Code, ShouldEqual, 200)
  722. dto := mock.SavedDashboards[0]
  723. So(dto.Dashboard.FolderId, ShouldEqual, 0)
  724. So(dto.Dashboard.Title, ShouldEqual, "Child dash")
  725. So(dto.Message, ShouldEqual, "Restored from version 1")
  726. })
  727. })
  728. }
  729. func GetDashboardShouldReturn200(sc *scenarioContext) dtos.DashboardFullWithMeta {
  730. CallGetDashboard(sc)
  731. So(sc.resp.Code, ShouldEqual, 200)
  732. dash := dtos.DashboardFullWithMeta{}
  733. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  734. So(err, ShouldBeNil)
  735. return dash
  736. }
  737. func CallGetDashboard(sc *scenarioContext) {
  738. sc.handlerFunc = GetDashboard
  739. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  740. }
  741. func CallGetDashboardVersion(sc *scenarioContext) {
  742. bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
  743. query.Result = &m.DashboardVersion{}
  744. return nil
  745. })
  746. sc.handlerFunc = GetDashboardVersion
  747. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  748. }
  749. func CallGetDashboardVersions(sc *scenarioContext) {
  750. bus.AddHandler("test", func(query *m.GetDashboardVersionsQuery) error {
  751. query.Result = []*m.DashboardVersionDTO{}
  752. return nil
  753. })
  754. sc.handlerFunc = GetDashboardVersions
  755. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  756. }
  757. func CallDeleteDashboard(sc *scenarioContext) {
  758. bus.AddHandler("test", func(cmd *m.DeleteDashboardCommand) error {
  759. return nil
  760. })
  761. sc.handlerFunc = DeleteDashboard
  762. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  763. }
  764. func CallDeleteDashboardByUID(sc *scenarioContext) {
  765. bus.AddHandler("test", func(cmd *m.DeleteDashboardCommand) error {
  766. return nil
  767. })
  768. sc.handlerFunc = DeleteDashboardByUID
  769. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  770. }
  771. func CallPostDashboard(sc *scenarioContext) {
  772. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  773. }
  774. func CallRestoreDashboardVersion(sc *scenarioContext) {
  775. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  776. }
  777. func CallPostDashboardShouldReturnSuccess(sc *scenarioContext) {
  778. CallPostDashboard(sc)
  779. So(sc.resp.Code, ShouldEqual, 200)
  780. }
  781. func postDashboardScenario(desc string, url string, routePattern string, mock *dashboards.FakeDashboardService, cmd m.SaveDashboardCommand, fn scenarioFunc) {
  782. Convey(desc+" "+url, func() {
  783. defer bus.ClearBusHandlers()
  784. cfg := setting.NewCfg()
  785. cfg.EditorsCanAdmin = false
  786. hs := HTTPServer{
  787. Bus: bus.GetBus(),
  788. Cfg: cfg,
  789. }
  790. sc := setupScenarioContext(url)
  791. sc.defaultHandler = Wrap(func(c *m.ReqContext) Response {
  792. sc.context = c
  793. sc.context.SignedInUser = &m.SignedInUser{OrgId: cmd.OrgId, UserId: cmd.UserId}
  794. return hs.PostDashboard(c, cmd)
  795. })
  796. origNewDashboardService := dashboards.NewService
  797. dashboards.MockDashboardService(mock)
  798. sc.m.Post(routePattern, sc.defaultHandler)
  799. defer func() {
  800. dashboards.NewService = origNewDashboardService
  801. }()
  802. fn(sc)
  803. })
  804. }
  805. func postDiffScenario(desc string, url string, routePattern string, cmd dtos.CalculateDiffOptions, role m.RoleType, fn scenarioFunc) {
  806. Convey(desc+" "+url, func() {
  807. defer bus.ClearBusHandlers()
  808. sc := setupScenarioContext(url)
  809. sc.defaultHandler = Wrap(func(c *m.ReqContext) Response {
  810. sc.context = c
  811. sc.context.SignedInUser = &m.SignedInUser{
  812. OrgId: TestOrgID,
  813. UserId: TestUserID,
  814. }
  815. sc.context.OrgRole = role
  816. return CalculateDashboardDiff(c, cmd)
  817. })
  818. sc.m.Post(routePattern, sc.defaultHandler)
  819. fn(sc)
  820. })
  821. }
  822. func restoreDashboardVersionScenario(desc string, url string, routePattern string, mock *dashboards.FakeDashboardService, cmd dtos.RestoreDashboardVersionCommand, fn scenarioFunc) {
  823. Convey(desc+" "+url, func() {
  824. defer bus.ClearBusHandlers()
  825. hs := HTTPServer{
  826. Cfg: setting.NewCfg(),
  827. Bus: bus.GetBus(),
  828. }
  829. sc := setupScenarioContext(url)
  830. sc.defaultHandler = Wrap(func(c *m.ReqContext) Response {
  831. sc.context = c
  832. sc.context.SignedInUser = &m.SignedInUser{
  833. OrgId: TestOrgID,
  834. UserId: TestUserID,
  835. }
  836. sc.context.OrgRole = m.ROLE_ADMIN
  837. return hs.RestoreDashboardVersion(c, cmd)
  838. })
  839. origNewDashboardService := dashboards.NewService
  840. dashboards.MockDashboardService(mock)
  841. sc.m.Post(routePattern, sc.defaultHandler)
  842. defer func() {
  843. dashboards.NewService = origNewDashboardService
  844. }()
  845. fn(sc)
  846. })
  847. }
  848. func (sc *scenarioContext) ToJSON() *simplejson.Json {
  849. var result *simplejson.Json
  850. err := json.NewDecoder(sc.resp.Body).Decode(&result)
  851. So(err, ShouldBeNil)
  852. return result
  853. }