2015-03-21 07:53:16 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2016-03-12 03:13:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2015-03-21 07:53:16 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDashboardSnapshotDBAccess(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("Testing DashboardSnapshot data access", t, func() {
|
|
|
|
InitTestDB(t)
|
|
|
|
|
2018-02-20 16:26:08 -06:00
|
|
|
Convey("Given saved snapshot", func() {
|
2015-03-21 07:53:16 -05:00
|
|
|
cmd := m.CreateDashboardSnapshotCommand{
|
|
|
|
Key: "hej",
|
2016-03-12 03:13:49 -06:00
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2015-03-21 07:53:16 -05:00
|
|
|
"hello": "mupp",
|
2016-03-12 03:13:49 -06:00
|
|
|
}),
|
2018-02-20 16:26:08 -06:00
|
|
|
UserId: 1000,
|
|
|
|
OrgId: 1,
|
2015-03-21 07:53:16 -05:00
|
|
|
}
|
|
|
|
err := CreateDashboardSnapshot(&cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-02-20 16:26:08 -06:00
|
|
|
Convey("Should be able to get snapshot by key", func() {
|
2015-03-21 07:53:16 -05:00
|
|
|
query := m.GetDashboardSnapshotQuery{Key: "hej"}
|
|
|
|
err = GetDashboardSnapshot(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result, ShouldNotBeNil)
|
2016-03-12 03:13:49 -06:00
|
|
|
So(query.Result.Dashboard.Get("hello").MustString(), ShouldEqual, "mupp")
|
2015-03-21 07:53:16 -05:00
|
|
|
})
|
|
|
|
|
2018-02-20 16:26:08 -06:00
|
|
|
Convey("And the user has the admin role", func() {
|
|
|
|
Convey("Should return all the snapshots", func() {
|
|
|
|
query := m.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
|
|
|
|
}
|
|
|
|
err := SearchDashboardSnapshots(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result, ShouldNotBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("And the user has the editor role and has created a snapshot", func() {
|
|
|
|
Convey("Should return all the snapshots", func() {
|
|
|
|
query := m.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 1000},
|
|
|
|
}
|
|
|
|
err := SearchDashboardSnapshots(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result, ShouldNotBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("And the user has the editor role and has not created any snapshot", func() {
|
|
|
|
Convey("Should not return any snapshots", func() {
|
|
|
|
query := m.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 2},
|
|
|
|
}
|
|
|
|
err := SearchDashboardSnapshots(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result, ShouldNotBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("And the user is anonymous", func() {
|
|
|
|
cmd := m.CreateDashboardSnapshotCommand{
|
|
|
|
Key: "strangesnapshotwithuserid0",
|
|
|
|
DeleteKey: "adeletekey",
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"hello": "mupp",
|
|
|
|
}),
|
|
|
|
UserId: 0,
|
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
err := CreateDashboardSnapshot(&cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("Should not return any snapshots", func() {
|
|
|
|
query := m.GetDashboardSnapshotsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
|
|
|
|
}
|
|
|
|
err := SearchDashboardSnapshots(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result, ShouldNotBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
})
|
2015-03-21 07:53:16 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|