dashboard_snapshot_test.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package api
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestDashboardSnapshotApiEndpoint(t *testing.T) {
  11. Convey("Given a single snapshot", t, func() {
  12. jsonModel, _ := simplejson.NewJson([]byte(`{"id":100}`))
  13. mockSnapshotResult := &m.DashboardSnapshot{
  14. Id: 1,
  15. Dashboard: jsonModel,
  16. Expires: time.Now().Add(time.Duration(1000) * time.Second),
  17. UserId: 999999,
  18. }
  19. bus.AddHandler("test", func(query *m.GetDashboardSnapshotQuery) error {
  20. query.Result = mockSnapshotResult
  21. return nil
  22. })
  23. bus.AddHandler("test", func(cmd *m.DeleteDashboardSnapshotCommand) error {
  24. return nil
  25. })
  26. viewerRole := m.ROLE_VIEWER
  27. editorRole := m.ROLE_EDITOR
  28. aclMockResp := []*m.DashboardAclInfoDTO{}
  29. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  30. query.Result = aclMockResp
  31. return nil
  32. })
  33. teamResp := []*m.Team{}
  34. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  35. query.Result = teamResp
  36. return nil
  37. })
  38. Convey("When user has editor role and is not in the ACL", func() {
  39. Convey("Should not be able to delete snapshot", func() {
  40. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/snapshots-delete/12345", "/api/snapshots-delete/:key", m.ROLE_EDITOR, func(sc *scenarioContext) {
  41. sc.handlerFunc = DeleteDashboardSnapshot
  42. sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec()
  43. So(sc.resp.Code, ShouldEqual, 403)
  44. })
  45. })
  46. })
  47. Convey("When user is editor and dashboard has default ACL", func() {
  48. aclMockResp = []*m.DashboardAclInfoDTO{
  49. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  50. {Role: &editorRole, Permission: m.PERMISSION_EDIT},
  51. }
  52. Convey("Should be able to delete a snapshot", func() {
  53. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/snapshots-delete/12345", "/api/snapshots-delete/:key", m.ROLE_EDITOR, func(sc *scenarioContext) {
  54. sc.handlerFunc = DeleteDashboardSnapshot
  55. sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec()
  56. So(sc.resp.Code, ShouldEqual, 200)
  57. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  58. So(err, ShouldBeNil)
  59. So(respJSON.Get("message").MustString(), ShouldStartWith, "Snapshot deleted")
  60. })
  61. })
  62. })
  63. Convey("When user is editor and is the creator of the snapshot", func() {
  64. aclMockResp = []*m.DashboardAclInfoDTO{}
  65. mockSnapshotResult.UserId = TestUserID
  66. Convey("Should be able to delete a snapshot", func() {
  67. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/snapshots-delete/12345", "/api/snapshots-delete/:key", m.ROLE_EDITOR, func(sc *scenarioContext) {
  68. sc.handlerFunc = DeleteDashboardSnapshot
  69. sc.fakeReqWithParams("GET", sc.url, map[string]string{"key": "12345"}).exec()
  70. So(sc.resp.Code, ShouldEqual, 200)
  71. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  72. So(err, ShouldBeNil)
  73. So(respJSON.Get("message").MustString(), ShouldStartWith, "Snapshot deleted")
  74. })
  75. })
  76. })
  77. })
  78. }