dashboard_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package sqlstore
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/go-xorm/xorm"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/search"
  10. "github.com/grafana/grafana/pkg/setting"
  11. )
  12. func TestDashboardDataAccess(t *testing.T) {
  13. var x *xorm.Engine
  14. Convey("Testing DB", t, func() {
  15. x = InitTestDB(t)
  16. Convey("Given saved dashboard", func() {
  17. savedFolder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  18. savedDash := insertTestDashboard("test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
  19. insertTestDashboard("test dash 45", 1, savedFolder.Id, false, "prod")
  20. insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  21. Convey("Should return dashboard model", func() {
  22. So(savedDash.Title, ShouldEqual, "test dash 23")
  23. So(savedDash.Slug, ShouldEqual, "test-dash-23")
  24. So(savedDash.Id, ShouldNotEqual, 0)
  25. So(savedDash.IsFolder, ShouldBeFalse)
  26. So(savedDash.FolderId, ShouldBeGreaterThan, 0)
  27. So(len(savedDash.Uid), ShouldBeGreaterThan, 0)
  28. So(savedFolder.Title, ShouldEqual, "1 test dash folder")
  29. So(savedFolder.Slug, ShouldEqual, "1-test-dash-folder")
  30. So(savedFolder.Id, ShouldNotEqual, 0)
  31. So(savedFolder.IsFolder, ShouldBeTrue)
  32. So(savedFolder.FolderId, ShouldEqual, 0)
  33. So(len(savedFolder.Uid), ShouldBeGreaterThan, 0)
  34. })
  35. Convey("Should be able to get dashboard by id", func() {
  36. query := m.GetDashboardQuery{
  37. Id: savedDash.Id,
  38. OrgId: 1,
  39. }
  40. err := GetDashboard(&query)
  41. So(err, ShouldBeNil)
  42. So(query.Result.Title, ShouldEqual, "test dash 23")
  43. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  44. So(query.Result.Id, ShouldEqual, savedDash.Id)
  45. So(query.Result.Uid, ShouldEqual, savedDash.Uid)
  46. So(query.Result.IsFolder, ShouldBeFalse)
  47. })
  48. Convey("Should be able to get dashboard by slug", func() {
  49. query := m.GetDashboardQuery{
  50. Slug: "test-dash-23",
  51. OrgId: 1,
  52. }
  53. err := GetDashboard(&query)
  54. So(err, ShouldBeNil)
  55. So(query.Result.Title, ShouldEqual, "test dash 23")
  56. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  57. So(query.Result.Id, ShouldEqual, savedDash.Id)
  58. So(query.Result.Uid, ShouldEqual, savedDash.Uid)
  59. So(query.Result.IsFolder, ShouldBeFalse)
  60. })
  61. Convey("Should be able to get dashboard by uid", func() {
  62. query := m.GetDashboardQuery{
  63. Uid: savedDash.Uid,
  64. OrgId: 1,
  65. }
  66. err := GetDashboard(&query)
  67. So(err, ShouldBeNil)
  68. So(query.Result.Title, ShouldEqual, "test dash 23")
  69. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  70. So(query.Result.Id, ShouldEqual, savedDash.Id)
  71. So(query.Result.Uid, ShouldEqual, savedDash.Uid)
  72. So(query.Result.IsFolder, ShouldBeFalse)
  73. })
  74. Convey("Should be able to delete dashboard", func() {
  75. dash := insertTestDashboard("delete me", 1, 0, false, "delete this")
  76. err := DeleteDashboard(&m.DeleteDashboardCommand{
  77. Id: dash.Id,
  78. OrgId: 1,
  79. })
  80. So(err, ShouldBeNil)
  81. })
  82. Convey("Should return error if no dashboard is updated", func() {
  83. cmd := m.SaveDashboardCommand{
  84. OrgId: 1,
  85. Overwrite: true,
  86. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  87. "id": float64(123412321),
  88. "title": "Expect error",
  89. "tags": []interface{}{},
  90. }),
  91. }
  92. err := SaveDashboard(&cmd)
  93. So(err, ShouldNotBeNil)
  94. })
  95. Convey("Should not be able to overwrite dashboard in another org", func() {
  96. query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
  97. GetDashboard(&query)
  98. cmd := m.SaveDashboardCommand{
  99. OrgId: 2,
  100. Overwrite: true,
  101. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  102. "id": float64(query.Result.Id),
  103. "title": "Expect error",
  104. "tags": []interface{}{},
  105. }),
  106. }
  107. err := SaveDashboard(&cmd)
  108. So(err, ShouldNotBeNil)
  109. })
  110. Convey("Should be able to search for dashboard folder", func() {
  111. query := search.FindPersistedDashboardsQuery{
  112. Title: "1 test dash folder",
  113. OrgId: 1,
  114. SignedInUser: &m.SignedInUser{OrgId: 1},
  115. }
  116. err := SearchDashboards(&query)
  117. So(err, ShouldBeNil)
  118. So(len(query.Result), ShouldEqual, 1)
  119. hit := query.Result[0]
  120. So(hit.Type, ShouldEqual, search.DashHitFolder)
  121. So(hit.Url, ShouldEqual, fmt.Sprintf("/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
  122. })
  123. Convey("Should be able to search for a dashboard folder's children", func() {
  124. query := search.FindPersistedDashboardsQuery{
  125. OrgId: 1,
  126. FolderIds: []int64{savedFolder.Id},
  127. SignedInUser: &m.SignedInUser{OrgId: 1},
  128. }
  129. err := SearchDashboards(&query)
  130. So(err, ShouldBeNil)
  131. So(len(query.Result), ShouldEqual, 2)
  132. hit := query.Result[0]
  133. So(hit.Id, ShouldEqual, savedDash.Id)
  134. So(hit.Url, ShouldEqual, fmt.Sprintf("/d/%s/%s", savedDash.Uid, savedDash.Slug))
  135. })
  136. Convey("Should be able to search for dashboard by dashboard ids", func() {
  137. Convey("should be able to find two dashboards by id", func() {
  138. query := search.FindPersistedDashboardsQuery{
  139. DashboardIds: []int64{2, 3},
  140. SignedInUser: &m.SignedInUser{OrgId: 1},
  141. }
  142. err := SearchDashboards(&query)
  143. So(err, ShouldBeNil)
  144. So(len(query.Result), ShouldEqual, 2)
  145. hit := query.Result[0]
  146. So(len(hit.Tags), ShouldEqual, 2)
  147. hit2 := query.Result[1]
  148. So(len(hit2.Tags), ShouldEqual, 1)
  149. })
  150. Convey("DashboardIds that does not exists should not cause errors", func() {
  151. query := search.FindPersistedDashboardsQuery{
  152. DashboardIds: []int64{1000},
  153. SignedInUser: &m.SignedInUser{OrgId: 1},
  154. }
  155. err := SearchDashboards(&query)
  156. So(err, ShouldBeNil)
  157. So(len(query.Result), ShouldEqual, 0)
  158. })
  159. })
  160. Convey("Should be able to save dashboards with same name in different folders", func() {
  161. firstSaveCmd := m.SaveDashboardCommand{
  162. OrgId: 1,
  163. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  164. "id": nil,
  165. "title": "test dash folder and title",
  166. "tags": []interface{}{},
  167. "uid": "randomHash",
  168. }),
  169. FolderId: 3,
  170. }
  171. err := SaveDashboard(&firstSaveCmd)
  172. So(err, ShouldBeNil)
  173. secondSaveCmd := m.SaveDashboardCommand{
  174. OrgId: 1,
  175. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  176. "id": nil,
  177. "title": "test dash folder and title",
  178. "tags": []interface{}{},
  179. "uid": "moreRandomHash",
  180. }),
  181. FolderId: 1,
  182. }
  183. err = SaveDashboard(&secondSaveCmd)
  184. So(err, ShouldBeNil)
  185. })
  186. Convey("Should not be able to save dashboard with same name in the same folder", func() {
  187. firstSaveCmd := m.SaveDashboardCommand{
  188. OrgId: 1,
  189. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  190. "id": nil,
  191. "title": "test dash folder and title",
  192. "tags": []interface{}{},
  193. "uid": "randomHash",
  194. }),
  195. FolderId: 3,
  196. }
  197. err := SaveDashboard(&firstSaveCmd)
  198. So(err, ShouldBeNil)
  199. secondSaveCmd := m.SaveDashboardCommand{
  200. OrgId: 1,
  201. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  202. "id": nil,
  203. "title": "test dash folder and title",
  204. "tags": []interface{}{},
  205. "uid": "moreRandomHash",
  206. }),
  207. FolderId: 3,
  208. }
  209. err = SaveDashboard(&secondSaveCmd)
  210. So(err, ShouldEqual, m.ErrDashboardWithSameNameInFolderExists)
  211. })
  212. Convey("Should not be able to save dashboard with same uid", func() {
  213. cmd := m.SaveDashboardCommand{
  214. OrgId: 1,
  215. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  216. "id": nil,
  217. "title": "test dash 23",
  218. "uid": "dsfalkjngailuedt",
  219. }),
  220. }
  221. err := SaveDashboard(&cmd)
  222. So(err, ShouldBeNil)
  223. err = SaveDashboard(&cmd)
  224. So(err, ShouldNotBeNil)
  225. })
  226. Convey("Should be able to update dashboard with the same title and folder id", func() {
  227. cmd := m.SaveDashboardCommand{
  228. OrgId: 1,
  229. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  230. "uid": "randomHash",
  231. "title": "folderId",
  232. "style": "light",
  233. "tags": []interface{}{},
  234. }),
  235. FolderId: 2,
  236. }
  237. err := SaveDashboard(&cmd)
  238. So(err, ShouldBeNil)
  239. So(cmd.Result.FolderId, ShouldEqual, 2)
  240. cmd = m.SaveDashboardCommand{
  241. OrgId: 1,
  242. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  243. "id": cmd.Result.Id,
  244. "uid": "randomHash",
  245. "title": "folderId",
  246. "style": "dark",
  247. "version": cmd.Result.Version,
  248. "tags": []interface{}{},
  249. }),
  250. FolderId: 2,
  251. }
  252. err = SaveDashboard(&cmd)
  253. So(err, ShouldBeNil)
  254. })
  255. Convey("Should not be able to update using just uid", func() {
  256. cmd := m.SaveDashboardCommand{
  257. OrgId: 1,
  258. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  259. "uid": savedDash.Uid,
  260. "title": "folderId",
  261. "version": savedDash.Version,
  262. "tags": []interface{}{},
  263. }),
  264. FolderId: savedDash.FolderId,
  265. }
  266. err := SaveDashboard(&cmd)
  267. So(err, ShouldEqual, m.ErrDashboardWithSameUIDExists)
  268. })
  269. Convey("Should be able to update using just uid with overwrite", func() {
  270. cmd := m.SaveDashboardCommand{
  271. OrgId: 1,
  272. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  273. "uid": savedDash.Uid,
  274. "title": "folderId",
  275. "version": savedDash.Version,
  276. "tags": []interface{}{},
  277. }),
  278. FolderId: savedDash.FolderId,
  279. Overwrite: true,
  280. }
  281. err := SaveDashboard(&cmd)
  282. So(err, ShouldBeNil)
  283. })
  284. Convey("Should be able to update dashboard and remove folderId", func() {
  285. cmd := m.SaveDashboardCommand{
  286. OrgId: 1,
  287. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  288. "id": 1,
  289. "title": "folderId",
  290. "tags": []interface{}{},
  291. }),
  292. Overwrite: true,
  293. FolderId: 2,
  294. }
  295. err := SaveDashboard(&cmd)
  296. So(err, ShouldBeNil)
  297. So(cmd.Result.FolderId, ShouldEqual, 2)
  298. cmd = m.SaveDashboardCommand{
  299. OrgId: 1,
  300. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  301. "id": 1,
  302. "title": "folderId",
  303. "tags": []interface{}{},
  304. }),
  305. FolderId: 0,
  306. Overwrite: true,
  307. }
  308. err = SaveDashboard(&cmd)
  309. So(err, ShouldBeNil)
  310. query := m.GetDashboardQuery{
  311. Slug: cmd.Result.Slug,
  312. OrgId: 1,
  313. }
  314. err = GetDashboard(&query)
  315. So(err, ShouldBeNil)
  316. So(query.Result.FolderId, ShouldEqual, 0)
  317. })
  318. Convey("Should be able to delete a dashboard folder and its children", func() {
  319. deleteCmd := &m.DeleteDashboardCommand{Id: savedFolder.Id}
  320. err := DeleteDashboard(deleteCmd)
  321. So(err, ShouldBeNil)
  322. query := search.FindPersistedDashboardsQuery{
  323. OrgId: 1,
  324. FolderIds: []int64{savedFolder.Id},
  325. SignedInUser: &m.SignedInUser{},
  326. }
  327. err = SearchDashboards(&query)
  328. So(err, ShouldBeNil)
  329. So(len(query.Result), ShouldEqual, 0)
  330. })
  331. Convey("Should be able to get dashboard tags", func() {
  332. query := m.GetDashboardTagsQuery{OrgId: 1}
  333. err := GetDashboardTags(&query)
  334. So(err, ShouldBeNil)
  335. So(len(query.Result), ShouldEqual, 2)
  336. })
  337. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  338. starredDash := insertTestDashboard("starred dash", 1, 0, false)
  339. StarDashboard(&m.StarDashboardCommand{
  340. DashboardId: starredDash.Id,
  341. UserId: 10,
  342. })
  343. StarDashboard(&m.StarDashboardCommand{
  344. DashboardId: savedDash.Id,
  345. UserId: 1,
  346. })
  347. Convey("Should be able to search for starred dashboards", func() {
  348. query := search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: 10, OrgId: 1}, IsStarred: true}
  349. err := SearchDashboards(&query)
  350. So(err, ShouldBeNil)
  351. So(len(query.Result), ShouldEqual, 1)
  352. So(query.Result[0].Title, ShouldEqual, "starred dash")
  353. })
  354. })
  355. })
  356. Convey("Given a plugin with imported dashboards", func() {
  357. pluginId := "test-app"
  358. appFolder := insertTestDashboardForPlugin("app-test", 1, 0, true, pluginId)
  359. insertTestDashboardForPlugin("app-dash1", 1, appFolder.Id, false, pluginId)
  360. insertTestDashboardForPlugin("app-dash2", 1, appFolder.Id, false, pluginId)
  361. Convey("Should return imported dashboard", func() {
  362. query := m.GetDashboardsByPluginIdQuery{
  363. PluginId: pluginId,
  364. OrgId: 1,
  365. }
  366. err := GetDashboardsByPluginId(&query)
  367. So(err, ShouldBeNil)
  368. So(len(query.Result), ShouldEqual, 2)
  369. })
  370. })
  371. })
  372. }
  373. func insertTestDashboard(title string, orgId int64, folderId int64, isFolder bool, tags ...interface{}) *m.Dashboard {
  374. cmd := m.SaveDashboardCommand{
  375. OrgId: orgId,
  376. FolderId: folderId,
  377. IsFolder: isFolder,
  378. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  379. "id": nil,
  380. "title": title,
  381. "tags": tags,
  382. }),
  383. }
  384. err := SaveDashboard(&cmd)
  385. So(err, ShouldBeNil)
  386. return cmd.Result
  387. }
  388. func insertTestDashboardForPlugin(title string, orgId int64, folderId int64, isFolder bool, pluginId string) *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. }),
  397. PluginId: pluginId,
  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. }