dashboard_redirect_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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("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", 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. })
  32. })
  33. middlewareScenario("GET dashboard solo by legacy url", func(sc *scenarioContext) {
  34. sc.m.Get("/dashboard-solo/db/:slug", redirectFromLegacyDashboardSoloUrl, sc.defaultHandler)
  35. sc.fakeReqWithParams("GET", "/dashboard-solo/db/dash", map[string]string{}).exec()
  36. Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
  37. So(sc.resp.Code, ShouldEqual, 301)
  38. redirectUrl, _ := sc.resp.Result().Location()
  39. expectedUrl := m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)
  40. expectedUrl = strings.Replace(expectedUrl, "/d/", "/d-solo/", 1)
  41. So(redirectUrl.Path, ShouldEqual, expectedUrl)
  42. })
  43. })
  44. })
  45. }