github.com/hyperledger/aries-framework-go@v0.3.2/pkg/client/messaging/msgservice_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package messaging
     8  
     9  import (
    10  	"encoding/json"
    11  	"fmt"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
    18  )
    19  
    20  func TestMsgService_AcceptAndName(t *testing.T) {
    21  	tests := []struct {
    22  		name     string
    23  		args     *testArgs
    24  		testdata []struct {
    25  			msgtype string
    26  			purpose []string
    27  			result  bool
    28  		}
    29  	}{
    30  		{
    31  			name: "msgService accept with message type and purpose",
    32  			args: &testArgs{Name: "test-01", Type: "msg-type-01", Purpose: []string{"prp-01-01", "prp-01-02"}},
    33  			testdata: []struct {
    34  				msgtype string
    35  				purpose []string
    36  				result  bool
    37  			}{
    38  				{
    39  					"msg-type-01", []string{"prp-01-01", "prp-01-02"}, true,
    40  				},
    41  				{
    42  					"msg-type-01", []string{"prp-01-02"}, true,
    43  				},
    44  				{
    45  					"msg-type-01", []string{"prp-01-01"}, true,
    46  				},
    47  				{
    48  
    49  					"msg-type-01", []string{"prp-01-01", "prp-01-03", "prp-01-04"}, true,
    50  				},
    51  				{
    52  					"", []string{"prp-01-01", "prp-01-02"}, false,
    53  				},
    54  				{
    55  					"", []string{"prp-01-02"}, false,
    56  				},
    57  				{
    58  					"msg-type-01", nil, false,
    59  				},
    60  				{
    61  					"msg-type-02", []string{"prp-02-01", "prp-02-02"}, false,
    62  				},
    63  			},
    64  		},
    65  		{
    66  			name: "msgService accept success with only purposes",
    67  			args: &testArgs{Name: "test-01", Purpose: []string{"prp-01-01", "prp-01-02"}},
    68  			testdata: []struct {
    69  				msgtype string
    70  				purpose []string
    71  				result  bool
    72  			}{
    73  				{
    74  
    75  					"msg-type-01", []string{"prp-01-01", "prp-01-02"}, true,
    76  				},
    77  				{
    78  					"msg-type-01", []string{"prp-01-02"}, true,
    79  				},
    80  				{
    81  					"msg-type-01", []string{"prp-01-01"}, true,
    82  				},
    83  				{
    84  					"msg-type-01", []string{"prp-01-01", "prp-01-03", "prp-01-04"}, true,
    85  				},
    86  				{
    87  					"", []string{"prp-01-01", "prp-01-02"}, true,
    88  				},
    89  				{
    90  					"", []string{"prp-01-02"}, true,
    91  				},
    92  				{
    93  					"", []string{"prp-02-01", "prp-02-02"}, false,
    94  				},
    95  				{
    96  					"msg-type-01", nil, false,
    97  				},
    98  				{
    99  					"msg-type-02", []string{"prp-02-01", "prp-02-02"}, false,
   100  				},
   101  			},
   102  		},
   103  		{
   104  			name: "msgService accept success with only message type",
   105  			args: &testArgs{Name: "test-01", Type: "msg-type-01"},
   106  			testdata: []struct {
   107  				msgtype string
   108  				purpose []string
   109  				result  bool
   110  			}{
   111  				{
   112  					"msg-type-01", []string{"prp-01-01", "prp-01-02"}, true,
   113  				},
   114  				{
   115  					"msg-type-01", []string{"prp-01-02"}, true,
   116  				},
   117  				{
   118  					"", []string{"prp-01-01", "prp-01-02"}, false,
   119  				},
   120  				{
   121  					"", []string{"prp-01-02"}, false,
   122  				},
   123  				{
   124  					"msg-type-02", nil, false,
   125  				},
   126  			},
   127  		},
   128  		{
   129  			name: "msgService accept failure with no criteria",
   130  			args: &testArgs{Name: "test-01"},
   131  			testdata: []struct {
   132  				msgtype string
   133  				purpose []string
   134  				result  bool
   135  			}{
   136  				{
   137  					"msg-type-01", []string{"prp-01-01", "prp-01-02"}, false,
   138  				},
   139  				{
   140  					"msg-type-01", []string{"prp-01-02"}, false,
   141  				},
   142  				{
   143  					"", []string{"prp-01-01", "prp-01-02"}, false,
   144  				},
   145  				{
   146  					"msg-type-02", nil, false,
   147  				},
   148  			},
   149  		},
   150  	}
   151  
   152  	t.Parallel()
   153  
   154  	for _, test := range tests {
   155  		tc := test
   156  		t.Run(tc.name, func(t *testing.T) {
   157  			msgsvc := newMessageService(tc.args.Name, tc.args.Type, tc.args.Purpose, nil)
   158  			require.NotNil(t, msgsvc)
   159  			require.Equal(t, tc.args.Name, msgsvc.Name())
   160  
   161  			for _, testdata := range tc.testdata {
   162  				require.Equal(t, testdata.result, msgsvc.Accept(testdata.msgtype, testdata.purpose),
   163  					"test failed header[%s,%s] and criteria[%s]; expected[%v]",
   164  					testdata.msgtype, testdata.purpose, tc.args, testdata.result)
   165  			}
   166  		})
   167  	}
   168  }
   169  
   170  func TestMsgService_HandleInbound(t *testing.T) {
   171  	const (
   172  		sampleName = "sample-msgsvc-01"
   173  		myDID      = "sample-mydid-01"
   174  		theirDID   = "sample-theriDID-01"
   175  	)
   176  
   177  	t.Run("test message service handle inbound success with generic topic", func(t *testing.T) {
   178  		webhookCh := make(chan []byte)
   179  
   180  		msgsvc := newMessageService(sampleName, "", nil,
   181  			&mockNotifier{
   182  				NotifyFunc: func(topic string, message []byte) error {
   183  					require.Equal(t, sampleName, topic)
   184  					webhookCh <- message
   185  					return nil
   186  				},
   187  			})
   188  		require.NotNil(t, msgsvc)
   189  
   190  		go func() {
   191  			s, err := msgsvc.HandleInbound(
   192  				service.DIDCommMsgMap{"payload": sampleName}, service.NewDIDCommContext(myDID, theirDID, nil))
   193  			require.NoError(t, err)
   194  			require.Empty(t, s)
   195  		}()
   196  
   197  		select {
   198  		case msgBytes := <-webhookCh:
   199  			require.NotEmpty(t, msgBytes)
   200  
   201  			msg := mockTopic{}
   202  			err := json.Unmarshal(msgBytes, &msg)
   203  			require.NoError(t, err)
   204  
   205  			require.NotNil(t, msg.Message)
   206  			require.Equal(t, msg.Message["payload"], sampleName)
   207  			require.Equal(t, msg.MyDID, myDID)
   208  			require.Equal(t, msg.TheirDID, theirDID)
   209  
   210  		case <-time.After(2 * time.Second):
   211  			require.Fail(t, "didn't receive topic [%s] to webhook", sampleName)
   212  		}
   213  	})
   214  
   215  	t.Run("message service handle inbound failure", func(t *testing.T) {
   216  		msgsvc := newMessageService("", "", nil, &mockNotifier{})
   217  		s, err := msgsvc.HandleInbound(
   218  			service.DIDCommMsgMap{"payload": []byte(sampleName)}, service.NewDIDCommContext(myDID, theirDID, nil))
   219  		require.Error(t, err)
   220  		require.Contains(t, err.Error(), errTopicNotFound)
   221  		require.Empty(t, s)
   222  	})
   223  
   224  	t.Run("message service handle inbound topic handle failure", func(t *testing.T) {
   225  		const sampleErr = "sample topic error"
   226  		topicHandle := func(service.DIDCommMsg, service.DIDCommContext) ([]byte, error) {
   227  			return nil, fmt.Errorf(sampleErr)
   228  		}
   229  
   230  		msgsvc := newCustomMessageService(sampleName, "test", nil, &mockNotifier{}, topicHandle)
   231  		s, err := msgsvc.HandleInbound(
   232  			service.DIDCommMsgMap{"payload": []byte(sampleName)}, service.NewDIDCommContext(myDID, theirDID, nil))
   233  		require.Error(t, err)
   234  		require.Contains(t, err.Error(), sampleErr)
   235  		require.Empty(t, s)
   236  	})
   237  }
   238  
   239  // mockTopic mock topic from message service handler.
   240  type mockTopic struct {
   241  	Message  service.DIDCommMsgMap `json:"message"`
   242  	MyDID    string                `json:"mydid"`
   243  	TheirDID string                `json:"theirdid"`
   244  }
   245  
   246  type testArgs struct {
   247  	Name    string
   248  	Purpose []string
   249  	Type    string
   250  }