datasources_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "path/filepath"
  7. "testing"
  8. "github.com/grafana/grafana/pkg/models"
  9. macaron "gopkg.in/macaron.v1"
  10. "github.com/go-macaron/session"
  11. "github.com/grafana/grafana/pkg/bus"
  12. "github.com/grafana/grafana/pkg/middleware"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. const (
  16. TestOrgID = 1
  17. TestUserID = 1
  18. )
  19. func TestDataSourcesProxy(t *testing.T) {
  20. Convey("Given a user is logged in", t, func() {
  21. loggedInUserScenario("When calling GET on", "/api/datasources/", func(sc *scenarioContext) {
  22. // Stubs the database query
  23. bus.AddHandler("test", func(query *models.GetDataSourcesQuery) error {
  24. So(query.OrgId, ShouldEqual, TestOrgID)
  25. query.Result = []*models.DataSource{
  26. {Name: "mmm"},
  27. {Name: "ZZZ"},
  28. {Name: "BBB"},
  29. {Name: "aaa"},
  30. }
  31. return nil
  32. })
  33. // handler func being tested
  34. sc.handlerFunc = GetDataSources
  35. sc.fakeReq("GET", "/api/datasources").exec()
  36. respJSON := []map[string]interface{}{}
  37. err := json.NewDecoder(sc.resp.Body).Decode(&respJSON)
  38. So(err, ShouldBeNil)
  39. Convey("should return list of datasources for org sorted alphabetically and case insensitively", func() {
  40. So(respJSON[0]["name"], ShouldEqual, "aaa")
  41. So(respJSON[1]["name"], ShouldEqual, "BBB")
  42. So(respJSON[2]["name"], ShouldEqual, "mmm")
  43. So(respJSON[3]["name"], ShouldEqual, "ZZZ")
  44. })
  45. })
  46. })
  47. }
  48. func loggedInUserScenario(desc string, url string, fn scenarioFunc) {
  49. Convey(desc+" "+url, func() {
  50. defer bus.ClearBusHandlers()
  51. sc := &scenarioContext{
  52. url: url,
  53. }
  54. viewsPath, _ := filepath.Abs("../../public/views")
  55. sc.m = macaron.New()
  56. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  57. Directory: viewsPath,
  58. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  59. }))
  60. sc.m.Use(middleware.GetContextHandler())
  61. sc.m.Use(middleware.Sessioner(&session.Options{}))
  62. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  63. sc.context = c
  64. sc.context.UserId = TestUserID
  65. sc.context.OrgId = TestOrgID
  66. sc.context.OrgRole = models.ROLE_EDITOR
  67. if sc.handlerFunc != nil {
  68. return sc.handlerFunc(sc.context)
  69. }
  70. return nil
  71. })
  72. sc.m.Get(url, sc.defaultHandler)
  73. fn(sc)
  74. })
  75. }
  76. func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
  77. sc.resp = httptest.NewRecorder()
  78. req, err := http.NewRequest(method, url, nil)
  79. So(err, ShouldBeNil)
  80. sc.req = req
  81. return sc
  82. }
  83. func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
  84. sc.resp = httptest.NewRecorder()
  85. req, err := http.NewRequest(method, url, nil)
  86. q := req.URL.Query()
  87. for k, v := range queryParams {
  88. q.Add(k, v)
  89. }
  90. req.URL.RawQuery = q.Encode()
  91. So(err, ShouldBeNil)
  92. sc.req = req
  93. return sc
  94. }
  95. type scenarioContext struct {
  96. m *macaron.Macaron
  97. context *middleware.Context
  98. resp *httptest.ResponseRecorder
  99. handlerFunc handlerFunc
  100. defaultHandler macaron.Handler
  101. req *http.Request
  102. url string
  103. }
  104. func (sc *scenarioContext) exec() {
  105. sc.m.ServeHTTP(sc.resp, sc.req)
  106. }
  107. type scenarioFunc func(c *scenarioContext)
  108. type handlerFunc func(c *middleware.Context) Response