Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ioctl] Build SendAction unittest #3584

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
244a25c
build SendAction unittest
LuckyPigeon Aug 4, 2022
5b34bbb
fix ci failed
LuckyPigeon Aug 4, 2022
2a81c8e
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 9, 2022
50203de
delete blank line
LuckyPigeon Aug 9, 2022
87e6f7f
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 9, 2022
f86bcea
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 10, 2022
e4d3735
move RegisterWriteCommand into subtest
LuckyPigeon Aug 10, 2022
005055b
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 13, 2022
77e928f
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 16, 2022
f8d5752
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 19, 2022
56aafd0
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 19, 2022
388b9b5
add registerSignerFlag back
LuckyPigeon Aug 19, 2022
b9b85cb
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 23, 2022
b38c085
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 23, 2022
d3e0b2f
replace SelectTranslation with exact times
LuckyPigeon Aug 24, 2022
d9edca3
go fmt
huof6829 Aug 25, 2022
66250f0
Merge branch 'master' into dev_action_unittest_send_action
huof6829 Aug 25, 2022
0d53c0a
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Aug 29, 2022
831b54f
Merge branch 'master' into dev_action_unittest_send_action
LuckyPigeon Sep 5, 2022
2580e4c
Merge branch 'master' into dev_action_unittest_send_action
huof6829 Sep 6, 2022
798a293
Merge branch 'master' into dev_action_unittest_send_action
huof6829 Sep 6, 2022
3f7549f
add cmd flag in test
huof6829 Sep 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions ioctl/newcmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func selectTranslation(client ioctl.Client, trls map[config.Language]string) str

// NewActionCmd represents the action command
func NewActionCmd(client ioctl.Client) *cobra.Command {
ac := &cobra.Command{
cmd := &cobra.Command{
Use: "action",
Short: selectTranslation(client, _actionCmdShorts),
}
Expand All @@ -155,9 +155,10 @@ func NewActionCmd(client ioctl.Client) *cobra.Command {
// cmd.AddCommand(NewActionDeposit(client))
// cmd.AddCommand(NewActionSendRaw(client))

client.SetEndpointWithFlag(ac.PersistentFlags().StringVar)
client.SetInsecureWithFlag(ac.PersistentFlags().BoolVar)
return ac
client.SetEndpointWithFlag(cmd.PersistentFlags().StringVar)
client.SetInsecureWithFlag(cmd.PersistentFlags().BoolVar)

return cmd
}

// RegisterWriteCommand registers action flags for command
Expand Down Expand Up @@ -298,7 +299,7 @@ func SendAction(client ioctl.Client,
) error {
sk, err := account.PrivateKeyFromSigner(client, cmd, signer, password)
if err != nil {
return err
return errors.Wrap(err, "failed to get privateKey")
}

chainMeta, err := bc.GetChainMeta(client)
Expand Down Expand Up @@ -327,12 +328,11 @@ func SendAction(client ioctl.Client,

selp := sealed.Proto()
sk.Zero()
// TODO wait newcmd/action/actionhash impl pr #3425
// actionInfo, err := printActionProto(selp)
// if err != nil {
// return errors.Wrap(err, "failed to print action proto message")
// }
// cmd.Println(actionInfo)
actionInfo, err := printActionProto(client, selp)
if err != nil {
return errors.Wrap(err, "failed to print action proto message")
}
cmd.Println(actionInfo)

if !assumeYes {
infoWarn := selectTranslation(client, _infoWarn)
Expand Down
147 changes: 145 additions & 2 deletions ioctl/newcmd/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package action
import (
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/golang/mock/gomock"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand All @@ -25,7 +27,7 @@ func TestSigner(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(3)
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(2)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {})
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {})

Expand All @@ -51,7 +53,7 @@ func TestSendRaw(t *testing.T) {
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
selp := &iotextypes.Action{}

client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(12)
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(3)
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(7)

for _, test := range []struct {
Expand Down Expand Up @@ -127,3 +129,144 @@ func TestSendRaw(t *testing.T) {
require.Contains(err.Error(), expectedErr.Error())
})
}

func TestSendAction(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
passwd := "123456"

ks := keystore.NewKeyStore(t.TempDir(), 2, 1)
acc, err := ks.NewAccount(passwd)
require.NoError(err)
accAddr, err := address.FromBytes(acc.Address.Bytes())
require.NoError(err)

chainMetaResponse := &iotexapi.GetChainMetaResponse{ChainMeta: &iotextypes.ChainMeta{}}
elp := createEnvelope(0)
cost, err := elp.Cost()
require.NoError(err)
accountResponse := &iotexapi.GetAccountResponse{AccountMeta: &iotextypes.AccountMeta{
Address: accAddr.String(),
Nonce: 1,
PendingNonce: 1,
Balance: cost.String(),
}}

client.EXPECT().SelectTranslation(gomock.Any()).Return("action", config.English).Times(64)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {}).AnyTimes()
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {}).AnyTimes()
client.EXPECT().IsCryptoSm2().Return(false).Times(15)
client.EXPECT().NewKeyStore().Return(ks).Times(15)

t.Run("failed to get privateKey", func(t *testing.T) {
expectedErr := errors.New("failed to get privateKey")
client.EXPECT().ReadSecret().Return("", expectedErr).Times(1)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", "")
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), "", 0, false)
require.Contains(err.Error(), expectedErr.Error())
})

client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes()
client.EXPECT().ReadSecret().Return(passwd, nil).Times(1)
client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(7)
client.EXPECT().Alias(gomock.Any()).Return("producer", nil).Times(10)
client.EXPECT().ReadInput().Return("confirm", nil)
client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(2)
client.EXPECT().Config().Return(config.Config{
Explorer: "iotexscan",
Endpoint: "testnet1",
}).Times(11)

apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).Times(6)
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil).Times(4)
apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(&iotexapi.SendActionResponse{}, nil).Times(4)

t.Run("sends signed action to blockchain", func(t *testing.T) {
cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.NoError(err)
})

t.Run("send action with nonce", func(t *testing.T) {
mnemonic := "lake stove quarter shove dry matrix hire split wide attract argue core"
client.EXPECT().HdwalletMnemonic(gomock.Any()).Return(mnemonic, nil)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, "hdw::1/2", passwd, 1, false)
require.NoError(err)
})

t.Run("quit action command", func(t *testing.T) {
client.EXPECT().AskToConfirm(gomock.Any()).Return(false, nil)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.NoError(err)
})

t.Run("failed to ask confirm", func(t *testing.T) {
expectedErr := errors.New("failed to ask confirm")
client.EXPECT().AskToConfirm(gomock.Any()).Return(false, expectedErr)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})

t.Run("failed to pass balance check", func(t *testing.T) {
expectedErr := errors.New("failed to pass balance check")
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})

t.Run("failed to get nonce", func(t *testing.T) {
mnemonic := "lake stove quarter shove dry matrix hire split wide attract argue core"
expectedErr := errors.New("failed to get nonce")
client.EXPECT().HdwalletMnemonic(gomock.Any()).Return(mnemonic, nil)
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, "hdw::1/2", passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})

t.Run("failed to get chain meta", func(t *testing.T) {
expectedErr := errors.New("failed to get chain meta")
apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})
}