dashboard_snapshot_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package sqlstore
  2. import (
  3. "testing"
  4. "time"
  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/setting"
  9. )
  10. func TestDashboardSnapshotDBAccess(t *testing.T) {
  11. Convey("Testing DashboardSnapshot data access", t, func() {
  12. InitTestDB(t)
  13. Convey("Given saved snapshot", func() {
  14. cmd := m.CreateDashboardSnapshotCommand{
  15. Key: "hej",
  16. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  17. "hello": "mupp",
  18. }),
  19. UserId: 1000,
  20. OrgId: 1,
  21. }
  22. err := CreateDashboardSnapshot(&cmd)
  23. So(err, ShouldBeNil)
  24. Convey("Should be able to get snapshot by key", func() {
  25. query := m.GetDashboardSnapshotQuery{Key: "hej"}
  26. err = GetDashboardSnapshot(&query)
  27. So(err, ShouldBeNil)
  28. So(query.Result, ShouldNotBeNil)
  29. So(query.Result.Dashboard.Get("hello").MustString(), ShouldEqual, "mupp")
  30. })
  31. Convey("And the user has the admin role", func() {
  32. Convey("Should return all the snapshots", func() {
  33. query := m.GetDashboardSnapshotsQuery{
  34. OrgId: 1,
  35. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
  36. }
  37. err := SearchDashboardSnapshots(&query)
  38. So(err, ShouldBeNil)
  39. So(query.Result, ShouldNotBeNil)
  40. So(len(query.Result), ShouldEqual, 1)
  41. })
  42. })
  43. Convey("And the user has the editor role and has created a snapshot", func() {
  44. Convey("Should return all the snapshots", func() {
  45. query := m.GetDashboardSnapshotsQuery{
  46. OrgId: 1,
  47. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 1000},
  48. }
  49. err := SearchDashboardSnapshots(&query)
  50. So(err, ShouldBeNil)
  51. So(query.Result, ShouldNotBeNil)
  52. So(len(query.Result), ShouldEqual, 1)
  53. })
  54. })
  55. Convey("And the user has the editor role and has not created any snapshot", func() {
  56. Convey("Should not return any snapshots", func() {
  57. query := m.GetDashboardSnapshotsQuery{
  58. OrgId: 1,
  59. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 2},
  60. }
  61. err := SearchDashboardSnapshots(&query)
  62. So(err, ShouldBeNil)
  63. So(query.Result, ShouldNotBeNil)
  64. So(len(query.Result), ShouldEqual, 0)
  65. })
  66. })
  67. Convey("And the user is anonymous", func() {
  68. cmd := m.CreateDashboardSnapshotCommand{
  69. Key: "strangesnapshotwithuserid0",
  70. DeleteKey: "adeletekey",
  71. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  72. "hello": "mupp",
  73. }),
  74. UserId: 0,
  75. OrgId: 1,
  76. }
  77. err := CreateDashboardSnapshot(&cmd)
  78. So(err, ShouldBeNil)
  79. Convey("Should not return any snapshots", func() {
  80. query := m.GetDashboardSnapshotsQuery{
  81. OrgId: 1,
  82. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
  83. }
  84. err := SearchDashboardSnapshots(&query)
  85. So(err, ShouldBeNil)
  86. So(query.Result, ShouldNotBeNil)
  87. So(len(query.Result), ShouldEqual, 0)
  88. })
  89. })
  90. })
  91. })
  92. }
  93. func TestDeleteExpiredSnapshots(t *testing.T) {
  94. sqlstore := InitTestDB(t)
  95. Convey("Testing dashboard snapshots clean up", t, func() {
  96. setting.SnapShotRemoveExpired = true
  97. notExpiredsnapshot := createTestSnapshot(sqlstore, "key1", 48000)
  98. createTestSnapshot(sqlstore, "key2", -1200)
  99. createTestSnapshot(sqlstore, "key3", -1200)
  100. err := DeleteExpiredSnapshots(&m.DeleteExpiredSnapshotsCommand{})
  101. So(err, ShouldBeNil)
  102. query := m.GetDashboardSnapshotsQuery{
  103. OrgId: 1,
  104. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
  105. }
  106. err = SearchDashboardSnapshots(&query)
  107. So(err, ShouldBeNil)
  108. So(len(query.Result), ShouldEqual, 1)
  109. So(query.Result[0].Key, ShouldEqual, notExpiredsnapshot.Key)
  110. err = DeleteExpiredSnapshots(&m.DeleteExpiredSnapshotsCommand{})
  111. So(err, ShouldBeNil)
  112. query = m.GetDashboardSnapshotsQuery{
  113. OrgId: 1,
  114. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
  115. }
  116. SearchDashboardSnapshots(&query)
  117. So(len(query.Result), ShouldEqual, 1)
  118. So(query.Result[0].Key, ShouldEqual, notExpiredsnapshot.Key)
  119. })
  120. }
  121. func createTestSnapshot(sqlstore *SqlStore, key string, expires int64) *m.DashboardSnapshot {
  122. cmd := m.CreateDashboardSnapshotCommand{
  123. Key: key,
  124. DeleteKey: "delete" + key,
  125. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  126. "hello": "mupp",
  127. }),
  128. UserId: 1000,
  129. OrgId: 1,
  130. Expires: expires,
  131. }
  132. err := CreateDashboardSnapshot(&cmd)
  133. So(err, ShouldBeNil)
  134. // Set expiry date manually - to be able to create expired snapshots
  135. if expires < 0 {
  136. expireDate := time.Now().Add(time.Second * time.Duration(expires))
  137. _, err = sqlstore.engine.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, cmd.Result.Id)
  138. So(err, ShouldBeNil)
  139. }
  140. return cmd.Result
  141. }