diff --git a/http/api_handler.go b/http/api_handler.go index 14fb0869f9..dd8489235d 100644 --- a/http/api_handler.go +++ b/http/api_handler.go @@ -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 diff --git a/http/session_handler.go b/http/session_handler.go index 6a644865a0..1368a6276b 100644 --- a/http/session_handler.go +++ b/http/session_handler.go @@ -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) diff --git a/http/session_test.go b/http/session_test.go index 98d476f03c..fc9a3d3793 100644 --- a/http/session_test.go +++ b/http/session_test.go @@ -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)