datasources_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "path/filepath"
  7. "testing"
  8. m "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 *m.GetDataSourcesQuery) error {
  24. So(query.OrgId, ShouldEqual, TestOrgID)
  25. query.Result = []*m.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. viewsPath, _ := filepath.Abs("../../public/views")
  53. sc.m = macaron.New()
  54. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  55. Directory: viewsPath,
  56. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  57. }))
  58. sc.m.Use(middleware.GetContextHandler())
  59. sc.m.Use(middleware.Sessioner(&session.Options{}))
  60. sc.defaultHandler = func(c *middleware.Context) {
  61. sc.context = c
  62. sc.context.UserId = TestUserID
  63. sc.context.OrgId = TestOrgID
  64. sc.context.OrgRole = m.ROLE_EDITOR
  65. if sc.handlerFunc != nil {
  66. sc.handlerFunc(sc.context)
  67. }
  68. }
  69. sc.m.SetAutoHead(true)
  70. sc.m.Get(url, sc.defaultHandler)
  71. fn(sc)
  72. })
  73. }
  74. func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
  75. sc.resp = httptest.NewRecorder()
  76. req, err := http.NewRequest(method, url, nil)
  77. So(err, ShouldBeNil)
  78. sc.req = req
  79. return sc
  80. }
  81. type scenarioContext struct {
  82. m *macaron.Macaron
  83. context *middleware.Context
  84. resp *httptest.ResponseRecorder
  85. apiKey string
  86. authHeader string
  87. handlerFunc handlerFunc
  88. defaultHandler macaron.Handler
  89. req *http.Request
  90. }
  91. func (sc *scenarioContext) exec() {
  92. if sc.apiKey != "" {
  93. sc.req.Header.Add("Authorization", "Bearer "+sc.apiKey)
  94. }
  95. if sc.authHeader != "" {
  96. sc.req.Header.Add("Authorization", sc.authHeader)
  97. }
  98. sc.m.ServeHTTP(sc.resp, sc.req)
  99. }
  100. type scenarioFunc func(c *scenarioContext)
  101. type handlerFunc func(c *middleware.Context)