dashboard_snapshot_test.go 4.7 KB

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