common_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. . "github.com/smartystreets/goconvey/convey"
  11. "gopkg.in/macaron.v1"
  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. func (sc *scenarioContext) fakeReqNoAssertions(method, url string) *scenarioContext {
  79. sc.resp = httptest.NewRecorder()
  80. req, _ := http.NewRequest(method, url, nil)
  81. sc.req = req
  82. return sc
  83. }
  84. func (sc *scenarioContext) fakeReqNoAssertionsWithCookie(method, url string, cookie http.Cookie) *scenarioContext {
  85. sc.resp = httptest.NewRecorder()
  86. http.SetCookie(sc.resp, &cookie)
  87. req, _ := http.NewRequest(method, url, nil)
  88. req.Header = http.Header{"Cookie": sc.resp.Header()["Set-Cookie"]}
  89. sc.req = req
  90. return sc
  91. }
  92. type scenarioContext struct {
  93. m *macaron.Macaron
  94. context *m.ReqContext
  95. resp *httptest.ResponseRecorder
  96. handlerFunc handlerFunc
  97. defaultHandler macaron.Handler
  98. req *http.Request
  99. url string
  100. userAuthTokenService *auth.FakeUserAuthTokenService
  101. }
  102. func (sc *scenarioContext) exec() {
  103. sc.m.ServeHTTP(sc.resp, sc.req)
  104. }
  105. type scenarioFunc func(c *scenarioContext)
  106. type handlerFunc func(c *m.ReqContext) Response
  107. func setupScenarioContext(url string) *scenarioContext {
  108. sc := &scenarioContext{
  109. url: url,
  110. }
  111. viewsPath, _ := filepath.Abs("../../public/views")
  112. sc.m = macaron.New()
  113. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  114. Directory: viewsPath,
  115. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  116. }))
  117. sc.m.Use(middleware.GetContextHandler(nil, nil))
  118. return sc
  119. }