dashboard_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. package sqlstore
  2. import (
  3. "testing"
  4. "github.com/go-xorm/xorm"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/search"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. func TestDashboardDataAccess(t *testing.T) {
  12. var x *xorm.Engine
  13. Convey("Testing DB", t, func() {
  14. x = InitTestDB(t)
  15. Convey("Given saved dashboard", func() {
  16. savedFolder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  17. savedDash := insertTestDashboard("test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
  18. insertTestDashboard("test dash 45", 1, savedFolder.Id, false, "prod")
  19. insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  20. Convey("Should return dashboard model", func() {
  21. So(savedDash.Title, ShouldEqual, "test dash 23")
  22. So(savedDash.Slug, ShouldEqual, "test-dash-23")
  23. So(savedDash.Id, ShouldNotEqual, 0)
  24. So(savedDash.IsFolder, ShouldBeFalse)
  25. So(savedDash.FolderId, ShouldBeGreaterThan, 0)
  26. So(savedFolder.Title, ShouldEqual, "1 test dash folder")
  27. So(savedFolder.Slug, ShouldEqual, "1-test-dash-folder")
  28. So(savedFolder.Id, ShouldNotEqual, 0)
  29. So(savedFolder.IsFolder, ShouldBeTrue)
  30. So(savedFolder.FolderId, ShouldEqual, 0)
  31. })
  32. Convey("Should be able to get dashboard", func() {
  33. query := m.GetDashboardQuery{
  34. Slug: "test-dash-23",
  35. OrgId: 1,
  36. }
  37. err := GetDashboard(&query)
  38. So(err, ShouldBeNil)
  39. So(query.Result.Title, ShouldEqual, "test dash 23")
  40. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  41. So(query.Result.IsFolder, ShouldBeFalse)
  42. })
  43. Convey("Should be able to delete dashboard", func() {
  44. dash := insertTestDashboard("delete me", 1, 0, false, "delete this")
  45. err := DeleteDashboard(&m.DeleteDashboardCommand{
  46. Id: dash.Id,
  47. OrgId: 1,
  48. })
  49. So(err, ShouldBeNil)
  50. })
  51. Convey("Should return error if no dashboard is updated", func() {
  52. cmd := m.SaveDashboardCommand{
  53. OrgId: 1,
  54. Overwrite: true,
  55. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  56. "id": float64(123412321),
  57. "title": "Expect error",
  58. "tags": []interface{}{},
  59. }),
  60. }
  61. err := SaveDashboard(&cmd)
  62. So(err, ShouldNotBeNil)
  63. })
  64. Convey("Should not be able to overwrite dashboard in another org", func() {
  65. query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
  66. GetDashboard(&query)
  67. cmd := m.SaveDashboardCommand{
  68. OrgId: 2,
  69. Overwrite: true,
  70. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  71. "id": float64(query.Result.Id),
  72. "title": "Expect error",
  73. "tags": []interface{}{},
  74. }),
  75. }
  76. err := SaveDashboard(&cmd)
  77. So(err, ShouldNotBeNil)
  78. })
  79. Convey("Should be able to search for dashboard folder", func() {
  80. query := search.FindPersistedDashboardsQuery{
  81. Title: "1 test dash folder",
  82. OrgId: 1,
  83. SignedInUser: &m.SignedInUser{OrgId: 1},
  84. }
  85. err := SearchDashboards(&query)
  86. So(err, ShouldBeNil)
  87. So(len(query.Result), ShouldEqual, 1)
  88. hit := query.Result[0]
  89. So(hit.Type, ShouldEqual, search.DashHitFolder)
  90. })
  91. Convey("Should be able to search for a dashboard folder's children", func() {
  92. query := search.FindPersistedDashboardsQuery{
  93. OrgId: 1,
  94. FolderId: savedFolder.Id,
  95. SignedInUser: &m.SignedInUser{OrgId: 1},
  96. }
  97. err := SearchDashboards(&query)
  98. So(err, ShouldBeNil)
  99. So(len(query.Result), ShouldEqual, 2)
  100. hit := query.Result[0]
  101. So(hit.Id, ShouldEqual, savedDash.Id)
  102. })
  103. Convey("Should be able to search for dashboard by dashboard ids", func() {
  104. Convey("should be able to find two dashboards by id", func() {
  105. query := search.FindPersistedDashboardsQuery{
  106. DashboardIds: []int64{2, 3},
  107. SignedInUser: &m.SignedInUser{OrgId: 1},
  108. }
  109. err := SearchDashboards(&query)
  110. So(err, ShouldBeNil)
  111. So(len(query.Result), ShouldEqual, 2)
  112. hit := query.Result[0]
  113. So(len(hit.Tags), ShouldEqual, 2)
  114. hit2 := query.Result[1]
  115. So(len(hit2.Tags), ShouldEqual, 1)
  116. })
  117. Convey("DashboardIds that does not exists should not cause errors", func() {
  118. query := search.FindPersistedDashboardsQuery{
  119. DashboardIds: []int64{1000},
  120. SignedInUser: &m.SignedInUser{OrgId: 1},
  121. }
  122. err := SearchDashboards(&query)
  123. So(err, ShouldBeNil)
  124. So(len(query.Result), ShouldEqual, 0)
  125. })
  126. })
  127. Convey("Should not be able to save dashboard with same name", func() {
  128. cmd := m.SaveDashboardCommand{
  129. OrgId: 1,
  130. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  131. "id": nil,
  132. "title": "test dash 23",
  133. "tags": []interface{}{},
  134. }),
  135. }
  136. err := SaveDashboard(&cmd)
  137. So(err, ShouldNotBeNil)
  138. })
  139. Convey("Should be able to update dashboard and remove folderId", func() {
  140. cmd := m.SaveDashboardCommand{
  141. OrgId: 1,
  142. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  143. "id": 1,
  144. "title": "folderId",
  145. "tags": []interface{}{},
  146. }),
  147. Overwrite: true,
  148. FolderId: 2,
  149. }
  150. err := SaveDashboard(&cmd)
  151. So(err, ShouldBeNil)
  152. So(cmd.Result.FolderId, ShouldEqual, 2)
  153. cmd = m.SaveDashboardCommand{
  154. OrgId: 1,
  155. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  156. "id": 1,
  157. "title": "folderId",
  158. "tags": []interface{}{},
  159. }),
  160. FolderId: 0,
  161. Overwrite: true,
  162. }
  163. err = SaveDashboard(&cmd)
  164. So(err, ShouldBeNil)
  165. query := m.GetDashboardQuery{
  166. Slug: cmd.Result.Slug,
  167. OrgId: 1,
  168. }
  169. err = GetDashboard(&query)
  170. So(err, ShouldBeNil)
  171. So(query.Result.FolderId, ShouldEqual, 0)
  172. })
  173. Convey("Should be able to delete a dashboard folder and its children", func() {
  174. deleteCmd := &m.DeleteDashboardCommand{Id: savedFolder.Id}
  175. err := DeleteDashboard(deleteCmd)
  176. So(err, ShouldBeNil)
  177. query := search.FindPersistedDashboardsQuery{
  178. OrgId: 1,
  179. FolderId: savedFolder.Id,
  180. SignedInUser: &m.SignedInUser{},
  181. }
  182. err = SearchDashboards(&query)
  183. So(err, ShouldBeNil)
  184. So(len(query.Result), ShouldEqual, 0)
  185. })
  186. Convey("Should be able to get dashboard tags", func() {
  187. query := m.GetDashboardTagsQuery{OrgId: 1}
  188. err := GetDashboardTags(&query)
  189. So(err, ShouldBeNil)
  190. So(len(query.Result), ShouldEqual, 2)
  191. })
  192. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  193. starredDash := insertTestDashboard("starred dash", 1, 0, false)
  194. StarDashboard(&m.StarDashboardCommand{
  195. DashboardId: starredDash.Id,
  196. UserId: 10,
  197. })
  198. StarDashboard(&m.StarDashboardCommand{
  199. DashboardId: savedDash.Id,
  200. UserId: 1,
  201. })
  202. Convey("Should be able to search for starred dashboards", func() {
  203. query := search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: 10, OrgId: 1}, IsStarred: true}
  204. err := SearchDashboards(&query)
  205. So(err, ShouldBeNil)
  206. So(len(query.Result), ShouldEqual, 1)
  207. So(query.Result[0].Title, ShouldEqual, "starred dash")
  208. })
  209. })
  210. })
  211. Convey("Given one dashboard folder with two dashboards and one dashboard in the root folder", func() {
  212. folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  213. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  214. childDash := insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
  215. insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
  216. currentUser := createUser("viewer", "Viewer", false)
  217. Convey("and no acls are set", func() {
  218. Convey("should return all dashboards", func() {
  219. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
  220. err := SearchDashboards(query)
  221. So(err, ShouldBeNil)
  222. So(len(query.Result), ShouldEqual, 2)
  223. So(query.Result[0].Id, ShouldEqual, folder.Id)
  224. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  225. })
  226. })
  227. Convey("and acl is set for dashboard folder", func() {
  228. var otherUser int64 = 999
  229. updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
  230. Convey("should not return folder", func() {
  231. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
  232. err := SearchDashboards(query)
  233. So(err, ShouldBeNil)
  234. So(len(query.Result), ShouldEqual, 1)
  235. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  236. })
  237. Convey("when the user is given permission", func() {
  238. updateTestDashboardWithAcl(folder.Id, currentUser.Id, m.PERMISSION_EDIT)
  239. Convey("should be able to access folder", func() {
  240. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
  241. err := SearchDashboards(query)
  242. So(err, ShouldBeNil)
  243. So(len(query.Result), ShouldEqual, 2)
  244. So(query.Result[0].Id, ShouldEqual, folder.Id)
  245. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  246. })
  247. })
  248. Convey("when the user is an admin", func() {
  249. Convey("should be able to access folder", func() {
  250. query := &search.FindPersistedDashboardsQuery{
  251. SignedInUser: &m.SignedInUser{
  252. UserId: currentUser.Id,
  253. OrgId: 1,
  254. OrgRole: m.ROLE_ADMIN,
  255. },
  256. OrgId: 1,
  257. DashboardIds: []int64{folder.Id, dashInRoot.Id},
  258. }
  259. err := SearchDashboards(query)
  260. So(err, ShouldBeNil)
  261. So(len(query.Result), ShouldEqual, 2)
  262. So(query.Result[0].Id, ShouldEqual, folder.Id)
  263. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  264. })
  265. })
  266. })
  267. Convey("and acl is set for dashboard child and folder has all permissions removed", func() {
  268. var otherUser int64 = 999
  269. aclId := updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
  270. removeAcl(aclId)
  271. updateTestDashboardWithAcl(childDash.Id, otherUser, m.PERMISSION_EDIT)
  272. Convey("should not return folder or child", func() {
  273. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
  274. err := SearchDashboards(query)
  275. So(err, ShouldBeNil)
  276. So(len(query.Result), ShouldEqual, 1)
  277. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  278. })
  279. Convey("when the user is given permission to child", func() {
  280. updateTestDashboardWithAcl(childDash.Id, currentUser.Id, m.PERMISSION_EDIT)
  281. Convey("should be able to search for child dashboard but not folder", func() {
  282. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
  283. err := SearchDashboards(query)
  284. So(err, ShouldBeNil)
  285. So(len(query.Result), ShouldEqual, 2)
  286. So(query.Result[0].Id, ShouldEqual, childDash.Id)
  287. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  288. })
  289. })
  290. Convey("when the user is an admin", func() {
  291. Convey("should be able to search for child dash and folder", func() {
  292. query := &search.FindPersistedDashboardsQuery{
  293. SignedInUser: &m.SignedInUser{
  294. UserId: currentUser.Id,
  295. OrgId: 1,
  296. OrgRole: m.ROLE_ADMIN,
  297. },
  298. OrgId: 1,
  299. DashboardIds: []int64{folder.Id, dashInRoot.Id, childDash.Id},
  300. }
  301. err := SearchDashboards(query)
  302. So(err, ShouldBeNil)
  303. So(len(query.Result), ShouldEqual, 3)
  304. So(query.Result[0].Id, ShouldEqual, folder.Id)
  305. So(query.Result[1].Id, ShouldEqual, childDash.Id)
  306. So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
  307. })
  308. })
  309. })
  310. })
  311. Convey("Given two dashboard folders with one dashboard each and one dashboard in the root folder", func() {
  312. folder1 := insertTestDashboard("1 test dash folder", 1, 0, true, "prod")
  313. folder2 := insertTestDashboard("2 test dash folder", 1, 0, true, "prod")
  314. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod")
  315. childDash1 := insertTestDashboard("child dash 1", 1, folder1.Id, false, "prod")
  316. childDash2 := insertTestDashboard("child dash 2", 1, folder2.Id, false, "prod")
  317. currentUser := createUser("viewer", "Viewer", false)
  318. Convey("and one folder is expanded, the other collapsed", func() {
  319. Convey("should return dashboards in root and expanded folder", func() {
  320. query := &search.FindPersistedDashboardsQuery{ExpandedFolders: []int64{folder1.Id}, SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1}
  321. err := SearchDashboards(query)
  322. So(err, ShouldBeNil)
  323. So(len(query.Result), ShouldEqual, 4)
  324. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  325. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  326. So(query.Result[2].Id, ShouldEqual, childDash1.Id)
  327. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  328. })
  329. })
  330. Convey("and acl is set for one dashboard folder", func() {
  331. var otherUser int64 = 999
  332. updateTestDashboardWithAcl(folder1.Id, otherUser, m.PERMISSION_EDIT)
  333. Convey("and a dashboard is moved from folder without acl to the folder with an acl", func() {
  334. movedDash := moveDashboard(1, childDash2.Data, folder1.Id)
  335. So(movedDash.HasAcl, ShouldBeTrue)
  336. Convey("should not return folder with acl or its children", func() {
  337. query := &search.FindPersistedDashboardsQuery{
  338. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
  339. OrgId: 1,
  340. DashboardIds: []int64{folder1.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  341. }
  342. err := SearchDashboards(query)
  343. So(err, ShouldBeNil)
  344. So(len(query.Result), ShouldEqual, 1)
  345. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  346. })
  347. })
  348. Convey("and a dashboard is moved from folder with acl to the folder without an acl", func() {
  349. movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
  350. So(movedDash.HasAcl, ShouldBeFalse)
  351. Convey("should return folder without acl and its children", func() {
  352. query := &search.FindPersistedDashboardsQuery{
  353. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
  354. OrgId: 1,
  355. DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  356. }
  357. err := SearchDashboards(query)
  358. So(err, ShouldBeNil)
  359. So(len(query.Result), ShouldEqual, 4)
  360. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  361. So(query.Result[1].Id, ShouldEqual, childDash1.Id)
  362. So(query.Result[2].Id, ShouldEqual, childDash2.Id)
  363. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  364. })
  365. })
  366. Convey("and a dashboard with an acl is moved to the folder without an acl", func() {
  367. updateTestDashboardWithAcl(childDash1.Id, otherUser, m.PERMISSION_EDIT)
  368. movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
  369. So(movedDash.HasAcl, ShouldBeTrue)
  370. Convey("should return folder without acl but not the dashboard with acl", func() {
  371. query := &search.FindPersistedDashboardsQuery{
  372. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1},
  373. OrgId: 1,
  374. DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  375. }
  376. err := SearchDashboards(query)
  377. So(err, ShouldBeNil)
  378. So(len(query.Result), ShouldEqual, 3)
  379. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  380. So(query.Result[1].Id, ShouldEqual, childDash2.Id)
  381. So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
  382. })
  383. })
  384. })
  385. })
  386. })
  387. }
  388. func insertTestDashboard(title string, orgId int64, folderId int64, isFolder bool, tags ...interface{}) *m.Dashboard {
  389. cmd := m.SaveDashboardCommand{
  390. OrgId: orgId,
  391. FolderId: folderId,
  392. IsFolder: isFolder,
  393. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  394. "id": nil,
  395. "title": title,
  396. "tags": tags,
  397. }),
  398. }
  399. err := SaveDashboard(&cmd)
  400. So(err, ShouldBeNil)
  401. return cmd.Result
  402. }
  403. func createUser(name string, role string, isAdmin bool) m.User {
  404. setting.AutoAssignOrg = true
  405. setting.AutoAssignOrgRole = role
  406. currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
  407. err := CreateUser(&currentUserCmd)
  408. So(err, ShouldBeNil)
  409. q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id}
  410. GetUserOrgList(&q1)
  411. So(q1.Result[0].Role, ShouldEqual, role)
  412. return currentUserCmd.Result
  413. }
  414. func updateTestDashboardWithAcl(dashId int64, userId int64, permissions m.PermissionType) int64 {
  415. cmd := &m.SetDashboardAclCommand{
  416. OrgId: 1,
  417. UserId: userId,
  418. DashboardId: dashId,
  419. Permission: permissions,
  420. }
  421. err := SetDashboardAcl(cmd)
  422. So(err, ShouldBeNil)
  423. return cmd.Result.Id
  424. }
  425. func removeAcl(aclId int64) {
  426. err := RemoveDashboardAcl(&m.RemoveDashboardAclCommand{AclId: aclId, OrgId: 1})
  427. So(err, ShouldBeNil)
  428. }
  429. func moveDashboard(orgId int64, dashboard *simplejson.Json, newFolderId int64) *m.Dashboard {
  430. cmd := m.SaveDashboardCommand{
  431. OrgId: orgId,
  432. FolderId: newFolderId,
  433. Dashboard: dashboard,
  434. Overwrite: true,
  435. }
  436. err := SaveDashboard(&cmd)
  437. So(err, ShouldBeNil)
  438. return cmd.Result
  439. }