common_test.go 2.9 KB

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