dashboard_snapshot_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.TeamDTO{}
  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 DELETE on", "DELETE", "/api/snapshots/12345", "/api/snapshots/:key", m.ROLE_EDITOR, func(sc *scenarioContext) {
  41. sc.handlerFunc = DeleteDashboardSnapshot
  42. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
  43. So(sc.resp.Code, ShouldEqual, 403)
  44. })
  45. })
  46. })
  47. Convey("When user is anonymous", func() {
  48. Convey("Should be able to delete snapshot by deleteKey", func() {
  49. anonymousUserScenario("When calling GET on", "GET", "/api/snapshots-delete/12345", "/api/snapshots-delete/:deleteKey", func(sc *scenarioContext) {
  50. sc.handlerFunc = DeleteDashboardSnapshotByDeleteKey
  51. sc.fakeReqWithParams("GET", sc.url, map[string]string{"deleteKey": "12345"}).exec()
  52. So(sc.resp.Code, ShouldEqual, 200)
  53. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  54. So(err, ShouldBeNil)
  55. So(respJSON.Get("message").MustString(), ShouldStartWith, "Snapshot deleted")
  56. })
  57. })
  58. })
  59. Convey("When user is editor and dashboard has default ACL", func() {
  60. aclMockResp = []*m.DashboardAclInfoDTO{
  61. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  62. {Role: &editorRole, Permission: m.PERMISSION_EDIT},
  63. }
  64. Convey("Should be able to delete a snapshot", func() {
  65. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/snapshots/12345", "/api/snapshots/:key", m.ROLE_EDITOR, func(sc *scenarioContext) {
  66. sc.handlerFunc = DeleteDashboardSnapshot
  67. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
  68. So(sc.resp.Code, ShouldEqual, 200)
  69. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  70. So(err, ShouldBeNil)
  71. So(respJSON.Get("message").MustString(), ShouldStartWith, "Snapshot deleted")
  72. })
  73. })
  74. })
  75. Convey("When user is editor and is the creator of the snapshot", func() {
  76. aclMockResp = []*m.DashboardAclInfoDTO{}
  77. mockSnapshotResult.UserId = TestUserID
  78. Convey("Should be able to delete a snapshot", func() {
  79. loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/snapshots/12345", "/api/snapshots/:key", m.ROLE_EDITOR, func(sc *scenarioContext) {
  80. sc.handlerFunc = DeleteDashboardSnapshot
  81. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{"key": "12345"}).exec()
  82. So(sc.resp.Code, ShouldEqual, 200)
  83. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  84. So(err, ShouldBeNil)
  85. So(respJSON.Get("message").MustString(), ShouldStartWith, "Snapshot deleted")
  86. })
  87. })
  88. })
  89. })
  90. }