initialize session handler by SessionBackend
parent
3429e8d0c6
commit
83529543ac
|
@ -70,10 +70,8 @@ type APIBackend struct {
|
|||
// NewAPIHandler constructs all api handlers beneath it and returns an APIHandler
|
||||
func NewAPIHandler(b *APIBackend) *APIHandler {
|
||||
h := &APIHandler{}
|
||||
h.SessionHandler = NewSessionHandler()
|
||||
h.SessionHandler.BasicAuthService = b.BasicAuthService
|
||||
h.SessionHandler.SessionService = b.SessionService
|
||||
h.SessionHandler.Logger = b.Logger.With(zap.String("handler", "basicAuth"))
|
||||
sessionBackend := NewSessionBackend(b)
|
||||
h.SessionHandler = NewSessionHandler(sessionBackend)
|
||||
|
||||
h.BucketHandler = NewBucketHandler(b.UserResourceMappingService, b.LabelService, b.UserService)
|
||||
h.BucketHandler.BucketService = b.BucketService
|
||||
|
|
|
@ -9,6 +9,24 @@ import (
|
|||
"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.
|
||||
type SessionHandler struct {
|
||||
*httprouter.Router
|
||||
|
@ -19,9 +37,13 @@ type SessionHandler struct {
|
|||
}
|
||||
|
||||
// NewSessionHandler returns a new instance of SessionHandler.
|
||||
func NewSessionHandler() *SessionHandler {
|
||||
func NewSessionHandler(b *SessionBackend) *SessionHandler {
|
||||
h := &SessionHandler{
|
||||
Router: NewRouter(),
|
||||
Logger: b.Logger,
|
||||
|
||||
BasicAuthService: b.BasicAuthService,
|
||||
SessionService: b.SessionService,
|
||||
}
|
||||
|
||||
h.HandlerFunc("POST", "/api/v2/signin", h.handleSignin)
|
||||
|
|
|
@ -2,6 +2,7 @@ package http_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
@ -12,6 +13,16 @@ import (
|
|||
"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) {
|
||||
type fields struct {
|
||||
BasicAuthService platform.BasicAuthService
|
||||
|
@ -65,9 +76,10 @@ func TestBasicAuthHandler_handleSignin(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := platformhttp.NewSessionHandler()
|
||||
h.BasicAuthService = tt.fields.BasicAuthService
|
||||
h.SessionService = tt.fields.SessionService
|
||||
b := NewMockSessionBackend()
|
||||
b.BasicAuthService = tt.fields.BasicAuthService
|
||||
b.SessionService = tt.fields.SessionService
|
||||
h := platformhttp.NewSessionHandler(b)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "http://localhost:9999/api/v2/signin", nil)
|
||||
|
|
Loading…
Reference in New Issue