mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 21:42:38 +08:00
passing middleware tests
This commit is contained in:
@ -95,13 +95,14 @@ func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map
|
|||||||
}
|
}
|
||||||
|
|
||||||
type scenarioContext struct {
|
type scenarioContext struct {
|
||||||
m *macaron.Macaron
|
m *macaron.Macaron
|
||||||
context *m.ReqContext
|
context *m.ReqContext
|
||||||
resp *httptest.ResponseRecorder
|
resp *httptest.ResponseRecorder
|
||||||
handlerFunc handlerFunc
|
handlerFunc handlerFunc
|
||||||
defaultHandler macaron.Handler
|
defaultHandler macaron.Handler
|
||||||
req *http.Request
|
req *http.Request
|
||||||
url string
|
url string
|
||||||
|
userAuthTokenService *fakeUserAuthTokenService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *scenarioContext) exec() {
|
func (sc *scenarioContext) exec() {
|
||||||
@ -123,8 +124,31 @@ func setupScenarioContext(url string) *scenarioContext {
|
|||||||
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
||||||
}))
|
}))
|
||||||
|
|
||||||
sc.m.Use(middleware.GetContextHandler(nil))
|
sc.userAuthTokenService = newFakeUserAuthTokenService()
|
||||||
|
sc.m.Use(middleware.GetContextHandler(sc.userAuthTokenService))
|
||||||
sc.m.Use(middleware.Sessioner(&session.Options{}, 0))
|
sc.m.Use(middleware.Sessioner(&session.Options{}, 0))
|
||||||
|
|
||||||
return sc
|
return sc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fakeUserAuthTokenService struct {
|
||||||
|
initContextWithTokenProvider func(ctx *m.ReqContext, orgID int64) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFakeUserAuthTokenService() *fakeUserAuthTokenService {
|
||||||
|
return &fakeUserAuthTokenService{
|
||||||
|
initContextWithTokenProvider: func(ctx *m.ReqContext, orgID int64) bool {
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeUserAuthTokenService) InitContextWithToken(ctx *m.ReqContext, orgID int64) bool {
|
||||||
|
return s.initContextWithTokenProvider(ctx, orgID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeUserAuthTokenService) UserAuthenticatedHook(user *m.User, c *m.ReqContext) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeUserAuthTokenService) UserSignedOutHook(c *m.ReqContext) {}
|
||||||
|
@ -43,11 +43,6 @@ func TestMiddlewareContext(t *testing.T) {
|
|||||||
So(sc.resp.Header().Get("Cache-Control"), ShouldBeEmpty)
|
So(sc.resp.Header().Get("Cache-Control"), ShouldBeEmpty)
|
||||||
})
|
})
|
||||||
|
|
||||||
middlewareScenario("Non api request should init session", func(sc *scenarioContext) {
|
|
||||||
sc.fakeReq("GET", "/").exec()
|
|
||||||
So(sc.resp.Header().Get("Set-Cookie"), ShouldContainSubstring, "grafana_sess")
|
|
||||||
})
|
|
||||||
|
|
||||||
middlewareScenario("Invalid api key", func(sc *scenarioContext) {
|
middlewareScenario("Invalid api key", func(sc *scenarioContext) {
|
||||||
sc.apiKey = "invalid_key_test"
|
sc.apiKey = "invalid_key_test"
|
||||||
sc.fakeReq("GET", "/").exec()
|
sc.fakeReq("GET", "/").exec()
|
||||||
@ -151,22 +146,17 @@ func TestMiddlewareContext(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
middlewareScenario("UserId in session", func(sc *scenarioContext) {
|
middlewareScenario("Auth token service", func(sc *scenarioContext) {
|
||||||
|
var wasCalled bool
|
||||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
sc.userAuthTokenService.initContextWithTokenProvider = func(ctx *m.ReqContext, orgId int64) bool {
|
||||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
wasCalled = true
|
||||||
}).exec()
|
return false
|
||||||
|
}
|
||||||
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
|
||||||
query.Result = &m.SignedInUser{OrgId: 2, UserId: 12}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
sc.fakeReq("GET", "/").exec()
|
sc.fakeReq("GET", "/").exec()
|
||||||
|
|
||||||
Convey("should init context with user info", func() {
|
Convey("should call middleware", func() {
|
||||||
So(sc.context.IsSignedIn, ShouldBeTrue)
|
So(wasCalled, ShouldBeTrue)
|
||||||
So(sc.context.UserId, ShouldEqual, 12)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -487,7 +477,8 @@ func middlewareScenario(desc string, fn scenarioFunc) {
|
|||||||
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
||||||
}))
|
}))
|
||||||
|
|
||||||
sc.m.Use(GetContextHandler(nil))
|
sc.userAuthTokenService = newFakeUserAuthTokenService()
|
||||||
|
sc.m.Use(GetContextHandler(sc.userAuthTokenService))
|
||||||
// mock out gc goroutine
|
// mock out gc goroutine
|
||||||
session.StartSessionGC = func() {}
|
session.StartSessionGC = func() {}
|
||||||
sc.m.Use(Sessioner(&ms.Options{}, 0))
|
sc.m.Use(Sessioner(&ms.Options{}, 0))
|
||||||
@ -508,15 +499,16 @@ func middlewareScenario(desc string, fn scenarioFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type scenarioContext struct {
|
type scenarioContext struct {
|
||||||
m *macaron.Macaron
|
m *macaron.Macaron
|
||||||
context *m.ReqContext
|
context *m.ReqContext
|
||||||
resp *httptest.ResponseRecorder
|
resp *httptest.ResponseRecorder
|
||||||
apiKey string
|
apiKey string
|
||||||
authHeader string
|
authHeader string
|
||||||
respJson map[string]interface{}
|
respJson map[string]interface{}
|
||||||
handlerFunc handlerFunc
|
handlerFunc handlerFunc
|
||||||
defaultHandler macaron.Handler
|
defaultHandler macaron.Handler
|
||||||
url string
|
url string
|
||||||
|
userAuthTokenService *fakeUserAuthTokenService
|
||||||
|
|
||||||
req *http.Request
|
req *http.Request
|
||||||
}
|
}
|
||||||
@ -585,3 +577,25 @@ func (sc *scenarioContext) exec() {
|
|||||||
|
|
||||||
type scenarioFunc func(c *scenarioContext)
|
type scenarioFunc func(c *scenarioContext)
|
||||||
type handlerFunc func(c *m.ReqContext)
|
type handlerFunc func(c *m.ReqContext)
|
||||||
|
|
||||||
|
type fakeUserAuthTokenService struct {
|
||||||
|
initContextWithTokenProvider func(ctx *m.ReqContext, orgID int64) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFakeUserAuthTokenService() *fakeUserAuthTokenService {
|
||||||
|
return &fakeUserAuthTokenService{
|
||||||
|
initContextWithTokenProvider: func(ctx *m.ReqContext, orgID int64) bool {
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeUserAuthTokenService) InitContextWithToken(ctx *m.ReqContext, orgID int64) bool {
|
||||||
|
return s.initContextWithTokenProvider(ctx, orgID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeUserAuthTokenService) UserAuthenticatedHook(user *m.User, c *m.ReqContext) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeUserAuthTokenService) UserSignedOutHook(c *m.ReqContext) {}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/session"
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,18 +14,15 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
|||||||
|
|
||||||
Convey("Can redirect to correct org", t, func() {
|
Convey("Can redirect to correct org", t, func() {
|
||||||
middlewareScenario("when setting a correct org for the user", func(sc *scenarioContext) {
|
middlewareScenario("when setting a correct org for the user", func(sc *scenarioContext) {
|
||||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
|
||||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
|
||||||
}).exec()
|
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error {
|
bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
sc.userAuthTokenService.initContextWithTokenProvider = func(ctx *m.ReqContext, orgId int64) bool {
|
||||||
query.Result = &m.SignedInUser{OrgId: 1, UserId: 12}
|
ctx.SignedInUser = &m.SignedInUser{OrgId: 1, UserId: 12}
|
||||||
return nil
|
ctx.IsSignedIn = true
|
||||||
})
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
sc.m.Get("/", sc.defaultHandler)
|
sc.m.Get("/", sc.defaultHandler)
|
||||||
sc.fakeReq("GET", "/?orgId=3").exec()
|
sc.fakeReq("GET", "/?orgId=3").exec()
|
||||||
@ -37,14 +33,16 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
middlewareScenario("when setting an invalid org for user", func(sc *scenarioContext) {
|
middlewareScenario("when setting an invalid org for user", func(sc *scenarioContext) {
|
||||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
|
||||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
|
||||||
}).exec()
|
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error {
|
bus.AddHandler("test", func(query *m.SetUsingOrgCommand) error {
|
||||||
return fmt.Errorf("")
|
return fmt.Errorf("")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
sc.userAuthTokenService.initContextWithTokenProvider = func(ctx *m.ReqContext, orgId int64) bool {
|
||||||
|
ctx.SignedInUser = &m.SignedInUser{OrgId: 1, UserId: 12}
|
||||||
|
ctx.IsSignedIn = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
||||||
query.Result = &m.SignedInUser{OrgId: 1, UserId: 12}
|
query.Result = &m.SignedInUser{OrgId: 1, UserId: 12}
|
||||||
return nil
|
return nil
|
||||||
|
@ -74,15 +74,12 @@ func TestMiddlewareQuota(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
middlewareScenario("with user logged in", func(sc *scenarioContext) {
|
middlewareScenario("with user logged in", func(sc *scenarioContext) {
|
||||||
// log us in, so we have a user_id and org_id in the context
|
sc.userAuthTokenService.initContextWithTokenProvider = func(ctx *m.ReqContext, orgId int64) bool {
|
||||||
sc.fakeReq("GET", "/").handler(func(c *m.ReqContext) {
|
ctx.SignedInUser = &m.SignedInUser{OrgId: 2, UserId: 12}
|
||||||
c.Session.Set(session.SESS_KEY_USERID, int64(12))
|
ctx.IsSignedIn = true
|
||||||
}).exec()
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetSignedInUserQuery) error {
|
|
||||||
query.Result = &m.SignedInUser{OrgId: 2, UserId: 12}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
bus.AddHandler("globalQuota", func(query *m.GetGlobalQuotaByTargetQuery) error {
|
bus.AddHandler("globalQuota", func(query *m.GetGlobalQuotaByTargetQuery) error {
|
||||||
query.Result = &m.GlobalQuotaDTO{
|
query.Result = &m.GlobalQuotaDTO{
|
||||||
Target: query.Target,
|
Target: query.Target,
|
||||||
|
@ -64,7 +64,8 @@ func recoveryScenario(desc string, url string, fn scenarioFunc) {
|
|||||||
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
Delims: macaron.Delims{Left: "[[", Right: "]]"},
|
||||||
}))
|
}))
|
||||||
|
|
||||||
sc.m.Use(GetContextHandler(nil))
|
sc.userAuthTokenService = newFakeUserAuthTokenService()
|
||||||
|
sc.m.Use(GetContextHandler(sc.userAuthTokenService))
|
||||||
// mock out gc goroutine
|
// mock out gc goroutine
|
||||||
session.StartSessionGC = func() {}
|
session.StartSessionGC = func() {}
|
||||||
sc.m.Use(Sessioner(&ms.Options{}, 0))
|
sc.m.Use(Sessioner(&ms.Options{}, 0))
|
||||||
|
Reference in New Issue
Block a user