testing.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package middleware
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "gopkg.in/macaron.v1"
  8. "github.com/grafana/grafana/pkg/infra/remotecache"
  9. "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/services/auth"
  11. "github.com/grafana/grafana/pkg/setting"
  12. )
  13. type scenarioContext struct {
  14. m *macaron.Macaron
  15. context *models.ReqContext
  16. resp *httptest.ResponseRecorder
  17. apiKey string
  18. authHeader string
  19. tokenSessionCookie string
  20. respJson map[string]interface{}
  21. handlerFunc handlerFunc
  22. defaultHandler macaron.Handler
  23. url string
  24. userAuthTokenService *auth.FakeUserAuthTokenService
  25. remoteCacheService *remotecache.RemoteCache
  26. req *http.Request
  27. }
  28. func (sc *scenarioContext) withValidApiKey() *scenarioContext {
  29. sc.apiKey = "eyJrIjoidjVuQXdwTWFmRlA2em5hUzR1cmhkV0RMUzU1MTFNNDIiLCJuIjoiYXNkIiwiaWQiOjF9"
  30. return sc
  31. }
  32. func (sc *scenarioContext) withTokenSessionCookie(unhashedToken string) *scenarioContext {
  33. sc.tokenSessionCookie = unhashedToken
  34. return sc
  35. }
  36. func (sc *scenarioContext) withAuthorizationHeader(authHeader string) *scenarioContext {
  37. sc.authHeader = authHeader
  38. return sc
  39. }
  40. func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
  41. sc.resp = httptest.NewRecorder()
  42. req, err := http.NewRequest(method, url, nil)
  43. So(err, ShouldBeNil)
  44. sc.req = req
  45. return sc
  46. }
  47. func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
  48. sc.resp = httptest.NewRecorder()
  49. req, err := http.NewRequest(method, url, nil)
  50. q := req.URL.Query()
  51. for k, v := range queryParams {
  52. q.Add(k, v)
  53. }
  54. req.URL.RawQuery = q.Encode()
  55. So(err, ShouldBeNil)
  56. sc.req = req
  57. return sc
  58. }
  59. func (sc *scenarioContext) handler(fn handlerFunc) *scenarioContext {
  60. sc.handlerFunc = fn
  61. return sc
  62. }
  63. func (sc *scenarioContext) exec() {
  64. if sc.apiKey != "" {
  65. sc.req.Header.Add("Authorization", "Bearer "+sc.apiKey)
  66. }
  67. if sc.authHeader != "" {
  68. sc.req.Header.Add("Authorization", sc.authHeader)
  69. }
  70. if sc.tokenSessionCookie != "" {
  71. sc.req.AddCookie(&http.Cookie{
  72. Name: setting.LoginCookieName,
  73. Value: sc.tokenSessionCookie,
  74. })
  75. }
  76. sc.m.ServeHTTP(sc.resp, sc.req)
  77. if sc.resp.Header().Get("Content-Type") == "application/json; charset=UTF-8" {
  78. err := json.NewDecoder(sc.resp.Body).Decode(&sc.respJson)
  79. So(err, ShouldBeNil)
  80. }
  81. }
  82. type scenarioFunc func(c *scenarioContext)
  83. type handlerFunc func(c *models.ReqContext)