dashboard_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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/dashboards"
  11. "github.com/grafana/grafana/pkg/setting"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. // This tests three main scenarios.
  15. // If a user has access to execute an action on a dashboard:
  16. // 1. and the dashboard is in a folder which does not have an acl
  17. // 2. and the dashboard is in a folder which does have an acl
  18. // 3. Post dashboard response tests
  19. func TestDashboardApiEndpoint(t *testing.T) {
  20. Convey("Given a dashboard with a parent folder which does not have an acl", t, func() {
  21. fakeDash := m.NewDashboard("Child dash")
  22. fakeDash.Id = 1
  23. fakeDash.FolderId = 1
  24. fakeDash.HasAcl = false
  25. bus.AddHandler("test", func(query *m.GetDashboardsBySlugQuery) error {
  26. dashboards := []*m.Dashboard{fakeDash}
  27. query.Result = dashboards
  28. return nil
  29. })
  30. var getDashboardQueries []*m.GetDashboardQuery
  31. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  32. query.Result = fakeDash
  33. getDashboardQueries = append(getDashboardQueries, query)
  34. return nil
  35. })
  36. bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
  37. query.Result = false
  38. return nil
  39. })
  40. viewerRole := m.ROLE_VIEWER
  41. editorRole := m.ROLE_EDITOR
  42. aclMockResp := []*m.DashboardAclInfoDTO{
  43. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  44. {Role: &editorRole, Permission: m.PERMISSION_EDIT},
  45. }
  46. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  47. query.Result = aclMockResp
  48. return nil
  49. })
  50. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  51. query.Result = []*m.TeamDTO{}
  52. return nil
  53. })
  54. // This tests two scenarios:
  55. // 1. user is an org viewer
  56. // 2. user is an org editor
  57. Convey("When user is an Org Viewer", func() {
  58. role := m.ROLE_VIEWER
  59. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  60. dash := GetDashboardShouldReturn200(sc)
  61. Convey("Should lookup dashboard by slug", func() {
  62. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  63. })
  64. Convey("Should not be able to edit or save dashboard", func() {
  65. So(dash.Meta.CanEdit, ShouldBeFalse)
  66. So(dash.Meta.CanSave, ShouldBeFalse)
  67. So(dash.Meta.CanAdmin, ShouldBeFalse)
  68. })
  69. })
  70. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  71. dash := GetDashboardShouldReturn200(sc)
  72. Convey("Should lookup dashboard by uid", func() {
  73. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  74. })
  75. Convey("Should not be able to edit or save dashboard", func() {
  76. So(dash.Meta.CanEdit, ShouldBeFalse)
  77. So(dash.Meta.CanSave, ShouldBeFalse)
  78. So(dash.Meta.CanAdmin, ShouldBeFalse)
  79. })
  80. })
  81. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  82. CallDeleteDashboard(sc)
  83. So(sc.resp.Code, ShouldEqual, 403)
  84. Convey("Should lookup dashboard by slug", func() {
  85. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  86. })
  87. })
  88. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  89. CallDeleteDashboardByUID(sc)
  90. So(sc.resp.Code, ShouldEqual, 403)
  91. Convey("Should lookup dashboard by uid", func() {
  92. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  93. })
  94. })
  95. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  96. CallGetDashboardVersion(sc)
  97. So(sc.resp.Code, ShouldEqual, 403)
  98. })
  99. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  100. CallGetDashboardVersions(sc)
  101. So(sc.resp.Code, ShouldEqual, 403)
  102. })
  103. })
  104. Convey("When user is an Org Editor", func() {
  105. role := m.ROLE_EDITOR
  106. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  107. dash := GetDashboardShouldReturn200(sc)
  108. Convey("Should lookup dashboard by slug", func() {
  109. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  110. })
  111. Convey("Should be able to edit or save dashboard", func() {
  112. So(dash.Meta.CanEdit, ShouldBeTrue)
  113. So(dash.Meta.CanSave, ShouldBeTrue)
  114. So(dash.Meta.CanAdmin, ShouldBeFalse)
  115. })
  116. })
  117. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  118. dash := GetDashboardShouldReturn200(sc)
  119. Convey("Should lookup dashboard by uid", func() {
  120. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  121. })
  122. Convey("Should be able to edit or save dashboard", func() {
  123. So(dash.Meta.CanEdit, ShouldBeTrue)
  124. So(dash.Meta.CanSave, ShouldBeTrue)
  125. So(dash.Meta.CanAdmin, ShouldBeFalse)
  126. })
  127. })
  128. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  129. CallDeleteDashboard(sc)
  130. So(sc.resp.Code, ShouldEqual, 200)
  131. Convey("Should lookup dashboard by slug", func() {
  132. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  133. })
  134. })
  135. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  136. CallDeleteDashboardByUID(sc)
  137. So(sc.resp.Code, ShouldEqual, 200)
  138. Convey("Should lookup dashboard by uid", func() {
  139. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  140. })
  141. })
  142. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  143. CallGetDashboardVersion(sc)
  144. So(sc.resp.Code, ShouldEqual, 200)
  145. })
  146. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  147. CallGetDashboardVersions(sc)
  148. So(sc.resp.Code, ShouldEqual, 200)
  149. })
  150. })
  151. })
  152. Convey("Given a dashboard with a parent folder which has an acl", t, func() {
  153. fakeDash := m.NewDashboard("Child dash")
  154. fakeDash.Id = 1
  155. fakeDash.FolderId = 1
  156. fakeDash.HasAcl = true
  157. setting.ViewersCanEdit = false
  158. bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
  159. query.Result = false
  160. return nil
  161. })
  162. bus.AddHandler("test", func(query *m.GetDashboardsBySlugQuery) error {
  163. dashboards := []*m.Dashboard{fakeDash}
  164. query.Result = dashboards
  165. return nil
  166. })
  167. aclMockResp := []*m.DashboardAclInfoDTO{
  168. {
  169. DashboardId: 1,
  170. Permission: m.PERMISSION_EDIT,
  171. UserId: 200,
  172. },
  173. }
  174. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  175. query.Result = aclMockResp
  176. return nil
  177. })
  178. var getDashboardQueries []*m.GetDashboardQuery
  179. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  180. query.Result = fakeDash
  181. getDashboardQueries = append(getDashboardQueries, query)
  182. return nil
  183. })
  184. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  185. query.Result = []*m.TeamDTO{}
  186. return nil
  187. })
  188. // This tests six scenarios:
  189. // 1. user is an org viewer AND has no permissions for this dashboard
  190. // 2. user is an org editor AND has no permissions for this dashboard
  191. // 3. user is an org viewer AND has been granted edit permission for the dashboard
  192. // 4. user is an org viewer AND all viewers have edit permission for this dashboard
  193. // 5. user is an org viewer AND has been granted an admin permission
  194. // 6. user is an org editor AND has been granted a view permission
  195. Convey("When user is an Org Viewer and has no permissions for this dashboard", func() {
  196. role := m.ROLE_VIEWER
  197. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  198. sc.handlerFunc = GetDashboard
  199. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  200. Convey("Should lookup dashboard by slug", func() {
  201. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  202. })
  203. Convey("Should be denied access", func() {
  204. So(sc.resp.Code, ShouldEqual, 403)
  205. })
  206. })
  207. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  208. sc.handlerFunc = GetDashboard
  209. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  210. Convey("Should lookup dashboard by uid", func() {
  211. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  212. })
  213. Convey("Should be denied access", func() {
  214. So(sc.resp.Code, ShouldEqual, 403)
  215. })
  216. })
  217. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  218. CallDeleteDashboard(sc)
  219. So(sc.resp.Code, ShouldEqual, 403)
  220. Convey("Should lookup dashboard by slug", func() {
  221. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  222. })
  223. })
  224. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  225. CallDeleteDashboardByUID(sc)
  226. So(sc.resp.Code, ShouldEqual, 403)
  227. Convey("Should lookup dashboard by uid", func() {
  228. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  229. })
  230. })
  231. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  232. CallGetDashboardVersion(sc)
  233. So(sc.resp.Code, ShouldEqual, 403)
  234. })
  235. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  236. CallGetDashboardVersions(sc)
  237. So(sc.resp.Code, ShouldEqual, 403)
  238. })
  239. })
  240. Convey("When user is an Org Editor and has no permissions for this dashboard", func() {
  241. role := m.ROLE_EDITOR
  242. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  243. sc.handlerFunc = GetDashboard
  244. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  245. Convey("Should lookup dashboard by slug", func() {
  246. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  247. })
  248. Convey("Should be denied access", func() {
  249. So(sc.resp.Code, ShouldEqual, 403)
  250. })
  251. })
  252. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  253. sc.handlerFunc = GetDashboard
  254. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  255. Convey("Should lookup dashboard by uid", func() {
  256. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  257. })
  258. Convey("Should be denied access", func() {
  259. So(sc.resp.Code, ShouldEqual, 403)
  260. })
  261. })
  262. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  263. CallDeleteDashboard(sc)
  264. So(sc.resp.Code, ShouldEqual, 403)
  265. Convey("Should lookup dashboard by slug", func() {
  266. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  267. })
  268. })
  269. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  270. CallDeleteDashboardByUID(sc)
  271. So(sc.resp.Code, ShouldEqual, 403)
  272. Convey("Should lookup dashboard by uid", func() {
  273. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  274. })
  275. })
  276. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  277. CallGetDashboardVersion(sc)
  278. So(sc.resp.Code, ShouldEqual, 403)
  279. })
  280. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  281. CallGetDashboardVersions(sc)
  282. So(sc.resp.Code, ShouldEqual, 403)
  283. })
  284. })
  285. Convey("When user is an Org Viewer but has an edit permission", func() {
  286. role := m.ROLE_VIEWER
  287. mockResult := []*m.DashboardAclInfoDTO{
  288. {OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_EDIT},
  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/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  295. dash := GetDashboardShouldReturn200(sc)
  296. Convey("Should lookup dashboard by slug", func() {
  297. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  298. })
  299. Convey("Should be able to get dashboard with edit rights", func() {
  300. So(dash.Meta.CanEdit, ShouldBeTrue)
  301. So(dash.Meta.CanSave, ShouldBeTrue)
  302. So(dash.Meta.CanAdmin, ShouldBeFalse)
  303. })
  304. })
  305. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  306. dash := GetDashboardShouldReturn200(sc)
  307. Convey("Should lookup dashboard by uid", func() {
  308. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  309. })
  310. Convey("Should be able to get dashboard with edit rights", func() {
  311. So(dash.Meta.CanEdit, ShouldBeTrue)
  312. So(dash.Meta.CanSave, ShouldBeTrue)
  313. So(dash.Meta.CanAdmin, ShouldBeFalse)
  314. })
  315. })
  316. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  317. CallDeleteDashboard(sc)
  318. So(sc.resp.Code, ShouldEqual, 200)
  319. Convey("Should lookup dashboard by slug", func() {
  320. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  321. })
  322. })
  323. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  324. CallDeleteDashboardByUID(sc)
  325. So(sc.resp.Code, ShouldEqual, 200)
  326. Convey("Should lookup dashboard by uid", func() {
  327. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  328. })
  329. })
  330. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  331. CallGetDashboardVersion(sc)
  332. So(sc.resp.Code, ShouldEqual, 200)
  333. })
  334. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  335. CallGetDashboardVersions(sc)
  336. So(sc.resp.Code, ShouldEqual, 200)
  337. })
  338. })
  339. Convey("When user is an Org Viewer and viewers can edit", func() {
  340. role := m.ROLE_VIEWER
  341. setting.ViewersCanEdit = true
  342. mockResult := []*m.DashboardAclInfoDTO{
  343. {OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_VIEW},
  344. }
  345. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  346. query.Result = mockResult
  347. return nil
  348. })
  349. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  350. dash := GetDashboardShouldReturn200(sc)
  351. Convey("Should lookup dashboard by slug", func() {
  352. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  353. })
  354. Convey("Should be able to get dashboard with edit rights but can save should be false", func() {
  355. So(dash.Meta.CanEdit, ShouldBeTrue)
  356. So(dash.Meta.CanSave, ShouldBeFalse)
  357. So(dash.Meta.CanAdmin, ShouldBeFalse)
  358. })
  359. })
  360. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  361. dash := GetDashboardShouldReturn200(sc)
  362. Convey("Should lookup dashboard by uid", func() {
  363. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  364. })
  365. Convey("Should be able to get dashboard with edit rights but can save should be false", func() {
  366. So(dash.Meta.CanEdit, ShouldBeTrue)
  367. So(dash.Meta.CanSave, ShouldBeFalse)
  368. So(dash.Meta.CanAdmin, ShouldBeFalse)
  369. })
  370. })
  371. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  372. CallDeleteDashboard(sc)
  373. So(sc.resp.Code, ShouldEqual, 403)
  374. Convey("Should lookup dashboard by slug", func() {
  375. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  376. })
  377. })
  378. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  379. CallDeleteDashboardByUID(sc)
  380. So(sc.resp.Code, ShouldEqual, 403)
  381. Convey("Should lookup dashboard by uid", func() {
  382. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  383. })
  384. })
  385. })
  386. Convey("When user is an Org Viewer but has an admin permission", func() {
  387. role := m.ROLE_VIEWER
  388. mockResult := []*m.DashboardAclInfoDTO{
  389. {OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_ADMIN},
  390. }
  391. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  392. query.Result = mockResult
  393. return nil
  394. })
  395. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  396. dash := GetDashboardShouldReturn200(sc)
  397. Convey("Should lookup dashboard by slug", func() {
  398. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  399. })
  400. Convey("Should be able to get dashboard with edit rights", func() {
  401. So(dash.Meta.CanEdit, ShouldBeTrue)
  402. So(dash.Meta.CanSave, ShouldBeTrue)
  403. So(dash.Meta.CanAdmin, ShouldBeTrue)
  404. })
  405. })
  406. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  407. dash := GetDashboardShouldReturn200(sc)
  408. Convey("Should lookup dashboard by uid", func() {
  409. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  410. })
  411. Convey("Should be able to get dashboard with edit rights", func() {
  412. So(dash.Meta.CanEdit, ShouldBeTrue)
  413. So(dash.Meta.CanSave, ShouldBeTrue)
  414. So(dash.Meta.CanAdmin, ShouldBeTrue)
  415. })
  416. })
  417. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  418. CallDeleteDashboard(sc)
  419. So(sc.resp.Code, ShouldEqual, 200)
  420. Convey("Should lookup dashboard by slug", func() {
  421. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  422. })
  423. })
  424. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  425. CallDeleteDashboardByUID(sc)
  426. So(sc.resp.Code, ShouldEqual, 200)
  427. Convey("Should lookup dashboard by uid", func() {
  428. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  429. })
  430. })
  431. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  432. CallGetDashboardVersion(sc)
  433. So(sc.resp.Code, ShouldEqual, 200)
  434. })
  435. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  436. CallGetDashboardVersions(sc)
  437. So(sc.resp.Code, ShouldEqual, 200)
  438. })
  439. })
  440. Convey("When user is an Org Editor but has a view permission", func() {
  441. role := m.ROLE_EDITOR
  442. mockResult := []*m.DashboardAclInfoDTO{
  443. {OrgId: 1, DashboardId: 2, UserId: 1, Permission: m.PERMISSION_VIEW},
  444. }
  445. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  446. query.Result = mockResult
  447. return nil
  448. })
  449. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  450. dash := GetDashboardShouldReturn200(sc)
  451. Convey("Should lookup dashboard by slug", func() {
  452. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  453. })
  454. Convey("Should not be able to edit or save dashboard", func() {
  455. So(dash.Meta.CanEdit, ShouldBeFalse)
  456. So(dash.Meta.CanSave, ShouldBeFalse)
  457. })
  458. })
  459. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  460. dash := GetDashboardShouldReturn200(sc)
  461. Convey("Should lookup dashboard by uid", func() {
  462. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  463. })
  464. Convey("Should not be able to edit or save dashboard", func() {
  465. So(dash.Meta.CanEdit, ShouldBeFalse)
  466. So(dash.Meta.CanSave, ShouldBeFalse)
  467. })
  468. })
  469. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/child-dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  470. CallDeleteDashboard(sc)
  471. So(sc.resp.Code, ShouldEqual, 403)
  472. Convey("Should lookup dashboard by slug", func() {
  473. So(getDashboardQueries[0].Slug, ShouldEqual, "child-dash")
  474. })
  475. })
  476. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/uid/abcdefghi", "/api/dashboards/uid/:uid", role, func(sc *scenarioContext) {
  477. CallDeleteDashboardByUID(sc)
  478. So(sc.resp.Code, ShouldEqual, 403)
  479. Convey("Should lookup dashboard by uid", func() {
  480. So(getDashboardQueries[0].Uid, ShouldEqual, "abcdefghi")
  481. })
  482. })
  483. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions/1", "/api/dashboards/id/:dashboardId/versions/:id", role, func(sc *scenarioContext) {
  484. CallGetDashboardVersion(sc)
  485. So(sc.resp.Code, ShouldEqual, 403)
  486. })
  487. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/versions", "/api/dashboards/id/:dashboardId/versions", role, func(sc *scenarioContext) {
  488. CallGetDashboardVersions(sc)
  489. So(sc.resp.Code, ShouldEqual, 403)
  490. })
  491. })
  492. })
  493. Convey("Given two dashboards with the same title in different folders", t, func() {
  494. dashOne := m.NewDashboard("dash")
  495. dashOne.Id = 2
  496. dashOne.FolderId = 1
  497. dashOne.HasAcl = false
  498. dashTwo := m.NewDashboard("dash")
  499. dashTwo.Id = 4
  500. dashTwo.FolderId = 3
  501. dashTwo.HasAcl = false
  502. bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
  503. query.Result = false
  504. return nil
  505. })
  506. bus.AddHandler("test", func(query *m.GetDashboardsBySlugQuery) error {
  507. dashboards := []*m.Dashboard{dashOne, dashTwo}
  508. query.Result = dashboards
  509. return nil
  510. })
  511. role := m.ROLE_EDITOR
  512. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/db/dash", "/api/dashboards/db/:slug", role, func(sc *scenarioContext) {
  513. CallDeleteDashboard(sc)
  514. Convey("Should result in 412 Precondition failed", func() {
  515. So(sc.resp.Code, ShouldEqual, 412)
  516. result := sc.ToJSON()
  517. So(result.Get("status").MustString(), ShouldEqual, "multiple-slugs-exists")
  518. So(result.Get("message").MustString(), ShouldEqual, m.ErrDashboardsWithSameSlugExists.Error())
  519. })
  520. })
  521. })
  522. Convey("Post dashboard response tests", t, func() {
  523. // This tests that a valid request returns correct response
  524. Convey("Given a correct request for creating a dashboard", func() {
  525. cmd := m.SaveDashboardCommand{
  526. OrgId: 1,
  527. UserId: 5,
  528. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  529. "title": "Dash",
  530. }),
  531. Overwrite: true,
  532. FolderId: 3,
  533. IsFolder: false,
  534. Message: "msg",
  535. }
  536. mock := &dashboards.FakeDashboardService{
  537. SaveDashboardResult: &m.Dashboard{
  538. Id: 2,
  539. Uid: "uid",
  540. Title: "Dash",
  541. Slug: "dash",
  542. Version: 2,
  543. },
  544. }
  545. postDashboardScenario("When calling POST on", "/api/dashboards", "/api/dashboards", mock, cmd, func(sc *scenarioContext) {
  546. CallPostDashboardShouldReturnSuccess(sc)
  547. Convey("It should call dashboard service with correct data", func() {
  548. dto := mock.SavedDashboards[0]
  549. So(dto.OrgId, ShouldEqual, cmd.OrgId)
  550. So(dto.User.UserId, ShouldEqual, cmd.UserId)
  551. So(dto.Dashboard.FolderId, ShouldEqual, 3)
  552. So(dto.Dashboard.Title, ShouldEqual, "Dash")
  553. So(dto.Overwrite, ShouldBeTrue)
  554. So(dto.Message, ShouldEqual, "msg")
  555. })
  556. Convey("It should return correct response data", func() {
  557. result := sc.ToJSON()
  558. So(result.Get("status").MustString(), ShouldEqual, "success")
  559. So(result.Get("id").MustInt64(), ShouldEqual, 2)
  560. So(result.Get("uid").MustString(), ShouldEqual, "uid")
  561. So(result.Get("slug").MustString(), ShouldEqual, "dash")
  562. So(result.Get("url").MustString(), ShouldEqual, "/d/uid/dash")
  563. })
  564. })
  565. })
  566. // This tests that invalid requests returns expected error responses
  567. Convey("Given incorrect requests for creating a dashboard", func() {
  568. testCases := []struct {
  569. SaveError error
  570. ExpectedStatusCode int
  571. }{
  572. {SaveError: m.ErrDashboardNotFound, ExpectedStatusCode: 404},
  573. {SaveError: m.ErrFolderNotFound, ExpectedStatusCode: 400},
  574. {SaveError: m.ErrDashboardWithSameUIDExists, ExpectedStatusCode: 400},
  575. {SaveError: m.ErrDashboardWithSameNameInFolderExists, ExpectedStatusCode: 412},
  576. {SaveError: m.ErrDashboardVersionMismatch, ExpectedStatusCode: 412},
  577. {SaveError: m.ErrDashboardTitleEmpty, ExpectedStatusCode: 400},
  578. {SaveError: m.ErrDashboardFolderCannotHaveParent, ExpectedStatusCode: 400},
  579. {SaveError: m.ErrDashboardContainsInvalidAlertData, ExpectedStatusCode: 500},
  580. {SaveError: m.ErrDashboardFailedToUpdateAlertData, ExpectedStatusCode: 500},
  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. }
  654. func GetDashboardShouldReturn200(sc *scenarioContext) dtos.DashboardFullWithMeta {
  655. CallGetDashboard(sc)
  656. So(sc.resp.Code, ShouldEqual, 200)
  657. dash := dtos.DashboardFullWithMeta{}
  658. err := json.NewDecoder(sc.resp.Body).Decode(&dash)
  659. So(err, ShouldBeNil)
  660. return dash
  661. }
  662. func CallGetDashboard(sc *scenarioContext) {
  663. sc.handlerFunc = GetDashboard
  664. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  665. }
  666. func CallGetDashboardVersion(sc *scenarioContext) {
  667. bus.AddHandler("test", func(query *m.GetDashboardVersionQuery) error {
  668. query.Result = &m.DashboardVersion{}
  669. return nil
  670. })
  671. sc.handlerFunc = GetDashboardVersion
  672. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  673. }
  674. func CallGetDashboardVersions(sc *scenarioContext) {
  675. bus.AddHandler("test", func(query *m.GetDashboardVersionsQuery) error {
  676. query.Result = []*m.DashboardVersionDTO{}
  677. return nil
  678. })
  679. sc.handlerFunc = GetDashboardVersions
  680. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  681. }
  682. func CallDeleteDashboard(sc *scenarioContext) {
  683. bus.AddHandler("test", func(cmd *m.DeleteDashboardCommand) error {
  684. return nil
  685. })
  686. sc.handlerFunc = DeleteDashboard
  687. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  688. }
  689. func CallDeleteDashboardByUID(sc *scenarioContext) {
  690. bus.AddHandler("test", func(cmd *m.DeleteDashboardCommand) error {
  691. return nil
  692. })
  693. sc.handlerFunc = DeleteDashboardByUID
  694. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  695. }
  696. func CallPostDashboard(sc *scenarioContext) {
  697. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  698. }
  699. func CallPostDashboardShouldReturnSuccess(sc *scenarioContext) {
  700. CallPostDashboard(sc)
  701. So(sc.resp.Code, ShouldEqual, 200)
  702. }
  703. func postDashboardScenario(desc string, url string, routePattern string, mock *dashboards.FakeDashboardService, cmd m.SaveDashboardCommand, fn scenarioFunc) {
  704. Convey(desc+" "+url, func() {
  705. defer bus.ClearBusHandlers()
  706. sc := setupScenarioContext(url)
  707. sc.defaultHandler = Wrap(func(c *m.ReqContext) Response {
  708. sc.context = c
  709. sc.context.SignedInUser = &m.SignedInUser{OrgId: cmd.OrgId, UserId: cmd.UserId}
  710. return PostDashboard(c, cmd)
  711. })
  712. origNewDashboardService := dashboards.NewService
  713. dashboards.MockDashboardService(mock)
  714. sc.m.Post(routePattern, sc.defaultHandler)
  715. defer func() {
  716. dashboards.NewService = origNewDashboardService
  717. }()
  718. fn(sc)
  719. })
  720. }
  721. func postDiffScenario(desc string, url string, routePattern string, cmd dtos.CalculateDiffOptions, role m.RoleType, fn scenarioFunc) {
  722. Convey(desc+" "+url, func() {
  723. defer bus.ClearBusHandlers()
  724. sc := setupScenarioContext(url)
  725. sc.defaultHandler = Wrap(func(c *m.ReqContext) Response {
  726. sc.context = c
  727. sc.context.SignedInUser = &m.SignedInUser{
  728. OrgId: TestOrgID,
  729. UserId: TestUserID,
  730. }
  731. sc.context.OrgRole = role
  732. return CalculateDashboardDiff(c, cmd)
  733. })
  734. sc.m.Post(routePattern, sc.defaultHandler)
  735. fn(sc)
  736. })
  737. }
  738. func (sc *scenarioContext) ToJSON() *simplejson.Json {
  739. var result *simplejson.Json
  740. err := json.NewDecoder(sc.resp.Body).Decode(&result)
  741. So(err, ShouldBeNil)
  742. return result
  743. }