|
@@ -2,17 +2,11 @@ package api
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
- "net/http"
|
|
|
|
|
- "net/http/httptest"
|
|
|
|
|
- "path/filepath"
|
|
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
- macaron "gopkg.in/macaron.v1"
|
|
|
|
|
|
|
|
|
|
- "github.com/go-macaron/session"
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
- "github.com/grafana/grafana/pkg/middleware"
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -54,88 +48,3 @@ func TestDataSourcesProxy(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-func loggedInUserScenario(desc string, url string, fn scenarioFunc) {
|
|
|
|
|
- loggedInUserScenarioWithRole(desc, "GET", url, url, models.ROLE_EDITOR, fn)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func loggedInUserScenarioWithRole(desc string, method string, url string, routePattern string, role models.RoleType, fn scenarioFunc) {
|
|
|
|
|
- Convey(desc+" "+url, func() {
|
|
|
|
|
- defer bus.ClearBusHandlers()
|
|
|
|
|
-
|
|
|
|
|
- sc := &scenarioContext{
|
|
|
|
|
- url: url,
|
|
|
|
|
- }
|
|
|
|
|
- viewsPath, _ := filepath.Abs("../../public/views")
|
|
|
|
|
-
|
|
|
|
|
- sc.m = macaron.New()
|
|
|
|
|
- sc.m.Use(macaron.Renderer(macaron.RenderOptions{
|
|
|
|
|
- Directory: viewsPath,
|
|
|
|
|
- Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
|
|
|
|
- }))
|
|
|
|
|
-
|
|
|
|
|
- sc.m.Use(middleware.GetContextHandler())
|
|
|
|
|
- sc.m.Use(middleware.Sessioner(&session.Options{}))
|
|
|
|
|
-
|
|
|
|
|
- sc.defaultHandler = wrap(func(c *middleware.Context) Response {
|
|
|
|
|
- sc.context = c
|
|
|
|
|
- sc.context.UserId = TestUserID
|
|
|
|
|
- sc.context.OrgId = TestOrgID
|
|
|
|
|
- sc.context.OrgRole = role
|
|
|
|
|
- if sc.handlerFunc != nil {
|
|
|
|
|
- return sc.handlerFunc(sc.context)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return nil
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- switch method {
|
|
|
|
|
- case "GET":
|
|
|
|
|
- sc.m.Get(routePattern, sc.defaultHandler)
|
|
|
|
|
- case "DELETE":
|
|
|
|
|
- sc.m.Delete(routePattern, sc.defaultHandler)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- fn(sc)
|
|
|
|
|
- })
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func (sc *scenarioContext) fakeReq(method, url string) *scenarioContext {
|
|
|
|
|
- sc.resp = httptest.NewRecorder()
|
|
|
|
|
- req, err := http.NewRequest(method, url, nil)
|
|
|
|
|
- So(err, ShouldBeNil)
|
|
|
|
|
- sc.req = req
|
|
|
|
|
-
|
|
|
|
|
- return sc
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map[string]string) *scenarioContext {
|
|
|
|
|
- sc.resp = httptest.NewRecorder()
|
|
|
|
|
- req, err := http.NewRequest(method, url, nil)
|
|
|
|
|
- q := req.URL.Query()
|
|
|
|
|
- for k, v := range queryParams {
|
|
|
|
|
- q.Add(k, v)
|
|
|
|
|
- }
|
|
|
|
|
- req.URL.RawQuery = q.Encode()
|
|
|
|
|
- So(err, ShouldBeNil)
|
|
|
|
|
- sc.req = req
|
|
|
|
|
-
|
|
|
|
|
- return sc
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-type scenarioContext struct {
|
|
|
|
|
- m *macaron.Macaron
|
|
|
|
|
- context *middleware.Context
|
|
|
|
|
- resp *httptest.ResponseRecorder
|
|
|
|
|
- handlerFunc handlerFunc
|
|
|
|
|
- defaultHandler macaron.Handler
|
|
|
|
|
- req *http.Request
|
|
|
|
|
- url string
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func (sc *scenarioContext) exec() {
|
|
|
|
|
- sc.m.ServeHTTP(sc.resp, sc.req)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-type scenarioFunc func(c *scenarioContext)
|
|
|
|
|
-type handlerFunc func(c *middleware.Context) Response
|
|
|