2018-01-30 06:17:48 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/go-macaron/session"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2018-03-06 16:59:45 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"gopkg.in/macaron.v1"
|
2018-01-30 06:17:48 -06:00
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func loggedInUserScenario(desc string, url string, fn scenarioFunc) {
|
2018-03-06 16:59:45 -06:00
|
|
|
loggedInUserScenarioWithRole(desc, "GET", url, url, m.ROLE_EDITOR, fn)
|
2018-01-30 06:17:48 -06:00
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
func loggedInUserScenarioWithRole(desc string, method string, url string, routePattern string, role m.RoleType, fn scenarioFunc) {
|
2018-01-30 06:17:48 -06:00
|
|
|
Convey(desc+" "+url, func() {
|
|
|
|
defer bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
sc := setupScenarioContext(url)
|
2018-03-07 10:54:50 -06:00
|
|
|
sc.defaultHandler = wrap(func(c *m.ReqContext) Response {
|
2018-01-30 06:17:48 -06:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-24 01:55:16 -05:00
|
|
|
func anonymousUserScenario(desc string, method string, url string, routePattern string, fn scenarioFunc) {
|
|
|
|
Convey(desc+" "+url, func() {
|
|
|
|
defer bus.ClearBusHandlers()
|
|
|
|
|
|
|
|
sc := setupScenarioContext(url)
|
|
|
|
sc.defaultHandler = wrap(func(c *m.ReqContext) Response {
|
|
|
|
sc.context = c
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:17:48 -06:00
|
|
|
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
|
2018-03-07 10:54:50 -06:00
|
|
|
context *m.ReqContext
|
2018-01-30 06:17:48 -06:00
|
|
|
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)
|
2018-03-07 10:54:50 -06:00
|
|
|
type handlerFunc func(c *m.ReqContext) Response
|
2018-01-30 06:17:48 -06:00
|
|
|
|
|
|
|
func setupScenarioContext(url string) *scenarioContext {
|
|
|
|
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())
|
2018-03-15 15:23:33 -05:00
|
|
|
sc.m.Use(middleware.Sessioner(&session.Options{}, 0))
|
2018-01-30 06:17:48 -06:00
|
|
|
|
|
|
|
return sc
|
|
|
|
}
|