github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/actionhash_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 package action 6 7 import ( 8 "context" 9 "encoding/hex" 10 "testing" 11 12 "github.com/golang/mock/gomock" 13 "github.com/iotexproject/iotex-proto/golang/iotexapi" 14 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 15 "github.com/iotexproject/iotex-proto/golang/iotextypes" 16 "github.com/pkg/errors" 17 "github.com/stretchr/testify/require" 18 19 "github.com/iotexproject/iotex-core/action" 20 "github.com/iotexproject/iotex-core/ioctl/config" 21 "github.com/iotexproject/iotex-core/ioctl/util" 22 "github.com/iotexproject/iotex-core/pkg/unit" 23 "github.com/iotexproject/iotex-core/test/identityset" 24 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 25 ) 26 27 var ( 28 _signByte = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9} 29 _pubKeyString = "04403d3c0dbd3270ddfc248c3df1f9aafd60f1d8e7456961c9ef262" + "92262cc68f0ea9690263bef9e197a38f06026814fc70912c2b98d2e90a68f8ddc5328180a01" 30 ) 31 32 func TestNewActionHashCmd(t *testing.T) { 33 require := require.New(t) 34 ctrl := gomock.NewController(t) 35 client := mock_ioctlclient.NewMockClient(ctrl) 36 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 37 38 client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(6) 39 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2) 40 client.EXPECT().Alias(gomock.Any()).Return("producer", nil).Times(3) 41 42 _pubKeyByte, err := hex.DecodeString(_pubKeyString) 43 require.NoError(err) 44 45 t.Run("get action receipt", func(t *testing.T) { 46 getActionResponse := &iotexapi.GetActionsResponse{ 47 ActionInfo: []*iotexapi.ActionInfo{ 48 { 49 Index: 0, 50 ActHash: "test", 51 Action: &iotextypes.Action{ 52 SenderPubKey: _pubKeyByte, 53 Signature: _signByte, 54 Core: createEnvelope(0).Proto(), 55 }, 56 }, 57 }, 58 } 59 getReceiptResponse := &iotexapi.GetReceiptByActionResponse{ 60 ReceiptInfo: &iotexapi.ReceiptInfo{ 61 Receipt: &iotextypes.Receipt{ 62 Status: 1, 63 BlkHeight: 12, 64 ActHash: []byte("9b1d77d8b8902e8d4e662e7cd07d8a74179e032f030d92441ca7fba1ca68e0f4"), 65 GasConsumed: 123, 66 ContractAddress: "test", 67 TxIndex: 1, 68 ExecutionRevertMsg: "balance not enough", 69 }, 70 }, 71 } 72 apiServiceClient.EXPECT().GetActions(context.Background(), gomock.Any()).Return(getActionResponse, nil) 73 apiServiceClient.EXPECT().GetReceiptByAction(context.Background(), gomock.Any()).Return(getReceiptResponse, nil) 74 75 cmd := NewActionHashCmd(client) 76 result, err := util.ExecuteCmd(cmd, "test") 77 require.NoError(err) 78 require.Contains(result, "status: 1 (Success)\n") 79 require.Contains(result, "blkHeight: 12\n") 80 require.Contains(result, "gasConsumed: 123\n") 81 require.Contains(result, "senderPubKey: "+_pubKeyString+"\n") 82 require.Contains(result, "signature: 010203040506070809\n") 83 }) 84 85 t.Run("no action info returned", func(t *testing.T) { 86 getActionResponse := &iotexapi.GetActionsResponse{ 87 ActionInfo: []*iotexapi.ActionInfo{}, 88 } 89 expectedErr := errors.New("no action info returned") 90 apiServiceClient.EXPECT().GetActions(context.Background(), gomock.Any()).Return(getActionResponse, nil) 91 92 cmd := NewActionHashCmd(client) 93 _, err := util.ExecuteCmd(cmd, "test") 94 require.Equal(expectedErr.Error(), err.Error()) 95 }) 96 97 t.Run("failed to dial grpc connection", func(t *testing.T) { 98 expectedErr := errors.New("failed to dial grpc connection") 99 client.EXPECT().APIServiceClient().Return(nil, expectedErr).Times(1) 100 101 cmd := NewActionHashCmd(client) 102 _, err := util.ExecuteCmd(cmd, "test") 103 require.Equal(expectedErr, err) 104 }) 105 } 106 107 func createEnvelope(chainID uint32) action.Envelope { 108 tsf, _ := action.NewTransfer( 109 uint64(10), 110 unit.ConvertIotxToRau(1000+int64(10)), 111 identityset.Address(10%identityset.Size()).String(), 112 nil, 113 20000+uint64(10), 114 unit.ConvertIotxToRau(1+int64(10)), 115 ) 116 eb := action.EnvelopeBuilder{} 117 return eb. 118 SetAction(tsf). 119 SetGasLimit(tsf.GasLimit()). 120 SetGasPrice(tsf.GasPrice()). 121 SetNonce(tsf.Nonce()). 122 SetVersion(1). 123 SetChainID(chainID).Build() 124 }