github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/actionsendraw_test.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package action
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/golang/mock/gomock"
    12  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
    13  	"github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi"
    14  	"github.com/pkg/errors"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/iotexproject/iotex-core/ioctl/config"
    18  	"github.com/iotexproject/iotex-core/ioctl/util"
    19  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    20  )
    21  
    22  func TestNewActionSendRawCmd(t *testing.T) {
    23  	require := require.New(t)
    24  	ctrl := gomock.NewController(t)
    25  	client := mock_ioctlclient.NewMockClient(ctrl)
    26  	apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
    27  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
    28  
    29  	t.Run("action send raw", func(t *testing.T) {
    30  		client.EXPECT().APIServiceClient().Return(apiServiceClient, nil)
    31  		client.EXPECT().Config().Return(config.Config{
    32  			Explorer: "iotexscan",
    33  			Endpoint: "testnet1",
    34  		}).Times(2)
    35  		apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(&iotexapi.SendActionResponse{}, nil)
    36  		actBytes := "0a12080118a08d0622023130280162040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a41328c6912fa0e36414c38089c03e2fa8c88bba82ccc4ce5fb8ac4ef9f529dfce249a5b2f93a45b818e7f468a742b4e87be3b8077f95d1b3c49e9165b971848ead01"
    37  		cmd := NewActionSendRawCmd(client)
    38  		result, err := util.ExecuteCmd(cmd, actBytes)
    39  		require.NoError(err)
    40  		require.Contains(result, "Action has been sent to blockchain")
    41  	})
    42  
    43  	t.Run("failed to unmarshal data bytes", func(t *testing.T) {
    44  		expectedErr := errors.New("failed to unmarshal data bytes")
    45  		cmd := NewActionSendRawCmd(client)
    46  		_, err := util.ExecuteCmd(cmd, "02e940dd0fd5b5df4cfb8d6bcd9c74ec433e9a5c21acb72cbcb5be9e711b678f")
    47  		require.Contains(err.Error(), expectedErr.Error())
    48  	})
    49  
    50  	t.Run("failed to decode data", func(t *testing.T) {
    51  		expectedErr := errors.New("failed to decode data")
    52  		cmd := NewActionSendRawCmd(client)
    53  		_, err := util.ExecuteCmd(cmd, "test")
    54  		require.Contains(err.Error(), expectedErr.Error())
    55  	})
    56  }