datasources_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. loggedInUserScenarioWithRole(desc, "GET", url, url, models.ROLE_EDITOR, fn)
  50. }
  51. func loggedInUserScenarioWithRole(desc string, method string, url string, routePattern string, role models.RoleType, fn scenarioFunc) {
  52. Convey(desc+" "+url, func() {
  53. defer bus.ClearBusHandlers()
  54. sc := &scenarioContext{
  55. url: url,
  56. }
  57. viewsPath, _ := filepath.Abs("../../public/views")
  58. sc.m = macaron.New()
  59. sc.m.Use(macaron.Renderer(macaron.RenderOptions{
  60. Directory: viewsPath,
  61. Delims: macaron.Delims{Left: "[[", Right: "]]"},
  62. }))
  63. sc.m.Use(middleware.GetContextHandler())
  64. sc.m.Use(middleware.Sessioner(&session.Options{}))
  65. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  66. sc.context = c
  67. sc.context.UserId = TestUserID
  68. sc.context.OrgId = TestOrgID
  69. sc.context.OrgRole = role
  70. if sc.handlerFunc != nil {
  71. return sc.handlerFunc(sc.context)
  72. }
  73. return nil
  74. })
  75. switch method {
  76. case "GET":
  77. sc.m.Get(routePattern, sc.defaultHandler)
  78. case "DELETE":
  79. sc.m.Delete(routePattern, sc.defaultHandler)
  80. }
  81. fn(sc)
  82. })
  83. }
  84. func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
  85. sc.resp = httptest.NewRecorder()
  86. req, err := http.NewRequest(method, url, nil)
  87. So(err, ShouldBeNil)
  88. sc.req = req
  89. return sc
  90. }
  91. func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
  92. sc.resp = httptest.NewRecorder()
  93. req, err := http.NewRequest(method, url, nil)
  94. q := req.URL.Query()
  95. for k, v := range queryParams {
  96. q.Add(k, v)
  97. }
  98. req.URL.RawQuery = q.Encode()
  99. So(err, ShouldBeNil)
  100. sc.req = req
  101. return sc
  102. }
  103. type scenarioContext struct {
  104. m *macaron.Macaron
  105. context *middleware.Context
  106. resp *httptest.ResponseRecorder
  107. handlerFunc handlerFunc
  108. defaultHandler macaron.Handler
  109. req *http.Request
  110. url string
  111. }
  112. func (sc *scenarioContext) exec() {
  113. sc.m.ServeHTTP(sc.resp, sc.req)
  114. }
  115. type scenarioFunc func(c *scenarioContext)
  116. type handlerFunc func(c *middleware.Context) Response