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

Commit

Permalink
Merge pull request #964 from soluchok/issue-963
Browse files Browse the repository at this point in the history
refactor: Introduce - send message logic
  • Loading branch information
troyronda authored Dec 14, 2019
2 parents 9f7e2c3 + 1b6bf0b commit 92ac19f
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 517 deletions.
2 changes: 1 addition & 1 deletion pkg/client/didexchange/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (c *Client) HandleInvitation(invitation *Invitation) (string, error) {
return "", fmt.Errorf("failed to create DIDCommMsg: %w", err)
}

connectionID, err := c.didexchangeSvc.HandleInbound(msg)
connectionID, err := c.didexchangeSvc.HandleInbound(msg, "", "")
if err != nil {
return "", fmt.Errorf("failed from didexchange service handle: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/didexchange/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func TestServiceEvents(t *testing.T) {

msg, err := service.NewDIDCommMsg(request)
require.NoError(t, err)
_, err = didExSvc.HandleInbound(msg)
_, err = didExSvc.HandleInbound(msg, "", "")
require.NoError(t, err)

select {
Expand Down Expand Up @@ -679,7 +679,7 @@ func TestAcceptExchangeRequest(t *testing.T) {

msg, err := service.NewDIDCommMsg(request)
require.NoError(t, err)
_, err = didExSvc.HandleInbound(msg)
_, err = didExSvc.HandleInbound(msg, "", "")
require.NoError(t, err)

select {
Expand Down Expand Up @@ -760,7 +760,7 @@ func TestAcceptInvitation(t *testing.T) {

msg, svcErr := service.NewDIDCommMsg(invitation)
require.NoError(t, svcErr)
_, err = didExSvc.HandleInbound(msg)
_, err = didExSvc.HandleInbound(msg, "", "")
require.NoError(t, err)

select {
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/introduce/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func (c *Client) SendProposalWithInvitation(inv *didexchange.Invitation, recipie

// SendRequest sends a request
// sending a request means that introducee is willing to share its invitation
func (c *Client) SendRequest(dest *service.Destination) error {
func (c *Client) SendRequest(myDID, theirDID string) error {
return c.handleOutbound(&introduce.Request{
Type: introduce.RequestMsgType,
ID: c.newUUID(),
}, InvitationEnvelope{
Recps: []*introduce.Recipient{{Destination: dest}},
Recps: []*introduce.Recipient{{MyDID: myDID, TheirDID: theirDID}},
})
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *Client) handleOutbound(msg interface{}, o InvitationEnvelope) error {
return err
}

return c.service.HandleOutbound(didMsg, o.Recps[0].Destination)
return c.service.HandleOutbound(didMsg, o.Recps[0].MyDID, o.Recps[0].MyDID)
}

// InvitationEnvelope keeps the information needed for sending a proposal
Expand Down
24 changes: 12 additions & 12 deletions pkg/client/introduce/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func TestClient_SendProposal(t *testing.T) {
require.NotNil(t, svc)

DIDComm := serviceMocks.NewMockDIDComm(ctrl)
DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any()).Return(nil)
DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)

opts := InvitationEnvelope{
Recps: []*introduce.Recipient{
{Destination: &service.Destination{ServiceEndpoint: "service/endpoint1"}},
{Destination: &service.Destination{ServiceEndpoint: "service/endpoint2"}},
{MyDID: "My_DID1", TheirDID: "THEIR_DID1"},
{MyDID: "My_DID2", TheirDID: "THEIR_DID2"},
},
}
store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil)
Expand Down Expand Up @@ -148,14 +148,14 @@ func TestClient_SendProposalWithInvitation(t *testing.T) {
require.NotNil(t, svc)

DIDComm := serviceMocks.NewMockDIDComm(ctrl)
DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any()).Return(nil)
DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)

opts := InvitationEnvelope{
Inv: &didexchange.Invitation{
ID: UUID,
},
Recps: []*introduce.Recipient{
{Destination: &service.Destination{ServiceEndpoint: "service/endpoint"}},
{MyDID: "My_DID", TheirDID: "THEIR_DID"},
},
}
store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil)
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestClient_HandleRequest(t *testing.T) {
opts := InvitationEnvelope{
Recps: []*introduce.Recipient{
{To: &introduce.To{Name: "Carol"}},
{Destination: &service.Destination{ServiceEndpoint: "service/endpoint"}},
{MyDID: "My_DID2", TheirDID: "THEIR_DID2"},
},
}
store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil)
Expand Down Expand Up @@ -294,8 +294,8 @@ func TestClient_InvitationEnvelope(t *testing.T) {
ID: UUID,
},
Recps: []*introduce.Recipient{
{To: &introduce.To{Name: "Carol"}, Destination: &service.Destination{ServiceEndpoint: "service/endpoint1"}},
{Destination: &service.Destination{ServiceEndpoint: "service/endpoint1"}},
{To: &introduce.To{Name: "Carol"}, MyDID: "My_DID1", TheirDID: "THEIR_DID1"},
{MyDID: "My_DID2", TheirDID: "THEIR_DID2"},
},
}
store.EXPECT().Get(invitationEnvelopePrefix+UUID).Return(toBytes(t, opts), nil)
Expand Down Expand Up @@ -355,11 +355,11 @@ func TestClient_SendRequest(t *testing.T) {
require.NotNil(t, svc)

DIDComm := serviceMocks.NewMockDIDComm(ctrl)
DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any()).Return(nil)
DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)

opts := InvitationEnvelope{
Recps: []*introduce.Recipient{
{Destination: &service.Destination{ServiceEndpoint: "service/endpoint"}},
{MyDID: "My_DID", TheirDID: "THEIR_DID"},
},
}
store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil)
Expand All @@ -373,10 +373,10 @@ func TestClient_SendRequest(t *testing.T) {
require.NoError(t, err)

client.newUUID = func() string { return UUID }
require.NoError(t, client.SendRequest(opts.Recps[0].Destination))
require.NoError(t, client.SendRequest(opts.Recps[0].MyDID, opts.Recps[0].TheirDID))

// with error
require.EqualError(t, client.SendRequest(opts.Recps[0].Destination), "test error")
require.EqualError(t, client.SendRequest(opts.Recps[0].MyDID, opts.Recps[0].TheirDID), "test error")
}

func toBytes(t *testing.T, v interface{}) []byte {
Expand Down
4 changes: 2 additions & 2 deletions pkg/didcomm/common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
// Handler provides protocol service handle api.
type Handler interface {
// HandleInbound handles inbound messages.
HandleInbound(msg *DIDCommMsg) (string, error)
HandleInbound(msg *DIDCommMsg, myDID string, theirDID string) (string, error)
// HandleOutbound handles outbound messages.
HandleOutbound(msg *DIDCommMsg, dest *Destination) error
HandleOutbound(msg *DIDCommMsg, myDID, theirDID string) error
}

// DIDComm defines service APIs.
Expand Down
1 change: 1 addition & 0 deletions pkg/didcomm/dispatcher/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ type Service interface {
// Outbound interface
type Outbound interface {
Send(interface{}, string, *service.Destination) error
SendToDID(msg interface{}, myDID, theirDID string) error
}
5 changes: 5 additions & 0 deletions pkg/didcomm/dispatcher/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func NewOutbound(prov provider) *OutboundDispatcher {
}
}

// SendToDID msg
func (o *OutboundDispatcher) SendToDID(msg interface{}, myDID, theirDID string) error {
return nil
}

// Send msg
func (o *OutboundDispatcher) Send(msg interface{}, senderVerKey string, des *service.Destination) error {
for _, v := range o.outboundTransports {
Expand Down
4 changes: 2 additions & 2 deletions pkg/didcomm/protocol/didexchange/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func New(prov provider) (*Service, error) {
}

// HandleInbound handles inbound didexchange messages.
func (s *Service) HandleInbound(msg *service.DIDCommMsg) (string, error) {
func (s *Service) HandleInbound(msg *service.DIDCommMsg, myDID, theirDID string) (string, error) {
logger.Debugf("receive inbound message : %s", msg.Payload)

// fetch the thread id
Expand Down Expand Up @@ -181,7 +181,7 @@ func (s *Service) Accept(msgType string) bool {
}

// HandleOutbound handles outbound didexchange messages.
func (s *Service) HandleOutbound(msg *service.DIDCommMsg, destination *service.Destination) error {
func (s *Service) HandleOutbound(msg *service.DIDCommMsg, myDID, theirDID string) error {
return errors.New("not implemented")
}

Expand Down
46 changes: 25 additions & 21 deletions pkg/didcomm/protocol/didexchange/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestService_Handle_Inviter(t *testing.T) {
require.NoError(t, err)
msg, err := service.NewDIDCommMsg(payloadBytes)
require.NoError(t, err)
_, err = s.HandleInbound(msg)
_, err = s.HandleInbound(msg, "", "")
require.NoError(t, err)

select {
Expand All @@ -150,7 +150,7 @@ func TestService_Handle_Inviter(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(payloadBytes)
require.NoError(t, err)

_, err = s.HandleInbound(didMsg)
_, err = s.HandleInbound(didMsg, "", "")
require.NoError(t, err)

select {
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestService_Handle_Invitee(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(payloadBytes)
require.NoError(t, err)

_, err = s.HandleInbound(didMsg)
_, err = s.HandleInbound(didMsg, "", "")
require.NoError(t, err)

var connID string
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestService_Handle_Invitee(t *testing.T) {
didMsg, err = service.NewDIDCommMsg(payloadBytes)
require.NoError(t, err)

_, err = s.HandleInbound(didMsg)
_, err = s.HandleInbound(didMsg, "", "")
require.NoError(t, err)

// Alice automatically sends an ACK to Bob
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestService_Handle_EdgeCases(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(response)
require.NoError(t, err)

_, err = s.HandleInbound(didMsg)
_, err = s.HandleInbound(didMsg, "", "")
require.Error(t, err)
require.Contains(t, err.Error(), "handle inbound - next state : invalid state transition: "+
"null -> responded")
Expand All @@ -366,7 +366,7 @@ func TestService_Handle_EdgeCases(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(requestBytes)
require.NoError(t, err)

_, err = svc.HandleInbound(didMsg)
_, err = svc.HandleInbound(didMsg, "", "")
require.Error(t, err)
require.Equal(t, err.Error(), "threadID not found")
})
Expand All @@ -381,7 +381,7 @@ func TestService_Handle_EdgeCases(t *testing.T) {
transientStore := &mockstorage.MockStore{Store: make(map[string][]byte), ErrPut: errors.New("db error")}
svc.connectionStore = NewConnectionRecorder(transientStore, nil)

_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), ""))
_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), ""), "", "")
require.Error(t, err)
require.Contains(t, err.Error(), "save connection record")
})
Expand Down Expand Up @@ -419,7 +419,7 @@ func TestService_Handle_EdgeCases(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(requestBytes)
require.NoError(t, err)

_, err = svc.HandleInbound(didMsg)
_, err = svc.HandleInbound(didMsg, "", "")
require.NoError(t, err)
})
}
Expand Down Expand Up @@ -628,7 +628,7 @@ func TestEventsSuccess(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(invite)
require.NoError(t, err)

_, err = svc.HandleInbound(didMsg)
_, err = svc.HandleInbound(didMsg, "", "")
require.NoError(t, err)

select {
Expand Down Expand Up @@ -665,7 +665,7 @@ func TestContinueWithPublicDID(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(invite)
require.NoError(t, err)

_, err = svc.HandleInbound(didMsg)
_, err = svc.HandleInbound(didMsg, "", "")
require.NoError(t, err)
}

Expand Down Expand Up @@ -722,7 +722,7 @@ func TestEventsUserError(t *testing.T) {
err = svc.connectionStore.saveNewConnectionRecord(connRec)
require.NoError(t, err)

_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, id, ""))
_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, id, ""), "", "")
require.NoError(t, err)

select {
Expand All @@ -749,7 +749,8 @@ func TestEventStoreError(t *testing.T) {
}
}()

_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), ""))
_, err = svc.HandleInbound(
generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), ""), "", "")
require.NoError(t, err)
}

Expand Down Expand Up @@ -804,7 +805,8 @@ func TestServiceErrors(t *testing.T) {
return nil, errors.New("error")
}}
svc.connectionStore = NewConnectionRecorder(mockStore, nil)
_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), ""))
payload := generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), "")
_, err = svc.HandleInbound(payload, "", "")
require.Error(t, err)
require.Contains(t, err.Error(), "cannot fetch state from store")

Expand All @@ -815,7 +817,7 @@ func TestServiceErrors(t *testing.T) {

svc.connectionStore = NewConnectionRecorder(transientStore, nil)

_, err = svc.HandleInbound(msg)
_, err = svc.HandleInbound(msg, "", "")
require.Error(t, err)
require.Contains(t, err.Error(), "unrecognized msgType: invalid")

Expand All @@ -838,7 +840,7 @@ func TestHandleOutbound(t *testing.T) {
svc, err := New(&protocol.MockProvider{})
require.NoError(t, err)

err = svc.HandleOutbound(&service.DIDCommMsg{}, &service.Destination{})
err = svc.HandleOutbound(&service.DIDCommMsg{}, "", "")
require.Error(t, err)
require.Contains(t, err.Error(), "not implemented")
}
Expand Down Expand Up @@ -976,8 +978,9 @@ func TestAcceptExchangeRequest(t *testing.T) {
}
}()

_, err = svc.HandleInbound(generateRequestMsgPayload(t,
&protocol.MockProvider{StoreProvider: mockstorage.NewMockStoreProvider()}, randomString(), invitation.ID))
_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{
StoreProvider: mockstorage.NewMockStoreProvider(),
}, randomString(), invitation.ID), "", "")
require.NoError(t, err)

select {
Expand Down Expand Up @@ -1036,8 +1039,9 @@ func TestAcceptExchangeRequestWithPublicDID(t *testing.T) {
}
}()

_, err = svc.HandleInbound(generateRequestMsgPayload(t,
&protocol.MockProvider{StoreProvider: mockstorage.NewMockStoreProvider()}, randomString(), invitation.ID))
_, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{
StoreProvider: mockstorage.NewMockStoreProvider(),
}, randomString(), invitation.ID), "", "")
require.NoError(t, err)

select {
Expand Down Expand Up @@ -1098,7 +1102,7 @@ func TestAcceptInvitation(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(invitationBytes)
require.NoError(t, err)

_, err = svc.HandleInbound(didMsg)
_, err = svc.HandleInbound(didMsg, "", "")
require.NoError(t, err)

select {
Expand Down Expand Up @@ -1213,7 +1217,7 @@ func TestAcceptInvitationWithPublicDID(t *testing.T) {
didMsg, err := service.NewDIDCommMsg(invitationBytes)
require.NoError(t, err)

_, err = svc.HandleInbound(didMsg)
_, err = svc.HandleInbound(didMsg, "", "")
require.NoError(t, err)

select {
Expand Down
Loading

0 comments on commit 92ac19f

Please sign in to comment.