From ef308ab9bd49d0ffb09fab8fee39bf067f283348 Mon Sep 17 00:00:00 2001 From: zhulongcheng Date: Wed, 16 Jan 2019 11:34:09 +0800 Subject: [PATCH] feat(http): construct org handler by OrgBackend --- http/api_handler.go | 11 ++++------- http/bucket_test.go | 2 +- http/org_service.go | 38 +++++++++++++++++++++++++++++++++----- http/org_test.go | 35 +++++++++++++++++++++++++++-------- mock/org_service.go | 20 +++++++++++++++++++- 5 files changed, 84 insertions(+), 22 deletions(-) diff --git a/http/api_handler.go b/http/api_handler.go index 2e60bf4aad..6651111583 100644 --- a/http/api_handler.go +++ b/http/api_handler.go @@ -81,15 +81,12 @@ func NewAPIHandler(b *APIBackend) *APIHandler { b.UserResourceMappingService = authorizer.NewURMService(b.OrgLookupService, b.UserResourceMappingService) bucketBackend := NewBucketBackend(b) + bucketBackend.BucketService = authorizer.NewBucketService(b.BucketService) h.BucketHandler = NewBucketHandler(bucketBackend) - h.LabelHandler = NewLabelHandler() - h.LabelHandler.LabelService = b.LabelService - - h.OrgHandler = NewOrgHandler(b.UserResourceMappingService, b.LabelService, b.UserService) - h.OrgHandler.OrganizationService = authorizer.NewOrgService(b.OrganizationService) - h.OrgHandler.OrganizationOperationLogService = b.OrganizationOperationLogService - h.OrgHandler.SecretService = b.SecretService + orgBackend := NewOrgBackend(b) + orgBackend.OrganizationService = authorizer.NewOrgService(b.OrganizationService) + h.OrgHandler = NewOrgHandler(orgBackend) userBackend := NewUserBackend(b) userBackend.UserService = authorizer.NewUserService(b.UserService) diff --git a/http/bucket_test.go b/http/bucket_test.go index 5f343fd28d..fa6e310b07 100644 --- a/http/bucket_test.go +++ b/http/bucket_test.go @@ -29,7 +29,7 @@ func NewMockBucketBackend() *BucketBackend { UserResourceMappingService: mock.NewUserResourceMappingService(), LabelService: mock.NewLabelService(), UserService: mock.NewUserService(), - OrganizationService: &mock.OrganizationService{}, + OrganizationService: mock.NewOrganizationService(), } } diff --git a/http/org_service.go b/http/org_service.go index 6a5a6509c6..5b08fa20a4 100644 --- a/http/org_service.go +++ b/http/org_service.go @@ -12,6 +12,32 @@ import ( "path" ) +// OrgBackend is all services and associated parameters required to construct +// the OrgHandler. +type OrgBackend struct { + Logger *zap.Logger + + OrganizationService platform.OrganizationService + OrganizationOperationLogService platform.OrganizationOperationLogService + UserResourceMappingService platform.UserResourceMappingService + SecretService platform.SecretService + LabelService platform.LabelService + UserService platform.UserService +} + +func NewOrgBackend(b *APIBackend) *OrgBackend { + return &OrgBackend{ + Logger: b.Logger.With(zap.String("handler", "org")), + + OrganizationService: b.OrganizationService, + OrganizationOperationLogService: b.OrganizationOperationLogService, + UserResourceMappingService: b.UserResourceMappingService, + SecretService: b.SecretService, + LabelService: b.LabelService, + UserService: b.UserService, + } +} + // OrgHandler represents an HTTP API handler for orgs. type OrgHandler struct { *httprouter.Router @@ -42,15 +68,17 @@ const ( ) // NewOrgHandler returns a new instance of OrgHandler. -func NewOrgHandler(mappingService platform.UserResourceMappingService, - labelService platform.LabelService, userService platform.UserService) *OrgHandler { +func NewOrgHandler(b *OrgBackend) *OrgHandler { h := &OrgHandler{ Router: NewRouter(), Logger: zap.NewNop(), - UserResourceMappingService: mappingService, - LabelService: labelService, - UserService: userService, + OrganizationService: b.OrganizationService, + OrganizationOperationLogService: b.OrganizationOperationLogService, + UserResourceMappingService: b.UserResourceMappingService, + SecretService: b.SecretService, + LabelService: b.LabelService, + UserService: b.UserService, } h.HandlerFunc("POST", organizationsPath, h.handlePostOrg) diff --git a/http/org_test.go b/http/org_test.go index e5c02f5a6b..bf9e1aa284 100644 --- a/http/org_test.go +++ b/http/org_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "go.uber.org/zap" "io/ioutil" "net/http" "net/http/httptest" @@ -16,6 +17,20 @@ import ( platformtesting "github.com/influxdata/influxdb/testing" ) +// NewMockOrgBackend returns a OrgBackend with mock services. +func NewMockOrgBackend() *OrgBackend { + return &OrgBackend{ + Logger: zap.NewNop().With(zap.String("handler", "org")), + + OrganizationService: mock.NewOrganizationService(), + OrganizationOperationLogService: mock.NewOrganizationOperationLogService(), + UserResourceMappingService: mock.NewUserResourceMappingService(), + SecretService: mock.NewSecretService(), + LabelService: mock.NewLabelService(), + UserService: mock.NewUserService(), + } +} + func initOrganizationService(f platformtesting.OrganizationFields, t *testing.T) (platform.OrganizationService, string, func()) { t.Helper() svc := inmem.NewService() @@ -28,8 +43,9 @@ func initOrganizationService(f platformtesting.OrganizationFields, t *testing.T) } } - handler := NewOrgHandler(mock.NewUserResourceMappingService(), mock.NewLabelService(), mock.NewUserService()) - handler.OrganizationService = svc + orgBackend := NewMockOrgBackend() + orgBackend.OrganizationService = svc + handler := NewOrgHandler(orgBackend) server := httptest.NewServer(handler) client := OrganizationService{ Addr: server.URL, @@ -122,8 +138,9 @@ func TestSecretService_handleGetSecrets(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - h := NewOrgHandler(mock.NewUserResourceMappingService(), mock.NewLabelService(), mock.NewUserService()) - h.SecretService = tt.fields.SecretService + orgBackend := NewMockOrgBackend() + orgBackend.SecretService = tt.fields.SecretService + h := NewOrgHandler(orgBackend) u := fmt.Sprintf("http://any.url/api/v2/orgs/%s/secrets", tt.args.orgID) r := httptest.NewRequest("GET", u, nil) @@ -192,8 +209,9 @@ func TestSecretService_handlePatchSecrets(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - h := NewOrgHandler(mock.NewUserResourceMappingService(), mock.NewLabelService(), mock.NewUserService()) - h.SecretService = tt.fields.SecretService + orgBackend := NewMockOrgBackend() + orgBackend.SecretService = tt.fields.SecretService + h := NewOrgHandler(orgBackend) b, err := json.Marshal(tt.args.secrets) if err != nil { @@ -268,8 +286,9 @@ func TestSecretService_handleDeleteSecrets(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - h := NewOrgHandler(mock.NewUserResourceMappingService(), mock.NewLabelService(), mock.NewUserService()) - h.SecretService = tt.fields.SecretService + orgBackend := NewMockOrgBackend() + orgBackend.SecretService = tt.fields.SecretService + h := NewOrgHandler(orgBackend) b, err := json.Marshal(tt.args.secrets) if err != nil { diff --git a/mock/org_service.go b/mock/org_service.go index 48213da49b..3d568ea42a 100644 --- a/mock/org_service.go +++ b/mock/org_service.go @@ -2,7 +2,6 @@ package mock import ( "context" - platform "github.com/influxdata/influxdb" ) @@ -18,6 +17,25 @@ type OrganizationService struct { DeleteOrganizationF func(ctx context.Context, id platform.ID) error } +// NewOrganizationService returns a mock OrganizationService where its methods will return +// zero values. +func NewOrganizationService() *OrganizationService { + return &OrganizationService{ + FindOrganizationByIDF: func(ctx context.Context, id platform.ID) (*platform.Organization, error) { return nil, nil }, + FindOrganizationF: func(ctx context.Context, filter platform.OrganizationFilter) (*platform.Organization, error) { + return nil, nil + }, + FindOrganizationsF: func(ctx context.Context, filter platform.OrganizationFilter, opt ...platform.FindOptions) ([]*platform.Organization, int, error) { + return nil, 0, nil + }, + CreateOrganizationF: func(ctx context.Context, b *platform.Organization) error { return nil }, + UpdateOrganizationF: func(ctx context.Context, id platform.ID, upd platform.OrganizationUpdate) (*platform.Organization, error) { + return nil, nil + }, + DeleteOrganizationF: func(ctx context.Context, id platform.ID) error { return nil }, + } +} + //FindOrganizationByID calls FindOrganizationByIDF. func (s *OrganizationService) FindOrganizationByID(ctx context.Context, id platform.ID) (*platform.Organization, error) { return s.FindOrganizationByIDF(ctx, id)