initialize session handler by SessionBackend

pull/10616/head
zhulongcheng 2018-12-29 11:41:06 +08:00
parent 3429e8d0c6
commit 83529543ac
3 changed files with 40 additions and 8 deletions

View File

@ -70,10 +70,8 @@ type APIBackend struct {
// NewAPIHandler constructs all api handlers beneath it and returns an APIHandler // NewAPIHandler constructs all api handlers beneath it and returns an APIHandler
func NewAPIHandler(b *APIBackend) *APIHandler { func NewAPIHandler(b *APIBackend) *APIHandler {
h := &APIHandler{} h := &APIHandler{}
h.SessionHandler = NewSessionHandler() sessionBackend := NewSessionBackend(b)
h.SessionHandler.BasicAuthService = b.BasicAuthService h.SessionHandler = NewSessionHandler(sessionBackend)
h.SessionHandler.SessionService = b.SessionService
h.SessionHandler.Logger = b.Logger.With(zap.String("handler", "basicAuth"))
h.BucketHandler = NewBucketHandler(b.UserResourceMappingService, b.LabelService, b.UserService) h.BucketHandler = NewBucketHandler(b.UserResourceMappingService, b.LabelService, b.UserService)
h.BucketHandler.BucketService = b.BucketService h.BucketHandler.BucketService = b.BucketService

View File

@ -9,6 +9,24 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// SessionBackend is all services and associated parameters required to construct
// the SessionHandler.
type SessionBackend struct {
Logger *zap.Logger
BasicAuthService platform.BasicAuthService
SessionService platform.SessionService
}
func NewSessionBackend(b *APIBackend) *SessionBackend {
return &SessionBackend{
Logger: b.Logger.With(zap.String("handler", "session")),
BasicAuthService: b.BasicAuthService,
SessionService: b.SessionService,
}
}
// SessionHandler represents an HTTP API handler for authorizations. // SessionHandler represents an HTTP API handler for authorizations.
type SessionHandler struct { type SessionHandler struct {
*httprouter.Router *httprouter.Router
@ -19,9 +37,13 @@ type SessionHandler struct {
} }
// NewSessionHandler returns a new instance of SessionHandler. // NewSessionHandler returns a new instance of SessionHandler.
func NewSessionHandler() *SessionHandler { func NewSessionHandler(b *SessionBackend) *SessionHandler {
h := &SessionHandler{ h := &SessionHandler{
Router: NewRouter(), Router: NewRouter(),
Logger: b.Logger,
BasicAuthService: b.BasicAuthService,
SessionService: b.SessionService,
} }
h.HandlerFunc("POST", "/api/v2/signin", h.handleSignin) h.HandlerFunc("POST", "/api/v2/signin", h.handleSignin)

View File

@ -2,6 +2,7 @@ package http_test
import ( import (
"context" "context"
"go.uber.org/zap"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -12,6 +13,16 @@ import (
"github.com/influxdata/platform/mock" "github.com/influxdata/platform/mock"
) )
// NewMockSessionBackend returns a SessionBackend with mock services.
func NewMockSessionBackend() *platformhttp.SessionBackend {
return &platformhttp.SessionBackend{
Logger: zap.NewNop().With(zap.String("handler", "session")),
SessionService: mock.NewSessionService(),
BasicAuthService: mock.NewBasicAuthService("", ""),
}
}
func TestBasicAuthHandler_handleSignin(t *testing.T) { func TestBasicAuthHandler_handleSignin(t *testing.T) {
type fields struct { type fields struct {
BasicAuthService platform.BasicAuthService BasicAuthService platform.BasicAuthService
@ -65,9 +76,10 @@ func TestBasicAuthHandler_handleSignin(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := platformhttp.NewSessionHandler() b := NewMockSessionBackend()
h.BasicAuthService = tt.fields.BasicAuthService b.BasicAuthService = tt.fields.BasicAuthService
h.SessionService = tt.fields.SessionService b.SessionService = tt.fields.SessionService
h := platformhttp.NewSessionHandler(b)
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "http://localhost:9999/api/v2/signin", nil) r := httptest.NewRequest("POST", "http://localhost:9999/api/v2/signin", nil)