dashboard_snapshot_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. )
  8. func TestDashboardSnapshotDBAccess(t *testing.T) {
  9. Convey("Testing DashboardSnapshot data access", t, func() {
  10. InitTestDB(t)
  11. Convey("Given saved snapshot", func() {
  12. cmd := m.CreateDashboardSnapshotCommand{
  13. Key: "hej",
  14. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  15. "hello": "mupp",
  16. }),
  17. UserId: 1000,
  18. OrgId: 1,
  19. }
  20. err := CreateDashboardSnapshot(&cmd)
  21. So(err, ShouldBeNil)
  22. Convey("Should be able to get snapshot by key", func() {
  23. query := m.GetDashboardSnapshotQuery{Key: "hej"}
  24. err = GetDashboardSnapshot(&query)
  25. So(err, ShouldBeNil)
  26. So(query.Result, ShouldNotBeNil)
  27. So(query.Result.Dashboard.Get("hello").MustString(), ShouldEqual, "mupp")
  28. })
  29. Convey("And the user has the admin role", func() {
  30. Convey("Should return all the snapshots", func() {
  31. query := m.GetDashboardSnapshotsQuery{
  32. OrgId: 1,
  33. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
  34. }
  35. err := SearchDashboardSnapshots(&query)
  36. So(err, ShouldBeNil)
  37. So(query.Result, ShouldNotBeNil)
  38. So(len(query.Result), ShouldEqual, 1)
  39. })
  40. })
  41. Convey("And the user has the editor role and has created a snapshot", func() {
  42. Convey("Should return all the snapshots", func() {
  43. query := m.GetDashboardSnapshotsQuery{
  44. OrgId: 1,
  45. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 1000},
  46. }
  47. err := SearchDashboardSnapshots(&query)
  48. So(err, ShouldBeNil)
  49. So(query.Result, ShouldNotBeNil)
  50. So(len(query.Result), ShouldEqual, 1)
  51. })
  52. })
  53. Convey("And the user has the editor role and has not created any snapshot", func() {
  54. Convey("Should not return any snapshots", func() {
  55. query := m.GetDashboardSnapshotsQuery{
  56. OrgId: 1,
  57. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 2},
  58. }
  59. err := SearchDashboardSnapshots(&query)
  60. So(err, ShouldBeNil)
  61. So(query.Result, ShouldNotBeNil)
  62. So(len(query.Result), ShouldEqual, 0)
  63. })
  64. })
  65. Convey("And the user is anonymous", func() {
  66. cmd := m.CreateDashboardSnapshotCommand{
  67. Key: "strangesnapshotwithuserid0",
  68. DeleteKey: "adeletekey",
  69. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  70. "hello": "mupp",
  71. }),
  72. UserId: 0,
  73. OrgId: 1,
  74. }
  75. err := CreateDashboardSnapshot(&cmd)
  76. So(err, ShouldBeNil)
  77. Convey("Should not return any snapshots", func() {
  78. query := m.GetDashboardSnapshotsQuery{
  79. OrgId: 1,
  80. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
  81. }
  82. err := SearchDashboardSnapshots(&query)
  83. So(err, ShouldBeNil)
  84. So(query.Result, ShouldNotBeNil)
  85. So(len(query.Result), ShouldEqual, 0)
  86. })
  87. })
  88. })
  89. })
  90. }