feat(http): construct org handler by OrgBackend

pull/11700/head
zhulongcheng 2019-01-16 11:34:09 +08:00 committed by Leo Di Donato
parent e29f925679
commit ef308ab9bd
5 changed files with 84 additions and 22 deletions

View File

@ -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)

View File

@ -29,7 +29,7 @@ func NewMockBucketBackend() *BucketBackend {
UserResourceMappingService: mock.NewUserResourceMappingService(),
LabelService: mock.NewLabelService(),
UserService: mock.NewUserService(),
OrganizationService: &mock.OrganizationService{},
OrganizationService: mock.NewOrganizationService(),
}
}

View File

@ -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)

View File

@ -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 {

View File

@ -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)