mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Began work on auth_proxy feature (#1932), and began work on testing http api, and auth middleware
This commit is contained in:
40
pkg/middleware/middleware_test.go
Normal file
40
pkg/middleware/middleware_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Unknwon/macaron"
|
||||
"github.com/macaron-contrib/session"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestMiddlewareContext(t *testing.T) {
|
||||
|
||||
Convey("Given grafana context", t, func() {
|
||||
m := macaron.New()
|
||||
m.Use(GetContextHandler())
|
||||
m.Use(Sessioner(&session.Options{}))
|
||||
|
||||
var context *Context
|
||||
|
||||
m.Get("/", func(c *Context) {
|
||||
context = c
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
m.ServeHTTP(resp, req)
|
||||
|
||||
Convey("Should be able to get grafana context in handlers", func() {
|
||||
So(context, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("should return 200", func() {
|
||||
So(resp.Code, ShouldEqual, 200)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -16,17 +16,43 @@ const (
|
||||
)
|
||||
|
||||
var sessionManager *session.Manager
|
||||
var sessionOptions session.Options
|
||||
var sessionOptions *session.Options
|
||||
|
||||
func startSessionGC() {
|
||||
sessionManager.GC()
|
||||
time.AfterFunc(time.Duration(sessionOptions.Gclifetime)*time.Second, startSessionGC)
|
||||
}
|
||||
|
||||
func Sessioner(options session.Options) macaron.Handler {
|
||||
func prepareOptions(opt *session.Options) *session.Options {
|
||||
if len(opt.Provider) == 0 {
|
||||
opt.Provider = "memory"
|
||||
}
|
||||
if len(opt.ProviderConfig) == 0 {
|
||||
opt.ProviderConfig = "data/sessions"
|
||||
}
|
||||
if len(opt.CookieName) == 0 {
|
||||
opt.CookieName = "grafana_sess"
|
||||
}
|
||||
if len(opt.CookiePath) == 0 {
|
||||
opt.CookiePath = "/"
|
||||
}
|
||||
if opt.Gclifetime == 0 {
|
||||
opt.Gclifetime = 3600
|
||||
}
|
||||
if opt.Maxlifetime == 0 {
|
||||
opt.Maxlifetime = opt.Gclifetime
|
||||
}
|
||||
if opt.IDLength == 0 {
|
||||
opt.IDLength = 16
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
func Sessioner(options *session.Options) macaron.Handler {
|
||||
var err error
|
||||
sessionOptions = options
|
||||
sessionManager, err = session.NewManager(options.Provider, options)
|
||||
sessionOptions = prepareOptions(options)
|
||||
sessionManager, err = session.NewManager(options.Provider, *options)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user