github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/actiontransfer_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 "errors" 9 "testing" 10 11 "github.com/ethereum/go-ethereum/accounts/keystore" 12 "github.com/golang/mock/gomock" 13 "github.com/iotexproject/iotex-address/address" 14 "github.com/iotexproject/iotex-proto/golang/iotexapi" 15 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 16 "github.com/iotexproject/iotex-proto/golang/iotextypes" 17 "github.com/stretchr/testify/require" 18 19 "github.com/iotexproject/iotex-core/ioctl/config" 20 "github.com/iotexproject/iotex-core/ioctl/util" 21 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 22 ) 23 24 func TestNewActionTransferCmd(t *testing.T) { 25 require := require.New(t) 26 ctrl := gomock.NewController(t) 27 client := mock_ioctlclient.NewMockClient(ctrl) 28 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 29 30 ks := keystore.NewKeyStore(t.TempDir(), 2, 1) 31 acc, err := ks.NewAccount("") 32 require.NoError(err) 33 accAddr, err := address.FromBytes(acc.Address.Bytes()) 34 require.NoError(err) 35 36 client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() 37 client.EXPECT().Alias(gomock.Any()).Return("producer", nil).Times(10) 38 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes() 39 client.EXPECT().IsCryptoSm2().Return(false).AnyTimes() 40 client.EXPECT().ReadSecret().Return("", nil).AnyTimes() 41 client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(17) 42 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(7) 43 client.EXPECT().NewKeyStore().Return(ks).AnyTimes() 44 client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(5) 45 client.EXPECT().Config().Return(config.Config{ 46 Explorer: "iotexscan", 47 Endpoint: "testnet1", 48 }).Times(10) 49 50 accountResp := &iotexapi.GetAccountResponse{ 51 AccountMeta: &iotextypes.AccountMeta{ 52 IsContract: false, 53 PendingNonce: 10, 54 Balance: "100000000000000000000", 55 }, 56 } 57 chainMetaResp := &iotexapi.GetChainMetaResponse{ 58 ChainMeta: &iotextypes.ChainMeta{ 59 ChainID: 0, 60 }, 61 } 62 sendActionResp := &iotexapi.SendActionResponse{} 63 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil).Times(18) 64 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResp, nil).AnyTimes() 65 apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(sendActionResp, nil).Times(5) 66 67 t.Run("action transfer", func(t *testing.T) { 68 cmd := NewActionTransferCmd(client) 69 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String()) 70 require.NoError(err) 71 }) 72 73 t.Run("invalid amount", func(t *testing.T) { 74 expectedErr := errors.New("invalid amount") 75 cmd := NewActionTransferCmd(client) 76 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10.00.00", "--signer", accAddr.String()) 77 require.Contains(err.Error(), expectedErr.Error()) 78 }) 79 80 t.Run("action transfer with payload", func(t *testing.T) { 81 payload := "0a10080118a08d062202313062040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a4161e219c2c5d5987f8a9efa33e8df0cde9d5541689fff05784cdc24f12e9d9ee8283a5aa720f494b949535b7969c07633dfb68c4ef9359eb16edb9abc6ebfadc801" 82 cmd := NewActionTransferCmd(client) 83 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", payload, "--signer", accAddr.String()) 84 require.NoError(err) 85 }) 86 87 t.Run("failed to decode data", func(t *testing.T) { 88 expectedErr := errors.New("failed to decode data") 89 90 cmd := NewActionTransferCmd(client) 91 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", "test", "--signer", accAddr.String()) 92 require.Contains(err.Error(), expectedErr.Error()) 93 }) 94 95 t.Run("zero gas limit", func(t *testing.T) { 96 payload := "0a10080118a08d062202313062040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a4161e219c2c5d5987f8a9efa33e8df0cde9d5541689fff05784cdc24f12e9d9ee8283a5aa720f494b949535b7969c07633dfb68c4ef9359eb16edb9abc6ebfadc801" 97 98 cmd := NewActionTransferCmd(client) 99 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", payload, "--signer", accAddr.String(), "--gas-limit", "0") 100 require.NoError(err) 101 }) 102 103 t.Run("action transfer with zero gas price", func(t *testing.T) { 104 apiServiceClient.EXPECT().SuggestGasPrice(gomock.Any(), gomock.Any()).Return(&iotexapi.SuggestGasPriceResponse{ 105 GasPrice: 10, 106 }, nil) 107 108 cmd := NewActionTransferCmd(client) 109 result, err := util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String(), "--gas-price", "") 110 require.NoError(err) 111 require.Contains(result, "Action has been sent to blockchain.\nWait for several seconds and query this action by hash") 112 }) 113 114 t.Run("failed to get gas price", func(t *testing.T) { 115 expectedErr := errors.New("failed to get gas price") 116 apiServiceClient.EXPECT().SuggestGasPrice(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 117 118 cmd := NewActionTransferCmd(client) 119 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String(), "--gas-price", "") 120 require.Contains(err.Error(), expectedErr.Error()) 121 }) 122 123 t.Run("action transfer with nonce", func(t *testing.T) { 124 cmd := NewActionTransferCmd(client) 125 result, err := util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String(), "--nonce", "10") 126 require.NoError(err) 127 require.Contains(result, "Action has been sent to blockchain.\nWait for several seconds and query this action by hash") 128 }) 129 130 t.Run("failed to get account meta", func(t *testing.T) { 131 expectedErr := errors.New("failed to get account meta") 132 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 133 134 cmd := NewActionTransferCmd(client) 135 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String()) 136 require.Contains(err.Error(), expectedErr.Error()) 137 }) 138 139 t.Run("use 'ioctl contract' command instead", func(t *testing.T) { 140 expectedErr := errors.New("use 'ioctl contract' command instead") 141 accountResp := &iotexapi.GetAccountResponse{ 142 AccountMeta: &iotextypes.AccountMeta{ 143 IsContract: true, 144 }, 145 } 146 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil) 147 148 cmd := NewActionTransferCmd(client) 149 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String()) 150 require.Equal(expectedErr.Error(), err.Error()) 151 }) 152 153 t.Run("failed to get nonce", func(t *testing.T) { 154 expectedErr := errors.New("failed to get nonce") 155 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 156 157 cmd := NewActionTransferCmd(client) 158 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String()) 159 require.Contains(err.Error(), expectedErr.Error()) 160 }) 161 162 t.Run("failed to get signed address", func(t *testing.T) { 163 expectedErr := errors.New("failed to get signed address") 164 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectedErr) 165 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil) 166 167 cmd := NewActionTransferCmd(client) 168 _, err = util.ExecuteCmd(cmd, accAddr.String(), "10", "--signer", accAddr.String()) 169 require.Contains(err.Error(), expectedErr.Error()) 170 }) 171 172 t.Run("failed to get recipient address", func(t *testing.T) { 173 expectedErr := errors.New("failed to get recipient address") 174 client.EXPECT().Address(gomock.Any()).Return("", expectedErr) 175 176 cmd := NewActionTransferCmd(client) 177 _, err = util.ExecuteCmd(cmd, "0", "10", "--signer", accAddr.String()) 178 require.Contains(err.Error(), expectedErr.Error()) 179 }) 180 }