Skip to content

Commit

Permalink
Add validation service handle to identity service
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhuaaa committed Apr 23, 2024
1 parent 80bce47 commit 9cb2957
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *Server) startGRPC() error {
}
mlsv1pb.RegisterMlsApiServer(grpcServer, s.mlsv1)

s.identityv1, err = identityv1.NewService(s.Log, s.Config.MLSStore)
s.identityv1, err = identityv1.NewService(s.Log, s.Config.MLSStore, s.Config.MLSValidator)
if err != nil {
return errors.Wrap(err, "creating identity service")
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/identity/api/v1/identity_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

mlsstore "github.com/xmtp/xmtp-node-go/pkg/mls/store"
"github.com/xmtp/xmtp-node-go/pkg/mlsvalidate"
api "github.com/xmtp/xmtp-node-go/pkg/proto/identity/api/v1"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
Expand All @@ -13,17 +14,19 @@ import (
type Service struct {
api.UnimplementedIdentityApiServer

log *zap.Logger
store mlsstore.MlsStore
log *zap.Logger
store mlsstore.MlsStore
validationService mlsvalidate.MLSValidationService

ctx context.Context
ctxCancel func()
}

func NewService(log *zap.Logger, store mlsstore.MlsStore) (s *Service, err error) {
func NewService(log *zap.Logger, store mlsstore.MlsStore, validationService mlsvalidate.MLSValidationService) (s *Service, err error) {
s = &Service{
log: log.Named("identity"),
store: store,
log: log.Named("identity"),
store: store,
validationService: validationService,
}
s.ctx, s.ctxCancel = context.WithCancel(context.Background())

Expand Down
46 changes: 45 additions & 1 deletion pkg/identity/api/v1/identity_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,61 @@ package api

import (
"context"
"fmt"
"testing"
"time"

"github.com/nats-io/nats-server/v2/server"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/uptrace/bun"
mlsstore "github.com/xmtp/xmtp-node-go/pkg/mls/store"
"github.com/xmtp/xmtp-node-go/pkg/mlsvalidate"
identity "github.com/xmtp/xmtp-node-go/pkg/proto/identity/api/v1"
associations "github.com/xmtp/xmtp-node-go/pkg/proto/identity/associations"
mlsv1 "github.com/xmtp/xmtp-node-go/pkg/proto/mls/api/v1"
test "github.com/xmtp/xmtp-node-go/pkg/testing"
)

type mockedMLSValidationService struct {
mock.Mock
}

func (m *mockedMLSValidationService) GetAssociationState(ctx context.Context, oldUpdates []*associations.IdentityUpdate, newUpdates []*associations.IdentityUpdate) (*mlsvalidate.AssociationStateResult, error) {
return nil, nil
}

func (m *mockedMLSValidationService) ValidateKeyPackages(ctx context.Context, keyPackages [][]byte) ([]mlsvalidate.IdentityValidationResult, error) {
return nil, nil
}

func (m *mockedMLSValidationService) ValidateGroupMessages(ctx context.Context, groupMessages []*mlsv1.GroupMessageInput) ([]mlsvalidate.GroupMessageValidationResult, error) {
return nil, nil
}

func newMockedValidationService() *mockedMLSValidationService {
return new(mockedMLSValidationService)
}

func (m *mockedMLSValidationService) mockValidateKeyPackages(installationId []byte, accountAddress string) *mock.Call {

Check failure on line 41 in pkg/identity/api/v1/identity_service_test.go

View workflow job for this annotation

GitHub Actions / Lint

func `(*mockedMLSValidationService).mockValidateKeyPackages` is unused (unused)
return m.On("ValidateKeyPackages", mock.Anything, mock.Anything).Return([]mlsvalidate.IdentityValidationResult{
{
InstallationKey: installationId,
AccountAddress: accountAddress,
CredentialIdentity: []byte("test"),
Expiration: 0,
},
}, nil)
}

func (m *mockedMLSValidationService) mockValidateGroupMessages(groupId []byte) *mock.Call {

Check failure on line 52 in pkg/identity/api/v1/identity_service_test.go

View workflow job for this annotation

GitHub Actions / Lint

func `(*mockedMLSValidationService).mockValidateGroupMessages` is unused (unused)
return m.On("ValidateGroupMessages", mock.Anything, mock.Anything).Return([]mlsvalidate.GroupMessageValidationResult{
{
GroupId: fmt.Sprintf("%x", groupId),
},
}, nil)
}

func newTestService(t *testing.T, ctx context.Context) (*Service, *bun.DB, func()) {
log := test.NewLog(t)
db, _, mlsDbCleanup := test.NewMLSDB(t)
Expand All @@ -22,6 +65,7 @@ func newTestService(t *testing.T, ctx context.Context) (*Service, *bun.DB, func(
DB: db,
})
require.NoError(t, err)
mlsValidationService := newMockedValidationService()
natsServer, err := server.NewServer(&server.Options{
Port: server.RANDOM_PORT,
})
Expand All @@ -31,7 +75,7 @@ func newTestService(t *testing.T, ctx context.Context) (*Service, *bun.DB, func(
t.Fail()
}

svc, err := NewService(log, store)
svc, err := NewService(log, store, mlsValidationService)
require.NoError(t, err)

return svc, db, func() {
Expand Down
5 changes: 5 additions & 0 deletions pkg/mls/api/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
wakupb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
mlsstore "github.com/xmtp/xmtp-node-go/pkg/mls/store"
"github.com/xmtp/xmtp-node-go/pkg/mlsvalidate"
"github.com/xmtp/xmtp-node-go/pkg/proto/identity/associations"
mlsv1 "github.com/xmtp/xmtp-node-go/pkg/proto/mls/api/v1"
test "github.com/xmtp/xmtp-node-go/pkg/testing"
"github.com/xmtp/xmtp-node-go/pkg/topic"
Expand All @@ -26,6 +27,10 @@ type mockedMLSValidationService struct {
mock.Mock
}

func (m *mockedMLSValidationService) GetAssociationState(ctx context.Context, oldUpdates []*associations.IdentityUpdate, newUpdates []*associations.IdentityUpdate) (*mlsvalidate.AssociationStateResult, error) {
return nil, nil
}

func (m *mockedMLSValidationService) ValidateKeyPackages(ctx context.Context, keyPackages [][]byte) ([]mlsvalidate.IdentityValidationResult, error) {
args := m.Called(ctx, keyPackages)

Expand Down
4 changes: 4 additions & 0 deletions pkg/mlsvalidate/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type MockedGRPCService struct {
mock.Mock
}

func (m *MockedGRPCService) GetAssociationState(ctx context.Context, in *svc.GetAssociationStateRequest, opts ...grpc.CallOption) (*svc.GetAssociationStateResponse, error) {
return nil, nil
}

func (m *MockedGRPCService) ValidateKeyPackages(ctx context.Context, req *svc.ValidateKeyPackagesRequest, opts ...grpc.CallOption) (*svc.ValidateKeyPackagesResponse, error) {
args := m.Called(ctx, req)

Expand Down

0 comments on commit 9cb2957

Please sign in to comment.