github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/actionclaim_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/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 TestNewActionClaimCmd(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(7) 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(3) 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("action claim", func(t *testing.T) { 69 cmd := NewActionClaimCmd(client) 70 result, err := util.ExecuteCmd(cmd, "0") 71 require.NoError(err) 72 require.Contains(result, "Action has been sent to blockchain") 73 }) 74 75 t.Run("action claim with payload", func(t *testing.T) { 76 payload := "0a10080118a08d062202313062040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a4161e219c2c5d5987f8a9efa33e8df0cde9d5541689fff05784cdc24f12e9d9ee8283a5aa720f494b949535b7969c07633dfb68c4ef9359eb16edb9abc6ebfadc801" 77 cmd := NewActionClaimCmd(client) 78 result, err := util.ExecuteCmd(cmd, "0", payload, "--gas-limit", "0") 79 require.NoError(err) 80 require.Contains(result, "Action has been sent to blockchain") 81 }) 82 83 t.Run("failed to get nonce", func(t *testing.T) { 84 expectedErr := errors.New("failed to get nonce") 85 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 86 cmd := NewActionClaimCmd(client) 87 _, err := util.ExecuteCmd(cmd, "0") 88 require.Contains(err.Error(), expectedErr.Error()) 89 }) 90 91 t.Run("failed to get signer address", func(t *testing.T) { 92 expectedErr := errors.New("failed to get signer address") 93 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectedErr) 94 cmd := NewActionClaimCmd(client) 95 _, err := util.ExecuteCmd(cmd, "0") 96 require.Contains(err.Error(), expectedErr.Error()) 97 }) 98 99 t.Run("invalid amount", func(t *testing.T) { 100 expectedErr := errors.New("invalid amount") 101 cmd := NewActionClaimCmd(client) 102 _, err := util.ExecuteCmd(cmd, "") 103 require.Contains(err.Error(), expectedErr.Error()) 104 }) 105 }