datasources_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package api
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/bus"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. const (
  10. TestOrgID = 1
  11. TestUserID = 1
  12. )
  13. func TestDataSourcesProxy(t *testing.T) {
  14. Convey("Given a user is logged in", t, func() {
  15. loggedInUserScenario("When calling GET on", "/api/datasources/", func(sc *scenarioContext) {
  16. // Stubs the database query
  17. bus.AddHandler("test", func(query *models.GetDataSourcesQuery) error {
  18. So(query.OrgId, ShouldEqual, TestOrgID)
  19. query.Result = []*models.DataSource{
  20. {Name: "mmm"},
  21. {Name: "ZZZ"},
  22. {Name: "BBB"},
  23. {Name: "aaa"},
  24. }
  25. return nil
  26. })
  27. // handler func being tested
  28. sc.handlerFunc = GetDataSources
  29. sc.fakeReq("GET", "/api/datasources").exec()
  30. respJSON := []map[string]interface{}{}
  31. err := json.NewDecoder(sc.resp.Body).Decode(&respJSON)
  32. So(err, ShouldBeNil)
  33. Convey("should return list of datasources for org sorted alphabetically and case insensitively", func() {
  34. So(respJSON[0]["name"], ShouldEqual, "aaa")
  35. So(respJSON[1]["name"], ShouldEqual, "BBB")
  36. So(respJSON[2]["name"], ShouldEqual, "mmm")
  37. So(respJSON[3]["name"], ShouldEqual, "ZZZ")
  38. })
  39. })
  40. Convey("Should be able to save a data source", func() {
  41. loggedInUserScenario("When calling DELETE on non-existing", "/api/datasources/name/12345", func(sc *scenarioContext) {
  42. sc.handlerFunc = DeleteDataSourceByName
  43. sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
  44. So(sc.resp.Code, ShouldEqual, 404)
  45. })
  46. })
  47. })
  48. }