common_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package api
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "path/filepath"
  6. "github.com/go-macaron/session"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/middleware"
  9. m "github.com/grafana/grafana/pkg/models"
  10. "gopkg.in/macaron.v1"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. func loggedInUserScenario(desc string, url string, fn scenarioFunc) {
  14. loggedInUserScenarioWithRole(desc, "GET", url, url, m.ROLE_EDITOR, fn)
  15. }
  16. func loggedInUserScenarioWithRole(desc string, method string, url string, routePattern string, role m.RoleType, fn scenarioFunc) {
  17. Convey(desc+" "+url, func() {
  18. defer bus.ClearBusHandlers()
  19. sc := setupScenarioContext(url)
  20. sc.defaultHandler = wrap(func(c *m.ReqContext) Response {
  21. sc.context = c
  22. sc.context.UserId = TestUserID
  23. sc.context.OrgId = TestOrgID
  24. sc.context.OrgRole = role
  25. if sc.handlerFunc != nil {
  26. return sc.handlerFunc(sc.context)
  27. }
  28. return nil
  29. })
  30. switch method {
  31. case "GET":
  32. sc.m.Get(routePattern, sc.defaultHandler)
  33. case "DELETE":
  34. sc.m.Delete(routePattern, sc.defaultHandler)
  35. }
  36. fn(sc)
  37. })
  38. }
  39. func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
  40. sc.resp = httptest.NewRecorder()
  41. req, err := http.NewRequest(method, url, nil)
  42. So(err, ShouldBeNil)
  43. sc.req = req
  44. return sc
  45. }
  46. func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
  47. sc.resp = httptest.NewRecorder()
  48. req, err := http.NewRequest(method, url, nil)
  49. q := req.URL.Query()
  50. for k, v := range queryParams {
  51. q.Add(k, v)
  52. }
  53. req.URL.RawQuery = q.Encode()
  54. So(err, ShouldBeNil)
  55. sc.req = req
  56. return sc
  57. }
  58. type scenarioContext struct {
  59. m *macaron.Macaron
  60. context *m.ReqContext
  61. resp *httptest.ResponseRecorder
  62. handlerFunc handlerFunc
  63. defaultHandler macaron.Handler
  64. req *http.Request
  65. url string
  66. }
  67. func (sc *scenarioContext) exec() {
  68. sc.m.ServeHTTP(sc.resp, sc.req)
  69. }
  70. type scenarioFunc func(c *scenarioContext)
  71. type handlerFunc func(c *m.ReqContext) Response
  72. func setupScenarioContext(url string) *scenarioContext {
  73. sc := &scenarioContext{
  74. url: url,
  75. }
  76. viewsPath, _ := filepath.Abs("../../public/views")
  77. sc.m = macaron.New()
  78. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  79. Directory: viewsPath,
  80. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  81. }))
  82. sc.m.Use(middleware.GetContextHandler())
  83. sc.m.Use(middleware.Sessioner(&session.Options{}))
  84. return sc
  85. }