dashboard_redirect_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package middleware
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/bus"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/util"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestMiddlewareDashboardRedirect(t *testing.T) {
  11. Convey("Given the dashboard redirect middleware", t, func() {
  12. bus.ClearBusHandlers()
  13. redirectFromLegacyDashboardUrl := RedirectFromLegacyDashboardURL()
  14. redirectFromLegacyDashboardSoloUrl := RedirectFromLegacyDashboardSoloURL()
  15. fakeDash := m.NewDashboard("Child dash")
  16. fakeDash.Id = 1
  17. fakeDash.FolderId = 1
  18. fakeDash.HasAcl = false
  19. fakeDash.Uid = util.GenerateShortUID()
  20. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  21. query.Result = fakeDash
  22. return nil
  23. })
  24. middlewareScenario(t, "GET dashboard by legacy url", func(sc *scenarioContext) {
  25. sc.m.Get("/dashboard/db/:slug", redirectFromLegacyDashboardUrl, sc.defaultHandler)
  26. sc.fakeReqWithParams("GET", "/dashboard/db/dash?orgId=1&panelId=2", map[string]string{}).exec()
  27. Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
  28. So(sc.resp.Code, ShouldEqual, 301)
  29. redirectURL, _ := sc.resp.Result().Location()
  30. So(redirectURL.Path, ShouldEqual, m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug))
  31. So(len(redirectURL.Query()), ShouldEqual, 2)
  32. })
  33. })
  34. middlewareScenario(t, "GET dashboard solo by legacy url", func(sc *scenarioContext) {
  35. sc.m.Get("/dashboard-solo/db/:slug", redirectFromLegacyDashboardSoloUrl, sc.defaultHandler)
  36. sc.fakeReqWithParams("GET", "/dashboard-solo/db/dash?orgId=1&panelId=2", map[string]string{}).exec()
  37. Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
  38. So(sc.resp.Code, ShouldEqual, 301)
  39. redirectURL, _ := sc.resp.Result().Location()
  40. expectedURL := m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)
  41. expectedURL = strings.Replace(expectedURL, "/d/", "/d-solo/", 1)
  42. So(redirectURL.Path, ShouldEqual, expectedURL)
  43. So(len(redirectURL.Query()), ShouldEqual, 2)
  44. })
  45. })
  46. })
  47. }