Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Use dependency-injected grpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
IljaN committed Aug 26, 2020
1 parent f552072 commit 73c8b11
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
39 changes: 38 additions & 1 deletion pkg/proto/v0/accounts.pb.micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
context "context"
"errors"
"fmt"
"github.com/micro/go-micro/v2/client"
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/protobuf/types/known/timestamppb"
"log"
Expand Down Expand Up @@ -168,6 +169,8 @@ func init() {
log.Fatalf("Could not create new service")
}

hdlr.Client = mockClient{}

err = proto.RegisterAccountsServiceHandler(service.Server(), hdlr)
if err != nil {
log.Fatal("could not register the Accounts handler")
Expand Down Expand Up @@ -488,7 +491,7 @@ func TestUpdateAccount(t *testing.T) {
GidNumber: 1000,
// No email validation
// https://github.com/owncloud/ocis-accounts/issues/77
Mail: "1.2@3.c_@",
Mail: "1.2@3.c_@",
},
},
}
Expand Down Expand Up @@ -1169,3 +1172,37 @@ func TestAccountUpdateReadOnlyField(t *testing.T) {

cleanUp(t)
}

type mockClient struct{}

func (c mockClient) Init(option ...client.Option) error {
return nil
}

func (c mockClient) Options() client.Options {
return client.Options{}
}

func (c mockClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
return nil
}

func (c mockClient) NewRequest(service, endpoint string, req interface{}, reqOpts ...client.RequestOption) client.Request {
return nil
}

func (c mockClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
return nil
}

func (c mockClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
return nil, nil
}

func (c mockClient) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
return nil
}

func (c mockClient) String() string {
return "ClientMock"
}
3 changes: 1 addition & 2 deletions pkg/service/v0/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/blevesearch/bleve"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
mgrpc "github.com/micro/go-micro/v2/client/grpc"
merrors "github.com/micro/go-micro/v2/errors"
"github.com/owncloud/ocis-accounts/pkg/proto/v0"
"github.com/owncloud/ocis-accounts/pkg/provider"
Expand Down Expand Up @@ -324,7 +323,7 @@ func (s Service) CreateAccount(c context.Context, in *proto.CreateAccountRequest
}

// TODO: All users for now as create Account request does not have any role field
rs := settings.NewRoleService("com.owncloud.api.settings", mgrpc.NewClient())
rs := settings.NewRoleService("com.owncloud.api.settings", s.Client)
_, err = rs.AssignRoleToUser(c, &settings.AssignRoleToUserRequest{
AccountUuid: acc.Id,
RoleId: settings_svc.BundleUUIDRoleUser,
Expand Down
4 changes: 4 additions & 0 deletions pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"errors"
"fmt"
mclient "github.com/micro/go-micro/v2/client"
mgrpc "github.com/micro/go-micro/v2/client/grpc"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -287,6 +289,7 @@ func New(opts ...Option) (s *Service, err error) {
id: cfg.GRPC.Namespace + "." + cfg.Server.Name,
log: logger,
Config: cfg,
Client: mgrpc.NewClient(),
}

indexDir := filepath.Join(cfg.Server.AccountsDataPath, "index.bleve")
Expand Down Expand Up @@ -315,6 +318,7 @@ type Service struct {
log log.Logger
Config *config.Config
index bleve.Index
Client mclient.Client
}

func cleanupID(id string) (string, error) {
Expand Down

0 comments on commit 73c8b11

Please sign in to comment.