dashboard_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package sqlstore
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/search"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. func insertTestDashboard(title string, orgId int64, parentId int64, isFolder bool, tags ...interface{}) *m.Dashboard {
  11. cmd := m.SaveDashboardCommand{
  12. OrgId: orgId,
  13. ParentId: parentId,
  14. IsFolder: isFolder,
  15. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  16. "id": nil,
  17. "title": title,
  18. "tags": tags,
  19. }),
  20. }
  21. err := SaveDashboard(&cmd)
  22. So(err, ShouldBeNil)
  23. return cmd.Result
  24. }
  25. func TestDashboardDataAccess(t *testing.T) {
  26. Convey("Testing DB", t, func() {
  27. InitTestDB(t)
  28. Convey("Given saved dashboard", func() {
  29. savedFolder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  30. savedDash := insertTestDashboard("test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
  31. insertTestDashboard("test dash 45", 1, savedFolder.Id, false, "prod")
  32. insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  33. Convey("Should return dashboard model", func() {
  34. So(savedDash.Title, ShouldEqual, "test dash 23")
  35. So(savedDash.Slug, ShouldEqual, "test-dash-23")
  36. So(savedDash.Id, ShouldNotEqual, 0)
  37. So(savedDash.IsFolder, ShouldBeFalse)
  38. So(savedDash.ParentId, ShouldBeGreaterThan, 0)
  39. So(savedFolder.Title, ShouldEqual, "1 test dash folder")
  40. So(savedFolder.Slug, ShouldEqual, "1-test-dash-folder")
  41. So(savedFolder.Id, ShouldNotEqual, 0)
  42. So(savedFolder.IsFolder, ShouldBeTrue)
  43. So(savedFolder.ParentId, ShouldEqual, 0)
  44. })
  45. Convey("Should be able to get dashboard", func() {
  46. query := m.GetDashboardQuery{
  47. Slug: "test-dash-23",
  48. OrgId: 1,
  49. }
  50. err := GetDashboard(&query)
  51. So(err, ShouldBeNil)
  52. So(query.Result.Title, ShouldEqual, "test dash 23")
  53. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  54. So(query.Result.IsFolder, ShouldBeFalse)
  55. })
  56. Convey("Should be able to delete dashboard", func() {
  57. dash := insertTestDashboard("delete me", 1, 0, false, "delete this")
  58. err := DeleteDashboard(&m.DeleteDashboardCommand{
  59. Id: dash.Id,
  60. OrgId: 1,
  61. })
  62. So(err, ShouldBeNil)
  63. })
  64. Convey("Should return error if no dashboard is updated", func() {
  65. cmd := m.SaveDashboardCommand{
  66. OrgId: 1,
  67. Overwrite: true,
  68. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  69. "id": float64(123412321),
  70. "title": "Expect error",
  71. "tags": []interface{}{},
  72. }),
  73. }
  74. err := SaveDashboard(&cmd)
  75. So(err, ShouldNotBeNil)
  76. })
  77. Convey("Should not be able to overwrite dashboard in another org", func() {
  78. query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
  79. GetDashboard(&query)
  80. cmd := m.SaveDashboardCommand{
  81. OrgId: 2,
  82. Overwrite: true,
  83. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  84. "id": float64(query.Result.Id),
  85. "title": "Expect error",
  86. "tags": []interface{}{},
  87. }),
  88. }
  89. err := SaveDashboard(&cmd)
  90. So(err, ShouldNotBeNil)
  91. })
  92. Convey("Should be able to search for dashboard and return in folder hierarchy", func() {
  93. query := search.FindPersistedDashboardsQuery{
  94. Title: "test dash 23",
  95. OrgId: 1,
  96. Mode: "tree",
  97. SignedInUser: &m.SignedInUser{OrgId: 1},
  98. }
  99. err := SearchDashboards(&query)
  100. So(err, ShouldBeNil)
  101. So(len(query.Result), ShouldEqual, 1)
  102. hit := query.Result[0].Dashboards[0]
  103. So(len(hit.Tags), ShouldEqual, 2)
  104. So(hit.Type, ShouldEqual, search.DashHitDB)
  105. So(hit.ParentId, ShouldBeGreaterThan, 0)
  106. })
  107. Convey("Should be able to search for dashboard folder", func() {
  108. query := search.FindPersistedDashboardsQuery{
  109. Title: "1 test dash folder",
  110. OrgId: 1,
  111. SignedInUser: &m.SignedInUser{OrgId: 1},
  112. }
  113. err := SearchDashboards(&query)
  114. So(err, ShouldBeNil)
  115. So(len(query.Result), ShouldEqual, 1)
  116. hit := query.Result[0]
  117. So(hit.Type, ShouldEqual, search.DashHitFolder)
  118. })
  119. Convey("Should be able to search for a dashboard folder's children", func() {
  120. query := search.FindPersistedDashboardsQuery{
  121. OrgId: 1,
  122. ParentId: savedFolder.Id,
  123. SignedInUser: &m.SignedInUser{OrgId: 1},
  124. }
  125. err := SearchDashboards(&query)
  126. So(err, ShouldBeNil)
  127. So(len(query.Result), ShouldEqual, 2)
  128. hit := query.Result[0]
  129. So(hit.Id, ShouldEqual, savedDash.Id)
  130. })
  131. Convey("Should be able to search for dashboard by dashboard ids", func() {
  132. Convey("should be able to find two dashboards by id", func() {
  133. query := search.FindPersistedDashboardsQuery{
  134. DashboardIds: []int64{2, 3},
  135. Mode: "tree",
  136. SignedInUser: &m.SignedInUser{OrgId: 1},
  137. }
  138. err := SearchDashboards(&query)
  139. So(err, ShouldBeNil)
  140. So(len(query.Result[0].Dashboards), ShouldEqual, 2)
  141. hit := query.Result[0].Dashboards[0]
  142. So(len(hit.Tags), ShouldEqual, 2)
  143. hit2 := query.Result[0].Dashboards[1]
  144. So(len(hit2.Tags), ShouldEqual, 1)
  145. })
  146. Convey("DashboardIds that does not exists should not cause errors", func() {
  147. query := search.FindPersistedDashboardsQuery{
  148. DashboardIds: []int64{1000},
  149. SignedInUser: &m.SignedInUser{OrgId: 1},
  150. }
  151. err := SearchDashboards(&query)
  152. So(err, ShouldBeNil)
  153. So(len(query.Result), ShouldEqual, 0)
  154. })
  155. })
  156. Convey("Should not be able to save dashboard with same name", func() {
  157. cmd := m.SaveDashboardCommand{
  158. OrgId: 1,
  159. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  160. "id": nil,
  161. "title": "test dash 23",
  162. "tags": []interface{}{},
  163. }),
  164. }
  165. err := SaveDashboard(&cmd)
  166. So(err, ShouldNotBeNil)
  167. })
  168. Convey("Should be able to update dashboard and remove parentId", func() {
  169. cmd := m.SaveDashboardCommand{
  170. OrgId: 1,
  171. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  172. "id": 1,
  173. "title": "parentId",
  174. "tags": []interface{}{},
  175. }),
  176. Overwrite: true,
  177. ParentId: 2,
  178. }
  179. err := SaveDashboard(&cmd)
  180. So(err, ShouldBeNil)
  181. So(cmd.Result.ParentId, ShouldEqual, 2)
  182. cmd = m.SaveDashboardCommand{
  183. OrgId: 1,
  184. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  185. "id": 1,
  186. "title": "parentId",
  187. "tags": []interface{}{},
  188. }),
  189. ParentId: 0,
  190. Overwrite: true,
  191. }
  192. err = SaveDashboard(&cmd)
  193. So(err, ShouldBeNil)
  194. query := m.GetDashboardQuery{
  195. Slug: cmd.Result.Slug,
  196. OrgId: 1,
  197. }
  198. err = GetDashboard(&query)
  199. So(err, ShouldBeNil)
  200. So(query.Result.ParentId, ShouldEqual, 0)
  201. })
  202. Convey("Should be able to delete a dashboard folder and its children", func() {
  203. deleteCmd := &m.DeleteDashboardCommand{Id: savedFolder.Id}
  204. err := DeleteDashboard(deleteCmd)
  205. So(err, ShouldBeNil)
  206. query := search.FindPersistedDashboardsQuery{
  207. OrgId: 1,
  208. ParentId: savedFolder.Id,
  209. SignedInUser: &m.SignedInUser{},
  210. }
  211. err = SearchDashboards(&query)
  212. So(err, ShouldBeNil)
  213. So(len(query.Result), ShouldEqual, 0)
  214. })
  215. Convey("Should be able to get dashboard tags", func() {
  216. query := m.GetDashboardTagsQuery{OrgId: 1}
  217. err := GetDashboardTags(&query)
  218. So(err, ShouldBeNil)
  219. So(len(query.Result), ShouldEqual, 2)
  220. })
  221. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  222. starredDash := insertTestDashboard("starred dash", 1, 0, false)
  223. StarDashboard(&m.StarDashboardCommand{
  224. DashboardId: starredDash.Id,
  225. UserId: 10,
  226. })
  227. StarDashboard(&m.StarDashboardCommand{
  228. DashboardId: savedDash.Id,
  229. UserId: 1,
  230. })
  231. Convey("Should be able to search for starred dashboards", func() {
  232. query := search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: 10, OrgId: 1}, IsStarred: true}
  233. err := SearchDashboards(&query)
  234. So(err, ShouldBeNil)
  235. So(len(query.Result), ShouldEqual, 1)
  236. So(query.Result[0].Title, ShouldEqual, "starred dash")
  237. })
  238. })
  239. })
  240. Convey("Given one dashboard folder with two dashboard and one dashboard in the root folder", func() {
  241. folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  242. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  243. insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
  244. insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
  245. currentUser := createUser("viewer", "Viewer", false)
  246. Convey("and no acls are set", func() {
  247. Convey("should return all dashboards", func() {
  248. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
  249. err := SearchDashboards(query)
  250. So(err, ShouldBeNil)
  251. So(len(query.Result), ShouldEqual, 2)
  252. So(query.Result[0].Id, ShouldEqual, folder.Id)
  253. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  254. })
  255. })
  256. Convey("and acl is set for dashboard folder", func() {
  257. var otherUser int64 = 999
  258. updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
  259. Convey("should not return folder", func() {
  260. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
  261. err := SearchDashboards(query)
  262. So(err, ShouldBeNil)
  263. So(len(query.Result), ShouldEqual, 1)
  264. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  265. })
  266. Convey("when the user is given permission", func() {
  267. updateTestDashboardWithAcl(folder.Id, currentUser.Id, m.PERMISSION_EDIT)
  268. Convey("should be able to access folder", func() {
  269. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1}, OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id}}
  270. err := SearchDashboards(query)
  271. So(err, ShouldBeNil)
  272. So(len(query.Result), ShouldEqual, 2)
  273. So(query.Result[0].Id, ShouldEqual, folder.Id)
  274. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  275. })
  276. })
  277. Convey("when the user is an admin", func() {
  278. Convey("should be able to access folder", func() {
  279. query := &search.FindPersistedDashboardsQuery{
  280. SignedInUser: &m.SignedInUser{
  281. UserId: currentUser.Id,
  282. OrgId: 1,
  283. OrgRole: m.ROLE_ADMIN,
  284. },
  285. OrgId: 1,
  286. DashboardIds: []int64{folder.Id, dashInRoot.Id},
  287. }
  288. err := SearchDashboards(query)
  289. So(err, ShouldBeNil)
  290. So(len(query.Result), ShouldEqual, 2)
  291. So(query.Result[0].Id, ShouldEqual, folder.Id)
  292. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  293. })
  294. })
  295. })
  296. })
  297. })
  298. }
  299. func createUser(name string, role string, isAdmin bool) m.User {
  300. setting.AutoAssignOrg = true
  301. setting.AutoAssignOrgRole = role
  302. currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
  303. err := CreateUser(&currentUserCmd)
  304. So(err, ShouldBeNil)
  305. q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id}
  306. GetUserOrgList(&q1)
  307. So(q1.Result[0].Role, ShouldEqual, role)
  308. return currentUserCmd.Result
  309. }
  310. func updateTestDashboardWithAcl(dashId int64, userId int64, permissions m.PermissionType) {
  311. err := SetDashboardAcl(&m.SetDashboardAclCommand{
  312. OrgId: 1,
  313. UserId: userId,
  314. DashboardId: dashId,
  315. Permission: permissions,
  316. })
  317. So(err, ShouldBeNil)
  318. }