dashboard_redirect_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/smartystreets/goconvey/convey"
  8. )
  9. func TestMiddlewareDashboardRedirect(t *testing.T) {
  10. Convey("Given the dashboard redirect middleware", t, func() {
  11. bus.ClearBusHandlers()
  12. redirectFromLegacyDashboardUrl := RedirectFromLegacyDashboardUrl()
  13. redirectFromLegacyDashboardSoloUrl := RedirectFromLegacyDashboardSoloUrl()
  14. fakeDash := m.NewDashboard("Child dash")
  15. fakeDash.Id = 1
  16. fakeDash.FolderId = 1
  17. fakeDash.HasAcl = false
  18. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  19. query.Result = fakeDash
  20. return nil
  21. })
  22. middlewareScenario("GET dashboard by legacy url", func(sc *scenarioContext) {
  23. sc.m.Get("/dashboard/db/:slug", redirectFromLegacyDashboardUrl, sc.defaultHandler)
  24. sc.fakeReqWithParams("GET", "/dashboard/db/dash", map[string]string{}).exec()
  25. Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
  26. So(sc.resp.Code, ShouldEqual, 301)
  27. redirectUrl, _ := sc.resp.Result().Location()
  28. So(redirectUrl.Path, ShouldEqual, m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug))
  29. })
  30. })
  31. middlewareScenario("GET dashboard solo by legacy url", func(sc *scenarioContext) {
  32. sc.m.Get("/dashboard-solo/db/:slug", redirectFromLegacyDashboardSoloUrl, sc.defaultHandler)
  33. sc.fakeReqWithParams("GET", "/dashboard-solo/db/dash", map[string]string{}).exec()
  34. Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
  35. So(sc.resp.Code, ShouldEqual, 301)
  36. redirectUrl, _ := sc.resp.Result().Location()
  37. expectedUrl := m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)
  38. expectedUrl = strings.Replace(expectedUrl, "/d/", "/d-solo/", 1)
  39. So(redirectUrl.Path, ShouldEqual, expectedUrl)
  40. })
  41. })
  42. })
  43. }