common_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "github.com/grafana/grafana/pkg/services/auth"
  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 anonymousUserScenario(desc string, method string, url string, routePattern string, fn scenarioFunc) {
  40. Convey(desc+" "+url, func() {
  41. defer bus.ClearBusHandlers()
  42. sc := setupScenarioContext(url)
  43. sc.defaultHandler = Wrap(func(c *m.ReqContext) Response {
  44. sc.context = c
  45. if sc.handlerFunc != nil {
  46. return sc.handlerFunc(sc.context)
  47. }
  48. return nil
  49. })
  50. switch method {
  51. case "GET":
  52. sc.m.Get(routePattern, sc.defaultHandler)
  53. case "DELETE":
  54. sc.m.Delete(routePattern, sc.defaultHandler)
  55. }
  56. fn(sc)
  57. })
  58. }
  59. func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
  60. sc.resp = httptest.NewRecorder()
  61. req, err := http.NewRequest(method, url, nil)
  62. So(err, ShouldBeNil)
  63. sc.req = req
  64. return sc
  65. }
  66. func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
  67. sc.resp = httptest.NewRecorder()
  68. req, err := http.NewRequest(method, url, nil)
  69. q := req.URL.Query()
  70. for k, v := range queryParams {
  71. q.Add(k, v)
  72. }
  73. req.URL.RawQuery = q.Encode()
  74. So(err, ShouldBeNil)
  75. sc.req = req
  76. return sc
  77. }
  78. type scenarioContext struct {
  79. m *macaron.Macaron
  80. context *m.ReqContext
  81. resp *httptest.ResponseRecorder
  82. handlerFunc handlerFunc
  83. defaultHandler macaron.Handler
  84. req *http.Request
  85. url string
  86. userAuthTokenService *auth.FakeUserAuthTokenService
  87. }
  88. func (sc *scenarioContext) exec() {
  89. sc.m.ServeHTTP(sc.resp, sc.req)
  90. }
  91. type scenarioFunc func(c *scenarioContext)
  92. type handlerFunc func(c *m.ReqContext) Response
  93. func setupScenarioContext(url string) *scenarioContext {
  94. sc := &scenarioContext{
  95. url: url,
  96. }
  97. viewsPath, _ := filepath.Abs("../../public/views")
  98. sc.m = macaron.New()
  99. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  100. Directory: viewsPath,
  101. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  102. }))
  103. sc.m.Use(middleware.GetContextHandler(nil))
  104. return sc
  105. }