dashboard_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package sqlstore
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  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. "github.com/grafana/grafana/pkg/util"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestDashboardDataAccess(t *testing.T) {
  14. Convey("Testing DB", t, func() {
  15. 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 retry generation of uid once if it fails.", func() {
  83. timesCalled := 0
  84. generateNewUid = func() string {
  85. timesCalled += 1
  86. if timesCalled <= 2 {
  87. return savedDash.Uid
  88. } else {
  89. return util.GenerateShortUid()
  90. }
  91. }
  92. cmd := m.SaveDashboardCommand{
  93. OrgId: 1,
  94. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  95. "title": "new dash 12334",
  96. "tags": []interface{}{},
  97. }),
  98. }
  99. err := SaveDashboard(&cmd)
  100. So(err, ShouldBeNil)
  101. generateNewUid = util.GenerateShortUid
  102. })
  103. Convey("Should be able to create dashboard", func() {
  104. cmd := m.SaveDashboardCommand{
  105. OrgId: 1,
  106. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  107. "title": "folderId",
  108. "tags": []interface{}{},
  109. }),
  110. UserId: 100,
  111. }
  112. err := SaveDashboard(&cmd)
  113. So(err, ShouldBeNil)
  114. So(cmd.Result.CreatedBy, ShouldEqual, 100)
  115. So(cmd.Result.Created.IsZero(), ShouldBeFalse)
  116. So(cmd.Result.UpdatedBy, ShouldEqual, 100)
  117. So(cmd.Result.Updated.IsZero(), ShouldBeFalse)
  118. })
  119. Convey("Should be able to update dashboard by id and remove folderId", func() {
  120. cmd := m.SaveDashboardCommand{
  121. OrgId: 1,
  122. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  123. "id": savedDash.Id,
  124. "title": "folderId",
  125. "tags": []interface{}{},
  126. }),
  127. Overwrite: true,
  128. FolderId: 2,
  129. UserId: 100,
  130. }
  131. err := SaveDashboard(&cmd)
  132. So(err, ShouldBeNil)
  133. So(cmd.Result.FolderId, ShouldEqual, 2)
  134. cmd = m.SaveDashboardCommand{
  135. OrgId: 1,
  136. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  137. "id": savedDash.Id,
  138. "title": "folderId",
  139. "tags": []interface{}{},
  140. }),
  141. FolderId: 0,
  142. Overwrite: true,
  143. UserId: 100,
  144. }
  145. err = SaveDashboard(&cmd)
  146. So(err, ShouldBeNil)
  147. query := m.GetDashboardQuery{
  148. Id: savedDash.Id,
  149. OrgId: 1,
  150. }
  151. err = GetDashboard(&query)
  152. So(err, ShouldBeNil)
  153. So(query.Result.FolderId, ShouldEqual, 0)
  154. So(query.Result.CreatedBy, ShouldEqual, savedDash.CreatedBy)
  155. So(query.Result.Created, ShouldEqual, savedDash.Created.Truncate(time.Second))
  156. So(query.Result.UpdatedBy, ShouldEqual, 100)
  157. So(query.Result.Updated.IsZero(), ShouldBeFalse)
  158. })
  159. Convey("Should be able to delete a dashboard folder and its children", func() {
  160. deleteCmd := &m.DeleteDashboardCommand{Id: savedFolder.Id}
  161. err := DeleteDashboard(deleteCmd)
  162. So(err, ShouldBeNil)
  163. query := search.FindPersistedDashboardsQuery{
  164. OrgId: 1,
  165. FolderIds: []int64{savedFolder.Id},
  166. SignedInUser: &m.SignedInUser{},
  167. }
  168. err = SearchDashboards(&query)
  169. So(err, ShouldBeNil)
  170. So(len(query.Result), ShouldEqual, 0)
  171. })
  172. Convey("Should return error if no dashboard is found for update when dashboard id is greater than zero", func() {
  173. cmd := m.SaveDashboardCommand{
  174. OrgId: 1,
  175. Overwrite: true,
  176. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  177. "id": float64(123412321),
  178. "title": "Expect error",
  179. "tags": []interface{}{},
  180. }),
  181. }
  182. err := SaveDashboard(&cmd)
  183. So(err, ShouldEqual, m.ErrDashboardNotFound)
  184. })
  185. Convey("Should not return error if no dashboard is found for update when dashboard id is zero", func() {
  186. cmd := m.SaveDashboardCommand{
  187. OrgId: 1,
  188. Overwrite: true,
  189. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  190. "id": 0,
  191. "title": "New dash",
  192. "tags": []interface{}{},
  193. }),
  194. }
  195. err := SaveDashboard(&cmd)
  196. So(err, ShouldBeNil)
  197. })
  198. Convey("Should be able to get dashboard tags", func() {
  199. query := m.GetDashboardTagsQuery{OrgId: 1}
  200. err := GetDashboardTags(&query)
  201. So(err, ShouldBeNil)
  202. So(len(query.Result), ShouldEqual, 2)
  203. })
  204. Convey("Should be able to search for dashboard folder", func() {
  205. query := search.FindPersistedDashboardsQuery{
  206. Title: "1 test dash folder",
  207. OrgId: 1,
  208. SignedInUser: &m.SignedInUser{OrgId: 1, OrgRole: m.ROLE_EDITOR},
  209. }
  210. err := SearchDashboards(&query)
  211. So(err, ShouldBeNil)
  212. So(len(query.Result), ShouldEqual, 1)
  213. hit := query.Result[0]
  214. So(hit.Type, ShouldEqual, search.DashHitFolder)
  215. So(hit.Url, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
  216. So(hit.FolderTitle, ShouldEqual, "")
  217. })
  218. Convey("Should be able to search for a dashboard folder's children", func() {
  219. query := search.FindPersistedDashboardsQuery{
  220. OrgId: 1,
  221. FolderIds: []int64{savedFolder.Id},
  222. SignedInUser: &m.SignedInUser{OrgId: 1, OrgRole: m.ROLE_EDITOR},
  223. }
  224. err := SearchDashboards(&query)
  225. So(err, ShouldBeNil)
  226. So(len(query.Result), ShouldEqual, 2)
  227. hit := query.Result[0]
  228. So(hit.Id, ShouldEqual, savedDash.Id)
  229. So(hit.Url, ShouldEqual, fmt.Sprintf("/d/%s/%s", savedDash.Uid, savedDash.Slug))
  230. So(hit.FolderId, ShouldEqual, savedFolder.Id)
  231. So(hit.FolderUid, ShouldEqual, savedFolder.Uid)
  232. So(hit.FolderTitle, ShouldEqual, savedFolder.Title)
  233. So(hit.FolderUrl, ShouldEqual, fmt.Sprintf("/dashboards/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
  234. })
  235. Convey("Should be able to search for dashboard by dashboard ids", func() {
  236. Convey("should be able to find two dashboards by id", func() {
  237. query := search.FindPersistedDashboardsQuery{
  238. DashboardIds: []int64{2, 3},
  239. SignedInUser: &m.SignedInUser{OrgId: 1, OrgRole: m.ROLE_EDITOR},
  240. }
  241. err := SearchDashboards(&query)
  242. So(err, ShouldBeNil)
  243. So(len(query.Result), ShouldEqual, 2)
  244. hit := query.Result[0]
  245. So(len(hit.Tags), ShouldEqual, 2)
  246. hit2 := query.Result[1]
  247. So(len(hit2.Tags), ShouldEqual, 1)
  248. })
  249. })
  250. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  251. starredDash := insertTestDashboard("starred dash", 1, 0, false)
  252. StarDashboard(&m.StarDashboardCommand{
  253. DashboardId: starredDash.Id,
  254. UserId: 10,
  255. })
  256. StarDashboard(&m.StarDashboardCommand{
  257. DashboardId: savedDash.Id,
  258. UserId: 1,
  259. })
  260. Convey("Should be able to search for starred dashboards", func() {
  261. query := search.FindPersistedDashboardsQuery{
  262. SignedInUser: &m.SignedInUser{UserId: 10, OrgId: 1, OrgRole: m.ROLE_EDITOR},
  263. IsStarred: true,
  264. }
  265. err := SearchDashboards(&query)
  266. So(err, ShouldBeNil)
  267. So(len(query.Result), ShouldEqual, 1)
  268. So(query.Result[0].Title, ShouldEqual, "starred dash")
  269. })
  270. })
  271. })
  272. Convey("Given a plugin with imported dashboards", func() {
  273. pluginId := "test-app"
  274. appFolder := insertTestDashboardForPlugin("app-test", 1, 0, true, pluginId)
  275. insertTestDashboardForPlugin("app-dash1", 1, appFolder.Id, false, pluginId)
  276. insertTestDashboardForPlugin("app-dash2", 1, appFolder.Id, false, pluginId)
  277. Convey("Should return imported dashboard", func() {
  278. query := m.GetDashboardsByPluginIdQuery{
  279. PluginId: pluginId,
  280. OrgId: 1,
  281. }
  282. err := GetDashboardsByPluginId(&query)
  283. So(err, ShouldBeNil)
  284. So(len(query.Result), ShouldEqual, 2)
  285. })
  286. })
  287. })
  288. }
  289. func insertTestDashboard(title string, orgId int64, folderId int64, isFolder bool, tags ...interface{}) *m.Dashboard {
  290. cmd := m.SaveDashboardCommand{
  291. OrgId: orgId,
  292. FolderId: folderId,
  293. IsFolder: isFolder,
  294. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  295. "id": nil,
  296. "title": title,
  297. "tags": tags,
  298. }),
  299. }
  300. err := SaveDashboard(&cmd)
  301. So(err, ShouldBeNil)
  302. cmd.Result.Data.Set("id", cmd.Result.Id)
  303. cmd.Result.Data.Set("uid", cmd.Result.Uid)
  304. return cmd.Result
  305. }
  306. func insertTestDashboardForPlugin(title string, orgId int64, folderId int64, isFolder bool, pluginId string) *m.Dashboard {
  307. cmd := m.SaveDashboardCommand{
  308. OrgId: orgId,
  309. FolderId: folderId,
  310. IsFolder: isFolder,
  311. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  312. "id": nil,
  313. "title": title,
  314. }),
  315. PluginId: pluginId,
  316. }
  317. err := SaveDashboard(&cmd)
  318. So(err, ShouldBeNil)
  319. return cmd.Result
  320. }
  321. func createUser(name string, role string, isAdmin bool) m.User {
  322. setting.AutoAssignOrg = true
  323. setting.AutoAssignOrgRole = role
  324. currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
  325. err := CreateUser(&currentUserCmd)
  326. So(err, ShouldBeNil)
  327. q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id}
  328. GetUserOrgList(&q1)
  329. So(q1.Result[0].Role, ShouldEqual, role)
  330. return currentUserCmd.Result
  331. }
  332. func moveDashboard(orgId int64, dashboard *simplejson.Json, newFolderId int64) *m.Dashboard {
  333. cmd := m.SaveDashboardCommand{
  334. OrgId: orgId,
  335. FolderId: newFolderId,
  336. Dashboard: dashboard,
  337. Overwrite: true,
  338. }
  339. err := SaveDashboard(&cmd)
  340. So(err, ShouldBeNil)
  341. return cmd.Result
  342. }