2017-02-07 15:15:52 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-08 04:57:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-05-12 06:04:18 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-11-13 02:52:38 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-02-07 15:15:52 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-13 02:52:38 -06:00
|
|
|
testOrgID int64 = 1
|
|
|
|
testUserID int64 = 1
|
2017-02-07 15:15:52 -06:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
func TestDataSourcesProxy_userLoggedIn(t *testing.T) {
|
|
|
|
loggedInUserScenario(t, "When calling GET on", "/api/datasources/", func(sc *scenarioContext) {
|
|
|
|
// Stubs the database query
|
|
|
|
bus.AddHandler("test", func(query *models.GetDataSourcesQuery) error {
|
|
|
|
assert.Equal(t, testOrgID, query.OrgId)
|
|
|
|
query.Result = []*models.DataSource{
|
|
|
|
{Name: "mmm"},
|
|
|
|
{Name: "ZZZ"},
|
|
|
|
{Name: "BBB"},
|
|
|
|
{Name: "aaa"},
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-07 15:15:52 -06:00
|
|
|
})
|
2018-06-22 21:15:36 -05:00
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
// handler func being tested
|
|
|
|
sc.handlerFunc = GetDataSources
|
|
|
|
sc.fakeReq("GET", "/api/datasources").exec()
|
|
|
|
|
|
|
|
respJSON := []map[string]interface{}{}
|
|
|
|
err := json.NewDecoder(sc.resp.Body).Decode(&respJSON)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "aaa", respJSON[0]["name"])
|
|
|
|
assert.Equal(t, "BBB", respJSON[1]["name"])
|
|
|
|
assert.Equal(t, "mmm", respJSON[2]["name"])
|
|
|
|
assert.Equal(t, "ZZZ", respJSON[3]["name"])
|
2017-02-07 15:15:52 -06:00
|
|
|
})
|
2020-11-13 02:52:38 -06:00
|
|
|
|
|
|
|
loggedInUserScenario(t, "Should be able to save a data source when calling DELETE on non-existing",
|
|
|
|
"/api/datasources/name/12345", func(sc *scenarioContext) {
|
|
|
|
sc.handlerFunc = DeleteDataSourceByName
|
|
|
|
sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec()
|
|
|
|
assert.Equal(t, 404, sc.resp.Code)
|
|
|
|
})
|
2017-02-07 15:15:52 -06:00
|
|
|
}
|
2020-05-12 06:04:18 -05:00
|
|
|
|
|
|
|
// Adding data sources with invalid URLs should lead to an error.
|
|
|
|
func TestAddDataSource_InvalidURL(t *testing.T) {
|
|
|
|
defer bus.ClearBusHandlers()
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, "/api/datasources")
|
2020-05-12 06:04:18 -05:00
|
|
|
|
|
|
|
sc.m.Post(sc.url, Wrap(func(c *models.ReqContext) Response {
|
|
|
|
return AddDataSource(c, models.AddDataSourceCommand{
|
|
|
|
Name: "Test",
|
|
|
|
Url: "invalid:url",
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
assert.Equal(t, 400, sc.resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding data sources with URLs not specifying protocol should work.
|
|
|
|
func TestAddDataSource_URLWithoutProtocol(t *testing.T) {
|
|
|
|
defer bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
const name = "Test"
|
|
|
|
const url = "localhost:5432"
|
|
|
|
|
|
|
|
// Stub handler
|
|
|
|
bus.AddHandler("sql", func(cmd *models.AddDataSourceCommand) error {
|
|
|
|
assert.Equal(t, name, cmd.Name)
|
|
|
|
assert.Equal(t, url, cmd.Url)
|
|
|
|
|
|
|
|
cmd.Result = &models.DataSource{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, "/api/datasources")
|
2020-05-12 06:04:18 -05:00
|
|
|
|
|
|
|
sc.m.Post(sc.url, Wrap(func(c *models.ReqContext) Response {
|
|
|
|
return AddDataSource(c, models.AddDataSourceCommand{
|
|
|
|
Name: name,
|
|
|
|
Url: url,
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating data sources with invalid URLs should lead to an error.
|
|
|
|
func TestUpdateDataSource_InvalidURL(t *testing.T) {
|
|
|
|
defer bus.ClearBusHandlers()
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, "/api/datasources/1234")
|
2020-05-12 06:04:18 -05:00
|
|
|
|
|
|
|
sc.m.Put(sc.url, Wrap(func(c *models.ReqContext) Response {
|
|
|
|
return AddDataSource(c, models.AddDataSourceCommand{
|
|
|
|
Name: "Test",
|
|
|
|
Url: "invalid:url",
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
assert.Equal(t, 400, sc.resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating data sources with URLs not specifying protocol should work.
|
|
|
|
func TestUpdateDataSource_URLWithoutProtocol(t *testing.T) {
|
|
|
|
defer bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
const name = "Test"
|
|
|
|
const url = "localhost:5432"
|
|
|
|
|
|
|
|
// Stub handler
|
|
|
|
bus.AddHandler("sql", func(cmd *models.AddDataSourceCommand) error {
|
|
|
|
assert.Equal(t, name, cmd.Name)
|
|
|
|
assert.Equal(t, url, cmd.Url)
|
|
|
|
|
|
|
|
cmd.Result = &models.DataSource{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-11-13 02:52:38 -06:00
|
|
|
sc := setupScenarioContext(t, "/api/datasources/1234")
|
2020-05-12 06:04:18 -05:00
|
|
|
|
|
|
|
sc.m.Put(sc.url, Wrap(func(c *models.ReqContext) Response {
|
|
|
|
return AddDataSource(c, models.AddDataSourceCommand{
|
|
|
|
Name: name,
|
|
|
|
Url: url,
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
}
|