dashboard_test.go 31 KB

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