github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/stake2withdraw_test.go (about) 1 // Copyright (c) 2022 IoTeX 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/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/pkg/errors" 18 "github.com/stretchr/testify/require" 19 20 "github.com/iotexproject/iotex-core/ioctl/config" 21 "github.com/iotexproject/iotex-core/ioctl/util" 22 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 23 ) 24 25 func TestNewStake2WithdrawCmd(t *testing.T) { 26 require := require.New(t) 27 ctrl := gomock.NewController(t) 28 client := mock_ioctlclient.NewMockClient(ctrl) 29 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 30 31 ks := keystore.NewKeyStore(t.TempDir(), 2, 1) 32 acc, err := ks.NewAccount("") 33 require.NoError(err) 34 accAddr, err := address.FromBytes(acc.Address.Bytes()) 35 require.NoError(err) 36 37 client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes() 38 client.EXPECT().Alias(gomock.Any()).Return("producer", nil).Times(2) 39 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes() 40 client.EXPECT().IsCryptoSm2().Return(false).Times(8) 41 client.EXPECT().ReadSecret().Return("", nil).Times(2) 42 client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(2) 43 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(4) 44 client.EXPECT().NewKeyStore().Return(ks).Times(4) 45 client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(2) 46 client.EXPECT().Config().Return(config.Config{ 47 Explorer: "iotexscan", 48 Endpoint: "testnet1", 49 }).AnyTimes() 50 51 accountResp := &iotexapi.GetAccountResponse{ 52 AccountMeta: &iotextypes.AccountMeta{ 53 IsContract: false, 54 PendingNonce: 10, 55 Balance: "100000000000000000000", 56 }, 57 } 58 chainMetaResp := &iotexapi.GetChainMetaResponse{ 59 ChainMeta: &iotextypes.ChainMeta{ 60 ChainID: 0, 61 }, 62 } 63 sendActionResp := &iotexapi.SendActionResponse{} 64 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil).Times(4) 65 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResp, nil).Times(2) 66 apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(sendActionResp, nil).Times(2) 67 68 t.Run("stake to withdraw", func(t *testing.T) { 69 cmd := NewStake2WithdrawCmd(client) 70 result, err := util.ExecuteCmd(cmd, "10", "--signer", accAddr.String()) 71 require.NoError(err) 72 require.Contains(result, "Action has been sent to blockchain.") 73 }) 74 75 t.Run("stake to withdraw with payload", func(t *testing.T) { 76 payload := "0a10080118a08d062202313062040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a4161e219c2c5d5987f8a9efa33e8df0cde9d5541689fff05784cdc24f12e9d9ee8283a5aa720f494b949535b7969c07633dfb68c4ef9359eb16edb9abc6ebfadc801" 77 78 cmd := NewStake2WithdrawCmd(client) 79 result, err := util.ExecuteCmd(cmd, "10", payload, "--signer", accAddr.String()) 80 require.NoError(err) 81 require.Contains(result, "Action has been sent to blockchain.") 82 }) 83 84 t.Run("failed to convert bucket index", func(t *testing.T) { 85 expectedErr := errors.New("failed to convert bucket index") 86 87 cmd := NewStake2WithdrawCmd(client) 88 _, err := util.ExecuteCmd(cmd, "test", "--signer", accAddr.String()) 89 require.Contains(err.Error(), expectedErr.Error()) 90 }) 91 92 t.Run("failed to decode data", func(t *testing.T) { 93 expectedErr := errors.New("failed to decode data") 94 95 cmd := NewStake2WithdrawCmd(client) 96 _, err := util.ExecuteCmd(cmd, "10", "test", "--signer", accAddr.String()) 97 require.Contains(err.Error(), expectedErr.Error()) 98 }) 99 100 t.Run("failed to get gas price", func(t *testing.T) { 101 expectedErr := errors.New("failed to get gas price") 102 apiServiceClient.EXPECT().SuggestGasPrice(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 103 104 cmd := NewStake2WithdrawCmd(client) 105 _, err = util.ExecuteCmd(cmd, "10", "--signer", accAddr.String(), "--gas-price", "") 106 require.Contains(err.Error(), expectedErr.Error()) 107 }) 108 109 t.Run("failed to get nonce", func(t *testing.T) { 110 expectedErr := errors.New("failed to get nonce") 111 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 112 113 cmd := NewStake2WithdrawCmd(client) 114 _, err = util.ExecuteCmd(cmd, "10", "--signer", accAddr.String()) 115 require.Contains(err.Error(), expectedErr.Error()) 116 }) 117 118 t.Run("failed to get signed address", func(t *testing.T) { 119 expectedErr := errors.New("failed to get signed address") 120 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectedErr) 121 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResp, nil).AnyTimes() 122 123 cmd := NewStake2WithdrawCmd(client) 124 _, err = util.ExecuteCmd(cmd, "10", "--signer", accAddr.String()) 125 require.Contains(err.Error(), expectedErr.Error()) 126 }) 127 }